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