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