]> git.sesse.net Git - qscale/blob - libqscale.c
Use SSE intrinsics for the horizontal scaling as well.
[qscale] / libqscale.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 <math.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <malloc.h>
24
25 #include "libqscale.h"
26
27 /* The number of pixels to process at a time when scaling vertically. */
28 #define CACHE_LINE_FACTOR 16
29
30 /* Whether to use SSE for horizontal scaling or not (requires SSE3). */
31 #define USE_HORIZONTAL_SSE 1
32
33 /* Whether to use SSE for vertical scaling or not (requires only SSE1). */
34 #define USE_VERTICAL_SSE 1
35
36 #if USE_VERTICAL_SSE
37 #undef CACHE_LINE_FACTOR
38 #define CACHE_LINE_FACTOR 16
39 #endif
40
41 #ifndef M_PI
42 #define M_PI 3.14159265358979323846264
43 #endif
44
45 #if USE_VERTICAL_SSE || USE_HORIZONTAL_SSE
46 typedef float v4sf __attribute__((vector_size(16)));
47 typedef int v4si __attribute__((vector_size(16)));
48 typedef short v8hi __attribute__((vector_size(16)));
49 typedef char v16qi __attribute__((vector_size(16)));
50 #endif
51
52 qscale_img *qscale_load_jpeg(const char *filename)
53 {
54         FILE *file = fopen(filename, "rb");
55         qscale_img *img;
56         if (file == NULL) {
57                 return NULL;
58         }
59
60         img = qscale_load_jpeg_from_stdio(file);
61
62         fclose(file);
63         return img;
64 }
65
66 qscale_img *qscale_load_jpeg_from_stdio(FILE *file)
67 {
68         qscale_img *img = (qscale_img *)malloc(sizeof(qscale_img));
69         if (img == NULL) {
70                 return NULL;
71         }
72
73         img->data_y = img->data_cb = img->data_cr = NULL;
74
75         /* FIXME: Better error handling here (ie., return NULL). */
76         struct jpeg_decompress_struct dinfo;
77         struct jpeg_error_mgr jerr;
78         dinfo.err = jpeg_std_error(&jerr);
79         jpeg_create_decompress(&dinfo);
80         jpeg_stdio_src(&dinfo, file);
81         jpeg_read_header(&dinfo, TRUE);
82         dinfo.raw_data_out = TRUE;
83         jpeg_start_decompress(&dinfo);
84         
85         if (dinfo.num_components != 1 && dinfo.num_components != 3) {
86                 qscale_destroy(img);
87                 return NULL;
88         }
89         img->num_components = dinfo.num_components;
90
91         img->width = dinfo.image_width;
92         img->height = dinfo.image_height;
93
94         img->w0 = dinfo.image_width * dinfo.comp_info[0].h_samp_factor / dinfo.max_h_samp_factor;
95         img->h0 = dinfo.image_height * dinfo.comp_info[0].v_samp_factor / dinfo.max_v_samp_factor;
96
97         if (img->num_components == 3) {
98                 img->w1 = dinfo.image_width * dinfo.comp_info[1].h_samp_factor / dinfo.max_h_samp_factor;
99                 img->h1 = dinfo.image_height * dinfo.comp_info[1].v_samp_factor / dinfo.max_v_samp_factor;
100
101                 img->w2 = dinfo.image_width * dinfo.comp_info[2].h_samp_factor / dinfo.max_h_samp_factor;
102                 img->h2 = dinfo.image_height * dinfo.comp_info[2].v_samp_factor / dinfo.max_v_samp_factor;
103         }
104
105         img->samp_h0 = dinfo.comp_info[0].h_samp_factor;
106         img->samp_v0 = dinfo.comp_info[0].v_samp_factor;
107
108         if (img->num_components == 3) {
109                 img->samp_h1 = dinfo.comp_info[1].h_samp_factor;
110                 img->samp_v1 = dinfo.comp_info[1].v_samp_factor;
111
112                 img->samp_h2 = dinfo.comp_info[2].h_samp_factor;
113                 img->samp_v2 = dinfo.comp_info[2].v_samp_factor;
114         }
115
116         img->data_y  = (JSAMPLE*)memalign(16, dinfo.comp_info[0].height_in_blocks * dinfo.comp_info[0].width_in_blocks * DCTSIZE * DCTSIZE);
117         if (img->data_y == NULL) {
118                 qscale_destroy(img);
119                 return NULL;
120         }
121
122         if (img->num_components == 3) {
123                 img->data_cb = (JSAMPLE*)memalign(16, dinfo.comp_info[1].height_in_blocks * dinfo.comp_info[1].width_in_blocks * DCTSIZE * DCTSIZE);
124                 img->data_cr = (JSAMPLE*)memalign(16, dinfo.comp_info[2].height_in_blocks * dinfo.comp_info[2].width_in_blocks * DCTSIZE * DCTSIZE);
125                 if (img->data_cb == NULL || img->data_cr == NULL) {
126                         qscale_destroy(img);
127                         return NULL;
128                 }
129         }
130
131         int total_lines = 0, blocks = 0;
132         while (total_lines < dinfo.comp_info[0].height_in_blocks * DCTSIZE) {
133                 unsigned max_lines = dinfo.max_v_samp_factor * DCTSIZE;
134
135                 JSAMPROW y_row_ptrs[max_lines];
136                 JSAMPROW cb_row_ptrs[max_lines];
137                 JSAMPROW cr_row_ptrs[max_lines];
138                 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
139
140                 int i;
141                 for (i = 0; i < max_lines; ++i) {
142                         y_row_ptrs[i]  = img->data_y  + (i+blocks*DCTSIZE*dinfo.comp_info[0].v_samp_factor) * dinfo.comp_info[0].width_in_blocks * DCTSIZE;
143                         if (img->num_components == 3) {
144                                 cb_row_ptrs[i] = img->data_cb + (i+blocks*DCTSIZE*dinfo.comp_info[1].v_samp_factor) * dinfo.comp_info[1].width_in_blocks * DCTSIZE;
145                                 cr_row_ptrs[i] = img->data_cr + (i+blocks*DCTSIZE*dinfo.comp_info[2].v_samp_factor) * dinfo.comp_info[2].width_in_blocks * DCTSIZE;
146                         }
147                 }
148
149                 total_lines += max_lines;
150                 ++blocks;
151
152                 if (jpeg_read_raw_data(&dinfo, ptrs, max_lines) == 0)
153                         break;
154         }
155
156         jpeg_destroy_decompress(&dinfo);
157         return img;
158 }
159
160 void qscale_destroy(qscale_img *img)
161 {
162         free(img->data_y);
163         free(img->data_cb);
164         free(img->data_cr);
165         free(img);
166 }
167
168
169 static double sinc(double x)
170 {
171         static const double cutoff = 1.220703668e-4;  /* sqrt(sqrt(eps)) */
172
173         if (abs(x) < cutoff) {
174                 /* For small |x|, use Taylor series instead */
175                 const double x2 = x * x;
176                 const double x4 = x2 * x2;
177
178                 return 1.0 - x2 / 6.0 + x4 / 120.0;
179         } else {
180                 return sin(x) / x;
181         }
182 }
183
184 static double lanczos_tap(double x)
185 {
186         if (x < -3.0 || x > 3.0)
187                 return 0.0;
188         if (x < 0.0)
189                 return sinc(-x*M_PI) * sinc(-x*M_PI / 3.0);
190         else
191                 return sinc(x*M_PI) * sinc(x*M_PI / 3.0);
192 }
193
194 static double mitchell_tap(double x)
195 {
196         const double b = 1.0 / 3.0;
197         const double c = 1.0 / 3.0;
198         const double p0 = (  6.0 -  2.0*b         ) / 6.0;
199         const double p2 = (-18.0 + 12.0*b +  6.0*c) / 6.0;
200         const double p3 = ( 12.0 -  9.0*b -  6.0*c) / 6.0;
201         const double q0 = (         8.0*b + 24.0*c) / 6.0;
202         const double q1 = (      - 12.0*b - 48.0*c) / 6.0;
203         const double q2 = (         6.0*b + 30.0*c) / 6.0;
204         const double q3 = (      -      b -  6.0*c) / 6.0;
205
206         if (x < -2.0) {
207                 return 0.0;
208         } else if (x < -1.0) {
209                 return q0 - x * (q1 - x * (q2 - x * q3));
210         } else if (x < 0.0) {
211                 return p0 + x * x * (p2 - x * p3);
212         } else if (x < 1.0) {
213                 return p0 + x * x * (p2 + x * p3);
214         } else if (x < 2.0) {
215                 return q0 + x * (q1 + x * (q2 + x * q3));
216         } else {
217                 return 0.0;
218         }
219 }
220
221 struct pix_desc {
222         unsigned start, end;
223         unsigned startcoeff;
224 };
225
226 static void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsigned nw, unsigned sstride, unsigned dstride, enum qscale_scaling_filter scaling_filter)
227 {
228         struct pix_desc *pd = (struct pix_desc *)malloc(nw * sizeof(struct pix_desc));
229         int size_coeffs = 8;
230         float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
231         int num_coeffs = 0;
232         int x, y;
233         double sf = (double)w / (double)nw;
234         double support;
235         
236         if (scaling_filter == LANCZOS) {
237                 support = (w > nw) ? (3.0 * sf) : (3.0 / sf);
238         } else {  /* Mitchell */
239                 support = (w > nw) ? (2.0 * sf) : (2.0 / sf);
240         }
241
242         /* calculate the filter */
243         for (x = 0; x < nw; ++x) {
244                 int start = ceil(x * sf - support);
245                 int end = floor(x * sf + support);
246                 int sx;
247                 double sum = 0.0;
248
249                 if (start < 0) {
250                         start = 0;
251                 }
252                 if (end > w - 1) {
253                         end = w - 1;
254                 }
255
256 #if USE_HORIZONTAL_SSE
257                 /* round up so we get a multiple of four for the SSE code */
258                 int num = (end - start + 1);
259                 if (num % 4 != 0) {
260                         /* prefer aligning it if possible */
261                         if (start % 4 != 0 && start % 4 <= num % 4) {
262                                 num += start % 4;
263                                 start -= start % 4;
264                         }
265                         if (num % 4 != 0) {
266                                 end += 4 - (num % 4);
267                         }
268                 }
269 #endif
270
271                 pd[x].start = start;
272                 pd[x].end = end;
273                 pd[x].startcoeff = num_coeffs;
274
275                 for (sx = start; sx <= end; ++sx) {
276                         double nd = (w > nw) ? (sx/sf - x) : (sx - x*sf);
277                         double f;
278                         if (scaling_filter == LANCZOS) {
279                                 f = lanczos_tap(nd);
280                         } else {  /* Mitchell */
281                                 f = mitchell_tap(nd);
282                         }
283                         if (num_coeffs == size_coeffs) {
284                                 size_coeffs <<= 1;
285                                 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
286                         }
287
288                         coeffs[num_coeffs++] = f;
289                         sum += f;
290                 }
291
292                 for (sx = start; sx <= end; ++sx) {
293                         coeffs[pd[x].startcoeff + sx - start] /= sum;
294                 }
295         }
296
297         for (y = 0; y < h; ++y) {
298                 float *sptr = pix + y*sstride;
299                 unsigned char *dptr = npix + y*dstride;
300                 unsigned char ch;
301                 for (x = 0; x < nw; ++x) {
302 #if USE_HORIZONTAL_SSE
303                         v4sf acc = { 0.0f, 0.0f, 0.0f, 0.0f };
304                         static const v4sf low = { 0.0f, 0.0f, 0.0f, 0.0f };
305                         static const v4sf high = { 255.0f, 255.0f, 255.0f, 255.0f };
306                         int result;
307                         int i;
308                 
309                         const float *sptr_xmm = &sptr[pd[x].start];
310                         const float *coeffptr = &coeffs[pd[x].startcoeff];
311                         const int filter_len = (pd[x].end - pd[x].start + 1) / 4;
312
313                         for (i = 0; i < filter_len; ++i) {
314                                 v4sf pixels = __builtin_ia32_loadups(&sptr_xmm[i * 4]);
315                                 v4sf coeffs = __builtin_ia32_loadups(&coeffptr[i * 4]);
316                                 acc = __builtin_ia32_addps(acc, __builtin_ia32_mulps(pixels, coeffs));
317                         }
318                         acc = __builtin_ia32_haddps(acc, acc);  
319                         acc = __builtin_ia32_haddps(acc, acc);
320                         acc = __builtin_ia32_maxss(acc, low);
321                         acc = __builtin_ia32_minss(acc, high);
322                         result = __builtin_ia32_cvtss2si(acc);
323
324                         *dptr++ = (unsigned char)result;
325 #else
326                         float acc = 0.0;
327                         float *cf = &coeffs[pd[x].startcoeff];
328                         unsigned sx;
329                         
330                         for (sx = pd[x].start; sx <= pd[x].end; ++sx) {
331                                 acc += sptr[sx] * *cf++;
332                         }
333
334                         if (acc < 0.0)
335                                 ch = 0;
336                         else if (acc > 255.0)
337                                 ch = 255;
338                         else
339                                 ch = (unsigned char)acc;
340                         *dptr++ = ch;
341 #endif
342                 }
343                 ch = dptr[-1];
344                 for ( ; x < dstride; ++x) {
345                         *dptr++ = ch;
346                 }
347         }
348 }
349
350 static void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsigned nh, unsigned dstride, enum qscale_scaling_filter scaling_filter)
351 {
352         struct pix_desc *pd = (struct pix_desc *)malloc(nh * sizeof(struct pix_desc));
353         int size_coeffs = 8;
354         float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
355         int num_coeffs = 0;
356         int x, y, sy;
357         double sf = (double)h / (double)nh;
358         double support;
359         
360         if (scaling_filter == LANCZOS) {
361                 support = (h > nh) ? (3.0 * sf) : (3.0 / sf);
362         } else {  /* Mitchell */
363                 support = (h > nh) ? (2.0 * sf) : (2.0 / sf);
364         }
365
366         /* calculate the filter */
367         for (y = 0; y < nh; ++y) {
368                 int start = ceil(y * sf - support);
369                 int end = floor(y * sf + support);
370                 double sum = 0.0;
371
372                 if (start < 0) {
373                         start = 0;
374                 }
375                 if (end > h - 1) {
376                         end = h - 1;
377                 }
378
379                 pd[y].start = start;
380                 pd[y].end = end;
381                 pd[y].startcoeff = num_coeffs;
382
383                 for (sy = start; sy <= end; ++sy) {
384                         double nd = (h > nh) ? (sy/sf - y) : (sy - y*sf);
385                         double f;
386                         if (scaling_filter == LANCZOS) {
387                                 f = lanczos_tap(nd);
388                         } else {  /* Mitchell */
389                                 f = mitchell_tap(nd);
390                         }
391                         if (num_coeffs == size_coeffs) {
392                                 size_coeffs <<= 1;
393                                 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
394                         }
395                         
396                         coeffs[num_coeffs++] = f;
397                         sum += f;
398                 }
399
400                 for (sy = start; sy <= end; ++sy) {
401                         coeffs[pd[y].startcoeff + sy - start] /= sum;
402                 }
403         }
404
405 #if CACHE_LINE_FACTOR > 1
406         for (x = 0; x < (w/CACHE_LINE_FACTOR) * CACHE_LINE_FACTOR; x += CACHE_LINE_FACTOR) {
407                 unsigned char *sptr = pix + x;
408                 float *dptr = npix + x;
409                 for (y = 0; y < nh; ++y) {
410 #if USE_VERTICAL_SSE
411                         /* A zero is useful during unpacking. */
412                         static const v4sf zero = { 0.0f, 0.0f, 0.0f, 0.0f };
413                         const unsigned char *sptr_xmm = &sptr[pd[y].start * w];
414                         const float *coeffptr = &coeffs[pd[y].startcoeff];
415                         const int filter_len = pd[y].end - pd[y].start + 1;
416                         int i;
417
418                         v4sf acc0 = { 0.0f, 0.0f, 0.0f, 0.0f };
419                         v4sf acc1 = { 0.0f, 0.0f, 0.0f, 0.0f };
420                         v4sf acc2 = { 0.0f, 0.0f, 0.0f, 0.0f };
421                         v4sf acc3 = { 0.0f, 0.0f, 0.0f, 0.0f };
422                         
423                         for (i = 0; i < filter_len; ++i, ++coeffptr, sptr_xmm += w) {
424                                 __builtin_prefetch(sptr_xmm + w, 0);
425                                 v16qi src = (v16qi)__builtin_ia32_loadups((float*)sptr_xmm);
426
427                                 // unpack into words
428                                 v8hi src_lo = (v8hi)__builtin_ia32_punpcklbw128(src, (v16qi)zero);
429                                 v8hi src_hi = (v8hi)__builtin_ia32_punpckhbw128(src, (v16qi)zero);
430
431                                 // unpack into dwords, convert to floats
432                                 v4si src0_i = (v4si)__builtin_ia32_punpcklwd128(src_lo, (v8hi)zero);
433                                 v4si src1_i = (v4si)__builtin_ia32_punpckhwd128(src_lo, (v8hi)zero);
434                                 v4si src2_i = (v4si)__builtin_ia32_punpcklwd128(src_hi, (v8hi)zero);
435                                 v4si src3_i = (v4si)__builtin_ia32_punpckhwd128(src_hi, (v8hi)zero);
436
437                                 v4sf src0 = __builtin_ia32_cvtdq2ps(src0_i);
438                                 v4sf src1 = __builtin_ia32_cvtdq2ps(src1_i);
439                                 v4sf src2 = __builtin_ia32_cvtdq2ps(src2_i);
440                                 v4sf src3 = __builtin_ia32_cvtdq2ps(src3_i);
441                         
442                                 // fetch the coefficient, and replicate it
443                                 v4sf coeff = { *coeffptr, *coeffptr, *coeffptr, *coeffptr };
444
445                                 // do the actual muladds
446                                 acc0 = __builtin_ia32_addps(acc0, __builtin_ia32_mulps(src0, coeff));
447                                 acc1 = __builtin_ia32_addps(acc1, __builtin_ia32_mulps(src1, coeff));
448                                 acc2 = __builtin_ia32_addps(acc2, __builtin_ia32_mulps(src2, coeff));
449                                 acc3 = __builtin_ia32_addps(acc3, __builtin_ia32_mulps(src3, coeff));
450                         }
451
452                         *(v4sf *)(&dptr[0]) = acc0;
453                         *(v4sf *)(&dptr[4]) = acc1;
454                         *(v4sf *)(&dptr[8]) = acc2;
455                         *(v4sf *)(&dptr[12]) = acc3;
456 #else
457                         int i;
458                         float acc[CACHE_LINE_FACTOR];
459                         for (i = 0; i < CACHE_LINE_FACTOR; ++i)
460                                 acc[i] = 0.0;
461                         float *cf = &coeffs[pd[y].startcoeff];
462                         unsigned sy;
463                 
464                         for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
465                                 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
466                                         acc[i] += sptr[sy * w + i] * *cf;
467                                 }
468                                 ++cf;
469                         }
470
471                         for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
472                                 dptr[i] = acc[i];
473                         }
474 #endif
475                         dptr += dstride;
476                 }
477         }
478         for (x = (x/CACHE_LINE_FACTOR)*CACHE_LINE_FACTOR; x < w; ++x) {
479 #else
480         for (x = 0; x < w; ++x) {
481 #endif
482                 unsigned char *sptr = pix + x;
483                 float *dptr = npix + x;
484                 for (y = 0; y < nh; ++y) {
485                         float acc = 0.0;
486                         float *cf = &coeffs[pd[y].startcoeff];
487                         unsigned sy;
488                         
489                         for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
490                                 acc += sptr[sy * w] * *cf++;
491                         }
492
493                         *dptr = acc;
494                         dptr += dstride;
495                 }
496         }
497 }
498
499 qscale_img *qscale_clone(const qscale_img *img)
500 {
501         qscale_img *dst = (qscale_img *)malloc(sizeof(qscale_img));
502         if (dst == NULL) {
503                 return NULL;
504         }
505
506         *dst = *img;
507
508         unsigned dstride0 = (dst->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
509         unsigned dstride1 = (dst->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
510         unsigned dstride2 = (dst->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
511
512         /* FIXME: handle out-of-memory gracefully */
513         {
514                 dst->data_y = (unsigned char *)malloc(dst->h0 * dstride0);
515                 memcpy(dst->data_y, img->data_y, dst->h0 * dstride0);
516         }
517         {
518                 dst->data_cb = (unsigned char *)malloc(dst->h1 * dstride1);
519                 memcpy(dst->data_cb, img->data_cb, dst->h1 * dstride1);
520         }
521         {
522                 dst->data_cr = (unsigned char *)malloc(dst->h2 * dstride2);
523                 memcpy(dst->data_cr, img->data_cr, dst->h2 * dstride2);
524         }
525
526         return dst;
527 }
528
529 qscale_img *qscale_scale(qscale_img *src, unsigned width, unsigned height, unsigned samp_h0, unsigned samp_v0, unsigned samp_h1, unsigned samp_v1, unsigned samp_h2, unsigned samp_v2, enum qscale_scaling_filter scaling_filter)
530 {
531         qscale_img *dst = (qscale_img *)malloc(sizeof(qscale_img));
532         if (dst == NULL) {
533                 return NULL;
534         }
535
536         dst->width = width;
537         dst->height = height;
538         dst->num_components = src->num_components;
539
540         unsigned max_samp_h, max_samp_v;
541         max_samp_h = samp_h0;
542         if (src->num_components == 3) {
543                 if (samp_h1 > max_samp_h)
544                         max_samp_h = samp_h1;
545                 if (samp_h2 > max_samp_h)
546                         max_samp_h = samp_h2;
547         }
548
549         max_samp_v = samp_v0;
550         if (src->num_components == 3) {
551                 if (samp_v1 > max_samp_v)
552                         max_samp_v = samp_v1;
553                 if (samp_v2 > max_samp_v)
554                         max_samp_v = samp_v2;
555         }
556
557         dst->w0 = width * samp_h0 / max_samp_h;
558         dst->h0 = height * samp_v0 / max_samp_v;
559
560         if (src->num_components == 3) {
561                 dst->w1 = width * samp_h1 / max_samp_h;
562                 dst->h1 = height * samp_v1 / max_samp_v;
563
564                 dst->w2 = width * samp_h2 / max_samp_h;
565                 dst->h2 = height * samp_v2 / max_samp_v;
566         }
567
568         dst->samp_h0 = samp_h0;
569         dst->samp_v0 = samp_v0;
570
571         if (src->num_components == 3) {
572                 dst->samp_h1 = samp_h1;
573                 dst->samp_v1 = samp_v1;
574
575                 dst->samp_h2 = samp_h2;
576                 dst->samp_v2 = samp_v2;
577         }
578
579         unsigned dstride0 = (dst->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
580         unsigned dstride1 = (dst->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
581         unsigned dstride2 = (dst->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
582
583         unsigned sstride0 = (src->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
584         unsigned sstride1 = (src->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
585         unsigned sstride2 = (src->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
586
587         /* FIXME: handle out-of-memory gracefully */
588         {
589                 float *npix = (float*)memalign(16, sstride0 * dst->h0 * sizeof(float));
590                 vscale(src->data_y, npix, sstride0, src->h0, dst->h0, sstride0, scaling_filter);
591                 dst->data_y = (unsigned char *)malloc(dst->h0 * dstride0);
592                 hscale(npix, dst->data_y, src->w0, dst->h0, dst->w0, sstride0, dstride0, scaling_filter);
593                 free(npix);
594         }
595         if (src->num_components == 3) {
596                 {
597                         float *npix = (float*)memalign(16, sstride1 * dst->h1 * sizeof(float));
598                         vscale(src->data_cr, npix, sstride1, src->h1, dst->h1, sstride1, scaling_filter);
599                         dst->data_cr = (unsigned char *)malloc(dst->h1 * dstride1);
600                         hscale(npix, dst->data_cr, src->w1, dst->h1, dst->w1, sstride1, dstride1, scaling_filter);
601                         free(npix);
602                 }
603                 {
604                         float *npix = (float*)memalign(16, sstride2 * dst->h2 * sizeof(float));
605                         vscale(src->data_cb, npix, sstride2, src->h2, dst->h2, sstride2, scaling_filter);
606                         dst->data_cb = (unsigned char *)malloc(dst->h2 * dstride2);
607                         hscale(npix, dst->data_cb, src->w2, dst->h2, dst->w2, sstride2, dstride2, scaling_filter);
608                         free(npix);
609                 }
610         }
611
612         return dst;
613 }
614
615 int qscale_save_jpeg(const qscale_img *img, const char *filename, unsigned jpeg_quality, enum qscale_jpeg_mode jpeg_mode)
616 {
617         FILE *file = fopen(filename, "wb");
618         if (file == NULL) {
619                 return -1;
620         }
621
622         int err = qscale_save_jpeg_to_stdio(img, file, jpeg_quality, jpeg_mode);
623
624         fclose(file);
625         return err;
626 }
627
628 int qscale_save_jpeg_to_stdio(const qscale_img *img, FILE *file, unsigned jpeg_quality, enum qscale_jpeg_mode jpeg_mode)
629 {
630         struct jpeg_compress_struct cinfo;
631         struct jpeg_error_mgr jerr;
632         cinfo.err = jpeg_std_error(&jerr);
633         jpeg_create_compress(&cinfo);
634         jpeg_stdio_dest(&cinfo, file);
635         cinfo.input_components = img->num_components;
636         jpeg_set_defaults(&cinfo);
637         jpeg_set_quality(&cinfo, jpeg_quality, FALSE);
638
639         if (jpeg_mode == PROGRESSIVE) {
640                 jpeg_simple_progression(&cinfo);
641         }
642
643         cinfo.image_width = img->width;
644         cinfo.image_height = img->height;
645         cinfo.raw_data_in = TRUE;
646         if (img->num_components == 3) {
647                 jpeg_set_colorspace(&cinfo, JCS_YCbCr);
648         } else {
649                 jpeg_set_colorspace(&cinfo, JCS_GRAYSCALE);
650         }
651         cinfo.comp_info[0].h_samp_factor = img->samp_h0;
652         cinfo.comp_info[0].v_samp_factor = img->samp_v0;
653         if (img->num_components == 3) {
654                 cinfo.comp_info[1].h_samp_factor = img->samp_h1;
655                 cinfo.comp_info[1].v_samp_factor = img->samp_v1;
656                 cinfo.comp_info[2].h_samp_factor = img->samp_h2;
657                 cinfo.comp_info[2].v_samp_factor = img->samp_v2;
658         }
659         jpeg_start_compress(&cinfo, TRUE);
660
661         unsigned dstride0 = (img->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
662         unsigned dstride1 = (img->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
663         unsigned dstride2 = (img->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
664
665         int total_lines = 0;
666         int blocks = 0;
667         while (total_lines < cinfo.comp_info[0].height_in_blocks * DCTSIZE) {
668                 unsigned max_lines = cinfo.max_v_samp_factor * DCTSIZE;
669
670                 JSAMPROW y_row_ptrs[max_lines];
671                 JSAMPROW cb_row_ptrs[max_lines];
672                 JSAMPROW cr_row_ptrs[max_lines];
673                 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
674                 int i;
675
676                 for (i = 0; i < max_lines; ++i) {
677                         /* simple edge extension */
678                         int yline = i + blocks*DCTSIZE*cinfo.comp_info[0].v_samp_factor;
679                         if (yline > img->h0 - 1)
680                                 yline = img->h0 - 1;
681
682                         y_row_ptrs[i]  = img->data_y  + yline * dstride0;
683
684                         if (img->num_components == 3) {
685                                 int cbline = i + blocks*DCTSIZE*cinfo.comp_info[1].v_samp_factor;
686                                 if (cbline > img->h1 - 1)
687                                         cbline = img->h1 - 1;
688
689                                 int crline = i + blocks*DCTSIZE*cinfo.comp_info[2].v_samp_factor;
690                                 if (crline > img->h2 - 1)
691                                         crline = img->h2 - 1;
692
693                                 cb_row_ptrs[i] = img->data_cb + cbline * dstride1;
694                                 cr_row_ptrs[i] = img->data_cr + crline * dstride2;
695                         }
696                 }
697
698                 total_lines += max_lines;
699                 ++blocks;
700
701                 jpeg_write_raw_data(&cinfo, ptrs, max_lines);
702         }
703         jpeg_finish_compress(&cinfo);
704         jpeg_destroy_compress(&cinfo);
705
706         return 0;
707 }