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, 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
44 #include "jconfigint.h"
46 #ifdef UPSAMPLE_MERGING_SUPPORTED
49 /* Private subobject */
52 struct jpeg_upsampler pub; /* public fields */
54 /* Pointer to routine to do actual upsampling/conversion of one row group */
55 void (*upmethod) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
56 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf);
58 /* Private state for YCC->RGB conversion */
59 int *Cr_r_tab; /* => table for Cr to R conversion */
60 int *Cb_b_tab; /* => table for Cb to B conversion */
61 JLONG *Cr_g_tab; /* => table for Cr to G conversion */
62 JLONG *Cb_g_tab; /* => table for Cb to G conversion */
64 /* For 2:1 vertical sampling, we produce two output rows at a time.
65 * We need a "spare" row buffer to hold the second output row if the
66 * application provides just a one-row buffer; we also use the spare
67 * to discard the dummy last row if the image height is odd.
70 boolean spare_full; /* T if spare buffer is occupied */
72 JDIMENSION out_row_width; /* samples per output row */
73 JDIMENSION rows_to_go; /* counts rows remaining in image */
76 typedef my_upsampler *my_upsample_ptr;
78 #define SCALEBITS 16 /* speediest right-shift on some machines */
79 #define ONE_HALF ((JLONG)1 << (SCALEBITS - 1))
80 #define FIX(x) ((JLONG)((x) * (1L << SCALEBITS) + 0.5))
83 /* Include inline routines for colorspace extensions */
91 #define RGB_RED EXT_RGB_RED
92 #define RGB_GREEN EXT_RGB_GREEN
93 #define RGB_BLUE EXT_RGB_BLUE
94 #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
95 #define h2v1_merged_upsample_internal extrgb_h2v1_merged_upsample_internal
96 #define h2v2_merged_upsample_internal extrgb_h2v2_merged_upsample_internal
102 #undef h2v1_merged_upsample_internal
103 #undef h2v2_merged_upsample_internal
105 #define RGB_RED EXT_RGBX_RED
106 #define RGB_GREEN EXT_RGBX_GREEN
107 #define RGB_BLUE EXT_RGBX_BLUE
109 #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
110 #define h2v1_merged_upsample_internal extrgbx_h2v1_merged_upsample_internal
111 #define h2v2_merged_upsample_internal extrgbx_h2v2_merged_upsample_internal
112 #include "jdmrgext.c"
118 #undef h2v1_merged_upsample_internal
119 #undef h2v2_merged_upsample_internal
121 #define RGB_RED EXT_BGR_RED
122 #define RGB_GREEN EXT_BGR_GREEN
123 #define RGB_BLUE EXT_BGR_BLUE
124 #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
125 #define h2v1_merged_upsample_internal extbgr_h2v1_merged_upsample_internal
126 #define h2v2_merged_upsample_internal extbgr_h2v2_merged_upsample_internal
127 #include "jdmrgext.c"
132 #undef h2v1_merged_upsample_internal
133 #undef h2v2_merged_upsample_internal
135 #define RGB_RED EXT_BGRX_RED
136 #define RGB_GREEN EXT_BGRX_GREEN
137 #define RGB_BLUE EXT_BGRX_BLUE
139 #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
140 #define h2v1_merged_upsample_internal extbgrx_h2v1_merged_upsample_internal
141 #define h2v2_merged_upsample_internal extbgrx_h2v2_merged_upsample_internal
142 #include "jdmrgext.c"
148 #undef h2v1_merged_upsample_internal
149 #undef h2v2_merged_upsample_internal
151 #define RGB_RED EXT_XBGR_RED
152 #define RGB_GREEN EXT_XBGR_GREEN
153 #define RGB_BLUE EXT_XBGR_BLUE
155 #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
156 #define h2v1_merged_upsample_internal extxbgr_h2v1_merged_upsample_internal
157 #define h2v2_merged_upsample_internal extxbgr_h2v2_merged_upsample_internal
158 #include "jdmrgext.c"
164 #undef h2v1_merged_upsample_internal
165 #undef h2v2_merged_upsample_internal
167 #define RGB_RED EXT_XRGB_RED
168 #define RGB_GREEN EXT_XRGB_GREEN
169 #define RGB_BLUE EXT_XRGB_BLUE
171 #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
172 #define h2v1_merged_upsample_internal extxrgb_h2v1_merged_upsample_internal
173 #define h2v2_merged_upsample_internal extxrgb_h2v2_merged_upsample_internal
174 #include "jdmrgext.c"
180 #undef h2v1_merged_upsample_internal
181 #undef h2v2_merged_upsample_internal
185 * Initialize tables for YCC->RGB colorspace conversion.
186 * This is taken directly from jdcolor.c; see that file for more info.
190 build_ycc_rgb_table(j_decompress_ptr cinfo)
192 my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
197 upsample->Cr_r_tab = (int *)
198 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
199 (MAXJSAMPLE + 1) * sizeof(int));
200 upsample->Cb_b_tab = (int *)
201 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
202 (MAXJSAMPLE + 1) * sizeof(int));
203 upsample->Cr_g_tab = (JLONG *)
204 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
205 (MAXJSAMPLE + 1) * sizeof(JLONG));
206 upsample->Cb_g_tab = (JLONG *)
207 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
208 (MAXJSAMPLE + 1) * sizeof(JLONG));
210 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
211 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
212 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
213 /* Cr=>R value is nearest int to 1.40200 * x */
214 upsample->Cr_r_tab[i] = (int)
215 RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
216 /* Cb=>B value is nearest int to 1.77200 * x */
217 upsample->Cb_b_tab[i] = (int)
218 RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
219 /* Cr=>G value is scaled-up -0.71414 * x */
220 upsample->Cr_g_tab[i] = (-FIX(0.71414)) * x;
221 /* Cb=>G value is scaled-up -0.34414 * x */
222 /* We also add in ONE_HALF so that need not do it in inner loop */
223 upsample->Cb_g_tab[i] = (-FIX(0.34414)) * x + ONE_HALF;
229 * Initialize for an upsampling pass.
233 start_pass_merged_upsample(j_decompress_ptr cinfo)
235 my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
237 /* Mark the spare buffer empty */
238 upsample->spare_full = FALSE;
239 /* Initialize total-height counter for detecting bottom of image */
240 upsample->rows_to_go = cinfo->output_height;
245 * Control routine to do upsampling (and color conversion).
247 * The control routine just handles the row buffering considerations.
251 merged_2v_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
252 JDIMENSION *in_row_group_ctr,
253 JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
254 JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
255 /* 2:1 vertical sampling case: may need a spare row. */
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 */
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;
270 jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0, 1,
271 upsample->out_row_width);
273 jcopy_sample_rows(&upsample->spare_row, 0, output_buf + *out_row_ctr, 0, 1,
277 upsample->spare_full = FALSE;
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)))
288 /* Figure number of rows to return to caller. */
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];
300 work_ptrs[1] = output_buf[*out_row_ctr + 1];
302 work_ptrs[1] = upsample->spare_row;
303 upsample->spare_full = TRUE;
305 /* Now do the upsampling. */
308 (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
310 (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
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)++;
324 merged_1v_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
325 JDIMENSION *in_row_group_ctr,
326 JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
327 JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
328 /* 1:1 vertical sampling case: much easier, never need a spare row. */
330 my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
332 /* Just do the upsampling. */
333 (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
334 output_buf + *out_row_ctr);
337 (*in_row_group_ctr)++;
342 * These are the routines invoked by the control routines to do
343 * the actual upsampling/conversion. One row group is processed per call.
345 * Note: since we may be writing directly into application-supplied buffers,
346 * we have to be honest about the output width; we can't assume the buffer
347 * has been rounded up to an even width.
352 * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
356 h2v1_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
357 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
359 switch (cinfo->out_color_space) {
361 extrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
366 extrgbx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
370 extbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
375 extbgrx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
380 extxbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
385 extxrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
389 h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
397 * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
401 h2v2_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
402 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
404 switch (cinfo->out_color_space) {
406 extrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
411 extrgbx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
415 extbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
420 extbgrx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
425 extxbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
430 extxrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
434 h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
445 #define PACK_SHORT_565_LE(r, g, b) ((((r) << 8) & 0xF800) | \
446 (((g) << 3) & 0x7E0) | ((b) >> 3))
447 #define PACK_SHORT_565_BE(r, g, b) (((r) & 0xF8) | ((g) >> 5) | \
448 (((g) << 11) & 0xE000) | \
449 (((b) << 5) & 0x1F00))
451 #define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l)
452 #define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r)
454 #define PACK_NEED_ALIGNMENT(ptr) (((size_t)(ptr)) & 3)
456 #define WRITE_TWO_PIXELS_LE(addr, pixels) { \
457 ((INT16 *)(addr))[0] = (INT16)(pixels); \
458 ((INT16 *)(addr))[1] = (INT16)((pixels) >> 16); \
460 #define WRITE_TWO_PIXELS_BE(addr, pixels) { \
461 ((INT16 *)(addr))[1] = (INT16)(pixels); \
462 ((INT16 *)(addr))[0] = (INT16)((pixels) >> 16); \
465 #define DITHER_565_R(r, dither) ((r) + ((dither) & 0xFF))
466 #define DITHER_565_G(g, dither) ((g) + (((dither) & 0xFF) >> 1))
467 #define DITHER_565_B(b, dither) ((b) + ((dither) & 0xFF))
470 /* Declarations for ordered dithering
472 * We use a 4x4 ordered dither array packed into 32 bits. This array is
473 * sufficient for dithering RGB888 to RGB565.
476 #define DITHER_MASK 0x3
477 #define DITHER_ROTATE(x) ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
478 static const JLONG dither_matrix[4] = {
486 /* Include inline routines for RGB565 conversion */
488 #define PACK_SHORT_565 PACK_SHORT_565_LE
489 #define PACK_TWO_PIXELS PACK_TWO_PIXELS_LE
490 #define WRITE_TWO_PIXELS WRITE_TWO_PIXELS_LE
491 #define h2v1_merged_upsample_565_internal h2v1_merged_upsample_565_le
492 #define h2v1_merged_upsample_565D_internal h2v1_merged_upsample_565D_le
493 #define h2v2_merged_upsample_565_internal h2v2_merged_upsample_565_le
494 #define h2v2_merged_upsample_565D_internal h2v2_merged_upsample_565D_le
495 #include "jdmrg565.c"
496 #undef PACK_SHORT_565
497 #undef PACK_TWO_PIXELS
498 #undef WRITE_TWO_PIXELS
499 #undef h2v1_merged_upsample_565_internal
500 #undef h2v1_merged_upsample_565D_internal
501 #undef h2v2_merged_upsample_565_internal
502 #undef h2v2_merged_upsample_565D_internal
504 #define PACK_SHORT_565 PACK_SHORT_565_BE
505 #define PACK_TWO_PIXELS PACK_TWO_PIXELS_BE
506 #define WRITE_TWO_PIXELS WRITE_TWO_PIXELS_BE
507 #define h2v1_merged_upsample_565_internal h2v1_merged_upsample_565_be
508 #define h2v1_merged_upsample_565D_internal h2v1_merged_upsample_565D_be
509 #define h2v2_merged_upsample_565_internal h2v2_merged_upsample_565_be
510 #define h2v2_merged_upsample_565D_internal h2v2_merged_upsample_565D_be
511 #include "jdmrg565.c"
512 #undef PACK_SHORT_565
513 #undef PACK_TWO_PIXELS
514 #undef WRITE_TWO_PIXELS
515 #undef h2v1_merged_upsample_565_internal
516 #undef h2v1_merged_upsample_565D_internal
517 #undef h2v2_merged_upsample_565_internal
518 #undef h2v2_merged_upsample_565D_internal
521 static INLINE boolean is_big_endian(void)
524 if (*(char *)&test_value != 1)
531 h2v1_merged_upsample_565(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
532 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
535 h2v1_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
538 h2v1_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
544 h2v1_merged_upsample_565D(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
545 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
548 h2v1_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
551 h2v1_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
557 h2v2_merged_upsample_565(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
558 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
561 h2v2_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
564 h2v2_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
570 h2v2_merged_upsample_565D(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
571 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
574 h2v2_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
577 h2v2_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
583 * Module initialization routine for merged upsampling/color conversion.
585 * NB: this is called under the conditions determined by use_merged_upsample()
586 * in jdmaster.c. That routine MUST correspond to the actual capabilities
587 * of this module; no safety checks are made here.
591 jinit_merged_upsampler(j_decompress_ptr cinfo)
593 my_upsample_ptr upsample;
595 upsample = (my_upsample_ptr)
596 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
597 sizeof(my_upsampler));
598 cinfo->upsample = (struct jpeg_upsampler *)upsample;
599 upsample->pub.start_pass = start_pass_merged_upsample;
600 upsample->pub.need_context_rows = FALSE;
602 upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
604 if (cinfo->max_v_samp_factor == 2) {
605 upsample->pub.upsample = merged_2v_upsample;
606 if (jsimd_can_h2v2_merged_upsample())
607 upsample->upmethod = jsimd_h2v2_merged_upsample;
609 upsample->upmethod = h2v2_merged_upsample;
610 if (cinfo->out_color_space == JCS_RGB565) {
611 if (cinfo->dither_mode != JDITHER_NONE) {
612 upsample->upmethod = h2v2_merged_upsample_565D;
614 upsample->upmethod = h2v2_merged_upsample_565;
617 /* Allocate a spare row buffer */
618 upsample->spare_row = (JSAMPROW)
619 (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
620 (size_t)(upsample->out_row_width * sizeof(JSAMPLE)));
622 upsample->pub.upsample = merged_1v_upsample;
623 if (jsimd_can_h2v1_merged_upsample())
624 upsample->upmethod = jsimd_h2v1_merged_upsample;
626 upsample->upmethod = h2v1_merged_upsample;
627 if (cinfo->out_color_space == JCS_RGB565) {
628 if (cinfo->dither_mode != JDITHER_NONE) {
629 upsample->upmethod = h2v1_merged_upsample_565D;
631 upsample->upmethod = h2v1_merged_upsample_565;
634 /* No spare row needed */
635 upsample->spare_row = NULL;
638 build_ycc_rgb_table(cinfo);
641 #endif /* UPSAMPLE_MERGING_SUPPORTED */