jdcolor.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * jdcolor.c
  3. *
  4. * Copyright (C) 1991-1997, Thomas G. Lane.
  5. * Modified 2011-2023 by Guido Vollbeding.
  6. * This file is part of the Independent JPEG Group's software.
  7. * For conditions of distribution and use, see the accompanying README file.
  8. *
  9. * This file contains output colorspace conversion routines.
  10. */
  11. #define JPEG_INTERNALS
  12. #include "jinclude.h"
  13. #include "jpeglib.h"
  14. #if RANGE_BITS < 2
  15. /* Deliberate syntax err */
  16. Sorry, this code requires 2 or more range extension bits.
  17. #endif
  18. /* Private subobject */
  19. typedef struct {
  20. struct jpeg_color_deconverter pub; /* public fields */
  21. /* Private state for YCbCr->RGB and BG_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. /* Private state for RGB->Y conversion */
  27. INT32 * R_y_tab; /* => table for R to Y conversion */
  28. INT32 * G_y_tab; /* => table for G to Y conversion */
  29. INT32 * B_y_tab; /* => table for B to Y conversion */
  30. } my_color_deconverter;
  31. typedef my_color_deconverter * my_cconvert_ptr;
  32. /*************** YCbCr -> RGB conversion: most common case **************/
  33. /*************** BG_YCC -> RGB conversion: less common case **************/
  34. /*************** RGB -> Y conversion: less common case **************/
  35. /*
  36. * YCbCr is defined per Recommendation ITU-R BT.601-7 (03/2011),
  37. * previously known as Recommendation CCIR 601-1, except that Cb and Cr
  38. * are normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
  39. * sRGB (standard RGB color space) is defined per IEC 61966-2-1:1999.
  40. * sYCC (standard luma-chroma-chroma color space with extended gamut)
  41. * is defined per IEC 61966-2-1:1999 Amendment A1:2003 Annex F.
  42. * bg-sRGB and bg-sYCC (big gamut standard color spaces)
  43. * are defined per IEC 61966-2-1:1999 Amendment A1:2003 Annex G.
  44. * Note that the derived conversion coefficients given in some of these
  45. * documents are imprecise. The general conversion equations are
  46. *
  47. * R = Y + K * (1 - Kr) * Cr
  48. * G = Y - K * (Kb * (1 - Kb) * Cb + Kr * (1 - Kr) * Cr) / (1 - Kr - Kb)
  49. * B = Y + K * (1 - Kb) * Cb
  50. *
  51. * Y = Kr * R + (1 - Kr - Kb) * G + Kb * B
  52. *
  53. * With Kr = 0.299 and Kb = 0.114 (derived according to SMPTE RP 177-1993
  54. * from the 1953 FCC NTSC primaries and CIE Illuminant C), K = 2 for sYCC,
  55. * the conversion equations to be implemented are therefore
  56. *
  57. * R = Y + 1.402 * Cr
  58. * G = Y - 0.344136286 * Cb - 0.714136286 * Cr
  59. * B = Y + 1.772 * Cb
  60. *
  61. * Y = 0.299 * R + 0.587 * G + 0.114 * B
  62. *
  63. * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
  64. * For bg-sYCC, with K = 4, the equations are
  65. *
  66. * R = Y + 2.804 * Cr
  67. * G = Y - 0.688272572 * Cb - 1.428272572 * Cr
  68. * B = Y + 3.544 * Cb
  69. *
  70. * To avoid floating-point arithmetic, we represent the fractional constants
  71. * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
  72. * the products by 2^16, with appropriate rounding, to get the correct answer.
  73. * Notice that Y, being an integral input, does not contribute any fraction
  74. * so it need not participate in the rounding.
  75. *
  76. * For even more speed, we avoid doing any multiplications in the inner loop
  77. * by precalculating the constants times Cb and Cr for all possible values.
  78. * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
  79. * for 9-bit to 12-bit samples it is still acceptable. It's not very
  80. * reasonable for 16-bit samples, but if you want lossless storage
  81. * you shouldn't be changing colorspace anyway.
  82. * The Cr=>R and Cb=>B values can be rounded to integers in advance;
  83. * the values for the G calculation are left scaled up,
  84. * since we must add them together before rounding.
  85. */
  86. #define SCALEBITS 16 /* speediest right-shift on some machines */
  87. #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
  88. #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
  89. /*
  90. * Initialize tables for YCbCr->RGB and BG_YCC->RGB colorspace conversion.
  91. */
  92. LOCAL(void)
  93. build_ycc_rgb_table (j_decompress_ptr cinfo)
  94. /* Normal case, sYCC */
  95. {
  96. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  97. int i;
  98. INT32 x;
  99. SHIFT_TEMPS
  100. cconvert->Cr_r_tab = (int *) (*cinfo->mem->alloc_small)
  101. ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
  102. cconvert->Cb_b_tab = (int *) (*cinfo->mem->alloc_small)
  103. ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
  104. cconvert->Cr_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
  105. ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
  106. cconvert->Cb_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
  107. ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
  108. for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
  109. /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
  110. /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
  111. /* Cr=>R value is nearest int to 1.402 * x */
  112. cconvert->Cr_r_tab[i] = (int) DESCALE(FIX(1.402) * x, SCALEBITS);
  113. /* Cb=>B value is nearest int to 1.772 * x */
  114. cconvert->Cb_b_tab[i] = (int) DESCALE(FIX(1.772) * x, SCALEBITS);
  115. /* Cr=>G value is scaled-up -0.714136286 * x */
  116. cconvert->Cr_g_tab[i] = (- FIX(0.714136286)) * x;
  117. /* Cb=>G value is scaled-up -0.344136286 * x */
  118. /* We also add in ONE_HALF so that need not do it in inner loop */
  119. cconvert->Cb_g_tab[i] = (- FIX(0.344136286)) * x + ONE_HALF;
  120. }
  121. }
  122. LOCAL(void)
  123. build_bg_ycc_rgb_table (j_decompress_ptr cinfo)
  124. /* Wide gamut case, bg-sYCC */
  125. {
  126. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  127. int i;
  128. INT32 x;
  129. SHIFT_TEMPS
  130. cconvert->Cr_r_tab = (int *) (*cinfo->mem->alloc_small)
  131. ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
  132. cconvert->Cb_b_tab = (int *) (*cinfo->mem->alloc_small)
  133. ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
  134. cconvert->Cr_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
  135. ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
  136. cconvert->Cb_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
  137. ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
  138. for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
  139. /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
  140. /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
  141. /* Cr=>R value is nearest int to 2.804 * x */
  142. cconvert->Cr_r_tab[i] = (int) DESCALE(FIX(2.804) * x, SCALEBITS);
  143. /* Cb=>B value is nearest int to 3.544 * x */
  144. cconvert->Cb_b_tab[i] = (int) DESCALE(FIX(3.544) * x, SCALEBITS);
  145. /* Cr=>G value is scaled-up -1.428272572 * x */
  146. cconvert->Cr_g_tab[i] = (- FIX(1.428272572)) * x;
  147. /* Cb=>G value is scaled-up -0.688272572 * x */
  148. /* We also add in ONE_HALF so that need not do it in inner loop */
  149. cconvert->Cb_g_tab[i] = (- FIX(0.688272572)) * x + ONE_HALF;
  150. }
  151. }
  152. /*
  153. * Convert some rows of samples to the output colorspace.
  154. *
  155. * Note that we change from noninterleaved, one-plane-per-component format
  156. * to interleaved-pixel format. The output buffer is therefore three times
  157. * as wide as the input buffer.
  158. *
  159. * A starting row offset is provided only for the input buffer. The caller
  160. * can easily adjust the passed output_buf value to accommodate any row
  161. * offset required on that side.
  162. */
  163. METHODDEF(void)
  164. ycc_rgb_convert (j_decompress_ptr cinfo,
  165. JSAMPIMAGE input_buf, JDIMENSION input_row,
  166. JSAMPARRAY output_buf, int num_rows)
  167. {
  168. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  169. register int y, cb, cr;
  170. register JSAMPROW outptr;
  171. register JSAMPROW inptr0, inptr1, inptr2;
  172. register JDIMENSION col;
  173. JDIMENSION num_cols = cinfo->output_width;
  174. /* copy these pointers into registers if possible */
  175. register JSAMPLE * range_limit = cinfo->sample_range_limit;
  176. register int * Crrtab = cconvert->Cr_r_tab;
  177. register int * Cbbtab = cconvert->Cb_b_tab;
  178. register INT32 * Crgtab = cconvert->Cr_g_tab;
  179. register INT32 * Cbgtab = cconvert->Cb_g_tab;
  180. SHIFT_TEMPS
  181. while (--num_rows >= 0) {
  182. inptr0 = input_buf[0][input_row];
  183. inptr1 = input_buf[1][input_row];
  184. inptr2 = input_buf[2][input_row];
  185. input_row++;
  186. outptr = *output_buf++;
  187. for (col = 0; col < num_cols; col++) {
  188. y = GETJSAMPLE(inptr0[col]);
  189. cb = GETJSAMPLE(inptr1[col]);
  190. cr = GETJSAMPLE(inptr2[col]);
  191. /* Range-limiting is essential due to noise introduced by DCT losses,
  192. * for extended gamut (sYCC) and wide gamut (bg-sYCC) encodings.
  193. */
  194. outptr[RGB_RED] = range_limit[y + Crrtab[cr]];
  195. outptr[RGB_GREEN] = range_limit[y +
  196. ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
  197. SCALEBITS))];
  198. outptr[RGB_BLUE] = range_limit[y + Cbbtab[cb]];
  199. outptr += RGB_PIXELSIZE;
  200. }
  201. }
  202. }
  203. /**************** Cases other than YCC -> RGB ****************/
  204. /*
  205. * Initialize for RGB->grayscale colorspace conversion.
  206. */
  207. LOCAL(void)
  208. build_rgb_y_table (j_decompress_ptr cinfo)
  209. {
  210. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  211. INT32 i;
  212. cconvert->R_y_tab = (INT32 *) (*cinfo->mem->alloc_small)
  213. ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
  214. cconvert->G_y_tab = (INT32 *) (*cinfo->mem->alloc_small)
  215. ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
  216. cconvert->B_y_tab = (INT32 *) (*cinfo->mem->alloc_small)
  217. ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
  218. for (i = 0; i <= MAXJSAMPLE; i++) {
  219. cconvert->R_y_tab[i] = FIX(0.299) * i;
  220. cconvert->G_y_tab[i] = FIX(0.587) * i;
  221. cconvert->B_y_tab[i] = FIX(0.114) * i + ONE_HALF;
  222. }
  223. }
  224. /*
  225. * Convert RGB to grayscale.
  226. */
  227. METHODDEF(void)
  228. rgb_gray_convert (j_decompress_ptr cinfo,
  229. JSAMPIMAGE input_buf, JDIMENSION input_row,
  230. JSAMPARRAY output_buf, int num_rows)
  231. {
  232. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  233. register INT32 y;
  234. register INT32 * Rytab = cconvert->R_y_tab;
  235. register INT32 * Gytab = cconvert->G_y_tab;
  236. register INT32 * Bytab = cconvert->B_y_tab;
  237. register JSAMPROW outptr;
  238. register JSAMPROW inptr0, inptr1, inptr2;
  239. register JDIMENSION col;
  240. JDIMENSION num_cols = cinfo->output_width;
  241. while (--num_rows >= 0) {
  242. inptr0 = input_buf[0][input_row];
  243. inptr1 = input_buf[1][input_row];
  244. inptr2 = input_buf[2][input_row];
  245. input_row++;
  246. outptr = *output_buf++;
  247. for (col = 0; col < num_cols; col++) {
  248. y = Rytab[GETJSAMPLE(inptr0[col])];
  249. y += Gytab[GETJSAMPLE(inptr1[col])];
  250. y += Bytab[GETJSAMPLE(inptr2[col])];
  251. outptr[col] = (JSAMPLE) (y >> SCALEBITS);
  252. }
  253. }
  254. }
  255. /*
  256. * Convert some rows of samples to the output colorspace.
  257. * [R-G,G,B-G] to [R,G,B] conversion with modulo calculation
  258. * (inverse color transform).
  259. * This can be seen as an adaption of the general YCbCr->RGB
  260. * conversion equation with Kr = Kb = 0, while replacing the
  261. * normalization by modulo calculation.
  262. */
  263. METHODDEF(void)
  264. rgb1_rgb_convert (j_decompress_ptr cinfo,
  265. JSAMPIMAGE input_buf, JDIMENSION input_row,
  266. JSAMPARRAY output_buf, int num_rows)
  267. {
  268. register int r, g, b;
  269. register JSAMPROW outptr;
  270. register JSAMPROW inptr0, inptr1, inptr2;
  271. register JDIMENSION col;
  272. JDIMENSION num_cols = cinfo->output_width;
  273. while (--num_rows >= 0) {
  274. inptr0 = input_buf[0][input_row];
  275. inptr1 = input_buf[1][input_row];
  276. inptr2 = input_buf[2][input_row];
  277. input_row++;
  278. outptr = *output_buf++;
  279. for (col = 0; col < num_cols; col++) {
  280. r = GETJSAMPLE(inptr0[col]);
  281. g = GETJSAMPLE(inptr1[col]);
  282. b = GETJSAMPLE(inptr2[col]);
  283. /* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD
  284. * (modulo) operator is equivalent to the bitmask operator AND.
  285. */
  286. outptr[RGB_RED] = (JSAMPLE) ((r + g - CENTERJSAMPLE) & MAXJSAMPLE);
  287. outptr[RGB_GREEN] = (JSAMPLE) g;
  288. outptr[RGB_BLUE] = (JSAMPLE) ((b + g - CENTERJSAMPLE) & MAXJSAMPLE);
  289. outptr += RGB_PIXELSIZE;
  290. }
  291. }
  292. }
  293. /*
  294. * [R-G,G,B-G] to grayscale conversion with modulo calculation
  295. * (inverse color transform).
  296. */
  297. METHODDEF(void)
  298. rgb1_gray_convert (j_decompress_ptr cinfo,
  299. JSAMPIMAGE input_buf, JDIMENSION input_row,
  300. JSAMPARRAY output_buf, int num_rows)
  301. {
  302. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  303. register int r, g, b;
  304. register INT32 y;
  305. register INT32 * Rytab = cconvert->R_y_tab;
  306. register INT32 * Gytab = cconvert->G_y_tab;
  307. register INT32 * Bytab = cconvert->B_y_tab;
  308. register JSAMPROW outptr;
  309. register JSAMPROW inptr0, inptr1, inptr2;
  310. register JDIMENSION col;
  311. JDIMENSION num_cols = cinfo->output_width;
  312. while (--num_rows >= 0) {
  313. inptr0 = input_buf[0][input_row];
  314. inptr1 = input_buf[1][input_row];
  315. inptr2 = input_buf[2][input_row];
  316. input_row++;
  317. outptr = *output_buf++;
  318. for (col = 0; col < num_cols; col++) {
  319. r = GETJSAMPLE(inptr0[col]);
  320. g = GETJSAMPLE(inptr1[col]);
  321. b = GETJSAMPLE(inptr2[col]);
  322. /* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD
  323. * (modulo) operator is equivalent to the bitmask operator AND.
  324. */
  325. y = Rytab[(r + g - CENTERJSAMPLE) & MAXJSAMPLE];
  326. y += Gytab[g];
  327. y += Bytab[(b + g - CENTERJSAMPLE) & MAXJSAMPLE];
  328. outptr[col] = (JSAMPLE) (y >> SCALEBITS);
  329. }
  330. }
  331. }
  332. /*
  333. * Convert some rows of samples to the output colorspace.
  334. * No colorspace change, but conversion from separate-planes
  335. * to interleaved representation.
  336. */
  337. METHODDEF(void)
  338. rgb_convert (j_decompress_ptr cinfo,
  339. JSAMPIMAGE input_buf, JDIMENSION input_row,
  340. JSAMPARRAY output_buf, int num_rows)
  341. {
  342. register JSAMPROW outptr;
  343. register JSAMPROW inptr0, inptr1, inptr2;
  344. register JDIMENSION col;
  345. JDIMENSION num_cols = cinfo->output_width;
  346. while (--num_rows >= 0) {
  347. inptr0 = input_buf[0][input_row];
  348. inptr1 = input_buf[1][input_row];
  349. inptr2 = input_buf[2][input_row];
  350. input_row++;
  351. outptr = *output_buf++;
  352. for (col = 0; col < num_cols; col++) {
  353. /* We can dispense with GETJSAMPLE() here */
  354. outptr[RGB_RED] = inptr0[col];
  355. outptr[RGB_GREEN] = inptr1[col];
  356. outptr[RGB_BLUE] = inptr2[col];
  357. outptr += RGB_PIXELSIZE;
  358. }
  359. }
  360. }
  361. /*
  362. * Color conversion for no colorspace change: just copy the data,
  363. * converting from separate-planes to interleaved representation.
  364. * Note: Omit uninteresting components in output buffer.
  365. */
  366. METHODDEF(void)
  367. null_convert (j_decompress_ptr cinfo,
  368. JSAMPIMAGE input_buf, JDIMENSION input_row,
  369. JSAMPARRAY output_buf, int num_rows)
  370. {
  371. register JSAMPROW outptr;
  372. register JSAMPROW inptr;
  373. register JDIMENSION count;
  374. register int out_comps = cinfo->out_color_components;
  375. JDIMENSION num_cols = cinfo->output_width;
  376. JSAMPROW startptr;
  377. int ci;
  378. jpeg_component_info *compptr;
  379. while (--num_rows >= 0) {
  380. /* It seems fastest to make a separate pass for each component. */
  381. startptr = *output_buf++;
  382. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  383. ci++, compptr++) {
  384. if (! compptr->component_needed)
  385. continue; /* skip uninteresting component */
  386. inptr = input_buf[ci][input_row];
  387. outptr = startptr++;
  388. for (count = num_cols; count > 0; count--) {
  389. *outptr = *inptr++; /* don't need GETJSAMPLE() here */
  390. outptr += out_comps;
  391. }
  392. }
  393. input_row++;
  394. }
  395. }
  396. /*
  397. * Color conversion for grayscale: just copy the data.
  398. * This also works for YCC -> grayscale conversion, in which
  399. * we just copy the Y (luminance) component and ignore chrominance.
  400. */
  401. METHODDEF(void)
  402. grayscale_convert (j_decompress_ptr cinfo,
  403. JSAMPIMAGE input_buf, JDIMENSION input_row,
  404. JSAMPARRAY output_buf, int num_rows)
  405. {
  406. jcopy_sample_rows(input_buf[0] + input_row, output_buf,
  407. num_rows, cinfo->output_width);
  408. }
  409. /*
  410. * Convert grayscale to RGB: just duplicate the graylevel three times.
  411. * This is provided to support applications that don't want to cope
  412. * with grayscale as a separate case.
  413. */
  414. METHODDEF(void)
  415. gray_rgb_convert (j_decompress_ptr cinfo,
  416. JSAMPIMAGE input_buf, JDIMENSION input_row,
  417. JSAMPARRAY output_buf, int num_rows)
  418. {
  419. register JSAMPROW outptr;
  420. register JSAMPROW inptr;
  421. register JDIMENSION col;
  422. JDIMENSION num_cols = cinfo->output_width;
  423. while (--num_rows >= 0) {
  424. inptr = input_buf[0][input_row++];
  425. outptr = *output_buf++;
  426. for (col = 0; col < num_cols; col++) {
  427. /* We can dispense with GETJSAMPLE() here */
  428. outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
  429. outptr += RGB_PIXELSIZE;
  430. }
  431. }
  432. }
  433. /*
  434. * Convert some rows of samples to the output colorspace.
  435. * This version handles Adobe-style YCCK->CMYK conversion,
  436. * where we convert YCbCr to R=1-C, G=1-M, and B=1-Y using the
  437. * same conversion as above, while passing K (black) unchanged.
  438. * We assume build_ycc_rgb_table has been called.
  439. */
  440. METHODDEF(void)
  441. ycck_cmyk_convert (j_decompress_ptr cinfo,
  442. JSAMPIMAGE input_buf, JDIMENSION input_row,
  443. JSAMPARRAY output_buf, int num_rows)
  444. {
  445. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  446. register int y, cb, cr;
  447. register JSAMPROW outptr;
  448. register JSAMPROW inptr0, inptr1, inptr2, inptr3;
  449. register JDIMENSION col;
  450. JDIMENSION num_cols = cinfo->output_width;
  451. /* copy these pointers into registers if possible */
  452. register JSAMPLE * range_limit = cinfo->sample_range_limit;
  453. register int * Crrtab = cconvert->Cr_r_tab;
  454. register int * Cbbtab = cconvert->Cb_b_tab;
  455. register INT32 * Crgtab = cconvert->Cr_g_tab;
  456. register INT32 * Cbgtab = cconvert->Cb_g_tab;
  457. SHIFT_TEMPS
  458. while (--num_rows >= 0) {
  459. inptr0 = input_buf[0][input_row];
  460. inptr1 = input_buf[1][input_row];
  461. inptr2 = input_buf[2][input_row];
  462. inptr3 = input_buf[3][input_row];
  463. input_row++;
  464. outptr = *output_buf++;
  465. for (col = 0; col < num_cols; col++) {
  466. y = GETJSAMPLE(inptr0[col]);
  467. cb = GETJSAMPLE(inptr1[col]);
  468. cr = GETJSAMPLE(inptr2[col]);
  469. /* Range-limiting is essential due to noise introduced by DCT losses,
  470. * and for extended gamut encodings (sYCC).
  471. */
  472. outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
  473. outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
  474. ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
  475. SCALEBITS)))];
  476. outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
  477. /* K passes through unchanged */
  478. outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
  479. outptr += 4;
  480. }
  481. }
  482. }
  483. /*
  484. * Convert CMYK to YK part of YCCK for colorless output.
  485. * We assume build_rgb_y_table has been called.
  486. */
  487. METHODDEF(void)
  488. cmyk_yk_convert (j_decompress_ptr cinfo,
  489. JSAMPIMAGE input_buf, JDIMENSION input_row,
  490. JSAMPARRAY output_buf, int num_rows)
  491. {
  492. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  493. register INT32 y;
  494. register INT32 * Rytab = cconvert->R_y_tab;
  495. register INT32 * Gytab = cconvert->G_y_tab;
  496. register INT32 * Bytab = cconvert->B_y_tab;
  497. register JSAMPROW outptr;
  498. register JSAMPROW inptr0, inptr1, inptr2, inptr3;
  499. register JDIMENSION col;
  500. JDIMENSION num_cols = cinfo->output_width;
  501. while (--num_rows >= 0) {
  502. inptr0 = input_buf[0][input_row];
  503. inptr1 = input_buf[1][input_row];
  504. inptr2 = input_buf[2][input_row];
  505. inptr3 = input_buf[3][input_row];
  506. input_row++;
  507. outptr = *output_buf++;
  508. for (col = 0; col < num_cols; col++) {
  509. y = Rytab[MAXJSAMPLE - GETJSAMPLE(inptr0[col])];
  510. y += Gytab[MAXJSAMPLE - GETJSAMPLE(inptr1[col])];
  511. y += Bytab[MAXJSAMPLE - GETJSAMPLE(inptr2[col])];
  512. outptr[0] = (JSAMPLE) (y >> SCALEBITS);
  513. /* K passes through unchanged */
  514. outptr[1] = inptr3[col]; /* don't need GETJSAMPLE here */
  515. outptr += 2;
  516. }
  517. }
  518. }
  519. /*
  520. * Empty method for start_pass.
  521. */
  522. METHODDEF(void)
  523. start_pass_dcolor (j_decompress_ptr cinfo)
  524. {
  525. /* no work needed */
  526. }
  527. /*
  528. * Module initialization routine for output colorspace conversion.
  529. */
  530. GLOBAL(void)
  531. jinit_color_deconverter (j_decompress_ptr cinfo)
  532. {
  533. my_cconvert_ptr cconvert;
  534. int ci, i;
  535. cconvert = (my_cconvert_ptr) (*cinfo->mem->alloc_small)
  536. ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_color_deconverter));
  537. cinfo->cconvert = &cconvert->pub;
  538. cconvert->pub.start_pass = start_pass_dcolor;
  539. /* Make sure num_components agrees with jpeg_color_space */
  540. switch (cinfo->jpeg_color_space) {
  541. case JCS_GRAYSCALE:
  542. if (cinfo->num_components != 1)
  543. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  544. break;
  545. case JCS_RGB:
  546. case JCS_YCbCr:
  547. case JCS_BG_RGB:
  548. case JCS_BG_YCC:
  549. if (cinfo->num_components != 3)
  550. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  551. break;
  552. case JCS_CMYK:
  553. case JCS_YCCK:
  554. if (cinfo->num_components != 4)
  555. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  556. break;
  557. default: /* JCS_UNKNOWN can be anything */
  558. if (cinfo->num_components < 1)
  559. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  560. }
  561. /* Support color transform only for RGB colorspaces */
  562. if (cinfo->color_transform &&
  563. cinfo->jpeg_color_space != JCS_RGB &&
  564. cinfo->jpeg_color_space != JCS_BG_RGB)
  565. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  566. /* Set out_color_components and conversion method based on requested space.
  567. * Also adjust the component_needed flags for any unused components,
  568. * so that earlier pipeline stages can avoid useless computation.
  569. */
  570. switch (cinfo->out_color_space) {
  571. case JCS_GRAYSCALE:
  572. cinfo->out_color_components = 1;
  573. switch (cinfo->jpeg_color_space) {
  574. case JCS_GRAYSCALE:
  575. case JCS_YCbCr:
  576. case JCS_BG_YCC:
  577. cconvert->pub.color_convert = grayscale_convert;
  578. /* For color->grayscale conversion, only the Y (0) component is needed */
  579. for (ci = 1; ci < cinfo->num_components; ci++)
  580. cinfo->comp_info[ci].component_needed = FALSE;
  581. break;
  582. case JCS_RGB:
  583. switch (cinfo->color_transform) {
  584. case JCT_NONE:
  585. cconvert->pub.color_convert = rgb_gray_convert;
  586. break;
  587. case JCT_SUBTRACT_GREEN:
  588. cconvert->pub.color_convert = rgb1_gray_convert;
  589. break;
  590. default:
  591. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  592. }
  593. build_rgb_y_table(cinfo);
  594. break;
  595. default:
  596. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  597. }
  598. break;
  599. case JCS_RGB:
  600. cinfo->out_color_components = RGB_PIXELSIZE;
  601. switch (cinfo->jpeg_color_space) {
  602. case JCS_GRAYSCALE:
  603. cconvert->pub.color_convert = gray_rgb_convert;
  604. break;
  605. case JCS_YCbCr:
  606. cconvert->pub.color_convert = ycc_rgb_convert;
  607. build_ycc_rgb_table(cinfo);
  608. break;
  609. case JCS_BG_YCC:
  610. cconvert->pub.color_convert = ycc_rgb_convert;
  611. build_bg_ycc_rgb_table(cinfo);
  612. break;
  613. case JCS_RGB:
  614. switch (cinfo->color_transform) {
  615. case JCT_NONE:
  616. cconvert->pub.color_convert = rgb_convert;
  617. break;
  618. case JCT_SUBTRACT_GREEN:
  619. cconvert->pub.color_convert = rgb1_rgb_convert;
  620. break;
  621. default:
  622. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  623. }
  624. break;
  625. default:
  626. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  627. }
  628. break;
  629. case JCS_BG_RGB:
  630. if (cinfo->jpeg_color_space != JCS_BG_RGB)
  631. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  632. cinfo->out_color_components = RGB_PIXELSIZE;
  633. switch (cinfo->color_transform) {
  634. case JCT_NONE:
  635. cconvert->pub.color_convert = rgb_convert;
  636. break;
  637. case JCT_SUBTRACT_GREEN:
  638. cconvert->pub.color_convert = rgb1_rgb_convert;
  639. break;
  640. default:
  641. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  642. }
  643. break;
  644. case JCS_CMYK:
  645. if (cinfo->jpeg_color_space != JCS_YCCK)
  646. goto def_label;
  647. cinfo->out_color_components = 4;
  648. cconvert->pub.color_convert = ycck_cmyk_convert;
  649. build_ycc_rgb_table(cinfo);
  650. break;
  651. case JCS_YCCK:
  652. if (cinfo->jpeg_color_space != JCS_CMYK ||
  653. /* Support only YK part of YCCK for colorless output */
  654. ! cinfo->comp_info[0].component_needed ||
  655. cinfo->comp_info[1].component_needed ||
  656. cinfo->comp_info[2].component_needed ||
  657. ! cinfo->comp_info[3].component_needed)
  658. goto def_label;
  659. cinfo->out_color_components = 2;
  660. /* Need all components on input side */
  661. cinfo->comp_info[1].component_needed = TRUE;
  662. cinfo->comp_info[2].component_needed = TRUE;
  663. cconvert->pub.color_convert = cmyk_yk_convert;
  664. build_rgb_y_table(cinfo);
  665. break;
  666. default: def_label: /* permit null conversion to same output space */
  667. if (cinfo->out_color_space != cinfo->jpeg_color_space)
  668. /* unsupported non-null conversion */
  669. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  670. i = 0;
  671. for (ci = 0; ci < cinfo->num_components; ci++)
  672. if (cinfo->comp_info[ci].component_needed)
  673. i++; /* count output color components */
  674. cinfo->out_color_components = i;
  675. cconvert->pub.color_convert = null_convert;
  676. }
  677. if (cinfo->quantize_colors)
  678. cinfo->output_components = 1; /* single colormapped output component */
  679. else
  680. cinfo->output_components = cinfo->out_color_components;
  681. }