jccolor.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. * jccolor.c
  3. *
  4. * Copyright (C) 1991-1996, 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 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 = 0.5 * (B - Y) / (1 - Kb)
  35. * Cr = 0.5 * (R - Y) / (1 - Kr)
  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),
  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 you
  57. * 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] = FIX(0.5) * i + CBCR_OFFSET + ONE_HALF-1;
  104. /* B=>Cb and R=>Cr tables are the same
  105. rgb_ycc_tab[i+R_CR_OFF] = FIX(0.5) * i + 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.
  116. * The 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 caller
  119. * can easily adjust the passed input_buf value to accommodate any row
  120. * 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 INT32 * ctab = cconvert->rgb_ycc_tab;
  129. register int r, g, b;
  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. /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  145. * must be too; we do not need an explicit range-limiting operation.
  146. * Hence the value being shifted is never negative, and we don't
  147. * need the general RIGHT_SHIFT macro.
  148. */
  149. /* Y */
  150. outptr0[col] = (JSAMPLE)
  151. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  152. >> SCALEBITS);
  153. /* Cb */
  154. outptr1[col] = (JSAMPLE)
  155. ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  156. >> SCALEBITS);
  157. /* Cr */
  158. outptr2[col] = (JSAMPLE)
  159. ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  160. >> SCALEBITS);
  161. inptr += RGB_PIXELSIZE;
  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, which is the same
  169. * 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 * ctab = cconvert->rgb_ycc_tab;
  179. register int r, g, b;
  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. r = GETJSAMPLE(inptr[RGB_RED]);
  189. g = GETJSAMPLE(inptr[RGB_GREEN]);
  190. b = GETJSAMPLE(inptr[RGB_BLUE]);
  191. /* Y */
  192. outptr[col] = (JSAMPLE)
  193. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  194. >> SCALEBITS);
  195. inptr += RGB_PIXELSIZE;
  196. }
  197. }
  198. }
  199. /*
  200. * Convert some rows of samples to the JPEG colorspace.
  201. * This version handles Adobe-style CMYK->YCCK conversion,
  202. * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
  203. * conversion as above, while passing K (black) unchanged.
  204. * We assume rgb_ycc_start has been called.
  205. */
  206. METHODDEF(void)
  207. cmyk_ycck_convert (j_compress_ptr cinfo,
  208. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  209. JDIMENSION output_row, int num_rows)
  210. {
  211. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  212. register INT32 * ctab = cconvert->rgb_ycc_tab;
  213. register int r, g, b;
  214. register JSAMPROW inptr;
  215. register JSAMPROW outptr0, outptr1, outptr2, outptr3;
  216. register JDIMENSION col;
  217. JDIMENSION num_cols = cinfo->image_width;
  218. while (--num_rows >= 0) {
  219. inptr = *input_buf++;
  220. outptr0 = output_buf[0][output_row];
  221. outptr1 = output_buf[1][output_row];
  222. outptr2 = output_buf[2][output_row];
  223. outptr3 = output_buf[3][output_row];
  224. output_row++;
  225. for (col = 0; col < num_cols; col++) {
  226. r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
  227. g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
  228. b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
  229. /* K passes through as-is */
  230. outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
  231. /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  232. * must be too; we do not need an explicit range-limiting operation.
  233. * Hence the value being shifted is never negative, and we don't
  234. * need the general RIGHT_SHIFT macro.
  235. */
  236. /* Y */
  237. outptr0[col] = (JSAMPLE)
  238. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  239. >> SCALEBITS);
  240. /* Cb */
  241. outptr1[col] = (JSAMPLE)
  242. ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  243. >> SCALEBITS);
  244. /* Cr */
  245. outptr2[col] = (JSAMPLE)
  246. ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  247. >> SCALEBITS);
  248. inptr += 4;
  249. }
  250. }
  251. }
  252. /*
  253. * Convert some rows of samples to the JPEG colorspace.
  254. * [R,G,B] to [R-G,G,B-G] conversion with modulo calculation
  255. * (forward reversible color transform).
  256. * This can be seen as an adaption of the general RGB->YCbCr
  257. * conversion equation with Kr = Kb = 0, while replacing the
  258. * normalization by modulo calculation.
  259. */
  260. METHODDEF(void)
  261. rgb_rgb1_convert (j_compress_ptr cinfo,
  262. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  263. JDIMENSION output_row, int num_rows)
  264. {
  265. register int r, g, b;
  266. register JSAMPROW inptr;
  267. register JSAMPROW outptr0, outptr1, outptr2;
  268. register JDIMENSION col;
  269. JDIMENSION num_cols = cinfo->image_width;
  270. while (--num_rows >= 0) {
  271. inptr = *input_buf++;
  272. outptr0 = output_buf[0][output_row];
  273. outptr1 = output_buf[1][output_row];
  274. outptr2 = output_buf[2][output_row];
  275. output_row++;
  276. for (col = 0; col < num_cols; col++) {
  277. r = GETJSAMPLE(inptr[RGB_RED]);
  278. g = GETJSAMPLE(inptr[RGB_GREEN]);
  279. b = GETJSAMPLE(inptr[RGB_BLUE]);
  280. /* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD
  281. * (modulo) operator is equivalent to the bitmask operator AND.
  282. */
  283. outptr0[col] = (JSAMPLE) ((r - g + CENTERJSAMPLE) & MAXJSAMPLE);
  284. outptr1[col] = (JSAMPLE) g;
  285. outptr2[col] = (JSAMPLE) ((b - g + CENTERJSAMPLE) & MAXJSAMPLE);
  286. inptr += RGB_PIXELSIZE;
  287. }
  288. }
  289. }
  290. /*
  291. * Convert some rows of samples to the JPEG colorspace.
  292. * This version handles grayscale output with no conversion.
  293. * The source can be either plain grayscale or YCC (since Y == gray).
  294. */
  295. METHODDEF(void)
  296. grayscale_convert (j_compress_ptr cinfo,
  297. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  298. JDIMENSION output_row, int num_rows)
  299. {
  300. int instride = cinfo->input_components;
  301. register JSAMPROW inptr;
  302. register JSAMPROW outptr;
  303. register JDIMENSION col;
  304. JDIMENSION num_cols = cinfo->image_width;
  305. while (--num_rows >= 0) {
  306. inptr = *input_buf++;
  307. outptr = output_buf[0][output_row++];
  308. for (col = 0; col < num_cols; col++) {
  309. outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
  310. inptr += instride;
  311. }
  312. }
  313. }
  314. /*
  315. * Convert some rows of samples to the JPEG colorspace.
  316. * No colorspace conversion, but change from interleaved
  317. * to separate-planes representation.
  318. */
  319. METHODDEF(void)
  320. rgb_convert (j_compress_ptr cinfo,
  321. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  322. JDIMENSION output_row, int num_rows)
  323. {
  324. register JSAMPROW inptr;
  325. register JSAMPROW outptr0, outptr1, outptr2;
  326. register JDIMENSION col;
  327. JDIMENSION num_cols = cinfo->image_width;
  328. while (--num_rows >= 0) {
  329. inptr = *input_buf++;
  330. outptr0 = output_buf[0][output_row];
  331. outptr1 = output_buf[1][output_row];
  332. outptr2 = output_buf[2][output_row];
  333. output_row++;
  334. for (col = 0; col < num_cols; col++) {
  335. /* We can dispense with GETJSAMPLE() here */
  336. outptr0[col] = inptr[RGB_RED];
  337. outptr1[col] = inptr[RGB_GREEN];
  338. outptr2[col] = inptr[RGB_BLUE];
  339. inptr += RGB_PIXELSIZE;
  340. }
  341. }
  342. }
  343. /*
  344. * Convert some rows of samples to the JPEG colorspace.
  345. * This version handles multi-component colorspaces without conversion.
  346. * We assume input_components == num_components.
  347. */
  348. METHODDEF(void)
  349. null_convert (j_compress_ptr cinfo,
  350. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  351. JDIMENSION output_row, int num_rows)
  352. {
  353. int ci;
  354. register int nc = cinfo->num_components;
  355. register JSAMPROW inptr;
  356. register JSAMPROW outptr;
  357. register JDIMENSION col;
  358. JDIMENSION num_cols = cinfo->image_width;
  359. while (--num_rows >= 0) {
  360. /* It seems fastest to make a separate pass for each component. */
  361. for (ci = 0; ci < nc; ci++) {
  362. inptr = input_buf[0] + ci;
  363. outptr = output_buf[ci][output_row];
  364. for (col = 0; col < num_cols; col++) {
  365. *outptr++ = *inptr; /* don't need GETJSAMPLE() here */
  366. inptr += nc;
  367. }
  368. }
  369. input_buf++;
  370. output_row++;
  371. }
  372. }
  373. /*
  374. * Empty method for start_pass.
  375. */
  376. METHODDEF(void)
  377. null_method (j_compress_ptr cinfo)
  378. {
  379. /* no work needed */
  380. }
  381. /*
  382. * Module initialization routine for input colorspace conversion.
  383. */
  384. GLOBAL(void)
  385. jinit_color_converter (j_compress_ptr cinfo)
  386. {
  387. my_cconvert_ptr cconvert;
  388. cconvert = (my_cconvert_ptr)
  389. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  390. SIZEOF(my_color_converter));
  391. cinfo->cconvert = &cconvert->pub;
  392. /* set start_pass to null method until we find out differently */
  393. cconvert->pub.start_pass = null_method;
  394. /* Make sure input_components agrees with in_color_space */
  395. switch (cinfo->in_color_space) {
  396. case JCS_GRAYSCALE:
  397. if (cinfo->input_components != 1)
  398. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  399. break;
  400. case JCS_RGB:
  401. case JCS_BG_RGB:
  402. if (cinfo->input_components != RGB_PIXELSIZE)
  403. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  404. break;
  405. case JCS_YCbCr:
  406. case JCS_BG_YCC:
  407. if (cinfo->input_components != 3)
  408. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  409. break;
  410. case JCS_CMYK:
  411. case JCS_YCCK:
  412. if (cinfo->input_components != 4)
  413. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  414. break;
  415. default: /* JCS_UNKNOWN can be anything */
  416. if (cinfo->input_components < 1)
  417. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  418. break;
  419. }
  420. /* Support color transform only for RGB colorspaces */
  421. if (cinfo->color_transform &&
  422. cinfo->jpeg_color_space != JCS_RGB &&
  423. cinfo->jpeg_color_space != JCS_BG_RGB)
  424. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  425. /* Check num_components, set conversion method based on requested space */
  426. switch (cinfo->jpeg_color_space) {
  427. case JCS_GRAYSCALE:
  428. if (cinfo->num_components != 1)
  429. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  430. switch (cinfo->in_color_space) {
  431. case JCS_GRAYSCALE:
  432. case JCS_YCbCr:
  433. case JCS_BG_YCC:
  434. cconvert->pub.color_convert = grayscale_convert;
  435. break;
  436. case JCS_RGB:
  437. cconvert->pub.start_pass = rgb_ycc_start;
  438. cconvert->pub.color_convert = rgb_gray_convert;
  439. break;
  440. default:
  441. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  442. }
  443. break;
  444. case JCS_RGB:
  445. case JCS_BG_RGB:
  446. if (cinfo->num_components != 3)
  447. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  448. if (cinfo->in_color_space == cinfo->jpeg_color_space) {
  449. switch (cinfo->color_transform) {
  450. case JCT_NONE:
  451. cconvert->pub.color_convert = rgb_convert;
  452. break;
  453. case JCT_SUBTRACT_GREEN:
  454. cconvert->pub.color_convert = rgb_rgb1_convert;
  455. break;
  456. default:
  457. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  458. }
  459. } else
  460. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  461. break;
  462. case JCS_YCbCr:
  463. if (cinfo->num_components != 3)
  464. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  465. switch (cinfo->in_color_space) {
  466. case JCS_RGB:
  467. cconvert->pub.start_pass = rgb_ycc_start;
  468. cconvert->pub.color_convert = rgb_ycc_convert;
  469. break;
  470. case JCS_YCbCr:
  471. cconvert->pub.color_convert = null_convert;
  472. break;
  473. default:
  474. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  475. }
  476. break;
  477. case JCS_BG_YCC:
  478. if (cinfo->num_components != 3)
  479. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  480. switch (cinfo->in_color_space) {
  481. case JCS_RGB:
  482. /* For conversion from normal RGB input to BG_YCC representation,
  483. * the Cb/Cr values are first computed as usual, and then
  484. * quantized further after DCT processing by a factor of
  485. * 2 in reference to the nominal quantization factor.
  486. */
  487. /* need quantization scale by factor of 2 after DCT */
  488. cinfo->comp_info[1].component_needed = TRUE;
  489. cinfo->comp_info[2].component_needed = TRUE;
  490. /* compute normal YCC first */
  491. cconvert->pub.start_pass = rgb_ycc_start;
  492. cconvert->pub.color_convert = rgb_ycc_convert;
  493. break;
  494. case JCS_YCbCr:
  495. /* need quantization scale by factor of 2 after DCT */
  496. cinfo->comp_info[1].component_needed = TRUE;
  497. cinfo->comp_info[2].component_needed = TRUE;
  498. /*FALLTHROUGH*/
  499. case JCS_BG_YCC:
  500. /* Pass through for BG_YCC input */
  501. cconvert->pub.color_convert = null_convert;
  502. break;
  503. default:
  504. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  505. }
  506. break;
  507. case JCS_CMYK:
  508. if (cinfo->num_components != 4)
  509. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  510. if (cinfo->in_color_space == JCS_CMYK)
  511. cconvert->pub.color_convert = null_convert;
  512. else
  513. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  514. break;
  515. case JCS_YCCK:
  516. if (cinfo->num_components != 4)
  517. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  518. switch (cinfo->in_color_space) {
  519. case JCS_CMYK:
  520. cconvert->pub.start_pass = rgb_ycc_start;
  521. cconvert->pub.color_convert = cmyk_ycck_convert;
  522. break;
  523. case JCS_YCCK:
  524. cconvert->pub.color_convert = null_convert;
  525. break;
  526. default:
  527. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  528. }
  529. break;
  530. default: /* allow null conversion of JCS_UNKNOWN */
  531. if (cinfo->jpeg_color_space != cinfo->in_color_space ||
  532. cinfo->num_components != cinfo->input_components)
  533. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  534. cconvert->pub.color_convert = null_convert;
  535. break;
  536. }
  537. }