jccolor.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * jccolor.c
  3. *
  4. * Copyright (C) 1991-1996, Thomas G. Lane.
  5. * This file is part of the Independent JPEG Group's software.
  6. * For conditions of distribution and use, see the accompanying README file.
  7. *
  8. * This file contains input colorspace conversion routines.
  9. */
  10. #define JPEG_INTERNALS
  11. #include "jinclude.h"
  12. #include "jpeglib.h"
  13. /* Private subobject */
  14. typedef struct {
  15. struct jpeg_color_converter pub; /* public fields */
  16. /* Private state for RGB->YCC conversion */
  17. INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */
  18. } my_color_converter;
  19. typedef my_color_converter * my_cconvert_ptr;
  20. /**************** RGB -> YCbCr conversion: most common case **************/
  21. /*
  22. * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
  23. * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
  24. * The conversion equations to be implemented are therefore
  25. * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
  26. * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
  27. * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE
  28. * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
  29. * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
  30. * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and
  31. * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
  32. * were not represented exactly. Now we sacrifice exact representation of
  33. * maximum red and maximum blue in order to get exact grayscales.
  34. *
  35. * To avoid floating-point arithmetic, we represent the fractional constants
  36. * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
  37. * the products by 2^16, with appropriate rounding, to get the correct answer.
  38. *
  39. * For even more speed, we avoid doing any multiplications in the inner loop
  40. * by precalculating the constants times R,G,B for all possible values.
  41. * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
  42. * for 12-bit samples it is still acceptable. It's not very reasonable for
  43. * 16-bit samples, but if you want lossless storage you shouldn't be changing
  44. * colorspace anyway.
  45. * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
  46. * in the tables to save adding them separately in the inner loop.
  47. */
  48. #define SCALEBITS 16 /* speediest right-shift on some machines */
  49. #define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS)
  50. #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
  51. #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
  52. /* We allocate one big table and divide it up into eight parts, instead of
  53. * doing eight alloc_small requests. This lets us use a single table base
  54. * address, which can be held in a register in the inner loops on many
  55. * machines (more than can hold all eight addresses, anyway).
  56. */
  57. #define R_Y_OFF 0 /* offset to R => Y section */
  58. #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
  59. #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
  60. #define R_CB_OFF (3*(MAXJSAMPLE+1))
  61. #define G_CB_OFF (4*(MAXJSAMPLE+1))
  62. #define B_CB_OFF (5*(MAXJSAMPLE+1))
  63. #define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */
  64. #define G_CR_OFF (6*(MAXJSAMPLE+1))
  65. #define B_CR_OFF (7*(MAXJSAMPLE+1))
  66. #define TABLE_SIZE (8*(MAXJSAMPLE+1))
  67. /*
  68. * Initialize for RGB->YCC colorspace conversion.
  69. */
  70. METHODDEF(void)
  71. rgb_ycc_start (j_compress_ptr cinfo)
  72. {
  73. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  74. INT32 * rgb_ycc_tab;
  75. INT32 i;
  76. /* Allocate and fill in the conversion tables. */
  77. cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
  78. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  79. (TABLE_SIZE * SIZEOF(INT32)));
  80. for (i = 0; i <= MAXJSAMPLE; i++) {
  81. rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
  82. rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
  83. rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
  84. rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
  85. rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
  86. /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
  87. * This ensures that the maximum output will round to MAXJSAMPLE
  88. * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
  89. */
  90. rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
  91. /* B=>Cb and R=>Cr tables are the same
  92. rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
  93. */
  94. rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
  95. rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
  96. }
  97. }
  98. /*
  99. * Convert some rows of samples to the JPEG colorspace.
  100. *
  101. * Note that we change from the application's interleaved-pixel format
  102. * to our internal noninterleaved, one-plane-per-component format.
  103. * The input buffer is therefore three times as wide as the output buffer.
  104. *
  105. * A starting row offset is provided only for the output buffer. The caller
  106. * can easily adjust the passed input_buf value to accommodate any row
  107. * offset required on that side.
  108. */
  109. METHODDEF(void)
  110. rgb_ycc_convert (j_compress_ptr cinfo,
  111. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  112. JDIMENSION output_row, int num_rows)
  113. {
  114. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  115. register int r, g, b;
  116. register INT32 * ctab = cconvert->rgb_ycc_tab;
  117. register JSAMPROW inptr;
  118. register JSAMPROW outptr0, outptr1, outptr2;
  119. register JDIMENSION col;
  120. JDIMENSION num_cols = cinfo->image_width;
  121. while (--num_rows >= 0) {
  122. inptr = *input_buf++;
  123. outptr0 = output_buf[0][output_row];
  124. outptr1 = output_buf[1][output_row];
  125. outptr2 = output_buf[2][output_row];
  126. output_row++;
  127. for (col = 0; col < num_cols; col++) {
  128. r = GETJSAMPLE(inptr[RGB_RED]);
  129. g = GETJSAMPLE(inptr[RGB_GREEN]);
  130. b = GETJSAMPLE(inptr[RGB_BLUE]);
  131. inptr += RGB_PIXELSIZE;
  132. /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  133. * must be too; we do not need an explicit range-limiting operation.
  134. * Hence the value being shifted is never negative, and we don't
  135. * need the general RIGHT_SHIFT macro.
  136. */
  137. /* Y */
  138. outptr0[col] = (JSAMPLE)
  139. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  140. >> SCALEBITS);
  141. /* Cb */
  142. outptr1[col] = (JSAMPLE)
  143. ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  144. >> SCALEBITS);
  145. /* Cr */
  146. outptr2[col] = (JSAMPLE)
  147. ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  148. >> SCALEBITS);
  149. }
  150. }
  151. }
  152. /**************** Cases other than RGB -> YCbCr **************/
  153. /*
  154. * Convert some rows of samples to the JPEG colorspace.
  155. * This version handles RGB->grayscale conversion, which is the same
  156. * as the RGB->Y portion of RGB->YCbCr.
  157. * We assume rgb_ycc_start has been called (we only use the Y tables).
  158. */
  159. METHODDEF(void)
  160. rgb_gray_convert (j_compress_ptr cinfo,
  161. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  162. JDIMENSION output_row, int num_rows)
  163. {
  164. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  165. register int r, g, b;
  166. register INT32 * ctab = cconvert->rgb_ycc_tab;
  167. register JSAMPROW inptr;
  168. register JSAMPROW outptr;
  169. register JDIMENSION col;
  170. JDIMENSION num_cols = cinfo->image_width;
  171. while (--num_rows >= 0) {
  172. inptr = *input_buf++;
  173. outptr = output_buf[0][output_row];
  174. output_row++;
  175. for (col = 0; col < num_cols; col++) {
  176. r = GETJSAMPLE(inptr[RGB_RED]);
  177. g = GETJSAMPLE(inptr[RGB_GREEN]);
  178. b = GETJSAMPLE(inptr[RGB_BLUE]);
  179. inptr += RGB_PIXELSIZE;
  180. /* Y */
  181. outptr[col] = (JSAMPLE)
  182. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  183. >> SCALEBITS);
  184. }
  185. }
  186. }
  187. /*
  188. * Convert some rows of samples to the JPEG colorspace.
  189. * This version handles Adobe-style CMYK->YCCK conversion,
  190. * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
  191. * conversion as above, while passing K (black) unchanged.
  192. * We assume rgb_ycc_start has been called.
  193. */
  194. METHODDEF(void)
  195. cmyk_ycck_convert (j_compress_ptr cinfo,
  196. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  197. JDIMENSION output_row, int num_rows)
  198. {
  199. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  200. register int r, g, b;
  201. register INT32 * ctab = cconvert->rgb_ycc_tab;
  202. register JSAMPROW inptr;
  203. register JSAMPROW outptr0, outptr1, outptr2, outptr3;
  204. register JDIMENSION col;
  205. JDIMENSION num_cols = cinfo->image_width;
  206. while (--num_rows >= 0) {
  207. inptr = *input_buf++;
  208. outptr0 = output_buf[0][output_row];
  209. outptr1 = output_buf[1][output_row];
  210. outptr2 = output_buf[2][output_row];
  211. outptr3 = output_buf[3][output_row];
  212. output_row++;
  213. for (col = 0; col < num_cols; col++) {
  214. r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
  215. g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
  216. b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
  217. /* K passes through as-is */
  218. outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
  219. inptr += 4;
  220. /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  221. * must be too; we do not need an explicit range-limiting operation.
  222. * Hence the value being shifted is never negative, and we don't
  223. * need the general RIGHT_SHIFT macro.
  224. */
  225. /* Y */
  226. outptr0[col] = (JSAMPLE)
  227. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  228. >> SCALEBITS);
  229. /* Cb */
  230. outptr1[col] = (JSAMPLE)
  231. ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  232. >> SCALEBITS);
  233. /* Cr */
  234. outptr2[col] = (JSAMPLE)
  235. ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  236. >> SCALEBITS);
  237. }
  238. }
  239. }
  240. /*
  241. * Convert some rows of samples to the JPEG colorspace.
  242. * This version handles grayscale output with no conversion.
  243. * The source can be either plain grayscale or YCbCr (since Y == gray).
  244. */
  245. METHODDEF(void)
  246. grayscale_convert (j_compress_ptr cinfo,
  247. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  248. JDIMENSION output_row, int num_rows)
  249. {
  250. register JSAMPROW inptr;
  251. register JSAMPROW outptr;
  252. register JDIMENSION col;
  253. JDIMENSION num_cols = cinfo->image_width;
  254. int instride = cinfo->input_components;
  255. while (--num_rows >= 0) {
  256. inptr = *input_buf++;
  257. outptr = output_buf[0][output_row];
  258. output_row++;
  259. for (col = 0; col < num_cols; col++) {
  260. outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
  261. inptr += instride;
  262. }
  263. }
  264. }
  265. /*
  266. * Convert some rows of samples to the JPEG colorspace.
  267. * This version handles multi-component colorspaces without conversion.
  268. * We assume input_components == num_components.
  269. */
  270. METHODDEF(void)
  271. null_convert (j_compress_ptr cinfo,
  272. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  273. JDIMENSION output_row, int num_rows)
  274. {
  275. register JSAMPROW inptr;
  276. register JSAMPROW outptr;
  277. register JDIMENSION col;
  278. register int ci;
  279. int nc = cinfo->num_components;
  280. JDIMENSION num_cols = cinfo->image_width;
  281. while (--num_rows >= 0) {
  282. /* It seems fastest to make a separate pass for each component. */
  283. for (ci = 0; ci < nc; ci++) {
  284. inptr = *input_buf;
  285. outptr = output_buf[ci][output_row];
  286. for (col = 0; col < num_cols; col++) {
  287. outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
  288. inptr += nc;
  289. }
  290. }
  291. input_buf++;
  292. output_row++;
  293. }
  294. }
  295. /*
  296. * Empty method for start_pass.
  297. */
  298. METHODDEF(void)
  299. null_method (j_compress_ptr cinfo)
  300. {
  301. /* no work needed */
  302. }
  303. /*
  304. * Module initialization routine for input colorspace conversion.
  305. */
  306. GLOBAL(void)
  307. jinit_color_converter (j_compress_ptr cinfo)
  308. {
  309. my_cconvert_ptr cconvert;
  310. cconvert = (my_cconvert_ptr)
  311. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  312. SIZEOF(my_color_converter));
  313. cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
  314. /* set start_pass to null method until we find out differently */
  315. cconvert->pub.start_pass = null_method;
  316. /* Make sure input_components agrees with in_color_space */
  317. switch (cinfo->in_color_space) {
  318. case JCS_GRAYSCALE:
  319. if (cinfo->input_components != 1)
  320. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  321. break;
  322. case JCS_RGB:
  323. #if RGB_PIXELSIZE != 3
  324. if (cinfo->input_components != RGB_PIXELSIZE)
  325. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  326. break;
  327. #endif /* else share code with YCbCr */
  328. case JCS_YCbCr:
  329. if (cinfo->input_components != 3)
  330. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  331. break;
  332. case JCS_CMYK:
  333. case JCS_YCCK:
  334. if (cinfo->input_components != 4)
  335. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  336. break;
  337. default: /* JCS_UNKNOWN can be anything */
  338. if (cinfo->input_components < 1)
  339. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  340. break;
  341. }
  342. /* Check num_components, set conversion method based on requested space */
  343. switch (cinfo->jpeg_color_space) {
  344. case JCS_GRAYSCALE:
  345. if (cinfo->num_components != 1)
  346. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  347. if (cinfo->in_color_space == JCS_GRAYSCALE)
  348. cconvert->pub.color_convert = grayscale_convert;
  349. else if (cinfo->in_color_space == JCS_RGB) {
  350. cconvert->pub.start_pass = rgb_ycc_start;
  351. cconvert->pub.color_convert = rgb_gray_convert;
  352. } else if (cinfo->in_color_space == JCS_YCbCr)
  353. cconvert->pub.color_convert = grayscale_convert;
  354. else
  355. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  356. break;
  357. case JCS_RGB:
  358. if (cinfo->num_components != 3)
  359. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  360. if (cinfo->in_color_space == JCS_RGB && RGB_PIXELSIZE == 3)
  361. cconvert->pub.color_convert = null_convert;
  362. else
  363. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  364. break;
  365. case JCS_YCbCr:
  366. if (cinfo->num_components != 3)
  367. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  368. if (cinfo->in_color_space == JCS_RGB) {
  369. cconvert->pub.start_pass = rgb_ycc_start;
  370. cconvert->pub.color_convert = rgb_ycc_convert;
  371. } else if (cinfo->in_color_space == JCS_YCbCr)
  372. cconvert->pub.color_convert = null_convert;
  373. else
  374. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  375. break;
  376. case JCS_CMYK:
  377. if (cinfo->num_components != 4)
  378. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  379. if (cinfo->in_color_space == JCS_CMYK)
  380. cconvert->pub.color_convert = null_convert;
  381. else
  382. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  383. break;
  384. case JCS_YCCK:
  385. if (cinfo->num_components != 4)
  386. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  387. if (cinfo->in_color_space == JCS_CMYK) {
  388. cconvert->pub.start_pass = rgb_ycc_start;
  389. cconvert->pub.color_convert = cmyk_ycck_convert;
  390. } else if (cinfo->in_color_space == JCS_YCCK)
  391. cconvert->pub.color_convert = null_convert;
  392. else
  393. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  394. break;
  395. default: /* allow null conversion of JCS_UNKNOWN */
  396. if (cinfo->jpeg_color_space != cinfo->in_color_space ||
  397. cinfo->num_components != cinfo->input_components)
  398. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  399. cconvert->pub.color_convert = null_convert;
  400. break;
  401. }
  402. }