]> git.sesse.net Git - qscale/blob - libqscale.c
Use SSE intrinsics instead of asm() for vertical SSE, finally offloading the register...
[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                         int result;
304                         float acc;
305                         long tmp;
306                         static const float low = 0.0, high = 255.0;
307                         __asm__ (
308                                 "pxor %1, %1               \n"
309                                 "xor %2, %2                \n"
310                                 "0:                        \n"
311                                 "movups (%4,%2),%%xmm1     \n"
312                                 "movups (%3,%2),%%xmm2     \n"
313                                 "mulps %%xmm2,%%xmm1       \n"
314                                 "addps %%xmm1,%1           \n"
315                                 "add $16,%2                \n"
316                                 "dec %5                    \n"
317                                 "jnz 0b                    \n"
318                                 "haddps %1,%1              \n"
319                                 "haddps %1,%1              \n"
320                                 "maxss %6,%1               \n"
321                                 "minss %7,%1               \n"
322                                 "cvtss2si %1,%0            \n"
323                                 : "=r" (result),
324                                   "=&x" (acc),
325                                   "=&r" (tmp)
326                                 : "r" (&coeffs[pd[x].startcoeff]),
327                                   "r" (&sptr[pd[x].start]),
328                                   "r" ((pd[x].end - pd[x].start + 1)/4),
329                                   "m" (low),
330                                   "m" (high)
331                                 : "memory", "xmm1", "xmm2"
332                         );
333
334                         *dptr++ = (unsigned char)result;
335 #else
336                         float acc = 0.0;
337                         float *cf = &coeffs[pd[x].startcoeff];
338                         unsigned sx;
339                         
340                         for (sx = pd[x].start; sx <= pd[x].end; ++sx) {
341                                 acc += sptr[sx] * *cf++;
342                         }
343
344                         if (acc < 0.0)
345                                 ch = 0;
346                         else if (acc > 255.0)
347                                 ch = 255;
348                         else
349                                 ch = (unsigned char)acc;
350                         *dptr++ = ch;
351 #endif
352                 }
353                 ch = dptr[-1];
354                 for ( ; x < dstride; ++x) {
355                         *dptr++ = ch;
356                 }
357         }
358 }
359
360 static void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsigned nh, unsigned dstride, enum qscale_scaling_filter scaling_filter)
361 {
362         struct pix_desc *pd = (struct pix_desc *)malloc(nh * sizeof(struct pix_desc));
363         int size_coeffs = 8;
364         float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
365         int num_coeffs = 0;
366         int x, y, sy;
367         double sf = (double)h / (double)nh;
368         double support;
369         
370         if (scaling_filter == LANCZOS) {
371                 support = (h > nh) ? (3.0 * sf) : (3.0 / sf);
372         } else {  /* Mitchell */
373                 support = (h > nh) ? (2.0 * sf) : (2.0 / sf);
374         }
375
376         /* calculate the filter */
377         for (y = 0; y < nh; ++y) {
378                 int start = ceil(y * sf - support);
379                 int end = floor(y * sf + support);
380                 double sum = 0.0;
381
382                 if (start < 0) {
383                         start = 0;
384                 }
385                 if (end > h - 1) {
386                         end = h - 1;
387                 }
388
389                 pd[y].start = start;
390                 pd[y].end = end;
391                 pd[y].startcoeff = num_coeffs;
392
393                 for (sy = start; sy <= end; ++sy) {
394                         double nd = (h > nh) ? (sy/sf - y) : (sy - y*sf);
395                         double f;
396                         if (scaling_filter == LANCZOS) {
397                                 f = lanczos_tap(nd);
398                         } else {  /* Mitchell */
399                                 f = mitchell_tap(nd);
400                         }
401                         if (num_coeffs == size_coeffs) {
402                                 size_coeffs <<= 1;
403                                 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
404                         }
405                         
406                         coeffs[num_coeffs++] = f;
407                         sum += f;
408                 }
409
410                 for (sy = start; sy <= end; ++sy) {
411                         coeffs[pd[y].startcoeff + sy - start] /= sum;
412                 }
413         }
414
415 #if CACHE_LINE_FACTOR > 1
416         for (x = 0; x < (w/CACHE_LINE_FACTOR) * CACHE_LINE_FACTOR; x += CACHE_LINE_FACTOR) {
417                 unsigned char *sptr = pix + x;
418                 float *dptr = npix + x;
419                 for (y = 0; y < nh; ++y) {
420 #if USE_VERTICAL_SSE
421                         /* A zero is useful during unpacking. */
422                         static const v4sf zero = { 0.0f, 0.0f, 0.0f, 0.0f };
423                         const unsigned char *sptr_xmm = &sptr[pd[y].start * w];
424                         const float *coeffptr = &coeffs[pd[y].startcoeff];
425                         const int filter_len = pd[y].end - pd[y].start + 1;
426                         int i;
427
428                         v4sf acc0 = { 0.0f, 0.0f, 0.0f, 0.0f };
429                         v4sf acc1 = { 0.0f, 0.0f, 0.0f, 0.0f };
430                         v4sf acc2 = { 0.0f, 0.0f, 0.0f, 0.0f };
431                         v4sf acc3 = { 0.0f, 0.0f, 0.0f, 0.0f };
432                         
433                         for (i = 0; i < filter_len; ++i, ++coeffptr, sptr_xmm += w) {
434                                 __builtin_prefetch(sptr_xmm + w, 0);
435                                 v16qi src = (v16qi)__builtin_ia32_loadups((float*)sptr_xmm);
436
437                                 // unpack into words
438                                 v8hi src_lo = (v8hi)__builtin_ia32_punpcklbw128(src, (v16qi)zero);
439                                 v8hi src_hi = (v8hi)__builtin_ia32_punpckhbw128(src, (v16qi)zero);
440
441                                 // unpack into dwords, convert to floats
442                                 v4si src0_i = (v4si)__builtin_ia32_punpcklwd128(src_lo, (v8hi)zero);
443                                 v4si src1_i = (v4si)__builtin_ia32_punpckhwd128(src_lo, (v8hi)zero);
444                                 v4si src2_i = (v4si)__builtin_ia32_punpcklwd128(src_hi, (v8hi)zero);
445                                 v4si src3_i = (v4si)__builtin_ia32_punpckhwd128(src_hi, (v8hi)zero);
446
447                                 v4sf src0 = __builtin_ia32_cvtdq2ps(src0_i);
448                                 v4sf src1 = __builtin_ia32_cvtdq2ps(src1_i);
449                                 v4sf src2 = __builtin_ia32_cvtdq2ps(src2_i);
450                                 v4sf src3 = __builtin_ia32_cvtdq2ps(src3_i);
451                         
452                                 // fetch the coefficient, and replicate it
453                                 v4sf coeff = { *coeffptr, *coeffptr, *coeffptr, *coeffptr };
454
455                                 // do the actual muladds
456                                 acc0 = __builtin_ia32_addps(acc0, __builtin_ia32_mulps(src0, coeff));
457                                 acc1 = __builtin_ia32_addps(acc1, __builtin_ia32_mulps(src1, coeff));
458                                 acc2 = __builtin_ia32_addps(acc2, __builtin_ia32_mulps(src2, coeff));
459                                 acc3 = __builtin_ia32_addps(acc3, __builtin_ia32_mulps(src3, coeff));
460                         }
461
462                         *(v4sf *)(&dptr[0]) = acc0;
463                         *(v4sf *)(&dptr[4]) = acc1;
464                         *(v4sf *)(&dptr[8]) = acc2;
465                         *(v4sf *)(&dptr[12]) = acc3;
466 #else
467                         int i;
468                         float acc[CACHE_LINE_FACTOR];
469                         for (i = 0; i < CACHE_LINE_FACTOR; ++i)
470                                 acc[i] = 0.0;
471                         float *cf = &coeffs[pd[y].startcoeff];
472                         unsigned sy;
473                 
474                         for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
475                                 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
476                                         acc[i] += sptr[sy * w + i] * *cf;
477                                 }
478                                 ++cf;
479                         }
480
481                         for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
482                                 dptr[i] = acc[i];
483                         }
484 #endif
485                         dptr += dstride;
486                 }
487         }
488         for (x = (x/CACHE_LINE_FACTOR)*CACHE_LINE_FACTOR; x < w; ++x) {
489 #else
490         for (x = 0; x < w; ++x) {
491 #endif
492                 unsigned char *sptr = pix + x;
493                 float *dptr = npix + x;
494                 for (y = 0; y < nh; ++y) {
495                         float acc = 0.0;
496                         float *cf = &coeffs[pd[y].startcoeff];
497                         unsigned sy;
498                         
499                         for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
500                                 acc += sptr[sy * w] * *cf++;
501                         }
502
503                         *dptr = acc;
504                         dptr += dstride;
505                 }
506         }
507 }
508
509 qscale_img *qscale_clone(const qscale_img *img)
510 {
511         qscale_img *dst = (qscale_img *)malloc(sizeof(qscale_img));
512         if (dst == NULL) {
513                 return NULL;
514         }
515
516         *dst = *img;
517
518         unsigned dstride0 = (dst->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
519         unsigned dstride1 = (dst->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
520         unsigned dstride2 = (dst->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
521
522         /* FIXME: handle out-of-memory gracefully */
523         {
524                 dst->data_y = (unsigned char *)malloc(dst->h0 * dstride0);
525                 memcpy(dst->data_y, img->data_y, dst->h0 * dstride0);
526         }
527         {
528                 dst->data_cb = (unsigned char *)malloc(dst->h1 * dstride1);
529                 memcpy(dst->data_cb, img->data_cb, dst->h1 * dstride1);
530         }
531         {
532                 dst->data_cr = (unsigned char *)malloc(dst->h2 * dstride2);
533                 memcpy(dst->data_cr, img->data_cr, dst->h2 * dstride2);
534         }
535
536         return dst;
537 }
538
539 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)
540 {
541         qscale_img *dst = (qscale_img *)malloc(sizeof(qscale_img));
542         if (dst == NULL) {
543                 return NULL;
544         }
545
546         dst->width = width;
547         dst->height = height;
548         dst->num_components = src->num_components;
549
550         unsigned max_samp_h, max_samp_v;
551         max_samp_h = samp_h0;
552         if (src->num_components == 3) {
553                 if (samp_h1 > max_samp_h)
554                         max_samp_h = samp_h1;
555                 if (samp_h2 > max_samp_h)
556                         max_samp_h = samp_h2;
557         }
558
559         max_samp_v = samp_v0;
560         if (src->num_components == 3) {
561                 if (samp_v1 > max_samp_v)
562                         max_samp_v = samp_v1;
563                 if (samp_v2 > max_samp_v)
564                         max_samp_v = samp_v2;
565         }
566
567         dst->w0 = width * samp_h0 / max_samp_h;
568         dst->h0 = height * samp_v0 / max_samp_v;
569
570         if (src->num_components == 3) {
571                 dst->w1 = width * samp_h1 / max_samp_h;
572                 dst->h1 = height * samp_v1 / max_samp_v;
573
574                 dst->w2 = width * samp_h2 / max_samp_h;
575                 dst->h2 = height * samp_v2 / max_samp_v;
576         }
577
578         dst->samp_h0 = samp_h0;
579         dst->samp_v0 = samp_v0;
580
581         if (src->num_components == 3) {
582                 dst->samp_h1 = samp_h1;
583                 dst->samp_v1 = samp_v1;
584
585                 dst->samp_h2 = samp_h2;
586                 dst->samp_v2 = samp_v2;
587         }
588
589         unsigned dstride0 = (dst->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
590         unsigned dstride1 = (dst->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
591         unsigned dstride2 = (dst->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
592
593         unsigned sstride0 = (src->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
594         unsigned sstride1 = (src->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
595         unsigned sstride2 = (src->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
596
597         /* FIXME: handle out-of-memory gracefully */
598         {
599                 float *npix = (float*)memalign(16, sstride0 * dst->h0 * sizeof(float));
600                 vscale(src->data_y, npix, sstride0, src->h0, dst->h0, sstride0, scaling_filter);
601                 dst->data_y = (unsigned char *)malloc(dst->h0 * dstride0);
602                 hscale(npix, dst->data_y, src->w0, dst->h0, dst->w0, sstride0, dstride0, scaling_filter);
603                 free(npix);
604         }
605         if (src->num_components == 3) {
606                 {
607                         float *npix = (float*)memalign(16, sstride1 * dst->h1 * sizeof(float));
608                         vscale(src->data_cr, npix, sstride1, src->h1, dst->h1, sstride1, scaling_filter);
609                         dst->data_cr = (unsigned char *)malloc(dst->h1 * dstride1);
610                         hscale(npix, dst->data_cr, src->w1, dst->h1, dst->w1, sstride1, dstride1, scaling_filter);
611                         free(npix);
612                 }
613                 {
614                         float *npix = (float*)memalign(16, sstride2 * dst->h2 * sizeof(float));
615                         vscale(src->data_cb, npix, sstride2, src->h2, dst->h2, sstride2, scaling_filter);
616                         dst->data_cb = (unsigned char *)malloc(dst->h2 * dstride2);
617                         hscale(npix, dst->data_cb, src->w2, dst->h2, dst->w2, sstride2, dstride2, scaling_filter);
618                         free(npix);
619                 }
620         }
621
622         return dst;
623 }
624
625 int qscale_save_jpeg(const qscale_img *img, const char *filename, unsigned jpeg_quality, enum qscale_jpeg_mode jpeg_mode)
626 {
627         FILE *file = fopen(filename, "wb");
628         if (file == NULL) {
629                 return -1;
630         }
631
632         int err = qscale_save_jpeg_to_stdio(img, file, jpeg_quality, jpeg_mode);
633
634         fclose(file);
635         return err;
636 }
637
638 int qscale_save_jpeg_to_stdio(const qscale_img *img, FILE *file, unsigned jpeg_quality, enum qscale_jpeg_mode jpeg_mode)
639 {
640         struct jpeg_compress_struct cinfo;
641         struct jpeg_error_mgr jerr;
642         cinfo.err = jpeg_std_error(&jerr);
643         jpeg_create_compress(&cinfo);
644         jpeg_stdio_dest(&cinfo, file);
645         cinfo.input_components = img->num_components;
646         jpeg_set_defaults(&cinfo);
647         jpeg_set_quality(&cinfo, jpeg_quality, FALSE);
648
649         if (jpeg_mode == PROGRESSIVE) {
650                 jpeg_simple_progression(&cinfo);
651         }
652
653         cinfo.image_width = img->width;
654         cinfo.image_height = img->height;
655         cinfo.raw_data_in = TRUE;
656         if (img->num_components == 3) {
657                 jpeg_set_colorspace(&cinfo, JCS_YCbCr);
658         } else {
659                 jpeg_set_colorspace(&cinfo, JCS_GRAYSCALE);
660         }
661         cinfo.comp_info[0].h_samp_factor = img->samp_h0;
662         cinfo.comp_info[0].v_samp_factor = img->samp_v0;
663         if (img->num_components == 3) {
664                 cinfo.comp_info[1].h_samp_factor = img->samp_h1;
665                 cinfo.comp_info[1].v_samp_factor = img->samp_v1;
666                 cinfo.comp_info[2].h_samp_factor = img->samp_h2;
667                 cinfo.comp_info[2].v_samp_factor = img->samp_v2;
668         }
669         jpeg_start_compress(&cinfo, TRUE);
670
671         unsigned dstride0 = (img->w0 + DCTSIZE-1) & ~(DCTSIZE-1);
672         unsigned dstride1 = (img->w1 + DCTSIZE-1) & ~(DCTSIZE-1);
673         unsigned dstride2 = (img->w2 + DCTSIZE-1) & ~(DCTSIZE-1);
674
675         int total_lines = 0;
676         int blocks = 0;
677         while (total_lines < cinfo.comp_info[0].height_in_blocks * DCTSIZE) {
678                 unsigned max_lines = cinfo.max_v_samp_factor * DCTSIZE;
679
680                 JSAMPROW y_row_ptrs[max_lines];
681                 JSAMPROW cb_row_ptrs[max_lines];
682                 JSAMPROW cr_row_ptrs[max_lines];
683                 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
684                 int i;
685
686                 for (i = 0; i < max_lines; ++i) {
687                         /* simple edge extension */
688                         int yline = i + blocks*DCTSIZE*cinfo.comp_info[0].v_samp_factor;
689                         if (yline > img->h0 - 1)
690                                 yline = img->h0 - 1;
691
692                         y_row_ptrs[i]  = img->data_y  + yline * dstride0;
693
694                         if (img->num_components == 3) {
695                                 int cbline = i + blocks*DCTSIZE*cinfo.comp_info[1].v_samp_factor;
696                                 if (cbline > img->h1 - 1)
697                                         cbline = img->h1 - 1;
698
699                                 int crline = i + blocks*DCTSIZE*cinfo.comp_info[2].v_samp_factor;
700                                 if (crline > img->h2 - 1)
701                                         crline = img->h2 - 1;
702
703                                 cb_row_ptrs[i] = img->data_cb + cbline * dstride1;
704                                 cr_row_ptrs[i] = img->data_cr + crline * dstride2;
705                         }
706                 }
707
708                 total_lines += max_lines;
709                 ++blocks;
710
711                 jpeg_write_raw_data(&cinfo, ptrs, max_lines);
712         }
713         jpeg_finish_compress(&cinfo);
714         jpeg_destroy_compress(&cinfo);
715
716         return 0;
717 }