home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / jpegv5src.lha / jpeg-5 / jdcolor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-21  |  12.2 KB  |  375 lines

  1. /*
  2.  * jdcolor.c
  3.  *
  4.  * Copyright (C) 1991-1994, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains output colorspace conversion routines.
  9.  */
  10.  
  11. #define JPEG_INTERNALS
  12. #include "jinclude.h"
  13. #include "jpeglib.h"
  14.  
  15.  
  16. /* Private subobject */
  17.  
  18. typedef struct {
  19.   struct jpeg_color_deconverter pub; /* public fields */
  20.  
  21.   /* Private state for YCC->RGB conversion */
  22.   int * Cr_r_tab;        /* => table for Cr to R conversion */
  23.   int * Cb_b_tab;        /* => table for Cb to B conversion */
  24.   INT32 * Cr_g_tab;        /* => table for Cr to G conversion */
  25.   INT32 * Cb_g_tab;        /* => table for Cb to G conversion */
  26. } my_color_deconverter;
  27.  
  28. typedef my_color_deconverter * my_cconvert_ptr;
  29.  
  30.  
  31. /**************** YCbCr -> RGB conversion: most common case **************/
  32.  
  33. /*
  34.  * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
  35.  * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
  36.  * The conversion equations to be implemented are therefore
  37.  *    R = Y                + 1.40200 * Cr
  38.  *    G = Y - 0.34414 * Cb - 0.71414 * Cr
  39.  *    B = Y + 1.77200 * Cb
  40.  * where Cb and Cr represent the incoming values less MAXJSAMPLE/2.
  41.  * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
  42.  *
  43.  * To avoid floating-point arithmetic, we represent the fractional constants
  44.  * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
  45.  * the products by 2^16, with appropriate rounding, to get the correct answer.
  46.  * Notice that Y, being an integral input, does not contribute any fraction
  47.  * so it need not participate in the rounding.
  48.  *
  49.  * For even more speed, we avoid doing any multiplications in the inner loop
  50.  * by precalculating the constants times Cb and Cr for all possible values.
  51.  * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
  52.  * for 12-bit samples it is still acceptable.  It's not very reasonable for
  53.  * 16-bit samples, but if you want lossless storage you shouldn't be changing
  54.  * colorspace anyway.
  55.  * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
  56.  * values for the G calculation are left scaled up, since we must add them
  57.  * together before rounding.
  58.  */
  59.  
  60. #define SCALEBITS    16    /* speediest right-shift on some machines */
  61. #define ONE_HALF    ((INT32) 1 << (SCALEBITS-1))
  62. #define FIX(x)        ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
  63.  
  64.  
  65. /*
  66.  * Initialize for YCC->RGB colorspace conversion.
  67.  */
  68.  
  69. METHODDEF void
  70. ycc_rgb_start (j_decompress_ptr cinfo)
  71. {
  72.   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  73.   INT32 i, x2;
  74.   SHIFT_TEMPS
  75.  
  76.   cconvert->Cr_r_tab = (int *)
  77.     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  78.                 (MAXJSAMPLE+1) * SIZEOF(int));
  79.   cconvert->Cb_b_tab = (int *)
  80.     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  81.                 (MAXJSAMPLE+1) * SIZEOF(int));
  82.   cconvert->Cr_g_tab = (INT32 *)
  83.     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  84.                 (MAXJSAMPLE+1) * SIZEOF(INT32));
  85.   cconvert->Cb_g_tab = (INT32 *)
  86.     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  87.                 (MAXJSAMPLE+1) * SIZEOF(INT32));
  88.  
  89.   for (i = 0; i <= MAXJSAMPLE; i++) {
  90.     /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
  91.     /* The Cb or Cr value we are thinking of is x = i - MAXJSAMPLE/2 */
  92.     x2 = 2*i - MAXJSAMPLE;    /* twice x */
  93.     /* Cr=>R value is nearest int to 1.40200 * x */
  94.     cconvert->Cr_r_tab[i] = (int)
  95.             RIGHT_SHIFT(FIX(1.40200/2) * x2 + ONE_HALF, SCALEBITS);
  96.     /* Cb=>B value is nearest int to 1.77200 * x */
  97.     cconvert->Cb_b_tab[i] = (int)
  98.             RIGHT_SHIFT(FIX(1.77200/2) * x2 + ONE_HALF, SCALEBITS);
  99.     /* Cr=>G value is scaled-up -0.71414 * x */
  100.     cconvert->Cr_g_tab[i] = (- FIX(0.71414/2)) * x2;
  101.     /* Cb=>G value is scaled-up -0.34414 * x */
  102.     /* We also add in ONE_HALF so that need not do it in inner loop */
  103.     cconvert->Cb_g_tab[i] = (- FIX(0.34414/2)) * x2 + ONE_HALF;
  104.   }
  105. }
  106.  
  107.  
  108. /*
  109.  * Convert some rows of samples to the output colorspace.
  110.  *
  111.  * Note that we change from noninterleaved, one-plane-per-component format
  112.  * to interleaved-pixel format.  The output buffer is therefore three times
  113.  * as wide as the input buffer.
  114.  * A starting row offset is provided only for the input buffer.  The caller
  115.  * can easily adjust the passed output_buf value to accommodate any row
  116.  * offset required on that side.
  117.  */
  118.  
  119. METHODDEF void
  120. ycc_rgb_convert (j_decompress_ptr cinfo,
  121.          JSAMPIMAGE input_buf, JDIMENSION input_row,
  122.          JSAMPARRAY output_buf, int num_rows)
  123. {
  124.   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  125.   register int y, cb, cr;
  126.   register JSAMPROW outptr;
  127.   register JSAMPROW inptr0, inptr1, inptr2;
  128.   register JDIMENSION col;
  129.   JDIMENSION num_cols = cinfo->output_width;
  130.   /* copy these pointers into registers if possible */
  131.   register JSAMPLE * range_limit = cinfo->sample_range_limit;
  132.   register int * Crrtab = cconvert->Cr_r_tab;
  133.   register int * Cbbtab = cconvert->Cb_b_tab;
  134.   register INT32 * Crgtab = cconvert->Cr_g_tab;
  135.   register INT32 * Cbgtab = cconvert->Cb_g_tab;
  136.   SHIFT_TEMPS
  137.  
  138.   while (--num_rows >= 0) {
  139.     inptr0 = input_buf[0][input_row];
  140.     inptr1 = input_buf[1][input_row];
  141.     inptr2 = input_buf[2][input_row];
  142.     input_row++;
  143.     outptr = *output_buf++;
  144.     for (col = 0; col < num_cols; col++) {
  145.       y  = GETJSAMPLE(inptr0[col]);
  146.       cb = GETJSAMPLE(inptr1[col]);
  147.       cr = GETJSAMPLE(inptr2[col]);
  148.       /* Note: if the inputs were computed directly from RGB values,
  149.        * range-limiting would be unnecessary here; but due to possible
  150.        * noise in the DCT/IDCT phase, we do need to apply range limits.
  151.        */
  152.       outptr[RGB_RED] =   range_limit[y + Crrtab[cr]];
  153.       outptr[RGB_GREEN] = range_limit[y +
  154.                   ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
  155.                          SCALEBITS))];
  156.       outptr[RGB_BLUE] =  range_limit[y + Cbbtab[cb]];
  157.       outptr += RGB_PIXELSIZE;
  158.     }
  159.   }
  160. }
  161.  
  162.  
  163. /**************** Cases other than YCbCr -> RGB **************/
  164.  
  165.  
  166. /*
  167.  * Color conversion for no colorspace change: just copy the data,
  168.  * converting from separate-planes to interleaved representation.
  169.  */
  170.  
  171. METHODDEF void
  172. null_convert (j_decompress_ptr cinfo,
  173.           JSAMPIMAGE input_buf, JDIMENSION input_row,
  174.           JSAMPARRAY output_buf, int num_rows)
  175. {
  176.   register JSAMPROW inptr, outptr;
  177.   register JDIMENSION count;
  178.   register int num_components = cinfo->output_components;
  179.   JDIMENSION num_cols = cinfo->output_width;
  180.   int ci;
  181.  
  182.   while (--num_rows >= 0) {
  183.     for (ci = 0; ci < num_components; ci++) {
  184.       inptr = input_buf[ci][input_row];
  185.       outptr = output_buf[0] + ci;
  186.       for (count = num_cols; count > 0; count--) {
  187.     *outptr = *inptr++;    /* needn't bother with GETJSAMPLE() here */
  188.     outptr += num_components;
  189.       }
  190.     }
  191.     input_row++;
  192.     output_buf++;
  193.   }
  194. }
  195.  
  196.  
  197. /*
  198.  * Color conversion for grayscale: just copy the data.
  199.  * This also works for YCbCr -> grayscale conversion, in which
  200.  * we just copy the Y (luminance) component and ignore chrominance.
  201.  */
  202.  
  203. METHODDEF void
  204. grayscale_convert (j_decompress_ptr cinfo,
  205.            JSAMPIMAGE input_buf, JDIMENSION input_row,
  206.            JSAMPARRAY output_buf, int num_rows)
  207. {
  208.   jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
  209.             num_rows, cinfo->output_width);
  210. }
  211.  
  212.  
  213. /*
  214.  * Adobe-style YCCK->CMYK conversion.
  215.  * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
  216.  * conversion as above, while passing K (black) unchanged.
  217.  * We assume ycc_rgb_start has been called.
  218.  */
  219.  
  220. METHODDEF void
  221. ycck_cmyk_convert (j_decompress_ptr cinfo,
  222.            JSAMPIMAGE input_buf, JDIMENSION input_row,
  223.            JSAMPARRAY output_buf, int num_rows)
  224. {
  225.   my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  226.   register int y, cb, cr;
  227.   register JSAMPROW outptr;
  228.   register JSAMPROW inptr0, inptr1, inptr2, inptr3;
  229.   register JDIMENSION col;
  230.   JDIMENSION num_cols = cinfo->output_width;
  231.   /* copy these pointers into registers if possible */
  232.   register JSAMPLE * range_limit = cinfo->sample_range_limit;
  233.   register int * Crrtab = cconvert->Cr_r_tab;
  234.   register int * Cbbtab = cconvert->Cb_b_tab;
  235.   register INT32 * Crgtab = cconvert->Cr_g_tab;
  236.   register INT32 * Cbgtab = cconvert->Cb_g_tab;
  237.   SHIFT_TEMPS
  238.  
  239.   while (--num_rows >= 0) {
  240.     inptr0 = input_buf[0][input_row];
  241.     inptr1 = input_buf[1][input_row];
  242.     inptr2 = input_buf[2][input_row];
  243.     inptr3 = input_buf[3][input_row];
  244.     input_row++;
  245.     outptr = *output_buf++;
  246.     for (col = 0; col < num_cols; col++) {
  247.       y  = GETJSAMPLE(inptr0[col]);
  248.       cb = GETJSAMPLE(inptr1[col]);
  249.       cr = GETJSAMPLE(inptr2[col]);
  250.       /* Note: if the inputs were computed directly from RGB values,
  251.        * range-limiting would be unnecessary here; but due to possible
  252.        * noise in the DCT/IDCT phase, we do need to apply range limits.
  253.        */
  254.       outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])];    /* red */
  255.       outptr[1] = range_limit[MAXJSAMPLE - (y +            /* green */
  256.                   ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
  257.                          SCALEBITS)))];
  258.       outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])];    /* blue */
  259.       /* K passes through unchanged */
  260.       outptr[3] = inptr3[col];    /* don't need GETJSAMPLE here */
  261.       outptr += 4;
  262.     }
  263.   }
  264. }
  265.  
  266.  
  267. /*
  268.  * Empty method for start_pass.
  269.  */
  270.  
  271. METHODDEF void
  272. null_method (j_decompress_ptr cinfo)
  273. {
  274.   /* no work needed */
  275. }
  276.  
  277.  
  278. /*
  279.  * Module initialization routine for output colorspace conversion.
  280.  */
  281.  
  282. GLOBAL void
  283. jinit_color_deconverter (j_decompress_ptr cinfo)
  284. {
  285.   my_cconvert_ptr cconvert;
  286.   int ci;
  287.  
  288.   cconvert = (my_cconvert_ptr)
  289.     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  290.                 SIZEOF(my_color_deconverter));
  291.   cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
  292.   /* set start_pass to null method until we find out differently */
  293.   cconvert->pub.start_pass = null_method;
  294.  
  295.   /* Make sure num_components agrees with jpeg_color_space */
  296.   switch (cinfo->jpeg_color_space) {
  297.   case JCS_GRAYSCALE:
  298.     if (cinfo->num_components != 1)
  299.       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  300.     break;
  301.  
  302.   case JCS_RGB:
  303.   case JCS_YCbCr:
  304.     if (cinfo->num_components != 3)
  305.       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  306.     break;
  307.  
  308.   case JCS_CMYK:
  309.   case JCS_YCCK:
  310.     if (cinfo->num_components != 4)
  311.       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  312.     break;
  313.  
  314.   default:            /* JCS_UNKNOWN can be anything */
  315.     if (cinfo->num_components < 1)
  316.       ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  317.     break;
  318.   }
  319.  
  320.   /* Set out_color_components and conversion method based on requested space.
  321.    * Also clear the component_needed flags for any unused components,
  322.    * so that earlier pipeline stages can avoid useless computation.
  323.    */
  324.  
  325.   switch (cinfo->out_color_space) {
  326.   case JCS_GRAYSCALE:
  327.     cinfo->out_color_components = 1;
  328.     if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
  329.     cinfo->jpeg_color_space == JCS_YCbCr) {
  330.       cconvert->pub.color_convert = grayscale_convert;
  331.       /* For color->grayscale conversion, only the Y (0) component is needed */
  332.       for (ci = 1; ci < cinfo->num_components; ci++)
  333.     cinfo->comp_info[ci].component_needed = FALSE;
  334.     } else
  335.       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  336.     break;
  337.  
  338.   case JCS_RGB:
  339.     cinfo->out_color_components = RGB_PIXELSIZE;
  340.     if (cinfo->jpeg_color_space == JCS_YCbCr) {
  341.       cconvert->pub.start_pass = ycc_rgb_start;
  342.       cconvert->pub.color_convert = ycc_rgb_convert;
  343.     } else if (cinfo->jpeg_color_space == JCS_RGB && RGB_PIXELSIZE == 3) {
  344.       cconvert->pub.color_convert = null_convert;
  345.     } else
  346.       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  347.     break;
  348.  
  349.   case JCS_CMYK:
  350.     cinfo->out_color_components = 4;
  351.     if (cinfo->jpeg_color_space == JCS_YCCK) {
  352.       cconvert->pub.start_pass = ycc_rgb_start;
  353.       cconvert->pub.color_convert = ycck_cmyk_convert;
  354.     } else if (cinfo->jpeg_color_space == JCS_CMYK) {
  355.       cconvert->pub.color_convert = null_convert;
  356.     } else
  357.       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  358.     break;
  359.  
  360.   default:
  361.     /* Permit null conversion to same output space */
  362.     if (cinfo->out_color_space == cinfo->jpeg_color_space) {
  363.       cinfo->out_color_components = cinfo->num_components;
  364.       cconvert->pub.color_convert = null_convert;
  365.     } else            /* unsupported non-null conversion */
  366.       ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  367.     break;
  368.   }
  369.  
  370.   if (cinfo->quantize_colors)
  371.     cinfo->output_components = 1; /* single colormapped output component */
  372.   else
  373.     cinfo->output_components = cinfo->out_color_components;
  374. }
  375.