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