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