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)
build_ycc_rgb_table(j_decompress_ptr cinfo)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)
start_pass_merged_upsample(j_decompress_ptr cinfo)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)
merged_2v_upsample(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION * in_row_group_ctr,JDIMENSION in_row_groups_avail,JSAMPARRAY output_buf,JDIMENSION * out_row_ctr,JDIMENSION out_rows_avail)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 
233   if (upsample->spare_full) {
234     /* If we have a spare row saved from a previous cycle, just return it. */
235     JDIMENSION size = upsample->out_row_width;
236     if (cinfo->out_color_space == JCS_RGB565)
237       size = cinfo->output_width * 2;
238     jcopy_sample_rows(&upsample->spare_row, 0, output_buf + *out_row_ctr, 0, 1,
239                       size);
240     num_rows = 1;
241     upsample->spare_full = FALSE;
242   } else {
243     /* Figure number of rows to return to caller. */
244     num_rows = 2;
245     /* Not more than the distance to the end of the image. */
246     if (num_rows > upsample->rows_to_go)
247       num_rows = upsample->rows_to_go;
248     /* And not more than what the client can accept: */
249     out_rows_avail -= *out_row_ctr;
250     if (num_rows > out_rows_avail)
251       num_rows = out_rows_avail;
252     /* Create output pointer array for upsampler. */
253     work_ptrs[0] = output_buf[*out_row_ctr];
254     if (num_rows > 1) {
255       work_ptrs[1] = output_buf[*out_row_ctr + 1];
256     } else {
257       work_ptrs[1] = upsample->spare_row;
258       upsample->spare_full = TRUE;
259     }
260     /* Now do the upsampling. */
261     (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
262   }
263 
264   /* Adjust counts */
265   *out_row_ctr += num_rows;
266   upsample->rows_to_go -= num_rows;
267   /* When the buffer is emptied, declare this input row group consumed */
268   if (!upsample->spare_full)
269     (*in_row_group_ctr)++;
270 }
271 
272 
273 METHODDEF(void)
merged_1v_upsample(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION * in_row_group_ctr,JDIMENSION in_row_groups_avail,JSAMPARRAY output_buf,JDIMENSION * out_row_ctr,JDIMENSION out_rows_avail)274 merged_1v_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
275                    JDIMENSION *in_row_group_ctr,
276                    JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
277                    JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
278 /* 1:1 vertical sampling case: much easier, never need a spare row. */
279 {
280   my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
281 
282   /* Just do the upsampling. */
283   (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
284                          output_buf + *out_row_ctr);
285   /* Adjust counts */
286   (*out_row_ctr)++;
287   (*in_row_group_ctr)++;
288 }
289 
290 
291 /*
292  * These are the routines invoked by the control routines to do
293  * the actual upsampling/conversion.  One row group is processed per call.
294  *
295  * Note: since we may be writing directly into application-supplied buffers,
296  * we have to be honest about the output width; we can't assume the buffer
297  * has been rounded up to an even width.
298  */
299 
300 
301 /*
302  * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
303  */
304 
305 METHODDEF(void)
h2v1_merged_upsample(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION in_row_group_ctr,JSAMPARRAY output_buf)306 h2v1_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
307                      JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
308 {
309   switch (cinfo->out_color_space) {
310   case JCS_EXT_RGB:
311     extrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
312                                          output_buf);
313     break;
314   case JCS_EXT_RGBX:
315   case JCS_EXT_RGBA:
316     extrgbx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
317                                           output_buf);
318     break;
319   case JCS_EXT_BGR:
320     extbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
321                                          output_buf);
322     break;
323   case JCS_EXT_BGRX:
324   case JCS_EXT_BGRA:
325     extbgrx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
326                                           output_buf);
327     break;
328   case JCS_EXT_XBGR:
329   case JCS_EXT_ABGR:
330     extxbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
331                                           output_buf);
332     break;
333   case JCS_EXT_XRGB:
334   case JCS_EXT_ARGB:
335     extxrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
336                                           output_buf);
337     break;
338   default:
339     h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
340                                   output_buf);
341     break;
342   }
343 }
344 
345 
346 /*
347  * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
348  */
349 
350 METHODDEF(void)
h2v2_merged_upsample(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION in_row_group_ctr,JSAMPARRAY output_buf)351 h2v2_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
352                      JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
353 {
354   switch (cinfo->out_color_space) {
355   case JCS_EXT_RGB:
356     extrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
357                                          output_buf);
358     break;
359   case JCS_EXT_RGBX:
360   case JCS_EXT_RGBA:
361     extrgbx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
362                                           output_buf);
363     break;
364   case JCS_EXT_BGR:
365     extbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
366                                          output_buf);
367     break;
368   case JCS_EXT_BGRX:
369   case JCS_EXT_BGRA:
370     extbgrx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
371                                           output_buf);
372     break;
373   case JCS_EXT_XBGR:
374   case JCS_EXT_ABGR:
375     extxbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
376                                           output_buf);
377     break;
378   case JCS_EXT_XRGB:
379   case JCS_EXT_ARGB:
380     extxrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
381                                           output_buf);
382     break;
383   default:
384     h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
385                                   output_buf);
386     break;
387   }
388 }
389 
390 
391 /*
392  * RGB565 conversion
393  */
394 
395 #define PACK_SHORT_565_LE(r, g, b) \
396   ((((r) << 8) & 0xF800) | (((g) << 3) & 0x7E0) | ((b) >> 3))
397 #define PACK_SHORT_565_BE(r, g, b) \
398   (((r) & 0xF8) | ((g) >> 5) | (((g) << 11) & 0xE000) | (((b) << 5) & 0x1F00))
399 
400 #define PACK_TWO_PIXELS_LE(l, r)    ((r << 16) | l)
401 #define PACK_TWO_PIXELS_BE(l, r)    ((l << 16) | r)
402 
403 #define WRITE_TWO_PIXELS_LE(addr, pixels) { \
404   ((INT16 *)(addr))[0] = (INT16)(pixels); \
405   ((INT16 *)(addr))[1] = (INT16)((pixels) >> 16); \
406 }
407 #define WRITE_TWO_PIXELS_BE(addr, pixels) { \
408   ((INT16 *)(addr))[1] = (INT16)(pixels); \
409   ((INT16 *)(addr))[0] = (INT16)((pixels) >> 16); \
410 }
411 
412 #define DITHER_565_R(r, dither)  ((r) + ((dither) & 0xFF))
413 #define DITHER_565_G(g, dither)  ((g) + (((dither) & 0xFF) >> 1))
414 #define DITHER_565_B(b, dither)  ((b) + ((dither) & 0xFF))
415 
416 
417 /* Declarations for ordered dithering
418  *
419  * We use a 4x4 ordered dither array packed into 32 bits.  This array is
420  * sufficient for dithering RGB888 to RGB565.
421  */
422 
423 #define DITHER_MASK       0x3
424 #define DITHER_ROTATE(x)  ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
425 static const JLONG dither_matrix[4] = {
426   0x0008020A,
427   0x0C040E06,
428   0x030B0109,
429   0x0F070D05
430 };
431 
432 
433 /* Include inline routines for RGB565 conversion */
434 
435 #define PACK_SHORT_565  PACK_SHORT_565_LE
436 #define PACK_TWO_PIXELS  PACK_TWO_PIXELS_LE
437 #define WRITE_TWO_PIXELS  WRITE_TWO_PIXELS_LE
438 #define h2v1_merged_upsample_565_internal  h2v1_merged_upsample_565_le
439 #define h2v1_merged_upsample_565D_internal  h2v1_merged_upsample_565D_le
440 #define h2v2_merged_upsample_565_internal  h2v2_merged_upsample_565_le
441 #define h2v2_merged_upsample_565D_internal  h2v2_merged_upsample_565D_le
442 #include "jdmrg565.c"
443 #undef PACK_SHORT_565
444 #undef PACK_TWO_PIXELS
445 #undef WRITE_TWO_PIXELS
446 #undef h2v1_merged_upsample_565_internal
447 #undef h2v1_merged_upsample_565D_internal
448 #undef h2v2_merged_upsample_565_internal
449 #undef h2v2_merged_upsample_565D_internal
450 
451 #define PACK_SHORT_565  PACK_SHORT_565_BE
452 #define PACK_TWO_PIXELS  PACK_TWO_PIXELS_BE
453 #define WRITE_TWO_PIXELS  WRITE_TWO_PIXELS_BE
454 #define h2v1_merged_upsample_565_internal  h2v1_merged_upsample_565_be
455 #define h2v1_merged_upsample_565D_internal  h2v1_merged_upsample_565D_be
456 #define h2v2_merged_upsample_565_internal  h2v2_merged_upsample_565_be
457 #define h2v2_merged_upsample_565D_internal  h2v2_merged_upsample_565D_be
458 #include "jdmrg565.c"
459 #undef PACK_SHORT_565
460 #undef PACK_TWO_PIXELS
461 #undef WRITE_TWO_PIXELS
462 #undef h2v1_merged_upsample_565_internal
463 #undef h2v1_merged_upsample_565D_internal
464 #undef h2v2_merged_upsample_565_internal
465 #undef h2v2_merged_upsample_565D_internal
466 
467 
is_big_endian(void)468 static INLINE boolean is_big_endian(void)
469 {
470   int test_value = 1;
471   if (*(char *)&test_value != 1)
472     return TRUE;
473   return FALSE;
474 }
475 
476 
477 METHODDEF(void)
h2v1_merged_upsample_565(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION in_row_group_ctr,JSAMPARRAY output_buf)478 h2v1_merged_upsample_565(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
479                          JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
480 {
481   if (is_big_endian())
482     h2v1_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
483                                 output_buf);
484   else
485     h2v1_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
486                                 output_buf);
487 }
488 
489 
490 METHODDEF(void)
h2v1_merged_upsample_565D(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION in_row_group_ctr,JSAMPARRAY output_buf)491 h2v1_merged_upsample_565D(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
492                           JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
493 {
494   if (is_big_endian())
495     h2v1_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
496                                  output_buf);
497   else
498     h2v1_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
499                                  output_buf);
500 }
501 
502 
503 METHODDEF(void)
h2v2_merged_upsample_565(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION in_row_group_ctr,JSAMPARRAY output_buf)504 h2v2_merged_upsample_565(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
505                          JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
506 {
507   if (is_big_endian())
508     h2v2_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
509                                 output_buf);
510   else
511     h2v2_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
512                                 output_buf);
513 }
514 
515 
516 METHODDEF(void)
h2v2_merged_upsample_565D(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION in_row_group_ctr,JSAMPARRAY output_buf)517 h2v2_merged_upsample_565D(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
518                           JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
519 {
520   if (is_big_endian())
521     h2v2_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
522                                  output_buf);
523   else
524     h2v2_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
525                                  output_buf);
526 }
527 
528 
529 /*
530  * Module initialization routine for merged upsampling/color conversion.
531  *
532  * NB: this is called under the conditions determined by use_merged_upsample()
533  * in jdmaster.c.  That routine MUST correspond to the actual capabilities
534  * of this module; no safety checks are made here.
535  */
536 
537 GLOBAL(void)
jinit_merged_upsampler(j_decompress_ptr cinfo)538 jinit_merged_upsampler(j_decompress_ptr cinfo)
539 {
540   my_merged_upsample_ptr upsample;
541 
542   upsample = (my_merged_upsample_ptr)
543     (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
544                                 sizeof(my_merged_upsampler));
545   cinfo->upsample = (struct jpeg_upsampler *)upsample;
546   upsample->pub.start_pass = start_pass_merged_upsample;
547   upsample->pub.need_context_rows = FALSE;
548 
549   upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
550 
551   if (cinfo->max_v_samp_factor == 2) {
552     upsample->pub.upsample = merged_2v_upsample;
553     if (jsimd_can_h2v2_merged_upsample())
554       upsample->upmethod = jsimd_h2v2_merged_upsample;
555     else
556       upsample->upmethod = h2v2_merged_upsample;
557     if (cinfo->out_color_space == JCS_RGB565) {
558       if (cinfo->dither_mode != JDITHER_NONE) {
559         upsample->upmethod = h2v2_merged_upsample_565D;
560       } else {
561         upsample->upmethod = h2v2_merged_upsample_565;
562       }
563     }
564     /* Allocate a spare row buffer */
565     upsample->spare_row = (JSAMPROW)
566       (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
567                 (size_t)(upsample->out_row_width * sizeof(JSAMPLE)));
568   } else {
569     upsample->pub.upsample = merged_1v_upsample;
570     if (jsimd_can_h2v1_merged_upsample())
571       upsample->upmethod = jsimd_h2v1_merged_upsample;
572     else
573       upsample->upmethod = h2v1_merged_upsample;
574     if (cinfo->out_color_space == JCS_RGB565) {
575       if (cinfo->dither_mode != JDITHER_NONE) {
576         upsample->upmethod = h2v1_merged_upsample_565D;
577       } else {
578         upsample->upmethod = h2v1_merged_upsample_565;
579       }
580     }
581     /* No spare row needed */
582     upsample->spare_row = NULL;
583   }
584 
585   build_ycc_rgb_table(cinfo);
586 }
587 
588 #endif /* UPSAMPLE_MERGING_SUPPORTED */
589