0N/A/*
0N/A * reserved comment block
0N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
0N/A * jdcolor.c
0N/A *
0N/A * Copyright (C) 1991-1997, Thomas G. Lane.
0N/A * This file is part of the Independent JPEG Group's software.
0N/A * For conditions of distribution and use, see the accompanying README file.
0N/A *
0N/A * This file contains output colorspace conversion routines.
0N/A */
0N/A
0N/A#define JPEG_INTERNALS
0N/A#include "jinclude.h"
0N/A#include "jpeglib.h"
0N/A
0N/A
0N/A/* Private subobject */
0N/A
0N/Atypedef struct {
0N/A struct jpeg_color_deconverter pub; /* public fields */
0N/A
0N/A /* Private state for YCC->RGB conversion */
0N/A int * Cr_r_tab; /* => table for Cr to R conversion */
0N/A int * Cb_b_tab; /* => table for Cb to B conversion */
0N/A INT32 * Cr_g_tab; /* => table for Cr to G conversion */
0N/A INT32 * Cb_g_tab; /* => table for Cb to G conversion */
0N/A} my_color_deconverter;
0N/A
0N/Atypedef my_color_deconverter * my_cconvert_ptr;
0N/A
0N/A
0N/A/**************** YCbCr -> RGB conversion: most common case **************/
0N/A
0N/A/*
0N/A * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
0N/A * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
0N/A * The conversion equations to be implemented are therefore
0N/A * R = Y + 1.40200 * Cr
0N/A * G = Y - 0.34414 * Cb - 0.71414 * Cr
0N/A * B = Y + 1.77200 * Cb
0N/A * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
0N/A * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
0N/A *
0N/A * To avoid floating-point arithmetic, we represent the fractional constants
0N/A * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
0N/A * the products by 2^16, with appropriate rounding, to get the correct answer.
0N/A * Notice that Y, being an integral input, does not contribute any fraction
0N/A * so it need not participate in the rounding.
0N/A *
0N/A * For even more speed, we avoid doing any multiplications in the inner loop
0N/A * by precalculating the constants times Cb and Cr for all possible values.
0N/A * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
0N/A * for 12-bit samples it is still acceptable. It's not very reasonable for
0N/A * 16-bit samples, but if you want lossless storage you shouldn't be changing
0N/A * colorspace anyway.
0N/A * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
0N/A * values for the G calculation are left scaled up, since we must add them
0N/A * together before rounding.
0N/A */
0N/A
0N/A#define SCALEBITS 16 /* speediest right-shift on some machines */
0N/A#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
0N/A#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
0N/A
0N/A
0N/A/*
0N/A * Initialize tables for YCC->RGB colorspace conversion.
0N/A */
0N/A
0N/ALOCAL(void)
0N/Abuild_ycc_rgb_table (j_decompress_ptr cinfo)
0N/A{
0N/A my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
0N/A int i;
0N/A INT32 x;
0N/A SHIFT_TEMPS
0N/A
0N/A cconvert->Cr_r_tab = (int *)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A (MAXJSAMPLE+1) * SIZEOF(int));
0N/A cconvert->Cb_b_tab = (int *)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A (MAXJSAMPLE+1) * SIZEOF(int));
0N/A cconvert->Cr_g_tab = (INT32 *)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A (MAXJSAMPLE+1) * SIZEOF(INT32));
0N/A cconvert->Cb_g_tab = (INT32 *)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A (MAXJSAMPLE+1) * SIZEOF(INT32));
0N/A
0N/A for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
0N/A /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
0N/A /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
0N/A /* Cr=>R value is nearest int to 1.40200 * x */
0N/A cconvert->Cr_r_tab[i] = (int)
0N/A RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
0N/A /* Cb=>B value is nearest int to 1.77200 * x */
0N/A cconvert->Cb_b_tab[i] = (int)
0N/A RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
0N/A /* Cr=>G value is scaled-up -0.71414 * x */
0N/A cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
0N/A /* Cb=>G value is scaled-up -0.34414 * x */
0N/A /* We also add in ONE_HALF so that need not do it in inner loop */
0N/A cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Convert some rows of samples to the output colorspace.
0N/A *
0N/A * Note that we change from noninterleaved, one-plane-per-component format
0N/A * to interleaved-pixel format. The output buffer is therefore three times
0N/A * as wide as the input buffer.
0N/A * A starting row offset is provided only for the input buffer. The caller
0N/A * can easily adjust the passed output_buf value to accommodate any row
0N/A * offset required on that side.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Aycc_rgb_convert (j_decompress_ptr cinfo,
0N/A JSAMPIMAGE input_buf, JDIMENSION input_row,
0N/A JSAMPARRAY output_buf, int num_rows)
0N/A{
0N/A my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
0N/A register int y, cb, cr;
0N/A register JSAMPROW outptr;
0N/A register JSAMPROW inptr0, inptr1, inptr2;
0N/A register JDIMENSION col;
0N/A JDIMENSION num_cols = cinfo->output_width;
0N/A /* copy these pointers into registers if possible */
0N/A register JSAMPLE * range_limit = cinfo->sample_range_limit;
0N/A register int * Crrtab = cconvert->Cr_r_tab;
0N/A register int * Cbbtab = cconvert->Cb_b_tab;
0N/A register INT32 * Crgtab = cconvert->Cr_g_tab;
0N/A register INT32 * Cbgtab = cconvert->Cb_g_tab;
0N/A SHIFT_TEMPS
0N/A
0N/A while (--num_rows >= 0) {
0N/A inptr0 = input_buf[0][input_row];
0N/A inptr1 = input_buf[1][input_row];
0N/A inptr2 = input_buf[2][input_row];
0N/A input_row++;
0N/A outptr = *output_buf++;
0N/A for (col = 0; col < num_cols; col++) {
0N/A y = GETJSAMPLE(inptr0[col]);
0N/A cb = GETJSAMPLE(inptr1[col]);
0N/A cr = GETJSAMPLE(inptr2[col]);
0N/A /* Range-limiting is essential due to noise introduced by DCT losses. */
0N/A outptr[RGB_RED] = range_limit[y + Crrtab[cr]];
0N/A outptr[RGB_GREEN] = range_limit[y +
0N/A ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
0N/A SCALEBITS))];
0N/A outptr[RGB_BLUE] = range_limit[y + Cbbtab[cb]];
0N/A outptr += RGB_PIXELSIZE;
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A/**************** Cases other than YCbCr -> RGB **************/
0N/A
0N/A
0N/A/*
0N/A * Color conversion for no colorspace change: just copy the data,
0N/A * converting from separate-planes to interleaved representation.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Anull_convert (j_decompress_ptr cinfo,
0N/A JSAMPIMAGE input_buf, JDIMENSION input_row,
0N/A JSAMPARRAY output_buf, int num_rows)
0N/A{
0N/A register JSAMPROW inptr, outptr;
0N/A register JDIMENSION count;
0N/A register int num_components = cinfo->num_components;
0N/A JDIMENSION num_cols = cinfo->output_width;
0N/A int ci;
0N/A
0N/A while (--num_rows >= 0) {
0N/A for (ci = 0; ci < num_components; ci++) {
0N/A inptr = input_buf[ci][input_row];
0N/A outptr = output_buf[0] + ci;
0N/A for (count = num_cols; count > 0; count--) {
0N/A *outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */
0N/A outptr += num_components;
0N/A }
0N/A }
0N/A input_row++;
0N/A output_buf++;
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Color conversion for grayscale: just copy the data.
0N/A * This also works for YCbCr -> grayscale conversion, in which
0N/A * we just copy the Y (luminance) component and ignore chrominance.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Agrayscale_convert (j_decompress_ptr cinfo,
0N/A JSAMPIMAGE input_buf, JDIMENSION input_row,
0N/A JSAMPARRAY output_buf, int num_rows)
0N/A{
0N/A jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
0N/A num_rows, cinfo->output_width);
0N/A}
0N/A
0N/A/*
0N/A * Convert grayscale to RGB: just duplicate the graylevel three times.
0N/A * This is provided to support applications that don't want to cope
0N/A * with grayscale as a separate case.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Agray_rgb_convert (j_decompress_ptr cinfo,
0N/A JSAMPIMAGE input_buf, JDIMENSION input_row,
0N/A JSAMPARRAY output_buf, int num_rows)
0N/A{
0N/A register JSAMPROW inptr, outptr;
0N/A register JDIMENSION col;
0N/A JDIMENSION num_cols = cinfo->output_width;
0N/A
0N/A while (--num_rows >= 0) {
0N/A inptr = input_buf[0][input_row++];
0N/A outptr = *output_buf++;
0N/A for (col = 0; col < num_cols; col++) {
0N/A /* We can dispense with GETJSAMPLE() here */
0N/A outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
0N/A outptr += RGB_PIXELSIZE;
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Adobe-style YCCK->CMYK conversion.
0N/A * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
0N/A * conversion as above, while passing K (black) unchanged.
0N/A * We assume build_ycc_rgb_table has been called.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Aycck_cmyk_convert (j_decompress_ptr cinfo,
0N/A JSAMPIMAGE input_buf, JDIMENSION input_row,
0N/A JSAMPARRAY output_buf, int num_rows)
0N/A{
0N/A my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
0N/A register int y, cb, cr;
0N/A register JSAMPROW outptr;
0N/A register JSAMPROW inptr0, inptr1, inptr2, inptr3;
0N/A register JDIMENSION col;
0N/A JDIMENSION num_cols = cinfo->output_width;
0N/A /* copy these pointers into registers if possible */
0N/A register JSAMPLE * range_limit = cinfo->sample_range_limit;
0N/A register int * Crrtab = cconvert->Cr_r_tab;
0N/A register int * Cbbtab = cconvert->Cb_b_tab;
0N/A register INT32 * Crgtab = cconvert->Cr_g_tab;
0N/A register INT32 * Cbgtab = cconvert->Cb_g_tab;
0N/A SHIFT_TEMPS
0N/A
0N/A while (--num_rows >= 0) {
0N/A inptr0 = input_buf[0][input_row];
0N/A inptr1 = input_buf[1][input_row];
0N/A inptr2 = input_buf[2][input_row];
0N/A inptr3 = input_buf[3][input_row];
0N/A input_row++;
0N/A outptr = *output_buf++;
0N/A for (col = 0; col < num_cols; col++) {
0N/A y = GETJSAMPLE(inptr0[col]);
0N/A cb = GETJSAMPLE(inptr1[col]);
0N/A cr = GETJSAMPLE(inptr2[col]);
0N/A /* Range-limiting is essential due to noise introduced by DCT losses. */
0N/A outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
0N/A outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
0N/A ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
0N/A SCALEBITS)))];
0N/A outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
0N/A /* K passes through unchanged */
0N/A outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
0N/A outptr += 4;
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Empty method for start_pass.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Astart_pass_dcolor (j_decompress_ptr cinfo)
0N/A{
0N/A /* no work needed */
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Module initialization routine for output colorspace conversion.
0N/A */
0N/A
0N/AGLOBAL(void)
0N/Ajinit_color_deconverter (j_decompress_ptr cinfo)
0N/A{
0N/A my_cconvert_ptr cconvert;
0N/A int ci;
0N/A
0N/A cconvert = (my_cconvert_ptr)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A SIZEOF(my_color_deconverter));
0N/A cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
0N/A cconvert->pub.start_pass = start_pass_dcolor;
0N/A
0N/A /* Make sure num_components agrees with jpeg_color_space */
0N/A switch (cinfo->jpeg_color_space) {
0N/A case JCS_GRAYSCALE:
0N/A if (cinfo->num_components != 1)
0N/A ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
0N/A break;
0N/A case JCS_RGB:
0N/A case JCS_YCbCr:
0N/A if (cinfo->num_components != 3)
0N/A ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
0N/A break;
0N/A
0N/A case JCS_CMYK:
0N/A case JCS_YCCK:
0N/A if (cinfo->num_components != 4)
0N/A ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
0N/A break;
0N/A
0N/A default: /* JCS_UNKNOWN can be anything */
0N/A if (cinfo->num_components < 1)
0N/A ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
0N/A break;
0N/A }
0N/A
0N/A /* Set out_color_components and conversion method based on requested space.
0N/A * Also clear the component_needed flags for any unused components,
0N/A * so that earlier pipeline stages can avoid useless computation.
0N/A */
0N/A
0N/A switch (cinfo->out_color_space) {
0N/A case JCS_GRAYSCALE:
0N/A cinfo->out_color_components = 1;
0N/A if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
0N/A cinfo->jpeg_color_space == JCS_YCbCr) {
0N/A cconvert->pub.color_convert = grayscale_convert;
0N/A /* For color->grayscale conversion, only the Y (0) component is needed */
0N/A for (ci = 1; ci < cinfo->num_components; ci++)
0N/A cinfo->comp_info[ci].component_needed = FALSE;
0N/A } else
0N/A ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
0N/A break;
0N/A
0N/A case JCS_RGB:
0N/A cinfo->out_color_components = RGB_PIXELSIZE;
0N/A if (cinfo->jpeg_color_space == JCS_YCbCr) {
0N/A cconvert->pub.color_convert = ycc_rgb_convert;
0N/A build_ycc_rgb_table(cinfo);
0N/A } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
0N/A cconvert->pub.color_convert = gray_rgb_convert;
0N/A } else if (cinfo->jpeg_color_space == JCS_RGB && RGB_PIXELSIZE == 3) {
0N/A cconvert->pub.color_convert = null_convert;
0N/A } else
0N/A ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
0N/A break;
0N/A
0N/A case JCS_CMYK:
0N/A cinfo->out_color_components = 4;
0N/A if (cinfo->jpeg_color_space == JCS_YCCK) {
0N/A cconvert->pub.color_convert = ycck_cmyk_convert;
0N/A build_ycc_rgb_table(cinfo);
0N/A } else if (cinfo->jpeg_color_space == JCS_CMYK) {
0N/A cconvert->pub.color_convert = null_convert;
0N/A } else
0N/A ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
0N/A break;
0N/A
0N/A default:
0N/A /* Permit null conversion to same output space */
0N/A if (cinfo->out_color_space == cinfo->jpeg_color_space) {
0N/A cinfo->out_color_components = cinfo->num_components;
0N/A cconvert->pub.color_convert = null_convert;
0N/A } else /* unsupported non-null conversion */
0N/A ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
0N/A break;
0N/A }
0N/A
0N/A if (cinfo->quantize_colors)
0N/A cinfo->output_components = 1; /* single colormapped output component */
0N/A else
0N/A cinfo->output_components = cinfo->out_color_components;
0N/A}