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 */
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,
241 upsample->spare_full = FALSE;
243 /* Figure number of rows to return to caller. */
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];
255 work_ptrs[1] = output_buf[*out_row_ctr + 1];
257 work_ptrs[1] = upsample->spare_row;
258 upsample->spare_full = TRUE;
260 /* Now do the upsampling. */
261 (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
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)++;
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. */
280 my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
282 /* Just do the upsampling. */
283 (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
284 output_buf + *out_row_ctr);
287 (*in_row_group_ctr)++;
292 * These are the routines invoked by the control routines to do
293 * the actual upsampling/conversion. One row group is processed per call.
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.
302 * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
306 h2v1_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
307 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
309 switch (cinfo->out_color_space) {
311 extrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
316 extrgbx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
320 extbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
325 extbgrx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
330 extxbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
335 extxrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
339 h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
347 * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
351 h2v2_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
352 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
354 switch (cinfo->out_color_space) {
356 extrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
361 extrgbx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
365 extbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
370 extbgrx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
375 extxbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
380 extxrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
384 h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
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))
400 #define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l)
401 #define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r)
403 #define WRITE_TWO_PIXELS_LE(addr, pixels) { \
404 ((INT16 *)(addr))[0] = (INT16)(pixels); \
405 ((INT16 *)(addr))[1] = (INT16)((pixels) >> 16); \
407 #define WRITE_TWO_PIXELS_BE(addr, pixels) { \
408 ((INT16 *)(addr))[1] = (INT16)(pixels); \
409 ((INT16 *)(addr))[0] = (INT16)((pixels) >> 16); \
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))
417 /* Declarations for ordered dithering
419 * We use a 4x4 ordered dither array packed into 32 bits. This array is
420 * sufficient for dithering RGB888 to RGB565.
423 #define DITHER_MASK 0x3
424 #define DITHER_ROTATE(x) ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
425 static const JLONG dither_matrix[4] = {
433 /* Include inline routines for RGB565 conversion */
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
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
468 static INLINE boolean is_big_endian(void)
471 if (*(char *)&test_value != 1)
478 h2v1_merged_upsample_565(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
479 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
482 h2v1_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
485 h2v1_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
491 h2v1_merged_upsample_565D(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
492 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
495 h2v1_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
498 h2v1_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
504 h2v2_merged_upsample_565(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
505 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
508 h2v2_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
511 h2v2_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
517 h2v2_merged_upsample_565D(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
518 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
521 h2v2_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
524 h2v2_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
530 * Module initialization routine for merged upsampling/color conversion.
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.
538 jinit_merged_upsampler(j_decompress_ptr cinfo)
540 my_merged_upsample_ptr upsample;
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;
549 upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
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;
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;
561 upsample->upmethod = h2v2_merged_upsample_565;
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)));
569 upsample->pub.upsample = merged_1v_upsample;
570 if (jsimd_can_h2v1_merged_upsample())
571 upsample->upmethod = jsimd_h2v1_merged_upsample;
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;
578 upsample->upmethod = h2v1_merged_upsample_565;
581 /* No spare row needed */
582 upsample->spare_row = NULL;
585 build_ycc_rgb_table(cinfo);
588 #endif /* UPSAMPLE_MERGING_SUPPORTED */