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