Path: blob/master/src/java.desktop/share/native/libjavajpeg/jdmerge.c
41149 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jdmerge.c6*7* Copyright (C) 1994-1996, Thomas G. Lane.8* This file is part of the Independent JPEG Group's software.9* For conditions of distribution and use, see the accompanying README file.10*11* This file contains code for merged upsampling/color conversion.12*13* This file combines functions from jdsample.c and jdcolor.c;14* read those files first to understand what's going on.15*16* When the chroma components are to be upsampled by simple replication17* (ie, box filtering), we can save some work in color conversion by18* calculating all the output pixels corresponding to a pair of chroma19* samples at one time. In the conversion equations20* R = Y + K1 * Cr21* G = Y + K2 * Cb + K3 * Cr22* B = Y + K4 * Cb23* only the Y term varies among the group of pixels corresponding to a pair24* of chroma samples, so the rest of the terms can be calculated just once.25* At typical sampling ratios, this eliminates half or three-quarters of the26* multiplications needed for color conversion.27*28* This file currently provides implementations for the following cases:29* YCbCr => RGB color conversion only.30* Sampling ratios of 2h1v or 2h2v.31* No scaling needed at upsample time.32* Corner-aligned (non-CCIR601) sampling alignment.33* Other special cases could be added, but in most applications these are34* the only common cases. (For uncommon cases we fall back on the more35* general code in jdsample.c and jdcolor.c.)36*/3738#define JPEG_INTERNALS39#include "jinclude.h"40#include "jpeglib.h"4142#ifdef UPSAMPLE_MERGING_SUPPORTED434445/* Private subobject */4647typedef struct {48struct jpeg_upsampler pub; /* public fields */4950/* Pointer to routine to do actual upsampling/conversion of one row group */51JMETHOD(void, upmethod, (j_decompress_ptr cinfo,52JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,53JSAMPARRAY output_buf));5455/* Private state for YCC->RGB conversion */56int * Cr_r_tab; /* => table for Cr to R conversion */57int * Cb_b_tab; /* => table for Cb to B conversion */58INT32 * Cr_g_tab; /* => table for Cr to G conversion */59INT32 * Cb_g_tab; /* => table for Cb to G conversion */6061/* For 2:1 vertical sampling, we produce two output rows at a time.62* We need a "spare" row buffer to hold the second output row if the63* application provides just a one-row buffer; we also use the spare64* to discard the dummy last row if the image height is odd.65*/66JSAMPROW spare_row;67boolean spare_full; /* T if spare buffer is occupied */6869JDIMENSION out_row_width; /* samples per output row */70JDIMENSION rows_to_go; /* counts rows remaining in image */71} my_upsampler;7273typedef my_upsampler * my_upsample_ptr;7475#define SCALEBITS 16 /* speediest right-shift on some machines */76#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))77#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))787980/*81* Initialize tables for YCC->RGB colorspace conversion.82* This is taken directly from jdcolor.c; see that file for more info.83*/8485LOCAL(void)86build_ycc_rgb_table (j_decompress_ptr cinfo)87{88my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;89int i;90INT32 x;91SHIFT_TEMPS9293upsample->Cr_r_tab = (int *)94(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,95(MAXJSAMPLE+1) * SIZEOF(int));96upsample->Cb_b_tab = (int *)97(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,98(MAXJSAMPLE+1) * SIZEOF(int));99upsample->Cr_g_tab = (INT32 *)100(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,101(MAXJSAMPLE+1) * SIZEOF(INT32));102upsample->Cb_g_tab = (INT32 *)103(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,104(MAXJSAMPLE+1) * SIZEOF(INT32));105106for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {107/* i is the actual input pixel value, in the range 0..MAXJSAMPLE */108/* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */109/* Cr=>R value is nearest int to 1.40200 * x */110upsample->Cr_r_tab[i] = (int)111RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);112/* Cb=>B value is nearest int to 1.77200 * x */113upsample->Cb_b_tab[i] = (int)114RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);115/* Cr=>G value is scaled-up -0.71414 * x */116upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x;117/* Cb=>G value is scaled-up -0.34414 * x */118/* We also add in ONE_HALF so that need not do it in inner loop */119upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;120}121}122123124/*125* Initialize for an upsampling pass.126*/127128METHODDEF(void)129start_pass_merged_upsample (j_decompress_ptr cinfo)130{131my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;132133/* Mark the spare buffer empty */134upsample->spare_full = FALSE;135/* Initialize total-height counter for detecting bottom of image */136upsample->rows_to_go = cinfo->output_height;137}138139140/*141* Control routine to do upsampling (and color conversion).142*143* The control routine just handles the row buffering considerations.144*/145146METHODDEF(void)147merged_2v_upsample (j_decompress_ptr cinfo,148JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,149JDIMENSION in_row_groups_avail,150JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,151JDIMENSION out_rows_avail)152/* 2:1 vertical sampling case: may need a spare row. */153{154my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;155JSAMPROW work_ptrs[2];156JDIMENSION num_rows; /* number of rows returned to caller */157158if (upsample->spare_full) {159/* If we have a spare row saved from a previous cycle, just return it. */160jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,1611, upsample->out_row_width);162num_rows = 1;163upsample->spare_full = FALSE;164} else {165/* Figure number of rows to return to caller. */166num_rows = 2;167/* Not more than the distance to the end of the image. */168if (num_rows > upsample->rows_to_go)169num_rows = upsample->rows_to_go;170/* And not more than what the client can accept: */171out_rows_avail -= *out_row_ctr;172if (num_rows > out_rows_avail)173num_rows = out_rows_avail;174/* Create output pointer array for upsampler. */175work_ptrs[0] = output_buf[*out_row_ctr];176if (num_rows > 1) {177work_ptrs[1] = output_buf[*out_row_ctr + 1];178} else {179work_ptrs[1] = upsample->spare_row;180upsample->spare_full = TRUE;181}182/* Now do the upsampling. */183(*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);184}185186/* Adjust counts */187*out_row_ctr += num_rows;188upsample->rows_to_go -= num_rows;189/* When the buffer is emptied, declare this input row group consumed */190if (! upsample->spare_full)191(*in_row_group_ctr)++;192}193194195METHODDEF(void)196merged_1v_upsample (j_decompress_ptr cinfo,197JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,198JDIMENSION in_row_groups_avail,199JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,200JDIMENSION out_rows_avail)201/* 1:1 vertical sampling case: much easier, never need a spare row. */202{203my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;204205/* Just do the upsampling. */206(*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,207output_buf + *out_row_ctr);208/* Adjust counts */209(*out_row_ctr)++;210(*in_row_group_ctr)++;211}212213214/*215* These are the routines invoked by the control routines to do216* the actual upsampling/conversion. One row group is processed per call.217*218* Note: since we may be writing directly into application-supplied buffers,219* we have to be honest about the output width; we can't assume the buffer220* has been rounded up to an even width.221*/222223224/*225* Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.226*/227228METHODDEF(void)229h2v1_merged_upsample (j_decompress_ptr cinfo,230JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,231JSAMPARRAY output_buf)232{233my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;234register int y, cred, cgreen, cblue;235int cb, cr;236register JSAMPROW outptr;237JSAMPROW inptr0, inptr1, inptr2;238JDIMENSION col;239/* copy these pointers into registers if possible */240register JSAMPLE * range_limit = cinfo->sample_range_limit;241int * Crrtab = upsample->Cr_r_tab;242int * Cbbtab = upsample->Cb_b_tab;243INT32 * Crgtab = upsample->Cr_g_tab;244INT32 * Cbgtab = upsample->Cb_g_tab;245SHIFT_TEMPS246247inptr0 = input_buf[0][in_row_group_ctr];248inptr1 = input_buf[1][in_row_group_ctr];249inptr2 = input_buf[2][in_row_group_ctr];250outptr = output_buf[0];251/* Loop for each pair of output pixels */252for (col = cinfo->output_width >> 1; col > 0; col--) {253/* Do the chroma part of the calculation */254cb = GETJSAMPLE(*inptr1++);255cr = GETJSAMPLE(*inptr2++);256cred = Crrtab[cr];257cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);258cblue = Cbbtab[cb];259/* Fetch 2 Y values and emit 2 pixels */260y = GETJSAMPLE(*inptr0++);261outptr[RGB_RED] = range_limit[y + cred];262outptr[RGB_GREEN] = range_limit[y + cgreen];263outptr[RGB_BLUE] = range_limit[y + cblue];264outptr += RGB_PIXELSIZE;265y = GETJSAMPLE(*inptr0++);266outptr[RGB_RED] = range_limit[y + cred];267outptr[RGB_GREEN] = range_limit[y + cgreen];268outptr[RGB_BLUE] = range_limit[y + cblue];269outptr += RGB_PIXELSIZE;270}271/* If image width is odd, do the last output column separately */272if (cinfo->output_width & 1) {273cb = GETJSAMPLE(*inptr1);274cr = GETJSAMPLE(*inptr2);275cred = Crrtab[cr];276cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);277cblue = Cbbtab[cb];278y = GETJSAMPLE(*inptr0);279outptr[RGB_RED] = range_limit[y + cred];280outptr[RGB_GREEN] = range_limit[y + cgreen];281outptr[RGB_BLUE] = range_limit[y + cblue];282}283}284285286/*287* Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.288*/289290METHODDEF(void)291h2v2_merged_upsample (j_decompress_ptr cinfo,292JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,293JSAMPARRAY output_buf)294{295my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;296register int y, cred, cgreen, cblue;297int cb, cr;298register JSAMPROW outptr0, outptr1;299JSAMPROW inptr00, inptr01, inptr1, inptr2;300JDIMENSION col;301/* copy these pointers into registers if possible */302register JSAMPLE * range_limit = cinfo->sample_range_limit;303int * Crrtab = upsample->Cr_r_tab;304int * Cbbtab = upsample->Cb_b_tab;305INT32 * Crgtab = upsample->Cr_g_tab;306INT32 * Cbgtab = upsample->Cb_g_tab;307SHIFT_TEMPS308309inptr00 = input_buf[0][in_row_group_ctr*2];310inptr01 = input_buf[0][in_row_group_ctr*2 + 1];311inptr1 = input_buf[1][in_row_group_ctr];312inptr2 = input_buf[2][in_row_group_ctr];313outptr0 = output_buf[0];314outptr1 = output_buf[1];315/* Loop for each group of output pixels */316for (col = cinfo->output_width >> 1; col > 0; col--) {317/* Do the chroma part of the calculation */318cb = GETJSAMPLE(*inptr1++);319cr = GETJSAMPLE(*inptr2++);320cred = Crrtab[cr];321cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);322cblue = Cbbtab[cb];323/* Fetch 4 Y values and emit 4 pixels */324y = GETJSAMPLE(*inptr00++);325outptr0[RGB_RED] = range_limit[y + cred];326outptr0[RGB_GREEN] = range_limit[y + cgreen];327outptr0[RGB_BLUE] = range_limit[y + cblue];328outptr0 += RGB_PIXELSIZE;329y = GETJSAMPLE(*inptr00++);330outptr0[RGB_RED] = range_limit[y + cred];331outptr0[RGB_GREEN] = range_limit[y + cgreen];332outptr0[RGB_BLUE] = range_limit[y + cblue];333outptr0 += RGB_PIXELSIZE;334y = GETJSAMPLE(*inptr01++);335outptr1[RGB_RED] = range_limit[y + cred];336outptr1[RGB_GREEN] = range_limit[y + cgreen];337outptr1[RGB_BLUE] = range_limit[y + cblue];338outptr1 += RGB_PIXELSIZE;339y = GETJSAMPLE(*inptr01++);340outptr1[RGB_RED] = range_limit[y + cred];341outptr1[RGB_GREEN] = range_limit[y + cgreen];342outptr1[RGB_BLUE] = range_limit[y + cblue];343outptr1 += RGB_PIXELSIZE;344}345/* If image width is odd, do the last output column separately */346if (cinfo->output_width & 1) {347cb = GETJSAMPLE(*inptr1);348cr = GETJSAMPLE(*inptr2);349cred = Crrtab[cr];350cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);351cblue = Cbbtab[cb];352y = GETJSAMPLE(*inptr00);353outptr0[RGB_RED] = range_limit[y + cred];354outptr0[RGB_GREEN] = range_limit[y + cgreen];355outptr0[RGB_BLUE] = range_limit[y + cblue];356y = GETJSAMPLE(*inptr01);357outptr1[RGB_RED] = range_limit[y + cred];358outptr1[RGB_GREEN] = range_limit[y + cgreen];359outptr1[RGB_BLUE] = range_limit[y + cblue];360}361}362363364/*365* Module initialization routine for merged upsampling/color conversion.366*367* NB: this is called under the conditions determined by use_merged_upsample()368* in jdmaster.c. That routine MUST correspond to the actual capabilities369* of this module; no safety checks are made here.370*/371372GLOBAL(void)373jinit_merged_upsampler (j_decompress_ptr cinfo)374{375my_upsample_ptr upsample;376377upsample = (my_upsample_ptr)378(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,379SIZEOF(my_upsampler));380cinfo->upsample = (struct jpeg_upsampler *) upsample;381upsample->pub.start_pass = start_pass_merged_upsample;382upsample->pub.need_context_rows = FALSE;383384upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;385386if (cinfo->max_v_samp_factor == 2) {387upsample->pub.upsample = merged_2v_upsample;388upsample->upmethod = h2v2_merged_upsample;389/* Allocate a spare row buffer */390upsample->spare_row = (JSAMPROW)391(*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,392(size_t) (upsample->out_row_width * SIZEOF(JSAMPLE)));393} else {394upsample->pub.upsample = merged_1v_upsample;395upsample->upmethod = h2v1_merged_upsample;396/* No spare row needed */397upsample->spare_row = NULL;398}399400build_ycc_rgb_table(cinfo);401}402403#endif /* UPSAMPLE_MERGING_SUPPORTED */404405406