]> git.sesse.net Git - qscale/blob - qscale.c
Move scaling into libqscale.
[qscale] / qscale.c
1 /*
2  * qscale: Quick, high-quality JPEG-to-JPEG scaler.
3  * Copyright (C) 2008 Steinar H. Gunderson <sgunderson@bigfoot.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 2 of the License.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
17  */
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include "jpeglib.h"
23 #include "libqscale.h"
24
25 int main(int argc, char **argv)
26 {
27         /* user-settable parameters */
28         unsigned nominal_w = atoi(argv[1]);
29         unsigned nominal_h = atoi(argv[2]);
30         unsigned samp_h0 = 2, samp_v0 = 2;
31         unsigned samp_h1 = 1, samp_v1 = 1;
32         unsigned samp_h2 = 1, samp_v2 = 1;
33         unsigned jpeg_quality = 85;
34         /* end */
35
36         qscale_img *img = qscale_load_jpeg_from_stdio(stdin);
37         qscale_img *scaled = qscale_scale(img, nominal_w, nominal_h, samp_h0, samp_v0, samp_h1, samp_v1, samp_h2, samp_v2, LANCZOS);
38         qscale_destroy(img);
39         
40         struct jpeg_compress_struct cinfo;
41         struct jpeg_error_mgr jerr;
42         cinfo.err = jpeg_std_error(&jerr);
43         jpeg_create_compress(&cinfo);
44         jpeg_stdio_dest(&cinfo, stdout);
45         cinfo.input_components = 3;
46         jpeg_set_defaults(&cinfo);
47         jpeg_set_quality(&cinfo, jpeg_quality, FALSE);
48         cinfo.image_width = nominal_w;
49         cinfo.image_height = nominal_h;
50         cinfo.raw_data_in = TRUE;
51         jpeg_set_colorspace(&cinfo, JCS_YCbCr);
52         cinfo.comp_info[0].h_samp_factor = samp_h0;
53         cinfo.comp_info[0].v_samp_factor = samp_v0;
54         cinfo.comp_info[1].h_samp_factor = samp_h1;
55         cinfo.comp_info[1].v_samp_factor = samp_v1;
56         cinfo.comp_info[2].h_samp_factor = samp_h2;
57         cinfo.comp_info[2].v_samp_factor = samp_v2;
58         jpeg_start_compress(&cinfo, TRUE);
59
60         unsigned dstride0 = (scaled->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
61         unsigned dstride1 = (scaled->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
62         unsigned dstride2 = (scaled->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
63
64         int total_lines = 0;
65         int blocks = 0;
66         while (total_lines < cinfo.comp_info[0].height_in_blocks * DCTSIZE) {
67                 unsigned max_lines = cinfo.max_v_samp_factor * DCTSIZE;
68
69                 JSAMPROW y_row_ptrs[max_lines];
70                 JSAMPROW cb_row_ptrs[max_lines];
71                 JSAMPROW cr_row_ptrs[max_lines];
72                 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
73                 int i;
74
75                 for (i = 0; i < max_lines; ++i) {
76                         /* simple edge extension */
77                         int yline = i + blocks*DCTSIZE*cinfo.comp_info[0].v_samp_factor;
78                         if (yline > scaled->h0 - 1)
79                                 yline = scaled->h0 - 1;
80
81                         int cbline = i + blocks*DCTSIZE*cinfo.comp_info[1].v_samp_factor;
82                         if (cbline > scaled->h1 - 1)
83                                 cbline = scaled->h2 - 1;
84
85                         int crline = i + blocks*DCTSIZE*cinfo.comp_info[2].v_samp_factor;
86                         if (crline > scaled->h2 - 1)
87                                 crline = scaled->h2 - 1;
88
89                         y_row_ptrs[i]  = scaled->data_y  + yline * dstride0;
90                         cb_row_ptrs[i] = scaled->data_cb + cbline * dstride1;
91                         cr_row_ptrs[i] = scaled->data_cr + crline * dstride2;
92                 }
93                 
94                 total_lines += max_lines;
95                 ++blocks;
96
97                 jpeg_write_raw_data(&cinfo, ptrs, max_lines);
98         }
99         jpeg_finish_compress(&cinfo);
100         jpeg_destroy_compress(&cinfo);
101
102         return 0;
103 }
104