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