michael@0: /* michael@0: * jdmerge.c michael@0: * michael@0: * This file was part of the Independent JPEG Group's software: michael@0: * Copyright (C) 1994-1996, Thomas G. Lane. michael@0: * Copyright 2009 Pierre Ossman for Cendio AB michael@0: * libjpeg-turbo Modifications: michael@0: * Copyright (C) 2009, 2011, D. R. Commander. michael@0: * For conditions of distribution and use, see the accompanying README file. michael@0: * michael@0: * This file contains code for merged upsampling/color conversion. michael@0: * michael@0: * This file combines functions from jdsample.c and jdcolor.c; michael@0: * read those files first to understand what's going on. michael@0: * michael@0: * When the chroma components are to be upsampled by simple replication michael@0: * (ie, box filtering), we can save some work in color conversion by michael@0: * calculating all the output pixels corresponding to a pair of chroma michael@0: * samples at one time. In the conversion equations michael@0: * R = Y + K1 * Cr michael@0: * G = Y + K2 * Cb + K3 * Cr michael@0: * B = Y + K4 * Cb michael@0: * only the Y term varies among the group of pixels corresponding to a pair michael@0: * of chroma samples, so the rest of the terms can be calculated just once. michael@0: * At typical sampling ratios, this eliminates half or three-quarters of the michael@0: * multiplications needed for color conversion. michael@0: * michael@0: * This file currently provides implementations for the following cases: michael@0: * YCbCr => RGB color conversion only. michael@0: * Sampling ratios of 2h1v or 2h2v. michael@0: * No scaling needed at upsample time. michael@0: * Corner-aligned (non-CCIR601) sampling alignment. michael@0: * Other special cases could be added, but in most applications these are michael@0: * the only common cases. (For uncommon cases we fall back on the more michael@0: * general code in jdsample.c and jdcolor.c.) michael@0: */ michael@0: michael@0: #define JPEG_INTERNALS michael@0: #include "jinclude.h" michael@0: #include "jpeglib.h" michael@0: #include "jsimd.h" michael@0: #include "config.h" michael@0: michael@0: #ifdef UPSAMPLE_MERGING_SUPPORTED michael@0: michael@0: michael@0: /* Private subobject */ michael@0: michael@0: typedef struct { michael@0: struct jpeg_upsampler pub; /* public fields */ michael@0: michael@0: /* Pointer to routine to do actual upsampling/conversion of one row group */ michael@0: JMETHOD(void, upmethod, (j_decompress_ptr cinfo, michael@0: JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, michael@0: JSAMPARRAY output_buf)); michael@0: michael@0: /* Private state for YCC->RGB conversion */ michael@0: int * Cr_r_tab; /* => table for Cr to R conversion */ michael@0: int * Cb_b_tab; /* => table for Cb to B conversion */ michael@0: INT32 * Cr_g_tab; /* => table for Cr to G conversion */ michael@0: INT32 * Cb_g_tab; /* => table for Cb to G conversion */ michael@0: michael@0: /* For 2:1 vertical sampling, we produce two output rows at a time. michael@0: * We need a "spare" row buffer to hold the second output row if the michael@0: * application provides just a one-row buffer; we also use the spare michael@0: * to discard the dummy last row if the image height is odd. michael@0: */ michael@0: JSAMPROW spare_row; michael@0: boolean spare_full; /* T if spare buffer is occupied */ michael@0: michael@0: JDIMENSION out_row_width; /* samples per output row */ michael@0: JDIMENSION rows_to_go; /* counts rows remaining in image */ michael@0: } my_upsampler; michael@0: michael@0: typedef my_upsampler * my_upsample_ptr; michael@0: michael@0: #define SCALEBITS 16 /* speediest right-shift on some machines */ michael@0: #define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) michael@0: #define FIX(x) ((INT32) ((x) * (1L<RGB colorspace conversion. michael@0: * This is taken directly from jdcolor.c; see that file for more info. michael@0: */ michael@0: michael@0: LOCAL(void) michael@0: build_ycc_rgb_table (j_decompress_ptr cinfo) michael@0: { michael@0: my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; michael@0: int i; michael@0: INT32 x; michael@0: SHIFT_TEMPS michael@0: michael@0: upsample->Cr_r_tab = (int *) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: (MAXJSAMPLE+1) * SIZEOF(int)); michael@0: upsample->Cb_b_tab = (int *) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: (MAXJSAMPLE+1) * SIZEOF(int)); michael@0: upsample->Cr_g_tab = (INT32 *) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: (MAXJSAMPLE+1) * SIZEOF(INT32)); michael@0: upsample->Cb_g_tab = (INT32 *) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: (MAXJSAMPLE+1) * SIZEOF(INT32)); michael@0: michael@0: for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) { michael@0: /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */ michael@0: /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */ michael@0: /* Cr=>R value is nearest int to 1.40200 * x */ michael@0: upsample->Cr_r_tab[i] = (int) michael@0: RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS); michael@0: /* Cb=>B value is nearest int to 1.77200 * x */ michael@0: upsample->Cb_b_tab[i] = (int) michael@0: RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS); michael@0: /* Cr=>G value is scaled-up -0.71414 * x */ michael@0: upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x; michael@0: /* Cb=>G value is scaled-up -0.34414 * x */ michael@0: /* We also add in ONE_HALF so that need not do it in inner loop */ michael@0: upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Initialize for an upsampling pass. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: start_pass_merged_upsample (j_decompress_ptr cinfo) michael@0: { michael@0: my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; michael@0: michael@0: /* Mark the spare buffer empty */ michael@0: upsample->spare_full = FALSE; michael@0: /* Initialize total-height counter for detecting bottom of image */ michael@0: upsample->rows_to_go = cinfo->output_height; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Control routine to do upsampling (and color conversion). michael@0: * michael@0: * The control routine just handles the row buffering considerations. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: merged_2v_upsample (j_decompress_ptr cinfo, michael@0: JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, michael@0: JDIMENSION in_row_groups_avail, michael@0: JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, michael@0: JDIMENSION out_rows_avail) michael@0: /* 2:1 vertical sampling case: may need a spare row. */ michael@0: { michael@0: my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; michael@0: JSAMPROW work_ptrs[2]; michael@0: JDIMENSION num_rows; /* number of rows returned to caller */ michael@0: michael@0: if (upsample->spare_full) { michael@0: /* If we have a spare row saved from a previous cycle, just return it. */ michael@0: jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0, michael@0: 1, upsample->out_row_width); michael@0: num_rows = 1; michael@0: upsample->spare_full = FALSE; michael@0: } else { michael@0: /* Figure number of rows to return to caller. */ michael@0: num_rows = 2; michael@0: /* Not more than the distance to the end of the image. */ michael@0: if (num_rows > upsample->rows_to_go) michael@0: num_rows = upsample->rows_to_go; michael@0: /* And not more than what the client can accept: */ michael@0: out_rows_avail -= *out_row_ctr; michael@0: if (num_rows > out_rows_avail) michael@0: num_rows = out_rows_avail; michael@0: /* Create output pointer array for upsampler. */ michael@0: work_ptrs[0] = output_buf[*out_row_ctr]; michael@0: if (num_rows > 1) { michael@0: work_ptrs[1] = output_buf[*out_row_ctr + 1]; michael@0: } else { michael@0: work_ptrs[1] = upsample->spare_row; michael@0: upsample->spare_full = TRUE; michael@0: } michael@0: /* Now do the upsampling. */ michael@0: (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs); michael@0: } michael@0: michael@0: /* Adjust counts */ michael@0: *out_row_ctr += num_rows; michael@0: upsample->rows_to_go -= num_rows; michael@0: /* When the buffer is emptied, declare this input row group consumed */ michael@0: if (! upsample->spare_full) michael@0: (*in_row_group_ctr)++; michael@0: } michael@0: michael@0: michael@0: METHODDEF(void) michael@0: merged_1v_upsample (j_decompress_ptr cinfo, michael@0: JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, michael@0: JDIMENSION in_row_groups_avail, michael@0: JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, michael@0: JDIMENSION out_rows_avail) michael@0: /* 1:1 vertical sampling case: much easier, never need a spare row. */ michael@0: { michael@0: my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; michael@0: michael@0: /* Just do the upsampling. */ michael@0: (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, michael@0: output_buf + *out_row_ctr); michael@0: /* Adjust counts */ michael@0: (*out_row_ctr)++; michael@0: (*in_row_group_ctr)++; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * These are the routines invoked by the control routines to do michael@0: * the actual upsampling/conversion. One row group is processed per call. michael@0: * michael@0: * Note: since we may be writing directly into application-supplied buffers, michael@0: * we have to be honest about the output width; we can't assume the buffer michael@0: * has been rounded up to an even width. michael@0: */ michael@0: michael@0: michael@0: /* michael@0: * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: h2v1_merged_upsample (j_decompress_ptr cinfo, michael@0: JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, michael@0: JSAMPARRAY output_buf) michael@0: { michael@0: switch (cinfo->out_color_space) { michael@0: case JCS_EXT_RGB: michael@0: extrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, michael@0: output_buf); michael@0: break; michael@0: case JCS_EXT_RGBX: michael@0: case JCS_EXT_RGBA: michael@0: extrgbx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, michael@0: output_buf); michael@0: break; michael@0: case JCS_EXT_BGR: michael@0: extbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, michael@0: output_buf); michael@0: break; michael@0: case JCS_EXT_BGRX: michael@0: case JCS_EXT_BGRA: michael@0: extbgrx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, michael@0: output_buf); michael@0: break; michael@0: case JCS_EXT_XBGR: michael@0: case JCS_EXT_ABGR: michael@0: extxbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, michael@0: output_buf); michael@0: break; michael@0: case JCS_EXT_XRGB: michael@0: case JCS_EXT_ARGB: michael@0: extxrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, michael@0: output_buf); michael@0: break; michael@0: default: michael@0: h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, michael@0: output_buf); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: h2v2_merged_upsample (j_decompress_ptr cinfo, michael@0: JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, michael@0: JSAMPARRAY output_buf) michael@0: { michael@0: switch (cinfo->out_color_space) { michael@0: case JCS_EXT_RGB: michael@0: extrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, michael@0: output_buf); michael@0: break; michael@0: case JCS_EXT_RGBX: michael@0: case JCS_EXT_RGBA: michael@0: extrgbx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, michael@0: output_buf); michael@0: break; michael@0: case JCS_EXT_BGR: michael@0: extbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, michael@0: output_buf); michael@0: break; michael@0: case JCS_EXT_BGRX: michael@0: case JCS_EXT_BGRA: michael@0: extbgrx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, michael@0: output_buf); michael@0: break; michael@0: case JCS_EXT_XBGR: michael@0: case JCS_EXT_ABGR: michael@0: extxbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, michael@0: output_buf); michael@0: break; michael@0: case JCS_EXT_XRGB: michael@0: case JCS_EXT_ARGB: michael@0: extxrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, michael@0: output_buf); michael@0: break; michael@0: default: michael@0: h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr, michael@0: output_buf); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Module initialization routine for merged upsampling/color conversion. michael@0: * michael@0: * NB: this is called under the conditions determined by use_merged_upsample() michael@0: * in jdmaster.c. That routine MUST correspond to the actual capabilities michael@0: * of this module; no safety checks are made here. michael@0: */ michael@0: michael@0: GLOBAL(void) michael@0: jinit_merged_upsampler (j_decompress_ptr cinfo) michael@0: { michael@0: my_upsample_ptr upsample; michael@0: michael@0: upsample = (my_upsample_ptr) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: SIZEOF(my_upsampler)); michael@0: cinfo->upsample = (struct jpeg_upsampler *) upsample; michael@0: upsample->pub.start_pass = start_pass_merged_upsample; michael@0: upsample->pub.need_context_rows = FALSE; michael@0: michael@0: upsample->out_row_width = cinfo->output_width * cinfo->out_color_components; michael@0: michael@0: if (cinfo->max_v_samp_factor == 2) { michael@0: upsample->pub.upsample = merged_2v_upsample; michael@0: if (jsimd_can_h2v2_merged_upsample()) michael@0: upsample->upmethod = jsimd_h2v2_merged_upsample; michael@0: else michael@0: upsample->upmethod = h2v2_merged_upsample; michael@0: /* Allocate a spare row buffer */ michael@0: upsample->spare_row = (JSAMPROW) michael@0: (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: (size_t) (upsample->out_row_width * SIZEOF(JSAMPLE))); michael@0: } else { michael@0: upsample->pub.upsample = merged_1v_upsample; michael@0: if (jsimd_can_h2v1_merged_upsample()) michael@0: upsample->upmethod = jsimd_h2v1_merged_upsample; michael@0: else michael@0: upsample->upmethod = h2v1_merged_upsample; michael@0: /* No spare row needed */ michael@0: upsample->spare_row = NULL; michael@0: } michael@0: michael@0: build_ycc_rgb_table(cinfo); michael@0: } michael@0: michael@0: #endif /* UPSAMPLE_MERGING_SUPPORTED */