jccolor.cpp 15 KB

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