]> git.sesse.net Git - qscale/blob - qscale.c
Add a parameter for sequential/progressive mode.
[qscale] / qscale.c
1 /*
2  * qscale: Quick, high-quality JPEG-to-JPEG scaler.
3  * Copyright (C) 2008 Steinar H. Gunderson <sgunderson@bigfoot.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 2 of the License.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
17  */
18
19 #include <stdio.h>
20 #include <malloc.h>
21 #include <math.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include "jpeglib.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 double sinc(double x)
45 {
46         static const double cutoff = 1.220703668e-4;  /* sqrt(sqrt(eps)) */
47
48         if (abs(x) < cutoff) {
49                 /* For small |x|, use Taylor series instead */
50                 const double x2 = x * x;
51                 const double x4 = x2 * x2;
52
53                 return 1.0 - x2 / 6.0 + x4 / 120.0;
54         } else {
55                 return sin(x) / x;
56         }
57 }
58
59 double lanczos_tap(double x)
60 {
61         if (x < -3.0 || x > 3.0)
62                 return 0.0;
63         if (x < 0.0)
64                 return sinc(-x*M_PI) * sinc(-x*M_PI / 3.0);
65         else
66                 return sinc(x*M_PI) * sinc(x*M_PI / 3.0);
67 }
68
69
70 struct pix_desc {
71         unsigned start, end;
72         unsigned startcoeff;
73 };
74
75 void hscale(float *pix, unsigned char *npix, unsigned w, unsigned h, unsigned nw, unsigned sstride, unsigned dstride)
76 {
77         struct pix_desc *pd = (struct pix_desc *)malloc(nw * sizeof(struct pix_desc));
78         int size_coeffs = 8;
79         float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
80         int num_coeffs = 0;
81         int x, y;
82         double sf = (double)w / (double)nw;
83         double support = (w > nw) ? (3.0 * sf) : (3.0 / sf);
84
85         /* calculate the filter */
86         for (x = 0; x < nw; ++x) {
87                 int start = ceil(x * sf - support);
88                 int end = floor(x * sf + support);
89                 int sx;
90                 double sum = 0.0;
91
92                 if (start < 0) {
93                         start = 0;
94                 }
95                 if (end > w - 1) {
96                         end = w - 1;
97                 }
98
99 #if USE_HORIZONTAL_SSE
100                 /* round up so we get a multiple of four for the SSE code */
101                 int num = (end - start + 1);
102                 if (num % 4 != 0) {
103                         /* prefer aligning it if possible */
104                         if (start % 4 != 0 && start % 4 <= num % 4) {
105                                 num += start % 4;
106                                 start -= start % 4;
107                         }
108                         if (num % 4 != 0) {
109                                 end += 4 - (num % 4);
110                         }
111                 }
112 #endif
113
114                 pd[x].start = start;
115                 pd[x].end = end;
116                 pd[x].startcoeff = num_coeffs;
117
118                 for (sx = start; sx <= end; ++sx) {
119                         double nd = (w > nw) ? (sx/sf - x) : (sx - x*sf);
120                         double f = lanczos_tap(nd);
121                         if (num_coeffs == size_coeffs) {
122                                 size_coeffs <<= 1;
123                                 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
124                         }
125
126                         coeffs[num_coeffs++] = f;
127                         sum += f;
128                 }
129
130                 for (sx = start; sx <= end; ++sx) {
131                         coeffs[pd[x].startcoeff + sx - start] /= sum;
132                 }
133         }
134
135         for (y = 0; y < h; ++y) {
136                 float *sptr = pix + y*sstride;
137                 unsigned char *dptr = npix + y*dstride;
138                 unsigned char ch;
139                 for (x = 0; x < nw; ++x) {
140 #if USE_HORIZONTAL_SSE
141                         int result;
142                         float acc;
143                         long tmp;
144                         static const float low = 0.0, high = 255.0;
145                         __asm__ (
146                                 "pxor %1, %1               \n"
147                                 "xor %2, %2                \n"
148                                 "0:                        \n"
149                                 "movups (%4,%2),%%xmm1     \n"
150                                 "movups (%3,%2),%%xmm2     \n"
151                                 "mulps %%xmm2,%%xmm1       \n"
152                                 "addps %%xmm1,%1           \n"
153                                 "add $16,%2                \n"
154                                 "dec %5                    \n"
155                                 "jnz 0b                    \n"
156                                 "haddps %1,%1              \n"
157                                 "haddps %1,%1              \n"
158                                 "maxss %6,%1               \n"
159                                 "minss %7,%1               \n"
160                                 "cvtss2si %1,%0            \n"
161                                 : "=r" (result),
162                                   "=&x" (acc),
163                                   "=&r" (tmp)
164                                 : "r" (&coeffs[pd[x].startcoeff]),
165                                   "r" (&sptr[pd[x].start]),
166                                   "r" ((pd[x].end - pd[x].start + 1)/4),
167                                   "m" (low),
168                                   "m" (high)
169                                 : "memory", "xmm1", "xmm2"
170                         );
171
172                         *dptr++ = (unsigned char)result;
173 #else
174                         float acc = 0.0;
175                         float *cf = &coeffs[pd[x].startcoeff];
176                         unsigned sx;
177                         
178                         for (sx = pd[x].start; sx <= pd[x].end; ++sx) {
179                                 acc += sptr[sx] * *cf++;
180                         }
181
182                         if (acc < 0.0)
183                                 ch = 0;
184                         else if (acc > 255.0)
185                                 ch = 255;
186                         else
187                                 ch = (unsigned char)acc;
188                         *dptr++ = ch;
189 #endif
190                 }
191                 ch = dptr[-1];
192                 for ( ; x < dstride; ++x) {
193                         *dptr++ = ch;
194                 }
195         }
196 }
197
198 void vscale(unsigned char *pix, float *npix, unsigned w, unsigned h, unsigned nh, unsigned dstride)
199 {
200         struct pix_desc *pd = (struct pix_desc *)malloc(nh * sizeof(struct pix_desc));
201         int size_coeffs = 8;
202         float *coeffs = (float *)malloc(size_coeffs * sizeof(float));
203         int num_coeffs = 0;
204         int x, y, sy;
205         double sf = (double)h / (double)nh;
206         double support = (h > nh) ? (3.0 * sf) : (3.0 / sf);
207
208         /* calculate the filter */
209         for (y = 0; y < nh; ++y) {
210                 int start = ceil(y * sf - support);
211                 int end = floor(y * sf + support);
212                 double sum = 0.0;
213
214                 if (start < 0) {
215                         start = 0;
216                 }
217                 if (end > h - 1) {
218                         end = h - 1;
219                 }
220
221                 pd[y].start = start;
222                 pd[y].end = end;
223                 pd[y].startcoeff = num_coeffs;
224
225                 for (sy = start; sy <= end; ++sy) {
226                         double nd = (h > nh) ? (sy/sf - y) : (sy - y*sf);
227                         double f = lanczos_tap(nd);
228                         if (num_coeffs == size_coeffs) {
229                                 size_coeffs <<= 1;
230                                 coeffs = (float *)realloc(coeffs, size_coeffs * sizeof(float));
231                         }
232                         
233                         coeffs[num_coeffs++] = f;
234                         sum += f;
235                 }
236
237                 for (sy = start; sy <= end; ++sy) {
238                         coeffs[pd[y].startcoeff + sy - start] /= sum;
239                 }
240         }
241
242 #if CACHE_LINE_FACTOR > 1
243         for (x = 0; x < (w/CACHE_LINE_FACTOR) * CACHE_LINE_FACTOR; x += CACHE_LINE_FACTOR) {
244                 unsigned char *sptr = pix + x;
245                 float *dptr = npix + x;
246                 for (y = 0; y < nh; ++y) {
247 #if USE_VERTICAL_SSE
248                         /*
249                          * xmm0 - xmm3: acc[0..15]
250                          * xmm4: current filter coefficient
251                          * xmm5, xmm6, xmm7: scratchpad
252                          */
253                         __asm__ (
254                                 /* clear */
255                                 "pxor %%xmm0, %%xmm0          \n"
256                                 "pxor %%xmm1, %%xmm1          \n"
257                                 "pxor %%xmm2, %%xmm2          \n"
258                                 "pxor %%xmm3, %%xmm3          \n"
259
260                                 /* main loop */
261                                 "0:                           \n"
262                                 
263                                 /* a zero is useful during unpacking */
264                                 "pxor %%xmm4, %%xmm4          \n"
265                                 
266                                 /* fetch all 16 source bytes */
267                                 "movups (%0), %%xmm5          \n"
268                                 "prefetcht0 (%0,%3,4)         \n"
269
270                                 /* unpack into words (xmm5, xmm7) */
271                                 "movaps %%xmm5, %%xmm7        \n"
272                                 "punpcklbw %%xmm4, %%xmm5     \n"
273                                 "punpckhbw %%xmm4, %%xmm7     \n"
274
275                                 /* unpack xmm5 into dwords (xmm5, xmm6) */
276                                 "movaps %%xmm5, %%xmm6        \n"
277                                 "punpcklwd %%xmm4, %%xmm5     \n"
278                                 "punpckhwd %%xmm4, %%xmm6     \n"
279
280                                 /* convert xmm5, xmm6 to floats */
281                                 "cvtdq2ps %%xmm5, %%xmm5      \n"
282                                 "cvtdq2ps %%xmm6, %%xmm6      \n"
283
284                                 /* fetch the coefficient */
285                                 "movss (%2), %%xmm4           \n"
286                                 "shufps $0x0, %%xmm4, %%xmm4  \n"
287
288                                 /* do the muls for xmm5 and xmm6 */
289                                 "mulps %%xmm4, %%xmm5         \n"
290                                 "mulps %%xmm4, %%xmm6         \n"
291                                 "addps %%xmm5, %%xmm0         \n"
292                                 "addps %%xmm6, %%xmm1         \n"
293
294                                 /* get the zero back again */
295                                 "pxor %%xmm4, %%xmm4          \n"
296
297                                 /* unpack xmm7 into dwords (xmm7, xmm6) */
298                                 "movaps %%xmm7, %%xmm6        \n"
299                                 "punpcklwd %%xmm4, %%xmm7     \n"
300                                 "punpckhwd %%xmm4, %%xmm6     \n"
301
302                                 /* convert xmm7, xmm6 to floats */
303                                 "cvtdq2ps %%xmm7, %%xmm7      \n"
304                                 "cvtdq2ps %%xmm6, %%xmm6      \n"
305
306                                 /* fetch the coefficient */
307                                 "movss (%2), %%xmm4           \n"
308                                 "shufps $0x0, %%xmm4, %%xmm4  \n"
309
310                                 /* do the second set of muls */
311                                 "mulps %%xmm4, %%xmm7         \n"
312                                 "mulps %%xmm4, %%xmm6         \n"
313                                 "addps %%xmm7, %%xmm2         \n"
314                                 "addps %%xmm6, %%xmm3         \n"
315
316                                 /* move along, and loop */
317                                 "add $4, %2                   \n"
318                                 "add %3, %0                   \n"
319                                 "dec %1                       \n"
320                                 "jnz 0b                       \n"
321
322                                 /* store the values */
323                                 "movaps %%xmm0, (%4)          \n"
324                                 "movaps %%xmm1, 16(%4)        \n"
325                                 "movaps %%xmm2, 32(%4)        \n"
326                                 "movaps %%xmm3, 48(%4)        \n"
327                                 : :
328                                 "r" (&sptr[pd[y].start * w]),        /* 0: srcptr base */
329                                 "r" (pd[y].end - pd[y].start + 1),   /* 1: filter len */
330                                 "r" (&coeffs[pd[y].startcoeff]),     /* 2: coeffs base */
331                                 "r" ((long)w),                       /* 3: stride */
332                                 "r" (dptr)                           /* 4: dstptr base */
333                                 : "memory", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7"
334                         );
335 #else
336                         int i;
337                         float acc[CACHE_LINE_FACTOR];
338                         for (i = 0; i < CACHE_LINE_FACTOR; ++i)
339                                 acc[i] = 0.0;
340                         float *cf = &coeffs[pd[y].startcoeff];
341                         unsigned sy;
342                 
343                         for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
344                                 for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
345                                         acc[i] += sptr[sy * w + i] * *cf;
346                                 }
347                                 ++cf;
348                         }
349
350                         for (i = 0; i < CACHE_LINE_FACTOR; ++i) {
351                                 dptr[i] = acc[i];
352                         }
353 #endif
354                         dptr += dstride;
355                 }
356         }
357         for (x = (x/CACHE_LINE_FACTOR)*CACHE_LINE_FACTOR; x < w; ++x) {
358 #else
359         for (x = 0; x < w; ++x) {
360 #endif
361                 unsigned char *sptr = pix + x;
362                 float *dptr = npix + x;
363                 for (y = 0; y < nh; ++y) {
364                         float acc = 0.0;
365                         float *cf = &coeffs[pd[y].startcoeff];
366                         unsigned sy;
367                         
368                         for (sy = pd[y].start; sy <= pd[y].end; ++sy) {
369                                 acc += sptr[sy * w] * *cf++;
370                         }
371
372                         *dptr = acc;
373                         dptr += dstride;
374                 }
375         }
376 }
377
378 int main(int argc, char **argv)
379 {
380         /* user-settable parameters */
381         unsigned nominal_w = atoi(argv[1]);
382         unsigned nominal_h = atoi(argv[2]);
383         unsigned samp_h0 = 2, samp_v0 = 2;
384         unsigned samp_h1 = 1, samp_v1 = 1;
385         unsigned samp_h2 = 1, samp_v2 = 1;
386         unsigned jpeg_quality = 85;
387         /* end */
388
389         unsigned max_samp_h, max_samp_v;
390         max_samp_h = samp_h0;
391         if (samp_h1 > max_samp_h)
392                 max_samp_h = samp_h1;
393         if (samp_h2 > max_samp_h)
394                 max_samp_h = samp_h2;
395         
396         max_samp_v = samp_v0;
397         if (samp_v1 > max_samp_v)
398                 max_samp_v = samp_v1;
399         if (samp_v2 > max_samp_v)
400                 max_samp_v = samp_v2;
401
402         unsigned nw0 = nominal_w * samp_h0 / max_samp_h, nh0 = nominal_h * samp_v0 / max_samp_v;
403         unsigned nw1 = nominal_w * samp_h1 / max_samp_h, nh1 = nominal_h * samp_v1 / max_samp_v;
404         unsigned nw2 = nominal_w * samp_h2 / max_samp_h, nh2 = nominal_h * samp_v2 / max_samp_v;
405
406         unsigned stride0 = (nw0 + DCTSIZE-1) & ~(DCTSIZE-1);
407         unsigned stride1 = (nw1 + DCTSIZE-1) & ~(DCTSIZE-1);
408         unsigned stride2 = (nw2 + DCTSIZE-1) & ~(DCTSIZE-1);
409
410         struct jpeg_decompress_struct dinfo;
411         struct jpeg_error_mgr jerr;
412         dinfo.err = jpeg_std_error(&jerr);
413         jpeg_create_decompress(&dinfo);
414         jpeg_stdio_src(&dinfo, stdin);
415         jpeg_read_header(&dinfo, TRUE);
416         dinfo.raw_data_out = TRUE;
417         jpeg_start_decompress(&dinfo);
418
419         unsigned w0 = dinfo.image_width * dinfo.comp_info[0].h_samp_factor / dinfo.max_h_samp_factor;
420         unsigned h0 = dinfo.image_height * dinfo.comp_info[0].v_samp_factor / dinfo.max_v_samp_factor;
421
422         unsigned w1 = dinfo.image_width * dinfo.comp_info[1].h_samp_factor / dinfo.max_h_samp_factor;
423         unsigned h1 = dinfo.image_height * dinfo.comp_info[1].v_samp_factor / dinfo.max_v_samp_factor;
424
425         unsigned w2 = dinfo.image_width * dinfo.comp_info[2].h_samp_factor / dinfo.max_h_samp_factor;
426         unsigned h2 = dinfo.image_height * dinfo.comp_info[2].v_samp_factor / dinfo.max_v_samp_factor;
427
428         fprintf(stderr, "Scaling using Lanczos filter:\n");
429         fprintf(stderr, "  Y component: %ux%u -> %ux%u\n", dinfo.comp_info[0].width_in_blocks * DCTSIZE, dinfo.comp_info[0].height_in_blocks * DCTSIZE, nw0, nh0);
430         fprintf(stderr, "  Cb component: %ux%u -> %ux%u\n", dinfo.comp_info[1].width_in_blocks * DCTSIZE, dinfo.comp_info[1].height_in_blocks * DCTSIZE, nw1, nh1);
431         fprintf(stderr, "  Cr component: %ux%u -> %ux%u\n", dinfo.comp_info[2].width_in_blocks * DCTSIZE, dinfo.comp_info[2].height_in_blocks * DCTSIZE, nw2, nh2);
432
433         JSAMPLE *data_y  = (JSAMPLE*)memalign(16, dinfo.comp_info[0].height_in_blocks * dinfo.comp_info[0].width_in_blocks * DCTSIZE * DCTSIZE);
434         JSAMPLE *data_cb = (JSAMPLE*)memalign(16, dinfo.comp_info[1].height_in_blocks * dinfo.comp_info[1].width_in_blocks * DCTSIZE * DCTSIZE);
435         JSAMPLE *data_cr = (JSAMPLE*)memalign(16, dinfo.comp_info[2].height_in_blocks * dinfo.comp_info[2].width_in_blocks * DCTSIZE * DCTSIZE);
436         JSAMPLE *data_ny, *data_ncb, *data_ncr;
437
438         int total_lines = 0, blocks = 0;
439         while (total_lines < dinfo.comp_info[0].height_in_blocks * DCTSIZE) {
440                 unsigned max_lines = dinfo.max_v_samp_factor * DCTSIZE;
441
442                 JSAMPROW y_row_ptrs[max_lines];
443                 JSAMPROW cb_row_ptrs[max_lines];
444                 JSAMPROW cr_row_ptrs[max_lines];
445                 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
446                 int i;
447
448                 for (i = 0; i < max_lines; ++i) {
449                         y_row_ptrs[i]  = data_y  + (i+blocks*DCTSIZE*dinfo.comp_info[0].v_samp_factor) * dinfo.comp_info[0].width_in_blocks * DCTSIZE;
450                         cb_row_ptrs[i] = data_cb + (i+blocks*DCTSIZE*dinfo.comp_info[1].v_samp_factor) * dinfo.comp_info[1].width_in_blocks * DCTSIZE;
451                         cr_row_ptrs[i] = data_cr + (i+blocks*DCTSIZE*dinfo.comp_info[2].v_samp_factor) * dinfo.comp_info[2].width_in_blocks * DCTSIZE;
452                 }
453                 
454                 total_lines += max_lines;
455                 ++blocks;
456
457                 if (jpeg_read_raw_data(&dinfo, ptrs, max_lines) == 0)
458                         break;
459         }
460
461         {
462                 float *npix = (float*)memalign(16, dinfo.comp_info[0].width_in_blocks * DCTSIZE * nh0 * sizeof(float)); 
463                 vscale(data_y, npix, dinfo.comp_info[0].width_in_blocks * DCTSIZE, h0, nh0, dinfo.comp_info[0].width_in_blocks * DCTSIZE);
464                 data_ny = (unsigned char *)malloc(nh0 * stride0);
465                 hscale(npix, data_ny, w0, nh0, nw0, dinfo.comp_info[0].width_in_blocks * DCTSIZE, stride0);
466                 free(npix);
467         }
468         {
469                 float *npix = (float*)memalign(16, dinfo.comp_info[1].width_in_blocks * DCTSIZE * nh1 * sizeof(float)); 
470                 vscale(data_cr, npix, dinfo.comp_info[1].width_in_blocks * DCTSIZE, h1, nh1, dinfo.comp_info[1].width_in_blocks * DCTSIZE);
471                 data_ncr = (unsigned char *)malloc(nh1 * stride1);
472                 hscale(npix, data_ncr, w1, nh1, nw1, dinfo.comp_info[1].width_in_blocks * DCTSIZE, stride1);
473                 free(npix);
474         }
475         {
476                 float *npix = (float*)memalign(16, dinfo.comp_info[2].width_in_blocks * DCTSIZE * nh2 * sizeof(float)); 
477                 vscale(data_cb, npix, dinfo.comp_info[2].width_in_blocks * DCTSIZE, h2, nh2, dinfo.comp_info[2].width_in_blocks * DCTSIZE);
478                 data_ncb = (unsigned char *)malloc(nh2 * stride2);
479                 hscale(npix, data_ncb, w2, nh2, nw2, dinfo.comp_info[2].width_in_blocks * DCTSIZE, stride2);
480                 free(npix);
481         }
482         jpeg_destroy_decompress(&dinfo);
483         
484         struct jpeg_compress_struct cinfo;
485         cinfo.err = jpeg_std_error(&jerr);
486         jpeg_create_compress(&cinfo);
487         jpeg_stdio_dest(&cinfo, stdout);
488         cinfo.input_components = 3;
489         jpeg_set_defaults(&cinfo);
490         jpeg_set_quality(&cinfo, jpeg_quality, FALSE);
491         cinfo.image_width = nominal_w;
492         cinfo.image_height = nominal_h;
493         cinfo.raw_data_in = TRUE;
494         jpeg_set_colorspace(&cinfo, JCS_YCbCr);
495         cinfo.comp_info[0].h_samp_factor = samp_h0;
496         cinfo.comp_info[0].v_samp_factor = samp_v0;
497         cinfo.comp_info[1].h_samp_factor = samp_h1;
498         cinfo.comp_info[1].v_samp_factor = samp_v1;
499         cinfo.comp_info[2].h_samp_factor = samp_h2;
500         cinfo.comp_info[2].v_samp_factor = samp_v2;
501         jpeg_start_compress(&cinfo, TRUE);
502
503         total_lines = 0;
504         blocks = 0;
505         while (total_lines < cinfo.comp_info[0].height_in_blocks * DCTSIZE) {
506                 unsigned max_lines = cinfo.max_v_samp_factor * DCTSIZE;
507
508                 JSAMPROW y_row_ptrs[max_lines];
509                 JSAMPROW cb_row_ptrs[max_lines];
510                 JSAMPROW cr_row_ptrs[max_lines];
511                 JSAMPROW* ptrs[] = { y_row_ptrs, cb_row_ptrs, cr_row_ptrs };
512                 int i;
513
514                 for (i = 0; i < max_lines; ++i) {
515                         /* simple edge extension */
516                         int yline = i + blocks*DCTSIZE*cinfo.comp_info[0].v_samp_factor;
517                         if (yline > nh0 - 1)
518                                 yline = nh0 - 1;
519
520                         int cbline = i + blocks*DCTSIZE*cinfo.comp_info[1].v_samp_factor;
521                         if (cbline > nh1 - 1)
522                                 cbline = nh1 - 1;
523
524                         int crline = i + blocks*DCTSIZE*cinfo.comp_info[2].v_samp_factor;
525                         if (crline > nh2 - 1)
526                                 crline = nh2 - 1;
527
528                         y_row_ptrs[i]  = data_ny  + yline * stride0;
529                         cb_row_ptrs[i] = data_ncb + cbline * stride1;
530                         cr_row_ptrs[i] = data_ncr + crline * stride2;
531                 }
532                 
533                 total_lines += max_lines;
534                 ++blocks;
535
536                 jpeg_write_raw_data(&cinfo, ptrs, max_lines);
537         }
538         jpeg_finish_compress(&cinfo);
539         jpeg_destroy_compress(&cinfo);
540
541         return 0;
542 }
543