jccolor.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * jccolor.c
  3. *
  4. * Copyright (C) 1991-1996, Thomas G. Lane.
  5. * Modified 2011-2023 by Guido Vollbeding.
  6. * This file is part of the Independent JPEG Group's software.
  7. * For conditions of distribution and use, see the accompanying README file.
  8. *
  9. * This file contains input 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_converter pub; /* public fields */
  17. /* Private state for RGB->YCC conversion */
  18. INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */
  19. } my_color_converter;
  20. typedef my_color_converter * my_cconvert_ptr;
  21. /**************** RGB -> YCbCr conversion: most common case **************/
  22. /*
  23. * YCbCr is defined per Recommendation ITU-R BT.601-7 (03/2011),
  24. * previously known as Recommendation CCIR 601-1, except that Cb and Cr
  25. * are normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
  26. * sRGB (standard RGB color space) is defined per IEC 61966-2-1:1999.
  27. * sYCC (standard luma-chroma-chroma color space with extended gamut)
  28. * is defined per IEC 61966-2-1:1999 Amendment A1:2003 Annex F.
  29. * bg-sRGB and bg-sYCC (big gamut standard color spaces)
  30. * are defined per IEC 61966-2-1:1999 Amendment A1:2003 Annex G.
  31. * Note that the derived conversion coefficients given in some of these
  32. * documents are imprecise. The general conversion equations are
  33. * Y = Kr * R + (1 - Kr - Kb) * G + Kb * B
  34. * Cb = (B - Y) / (1 - Kb) / K
  35. * Cr = (R - Y) / (1 - Kr) / K
  36. * With Kr = 0.299 and Kb = 0.114 (derived according to SMPTE RP 177-1993
  37. * from the 1953 FCC NTSC primaries and CIE Illuminant C), K = 2 for sYCC,
  38. * the conversion equations to be implemented are therefore
  39. * Y = 0.299 * R + 0.587 * G + 0.114 * B
  40. * Cb = -0.168735892 * R - 0.331264108 * G + 0.5 * B + CENTERJSAMPLE
  41. * Cr = 0.5 * R - 0.418687589 * G - 0.081312411 * B + CENTERJSAMPLE
  42. * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
  43. * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and
  44. * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
  45. * were not represented exactly. Now we sacrifice exact representation of
  46. * maximum red and maximum blue in order to get exact grayscales.
  47. *
  48. * To avoid floating-point arithmetic, we represent the fractional constants
  49. * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
  50. * the products by 2^16, with appropriate rounding, to get the correct answer.
  51. *
  52. * For even more speed, we avoid doing any multiplications in the inner loop
  53. * by precalculating the constants times R,G,B for all possible values.
  54. * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
  55. * for 9-bit to 12-bit samples it is still acceptable. It's not very
  56. * reasonable for 16-bit samples, but if you want lossless storage
  57. * you shouldn't be changing colorspace anyway.
  58. * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
  59. * in the tables to save adding them separately in the inner loop.
  60. */
  61. #define SCALEBITS 16 /* speediest right-shift on some machines */
  62. #define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS)
  63. #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
  64. #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
  65. /* We allocate one big table and divide it up into eight parts, instead of
  66. * doing eight alloc_small requests. This lets us use a single table base
  67. * address, which can be held in a register in the inner loops on many
  68. * machines (more than can hold all eight addresses, anyway).
  69. */
  70. #define R_Y_OFF 0 /* offset to R => Y section */
  71. #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
  72. #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
  73. #define R_CB_OFF (3*(MAXJSAMPLE+1))
  74. #define G_CB_OFF (4*(MAXJSAMPLE+1))
  75. #define B_CB_OFF (5*(MAXJSAMPLE+1))
  76. #define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */
  77. #define G_CR_OFF (6*(MAXJSAMPLE+1))
  78. #define B_CR_OFF (7*(MAXJSAMPLE+1))
  79. #define TABLE_SIZE (8*(MAXJSAMPLE+1))
  80. /*
  81. * Initialize for RGB->YCC colorspace conversion.
  82. */
  83. METHODDEF(void)
  84. rgb_ycc_start (j_compress_ptr cinfo)
  85. {
  86. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  87. INT32 * rgb_ycc_tab;
  88. INT32 i;
  89. /* Allocate and fill in the conversion tables. */
  90. cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
  91. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  92. TABLE_SIZE * SIZEOF(INT32));
  93. for (i = 0; i <= MAXJSAMPLE; i++) {
  94. rgb_ycc_tab[i+R_Y_OFF] = FIX(0.299) * i;
  95. rgb_ycc_tab[i+G_Y_OFF] = FIX(0.587) * i;
  96. rgb_ycc_tab[i+B_Y_OFF] = FIX(0.114) * i + ONE_HALF;
  97. rgb_ycc_tab[i+R_CB_OFF] = (- FIX(0.168735892)) * i;
  98. rgb_ycc_tab[i+G_CB_OFF] = (- FIX(0.331264108)) * i;
  99. /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
  100. * This ensures that the maximum output will round to MAXJSAMPLE
  101. * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
  102. */
  103. rgb_ycc_tab[i+B_CB_OFF] = (i << (SCALEBITS-1)) + CBCR_OFFSET + ONE_HALF-1;
  104. /* B=>Cb and R=>Cr tables are the same
  105. rgb_ycc_tab[i+R_CR_OFF] = (i << (SCALEBITS-1)) + CBCR_OFFSET + ONE_HALF-1;
  106. */
  107. rgb_ycc_tab[i+G_CR_OFF] = (- FIX(0.418687589)) * i;
  108. rgb_ycc_tab[i+B_CR_OFF] = (- FIX(0.081312411)) * i;
  109. }
  110. }
  111. /*
  112. * Convert some rows of samples to the JPEG colorspace.
  113. *
  114. * Note that we change from the application's interleaved-pixel format
  115. * to our internal noninterleaved, one-plane-per-component format. The
  116. * input buffer is therefore three times as wide as the output buffer.
  117. *
  118. * A starting row offset is provided only for the output buffer. The
  119. * caller can easily adjust the passed input_buf value to accommodate
  120. * any row offset required on that side.
  121. */
  122. METHODDEF(void)
  123. rgb_ycc_convert (j_compress_ptr cinfo,
  124. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  125. JDIMENSION output_row, int num_rows)
  126. {
  127. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  128. register int r, g, b;
  129. register INT32 * ctab = cconvert->rgb_ycc_tab;
  130. register JSAMPROW inptr;
  131. register JSAMPROW outptr0, outptr1, outptr2;
  132. register JDIMENSION col;
  133. JDIMENSION num_cols = cinfo->image_width;
  134. while (--num_rows >= 0) {
  135. inptr = *input_buf++;
  136. outptr0 = output_buf[0][output_row];
  137. outptr1 = output_buf[1][output_row];
  138. outptr2 = output_buf[2][output_row];
  139. output_row++;
  140. for (col = 0; col < num_cols; col++) {
  141. r = GETJSAMPLE(inptr[RGB_RED]);
  142. g = GETJSAMPLE(inptr[RGB_GREEN]);
  143. b = GETJSAMPLE(inptr[RGB_BLUE]);
  144. inptr += RGB_PIXELSIZE;
  145. /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  146. * must be too; we do not need an explicit range-limiting operation.
  147. * Hence the value being shifted is never negative, and we don't
  148. * need the general RIGHT_SHIFT macro.
  149. */
  150. /* Y */
  151. outptr0[col] = (JSAMPLE)
  152. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  153. >> SCALEBITS);
  154. /* Cb */
  155. outptr1[col] = (JSAMPLE)
  156. ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  157. >> SCALEBITS);
  158. /* Cr */
  159. outptr2[col] = (JSAMPLE)
  160. ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  161. >> SCALEBITS);
  162. }
  163. }
  164. }
  165. /**************** Cases other than RGB -> YCbCr **************/
  166. /*
  167. * Convert some rows of samples to the JPEG colorspace.
  168. * This version handles RGB->grayscale conversion,
  169. * which is the same as the RGB->Y portion of RGB->YCbCr.
  170. * We assume rgb_ycc_start has been called (we only use the Y tables).
  171. */
  172. METHODDEF(void)
  173. rgb_gray_convert (j_compress_ptr cinfo,
  174. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  175. JDIMENSION output_row, int num_rows)
  176. {
  177. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  178. register INT32 y;
  179. register INT32 * ctab = cconvert->rgb_ycc_tab;
  180. register JSAMPROW inptr;
  181. register JSAMPROW outptr;
  182. register JDIMENSION col;
  183. JDIMENSION num_cols = cinfo->image_width;
  184. while (--num_rows >= 0) {
  185. inptr = *input_buf++;
  186. outptr = output_buf[0][output_row++];
  187. for (col = 0; col < num_cols; col++) {
  188. y = ctab[R_Y_OFF + GETJSAMPLE(inptr[RGB_RED])];
  189. y += ctab[G_Y_OFF + GETJSAMPLE(inptr[RGB_GREEN])];
  190. y += ctab[B_Y_OFF + GETJSAMPLE(inptr[RGB_BLUE])];
  191. inptr += RGB_PIXELSIZE;
  192. outptr[col] = (JSAMPLE) (y >> SCALEBITS);
  193. }
  194. }
  195. }
  196. /*
  197. * Convert some rows of samples to the JPEG colorspace.
  198. * This version handles Adobe-style CMYK->YCCK conversion,
  199. * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the
  200. * same conversion as above, while passing K (black) unchanged.
  201. * We assume rgb_ycc_start has been called.
  202. */
  203. METHODDEF(void)
  204. cmyk_ycck_convert (j_compress_ptr cinfo,
  205. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  206. JDIMENSION output_row, int num_rows)
  207. {
  208. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  209. register int r, g, b;
  210. register INT32 * ctab = cconvert->rgb_ycc_tab;
  211. register JSAMPROW inptr;
  212. register JSAMPROW outptr0, outptr1, outptr2, outptr3;
  213. register JDIMENSION col;
  214. JDIMENSION num_cols = cinfo->image_width;
  215. while (--num_rows >= 0) {
  216. inptr = *input_buf++;
  217. outptr0 = output_buf[0][output_row];
  218. outptr1 = output_buf[1][output_row];
  219. outptr2 = output_buf[2][output_row];
  220. outptr3 = output_buf[3][output_row];
  221. output_row++;
  222. for (col = 0; col < num_cols; col++) {
  223. r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
  224. g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
  225. b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
  226. /* K passes through as-is */
  227. outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
  228. inptr += 4;
  229. /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  230. * must be too; we do not need an explicit range-limiting operation.
  231. * Hence the value being shifted is never negative, and we don't
  232. * need the general RIGHT_SHIFT macro.
  233. */
  234. /* Y */
  235. outptr0[col] = (JSAMPLE)
  236. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  237. >> SCALEBITS);
  238. /* Cb */
  239. outptr1[col] = (JSAMPLE)
  240. ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  241. >> SCALEBITS);
  242. /* Cr */
  243. outptr2[col] = (JSAMPLE)
  244. ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  245. >> SCALEBITS);
  246. }
  247. }
  248. }
  249. /*
  250. * Convert some rows of samples to the JPEG colorspace.
  251. * [R,G,B] to [R-G,G,B-G] conversion with modulo calculation
  252. * (forward reversible color transform).
  253. * This can be seen as an adaption of the general RGB->YCbCr
  254. * conversion equation with Kr = Kb = 0, while replacing the
  255. * normalization by modulo calculation.
  256. */
  257. METHODDEF(void)
  258. rgb_rgb1_convert (j_compress_ptr cinfo,
  259. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  260. JDIMENSION output_row, int num_rows)
  261. {
  262. register int r, g, b;
  263. register JSAMPROW inptr;
  264. register JSAMPROW outptr0, outptr1, outptr2;
  265. register JDIMENSION col;
  266. JDIMENSION num_cols = cinfo->image_width;
  267. while (--num_rows >= 0) {
  268. inptr = *input_buf++;
  269. outptr0 = output_buf[0][output_row];
  270. outptr1 = output_buf[1][output_row];
  271. outptr2 = output_buf[2][output_row];
  272. output_row++;
  273. for (col = 0; col < num_cols; col++) {
  274. r = GETJSAMPLE(inptr[RGB_RED]);
  275. g = GETJSAMPLE(inptr[RGB_GREEN]);
  276. b = GETJSAMPLE(inptr[RGB_BLUE]);
  277. inptr += RGB_PIXELSIZE;
  278. /* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD
  279. * (modulo) operator is equivalent to the bitmask operator AND.
  280. */
  281. outptr0[col] = (JSAMPLE) ((r - g + CENTERJSAMPLE) & MAXJSAMPLE);
  282. outptr1[col] = (JSAMPLE) g;
  283. outptr2[col] = (JSAMPLE) ((b - g + CENTERJSAMPLE) & MAXJSAMPLE);
  284. }
  285. }
  286. }
  287. /*
  288. * Convert some rows of samples to the JPEG colorspace.
  289. * This version handles grayscale output with no conversion.
  290. * The source can be either plain grayscale or YCC (since Y == gray).
  291. */
  292. METHODDEF(void)
  293. grayscale_convert (j_compress_ptr cinfo,
  294. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  295. JDIMENSION output_row, int num_rows)
  296. {
  297. register JSAMPROW inptr;
  298. register JSAMPROW outptr;
  299. register JDIMENSION count;
  300. register int instride = cinfo->input_components;
  301. JDIMENSION num_cols = cinfo->image_width;
  302. while (--num_rows >= 0) {
  303. inptr = *input_buf++;
  304. outptr = output_buf[0][output_row++];
  305. for (count = num_cols; count > 0; count--) {
  306. *outptr++ = *inptr; /* don't need GETJSAMPLE() here */
  307. inptr += instride;
  308. }
  309. }
  310. }
  311. /*
  312. * Convert some rows of samples to the JPEG colorspace.
  313. * No colorspace conversion, but change from interleaved
  314. * to separate-planes representation.
  315. */
  316. METHODDEF(void)
  317. rgb_convert (j_compress_ptr cinfo,
  318. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  319. JDIMENSION output_row, int num_rows)
  320. {
  321. register JSAMPROW inptr;
  322. register JSAMPROW outptr0, outptr1, outptr2;
  323. register JDIMENSION col;
  324. JDIMENSION num_cols = cinfo->image_width;
  325. while (--num_rows >= 0) {
  326. inptr = *input_buf++;
  327. outptr0 = output_buf[0][output_row];
  328. outptr1 = output_buf[1][output_row];
  329. outptr2 = output_buf[2][output_row];
  330. output_row++;
  331. for (col = 0; col < num_cols; col++) {
  332. /* We can dispense with GETJSAMPLE() here */
  333. outptr0[col] = inptr[RGB_RED];
  334. outptr1[col] = inptr[RGB_GREEN];
  335. outptr2[col] = inptr[RGB_BLUE];
  336. inptr += RGB_PIXELSIZE;
  337. }
  338. }
  339. }
  340. /*
  341. * Convert some rows of samples to the JPEG colorspace.
  342. * This version handles multi-component colorspaces without conversion.
  343. * We assume input_components == num_components.
  344. */
  345. METHODDEF(void)
  346. null_convert (j_compress_ptr cinfo,
  347. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  348. JDIMENSION output_row, int num_rows)
  349. {
  350. register JSAMPROW inptr;
  351. register JSAMPROW outptr;
  352. register JDIMENSION count;
  353. register int num_comps = cinfo->num_components;
  354. JDIMENSION num_cols = cinfo->image_width;
  355. int ci;
  356. while (--num_rows >= 0) {
  357. /* It seems fastest to make a separate pass for each component. */
  358. for (ci = 0; ci < num_comps; ci++) {
  359. inptr = input_buf[0] + ci;
  360. outptr = output_buf[ci][output_row];
  361. for (count = num_cols; count > 0; count--) {
  362. *outptr++ = *inptr; /* don't need GETJSAMPLE() here */
  363. inptr += num_comps;
  364. }
  365. }
  366. input_buf++;
  367. output_row++;
  368. }
  369. }
  370. /*
  371. * Empty method for start_pass.
  372. */
  373. METHODDEF(void)
  374. null_method (j_compress_ptr cinfo)
  375. {
  376. /* no work needed */
  377. }
  378. /*
  379. * Module initialization routine for input colorspace conversion.
  380. */
  381. GLOBAL(void)
  382. jinit_color_converter (j_compress_ptr cinfo)
  383. {
  384. my_cconvert_ptr cconvert;
  385. cconvert = (my_cconvert_ptr) (*cinfo->mem->alloc_small)
  386. ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_color_converter));
  387. cinfo->cconvert = &cconvert->pub;
  388. /* set start_pass to null method until we find out differently */
  389. cconvert->pub.start_pass = null_method;
  390. /* Make sure input_components agrees with in_color_space */
  391. switch (cinfo->in_color_space) {
  392. case JCS_GRAYSCALE:
  393. if (cinfo->input_components != 1)
  394. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  395. break;
  396. case JCS_RGB:
  397. case JCS_BG_RGB:
  398. #if RGB_PIXELSIZE != 3
  399. if (cinfo->input_components != RGB_PIXELSIZE)
  400. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  401. break;
  402. #endif /* else share code with YCbCr */
  403. case JCS_YCbCr:
  404. case JCS_BG_YCC:
  405. if (cinfo->input_components != 3)
  406. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  407. break;
  408. case JCS_CMYK:
  409. case JCS_YCCK:
  410. if (cinfo->input_components != 4)
  411. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  412. break;
  413. default: /* JCS_UNKNOWN can be anything */
  414. if (cinfo->input_components < 1)
  415. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  416. }
  417. /* Support color transform only for RGB colorspaces */
  418. if (cinfo->color_transform &&
  419. cinfo->jpeg_color_space != JCS_RGB &&
  420. cinfo->jpeg_color_space != JCS_BG_RGB)
  421. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  422. /* Check num_components, set conversion method based on requested space */
  423. switch (cinfo->jpeg_color_space) {
  424. case JCS_GRAYSCALE:
  425. if (cinfo->num_components != 1)
  426. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  427. switch (cinfo->in_color_space) {
  428. case JCS_GRAYSCALE:
  429. case JCS_YCbCr:
  430. case JCS_BG_YCC:
  431. cconvert->pub.color_convert = grayscale_convert;
  432. break;
  433. case JCS_RGB:
  434. cconvert->pub.start_pass = rgb_ycc_start;
  435. cconvert->pub.color_convert = rgb_gray_convert;
  436. break;
  437. default:
  438. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  439. }
  440. break;
  441. case JCS_RGB:
  442. case JCS_BG_RGB:
  443. if (cinfo->num_components != 3)
  444. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  445. if (cinfo->in_color_space != cinfo->jpeg_color_space)
  446. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  447. switch (cinfo->color_transform) {
  448. case JCT_NONE:
  449. cconvert->pub.color_convert = rgb_convert;
  450. break;
  451. case JCT_SUBTRACT_GREEN:
  452. cconvert->pub.color_convert = rgb_rgb1_convert;
  453. break;
  454. default:
  455. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  456. }
  457. break;
  458. case JCS_YCbCr:
  459. if (cinfo->num_components != 3)
  460. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  461. switch (cinfo->in_color_space) {
  462. case JCS_RGB:
  463. cconvert->pub.start_pass = rgb_ycc_start;
  464. cconvert->pub.color_convert = rgb_ycc_convert;
  465. break;
  466. case JCS_YCbCr:
  467. cconvert->pub.color_convert = null_convert;
  468. break;
  469. default:
  470. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  471. }
  472. break;
  473. case JCS_BG_YCC:
  474. if (cinfo->num_components != 3)
  475. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  476. switch (cinfo->in_color_space) {
  477. case JCS_RGB:
  478. /* For conversion from normal RGB input to BG_YCC representation,
  479. * the Cb/Cr values are first computed as usual, and then
  480. * quantized further after DCT processing by a factor of
  481. * 2 in reference to the nominal quantization factor.
  482. */
  483. /* need quantization scale by factor of 2 after DCT */
  484. cinfo->comp_info[1].component_needed = TRUE;
  485. cinfo->comp_info[2].component_needed = TRUE;
  486. /* compute normal YCC first */
  487. cconvert->pub.start_pass = rgb_ycc_start;
  488. cconvert->pub.color_convert = rgb_ycc_convert;
  489. break;
  490. case JCS_YCbCr:
  491. /* need quantization scale by factor of 2 after DCT */
  492. cinfo->comp_info[1].component_needed = TRUE;
  493. cinfo->comp_info[2].component_needed = TRUE;
  494. /*FALLTHROUGH*/
  495. case JCS_BG_YCC:
  496. /* Pass through for BG_YCC input */
  497. cconvert->pub.color_convert = null_convert;
  498. break;
  499. default:
  500. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  501. }
  502. break;
  503. case JCS_CMYK:
  504. if (cinfo->num_components != 4)
  505. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  506. if (cinfo->in_color_space != JCS_CMYK)
  507. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  508. cconvert->pub.color_convert = null_convert;
  509. break;
  510. case JCS_YCCK:
  511. if (cinfo->num_components != 4)
  512. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  513. switch (cinfo->in_color_space) {
  514. case JCS_CMYK:
  515. cconvert->pub.start_pass = rgb_ycc_start;
  516. cconvert->pub.color_convert = cmyk_ycck_convert;
  517. break;
  518. case JCS_YCCK:
  519. cconvert->pub.color_convert = null_convert;
  520. break;
  521. default:
  522. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  523. }
  524. break;
  525. default: /* allow null conversion of JCS_UNKNOWN */
  526. if (cinfo->jpeg_color_space != cinfo->in_color_space ||
  527. cinfo->num_components != cinfo->input_components)
  528. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  529. cconvert->pub.color_convert = null_convert;
  530. }
  531. }