JDCOLOR.c
上传用户:qiutianh
上传日期:2022-08-08
资源大小:939k
文件大小:13k
源码类别:

图形图象

开发平台:

Visual C++

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