jdcolor.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * jdcolor.c
  3. *
  4. * Copyright (C) 1991-1997, 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. * Convert grayscale to RGB: just duplicate the graylevel three times.
  182. * This is provided to support applications that don't want to cope
  183. * with grayscale as a separate case.
  184. */
  185. METHODDEF(void)
  186. gray_rgb_convert (j_decompress_ptr cinfo,
  187. JSAMPIMAGE input_buf, JDIMENSION input_row,
  188. JSAMPARRAY output_buf, int num_rows)
  189. {
  190. register JSAMPROW inptr, outptr;
  191. register JDIMENSION col;
  192. JDIMENSION num_cols = cinfo->output_width;
  193. while (--num_rows >= 0) {
  194. inptr = input_buf[0][input_row++];
  195. outptr = *output_buf++;
  196. for (col = 0; col < num_cols; col++) {
  197. /* We can dispense with GETJSAMPLE() here */
  198. outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
  199. outptr += RGB_PIXELSIZE;
  200. }
  201. }
  202. }
  203. /*
  204. * Adobe-style YCCK->CMYK conversion.
  205. * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
  206. * conversion as above, while passing K (black) unchanged.
  207. * We assume build_ycc_rgb_table has been called.
  208. */
  209. METHODDEF(void)
  210. ycck_cmyk_convert (j_decompress_ptr cinfo,
  211. JSAMPIMAGE input_buf, JDIMENSION input_row,
  212. JSAMPARRAY output_buf, int num_rows)
  213. {
  214. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  215. register int y, cb, cr;
  216. register JSAMPROW outptr;
  217. register JSAMPROW inptr0, inptr1, inptr2, inptr3;
  218. register JDIMENSION col;
  219. JDIMENSION num_cols = cinfo->output_width;
  220. /* copy these pointers into registers if possible */
  221. register JSAMPLE * range_limit = cinfo->sample_range_limit;
  222. register int * Crrtab = cconvert->Cr_r_tab;
  223. register int * Cbbtab = cconvert->Cb_b_tab;
  224. register INT32 * Crgtab = cconvert->Cr_g_tab;
  225. register INT32 * Cbgtab = cconvert->Cb_g_tab;
  226. SHIFT_TEMPS
  227. while (--num_rows >= 0) {
  228. inptr0 = input_buf[0][input_row];
  229. inptr1 = input_buf[1][input_row];
  230. inptr2 = input_buf[2][input_row];
  231. inptr3 = input_buf[3][input_row];
  232. input_row++;
  233. outptr = *output_buf++;
  234. for (col = 0; col < num_cols; col++) {
  235. y = GETJSAMPLE(inptr0[col]);
  236. cb = GETJSAMPLE(inptr1[col]);
  237. cr = GETJSAMPLE(inptr2[col]);
  238. /* Range-limiting is essential due to noise introduced by DCT losses. */
  239. outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
  240. outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
  241. ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
  242. SCALEBITS)))];
  243. outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
  244. /* K passes through unchanged */
  245. outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
  246. outptr += 4;
  247. }
  248. }
  249. }
  250. /*
  251. * Empty method for start_pass.
  252. */
  253. METHODDEF(void)
  254. start_pass_dcolor (j_decompress_ptr cinfo)
  255. {
  256. /* no work needed */
  257. }
  258. /*
  259. * Module initialization routine for output colorspace conversion.
  260. */
  261. GLOBAL(void)
  262. jinit_color_deconverter (j_decompress_ptr cinfo)
  263. {
  264. my_cconvert_ptr cconvert;
  265. int ci;
  266. cconvert = (my_cconvert_ptr)
  267. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  268. SIZEOF(my_color_deconverter));
  269. cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
  270. cconvert->pub.start_pass = start_pass_dcolor;
  271. /* Make sure num_components agrees with jpeg_color_space */
  272. switch (cinfo->jpeg_color_space) {
  273. case JCS_GRAYSCALE:
  274. if (cinfo->num_components != 1)
  275. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  276. break;
  277. case JCS_RGB:
  278. case JCS_YCbCr:
  279. if (cinfo->num_components != 3)
  280. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  281. break;
  282. case JCS_CMYK:
  283. case JCS_YCCK:
  284. if (cinfo->num_components != 4)
  285. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  286. break;
  287. default: /* JCS_UNKNOWN can be anything */
  288. if (cinfo->num_components < 1)
  289. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  290. break;
  291. }
  292. /* Set out_color_components and conversion method based on requested space.
  293. * Also clear the component_needed flags for any unused components,
  294. * so that earlier pipeline stages can avoid useless computation.
  295. */
  296. switch (cinfo->out_color_space) {
  297. case JCS_GRAYSCALE:
  298. cinfo->out_color_components = 1;
  299. if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
  300. cinfo->jpeg_color_space == JCS_YCbCr) {
  301. cconvert->pub.color_convert = grayscale_convert;
  302. /* For color->grayscale conversion, only the Y (0) component is needed */
  303. for (ci = 1; ci < cinfo->num_components; ci++)
  304. cinfo->comp_info[ci].component_needed = FALSE;
  305. } else
  306. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  307. break;
  308. case JCS_RGB:
  309. cinfo->out_color_components = RGB_PIXELSIZE;
  310. if (cinfo->jpeg_color_space == JCS_YCbCr) {
  311. cconvert->pub.color_convert = ycc_rgb_convert;
  312. build_ycc_rgb_table(cinfo);
  313. } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
  314. cconvert->pub.color_convert = gray_rgb_convert;
  315. } else if (cinfo->jpeg_color_space == JCS_RGB && RGB_PIXELSIZE == 3) {
  316. cconvert->pub.color_convert = null_convert;
  317. } else
  318. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  319. break;
  320. case JCS_CMYK:
  321. cinfo->out_color_components = 4;
  322. if (cinfo->jpeg_color_space == JCS_YCCK) {
  323. cconvert->pub.color_convert = ycck_cmyk_convert;
  324. build_ycc_rgb_table(cinfo);
  325. } else if (cinfo->jpeg_color_space == JCS_CMYK) {
  326. cconvert->pub.color_convert = null_convert;
  327. } else
  328. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  329. break;
  330. default:
  331. /* Permit null conversion to same output space */
  332. if (cinfo->out_color_space == cinfo->jpeg_color_space) {
  333. cinfo->out_color_components = cinfo->num_components;
  334. cconvert->pub.color_convert = null_convert;
  335. } else /* unsupported non-null conversion */
  336. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  337. break;
  338. }
  339. if (cinfo->quantize_colors)
  340. cinfo->output_components = 1; /* single colormapped output component */
  341. else
  342. cinfo->output_components = cinfo->out_color_components;
  343. }