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
46 #ifdef UPSAMPLE_MERGING_SUPPORTED
49 #define SCALEBITS 16 /* speediest right-shift on some machines */
50 #define ONE_HALF ((JLONG)1 << (SCALEBITS - 1))
51 #define FIX(x) ((JLONG)((x) * (1L << SCALEBITS) + 0.5))
54 /* Include inline routines for colorspace extensions */
62 #define RGB_RED EXT_RGB_RED
63 #define RGB_GREEN EXT_RGB_GREEN
64 #define RGB_BLUE EXT_RGB_BLUE
65 #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
66 #define h2v1_merged_upsample_internal extrgb_h2v1_merged_upsample_internal
67 #define h2v2_merged_upsample_internal extrgb_h2v2_merged_upsample_internal
73 #undef h2v1_merged_upsample_internal
74 #undef h2v2_merged_upsample_internal
76 #define RGB_RED EXT_RGBX_RED
77 #define RGB_GREEN EXT_RGBX_GREEN
78 #define RGB_BLUE EXT_RGBX_BLUE
80 #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
81 #define h2v1_merged_upsample_internal extrgbx_h2v1_merged_upsample_internal
82 #define h2v2_merged_upsample_internal extrgbx_h2v2_merged_upsample_internal
89 #undef h2v1_merged_upsample_internal
90 #undef h2v2_merged_upsample_internal
92 #define RGB_RED EXT_BGR_RED
93 #define RGB_GREEN EXT_BGR_GREEN
94 #define RGB_BLUE EXT_BGR_BLUE
95 #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
96 #define h2v1_merged_upsample_internal extbgr_h2v1_merged_upsample_internal
97 #define h2v2_merged_upsample_internal extbgr_h2v2_merged_upsample_internal
103 #undef h2v1_merged_upsample_internal
104 #undef h2v2_merged_upsample_internal
106 #define RGB_RED EXT_BGRX_RED
107 #define RGB_GREEN EXT_BGRX_GREEN
108 #define RGB_BLUE EXT_BGRX_BLUE
110 #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
111 #define h2v1_merged_upsample_internal extbgrx_h2v1_merged_upsample_internal
112 #define h2v2_merged_upsample_internal extbgrx_h2v2_merged_upsample_internal
113 #include "jdmrgext.c"
119 #undef h2v1_merged_upsample_internal
120 #undef h2v2_merged_upsample_internal
122 #define RGB_RED EXT_XBGR_RED
123 #define RGB_GREEN EXT_XBGR_GREEN
124 #define RGB_BLUE EXT_XBGR_BLUE
126 #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
127 #define h2v1_merged_upsample_internal extxbgr_h2v1_merged_upsample_internal
128 #define h2v2_merged_upsample_internal extxbgr_h2v2_merged_upsample_internal
129 #include "jdmrgext.c"
135 #undef h2v1_merged_upsample_internal
136 #undef h2v2_merged_upsample_internal
138 #define RGB_RED EXT_XRGB_RED
139 #define RGB_GREEN EXT_XRGB_GREEN
140 #define RGB_BLUE EXT_XRGB_BLUE
142 #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
143 #define h2v1_merged_upsample_internal extxrgb_h2v1_merged_upsample_internal
144 #define h2v2_merged_upsample_internal extxrgb_h2v2_merged_upsample_internal
145 #include "jdmrgext.c"
151 #undef h2v1_merged_upsample_internal
152 #undef h2v2_merged_upsample_internal
156 * Initialize tables for YCC->RGB colorspace conversion.
157 * This is taken directly from jdcolor.c; see that file for more info.
161 build_ycc_rgb_table(j_decompress_ptr cinfo
)
163 my_merged_upsample_ptr upsample
= (my_merged_upsample_ptr
)cinfo
->upsample
;
168 upsample
->Cr_r_tab
= (int *)
169 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
)cinfo
, JPOOL_IMAGE
,
170 (MAXJSAMPLE
+ 1) * sizeof(int));
171 upsample
->Cb_b_tab
= (int *)
172 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
)cinfo
, JPOOL_IMAGE
,
173 (MAXJSAMPLE
+ 1) * sizeof(int));
174 upsample
->Cr_g_tab
= (JLONG
*)
175 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
)cinfo
, JPOOL_IMAGE
,
176 (MAXJSAMPLE
+ 1) * sizeof(JLONG
));
177 upsample
->Cb_g_tab
= (JLONG
*)
178 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
)cinfo
, JPOOL_IMAGE
,
179 (MAXJSAMPLE
+ 1) * sizeof(JLONG
));
181 for (i
= 0, x
= -CENTERJSAMPLE
; i
<= MAXJSAMPLE
; i
++, x
++) {
182 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
183 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
184 /* Cr=>R value is nearest int to 1.40200 * x */
185 upsample
->Cr_r_tab
[i
] = (int)
186 RIGHT_SHIFT(FIX(1.40200) * x
+ ONE_HALF
, SCALEBITS
);
187 /* Cb=>B value is nearest int to 1.77200 * x */
188 upsample
->Cb_b_tab
[i
] = (int)
189 RIGHT_SHIFT(FIX(1.77200) * x
+ ONE_HALF
, SCALEBITS
);
190 /* Cr=>G value is scaled-up -0.71414 * x */
191 upsample
->Cr_g_tab
[i
] = (-FIX(0.71414)) * x
;
192 /* Cb=>G value is scaled-up -0.34414 * x */
193 /* We also add in ONE_HALF so that need not do it in inner loop */
194 upsample
->Cb_g_tab
[i
] = (-FIX(0.34414)) * x
+ ONE_HALF
;
200 * Initialize for an upsampling pass.
204 start_pass_merged_upsample(j_decompress_ptr cinfo
)
206 my_merged_upsample_ptr upsample
= (my_merged_upsample_ptr
)cinfo
->upsample
;
208 /* Mark the spare buffer empty */
209 upsample
->spare_full
= FALSE
;
210 /* Initialize total-height counter for detecting bottom of image */
211 upsample
->rows_to_go
= cinfo
->output_height
;
216 * Control routine to do upsampling (and color conversion).
218 * The control routine just handles the row buffering considerations.
222 merged_2v_upsample(j_decompress_ptr cinfo
, JSAMPIMAGE input_buf
,
223 JDIMENSION
*in_row_group_ctr
,
224 JDIMENSION in_row_groups_avail
, JSAMPARRAY output_buf
,
225 JDIMENSION
*out_row_ctr
, JDIMENSION out_rows_avail
)
226 /* 2:1 vertical sampling case: may need a spare row. */
228 my_merged_upsample_ptr upsample
= (my_merged_upsample_ptr
)cinfo
->upsample
;
229 JSAMPROW work_ptrs
[2];
230 JDIMENSION num_rows
; /* number of rows returned to caller */
232 if (upsample
->spare_full
) {
233 /* If we have a spare row saved from a previous cycle, just return it. */
234 JDIMENSION size
= upsample
->out_row_width
;
235 if (cinfo
->out_color_space
== JCS_RGB565
)
236 size
= cinfo
->output_width
* 2;
237 jcopy_sample_rows(&upsample
->spare_row
, 0, output_buf
+ *out_row_ctr
, 0, 1,
240 upsample
->spare_full
= FALSE
;
242 /* Figure number of rows to return to caller. */
244 /* Not more than the distance to the end of the image. */
245 if (num_rows
> upsample
->rows_to_go
)
246 num_rows
= upsample
->rows_to_go
;
247 /* And not more than what the client can accept: */
248 out_rows_avail
-= *out_row_ctr
;
249 if (num_rows
> out_rows_avail
)
250 num_rows
= out_rows_avail
;
251 /* Create output pointer array for upsampler. */
252 work_ptrs
[0] = output_buf
[*out_row_ctr
];
254 work_ptrs
[1] = output_buf
[*out_row_ctr
+ 1];
256 work_ptrs
[1] = upsample
->spare_row
;
257 upsample
->spare_full
= TRUE
;
259 /* Now do the upsampling. */
260 (*upsample
->upmethod
) (cinfo
, input_buf
, *in_row_group_ctr
, work_ptrs
);
264 *out_row_ctr
+= num_rows
;
265 upsample
->rows_to_go
-= num_rows
;
266 /* When the buffer is emptied, declare this input row group consumed */
267 if (!upsample
->spare_full
)
268 (*in_row_group_ctr
)++;
273 merged_1v_upsample(j_decompress_ptr cinfo
, JSAMPIMAGE input_buf
,
274 JDIMENSION
*in_row_group_ctr
,
275 JDIMENSION in_row_groups_avail
, JSAMPARRAY output_buf
,
276 JDIMENSION
*out_row_ctr
, JDIMENSION out_rows_avail
)
277 /* 1:1 vertical sampling case: much easier, never need a spare row. */
279 my_merged_upsample_ptr upsample
= (my_merged_upsample_ptr
)cinfo
->upsample
;
281 /* Just do the upsampling. */
282 (*upsample
->upmethod
) (cinfo
, input_buf
, *in_row_group_ctr
,
283 output_buf
+ *out_row_ctr
);
286 (*in_row_group_ctr
)++;
291 * These are the routines invoked by the control routines to do
292 * the actual upsampling/conversion. One row group is processed per call.
294 * Note: since we may be writing directly into application-supplied buffers,
295 * we have to be honest about the output width; we can't assume the buffer
296 * has been rounded up to an even width.
301 * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
305 h2v1_merged_upsample(j_decompress_ptr cinfo
, JSAMPIMAGE input_buf
,
306 JDIMENSION in_row_group_ctr
, JSAMPARRAY output_buf
)
308 switch (cinfo
->out_color_space
) {
310 extrgb_h2v1_merged_upsample_internal(cinfo
, input_buf
, in_row_group_ctr
,
315 extrgbx_h2v1_merged_upsample_internal(cinfo
, input_buf
, in_row_group_ctr
,
319 extbgr_h2v1_merged_upsample_internal(cinfo
, input_buf
, in_row_group_ctr
,
324 extbgrx_h2v1_merged_upsample_internal(cinfo
, input_buf
, in_row_group_ctr
,
329 extxbgr_h2v1_merged_upsample_internal(cinfo
, input_buf
, in_row_group_ctr
,
334 extxrgb_h2v1_merged_upsample_internal(cinfo
, input_buf
, in_row_group_ctr
,
338 h2v1_merged_upsample_internal(cinfo
, input_buf
, in_row_group_ctr
,
346 * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
350 h2v2_merged_upsample(j_decompress_ptr cinfo
, JSAMPIMAGE input_buf
,
351 JDIMENSION in_row_group_ctr
, JSAMPARRAY output_buf
)
353 switch (cinfo
->out_color_space
) {
355 extrgb_h2v2_merged_upsample_internal(cinfo
, input_buf
, in_row_group_ctr
,
360 extrgbx_h2v2_merged_upsample_internal(cinfo
, input_buf
, in_row_group_ctr
,
364 extbgr_h2v2_merged_upsample_internal(cinfo
, input_buf
, in_row_group_ctr
,
369 extbgrx_h2v2_merged_upsample_internal(cinfo
, input_buf
, in_row_group_ctr
,
374 extxbgr_h2v2_merged_upsample_internal(cinfo
, input_buf
, in_row_group_ctr
,
379 extxrgb_h2v2_merged_upsample_internal(cinfo
, input_buf
, in_row_group_ctr
,
383 h2v2_merged_upsample_internal(cinfo
, input_buf
, in_row_group_ctr
,
394 #define PACK_SHORT_565_LE(r, g, b) \
395 ((((r) << 8) & 0xF800) | (((g) << 3) & 0x7E0) | ((b) >> 3))
396 #define PACK_SHORT_565_BE(r, g, b) \
397 (((r) & 0xF8) | ((g) >> 5) | (((g) << 11) & 0xE000) | (((b) << 5) & 0x1F00))
399 #define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l)
400 #define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r)
402 #define WRITE_TWO_PIXELS_LE(addr, pixels) { \
403 ((INT16 *)(addr))[0] = (INT16)(pixels); \
404 ((INT16 *)(addr))[1] = (INT16)((pixels) >> 16); \
406 #define WRITE_TWO_PIXELS_BE(addr, pixels) { \
407 ((INT16 *)(addr))[1] = (INT16)(pixels); \
408 ((INT16 *)(addr))[0] = (INT16)((pixels) >> 16); \
411 #define DITHER_565_R(r, dither) ((r) + ((dither) & 0xFF))
412 #define DITHER_565_G(g, dither) ((g) + (((dither) & 0xFF) >> 1))
413 #define DITHER_565_B(b, dither) ((b) + ((dither) & 0xFF))
416 /* Declarations for ordered dithering
418 * We use a 4x4 ordered dither array packed into 32 bits. This array is
419 * sufficient for dithering RGB888 to RGB565.
422 #define DITHER_MASK 0x3
423 #define DITHER_ROTATE(x) ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
424 static const JLONG dither_matrix
[4] = {
432 /* Include inline routines for RGB565 conversion */
434 #define PACK_SHORT_565 PACK_SHORT_565_LE
435 #define PACK_TWO_PIXELS PACK_TWO_PIXELS_LE
436 #define WRITE_TWO_PIXELS WRITE_TWO_PIXELS_LE
437 #define h2v1_merged_upsample_565_internal h2v1_merged_upsample_565_le
438 #define h2v1_merged_upsample_565D_internal h2v1_merged_upsample_565D_le
439 #define h2v2_merged_upsample_565_internal h2v2_merged_upsample_565_le
440 #define h2v2_merged_upsample_565D_internal h2v2_merged_upsample_565D_le
441 #include "jdmrg565.c"
442 #undef PACK_SHORT_565
443 #undef PACK_TWO_PIXELS
444 #undef WRITE_TWO_PIXELS
445 #undef h2v1_merged_upsample_565_internal
446 #undef h2v1_merged_upsample_565D_internal
447 #undef h2v2_merged_upsample_565_internal
448 #undef h2v2_merged_upsample_565D_internal
450 #define PACK_SHORT_565 PACK_SHORT_565_BE
451 #define PACK_TWO_PIXELS PACK_TWO_PIXELS_BE
452 #define WRITE_TWO_PIXELS WRITE_TWO_PIXELS_BE
453 #define h2v1_merged_upsample_565_internal h2v1_merged_upsample_565_be
454 #define h2v1_merged_upsample_565D_internal h2v1_merged_upsample_565D_be
455 #define h2v2_merged_upsample_565_internal h2v2_merged_upsample_565_be
456 #define h2v2_merged_upsample_565D_internal h2v2_merged_upsample_565D_be
457 #include "jdmrg565.c"
458 #undef PACK_SHORT_565
459 #undef PACK_TWO_PIXELS
460 #undef WRITE_TWO_PIXELS
461 #undef h2v1_merged_upsample_565_internal
462 #undef h2v1_merged_upsample_565D_internal
463 #undef h2v2_merged_upsample_565_internal
464 #undef h2v2_merged_upsample_565D_internal
467 static INLINE boolean
is_big_endian(void)
470 if (*(char *)&test_value
!= 1)
477 h2v1_merged_upsample_565(j_decompress_ptr cinfo
, JSAMPIMAGE input_buf
,
478 JDIMENSION in_row_group_ctr
, JSAMPARRAY output_buf
)
481 h2v1_merged_upsample_565_be(cinfo
, input_buf
, in_row_group_ctr
,
484 h2v1_merged_upsample_565_le(cinfo
, input_buf
, in_row_group_ctr
,
490 h2v1_merged_upsample_565D(j_decompress_ptr cinfo
, JSAMPIMAGE input_buf
,
491 JDIMENSION in_row_group_ctr
, JSAMPARRAY output_buf
)
494 h2v1_merged_upsample_565D_be(cinfo
, input_buf
, in_row_group_ctr
,
497 h2v1_merged_upsample_565D_le(cinfo
, input_buf
, in_row_group_ctr
,
503 h2v2_merged_upsample_565(j_decompress_ptr cinfo
, JSAMPIMAGE input_buf
,
504 JDIMENSION in_row_group_ctr
, JSAMPARRAY output_buf
)
507 h2v2_merged_upsample_565_be(cinfo
, input_buf
, in_row_group_ctr
,
510 h2v2_merged_upsample_565_le(cinfo
, input_buf
, in_row_group_ctr
,
516 h2v2_merged_upsample_565D(j_decompress_ptr cinfo
, JSAMPIMAGE input_buf
,
517 JDIMENSION in_row_group_ctr
, JSAMPARRAY output_buf
)
520 h2v2_merged_upsample_565D_be(cinfo
, input_buf
, in_row_group_ctr
,
523 h2v2_merged_upsample_565D_le(cinfo
, input_buf
, in_row_group_ctr
,
529 * Module initialization routine for merged upsampling/color conversion.
531 * NB: this is called under the conditions determined by use_merged_upsample()
532 * in jdmaster.c. That routine MUST correspond to the actual capabilities
533 * of this module; no safety checks are made here.
537 jinit_merged_upsampler(j_decompress_ptr cinfo
)
539 my_merged_upsample_ptr upsample
;
541 upsample
= (my_merged_upsample_ptr
)
542 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
)cinfo
, JPOOL_IMAGE
,
543 sizeof(my_merged_upsampler
));
544 cinfo
->upsample
= (struct jpeg_upsampler
*)upsample
;
545 upsample
->pub
.start_pass
= start_pass_merged_upsample
;
546 upsample
->pub
.need_context_rows
= FALSE
;
548 upsample
->out_row_width
= cinfo
->output_width
* cinfo
->out_color_components
;
550 if (cinfo
->max_v_samp_factor
== 2) {
551 upsample
->pub
.upsample
= merged_2v_upsample
;
552 if (jsimd_can_h2v2_merged_upsample())
553 upsample
->upmethod
= jsimd_h2v2_merged_upsample
;
555 upsample
->upmethod
= h2v2_merged_upsample
;
556 if (cinfo
->out_color_space
== JCS_RGB565
) {
557 if (cinfo
->dither_mode
!= JDITHER_NONE
) {
558 upsample
->upmethod
= h2v2_merged_upsample_565D
;
560 upsample
->upmethod
= h2v2_merged_upsample_565
;
563 /* Allocate a spare row buffer */
564 upsample
->spare_row
= (JSAMPROW
)
565 (*cinfo
->mem
->alloc_large
) ((j_common_ptr
)cinfo
, JPOOL_IMAGE
,
566 (size_t)(upsample
->out_row_width
* sizeof(JSAMPLE
)));
568 upsample
->pub
.upsample
= merged_1v_upsample
;
569 if (jsimd_can_h2v1_merged_upsample())
570 upsample
->upmethod
= jsimd_h2v1_merged_upsample
;
572 upsample
->upmethod
= h2v1_merged_upsample
;
573 if (cinfo
->out_color_space
== JCS_RGB565
) {
574 if (cinfo
->dither_mode
!= JDITHER_NONE
) {
575 upsample
->upmethod
= h2v1_merged_upsample_565D
;
577 upsample
->upmethod
= h2v1_merged_upsample_565
;
580 /* No spare row needed */
581 upsample
->spare_row
= NULL
;
584 build_ycc_rgb_table(cinfo
);
587 #endif /* UPSAMPLE_MERGING_SUPPORTED */