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