1991-10-06 20:00:00 -04:00
|
|
|
/*
|
|
|
|
* jdcolor.c
|
|
|
|
*
|
1998-03-26 19:00:00 -05:00
|
|
|
* Copyright (C) 1991-1997, Thomas G. Lane.
|
2013-01-12 19:00:00 -05:00
|
|
|
* Modified 2011-2012 by Guido Vollbeding.
|
1991-10-06 20:00:00 -04:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
#define JPEG_INTERNALS
|
1991-10-06 20:00:00 -04:00
|
|
|
#include "jinclude.h"
|
1994-09-23 20:00:00 -04:00
|
|
|
#include "jpeglib.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* 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 */
|
2012-01-14 19:00:00 -05:00
|
|
|
|
|
|
|
/* Private state for RGB->Y conversion */
|
|
|
|
INT32 * rgb_y_tab; /* => table for RGB to Y conversion */
|
1994-09-23 20:00:00 -04:00
|
|
|
} my_color_deconverter;
|
|
|
|
|
|
|
|
typedef my_color_deconverter * my_cconvert_ptr;
|
1991-10-06 20:00:00 -04:00
|
|
|
|
|
|
|
|
1992-03-16 19:00:00 -05:00
|
|
|
/**************** YCbCr -> RGB conversion: most common case **************/
|
2012-01-14 19:00:00 -05:00
|
|
|
/**************** RGB -> Y conversion: less common case **************/
|
1992-03-16 19:00:00 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2012-01-14 19:00:00 -05:00
|
|
|
*
|
1992-03-16 19:00:00 -05:00
|
|
|
* R = Y + 1.40200 * Cr
|
|
|
|
* G = Y - 0.34414 * Cb - 0.71414 * Cr
|
|
|
|
* B = Y + 1.77200 * Cb
|
2012-01-14 19:00:00 -05:00
|
|
|
*
|
|
|
|
* Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
|
|
|
|
*
|
1994-12-06 19:00:00 -05:00
|
|
|
* where Cb and Cr represent the incoming values less CENTERJSAMPLE.
|
1992-12-09 19:00:00 -05:00
|
|
|
* (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
|
1992-03-16 19:00:00 -05:00
|
|
|
*
|
|
|
|
* To avoid floating-point arithmetic, we represent the fractional constants
|
1992-12-09 19:00:00 -05:00
|
|
|
* 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.
|
1992-03-16 19:00:00 -05:00
|
|
|
* 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.
|
1992-12-09 19:00:00 -05:00
|
|
|
* 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
|
1992-03-16 19:00:00 -05:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
#define SCALEBITS 16 /* speediest right-shift on some machines */
|
1992-03-16 19:00:00 -05:00
|
|
|
#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
|
|
|
|
#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
|
|
|
|
|
2012-01-14 19:00:00 -05:00
|
|
|
/* We allocate one big table for RGB->Y conversion and divide it up into
|
|
|
|
* three parts, instead of doing three 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 three 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 TABLE_SIZE (3*(MAXJSAMPLE+1))
|
|
|
|
|
1992-03-16 19:00:00 -05:00
|
|
|
|
1991-10-06 20:00:00 -04:00
|
|
|
/*
|
1995-08-01 20:00:00 -04:00
|
|
|
* Initialize tables for YCC->RGB colorspace conversion.
|
1991-10-06 20:00:00 -04:00
|
|
|
*/
|
|
|
|
|
1996-02-06 19:00:00 -05:00
|
|
|
LOCAL(void)
|
1995-08-01 20:00:00 -04:00
|
|
|
build_ycc_rgb_table (j_decompress_ptr cinfo)
|
1991-10-06 20:00:00 -04:00
|
|
|
{
|
1994-09-23 20:00:00 -04:00
|
|
|
my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
|
1995-08-01 20:00:00 -04:00
|
|
|
int i;
|
|
|
|
INT32 x;
|
1992-03-16 19:00:00 -05:00
|
|
|
SHIFT_TEMPS
|
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
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));
|
1992-03-16 19:00:00 -05:00
|
|
|
|
1994-12-06 19:00:00 -05:00
|
|
|
for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
|
1992-03-16 19:00:00 -05:00
|
|
|
/* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
|
1994-12-06 19:00:00 -05:00
|
|
|
/* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
|
1992-03-16 19:00:00 -05:00
|
|
|
/* Cr=>R value is nearest int to 1.40200 * x */
|
1994-09-23 20:00:00 -04:00
|
|
|
cconvert->Cr_r_tab[i] = (int)
|
1994-12-06 19:00:00 -05:00
|
|
|
RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
|
1992-03-16 19:00:00 -05:00
|
|
|
/* Cb=>B value is nearest int to 1.77200 * x */
|
1994-09-23 20:00:00 -04:00
|
|
|
cconvert->Cb_b_tab[i] = (int)
|
1994-12-06 19:00:00 -05:00
|
|
|
RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
|
1992-03-16 19:00:00 -05:00
|
|
|
/* Cr=>G value is scaled-up -0.71414 * x */
|
1994-12-06 19:00:00 -05:00
|
|
|
cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
|
1992-03-16 19:00:00 -05:00
|
|
|
/* Cb=>G value is scaled-up -0.34414 * x */
|
|
|
|
/* We also add in ONE_HALF so that need not do it in inner loop */
|
1994-12-06 19:00:00 -05:00
|
|
|
cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
|
1992-03-16 19:00:00 -05:00
|
|
|
}
|
1991-10-06 20:00:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert some rows of samples to the output colorspace.
|
1994-09-23 20:00:00 -04:00
|
|
|
*
|
|
|
|
* 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.
|
1991-10-06 20:00:00 -04:00
|
|
|
*/
|
|
|
|
|
1996-02-06 19:00:00 -05:00
|
|
|
METHODDEF(void)
|
1994-09-23 20:00:00 -04:00
|
|
|
ycc_rgb_convert (j_decompress_ptr cinfo,
|
|
|
|
JSAMPIMAGE input_buf, JDIMENSION input_row,
|
|
|
|
JSAMPARRAY output_buf, int num_rows)
|
1991-10-06 20:00:00 -04:00
|
|
|
{
|
1994-09-23 20:00:00 -04:00
|
|
|
my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
|
1992-03-16 19:00:00 -05:00
|
|
|
register int y, cb, cr;
|
1994-09-23 20:00:00 -04:00
|
|
|
register JSAMPROW outptr;
|
1991-10-06 20:00:00 -04:00
|
|
|
register JSAMPROW inptr0, inptr1, inptr2;
|
1994-09-23 20:00:00 -04:00
|
|
|
register JDIMENSION col;
|
|
|
|
JDIMENSION num_cols = cinfo->output_width;
|
1992-12-09 19:00:00 -05:00
|
|
|
/* copy these pointers into registers if possible */
|
|
|
|
register JSAMPLE * range_limit = cinfo->sample_range_limit;
|
1994-09-23 20:00:00 -04:00
|
|
|
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;
|
1992-03-16 19:00:00 -05:00
|
|
|
SHIFT_TEMPS
|
1994-09-23 20:00:00 -04:00
|
|
|
|
|
|
|
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++;
|
1992-12-09 19:00:00 -05:00
|
|
|
for (col = 0; col < num_cols; col++) {
|
|
|
|
y = GETJSAMPLE(inptr0[col]);
|
|
|
|
cb = GETJSAMPLE(inptr1[col]);
|
|
|
|
cr = GETJSAMPLE(inptr2[col]);
|
1994-12-06 19:00:00 -05:00
|
|
|
/* Range-limiting is essential due to noise introduced by DCT losses. */
|
1994-09-23 20:00:00 -04:00
|
|
|
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;
|
1991-10-06 20:00:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1992-03-16 19:00:00 -05:00
|
|
|
/**************** Cases other than YCbCr -> RGB **************/
|
|
|
|
|
|
|
|
|
2012-01-14 19:00:00 -05:00
|
|
|
/*
|
|
|
|
* Initialize for RGB->grayscale colorspace conversion.
|
|
|
|
*/
|
|
|
|
|
|
|
|
LOCAL(void)
|
|
|
|
build_rgb_y_table (j_decompress_ptr cinfo)
|
|
|
|
{
|
|
|
|
my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
|
|
|
|
INT32 * rgb_y_tab;
|
|
|
|
INT32 i;
|
|
|
|
|
|
|
|
/* Allocate and fill in the conversion tables. */
|
|
|
|
cconvert->rgb_y_tab = rgb_y_tab = (INT32 *)
|
|
|
|
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
|
|
|
(TABLE_SIZE * SIZEOF(INT32)));
|
|
|
|
|
|
|
|
for (i = 0; i <= MAXJSAMPLE; i++) {
|
|
|
|
rgb_y_tab[i+R_Y_OFF] = FIX(0.29900) * i;
|
|
|
|
rgb_y_tab[i+G_Y_OFF] = FIX(0.58700) * i;
|
|
|
|
rgb_y_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert RGB to grayscale.
|
|
|
|
*/
|
|
|
|
|
|
|
|
METHODDEF(void)
|
|
|
|
rgb_gray_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;
|
2013-01-12 19:00:00 -05:00
|
|
|
register INT32 * ctab = cconvert->rgb_y_tab;
|
|
|
|
register int r, g, b;
|
|
|
|
register JSAMPROW outptr;
|
|
|
|
register JSAMPROW inptr0, inptr1, inptr2;
|
|
|
|
register JDIMENSION col;
|
|
|
|
JDIMENSION num_cols = cinfo->output_width;
|
|
|
|
|
|
|
|
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++) {
|
|
|
|
r = GETJSAMPLE(inptr0[col]);
|
|
|
|
g = GETJSAMPLE(inptr1[col]);
|
|
|
|
b = GETJSAMPLE(inptr2[col]);
|
|
|
|
/* Y */
|
|
|
|
outptr[col] = (JSAMPLE)
|
|
|
|
((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
|
|
|
|
>> SCALEBITS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* [R-G,G,B-G] to [R,G,B] conversion with modulo calculation
|
|
|
|
* (inverse color transform).
|
|
|
|
*/
|
|
|
|
|
|
|
|
METHODDEF(void)
|
|
|
|
rgb1_rgb_convert (j_decompress_ptr cinfo,
|
|
|
|
JSAMPIMAGE input_buf, JDIMENSION input_row,
|
|
|
|
JSAMPARRAY output_buf, int num_rows)
|
|
|
|
{
|
2012-01-14 19:00:00 -05:00
|
|
|
register int r, g, b;
|
2013-01-12 19:00:00 -05:00
|
|
|
register JSAMPROW outptr;
|
|
|
|
register JSAMPROW inptr0, inptr1, inptr2;
|
|
|
|
register JDIMENSION col;
|
|
|
|
JDIMENSION num_cols = cinfo->output_width;
|
|
|
|
|
|
|
|
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++) {
|
|
|
|
r = GETJSAMPLE(inptr0[col]);
|
|
|
|
g = GETJSAMPLE(inptr1[col]);
|
|
|
|
b = GETJSAMPLE(inptr2[col]);
|
|
|
|
/* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD
|
|
|
|
* (modulo) operator is equivalent to the bitmask operator AND.
|
|
|
|
*/
|
|
|
|
outptr[RGB_RED] = (JSAMPLE) ((r + g - CENTERJSAMPLE) & MAXJSAMPLE);
|
|
|
|
outptr[RGB_GREEN] = (JSAMPLE) g;
|
|
|
|
outptr[RGB_BLUE] = (JSAMPLE) ((b + g - CENTERJSAMPLE) & MAXJSAMPLE);
|
|
|
|
outptr += RGB_PIXELSIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* [R-G,G,B-G] to grayscale conversion with modulo calculation
|
|
|
|
* (inverse color transform).
|
|
|
|
*/
|
|
|
|
|
|
|
|
METHODDEF(void)
|
|
|
|
rgb1_gray_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;
|
2012-01-14 19:00:00 -05:00
|
|
|
register INT32 * ctab = cconvert->rgb_y_tab;
|
2013-01-12 19:00:00 -05:00
|
|
|
register int r, g, b;
|
2012-01-14 19:00:00 -05:00
|
|
|
register JSAMPROW outptr;
|
|
|
|
register JSAMPROW inptr0, inptr1, inptr2;
|
|
|
|
register JDIMENSION col;
|
|
|
|
JDIMENSION num_cols = cinfo->output_width;
|
|
|
|
|
|
|
|
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++) {
|
|
|
|
r = GETJSAMPLE(inptr0[col]);
|
|
|
|
g = GETJSAMPLE(inptr1[col]);
|
|
|
|
b = GETJSAMPLE(inptr2[col]);
|
2013-01-12 19:00:00 -05:00
|
|
|
/* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD
|
|
|
|
* (modulo) operator is equivalent to the bitmask operator AND.
|
|
|
|
*/
|
|
|
|
r = (r + g - CENTERJSAMPLE) & MAXJSAMPLE;
|
|
|
|
b = (b + g - CENTERJSAMPLE) & MAXJSAMPLE;
|
2012-01-14 19:00:00 -05:00
|
|
|
/* Y */
|
|
|
|
outptr[col] = (JSAMPLE)
|
|
|
|
((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
|
|
|
|
>> SCALEBITS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No colorspace change, but conversion from separate-planes
|
|
|
|
* to interleaved representation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
METHODDEF(void)
|
|
|
|
rgb_convert (j_decompress_ptr cinfo,
|
|
|
|
JSAMPIMAGE input_buf, JDIMENSION input_row,
|
|
|
|
JSAMPARRAY output_buf, int num_rows)
|
|
|
|
{
|
|
|
|
register JSAMPROW outptr;
|
|
|
|
register JSAMPROW inptr0, inptr1, inptr2;
|
|
|
|
register JDIMENSION col;
|
|
|
|
JDIMENSION num_cols = cinfo->output_width;
|
|
|
|
|
|
|
|
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++) {
|
|
|
|
/* We can dispense with GETJSAMPLE() here */
|
|
|
|
outptr[RGB_RED] = inptr0[col];
|
|
|
|
outptr[RGB_GREEN] = inptr1[col];
|
|
|
|
outptr[RGB_BLUE] = inptr2[col];
|
|
|
|
outptr += RGB_PIXELSIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1992-03-16 19:00:00 -05:00
|
|
|
/*
|
1994-09-23 20:00:00 -04:00
|
|
|
* Color conversion for no colorspace change: just copy the data,
|
|
|
|
* converting from separate-planes to interleaved representation.
|
1992-03-16 19:00:00 -05:00
|
|
|
*/
|
|
|
|
|
1996-02-06 19:00:00 -05:00
|
|
|
METHODDEF(void)
|
1994-09-23 20:00:00 -04:00
|
|
|
null_convert (j_decompress_ptr cinfo,
|
|
|
|
JSAMPIMAGE input_buf, JDIMENSION input_row,
|
|
|
|
JSAMPARRAY output_buf, int num_rows)
|
1992-03-16 19:00:00 -05:00
|
|
|
{
|
1994-09-23 20:00:00 -04:00
|
|
|
int ci;
|
2013-01-12 19:00:00 -05:00
|
|
|
register int nc = cinfo->num_components;
|
|
|
|
register JSAMPROW outptr;
|
|
|
|
register JSAMPROW inptr;
|
|
|
|
register JDIMENSION col;
|
|
|
|
JDIMENSION num_cols = cinfo->output_width;
|
1994-09-23 20:00:00 -04:00
|
|
|
|
|
|
|
while (--num_rows >= 0) {
|
2013-01-12 19:00:00 -05:00
|
|
|
for (ci = 0; ci < nc; ci++) {
|
1994-09-23 20:00:00 -04:00
|
|
|
inptr = input_buf[ci][input_row];
|
|
|
|
outptr = output_buf[0] + ci;
|
2013-01-12 19:00:00 -05:00
|
|
|
for (col = 0; col < num_cols; col++) {
|
1994-09-23 20:00:00 -04:00
|
|
|
*outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */
|
2013-01-12 19:00:00 -05:00
|
|
|
outptr += nc;
|
1994-09-23 20:00:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
input_row++;
|
|
|
|
output_buf++;
|
|
|
|
}
|
1992-03-16 19:00:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1991-10-06 20:00:00 -04:00
|
|
|
/*
|
1994-09-23 20:00:00 -04:00
|
|
|
* 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.
|
1991-10-06 20:00:00 -04:00
|
|
|
*/
|
|
|
|
|
1996-02-06 19:00:00 -05:00
|
|
|
METHODDEF(void)
|
1994-09-23 20:00:00 -04:00
|
|
|
grayscale_convert (j_decompress_ptr cinfo,
|
|
|
|
JSAMPIMAGE input_buf, JDIMENSION input_row,
|
|
|
|
JSAMPARRAY output_buf, int num_rows)
|
1991-10-06 20:00:00 -04:00
|
|
|
{
|
1994-09-23 20:00:00 -04:00
|
|
|
jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
|
|
|
|
num_rows, cinfo->output_width);
|
1991-10-06 20:00:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-03-26 19:00:00 -05:00
|
|
|
/*
|
|
|
|
* Convert grayscale to RGB: just duplicate the graylevel three times.
|
|
|
|
* This is provided to support applications that don't want to cope
|
|
|
|
* with grayscale as a separate case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
METHODDEF(void)
|
|
|
|
gray_rgb_convert (j_decompress_ptr cinfo,
|
|
|
|
JSAMPIMAGE input_buf, JDIMENSION input_row,
|
|
|
|
JSAMPARRAY output_buf, int num_rows)
|
|
|
|
{
|
2013-01-12 19:00:00 -05:00
|
|
|
register JSAMPROW outptr;
|
|
|
|
register JSAMPROW inptr;
|
1998-03-26 19:00:00 -05:00
|
|
|
register JDIMENSION col;
|
|
|
|
JDIMENSION num_cols = cinfo->output_width;
|
|
|
|
|
|
|
|
while (--num_rows >= 0) {
|
|
|
|
inptr = input_buf[0][input_row++];
|
|
|
|
outptr = *output_buf++;
|
|
|
|
for (col = 0; col < num_cols; col++) {
|
|
|
|
/* We can dispense with GETJSAMPLE() here */
|
|
|
|
outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
|
|
|
|
outptr += RGB_PIXELSIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1991-10-06 20:00:00 -04:00
|
|
|
/*
|
1994-09-23 20:00:00 -04:00
|
|
|
* Adobe-style YCCK->CMYK conversion.
|
|
|
|
* We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
|
|
|
|
* conversion as above, while passing K (black) unchanged.
|
1995-08-01 20:00:00 -04:00
|
|
|
* We assume build_ycc_rgb_table has been called.
|
1991-10-06 20:00:00 -04:00
|
|
|
*/
|
|
|
|
|
1996-02-06 19:00:00 -05:00
|
|
|
METHODDEF(void)
|
1994-09-23 20:00:00 -04:00
|
|
|
ycck_cmyk_convert (j_decompress_ptr cinfo,
|
|
|
|
JSAMPIMAGE input_buf, JDIMENSION input_row,
|
|
|
|
JSAMPARRAY output_buf, int num_rows)
|
1991-10-06 20:00:00 -04:00
|
|
|
{
|
1994-09-23 20:00:00 -04:00
|
|
|
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]);
|
1994-12-06 19:00:00 -05:00
|
|
|
/* Range-limiting is essential due to noise introduced by DCT losses. */
|
1994-09-23 20:00:00 -04:00
|
|
|
outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
|
|
|
|
outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
|
|
|
|
((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
|
|
|
|
SCALEBITS)))];
|
|
|
|
outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
|
|
|
|
/* K passes through unchanged */
|
|
|
|
outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
|
|
|
|
outptr += 4;
|
|
|
|
}
|
|
|
|
}
|
1991-10-06 20:00:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1994-09-23 20:00:00 -04:00
|
|
|
* Empty method for start_pass.
|
1991-10-06 20:00:00 -04:00
|
|
|
*/
|
|
|
|
|
1996-02-06 19:00:00 -05:00
|
|
|
METHODDEF(void)
|
1995-08-01 20:00:00 -04:00
|
|
|
start_pass_dcolor (j_decompress_ptr cinfo)
|
1991-10-06 20:00:00 -04:00
|
|
|
{
|
|
|
|
/* no work needed */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1994-09-23 20:00:00 -04:00
|
|
|
* Module initialization routine for output colorspace conversion.
|
1991-10-06 20:00:00 -04:00
|
|
|
*/
|
|
|
|
|
1996-02-06 19:00:00 -05:00
|
|
|
GLOBAL(void)
|
1994-09-23 20:00:00 -04:00
|
|
|
jinit_color_deconverter (j_decompress_ptr cinfo)
|
1991-10-06 20:00:00 -04:00
|
|
|
{
|
1994-09-23 20:00:00 -04:00
|
|
|
my_cconvert_ptr cconvert;
|
1993-02-17 19:00:00 -05:00
|
|
|
int ci;
|
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
cconvert = (my_cconvert_ptr)
|
|
|
|
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
|
|
|
SIZEOF(my_color_deconverter));
|
2013-01-12 19:00:00 -05:00
|
|
|
cinfo->cconvert = &cconvert->pub;
|
1995-08-01 20:00:00 -04:00
|
|
|
cconvert->pub.start_pass = start_pass_dcolor;
|
1994-09-23 20:00:00 -04:00
|
|
|
|
1991-10-06 20:00:00 -04:00
|
|
|
/* Make sure num_components agrees with jpeg_color_space */
|
|
|
|
switch (cinfo->jpeg_color_space) {
|
1994-09-23 20:00:00 -04:00
|
|
|
case JCS_GRAYSCALE:
|
1991-10-06 20:00:00 -04:00
|
|
|
if (cinfo->num_components != 1)
|
1994-09-23 20:00:00 -04:00
|
|
|
ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
|
1991-10-06 20:00:00 -04:00
|
|
|
break;
|
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
case JCS_RGB:
|
|
|
|
case JCS_YCbCr:
|
1991-10-06 20:00:00 -04:00
|
|
|
if (cinfo->num_components != 3)
|
1994-09-23 20:00:00 -04:00
|
|
|
ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
|
1991-10-06 20:00:00 -04:00
|
|
|
break;
|
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
case JCS_CMYK:
|
|
|
|
case JCS_YCCK:
|
1991-10-06 20:00:00 -04:00
|
|
|
if (cinfo->num_components != 4)
|
1994-09-23 20:00:00 -04:00
|
|
|
ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
|
1991-10-06 20:00:00 -04:00
|
|
|
break;
|
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
default: /* JCS_UNKNOWN can be anything */
|
|
|
|
if (cinfo->num_components < 1)
|
|
|
|
ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
|
1991-10-06 20:00:00 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-01-12 19:00:00 -05:00
|
|
|
/* Support color transform only for RGB colorspace */
|
|
|
|
if (cinfo->color_transform && cinfo->jpeg_color_space != JCS_RGB)
|
|
|
|
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
|
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
/* Set out_color_components and conversion method based on requested space.
|
|
|
|
* Also clear the component_needed flags for any unused components,
|
|
|
|
* so that earlier pipeline stages can avoid useless computation.
|
|
|
|
*/
|
1993-02-17 19:00:00 -05:00
|
|
|
|
1991-10-06 20:00:00 -04:00
|
|
|
switch (cinfo->out_color_space) {
|
1994-09-23 20:00:00 -04:00
|
|
|
case JCS_GRAYSCALE:
|
|
|
|
cinfo->out_color_components = 1;
|
|
|
|
if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
|
|
|
|
cinfo->jpeg_color_space == JCS_YCbCr) {
|
|
|
|
cconvert->pub.color_convert = grayscale_convert;
|
1993-02-17 19:00:00 -05:00
|
|
|
/* For color->grayscale conversion, only the Y (0) component is needed */
|
|
|
|
for (ci = 1; ci < cinfo->num_components; ci++)
|
1994-09-23 20:00:00 -04:00
|
|
|
cinfo->comp_info[ci].component_needed = FALSE;
|
2012-01-14 19:00:00 -05:00
|
|
|
} else if (cinfo->jpeg_color_space == JCS_RGB) {
|
2013-01-12 19:00:00 -05:00
|
|
|
switch (cinfo->color_transform) {
|
|
|
|
case JCT_NONE:
|
|
|
|
cconvert->pub.color_convert = rgb_gray_convert;
|
|
|
|
break;
|
|
|
|
case JCT_SUBTRACT_GREEN:
|
|
|
|
cconvert->pub.color_convert = rgb1_gray_convert;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
|
|
|
|
break;
|
|
|
|
}
|
2012-01-14 19:00:00 -05:00
|
|
|
build_rgb_y_table(cinfo);
|
1994-09-23 20:00:00 -04:00
|
|
|
} else
|
|
|
|
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JCS_RGB:
|
|
|
|
cinfo->out_color_components = RGB_PIXELSIZE;
|
|
|
|
if (cinfo->jpeg_color_space == JCS_YCbCr) {
|
|
|
|
cconvert->pub.color_convert = ycc_rgb_convert;
|
1995-08-01 20:00:00 -04:00
|
|
|
build_ycc_rgb_table(cinfo);
|
1998-03-26 19:00:00 -05:00
|
|
|
} else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
|
|
|
|
cconvert->pub.color_convert = gray_rgb_convert;
|
2012-01-14 19:00:00 -05:00
|
|
|
} else if (cinfo->jpeg_color_space == JCS_RGB) {
|
2013-01-12 19:00:00 -05:00
|
|
|
switch (cinfo->color_transform) {
|
|
|
|
case JCT_NONE:
|
|
|
|
cconvert->pub.color_convert = rgb_convert;
|
|
|
|
break;
|
|
|
|
case JCT_SUBTRACT_GREEN:
|
|
|
|
cconvert->pub.color_convert = rgb1_rgb_convert;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
|
|
|
|
break;
|
|
|
|
}
|
1992-03-16 19:00:00 -05:00
|
|
|
} else
|
1994-09-23 20:00:00 -04:00
|
|
|
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
|
1991-10-06 20:00:00 -04:00
|
|
|
break;
|
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
case JCS_CMYK:
|
|
|
|
cinfo->out_color_components = 4;
|
|
|
|
if (cinfo->jpeg_color_space == JCS_YCCK) {
|
|
|
|
cconvert->pub.color_convert = ycck_cmyk_convert;
|
1995-08-01 20:00:00 -04:00
|
|
|
build_ycc_rgb_table(cinfo);
|
1994-09-23 20:00:00 -04:00
|
|
|
} else if (cinfo->jpeg_color_space == JCS_CMYK) {
|
|
|
|
cconvert->pub.color_convert = null_convert;
|
1992-03-16 19:00:00 -05:00
|
|
|
} else
|
1994-09-23 20:00:00 -04:00
|
|
|
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
|
1991-10-06 20:00:00 -04:00
|
|
|
break;
|
|
|
|
|
1992-03-16 19:00:00 -05:00
|
|
|
default:
|
1994-09-23 20:00:00 -04:00
|
|
|
/* Permit null conversion to same output space */
|
1992-03-16 19:00:00 -05:00
|
|
|
if (cinfo->out_color_space == cinfo->jpeg_color_space) {
|
1994-09-23 20:00:00 -04:00
|
|
|
cinfo->out_color_components = cinfo->num_components;
|
|
|
|
cconvert->pub.color_convert = null_convert;
|
1992-03-16 19:00:00 -05:00
|
|
|
} else /* unsupported non-null conversion */
|
1994-09-23 20:00:00 -04:00
|
|
|
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
|
1991-10-06 20:00:00 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cinfo->quantize_colors)
|
1994-09-23 20:00:00 -04:00
|
|
|
cinfo->output_components = 1; /* single colormapped output component */
|
1991-10-06 20:00:00 -04:00
|
|
|
else
|
1994-09-23 20:00:00 -04:00
|
|
|
cinfo->output_components = cinfo->out_color_components;
|
1991-10-06 20:00:00 -04:00
|
|
|
}
|