jdcolor.c 23 KB

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