jccolor.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. /* KEVINJ: HACK!!!!!!!!! You are supposed to change RGB_PIXELSIZE, but I don't want to change this globally in case it breaks other stuff */
  132. if (cinfo->hack_use_input_components_as_RGB_PIXELSIZE)
  133. inptr +=cinfo->input_components;
  134. else
  135. inptr += RGB_PIXELSIZE;
  136. /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  137. * must be too; we do not need an explicit range-limiting operation.
  138. * Hence the value being shifted is never negative, and we don't
  139. * need the general RIGHT_SHIFT macro.
  140. */
  141. /* Y */
  142. outptr0[col] = (JSAMPLE)
  143. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  144. >> SCALEBITS);
  145. /* Cb */
  146. outptr1[col] = (JSAMPLE)
  147. ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  148. >> SCALEBITS);
  149. /* Cr */
  150. outptr2[col] = (JSAMPLE)
  151. ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  152. >> SCALEBITS);
  153. }
  154. }
  155. }
  156. /**************** Cases other than RGB -> YCbCr **************/
  157. /*
  158. * Convert some rows of samples to the JPEG colorspace.
  159. * This version handles RGB->grayscale conversion, which is the same
  160. * as the RGB->Y portion of RGB->YCbCr.
  161. * We assume rgb_ycc_start has been called (we only use the Y tables).
  162. */
  163. METHODDEF(void)
  164. rgb_gray_convert (j_compress_ptr cinfo,
  165. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  166. JDIMENSION output_row, int num_rows)
  167. {
  168. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  169. register int r, g, b;
  170. register INT32 * ctab = cconvert->rgb_ycc_tab;
  171. register JSAMPROW inptr;
  172. register JSAMPROW outptr;
  173. register JDIMENSION col;
  174. JDIMENSION num_cols = cinfo->image_width;
  175. while (--num_rows >= 0) {
  176. inptr = *input_buf++;
  177. outptr = output_buf[0][output_row];
  178. output_row++;
  179. for (col = 0; col < num_cols; col++) {
  180. r = GETJSAMPLE(inptr[RGB_RED]);
  181. g = GETJSAMPLE(inptr[RGB_GREEN]);
  182. b = GETJSAMPLE(inptr[RGB_BLUE]);
  183. inptr += RGB_PIXELSIZE;
  184. /* Y */
  185. outptr[col] = (JSAMPLE)
  186. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  187. >> SCALEBITS);
  188. }
  189. }
  190. }
  191. /*
  192. * Convert some rows of samples to the JPEG colorspace.
  193. * This version handles Adobe-style CMYK->YCCK conversion,
  194. * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
  195. * conversion as above, while passing K (black) unchanged.
  196. * We assume rgb_ycc_start has been called.
  197. */
  198. METHODDEF(void)
  199. cmyk_ycck_convert (j_compress_ptr cinfo,
  200. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  201. JDIMENSION output_row, int num_rows)
  202. {
  203. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  204. register int r, g, b;
  205. register INT32 * ctab = cconvert->rgb_ycc_tab;
  206. register JSAMPROW inptr;
  207. register JSAMPROW outptr0, outptr1, outptr2, outptr3;
  208. register JDIMENSION col;
  209. JDIMENSION num_cols = cinfo->image_width;
  210. while (--num_rows >= 0) {
  211. inptr = *input_buf++;
  212. outptr0 = output_buf[0][output_row];
  213. outptr1 = output_buf[1][output_row];
  214. outptr2 = output_buf[2][output_row];
  215. outptr3 = output_buf[3][output_row];
  216. output_row++;
  217. for (col = 0; col < num_cols; col++) {
  218. r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
  219. g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
  220. b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
  221. /* K passes through as-is */
  222. outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
  223. inptr += 4;
  224. /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  225. * must be too; we do not need an explicit range-limiting operation.
  226. * Hence the value being shifted is never negative, and we don't
  227. * need the general RIGHT_SHIFT macro.
  228. */
  229. /* Y */
  230. outptr0[col] = (JSAMPLE)
  231. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  232. >> SCALEBITS);
  233. /* Cb */
  234. outptr1[col] = (JSAMPLE)
  235. ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  236. >> SCALEBITS);
  237. /* Cr */
  238. outptr2[col] = (JSAMPLE)
  239. ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  240. >> SCALEBITS);
  241. }
  242. }
  243. }
  244. /*
  245. * Convert some rows of samples to the JPEG colorspace.
  246. * This version handles grayscale output with no conversion.
  247. * The source can be either plain grayscale or YCbCr (since Y == gray).
  248. */
  249. METHODDEF(void)
  250. grayscale_convert (j_compress_ptr cinfo,
  251. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  252. JDIMENSION output_row, int num_rows)
  253. {
  254. register JSAMPROW inptr;
  255. register JSAMPROW outptr;
  256. register JDIMENSION col;
  257. JDIMENSION num_cols = cinfo->image_width;
  258. int instride = cinfo->input_components;
  259. while (--num_rows >= 0) {
  260. inptr = *input_buf++;
  261. outptr = output_buf[0][output_row];
  262. output_row++;
  263. for (col = 0; col < num_cols; col++) {
  264. outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
  265. inptr += instride;
  266. }
  267. }
  268. }
  269. /*
  270. * Convert some rows of samples to the JPEG colorspace.
  271. * This version handles multi-component colorspaces without conversion.
  272. * We assume input_components == num_components.
  273. */
  274. METHODDEF(void)
  275. null_convert (j_compress_ptr cinfo,
  276. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  277. JDIMENSION output_row, int num_rows)
  278. {
  279. register JSAMPROW inptr;
  280. register JSAMPROW outptr;
  281. register JDIMENSION col;
  282. register int ci;
  283. int nc = cinfo->num_components;
  284. JDIMENSION num_cols = cinfo->image_width;
  285. while (--num_rows >= 0) {
  286. /* It seems fastest to make a separate pass for each component. */
  287. for (ci = 0; ci < nc; ci++) {
  288. inptr = *input_buf;
  289. outptr = output_buf[ci][output_row];
  290. for (col = 0; col < num_cols; col++) {
  291. outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
  292. inptr += nc;
  293. }
  294. }
  295. input_buf++;
  296. output_row++;
  297. }
  298. }
  299. /*
  300. * Empty method for start_pass.
  301. */
  302. METHODDEF(void)
  303. null_method (j_compress_ptr cinfo)
  304. {
  305. /* no work needed */
  306. }
  307. /*
  308. * Module initialization routine for input colorspace conversion.
  309. */
  310. GLOBAL(void)
  311. jinit_color_converter (j_compress_ptr cinfo)
  312. {
  313. my_cconvert_ptr cconvert;
  314. cconvert = (my_cconvert_ptr)
  315. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  316. SIZEOF(my_color_converter));
  317. cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
  318. /* set start_pass to null method until we find out differently */
  319. cconvert->pub.start_pass = null_method;
  320. /* Make sure input_components agrees with in_color_space */
  321. switch (cinfo->in_color_space) {
  322. case JCS_GRAYSCALE:
  323. if (cinfo->input_components != 1)
  324. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  325. break;
  326. case JCS_RGB:
  327. #if RGB_PIXELSIZE != 3
  328. if (cinfo->input_components != RGB_PIXELSIZE)
  329. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  330. break;
  331. #endif /* else share code with YCbCr */
  332. case JCS_YCbCr:
  333. if (cinfo->input_components != 3)
  334. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  335. break;
  336. case JCS_CMYK:
  337. case JCS_YCCK:
  338. if (cinfo->input_components != 4)
  339. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  340. break;
  341. default: /* JCS_UNKNOWN can be anything */
  342. if (cinfo->input_components < 1)
  343. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  344. break;
  345. }
  346. /* Check num_components, set conversion method based on requested space */
  347. switch (cinfo->jpeg_color_space) {
  348. case JCS_GRAYSCALE:
  349. if (cinfo->num_components != 1)
  350. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  351. if (cinfo->in_color_space == JCS_GRAYSCALE)
  352. cconvert->pub.color_convert = grayscale_convert;
  353. else if (cinfo->in_color_space == JCS_RGB) {
  354. cconvert->pub.start_pass = rgb_ycc_start;
  355. cconvert->pub.color_convert = rgb_gray_convert;
  356. } else if (cinfo->in_color_space == JCS_YCbCr)
  357. cconvert->pub.color_convert = grayscale_convert;
  358. else
  359. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  360. break;
  361. case JCS_RGB:
  362. if (cinfo->num_components != 3)
  363. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  364. if (cinfo->in_color_space == JCS_RGB && RGB_PIXELSIZE == 3)
  365. cconvert->pub.color_convert = null_convert;
  366. else
  367. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  368. break;
  369. case JCS_YCbCr:
  370. if (cinfo->num_components != 3)
  371. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  372. if (cinfo->in_color_space == JCS_RGB) {
  373. cconvert->pub.start_pass = rgb_ycc_start;
  374. cconvert->pub.color_convert = rgb_ycc_convert;
  375. } else if (cinfo->in_color_space == JCS_YCbCr)
  376. cconvert->pub.color_convert = null_convert;
  377. else
  378. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  379. break;
  380. case JCS_CMYK:
  381. if (cinfo->num_components != 4)
  382. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  383. if (cinfo->in_color_space == JCS_CMYK)
  384. cconvert->pub.color_convert = null_convert;
  385. else
  386. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  387. break;
  388. case JCS_YCCK:
  389. if (cinfo->num_components != 4)
  390. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  391. if (cinfo->in_color_space == JCS_CMYK) {
  392. cconvert->pub.start_pass = rgb_ycc_start;
  393. cconvert->pub.color_convert = cmyk_ycck_convert;
  394. } else if (cinfo->in_color_space == JCS_YCCK)
  395. cconvert->pub.color_convert = null_convert;
  396. else
  397. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  398. break;
  399. default: /* allow null conversion of JCS_UNKNOWN */
  400. if (cinfo->jpeg_color_space != cinfo->in_color_space ||
  401. cinfo->num_components != cinfo->input_components)
  402. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  403. cconvert->pub.color_convert = null_convert;
  404. break;
  405. }
  406. }
粤ICP备19079148号