jdcolor.c 22 KB

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