+/*
+ * qscale: Quick, high-quality JPEG-to-JPEG scaler.
+ * Copyright (C) 2008 Steinar H. Gunderson <sgunderson@bigfoot.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
img->w2 = dinfo.image_width * dinfo.comp_info[2].h_samp_factor / dinfo.max_h_samp_factor;
img->h2 = dinfo.image_height * dinfo.comp_info[2].v_samp_factor / dinfo.max_v_samp_factor;
+ img->samp_h0 = dinfo.comp_info[0].h_samp_factor;
+ img->samp_v0 = dinfo.comp_info[0].v_samp_factor;
+
+ img->samp_h1 = dinfo.comp_info[1].h_samp_factor;
+ img->samp_v1 = dinfo.comp_info[1].v_samp_factor;
+
+ img->samp_h2 = dinfo.comp_info[2].h_samp_factor;
+ img->samp_v2 = dinfo.comp_info[2].v_samp_factor;
+
img->data_y = (JSAMPLE*)memalign(16, dinfo.comp_info[0].height_in_blocks * dinfo.comp_info[0].width_in_blocks * DCTSIZE * DCTSIZE);
img->data_cb = (JSAMPLE*)memalign(16, dinfo.comp_info[1].height_in_blocks * dinfo.comp_info[1].width_in_blocks * DCTSIZE * DCTSIZE);
img->data_cr = (JSAMPLE*)memalign(16, dinfo.comp_info[2].height_in_blocks * dinfo.comp_info[2].width_in_blocks * DCTSIZE * DCTSIZE);
dst->w2 = width * samp_h2 / max_samp_h;
dst->h2 = height * samp_v2 / max_samp_v;
+ dst->samp_h0 = samp_h0;
+ dst->samp_v0 = samp_v0;
+
+ dst->samp_h1 = samp_h1;
+ dst->samp_v1 = samp_v1;
+
+ dst->samp_h2 = samp_h2;
+ dst->samp_v2 = samp_v2;
+
unsigned dstride0 = (dst->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
unsigned dstride1 = (dst->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
unsigned dstride2 = (dst->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
return dst;
}
+
+int qscale_save_jpeg(const qscale_img *img, const char *filename, unsigned jpeg_quality, enum qscale_jpeg_mode jpeg_mode)
+{
+ FILE *file = fopen(filename, "wb");
+ if (file == NULL) {
+ return -1;
+ }
+
+ int err = qscale_save_jpeg_to_stdio(img, file, jpeg_quality, jpeg_mode);
+
+ fclose(file);
+ return err;
+}
+
+int qscale_save_jpeg_to_stdio(const qscale_img *img, FILE *file, unsigned jpeg_quality, enum qscale_jpeg_mode jpeg_mode)
+{
+ struct jpeg_compress_struct cinfo;
+ struct jpeg_error_mgr jerr;
+ cinfo.err = jpeg_std_error(&jerr);
+ jpeg_create_compress(&cinfo);
+ jpeg_stdio_dest(&cinfo, stdout);
+ cinfo.input_components = 3;
+ jpeg_set_defaults(&cinfo);
+ jpeg_set_quality(&cinfo, jpeg_quality, FALSE);
+ cinfo.image_width = img->width;
+ cinfo.image_height = img->height;
+ cinfo.raw_data_in = TRUE;
+ jpeg_set_colorspace(&cinfo, JCS_YCbCr);
+ cinfo.comp_info[0].h_samp_factor = img->samp_h0;
+ cinfo.comp_info[0].v_samp_factor = img->samp_v0;
+ cinfo.comp_info[1].h_samp_factor = img->samp_h1;
+ cinfo.comp_info[1].v_samp_factor = img->samp_v1;
+ cinfo.comp_info[2].h_samp_factor = img->samp_h2;
+ cinfo.comp_info[2].v_samp_factor = img->samp_v2;
+ jpeg_start_compress(&cinfo, TRUE);
+
+ unsigned dstride0 = (img->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
+ unsigned dstride1 = (img->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
+ unsigned dstride2 = (img->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
+
+ int total_lines = 0;
+ int blocks = 0;
+ while (total_lines < cinfo.comp_info[0].height_in_blocks * DCTSIZE) {
+ unsigned max_lines = cinfo.max_v_samp_factor * DCTSIZE;
+
+ JSAMPROW y_row_ptrs[max_lines];
+ JSAMPROW cb_row_ptrs[max_lines];
+ JSAMPROW cr_row_ptrs[max_lines];
+ JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
+ int i;
+
+ for (i = 0; i < max_lines; ++i) {
+ /* simple edge extension */
+ int yline = i + blocks*DCTSIZE*cinfo.comp_info[0].v_samp_factor;
+ if (yline > img->h0 - 1)
+ yline = img->h0 - 1;
+
+ int cbline = i + blocks*DCTSIZE*cinfo.comp_info[1].v_samp_factor;
+ if (cbline > img->h1 - 1)
+ cbline = img->h1 - 1;
+
+ int crline = i + blocks*DCTSIZE*cinfo.comp_info[2].v_samp_factor;
+ if (crline > img->h2 - 1)
+ crline = img->h2 - 1;
+
+ y_row_ptrs[i] = img->data_y + yline * dstride0;
+ cb_row_ptrs[i] = img->data_cb + cbline * dstride1;
+ cr_row_ptrs[i] = img->data_cr + crline * dstride2;
+ }
+
+ total_lines += max_lines;
+ ++blocks;
+
+ jpeg_write_raw_data(&cinfo, ptrs, max_lines);
+ }
+ jpeg_finish_compress(&cinfo);
+ jpeg_destroy_compress(&cinfo);
+
+ return 0;
+}