Bump to 2.0.6
[platform/upstream/libjpeg-turbo.git] / jdmerge.c
1 /*
2  * jdmerge.c
3  *
4  * This file was part of the Independent JPEG Group's software:
5  * Copyright (C) 1994-1996, Thomas G. Lane.
6  * libjpeg-turbo Modifications:
7  * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
8  * Copyright (C) 2009, 2011, 2014-2015, 2020, D. R. Commander.
9  * Copyright (C) 2013, Linaro Limited.
10  * For conditions of distribution and use, see the accompanying README.ijg
11  * file.
12  *
13  * This file contains code for merged upsampling/color conversion.
14  *
15  * This file combines functions from jdsample.c and jdcolor.c;
16  * read those files first to understand what's going on.
17  *
18  * When the chroma components are to be upsampled by simple replication
19  * (ie, box filtering), we can save some work in color conversion by
20  * calculating all the output pixels corresponding to a pair of chroma
21  * samples at one time.  In the conversion equations
22  *      R = Y           + K1 * Cr
23  *      G = Y + K2 * Cb + K3 * Cr
24  *      B = Y + K4 * Cb
25  * only the Y term varies among the group of pixels corresponding to a pair
26  * of chroma samples, so the rest of the terms can be calculated just once.
27  * At typical sampling ratios, this eliminates half or three-quarters of the
28  * multiplications needed for color conversion.
29  *
30  * This file currently provides implementations for the following cases:
31  *      YCbCr => RGB color conversion only.
32  *      Sampling ratios of 2h1v or 2h2v.
33  *      No scaling needed at upsample time.
34  *      Corner-aligned (non-CCIR601) sampling alignment.
35  * Other special cases could be added, but in most applications these are
36  * the only common cases.  (For uncommon cases we fall back on the more
37  * general code in jdsample.c and jdcolor.c.)
38  */
39
40 #define JPEG_INTERNALS
41 #include "jinclude.h"
42 #include "jpeglib.h"
43 #include "jdmerge.h"
44 #include "jsimd.h"
45 #include "jconfigint.h"
46
47 #ifdef UPSAMPLE_MERGING_SUPPORTED
48
49
50 #define SCALEBITS       16      /* speediest right-shift on some machines */
51 #define ONE_HALF        ((JLONG)1 << (SCALEBITS - 1))
52 #define FIX(x)          ((JLONG)((x) * (1L << SCALEBITS) + 0.5))
53
54
55 /* Include inline routines for colorspace extensions */
56
57 #include "jdmrgext.c"
58 #undef RGB_RED
59 #undef RGB_GREEN
60 #undef RGB_BLUE
61 #undef RGB_PIXELSIZE
62
63 #define RGB_RED  EXT_RGB_RED
64 #define RGB_GREEN  EXT_RGB_GREEN
65 #define RGB_BLUE  EXT_RGB_BLUE
66 #define RGB_PIXELSIZE  EXT_RGB_PIXELSIZE
67 #define h2v1_merged_upsample_internal  extrgb_h2v1_merged_upsample_internal
68 #define h2v2_merged_upsample_internal  extrgb_h2v2_merged_upsample_internal
69 #include "jdmrgext.c"
70 #undef RGB_RED
71 #undef RGB_GREEN
72 #undef RGB_BLUE
73 #undef RGB_PIXELSIZE
74 #undef h2v1_merged_upsample_internal
75 #undef h2v2_merged_upsample_internal
76
77 #define RGB_RED  EXT_RGBX_RED
78 #define RGB_GREEN  EXT_RGBX_GREEN
79 #define RGB_BLUE  EXT_RGBX_BLUE
80 #define RGB_ALPHA  3
81 #define RGB_PIXELSIZE  EXT_RGBX_PIXELSIZE
82 #define h2v1_merged_upsample_internal  extrgbx_h2v1_merged_upsample_internal
83 #define h2v2_merged_upsample_internal  extrgbx_h2v2_merged_upsample_internal
84 #include "jdmrgext.c"
85 #undef RGB_RED
86 #undef RGB_GREEN
87 #undef RGB_BLUE
88 #undef RGB_ALPHA
89 #undef RGB_PIXELSIZE
90 #undef h2v1_merged_upsample_internal
91 #undef h2v2_merged_upsample_internal
92
93 #define RGB_RED  EXT_BGR_RED
94 #define RGB_GREEN  EXT_BGR_GREEN
95 #define RGB_BLUE  EXT_BGR_BLUE
96 #define RGB_PIXELSIZE  EXT_BGR_PIXELSIZE
97 #define h2v1_merged_upsample_internal  extbgr_h2v1_merged_upsample_internal
98 #define h2v2_merged_upsample_internal  extbgr_h2v2_merged_upsample_internal
99 #include "jdmrgext.c"
100 #undef RGB_RED
101 #undef RGB_GREEN
102 #undef RGB_BLUE
103 #undef RGB_PIXELSIZE
104 #undef h2v1_merged_upsample_internal
105 #undef h2v2_merged_upsample_internal
106
107 #define RGB_RED  EXT_BGRX_RED
108 #define RGB_GREEN  EXT_BGRX_GREEN
109 #define RGB_BLUE  EXT_BGRX_BLUE
110 #define RGB_ALPHA  3
111 #define RGB_PIXELSIZE  EXT_BGRX_PIXELSIZE
112 #define h2v1_merged_upsample_internal  extbgrx_h2v1_merged_upsample_internal
113 #define h2v2_merged_upsample_internal  extbgrx_h2v2_merged_upsample_internal
114 #include "jdmrgext.c"
115 #undef RGB_RED
116 #undef RGB_GREEN
117 #undef RGB_BLUE
118 #undef RGB_ALPHA
119 #undef RGB_PIXELSIZE
120 #undef h2v1_merged_upsample_internal
121 #undef h2v2_merged_upsample_internal
122
123 #define RGB_RED  EXT_XBGR_RED
124 #define RGB_GREEN  EXT_XBGR_GREEN
125 #define RGB_BLUE  EXT_XBGR_BLUE
126 #define RGB_ALPHA  0
127 #define RGB_PIXELSIZE  EXT_XBGR_PIXELSIZE
128 #define h2v1_merged_upsample_internal  extxbgr_h2v1_merged_upsample_internal
129 #define h2v2_merged_upsample_internal  extxbgr_h2v2_merged_upsample_internal
130 #include "jdmrgext.c"
131 #undef RGB_RED
132 #undef RGB_GREEN
133 #undef RGB_BLUE
134 #undef RGB_ALPHA
135 #undef RGB_PIXELSIZE
136 #undef h2v1_merged_upsample_internal
137 #undef h2v2_merged_upsample_internal
138
139 #define RGB_RED  EXT_XRGB_RED
140 #define RGB_GREEN  EXT_XRGB_GREEN
141 #define RGB_BLUE  EXT_XRGB_BLUE
142 #define RGB_ALPHA  0
143 #define RGB_PIXELSIZE  EXT_XRGB_PIXELSIZE
144 #define h2v1_merged_upsample_internal  extxrgb_h2v1_merged_upsample_internal
145 #define h2v2_merged_upsample_internal  extxrgb_h2v2_merged_upsample_internal
146 #include "jdmrgext.c"
147 #undef RGB_RED
148 #undef RGB_GREEN
149 #undef RGB_BLUE
150 #undef RGB_ALPHA
151 #undef RGB_PIXELSIZE
152 #undef h2v1_merged_upsample_internal
153 #undef h2v2_merged_upsample_internal
154
155
156 /*
157  * Initialize tables for YCC->RGB colorspace conversion.
158  * This is taken directly from jdcolor.c; see that file for more info.
159  */
160
161 LOCAL(void)
162 build_ycc_rgb_table(j_decompress_ptr cinfo)
163 {
164   my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
165   int i;
166   JLONG x;
167   SHIFT_TEMPS
168
169   upsample->Cr_r_tab = (int *)
170     (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
171                                 (MAXJSAMPLE + 1) * sizeof(int));
172   upsample->Cb_b_tab = (int *)
173     (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
174                                 (MAXJSAMPLE + 1) * sizeof(int));
175   upsample->Cr_g_tab = (JLONG *)
176     (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
177                                 (MAXJSAMPLE + 1) * sizeof(JLONG));
178   upsample->Cb_g_tab = (JLONG *)
179     (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
180                                 (MAXJSAMPLE + 1) * sizeof(JLONG));
181
182   for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
183     /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
184     /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
185     /* Cr=>R value is nearest int to 1.40200 * x */
186     upsample->Cr_r_tab[i] = (int)
187                     RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
188     /* Cb=>B value is nearest int to 1.77200 * x */
189     upsample->Cb_b_tab[i] = (int)
190                     RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
191     /* Cr=>G value is scaled-up -0.71414 * x */
192     upsample->Cr_g_tab[i] = (-FIX(0.71414)) * x;
193     /* Cb=>G value is scaled-up -0.34414 * x */
194     /* We also add in ONE_HALF so that need not do it in inner loop */
195     upsample->Cb_g_tab[i] = (-FIX(0.34414)) * x + ONE_HALF;
196   }
197 }
198
199
200 /*
201  * Initialize for an upsampling pass.
202  */
203
204 METHODDEF(void)
205 start_pass_merged_upsample(j_decompress_ptr cinfo)
206 {
207   my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
208
209   /* Mark the spare buffer empty */
210   upsample->spare_full = FALSE;
211   /* Initialize total-height counter for detecting bottom of image */
212   upsample->rows_to_go = cinfo->output_height;
213 }
214
215
216 /*
217  * Control routine to do upsampling (and color conversion).
218  *
219  * The control routine just handles the row buffering considerations.
220  */
221
222 METHODDEF(void)
223 merged_2v_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
224                    JDIMENSION *in_row_group_ctr,
225                    JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
226                    JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
227 /* 2:1 vertical sampling case: may need a spare row. */
228 {
229   my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
230   JSAMPROW work_ptrs[2];
231   JDIMENSION num_rows;          /* number of rows returned to caller */
232 #if _USE_PRODUCT_TV
233   int skip = 0;
234 #endif
235
236   if (upsample->spare_full) {
237     /* If we have a spare row saved from a previous cycle, just return it. */
238     JDIMENSION size = upsample->out_row_width;
239     if (cinfo->out_color_space == JCS_RGB565)
240       size = cinfo->output_width * 2;
241 #if _USE_PRODUCT_TV
242     jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0, 1,
243                       upsample->out_row_width);
244 #else
245     jcopy_sample_rows(&upsample->spare_row, 0, output_buf + *out_row_ctr, 0, 1,
246                       size);
247 #endif
248     num_rows = 1;
249     upsample->spare_full = FALSE;
250   } else {
251 #if _USE_PRODUCT_TV
252     int _region_y = (int)cinfo->region_y;
253     _region_y = (_region_y>>1)<<1;
254     if ((cinfo->region_w > 0) && (cinfo->region_h > 0)) {
255       if (((int)cinfo->output_scanline < _region_y) ||
256         ((int)cinfo->output_scanline >= (_region_y + (int)cinfo->region_h)))
257         skip = 1;
258     }
259 #endif
260     /* Figure number of rows to return to caller. */
261     num_rows = 2;
262     /* Not more than the distance to the end of the image. */
263     if (num_rows > upsample->rows_to_go)
264       num_rows = upsample->rows_to_go;
265     /* And not more than what the client can accept: */
266     out_rows_avail -= *out_row_ctr;
267     if (num_rows > out_rows_avail)
268       num_rows = out_rows_avail;
269     /* Create output pointer array for upsampler. */
270     work_ptrs[0] = output_buf[*out_row_ctr];
271     if (num_rows > 1) {
272       work_ptrs[1] = output_buf[*out_row_ctr + 1];
273     } else {
274       work_ptrs[1] = upsample->spare_row;
275       upsample->spare_full = TRUE;
276     }
277     /* Now do the upsampling. */
278 #if _USE_PRODUCT_TV
279   if (!skip)
280     (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
281 #else
282     (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
283 #endif
284   }
285
286   /* Adjust counts */
287   *out_row_ctr += num_rows;
288   upsample->rows_to_go -= num_rows;
289   /* When the buffer is emptied, declare this input row group consumed */
290   if (!upsample->spare_full)
291     (*in_row_group_ctr)++;
292 }
293
294
295 METHODDEF(void)
296 merged_1v_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
297                    JDIMENSION *in_row_group_ctr,
298                    JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
299                    JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
300 /* 1:1 vertical sampling case: much easier, never need a spare row. */
301 {
302   my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
303
304   /* Just do the upsampling. */
305   (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
306                          output_buf + *out_row_ctr);
307   /* Adjust counts */
308   (*out_row_ctr)++;
309   (*in_row_group_ctr)++;
310 }
311
312
313 /*
314  * These are the routines invoked by the control routines to do
315  * the actual upsampling/conversion.  One row group is processed per call.
316  *
317  * Note: since we may be writing directly into application-supplied buffers,
318  * we have to be honest about the output width; we can't assume the buffer
319  * has been rounded up to an even width.
320  */
321
322
323 /*
324  * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
325  */
326
327 METHODDEF(void)
328 h2v1_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
329                      JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
330 {
331   switch (cinfo->out_color_space) {
332   case JCS_EXT_RGB:
333     extrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
334                                          output_buf);
335     break;
336   case JCS_EXT_RGBX:
337   case JCS_EXT_RGBA:
338     extrgbx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
339                                           output_buf);
340     break;
341   case JCS_EXT_BGR:
342     extbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
343                                          output_buf);
344     break;
345   case JCS_EXT_BGRX:
346   case JCS_EXT_BGRA:
347     extbgrx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
348                                           output_buf);
349     break;
350   case JCS_EXT_XBGR:
351   case JCS_EXT_ABGR:
352     extxbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
353                                           output_buf);
354     break;
355   case JCS_EXT_XRGB:
356   case JCS_EXT_ARGB:
357     extxrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
358                                           output_buf);
359     break;
360   default:
361     h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
362                                   output_buf);
363     break;
364   }
365 }
366
367
368 /*
369  * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
370  */
371
372 METHODDEF(void)
373 h2v2_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
374                      JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
375 {
376   switch (cinfo->out_color_space) {
377   case JCS_EXT_RGB:
378     extrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
379                                          output_buf);
380     break;
381   case JCS_EXT_RGBX:
382   case JCS_EXT_RGBA:
383     extrgbx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
384                                           output_buf);
385     break;
386   case JCS_EXT_BGR:
387     extbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
388                                          output_buf);
389     break;
390   case JCS_EXT_BGRX:
391   case JCS_EXT_BGRA:
392     extbgrx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
393                                           output_buf);
394     break;
395   case JCS_EXT_XBGR:
396   case JCS_EXT_ABGR:
397     extxbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
398                                           output_buf);
399     break;
400   case JCS_EXT_XRGB:
401   case JCS_EXT_ARGB:
402     extxrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
403                                           output_buf);
404     break;
405   default:
406     h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
407                                   output_buf);
408     break;
409   }
410 }
411
412
413 /*
414  * RGB565 conversion
415  */
416
417 #define PACK_SHORT_565_LE(r, g, b) \
418   ((((r) << 8) & 0xF800) | (((g) << 3) & 0x7E0) | ((b) >> 3))
419 #define PACK_SHORT_565_BE(r, g, b) \
420   (((r) & 0xF8) | ((g) >> 5) | (((g) << 11) & 0xE000) | (((b) << 5) & 0x1F00))
421
422 #define PACK_TWO_PIXELS_LE(l, r)    ((r << 16) | l)
423 #define PACK_TWO_PIXELS_BE(l, r)    ((l << 16) | r)
424
425 #define WRITE_TWO_PIXELS_LE(addr, pixels) { \
426   ((INT16 *)(addr))[0] = (INT16)(pixels); \
427   ((INT16 *)(addr))[1] = (INT16)((pixels) >> 16); \
428 }
429 #define WRITE_TWO_PIXELS_BE(addr, pixels) { \
430   ((INT16 *)(addr))[1] = (INT16)(pixels); \
431   ((INT16 *)(addr))[0] = (INT16)((pixels) >> 16); \
432 }
433
434 #define DITHER_565_R(r, dither)  ((r) + ((dither) & 0xFF))
435 #define DITHER_565_G(g, dither)  ((g) + (((dither) & 0xFF) >> 1))
436 #define DITHER_565_B(b, dither)  ((b) + ((dither) & 0xFF))
437
438
439 /* Declarations for ordered dithering
440  *
441  * We use a 4x4 ordered dither array packed into 32 bits.  This array is
442  * sufficient for dithering RGB888 to RGB565.
443  */
444
445 #define DITHER_MASK       0x3
446 #define DITHER_ROTATE(x)  ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
447 static const JLONG dither_matrix[4] = {
448   0x0008020A,
449   0x0C040E06,
450   0x030B0109,
451   0x0F070D05
452 };
453
454
455 /* Include inline routines for RGB565 conversion */
456
457 #define PACK_SHORT_565  PACK_SHORT_565_LE
458 #define PACK_TWO_PIXELS  PACK_TWO_PIXELS_LE
459 #define WRITE_TWO_PIXELS  WRITE_TWO_PIXELS_LE
460 #define h2v1_merged_upsample_565_internal  h2v1_merged_upsample_565_le
461 #define h2v1_merged_upsample_565D_internal  h2v1_merged_upsample_565D_le
462 #define h2v2_merged_upsample_565_internal  h2v2_merged_upsample_565_le
463 #define h2v2_merged_upsample_565D_internal  h2v2_merged_upsample_565D_le
464 #include "jdmrg565.c"
465 #undef PACK_SHORT_565
466 #undef PACK_TWO_PIXELS
467 #undef WRITE_TWO_PIXELS
468 #undef h2v1_merged_upsample_565_internal
469 #undef h2v1_merged_upsample_565D_internal
470 #undef h2v2_merged_upsample_565_internal
471 #undef h2v2_merged_upsample_565D_internal
472
473 #define PACK_SHORT_565  PACK_SHORT_565_BE
474 #define PACK_TWO_PIXELS  PACK_TWO_PIXELS_BE
475 #define WRITE_TWO_PIXELS  WRITE_TWO_PIXELS_BE
476 #define h2v1_merged_upsample_565_internal  h2v1_merged_upsample_565_be
477 #define h2v1_merged_upsample_565D_internal  h2v1_merged_upsample_565D_be
478 #define h2v2_merged_upsample_565_internal  h2v2_merged_upsample_565_be
479 #define h2v2_merged_upsample_565D_internal  h2v2_merged_upsample_565D_be
480 #include "jdmrg565.c"
481 #undef PACK_SHORT_565
482 #undef PACK_TWO_PIXELS
483 #undef WRITE_TWO_PIXELS
484 #undef h2v1_merged_upsample_565_internal
485 #undef h2v1_merged_upsample_565D_internal
486 #undef h2v2_merged_upsample_565_internal
487 #undef h2v2_merged_upsample_565D_internal
488
489
490 static INLINE boolean is_big_endian(void)
491 {
492   int test_value = 1;
493   if (*(char *)&test_value != 1)
494     return TRUE;
495   return FALSE;
496 }
497
498
499 METHODDEF(void)
500 h2v1_merged_upsample_565(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
501                          JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
502 {
503   if (is_big_endian())
504     h2v1_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
505                                 output_buf);
506   else
507     h2v1_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
508                                 output_buf);
509 }
510
511
512 METHODDEF(void)
513 h2v1_merged_upsample_565D(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
514                           JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
515 {
516   if (is_big_endian())
517     h2v1_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
518                                  output_buf);
519   else
520     h2v1_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
521                                  output_buf);
522 }
523
524
525 METHODDEF(void)
526 h2v2_merged_upsample_565(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
527                          JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
528 {
529   if (is_big_endian())
530     h2v2_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
531                                 output_buf);
532   else
533     h2v2_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
534                                 output_buf);
535 }
536
537
538 METHODDEF(void)
539 h2v2_merged_upsample_565D(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
540                           JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
541 {
542   if (is_big_endian())
543     h2v2_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
544                                  output_buf);
545   else
546     h2v2_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
547                                  output_buf);
548 }
549
550
551 /*
552  * Module initialization routine for merged upsampling/color conversion.
553  *
554  * NB: this is called under the conditions determined by use_merged_upsample()
555  * in jdmaster.c.  That routine MUST correspond to the actual capabilities
556  * of this module; no safety checks are made here.
557  */
558
559 GLOBAL(void)
560 jinit_merged_upsampler(j_decompress_ptr cinfo)
561 {
562   my_merged_upsample_ptr upsample;
563
564   upsample = (my_merged_upsample_ptr)
565     (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
566                                 sizeof(my_merged_upsampler));
567   cinfo->upsample = (struct jpeg_upsampler *)upsample;
568   upsample->pub.start_pass = start_pass_merged_upsample;
569   upsample->pub.need_context_rows = FALSE;
570
571   upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
572
573   if (cinfo->max_v_samp_factor == 2) {
574     upsample->pub.upsample = merged_2v_upsample;
575     if (jsimd_can_h2v2_merged_upsample())
576       upsample->upmethod = jsimd_h2v2_merged_upsample;
577     else
578       upsample->upmethod = h2v2_merged_upsample;
579     if (cinfo->out_color_space == JCS_RGB565) {
580       if (cinfo->dither_mode != JDITHER_NONE) {
581         upsample->upmethod = h2v2_merged_upsample_565D;
582       } else {
583         upsample->upmethod = h2v2_merged_upsample_565;
584       }
585     }
586     /* Allocate a spare row buffer */
587     upsample->spare_row = (JSAMPROW)
588       (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
589                 (size_t)(upsample->out_row_width * sizeof(JSAMPLE)));
590   } else {
591     upsample->pub.upsample = merged_1v_upsample;
592     if (jsimd_can_h2v1_merged_upsample())
593       upsample->upmethod = jsimd_h2v1_merged_upsample;
594     else
595       upsample->upmethod = h2v1_merged_upsample;
596     if (cinfo->out_color_space == JCS_RGB565) {
597       if (cinfo->dither_mode != JDITHER_NONE) {
598         upsample->upmethod = h2v1_merged_upsample_565D;
599       } else {
600         upsample->upmethod = h2v1_merged_upsample_565;
601       }
602     }
603     /* No spare row needed */
604     upsample->spare_row = NULL;
605   }
606
607   build_ycc_rgb_table(cinfo);
608 }
609
610 #endif /* UPSAMPLE_MERGING_SUPPORTED */