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