jdcolor.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*
  2. * jdcolor.c
  3. *
  4. * Copyright (C) 1991-1997, Thomas G. Lane.
  5. * Modified 2011-2020 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. * Note: Omit uninteresting components in output buffer.
  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 out_comps = cinfo->out_color_components;
  381. JDIMENSION num_cols = cinfo->output_width;
  382. JSAMPROW startptr;
  383. int ci;
  384. jpeg_component_info *compptr;
  385. while (--num_rows >= 0) {
  386. /* It seems fastest to make a separate pass for each component. */
  387. startptr = *output_buf++;
  388. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  389. ci++, compptr++) {
  390. if (! compptr->component_needed)
  391. continue; /* skip uninteresting component */
  392. inptr = input_buf[ci][input_row];
  393. outptr = startptr++;
  394. for (count = num_cols; count > 0; count--) {
  395. *outptr = *inptr++; /* don't need GETJSAMPLE() here */
  396. outptr += out_comps;
  397. }
  398. }
  399. input_row++;
  400. }
  401. }
  402. /*
  403. * Color conversion for grayscale: just copy the data.
  404. * This also works for YCC -> grayscale conversion, in which
  405. * we just copy the Y (luminance) component and ignore chrominance.
  406. */
  407. METHODDEF(void)
  408. grayscale_convert (j_decompress_ptr cinfo,
  409. JSAMPIMAGE input_buf, JDIMENSION input_row,
  410. JSAMPARRAY output_buf, int num_rows)
  411. {
  412. jcopy_sample_rows(input_buf[0] + input_row, output_buf,
  413. num_rows, cinfo->output_width);
  414. }
  415. /*
  416. * Convert grayscale to RGB: just duplicate the graylevel three times.
  417. * This is provided to support applications that don't want to cope
  418. * with grayscale as a separate case.
  419. */
  420. METHODDEF(void)
  421. gray_rgb_convert (j_decompress_ptr cinfo,
  422. JSAMPIMAGE input_buf, JDIMENSION input_row,
  423. JSAMPARRAY output_buf, int num_rows)
  424. {
  425. register JSAMPROW outptr;
  426. register JSAMPROW inptr;
  427. register JDIMENSION col;
  428. JDIMENSION num_cols = cinfo->output_width;
  429. while (--num_rows >= 0) {
  430. inptr = input_buf[0][input_row++];
  431. outptr = *output_buf++;
  432. for (col = 0; col < num_cols; col++) {
  433. /* We can dispense with GETJSAMPLE() here */
  434. outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
  435. outptr += RGB_PIXELSIZE;
  436. }
  437. }
  438. }
  439. /*
  440. * Convert some rows of samples to the output colorspace.
  441. * This version handles Adobe-style YCCK->CMYK conversion,
  442. * where we convert YCbCr to R=1-C, G=1-M, and B=1-Y using the
  443. * same conversion as above, while passing K (black) unchanged.
  444. * We assume build_ycc_rgb_table has been called.
  445. */
  446. METHODDEF(void)
  447. ycck_cmyk_convert (j_decompress_ptr cinfo,
  448. JSAMPIMAGE input_buf, JDIMENSION input_row,
  449. JSAMPARRAY output_buf, int num_rows)
  450. {
  451. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  452. register int y, cb, cr;
  453. register JSAMPROW outptr;
  454. register JSAMPROW inptr0, inptr1, inptr2, inptr3;
  455. register JDIMENSION col;
  456. JDIMENSION num_cols = cinfo->output_width;
  457. /* copy these pointers into registers if possible */
  458. register JSAMPLE * range_limit = cinfo->sample_range_limit;
  459. register int * Crrtab = cconvert->Cr_r_tab;
  460. register int * Cbbtab = cconvert->Cb_b_tab;
  461. register INT32 * Crgtab = cconvert->Cr_g_tab;
  462. register INT32 * Cbgtab = cconvert->Cb_g_tab;
  463. SHIFT_TEMPS
  464. while (--num_rows >= 0) {
  465. inptr0 = input_buf[0][input_row];
  466. inptr1 = input_buf[1][input_row];
  467. inptr2 = input_buf[2][input_row];
  468. inptr3 = input_buf[3][input_row];
  469. input_row++;
  470. outptr = *output_buf++;
  471. for (col = 0; col < num_cols; col++) {
  472. y = GETJSAMPLE(inptr0[col]);
  473. cb = GETJSAMPLE(inptr1[col]);
  474. cr = GETJSAMPLE(inptr2[col]);
  475. /* Range-limiting is essential due to noise introduced by DCT losses,
  476. * and for extended gamut encodings (sYCC).
  477. */
  478. outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
  479. outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
  480. ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
  481. SCALEBITS)))];
  482. outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
  483. /* K passes through unchanged */
  484. outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
  485. outptr += 4;
  486. }
  487. }
  488. }
  489. /*
  490. * Convert CMYK to YK part of YCCK for colorless output.
  491. * We assume build_rgb_y_table has been called.
  492. */
  493. METHODDEF(void)
  494. cmyk_yk_convert (j_decompress_ptr cinfo,
  495. JSAMPIMAGE input_buf, JDIMENSION input_row,
  496. JSAMPARRAY output_buf, int num_rows)
  497. {
  498. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  499. register int r, g, b;
  500. register INT32 * ctab = cconvert->rgb_y_tab;
  501. register JSAMPROW outptr;
  502. register JSAMPROW inptr0, inptr1, inptr2, inptr3;
  503. register JDIMENSION col;
  504. JDIMENSION num_cols = cinfo->output_width;
  505. while (--num_rows >= 0) {
  506. inptr0 = input_buf[0][input_row];
  507. inptr1 = input_buf[1][input_row];
  508. inptr2 = input_buf[2][input_row];
  509. inptr3 = input_buf[3][input_row];
  510. input_row++;
  511. outptr = *output_buf++;
  512. for (col = 0; col < num_cols; col++) {
  513. r = MAXJSAMPLE - GETJSAMPLE(inptr0[col]);
  514. g = MAXJSAMPLE - GETJSAMPLE(inptr1[col]);
  515. b = MAXJSAMPLE - GETJSAMPLE(inptr2[col]);
  516. /* Y */
  517. outptr[0] = (JSAMPLE)
  518. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  519. >> SCALEBITS);
  520. /* K passes through unchanged */
  521. outptr[1] = inptr3[col]; /* don't need GETJSAMPLE here */
  522. outptr += 2;
  523. }
  524. }
  525. }
  526. /*
  527. * Empty method for start_pass.
  528. */
  529. METHODDEF(void)
  530. start_pass_dcolor (j_decompress_ptr cinfo)
  531. {
  532. /* no work needed */
  533. }
  534. /*
  535. * Module initialization routine for output colorspace conversion.
  536. */
  537. GLOBAL(void)
  538. jinit_color_deconverter (j_decompress_ptr cinfo)
  539. {
  540. my_cconvert_ptr cconvert;
  541. int ci, i;
  542. cconvert = (my_cconvert_ptr) (*cinfo->mem->alloc_small)
  543. ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_color_deconverter));
  544. cinfo->cconvert = &cconvert->pub;
  545. cconvert->pub.start_pass = start_pass_dcolor;
  546. /* Make sure num_components agrees with jpeg_color_space */
  547. switch (cinfo->jpeg_color_space) {
  548. case JCS_GRAYSCALE:
  549. if (cinfo->num_components != 1)
  550. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  551. break;
  552. case JCS_RGB:
  553. case JCS_YCbCr:
  554. case JCS_BG_RGB:
  555. case JCS_BG_YCC:
  556. if (cinfo->num_components != 3)
  557. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  558. break;
  559. case JCS_CMYK:
  560. case JCS_YCCK:
  561. if (cinfo->num_components != 4)
  562. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  563. break;
  564. default: /* JCS_UNKNOWN can be anything */
  565. if (cinfo->num_components < 1)
  566. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  567. }
  568. /* Support color transform only for RGB colorspaces */
  569. if (cinfo->color_transform &&
  570. cinfo->jpeg_color_space != JCS_RGB &&
  571. cinfo->jpeg_color_space != JCS_BG_RGB)
  572. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  573. /* Set out_color_components and conversion method based on requested space.
  574. * Also adjust the component_needed flags for any unused components,
  575. * so that earlier pipeline stages can avoid useless computation.
  576. */
  577. switch (cinfo->out_color_space) {
  578. case JCS_GRAYSCALE:
  579. cinfo->out_color_components = 1;
  580. switch (cinfo->jpeg_color_space) {
  581. case JCS_GRAYSCALE:
  582. case JCS_YCbCr:
  583. case JCS_BG_YCC:
  584. cconvert->pub.color_convert = grayscale_convert;
  585. /* For color->grayscale conversion, only the Y (0) component is needed */
  586. for (ci = 1; ci < cinfo->num_components; ci++)
  587. cinfo->comp_info[ci].component_needed = FALSE;
  588. break;
  589. case JCS_RGB:
  590. switch (cinfo->color_transform) {
  591. case JCT_NONE:
  592. cconvert->pub.color_convert = rgb_gray_convert;
  593. break;
  594. case JCT_SUBTRACT_GREEN:
  595. cconvert->pub.color_convert = rgb1_gray_convert;
  596. break;
  597. default:
  598. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  599. }
  600. build_rgb_y_table(cinfo);
  601. break;
  602. default:
  603. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  604. }
  605. break;
  606. case JCS_RGB:
  607. cinfo->out_color_components = RGB_PIXELSIZE;
  608. switch (cinfo->jpeg_color_space) {
  609. case JCS_GRAYSCALE:
  610. cconvert->pub.color_convert = gray_rgb_convert;
  611. break;
  612. case JCS_YCbCr:
  613. cconvert->pub.color_convert = ycc_rgb_convert;
  614. build_ycc_rgb_table(cinfo);
  615. break;
  616. case JCS_BG_YCC:
  617. cconvert->pub.color_convert = ycc_rgb_convert;
  618. build_bg_ycc_rgb_table(cinfo);
  619. break;
  620. case JCS_RGB:
  621. switch (cinfo->color_transform) {
  622. case JCT_NONE:
  623. cconvert->pub.color_convert = rgb_convert;
  624. break;
  625. case JCT_SUBTRACT_GREEN:
  626. cconvert->pub.color_convert = rgb1_rgb_convert;
  627. break;
  628. default:
  629. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  630. }
  631. break;
  632. default:
  633. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  634. }
  635. break;
  636. case JCS_BG_RGB:
  637. if (cinfo->jpeg_color_space != JCS_BG_RGB)
  638. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  639. cinfo->out_color_components = RGB_PIXELSIZE;
  640. switch (cinfo->color_transform) {
  641. case JCT_NONE:
  642. cconvert->pub.color_convert = rgb_convert;
  643. break;
  644. case JCT_SUBTRACT_GREEN:
  645. cconvert->pub.color_convert = rgb1_rgb_convert;
  646. break;
  647. default:
  648. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  649. }
  650. break;
  651. case JCS_CMYK:
  652. if (cinfo->jpeg_color_space != JCS_YCCK)
  653. goto def_label;
  654. cinfo->out_color_components = 4;
  655. cconvert->pub.color_convert = ycck_cmyk_convert;
  656. build_ycc_rgb_table(cinfo);
  657. break;
  658. case JCS_YCCK:
  659. if (cinfo->jpeg_color_space != JCS_CMYK ||
  660. /* Support only YK part of YCCK for colorless output */
  661. ! cinfo->comp_info[0].component_needed ||
  662. cinfo->comp_info[1].component_needed ||
  663. cinfo->comp_info[2].component_needed ||
  664. ! cinfo->comp_info[3].component_needed)
  665. goto def_label;
  666. cinfo->out_color_components = 2;
  667. /* Need all components on input side */
  668. cinfo->comp_info[1].component_needed = TRUE;
  669. cinfo->comp_info[2].component_needed = TRUE;
  670. cconvert->pub.color_convert = cmyk_yk_convert;
  671. build_rgb_y_table(cinfo);
  672. break;
  673. default: def_label: /* permit null conversion to same output space */
  674. if (cinfo->out_color_space != cinfo->jpeg_color_space)
  675. /* unsupported non-null conversion */
  676. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  677. i = 0;
  678. for (ci = 0; ci < cinfo->num_components; ci++)
  679. if (cinfo->comp_info[ci].component_needed)
  680. i++; /* count output color components */
  681. cinfo->out_color_components = i;
  682. cconvert->pub.color_convert = null_convert;
  683. }
  684. if (cinfo->quantize_colors)
  685. cinfo->output_components = 1; /* single colormapped output component */
  686. else
  687. cinfo->output_components = cinfo->out_color_components;
  688. }