nuclear@2: /* nuclear@2: * jccolor.c nuclear@2: * nuclear@2: * Copyright (C) 1991-1996, 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 input 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_converter pub; /* public fields */ nuclear@2: nuclear@2: /* Private state for RGB->YCC conversion */ nuclear@2: INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */ nuclear@2: } my_color_converter; nuclear@2: nuclear@2: typedef my_color_converter * my_cconvert_ptr; nuclear@2: nuclear@2: nuclear@2: /**************** RGB -> YCbCr 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: * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B nuclear@2: * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE nuclear@2: * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE nuclear@2: * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.) nuclear@2: * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2, nuclear@2: * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and nuclear@2: * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0) nuclear@2: * were not represented exactly. Now we sacrifice exact representation of nuclear@2: * maximum red and maximum blue in order to get exact grayscales. 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: * nuclear@2: * For even more speed, we avoid doing any multiplications in the inner loop nuclear@2: * by precalculating the constants times R,G,B 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 CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included nuclear@2: * in the tables to save adding them separately in the inner loop. nuclear@2: */ nuclear@2: nuclear@2: #define SCALEBITS 16 /* speediest right-shift on some machines */ nuclear@2: #define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS) nuclear@2: #define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) nuclear@2: #define FIX(x) ((INT32) ((x) * (1L< Y section */ nuclear@2: #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */ nuclear@2: #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */ nuclear@2: #define R_CB_OFF (3*(MAXJSAMPLE+1)) nuclear@2: #define G_CB_OFF (4*(MAXJSAMPLE+1)) nuclear@2: #define B_CB_OFF (5*(MAXJSAMPLE+1)) nuclear@2: #define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */ nuclear@2: #define G_CR_OFF (6*(MAXJSAMPLE+1)) nuclear@2: #define B_CR_OFF (7*(MAXJSAMPLE+1)) nuclear@2: #define TABLE_SIZE (8*(MAXJSAMPLE+1)) nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Initialize for RGB->YCC colorspace conversion. nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: rgb_ycc_start (j_compress_ptr cinfo) nuclear@2: { nuclear@2: my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; nuclear@2: INT32 * rgb_ycc_tab; nuclear@2: INT32 i; nuclear@2: nuclear@2: /* Allocate and fill in the conversion tables. */ nuclear@2: cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *) nuclear@2: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, nuclear@2: (TABLE_SIZE * SIZEOF(INT32))); nuclear@2: nuclear@2: for (i = 0; i <= MAXJSAMPLE; i++) { nuclear@2: rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i; nuclear@2: rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i; nuclear@2: rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF; nuclear@2: rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i; nuclear@2: rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i; nuclear@2: /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr. nuclear@2: * This ensures that the maximum output will round to MAXJSAMPLE nuclear@2: * not MAXJSAMPLE+1, and thus that we don't have to range-limit. nuclear@2: */ nuclear@2: rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1; nuclear@2: /* B=>Cb and R=>Cr tables are the same nuclear@2: rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1; nuclear@2: */ nuclear@2: rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i; nuclear@2: rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i; nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Convert some rows of samples to the JPEG colorspace. nuclear@2: * nuclear@2: * Note that we change from the application's interleaved-pixel format nuclear@2: * to our internal noninterleaved, one-plane-per-component format. nuclear@2: * The input buffer is therefore three times as wide as the output buffer. nuclear@2: * nuclear@2: * A starting row offset is provided only for the output buffer. The caller nuclear@2: * can easily adjust the passed input_buf value to accommodate any row nuclear@2: * offset required on that side. nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: rgb_ycc_convert (j_compress_ptr cinfo, nuclear@2: JSAMPARRAY input_buf, JSAMPIMAGE output_buf, nuclear@2: JDIMENSION output_row, int num_rows) nuclear@2: { nuclear@2: my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; nuclear@2: register int r, g, b; nuclear@2: register INT32 * ctab = cconvert->rgb_ycc_tab; nuclear@2: register JSAMPROW inptr; nuclear@2: register JSAMPROW outptr0, outptr1, outptr2; nuclear@2: register JDIMENSION col; nuclear@2: JDIMENSION num_cols = cinfo->image_width; nuclear@2: nuclear@2: while (--num_rows >= 0) { nuclear@2: inptr = *input_buf++; nuclear@2: outptr0 = output_buf[0][output_row]; nuclear@2: outptr1 = output_buf[1][output_row]; nuclear@2: outptr2 = output_buf[2][output_row]; nuclear@2: output_row++; nuclear@2: for (col = 0; col < num_cols; col++) { nuclear@2: r = GETJSAMPLE(inptr[RGB_RED]); nuclear@2: g = GETJSAMPLE(inptr[RGB_GREEN]); nuclear@2: b = GETJSAMPLE(inptr[RGB_BLUE]); nuclear@2: inptr += RGB_PIXELSIZE; nuclear@2: /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations nuclear@2: * must be too; we do not need an explicit range-limiting operation. nuclear@2: * Hence the value being shifted is never negative, and we don't nuclear@2: * need the general RIGHT_SHIFT macro. nuclear@2: */ nuclear@2: /* Y */ nuclear@2: outptr0[col] = (JSAMPLE) nuclear@2: ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) nuclear@2: >> SCALEBITS); nuclear@2: /* Cb */ nuclear@2: outptr1[col] = (JSAMPLE) nuclear@2: ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF]) nuclear@2: >> SCALEBITS); nuclear@2: /* Cr */ nuclear@2: outptr2[col] = (JSAMPLE) nuclear@2: ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) nuclear@2: >> SCALEBITS); nuclear@2: } nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /**************** Cases other than RGB -> YCbCr **************/ nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Convert some rows of samples to the JPEG colorspace. nuclear@2: * This version handles RGB->grayscale conversion, which is the same nuclear@2: * as the RGB->Y portion of RGB->YCbCr. nuclear@2: * We assume rgb_ycc_start has been called (we only use the Y tables). nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: rgb_gray_convert (j_compress_ptr cinfo, nuclear@2: JSAMPARRAY input_buf, JSAMPIMAGE output_buf, nuclear@2: JDIMENSION output_row, int num_rows) nuclear@2: { nuclear@2: my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; nuclear@2: register int r, g, b; nuclear@2: register INT32 * ctab = cconvert->rgb_ycc_tab; nuclear@2: register JSAMPROW inptr; nuclear@2: register JSAMPROW outptr; nuclear@2: register JDIMENSION col; nuclear@2: JDIMENSION num_cols = cinfo->image_width; nuclear@2: nuclear@2: while (--num_rows >= 0) { nuclear@2: inptr = *input_buf++; nuclear@2: outptr = output_buf[0][output_row]; nuclear@2: output_row++; nuclear@2: for (col = 0; col < num_cols; col++) { nuclear@2: r = GETJSAMPLE(inptr[RGB_RED]); nuclear@2: g = GETJSAMPLE(inptr[RGB_GREEN]); nuclear@2: b = GETJSAMPLE(inptr[RGB_BLUE]); nuclear@2: inptr += RGB_PIXELSIZE; nuclear@2: /* Y */ nuclear@2: outptr[col] = (JSAMPLE) nuclear@2: ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) nuclear@2: >> SCALEBITS); nuclear@2: } nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Convert some rows of samples to the JPEG colorspace. nuclear@2: * This version handles Adobe-style CMYK->YCCK conversion, nuclear@2: * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same nuclear@2: * conversion as above, while passing K (black) unchanged. nuclear@2: * We assume rgb_ycc_start has been called. nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: cmyk_ycck_convert (j_compress_ptr cinfo, nuclear@2: JSAMPARRAY input_buf, JSAMPIMAGE output_buf, nuclear@2: JDIMENSION output_row, int num_rows) nuclear@2: { nuclear@2: my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; nuclear@2: register int r, g, b; nuclear@2: register INT32 * ctab = cconvert->rgb_ycc_tab; nuclear@2: register JSAMPROW inptr; nuclear@2: register JSAMPROW outptr0, outptr1, outptr2, outptr3; nuclear@2: register JDIMENSION col; nuclear@2: JDIMENSION num_cols = cinfo->image_width; nuclear@2: nuclear@2: while (--num_rows >= 0) { nuclear@2: inptr = *input_buf++; nuclear@2: outptr0 = output_buf[0][output_row]; nuclear@2: outptr1 = output_buf[1][output_row]; nuclear@2: outptr2 = output_buf[2][output_row]; nuclear@2: outptr3 = output_buf[3][output_row]; nuclear@2: output_row++; nuclear@2: for (col = 0; col < num_cols; col++) { nuclear@2: r = MAXJSAMPLE - GETJSAMPLE(inptr[0]); nuclear@2: g = MAXJSAMPLE - GETJSAMPLE(inptr[1]); nuclear@2: b = MAXJSAMPLE - GETJSAMPLE(inptr[2]); nuclear@2: /* K passes through as-is */ nuclear@2: outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */ nuclear@2: inptr += 4; nuclear@2: /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations nuclear@2: * must be too; we do not need an explicit range-limiting operation. nuclear@2: * Hence the value being shifted is never negative, and we don't nuclear@2: * need the general RIGHT_SHIFT macro. nuclear@2: */ nuclear@2: /* Y */ nuclear@2: outptr0[col] = (JSAMPLE) nuclear@2: ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) nuclear@2: >> SCALEBITS); nuclear@2: /* Cb */ nuclear@2: outptr1[col] = (JSAMPLE) nuclear@2: ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF]) nuclear@2: >> SCALEBITS); nuclear@2: /* Cr */ nuclear@2: outptr2[col] = (JSAMPLE) nuclear@2: ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) nuclear@2: >> SCALEBITS); nuclear@2: } nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Convert some rows of samples to the JPEG colorspace. nuclear@2: * This version handles grayscale output with no conversion. nuclear@2: * The source can be either plain grayscale or YCbCr (since Y == gray). nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: grayscale_convert (j_compress_ptr cinfo, nuclear@2: JSAMPARRAY input_buf, JSAMPIMAGE output_buf, nuclear@2: JDIMENSION output_row, int num_rows) nuclear@2: { nuclear@2: register JSAMPROW inptr; nuclear@2: register JSAMPROW outptr; nuclear@2: register JDIMENSION col; nuclear@2: JDIMENSION num_cols = cinfo->image_width; nuclear@2: int instride = cinfo->input_components; nuclear@2: nuclear@2: while (--num_rows >= 0) { nuclear@2: inptr = *input_buf++; nuclear@2: outptr = output_buf[0][output_row]; nuclear@2: output_row++; nuclear@2: for (col = 0; col < num_cols; col++) { nuclear@2: outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */ nuclear@2: inptr += instride; nuclear@2: } nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Convert some rows of samples to the JPEG colorspace. nuclear@2: * This version handles multi-component colorspaces without conversion. nuclear@2: * We assume input_components == num_components. nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: null_convert (j_compress_ptr cinfo, nuclear@2: JSAMPARRAY input_buf, JSAMPIMAGE output_buf, nuclear@2: JDIMENSION output_row, int num_rows) nuclear@2: { nuclear@2: register JSAMPROW inptr; nuclear@2: register JSAMPROW outptr; nuclear@2: register JDIMENSION col; nuclear@2: register int ci; nuclear@2: int nc = cinfo->num_components; nuclear@2: JDIMENSION num_cols = cinfo->image_width; nuclear@2: nuclear@2: while (--num_rows >= 0) { nuclear@2: /* It seems fastest to make a separate pass for each component. */ nuclear@2: for (ci = 0; ci < nc; ci++) { nuclear@2: inptr = *input_buf; nuclear@2: outptr = output_buf[ci][output_row]; nuclear@2: for (col = 0; col < num_cols; col++) { nuclear@2: outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */ nuclear@2: inptr += nc; nuclear@2: } nuclear@2: } nuclear@2: input_buf++; nuclear@2: output_row++; 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: null_method (j_compress_ptr cinfo) nuclear@2: { nuclear@2: /* no work needed */ nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Module initialization routine for input colorspace conversion. nuclear@2: */ nuclear@2: nuclear@2: GLOBAL(void) nuclear@2: jinit_color_converter (j_compress_ptr cinfo) nuclear@2: { nuclear@2: my_cconvert_ptr cconvert; 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_converter)); nuclear@2: cinfo->cconvert = (struct jpeg_color_converter *) cconvert; nuclear@2: /* set start_pass to null method until we find out differently */ nuclear@2: cconvert->pub.start_pass = null_method; nuclear@2: nuclear@2: /* Make sure input_components agrees with in_color_space */ nuclear@2: switch (cinfo->in_color_space) { nuclear@2: case JCS_GRAYSCALE: nuclear@2: if (cinfo->input_components != 1) nuclear@2: ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); nuclear@2: break; nuclear@2: nuclear@2: case JCS_RGB: nuclear@2: #if RGB_PIXELSIZE != 3 nuclear@2: if (cinfo->input_components != RGB_PIXELSIZE) nuclear@2: ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); nuclear@2: break; nuclear@2: #endif /* else share code with YCbCr */ nuclear@2: nuclear@2: case JCS_YCbCr: nuclear@2: if (cinfo->input_components != 3) nuclear@2: ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); nuclear@2: break; nuclear@2: nuclear@2: case JCS_CMYK: nuclear@2: case JCS_YCCK: nuclear@2: if (cinfo->input_components != 4) nuclear@2: ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); nuclear@2: break; nuclear@2: nuclear@2: default: /* JCS_UNKNOWN can be anything */ nuclear@2: if (cinfo->input_components < 1) nuclear@2: ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); nuclear@2: break; nuclear@2: } nuclear@2: nuclear@2: /* Check num_components, set conversion method based on requested 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: if (cinfo->in_color_space == JCS_GRAYSCALE) nuclear@2: cconvert->pub.color_convert = grayscale_convert; nuclear@2: else if (cinfo->in_color_space == JCS_RGB) { nuclear@2: cconvert->pub.start_pass = rgb_ycc_start; nuclear@2: cconvert->pub.color_convert = rgb_gray_convert; nuclear@2: } else if (cinfo->in_color_space == JCS_YCbCr) nuclear@2: cconvert->pub.color_convert = grayscale_convert; nuclear@2: else nuclear@2: ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); nuclear@2: break; nuclear@2: nuclear@2: case JCS_RGB: nuclear@2: if (cinfo->num_components != 3) nuclear@2: ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); nuclear@2: if (cinfo->in_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_YCbCr: nuclear@2: if (cinfo->num_components != 3) nuclear@2: ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); nuclear@2: if (cinfo->in_color_space == JCS_RGB) { nuclear@2: cconvert->pub.start_pass = rgb_ycc_start; nuclear@2: cconvert->pub.color_convert = rgb_ycc_convert; nuclear@2: } else if (cinfo->in_color_space == JCS_YCbCr) 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: if (cinfo->num_components != 4) nuclear@2: ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); nuclear@2: if (cinfo->in_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: case JCS_YCCK: nuclear@2: if (cinfo->num_components != 4) nuclear@2: ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); nuclear@2: if (cinfo->in_color_space == JCS_CMYK) { nuclear@2: cconvert->pub.start_pass = rgb_ycc_start; nuclear@2: cconvert->pub.color_convert = cmyk_ycck_convert; nuclear@2: } else if (cinfo->in_color_space == JCS_YCCK) 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: /* allow null conversion of JCS_UNKNOWN */ nuclear@2: if (cinfo->jpeg_color_space != cinfo->in_color_space || nuclear@2: cinfo->num_components != cinfo->input_components) nuclear@2: ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); nuclear@2: cconvert->pub.color_convert = null_convert; nuclear@2: break; nuclear@2: } nuclear@2: }