jdcolor.cpp 12 KB

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