⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jccolor.c

📁 This is a resource based on j2me embedded,if you dont understand,you can connection with me .
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * * @(#)jccolor.c	1.12 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. *//* * jccolor.c * * Copyright (C) 1991-1996, 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 input colorspace conversion routines. */#define JPEG_INTERNALS#include "jinclude.h"#include "jpeglib.h"/* Private subobject */typedef struct {  struct jpeg_color_converter pub; /* public fields */  /* Private state for RGB->YCC conversion */  INT32 * rgb_ycc_tab;		/* => table for RGB to YCbCr conversion */} my_color_converter;typedef my_color_converter * my_cconvert_ptr;/**************** RGB -> YCbCr 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 *	Y  =  0.29900 * R + 0.58700 * G + 0.11400 * B *	Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B  + CENTERJSAMPLE *	Cr =  0.50000 * R - 0.41869 * G - 0.08131 * B  + CENTERJSAMPLE * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.) * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2, * rather than CENTERJSAMPLE, for Cb and Cr.  This gave equal positive and * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0) * were not represented exactly.  Now we sacrifice exact representation of * maximum red and maximum blue in order to get exact grayscales. * * 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. * * For even more speed, we avoid doing any multiplications in the inner loop * by precalculating the constants times R,G,B 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 CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included * in the tables to save adding them separately in the inner loop. */#define SCALEBITS	16	/* speediest right-shift on some machines */#define CBCR_OFFSET	((INT32) CENTERJSAMPLE << SCALEBITS)#define ONE_HALF	((INT32) 1 << (SCALEBITS-1))#define FIX(x)		((INT32) ((x) * (1L<<SCALEBITS) + 0.5))/* We allocate one big table and divide it up into eight parts, instead of * doing eight alloc_small requests.  This lets us use a single table base * address, which can be held in a register in the inner loops on many * machines (more than can hold all eight addresses, anyway). */#define R_Y_OFF		0			/* offset to R => Y section */#define G_Y_OFF		(1*(MAXJSAMPLE+1))	/* offset to G => Y section */#define B_Y_OFF		(2*(MAXJSAMPLE+1))	/* etc. */#define R_CB_OFF	(3*(MAXJSAMPLE+1))#define G_CB_OFF	(4*(MAXJSAMPLE+1))#define B_CB_OFF	(5*(MAXJSAMPLE+1))#define R_CR_OFF	B_CB_OFF		/* B=>Cb, R=>Cr are the same */#define G_CR_OFF	(6*(MAXJSAMPLE+1))#define B_CR_OFF	(7*(MAXJSAMPLE+1))#define TABLE_SIZE	(8*(MAXJSAMPLE+1))/* * Initialize for RGB->YCC colorspace conversion. */METHODDEF(void)rgb_ycc_start (j_compress_ptr cinfo){  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;  INT32 * rgb_ycc_tab;  INT32 i;  /* Allocate and fill in the conversion tables. */  cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,				(TABLE_SIZE * SIZEOF(INT32)));  for (i = 0; i <= MAXJSAMPLE; i++) {    rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;    rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;    rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i     + ONE_HALF;    rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;    rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;    /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.     * This ensures that the maximum output will round to MAXJSAMPLE     * not MAXJSAMPLE+1, and thus that we don't have to range-limit.     */    rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i    + CBCR_OFFSET + ONE_HALF-1;/*  B=>Cb and R=>Cr tables are the same    rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i    + CBCR_OFFSET + ONE_HALF-1;*/    rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;    rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;  }}/* * Convert some rows of samples to the JPEG colorspace. * * Note that we change from the application's interleaved-pixel format * to our internal noninterleaved, one-plane-per-component format. * The input buffer is therefore three times as wide as the output buffer. * * A starting row offset is provided only for the output buffer.  The caller * can easily adjust the passed input_buf value to accommodate any row * offset required on that side. */METHODDEF(void)rgb_ycc_convert (j_compress_ptr cinfo,		 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,		 JDIMENSION output_row, int num_rows){  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;  register int r, g, b;  register INT32 * ctab = cconvert->rgb_ycc_tab;  register JSAMPROW inptr;  register JSAMPROW outptr0, outptr1, outptr2;  register JDIMENSION col;  JDIMENSION num_cols = cinfo->image_width;  while (--num_rows >= 0) {    inptr = *input_buf++;    outptr0 = output_buf[0][output_row];    outptr1 = output_buf[1][output_row];    outptr2 = output_buf[2][output_row];    output_row++;    for (col = 0; col < num_cols; col++) {      r = GETJSAMPLE(inptr[RGB_RED]);      g = GETJSAMPLE(inptr[RGB_GREEN]);      b = GETJSAMPLE(inptr[RGB_BLUE]);      inptr += RGB_PIXELSIZE;      /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations       * must be too; we do not need an explicit range-limiting operation.       * Hence the value being shifted is never negative, and we don't       * need the general RIGHT_SHIFT macro.       */      /* Y */      outptr0[col] = (JSAMPLE)		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])		 >> SCALEBITS);      /* Cb */      outptr1[col] = (JSAMPLE)		((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])		 >> SCALEBITS);      /* Cr */      outptr2[col] = (JSAMPLE)		((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])		 >> SCALEBITS);    }  }}/**************** Cases other than RGB -> YCbCr **************//* * Convert some rows of samples to the JPEG colorspace. * This version handles RGB->grayscale conversion, which is the same * as the RGB->Y portion of RGB->YCbCr. * We assume rgb_ycc_start has been called (we only use the Y tables). */METHODDEF(void)rgb_gray_convert (j_compress_ptr cinfo,		  JSAMPARRAY input_buf, JSAMPIMAGE output_buf,		  JDIMENSION output_row, int num_rows){  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;  register int r, g, b;  register INT32 * ctab = cconvert->rgb_ycc_tab;  register JSAMPROW inptr;  register JSAMPROW outptr;  register JDIMENSION col;  JDIMENSION num_cols = cinfo->image_width;  while (--num_rows >= 0) {    inptr = *input_buf++;    outptr = output_buf[0][output_row];    output_row++;    for (col = 0; col < num_cols; col++) {      r = GETJSAMPLE(inptr[RGB_RED]);      g = GETJSAMPLE(inptr[RGB_GREEN]);      b = GETJSAMPLE(inptr[RGB_BLUE]);      inptr += RGB_PIXELSIZE;      /* Y */      outptr[col] = (JSAMPLE)		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])		 >> SCALEBITS);    }  }}#ifdef NIFTYMETHODDEF(void)rgba_ycbcra_convert (j_compress_ptr cinfo,		   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,		   JDIMENSION output_row, int num_rows){  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;  register int r, g, b;  register INT32 * ctab = cconvert->rgb_ycc_tab;  register JSAMPROW inptr;  register JSAMPROW outptr0, outptr1, outptr2, outptr3;  register JDIMENSION col;  JDIMENSION num_cols = cinfo->image_width;   while (--num_rows >= 0) {    inptr = *input_buf++;    outptr0 = output_buf[0][output_row];    outptr1 = output_buf[1][output_row];    outptr2 = output_buf[2][output_row];    outptr3 = output_buf[3][output_row];    output_row++;    for (col = 0; col < num_cols; col++) {      r = GETJSAMPLE(inptr[0]);      g = GETJSAMPLE(inptr[1]);      b = GETJSAMPLE(inptr[2]);      /* Alpha passes through as-is */      outptr3[col] = inptr[3];  /* don't need GETJSAMPLE here */      inptr += 4;      /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations       * must be too; we do not need an explicit range-limiting operation.       * Hence the value being shifted is never negative, and we don't       * need the general RIGHT_SHIFT macro.       */      /* Y */      outptr0[col] = (JSAMPLE)                ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])                 >> SCALEBITS);      /* Cb */      outptr1[col] = (JSAMPLE)                ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])                 >> SCALEBITS);      /* Cr */      outptr2[col] = (JSAMPLE)                ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])                 >> SCALEBITS);    }  }}/* the following version contains a bug which has been   given an eternal life via the FlashPix spec.*/METHODDEF(void)rgba_ycbcra_legacy_convert (j_compress_ptr cinfo,		   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,		   JDIMENSION output_row, int num_rows){  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;  register int r, g, b;  register INT32 * ctab = cconvert->rgb_ycc_tab;  register JSAMPROW inptr;  register JSAMPROW outptr0, outptr1, outptr2, outptr3;  register JDIMENSION col;  JDIMENSION num_cols = cinfo->image_width;   while (--num_rows >= 0) {    inptr = *input_buf++;    outptr0 = output_buf[0][output_row];    outptr1 = output_buf[1][output_row];    outptr2 = output_buf[2][output_row];    outptr3 = output_buf[3][output_row];    output_row++;    for (col = 0; col < num_cols; col++) {      r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);      g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);      b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);      /* Alpha passes through as-is */      outptr3[col] = inptr[3];  /* don't need GETJSAMPLE here */      inptr += 4;      /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations       * must be too; we do not need an explicit range-limiting operation.       * Hence the value being shifted is never negative, and we don't       * need the general RIGHT_SHIFT macro.       */      /* Y */      outptr0[col] = (JSAMPLE)                ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])                 >> SCALEBITS);      /* Cb */      outptr1[col] = (JSAMPLE)                ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])                 >> SCALEBITS);      /* Cr */      outptr2[col] = (JSAMPLE)                ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])                 >> SCALEBITS);    }  }}#endif/* * Convert some rows of samples to the JPEG colorspace. * This version handles Adobe-style CMYK->YCCK conversion,

⌨️ 快捷键说明

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