jdcolor.c

来自「This is a resource based on j2me embedde」· C语言 代码 · 共 588 行 · 第 1/2 页

C
588
字号
/* * * @(#)jdcolor.c	1.15 06/10/03 * * Portions Copyright  2000-2008 Sun Microsystems, Inc. All Rights * Reserved.  Use is subject to license terms. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. *  * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). *  * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA *  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. *//* * jdcolor.c * * Copyright (C) 1991-1997, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains output colorspace conversion routines. */#define JPEG_INTERNALS#include "jinclude.h"#include "jpeglib.h"#ifdef NIFTY/* various goodies used for generating the lookup tables used in   RGB <--> PhotoYCC conversion. */   #include <math.h>#define SCALE_PREC      5#define SCALE_RND       (1 << (SCALE_PREC - 1))#define SCALE           (1 << SCALE_PREC)#define unscale(x)      (((x) + SCALE_RND) >> SCALE_PREC)#define clip(x)         (((long)(x) & ~0xff) ? (((long)(x) < 0) ? 0 : 255) : (long)(x))#endif/* Private subobject */typedef struct {  struct jpeg_color_deconverter pub; /* public fields */  /* Private state for YCC->RGB conversion */  int * Cr_r_tab;		/* => table for Cr to R conversion */  int * Cb_b_tab;		/* => table for Cb to B conversion */  INT32 * Cr_g_tab;		/* => table for Cr to G conversion */  INT32 * Cb_g_tab;		/* => table for Cb to G conversion */#ifdef NIFTY  /* Private state for the PhotoYCC->RGB conversion tables */  coef_c1 *C1;  coef_c2 *C2;  short *xy;#endif} my_color_deconverter;typedef my_color_deconverter * my_cconvert_ptr;/**************** YCbCr -> RGB conversion: most common case **************//* * YCbCr is defined per CCIR 601-1, except that Cb and Cr are * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5. * The conversion equations to be implemented are therefore *	R = Y                + 1.40200 * Cr *	G = Y - 0.34414 * Cb - 0.71414 * Cr *	B = Y + 1.77200 * Cb * where Cb and Cr represent the incoming values less CENTERJSAMPLE. * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.) * * To avoid floating-point arithmetic, we represent the fractional constants * as integers scaled up by 2^16 (about 4 digits precision); we have to divide * the products by 2^16, with appropriate rounding, to get the correct answer. * Notice that Y, being an integral input, does not contribute any fraction * so it need not participate in the rounding. * * For even more speed, we avoid doing any multiplications in the inner loop * by precalculating the constants times Cb and Cr for all possible values. * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table); * for 12-bit samples it is still acceptable.  It's not very reasonable for * 16-bit samples, but if you want lossless storage you shouldn't be changing * colorspace anyway. * The Cr=>R and Cb=>B values can be rounded to integers in advance; the * values for the G calculation are left scaled up, since we must add them * together before rounding. */#define SCALEBITS	16	/* speediest right-shift on some machines */#define ONE_HALF	((INT32) 1 << (SCALEBITS-1))#define FIX(x)		((INT32) ((x) * (1L<<SCALEBITS) + 0.5))/* * Initialize tables for YCC->RGB colorspace conversion. */LOCAL(void)build_ycc_rgb_table (j_decompress_ptr cinfo){  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;  int i;  INT32 x;  SHIFT_TEMPS  cconvert->Cr_r_tab = (int *)    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,				(MAXJSAMPLE+1) * SIZEOF(int));  cconvert->Cb_b_tab = (int *)    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,				(MAXJSAMPLE+1) * SIZEOF(int));  cconvert->Cr_g_tab = (INT32 *)    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,				(MAXJSAMPLE+1) * SIZEOF(INT32));  cconvert->Cb_g_tab = (INT32 *)    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,				(MAXJSAMPLE+1) * SIZEOF(INT32));  for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {    /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */    /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */    /* Cr=>R value is nearest int to 1.40200 * x */    cconvert->Cr_r_tab[i] = (int)		    RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);    /* Cb=>B value is nearest int to 1.77200 * x */    cconvert->Cb_b_tab[i] = (int)		    RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);    /* Cr=>G value is scaled-up -0.71414 * x */    cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;    /* Cb=>G value is scaled-up -0.34414 * x */    /* We also add in ONE_HALF so that need not do it in inner loop */    cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;  }}/* * Convert some rows of samples to the output colorspace. * * Note that we change from noninterleaved, one-plane-per-component format * to interleaved-pixel format.  The output buffer is therefore three times * as wide as the input buffer. * A starting row offset is provided only for the input buffer.  The caller * can easily adjust the passed output_buf value to accommodate any row * offset required on that side. */METHODDEF(void)ycc_rgb_convert (j_decompress_ptr cinfo,		 JSAMPIMAGE input_buf, JDIMENSION input_row,		 JSAMPARRAY output_buf, int num_rows){  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;  register int y, cb, cr;  register JSAMPROW outptr;  register JSAMPROW inptr0, inptr1, inptr2;  register JDIMENSION col;  JDIMENSION num_cols = cinfo->output_width;  /* copy these pointers into registers if possible */  register JSAMPLE * range_limit = cinfo->sample_range_limit;  register int * Crrtab = cconvert->Cr_r_tab;  register int * Cbbtab = cconvert->Cb_b_tab;  register INT32 * Crgtab = cconvert->Cr_g_tab;  register INT32 * Cbgtab = cconvert->Cb_g_tab;  SHIFT_TEMPS  while (--num_rows >= 0) {    inptr0 = input_buf[0][input_row];    inptr1 = input_buf[1][input_row];    inptr2 = input_buf[2][input_row];    input_row++;    outptr = *output_buf++;    for (col = 0; col < num_cols; col++) {      y  = GETJSAMPLE(inptr0[col]);      cb = GETJSAMPLE(inptr1[col]);      cr = GETJSAMPLE(inptr2[col]);      /* Range-limiting is essential due to noise introduced by DCT losses. */      outptr[RGB_RED] =   range_limit[y + Crrtab[cr]];      outptr[RGB_GREEN] = range_limit[y +			      ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],						 SCALEBITS))];      outptr[RGB_BLUE] =  range_limit[y + Cbbtab[cb]];      outptr += RGB_PIXELSIZE;    }  }}/**************** Cases other than YCbCr -> RGB **************//* * Color conversion for no colorspace change: just copy the data, * converting from separate-planes to interleaved representation. */METHODDEF(void)null_convert (j_decompress_ptr cinfo,	      JSAMPIMAGE input_buf, JDIMENSION input_row,	      JSAMPARRAY output_buf, int num_rows){  register JSAMPROW inptr, outptr;  register JDIMENSION count;  register int num_components = cinfo->num_components;  JDIMENSION num_cols = cinfo->output_width;  int ci;  while (--num_rows >= 0) {    for (ci = 0; ci < num_components; ci++) {      inptr = input_buf[ci][input_row];      outptr = output_buf[0] + ci;      for (count = num_cols; count > 0; count--) {	*outptr = *inptr++;	/* needn't bother with GETJSAMPLE() here */	outptr += num_components;      }    }    input_row++;    output_buf++;  }}/* * Color conversion for grayscale: just copy the data. * This also works for YCbCr -> grayscale conversion, in which * we just copy the Y (luminance) component and ignore chrominance. */METHODDEF(void)grayscale_convert (j_decompress_ptr cinfo,		   JSAMPIMAGE input_buf, JDIMENSION input_row,		   JSAMPARRAY output_buf, int num_rows){  jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,		    num_rows, cinfo->output_width);}#ifdef NIFTYMETHODDEF(void)ycbcra_rgba_convert (j_decompress_ptr cinfo,                   JSAMPIMAGE input_buf, JDIMENSION input_row,                   JSAMPARRAY output_buf, int num_rows){  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;  register int y, cb, cr;  register JSAMPROW outptr;  register JSAMPROW inptr0, inptr1, inptr2, inptr3;  register JDIMENSION col;  JDIMENSION num_cols = cinfo->output_width;  /* copy these pointers into registers if possible */  register JSAMPLE * range_limit = cinfo->sample_range_limit;  register int * Crrtab = cconvert->Cr_r_tab;  register int * Cbbtab = cconvert->Cb_b_tab;  register INT32 * Crgtab = cconvert->Cr_g_tab;  register INT32 * Cbgtab = cconvert->Cb_g_tab;  SHIFT_TEMPS   while (--num_rows >= 0) {    inptr0 = input_buf[0][input_row];    inptr1 = input_buf[1][input_row];    inptr2 = input_buf[2][input_row];    inptr3 = input_buf[3][input_row];    input_row++;    outptr = *output_buf++;    for (col = 0; col < num_cols; col++) {      y  = GETJSAMPLE(inptr0[col]);      cb = GETJSAMPLE(inptr1[col]);      cr = GETJSAMPLE(inptr2[col]);      /* Range-limiting is essential due to noise introduced by DCT losses. */      outptr[0] = range_limit[(y + Crrtab[cr])];   /* red */      outptr[1] = range_limit[(y +                 /* green */                              ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],                                                 SCALEBITS)))];

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?