jdcolor.c 23 KB

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