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