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.
12 * This file contains code for merged upsampling/color conversion.
14 * This file combines functions from jdsample.c and jdcolor.c;
15 * read those files first to understand what's going on.
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
22 * G = Y + K2 * Cb + K3 * Cr
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.
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.)
39 #define JPEG_INTERNALS
43 #include "jconfigint.h"
45 #ifdef UPSAMPLE_MERGING_SUPPORTED
48 /* Private subobject */
51 struct jpeg_upsampler pub; /* public fields */
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);
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 */
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.
69 boolean spare_full; /* T if spare buffer is occupied */
71 JDIMENSION out_row_width; /* samples per output row */
72 JDIMENSION rows_to_go; /* counts rows remaining in image */
75 typedef my_upsampler * my_upsample_ptr;
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))
82 /* Include inline routines for colorspace extensions */
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
101 #undef h2v1_merged_upsample_internal
102 #undef h2v2_merged_upsample_internal
104 #define RGB_RED EXT_RGBX_RED
105 #define RGB_GREEN EXT_RGBX_GREEN
106 #define RGB_BLUE EXT_RGBX_BLUE
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"
117 #undef h2v1_merged_upsample_internal
118 #undef h2v2_merged_upsample_internal
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"
131 #undef h2v1_merged_upsample_internal
132 #undef h2v2_merged_upsample_internal
134 #define RGB_RED EXT_BGRX_RED
135 #define RGB_GREEN EXT_BGRX_GREEN
136 #define RGB_BLUE EXT_BGRX_BLUE
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"
147 #undef h2v1_merged_upsample_internal
148 #undef h2v2_merged_upsample_internal
150 #define RGB_RED EXT_XBGR_RED
151 #define RGB_GREEN EXT_XBGR_GREEN
152 #define RGB_BLUE EXT_XBGR_BLUE
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"
163 #undef h2v1_merged_upsample_internal
164 #undef h2v2_merged_upsample_internal
166 #define RGB_RED EXT_XRGB_RED
167 #define RGB_GREEN EXT_XRGB_GREEN
168 #define RGB_BLUE EXT_XRGB_BLUE
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"
179 #undef h2v1_merged_upsample_internal
180 #undef h2v2_merged_upsample_internal
184 * Initialize tables for YCC->RGB colorspace conversion.
185 * This is taken directly from jdcolor.c; see that file for more info.
189 build_ycc_rgb_table (j_decompress_ptr cinfo)
191 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
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));
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;
228 * Initialize for an upsampling pass.
232 start_pass_merged_upsample (j_decompress_ptr cinfo)
234 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
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;
244 * Control routine to do upsampling (and color conversion).
246 * The control routine just handles the row buffering considerations.
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. */
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,
271 1, upsample->out_row_width);
273 jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
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,
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. */
331 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
333 /* Just do the upsampling. */
334 (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
335 output_buf + *out_row_ctr);
338 (*in_row_group_ctr)++;
343 * These are the routines invoked by the control routines to do
344 * the actual upsampling/conversion. One row group is processed per call.
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.
353 * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
357 h2v1_merged_upsample (j_decompress_ptr cinfo,
358 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
359 JSAMPARRAY output_buf)
361 switch (cinfo->out_color_space) {
363 extrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
368 extrgbx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
372 extbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
377 extbgrx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
382 extxbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
387 extxrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
391 h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
399 * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
403 h2v2_merged_upsample (j_decompress_ptr cinfo,
404 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
405 JSAMPARRAY output_buf)
407 switch (cinfo->out_color_space) {
409 extrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
414 extrgbx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
418 extbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
423 extbgrx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
428 extxbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
433 extxrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
437 h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
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))
454 #define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l)
455 #define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r)
457 #define PACK_NEED_ALIGNMENT(ptr) (((size_t)(ptr)) & 3)
459 #define WRITE_TWO_PIXELS_LE(addr, pixels) { \
460 ((INT16*)(addr))[0] = (pixels); \
461 ((INT16*)(addr))[1] = (pixels) >> 16; \
463 #define WRITE_TWO_PIXELS_BE(addr, pixels) { \
464 ((INT16*)(addr))[1] = (pixels); \
465 ((INT16*)(addr))[0] = (pixels) >> 16; \
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))
473 /* Declarations for ordered dithering
475 * We use a 4x4 ordered dither array packed into 32 bits. This array is
476 * sufficent for dithering RGB888 to RGB565.
479 #define DITHER_MASK 0x3
480 #define DITHER_ROTATE(x) (((x) << 24) | (((x) >> 8) & 0x00FFFFFF))
481 static const INT32 dither_matrix[4] = {
489 /* Include inline routines for RGB565 conversion */
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
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
524 static INLINE boolean is_big_endian(void)
527 if(*(char *)&test_value != 1)
534 h2v1_merged_upsample_565 (j_decompress_ptr cinfo,
535 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
536 JSAMPARRAY output_buf)
539 h2v1_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
542 h2v1_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
548 h2v1_merged_upsample_565D (j_decompress_ptr cinfo,
549 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
550 JSAMPARRAY output_buf)
553 h2v1_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
556 h2v1_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
562 h2v2_merged_upsample_565 (j_decompress_ptr cinfo,
563 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
564 JSAMPARRAY output_buf)
567 h2v2_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
570 h2v2_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
576 h2v2_merged_upsample_565D (j_decompress_ptr cinfo,
577 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
578 JSAMPARRAY output_buf)
581 h2v2_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
584 h2v2_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
590 * Module initialization routine for merged upsampling/color conversion.
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.
598 jinit_merged_upsampler (j_decompress_ptr cinfo)
600 my_upsample_ptr upsample;
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;
609 upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
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;
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;
621 upsample->upmethod = h2v2_merged_upsample_565;
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)));
629 upsample->pub.upsample = merged_1v_upsample;
630 if (jsimd_can_h2v1_merged_upsample())
631 upsample->upmethod = jsimd_h2v1_merged_upsample;
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;
638 upsample->upmethod = h2v1_merged_upsample_565;
641 /* No spare row needed */
642 upsample->spare_row = NULL;
645 build_ycc_rgb_table(cinfo);
648 #endif /* UPSAMPLE_MERGING_SUPPORTED */