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
13 * This file contains code for merged upsampling/color conversion.
15 * This file combines functions from jdsample.c and jdcolor.c;
16 * read those files first to understand what's going on.
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
23 * G = Y + K2 * Cb + K3 * Cr
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.
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.)
40 #define JPEG_INTERNALS
45 #include "jconfigint.h"
47 #ifdef UPSAMPLE_MERGING_SUPPORTED
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))
55 /* Include inline routines for colorspace extensions */
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
74 #undef h2v1_merged_upsample_internal
75 #undef h2v2_merged_upsample_internal
77 #define RGB_RED EXT_RGBX_RED
78 #define RGB_GREEN EXT_RGBX_GREEN
79 #define RGB_BLUE EXT_RGBX_BLUE
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
90 #undef h2v1_merged_upsample_internal
91 #undef h2v2_merged_upsample_internal
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
104 #undef h2v1_merged_upsample_internal
105 #undef h2v2_merged_upsample_internal
107 #define RGB_RED EXT_BGRX_RED
108 #define RGB_GREEN EXT_BGRX_GREEN
109 #define RGB_BLUE EXT_BGRX_BLUE
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"
120 #undef h2v1_merged_upsample_internal
121 #undef h2v2_merged_upsample_internal
123 #define RGB_RED EXT_XBGR_RED
124 #define RGB_GREEN EXT_XBGR_GREEN
125 #define RGB_BLUE EXT_XBGR_BLUE
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"
136 #undef h2v1_merged_upsample_internal
137 #undef h2v2_merged_upsample_internal
139 #define RGB_RED EXT_XRGB_RED
140 #define RGB_GREEN EXT_XRGB_GREEN
141 #define RGB_BLUE EXT_XRGB_BLUE
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"
152 #undef h2v1_merged_upsample_internal
153 #undef h2v2_merged_upsample_internal
157 * Initialize tables for YCC->RGB colorspace conversion.
158 * This is taken directly from jdcolor.c; see that file for more info.
162 build_ycc_rgb_table(j_decompress_ptr cinfo)
164 my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
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));
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;
201 * Initialize for an upsampling pass.
205 start_pass_merged_upsample(j_decompress_ptr cinfo)
207 my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
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;
217 * Control routine to do upsampling (and color conversion).
219 * The control routine just handles the row buffering considerations.
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. */
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 */
236 if (upsample->spare_full) {
237 /* If we have a spare row saved from a previous cycle, just return it. */
238 JDIMENSION size = upsample->out_row_width;
239 if (cinfo->out_color_space == JCS_RGB565)
240 size = cinfo->output_width * 2;
242 jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0, 1,
243 upsample->out_row_width);
245 jcopy_sample_rows(&upsample->spare_row, 0, output_buf + *out_row_ctr, 0, 1,
249 upsample->spare_full = FALSE;
252 int _region_y = (int)cinfo->region_y;
253 _region_y = (_region_y>>1)<<1;
254 if ((cinfo->region_w > 0) && (cinfo->region_h > 0)) {
255 if (((int)cinfo->output_scanline < _region_y) ||
256 ((int)cinfo->output_scanline >= (_region_y + (int)cinfo->region_h)))
260 /* Figure number of rows to return to caller. */
262 /* Not more than the distance to the end of the image. */
263 if (num_rows > upsample->rows_to_go)
264 num_rows = upsample->rows_to_go;
265 /* And not more than what the client can accept: */
266 out_rows_avail -= *out_row_ctr;
267 if (num_rows > out_rows_avail)
268 num_rows = out_rows_avail;
269 /* Create output pointer array for upsampler. */
270 work_ptrs[0] = output_buf[*out_row_ctr];
272 work_ptrs[1] = output_buf[*out_row_ctr + 1];
274 work_ptrs[1] = upsample->spare_row;
275 upsample->spare_full = TRUE;
277 /* Now do the upsampling. */
280 (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
282 (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
287 *out_row_ctr += num_rows;
288 upsample->rows_to_go -= num_rows;
289 /* When the buffer is emptied, declare this input row group consumed */
290 if (!upsample->spare_full)
291 (*in_row_group_ctr)++;
296 merged_1v_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
297 JDIMENSION *in_row_group_ctr,
298 JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
299 JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
300 /* 1:1 vertical sampling case: much easier, never need a spare row. */
302 my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
304 /* Just do the upsampling. */
305 (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
306 output_buf + *out_row_ctr);
309 (*in_row_group_ctr)++;
314 * These are the routines invoked by the control routines to do
315 * the actual upsampling/conversion. One row group is processed per call.
317 * Note: since we may be writing directly into application-supplied buffers,
318 * we have to be honest about the output width; we can't assume the buffer
319 * has been rounded up to an even width.
324 * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
328 h2v1_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
329 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
331 switch (cinfo->out_color_space) {
333 extrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
338 extrgbx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
342 extbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
347 extbgrx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
352 extxbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
357 extxrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
361 h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
369 * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
373 h2v2_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
374 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
376 switch (cinfo->out_color_space) {
378 extrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
383 extrgbx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
387 extbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
392 extbgrx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
397 extxbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
402 extxrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
406 h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
417 #define PACK_SHORT_565_LE(r, g, b) \
418 ((((r) << 8) & 0xF800) | (((g) << 3) & 0x7E0) | ((b) >> 3))
419 #define PACK_SHORT_565_BE(r, g, b) \
420 (((r) & 0xF8) | ((g) >> 5) | (((g) << 11) & 0xE000) | (((b) << 5) & 0x1F00))
422 #define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l)
423 #define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r)
425 #define WRITE_TWO_PIXELS_LE(addr, pixels) { \
426 ((INT16 *)(addr))[0] = (INT16)(pixels); \
427 ((INT16 *)(addr))[1] = (INT16)((pixels) >> 16); \
429 #define WRITE_TWO_PIXELS_BE(addr, pixels) { \
430 ((INT16 *)(addr))[1] = (INT16)(pixels); \
431 ((INT16 *)(addr))[0] = (INT16)((pixels) >> 16); \
434 #define DITHER_565_R(r, dither) ((r) + ((dither) & 0xFF))
435 #define DITHER_565_G(g, dither) ((g) + (((dither) & 0xFF) >> 1))
436 #define DITHER_565_B(b, dither) ((b) + ((dither) & 0xFF))
439 /* Declarations for ordered dithering
441 * We use a 4x4 ordered dither array packed into 32 bits. This array is
442 * sufficient for dithering RGB888 to RGB565.
445 #define DITHER_MASK 0x3
446 #define DITHER_ROTATE(x) ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
447 static const JLONG dither_matrix[4] = {
455 /* Include inline routines for RGB565 conversion */
457 #define PACK_SHORT_565 PACK_SHORT_565_LE
458 #define PACK_TWO_PIXELS PACK_TWO_PIXELS_LE
459 #define WRITE_TWO_PIXELS WRITE_TWO_PIXELS_LE
460 #define h2v1_merged_upsample_565_internal h2v1_merged_upsample_565_le
461 #define h2v1_merged_upsample_565D_internal h2v1_merged_upsample_565D_le
462 #define h2v2_merged_upsample_565_internal h2v2_merged_upsample_565_le
463 #define h2v2_merged_upsample_565D_internal h2v2_merged_upsample_565D_le
464 #include "jdmrg565.c"
465 #undef PACK_SHORT_565
466 #undef PACK_TWO_PIXELS
467 #undef WRITE_TWO_PIXELS
468 #undef h2v1_merged_upsample_565_internal
469 #undef h2v1_merged_upsample_565D_internal
470 #undef h2v2_merged_upsample_565_internal
471 #undef h2v2_merged_upsample_565D_internal
473 #define PACK_SHORT_565 PACK_SHORT_565_BE
474 #define PACK_TWO_PIXELS PACK_TWO_PIXELS_BE
475 #define WRITE_TWO_PIXELS WRITE_TWO_PIXELS_BE
476 #define h2v1_merged_upsample_565_internal h2v1_merged_upsample_565_be
477 #define h2v1_merged_upsample_565D_internal h2v1_merged_upsample_565D_be
478 #define h2v2_merged_upsample_565_internal h2v2_merged_upsample_565_be
479 #define h2v2_merged_upsample_565D_internal h2v2_merged_upsample_565D_be
480 #include "jdmrg565.c"
481 #undef PACK_SHORT_565
482 #undef PACK_TWO_PIXELS
483 #undef WRITE_TWO_PIXELS
484 #undef h2v1_merged_upsample_565_internal
485 #undef h2v1_merged_upsample_565D_internal
486 #undef h2v2_merged_upsample_565_internal
487 #undef h2v2_merged_upsample_565D_internal
490 static INLINE boolean is_big_endian(void)
493 if (*(char *)&test_value != 1)
500 h2v1_merged_upsample_565(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
501 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
504 h2v1_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
507 h2v1_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
513 h2v1_merged_upsample_565D(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
514 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
517 h2v1_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
520 h2v1_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
526 h2v2_merged_upsample_565(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
527 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
530 h2v2_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
533 h2v2_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
539 h2v2_merged_upsample_565D(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
540 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
543 h2v2_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
546 h2v2_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
552 * Module initialization routine for merged upsampling/color conversion.
554 * NB: this is called under the conditions determined by use_merged_upsample()
555 * in jdmaster.c. That routine MUST correspond to the actual capabilities
556 * of this module; no safety checks are made here.
560 jinit_merged_upsampler(j_decompress_ptr cinfo)
562 my_merged_upsample_ptr upsample;
564 upsample = (my_merged_upsample_ptr)
565 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
566 sizeof(my_merged_upsampler));
567 cinfo->upsample = (struct jpeg_upsampler *)upsample;
568 upsample->pub.start_pass = start_pass_merged_upsample;
569 upsample->pub.need_context_rows = FALSE;
571 upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
573 if (cinfo->max_v_samp_factor == 2) {
574 upsample->pub.upsample = merged_2v_upsample;
575 if (jsimd_can_h2v2_merged_upsample())
576 upsample->upmethod = jsimd_h2v2_merged_upsample;
578 upsample->upmethod = h2v2_merged_upsample;
579 if (cinfo->out_color_space == JCS_RGB565) {
580 if (cinfo->dither_mode != JDITHER_NONE) {
581 upsample->upmethod = h2v2_merged_upsample_565D;
583 upsample->upmethod = h2v2_merged_upsample_565;
586 /* Allocate a spare row buffer */
587 upsample->spare_row = (JSAMPROW)
588 (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
589 (size_t)(upsample->out_row_width * sizeof(JSAMPLE)));
591 upsample->pub.upsample = merged_1v_upsample;
592 if (jsimd_can_h2v1_merged_upsample())
593 upsample->upmethod = jsimd_h2v1_merged_upsample;
595 upsample->upmethod = h2v1_merged_upsample;
596 if (cinfo->out_color_space == JCS_RGB565) {
597 if (cinfo->dither_mode != JDITHER_NONE) {
598 upsample->upmethod = h2v1_merged_upsample_565D;
600 upsample->upmethod = h2v1_merged_upsample_565;
603 /* No spare row needed */
604 upsample->spare_row = NULL;
607 build_ycc_rgb_table(cinfo);
610 #endif /* UPSAMPLE_MERGING_SUPPORTED */