jdcolor.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /*
  2. * jdcolor.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1991-1997, Thomas G. Lane.
  6. * Modified 2011 by Guido Vollbeding.
  7. * libjpeg-turbo Modifications:
  8. * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
  9. * Copyright (C) 2009, 2011-2012, 2014-2015, D. R. Commander.
  10. * Copyright (C) 2013, Linaro Limited.
  11. * For conditions of distribution and use, see the accompanying README.ijg
  12. * file.
  13. *
  14. * This file contains output colorspace conversion routines.
  15. */
  16. #define JPEG_INTERNALS
  17. #include "jinclude.h"
  18. #include "jpeglib.h"
  19. #include "jsimd.h"
  20. #include "jconfigint.h"
  21. /* Private subobject */
  22. typedef struct {
  23. struct jpeg_color_deconverter pub; /* public fields */
  24. /* Private state for YCC->RGB conversion */
  25. int *Cr_r_tab; /* => table for Cr to R conversion */
  26. int *Cb_b_tab; /* => table for Cb to B conversion */
  27. JLONG *Cr_g_tab; /* => table for Cr to G conversion */
  28. JLONG *Cb_g_tab; /* => table for Cb to G conversion */
  29. /* Private state for RGB->Y conversion */
  30. JLONG *rgb_y_tab; /* => table for RGB to Y conversion */
  31. } my_color_deconverter;
  32. typedef my_color_deconverter *my_cconvert_ptr;
  33. /**************** YCbCr -> RGB conversion: most common case **************/
  34. /**************** RGB -> Y conversion: less common case **************/
  35. /*
  36. * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
  37. * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
  38. * The conversion equations to be implemented are therefore
  39. *
  40. * R = Y + 1.40200 * Cr
  41. * G = Y - 0.34414 * Cb - 0.71414 * Cr
  42. * B = Y + 1.77200 * Cb
  43. *
  44. * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
  45. *
  46. * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
  47. * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
  48. *
  49. * To avoid floating-point arithmetic, we represent the fractional constants
  50. * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
  51. * the products by 2^16, with appropriate rounding, to get the correct answer.
  52. * Notice that Y, being an integral input, does not contribute any fraction
  53. * so it need not participate in the rounding.
  54. *
  55. * For even more speed, we avoid doing any multiplications in the inner loop
  56. * by precalculating the constants times Cb and Cr for all possible values.
  57. * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
  58. * for 12-bit samples it is still acceptable. It's not very reasonable for
  59. * 16-bit samples, but if you want lossless storage you shouldn't be changing
  60. * colorspace anyway.
  61. * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
  62. * values for the G calculation are left scaled up, since we must add them
  63. * together before rounding.
  64. */
  65. #define SCALEBITS 16 /* speediest right-shift on some machines */
  66. #define ONE_HALF ((JLONG)1 << (SCALEBITS - 1))
  67. #define FIX(x) ((JLONG)((x) * (1L << SCALEBITS) + 0.5))
  68. /* We allocate one big table for RGB->Y conversion and divide it up into
  69. * three parts, instead of doing three alloc_small requests. This lets us
  70. * use a single table base address, which can be held in a register in the
  71. * inner loops on many machines (more than can hold all three addresses,
  72. * anyway).
  73. */
  74. #define R_Y_OFF 0 /* offset to R => Y section */
  75. #define G_Y_OFF (1 * (MAXJSAMPLE + 1)) /* offset to G => Y section */
  76. #define B_Y_OFF (2 * (MAXJSAMPLE + 1)) /* etc. */
  77. #define TABLE_SIZE (3 * (MAXJSAMPLE + 1))
  78. /* Include inline routines for colorspace extensions */
  79. #include "jdcolext.c"
  80. #undef RGB_RED
  81. #undef RGB_GREEN
  82. #undef RGB_BLUE
  83. #undef RGB_PIXELSIZE
  84. #define RGB_RED EXT_RGB_RED
  85. #define RGB_GREEN EXT_RGB_GREEN
  86. #define RGB_BLUE EXT_RGB_BLUE
  87. #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
  88. #define ycc_rgb_convert_internal ycc_extrgb_convert_internal
  89. #define gray_rgb_convert_internal gray_extrgb_convert_internal
  90. #define rgb_rgb_convert_internal rgb_extrgb_convert_internal
  91. #include "jdcolext.c"
  92. #undef RGB_RED
  93. #undef RGB_GREEN
  94. #undef RGB_BLUE
  95. #undef RGB_PIXELSIZE
  96. #undef ycc_rgb_convert_internal
  97. #undef gray_rgb_convert_internal
  98. #undef rgb_rgb_convert_internal
  99. #define RGB_RED EXT_RGBX_RED
  100. #define RGB_GREEN EXT_RGBX_GREEN
  101. #define RGB_BLUE EXT_RGBX_BLUE
  102. #define RGB_ALPHA 3
  103. #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
  104. #define ycc_rgb_convert_internal ycc_extrgbx_convert_internal
  105. #define gray_rgb_convert_internal gray_extrgbx_convert_internal
  106. #define rgb_rgb_convert_internal rgb_extrgbx_convert_internal
  107. #include "jdcolext.c"
  108. #undef RGB_RED
  109. #undef RGB_GREEN
  110. #undef RGB_BLUE
  111. #undef RGB_ALPHA
  112. #undef RGB_PIXELSIZE
  113. #undef ycc_rgb_convert_internal
  114. #undef gray_rgb_convert_internal
  115. #undef rgb_rgb_convert_internal
  116. #define RGB_RED EXT_BGR_RED
  117. #define RGB_GREEN EXT_BGR_GREEN
  118. #define RGB_BLUE EXT_BGR_BLUE
  119. #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
  120. #define ycc_rgb_convert_internal ycc_extbgr_convert_internal
  121. #define gray_rgb_convert_internal gray_extbgr_convert_internal
  122. #define rgb_rgb_convert_internal rgb_extbgr_convert_internal
  123. #include "jdcolext.c"
  124. #undef RGB_RED
  125. #undef RGB_GREEN
  126. #undef RGB_BLUE
  127. #undef RGB_PIXELSIZE
  128. #undef ycc_rgb_convert_internal
  129. #undef gray_rgb_convert_internal
  130. #undef rgb_rgb_convert_internal
  131. #define RGB_RED EXT_BGRX_RED
  132. #define RGB_GREEN EXT_BGRX_GREEN
  133. #define RGB_BLUE EXT_BGRX_BLUE
  134. #define RGB_ALPHA 3
  135. #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
  136. #define ycc_rgb_convert_internal ycc_extbgrx_convert_internal
  137. #define gray_rgb_convert_internal gray_extbgrx_convert_internal
  138. #define rgb_rgb_convert_internal rgb_extbgrx_convert_internal
  139. #include "jdcolext.c"
  140. #undef RGB_RED
  141. #undef RGB_GREEN
  142. #undef RGB_BLUE
  143. #undef RGB_ALPHA
  144. #undef RGB_PIXELSIZE
  145. #undef ycc_rgb_convert_internal
  146. #undef gray_rgb_convert_internal
  147. #undef rgb_rgb_convert_internal
  148. #define RGB_RED EXT_XBGR_RED
  149. #define RGB_GREEN EXT_XBGR_GREEN
  150. #define RGB_BLUE EXT_XBGR_BLUE
  151. #define RGB_ALPHA 0
  152. #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
  153. #define ycc_rgb_convert_internal ycc_extxbgr_convert_internal
  154. #define gray_rgb_convert_internal gray_extxbgr_convert_internal
  155. #define rgb_rgb_convert_internal rgb_extxbgr_convert_internal
  156. #include "jdcolext.c"
  157. #undef RGB_RED
  158. #undef RGB_GREEN
  159. #undef RGB_BLUE
  160. #undef RGB_ALPHA
  161. #undef RGB_PIXELSIZE
  162. #undef ycc_rgb_convert_internal
  163. #undef gray_rgb_convert_internal
  164. #undef rgb_rgb_convert_internal
  165. #define RGB_RED EXT_XRGB_RED
  166. #define RGB_GREEN EXT_XRGB_GREEN
  167. #define RGB_BLUE EXT_XRGB_BLUE
  168. #define RGB_ALPHA 0
  169. #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
  170. #define ycc_rgb_convert_internal ycc_extxrgb_convert_internal
  171. #define gray_rgb_convert_internal gray_extxrgb_convert_internal
  172. #define rgb_rgb_convert_internal rgb_extxrgb_convert_internal
  173. #include "jdcolext.c"
  174. #undef RGB_RED
  175. #undef RGB_GREEN
  176. #undef RGB_BLUE
  177. #undef RGB_ALPHA
  178. #undef RGB_PIXELSIZE
  179. #undef ycc_rgb_convert_internal
  180. #undef gray_rgb_convert_internal
  181. #undef rgb_rgb_convert_internal
  182. /*
  183. * Initialize tables for YCC->RGB colorspace conversion.
  184. */
  185. LOCAL(void)
  186. build_ycc_rgb_table(j_decompress_ptr cinfo)
  187. {
  188. my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
  189. int i;
  190. JLONG x;
  191. SHIFT_TEMPS
  192. cconvert->Cr_r_tab = (int *)
  193. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  194. (MAXJSAMPLE + 1) * sizeof(int));
  195. cconvert->Cb_b_tab = (int *)
  196. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  197. (MAXJSAMPLE + 1) * sizeof(int));
  198. cconvert->Cr_g_tab = (JLONG *)
  199. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  200. (MAXJSAMPLE + 1) * sizeof(JLONG));
  201. cconvert->Cb_g_tab = (JLONG *)
  202. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  203. (MAXJSAMPLE + 1) * sizeof(JLONG));
  204. for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
  205. /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
  206. /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
  207. /* Cr=>R value is nearest int to 1.40200 * x */
  208. cconvert->Cr_r_tab[i] = (int)
  209. RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
  210. /* Cb=>B value is nearest int to 1.77200 * x */
  211. cconvert->Cb_b_tab[i] = (int)
  212. RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
  213. /* Cr=>G value is scaled-up -0.71414 * x */
  214. cconvert->Cr_g_tab[i] = (-FIX(0.71414)) * x;
  215. /* Cb=>G value is scaled-up -0.34414 * x */
  216. /* We also add in ONE_HALF so that need not do it in inner loop */
  217. cconvert->Cb_g_tab[i] = (-FIX(0.34414)) * x + ONE_HALF;
  218. }
  219. }
  220. /*
  221. * Convert some rows of samples to the output colorspace.
  222. */
  223. METHODDEF(void)
  224. ycc_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  225. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  226. {
  227. switch (cinfo->out_color_space) {
  228. case JCS_EXT_RGB:
  229. ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
  230. num_rows);
  231. break;
  232. case JCS_EXT_RGBX:
  233. case JCS_EXT_RGBA:
  234. ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
  235. num_rows);
  236. break;
  237. case JCS_EXT_BGR:
  238. ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
  239. num_rows);
  240. break;
  241. case JCS_EXT_BGRX:
  242. case JCS_EXT_BGRA:
  243. ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
  244. num_rows);
  245. break;
  246. case JCS_EXT_XBGR:
  247. case JCS_EXT_ABGR:
  248. ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
  249. num_rows);
  250. break;
  251. case JCS_EXT_XRGB:
  252. case JCS_EXT_ARGB:
  253. ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
  254. num_rows);
  255. break;
  256. default:
  257. ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
  258. num_rows);
  259. break;
  260. }
  261. }
  262. /**************** Cases other than YCbCr -> RGB **************/
  263. /*
  264. * Initialize for RGB->grayscale colorspace conversion.
  265. */
  266. LOCAL(void)
  267. build_rgb_y_table(j_decompress_ptr cinfo)
  268. {
  269. my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
  270. JLONG *rgb_y_tab;
  271. JLONG i;
  272. /* Allocate and fill in the conversion tables. */
  273. cconvert->rgb_y_tab = rgb_y_tab = (JLONG *)
  274. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  275. (TABLE_SIZE * sizeof(JLONG)));
  276. for (i = 0; i <= MAXJSAMPLE; i++) {
  277. rgb_y_tab[i + R_Y_OFF] = FIX(0.29900) * i;
  278. rgb_y_tab[i + G_Y_OFF] = FIX(0.58700) * i;
  279. rgb_y_tab[i + B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
  280. }
  281. }
  282. /*
  283. * Convert RGB to grayscale.
  284. */
  285. METHODDEF(void)
  286. rgb_gray_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  287. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  288. {
  289. my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
  290. register int r, g, b;
  291. register JLONG *ctab = cconvert->rgb_y_tab;
  292. register JSAMPROW outptr;
  293. register JSAMPROW inptr0, inptr1, inptr2;
  294. register JDIMENSION col;
  295. JDIMENSION num_cols = cinfo->output_width;
  296. while (--num_rows >= 0) {
  297. inptr0 = input_buf[0][input_row];
  298. inptr1 = input_buf[1][input_row];
  299. inptr2 = input_buf[2][input_row];
  300. input_row++;
  301. outptr = *output_buf++;
  302. for (col = 0; col < num_cols; col++) {
  303. r = GETJSAMPLE(inptr0[col]);
  304. g = GETJSAMPLE(inptr1[col]);
  305. b = GETJSAMPLE(inptr2[col]);
  306. /* Y */
  307. outptr[col] = (JSAMPLE)((ctab[r + R_Y_OFF] + ctab[g + G_Y_OFF] +
  308. ctab[b + B_Y_OFF]) >> SCALEBITS);
  309. }
  310. }
  311. }
  312. /*
  313. * Color conversion for no colorspace change: just copy the data,
  314. * converting from separate-planes to interleaved representation.
  315. */
  316. METHODDEF(void)
  317. null_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  318. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  319. {
  320. register JSAMPROW inptr, inptr0, inptr1, inptr2, inptr3, outptr;
  321. register JDIMENSION col;
  322. register int num_components = cinfo->num_components;
  323. JDIMENSION num_cols = cinfo->output_width;
  324. int ci;
  325. if (num_components == 3) {
  326. while (--num_rows >= 0) {
  327. inptr0 = input_buf[0][input_row];
  328. inptr1 = input_buf[1][input_row];
  329. inptr2 = input_buf[2][input_row];
  330. input_row++;
  331. outptr = *output_buf++;
  332. for (col = 0; col < num_cols; col++) {
  333. *outptr++ = inptr0[col];
  334. *outptr++ = inptr1[col];
  335. *outptr++ = inptr2[col];
  336. }
  337. }
  338. } else if (num_components == 4) {
  339. while (--num_rows >= 0) {
  340. inptr0 = input_buf[0][input_row];
  341. inptr1 = input_buf[1][input_row];
  342. inptr2 = input_buf[2][input_row];
  343. inptr3 = input_buf[3][input_row];
  344. input_row++;
  345. outptr = *output_buf++;
  346. for (col = 0; col < num_cols; col++) {
  347. *outptr++ = inptr0[col];
  348. *outptr++ = inptr1[col];
  349. *outptr++ = inptr2[col];
  350. *outptr++ = inptr3[col];
  351. }
  352. }
  353. } else {
  354. while (--num_rows >= 0) {
  355. for (ci = 0; ci < num_components; ci++) {
  356. inptr = input_buf[ci][input_row];
  357. outptr = *output_buf;
  358. for (col = 0; col < num_cols; col++) {
  359. outptr[ci] = inptr[col];
  360. outptr += num_components;
  361. }
  362. }
  363. output_buf++;
  364. input_row++;
  365. }
  366. }
  367. }
  368. /*
  369. * Color conversion for grayscale: just copy the data.
  370. * This also works for YCbCr -> grayscale conversion, in which
  371. * we just copy the Y (luminance) component and ignore chrominance.
  372. */
  373. METHODDEF(void)
  374. grayscale_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  375. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  376. {
  377. jcopy_sample_rows(input_buf[0], (int)input_row, output_buf, 0, num_rows,
  378. cinfo->output_width);
  379. }
  380. /*
  381. * Convert grayscale to RGB
  382. */
  383. METHODDEF(void)
  384. gray_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  385. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  386. {
  387. switch (cinfo->out_color_space) {
  388. case JCS_EXT_RGB:
  389. gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
  390. num_rows);
  391. break;
  392. case JCS_EXT_RGBX:
  393. case JCS_EXT_RGBA:
  394. gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
  395. num_rows);
  396. break;
  397. case JCS_EXT_BGR:
  398. gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
  399. num_rows);
  400. break;
  401. case JCS_EXT_BGRX:
  402. case JCS_EXT_BGRA:
  403. gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
  404. num_rows);
  405. break;
  406. case JCS_EXT_XBGR:
  407. case JCS_EXT_ABGR:
  408. gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
  409. num_rows);
  410. break;
  411. case JCS_EXT_XRGB:
  412. case JCS_EXT_ARGB:
  413. gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
  414. num_rows);
  415. break;
  416. default:
  417. gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
  418. num_rows);
  419. break;
  420. }
  421. }
  422. /*
  423. * Convert plain RGB to extended RGB
  424. */
  425. METHODDEF(void)
  426. rgb_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  427. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  428. {
  429. switch (cinfo->out_color_space) {
  430. case JCS_EXT_RGB:
  431. rgb_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
  432. num_rows);
  433. break;
  434. case JCS_EXT_RGBX:
  435. case JCS_EXT_RGBA:
  436. rgb_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
  437. num_rows);
  438. break;
  439. case JCS_EXT_BGR:
  440. rgb_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
  441. num_rows);
  442. break;
  443. case JCS_EXT_BGRX:
  444. case JCS_EXT_BGRA:
  445. rgb_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
  446. num_rows);
  447. break;
  448. case JCS_EXT_XBGR:
  449. case JCS_EXT_ABGR:
  450. rgb_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
  451. num_rows);
  452. break;
  453. case JCS_EXT_XRGB:
  454. case JCS_EXT_ARGB:
  455. rgb_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
  456. num_rows);
  457. break;
  458. default:
  459. rgb_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
  460. num_rows);
  461. break;
  462. }
  463. }
  464. /*
  465. * Adobe-style YCCK->CMYK conversion.
  466. * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
  467. * conversion as above, while passing K (black) unchanged.
  468. * We assume build_ycc_rgb_table has been called.
  469. */
  470. METHODDEF(void)
  471. ycck_cmyk_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  472. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  473. {
  474. my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
  475. register int y, cb, cr;
  476. register JSAMPROW outptr;
  477. register JSAMPROW inptr0, inptr1, inptr2, inptr3;
  478. register JDIMENSION col;
  479. JDIMENSION num_cols = cinfo->output_width;
  480. /* copy these pointers into registers if possible */
  481. register JSAMPLE *range_limit = cinfo->sample_range_limit;
  482. register int *Crrtab = cconvert->Cr_r_tab;
  483. register int *Cbbtab = cconvert->Cb_b_tab;
  484. register JLONG *Crgtab = cconvert->Cr_g_tab;
  485. register JLONG *Cbgtab = cconvert->Cb_g_tab;
  486. SHIFT_TEMPS
  487. while (--num_rows >= 0) {
  488. inptr0 = input_buf[0][input_row];
  489. inptr1 = input_buf[1][input_row];
  490. inptr2 = input_buf[2][input_row];
  491. inptr3 = input_buf[3][input_row];
  492. input_row++;
  493. outptr = *output_buf++;
  494. for (col = 0; col < num_cols; col++) {
  495. y = GETJSAMPLE(inptr0[col]);
  496. cb = GETJSAMPLE(inptr1[col]);
  497. cr = GETJSAMPLE(inptr2[col]);
  498. /* Range-limiting is essential due to noise introduced by DCT losses. */
  499. outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
  500. outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
  501. ((int)RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
  502. SCALEBITS)))];
  503. outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
  504. /* K passes through unchanged */
  505. outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
  506. outptr += 4;
  507. }
  508. }
  509. }
  510. /*
  511. * RGB565 conversion
  512. */
  513. #define PACK_SHORT_565_LE(r, g, b) ((((r) << 8) & 0xF800) | \
  514. (((g) << 3) & 0x7E0) | ((b) >> 3))
  515. #define PACK_SHORT_565_BE(r, g, b) (((r) & 0xF8) | ((g) >> 5) | \
  516. (((g) << 11) & 0xE000) | \
  517. (((b) << 5) & 0x1F00))
  518. #define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l)
  519. #define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r)
  520. #define PACK_NEED_ALIGNMENT(ptr) (((size_t)(ptr)) & 3)
  521. #define WRITE_TWO_ALIGNED_PIXELS(addr, pixels) ((*(int *)(addr)) = pixels)
  522. #define DITHER_565_R(r, dither) ((r) + ((dither) & 0xFF))
  523. #define DITHER_565_G(g, dither) ((g) + (((dither) & 0xFF) >> 1))
  524. #define DITHER_565_B(b, dither) ((b) + ((dither) & 0xFF))
  525. /* Declarations for ordered dithering
  526. *
  527. * We use a 4x4 ordered dither array packed into 32 bits. This array is
  528. * sufficent for dithering RGB888 to RGB565.
  529. */
  530. #define DITHER_MASK 0x3
  531. #define DITHER_ROTATE(x) ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
  532. static const JLONG dither_matrix[4] = {
  533. 0x0008020A,
  534. 0x0C040E06,
  535. 0x030B0109,
  536. 0x0F070D05
  537. };
  538. static INLINE boolean is_big_endian(void)
  539. {
  540. int test_value = 1;
  541. if (*(char *)&test_value != 1)
  542. return TRUE;
  543. return FALSE;
  544. }
  545. /* Include inline routines for RGB565 conversion */
  546. #define PACK_SHORT_565 PACK_SHORT_565_LE
  547. #define PACK_TWO_PIXELS PACK_TWO_PIXELS_LE
  548. #define ycc_rgb565_convert_internal ycc_rgb565_convert_le
  549. #define ycc_rgb565D_convert_internal ycc_rgb565D_convert_le
  550. #define rgb_rgb565_convert_internal rgb_rgb565_convert_le
  551. #define rgb_rgb565D_convert_internal rgb_rgb565D_convert_le
  552. #define gray_rgb565_convert_internal gray_rgb565_convert_le
  553. #define gray_rgb565D_convert_internal gray_rgb565D_convert_le
  554. #include "jdcol565.c"
  555. #undef PACK_SHORT_565
  556. #undef PACK_TWO_PIXELS
  557. #undef ycc_rgb565_convert_internal
  558. #undef ycc_rgb565D_convert_internal
  559. #undef rgb_rgb565_convert_internal
  560. #undef rgb_rgb565D_convert_internal
  561. #undef gray_rgb565_convert_internal
  562. #undef gray_rgb565D_convert_internal
  563. #define PACK_SHORT_565 PACK_SHORT_565_BE
  564. #define PACK_TWO_PIXELS PACK_TWO_PIXELS_BE
  565. #define ycc_rgb565_convert_internal ycc_rgb565_convert_be
  566. #define ycc_rgb565D_convert_internal ycc_rgb565D_convert_be
  567. #define rgb_rgb565_convert_internal rgb_rgb565_convert_be
  568. #define rgb_rgb565D_convert_internal rgb_rgb565D_convert_be
  569. #define gray_rgb565_convert_internal gray_rgb565_convert_be
  570. #define gray_rgb565D_convert_internal gray_rgb565D_convert_be
  571. #include "jdcol565.c"
  572. #undef PACK_SHORT_565
  573. #undef PACK_TWO_PIXELS
  574. #undef ycc_rgb565_convert_internal
  575. #undef ycc_rgb565D_convert_internal
  576. #undef rgb_rgb565_convert_internal
  577. #undef rgb_rgb565D_convert_internal
  578. #undef gray_rgb565_convert_internal
  579. #undef gray_rgb565D_convert_internal
  580. METHODDEF(void)
  581. ycc_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  582. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  583. {
  584. if (is_big_endian())
  585. ycc_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
  586. else
  587. ycc_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
  588. }
  589. METHODDEF(void)
  590. ycc_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  591. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  592. {
  593. if (is_big_endian())
  594. ycc_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
  595. else
  596. ycc_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
  597. }
  598. METHODDEF(void)
  599. rgb_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  600. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  601. {
  602. if (is_big_endian())
  603. rgb_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
  604. else
  605. rgb_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
  606. }
  607. METHODDEF(void)
  608. rgb_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  609. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  610. {
  611. if (is_big_endian())
  612. rgb_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
  613. else
  614. rgb_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
  615. }
  616. METHODDEF(void)
  617. gray_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  618. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  619. {
  620. if (is_big_endian())
  621. gray_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
  622. else
  623. gray_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
  624. }
  625. METHODDEF(void)
  626. gray_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  627. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  628. {
  629. if (is_big_endian())
  630. gray_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
  631. else
  632. gray_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
  633. }
  634. /*
  635. * Empty method for start_pass.
  636. */
  637. METHODDEF(void)
  638. start_pass_dcolor(j_decompress_ptr cinfo)
  639. {
  640. /* no work needed */
  641. }
  642. /*
  643. * Module initialization routine for output colorspace conversion.
  644. */
  645. GLOBAL(void)
  646. jinit_color_deconverter(j_decompress_ptr cinfo)
  647. {
  648. my_cconvert_ptr cconvert;
  649. int ci;
  650. cconvert = (my_cconvert_ptr)
  651. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  652. sizeof(my_color_deconverter));
  653. cinfo->cconvert = (struct jpeg_color_deconverter *)cconvert;
  654. cconvert->pub.start_pass = start_pass_dcolor;
  655. /* Make sure num_components agrees with jpeg_color_space */
  656. switch (cinfo->jpeg_color_space) {
  657. case JCS_GRAYSCALE:
  658. if (cinfo->num_components != 1)
  659. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  660. break;
  661. case JCS_RGB:
  662. case JCS_YCbCr:
  663. if (cinfo->num_components != 3)
  664. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  665. break;
  666. case JCS_CMYK:
  667. case JCS_YCCK:
  668. if (cinfo->num_components != 4)
  669. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  670. break;
  671. default: /* JCS_UNKNOWN can be anything */
  672. if (cinfo->num_components < 1)
  673. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  674. break;
  675. }
  676. /* Set out_color_components and conversion method based on requested space.
  677. * Also clear the component_needed flags for any unused components,
  678. * so that earlier pipeline stages can avoid useless computation.
  679. */
  680. switch (cinfo->out_color_space) {
  681. case JCS_GRAYSCALE:
  682. cinfo->out_color_components = 1;
  683. if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
  684. cinfo->jpeg_color_space == JCS_YCbCr) {
  685. cconvert->pub.color_convert = grayscale_convert;
  686. /* For color->grayscale conversion, only the Y (0) component is needed */
  687. for (ci = 1; ci < cinfo->num_components; ci++)
  688. cinfo->comp_info[ci].component_needed = FALSE;
  689. } else if (cinfo->jpeg_color_space == JCS_RGB) {
  690. cconvert->pub.color_convert = rgb_gray_convert;
  691. build_rgb_y_table(cinfo);
  692. } else
  693. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  694. break;
  695. case JCS_RGB:
  696. case JCS_EXT_RGB:
  697. case JCS_EXT_RGBX:
  698. case JCS_EXT_BGR:
  699. case JCS_EXT_BGRX:
  700. case JCS_EXT_XBGR:
  701. case JCS_EXT_XRGB:
  702. case JCS_EXT_RGBA:
  703. case JCS_EXT_BGRA:
  704. case JCS_EXT_ABGR:
  705. case JCS_EXT_ARGB:
  706. cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
  707. if (cinfo->jpeg_color_space == JCS_YCbCr) {
  708. if (jsimd_can_ycc_rgb())
  709. cconvert->pub.color_convert = jsimd_ycc_rgb_convert;
  710. else {
  711. cconvert->pub.color_convert = ycc_rgb_convert;
  712. build_ycc_rgb_table(cinfo);
  713. }
  714. } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
  715. cconvert->pub.color_convert = gray_rgb_convert;
  716. } else if (cinfo->jpeg_color_space == JCS_RGB) {
  717. if (rgb_red[cinfo->out_color_space] == 0 &&
  718. rgb_green[cinfo->out_color_space] == 1 &&
  719. rgb_blue[cinfo->out_color_space] == 2 &&
  720. rgb_pixelsize[cinfo->out_color_space] == 3)
  721. cconvert->pub.color_convert = null_convert;
  722. else
  723. cconvert->pub.color_convert = rgb_rgb_convert;
  724. } else
  725. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  726. break;
  727. case JCS_RGB565:
  728. cinfo->out_color_components = 3;
  729. if (cinfo->dither_mode == JDITHER_NONE) {
  730. if (cinfo->jpeg_color_space == JCS_YCbCr) {
  731. if (jsimd_can_ycc_rgb565())
  732. cconvert->pub.color_convert = jsimd_ycc_rgb565_convert;
  733. else {
  734. cconvert->pub.color_convert = ycc_rgb565_convert;
  735. build_ycc_rgb_table(cinfo);
  736. }
  737. } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
  738. cconvert->pub.color_convert = gray_rgb565_convert;
  739. } else if (cinfo->jpeg_color_space == JCS_RGB) {
  740. cconvert->pub.color_convert = rgb_rgb565_convert;
  741. } else
  742. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  743. } else {
  744. /* only ordered dithering is supported */
  745. if (cinfo->jpeg_color_space == JCS_YCbCr) {
  746. cconvert->pub.color_convert = ycc_rgb565D_convert;
  747. build_ycc_rgb_table(cinfo);
  748. } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
  749. cconvert->pub.color_convert = gray_rgb565D_convert;
  750. } else if (cinfo->jpeg_color_space == JCS_RGB) {
  751. cconvert->pub.color_convert = rgb_rgb565D_convert;
  752. } else
  753. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  754. }
  755. break;
  756. case JCS_CMYK:
  757. cinfo->out_color_components = 4;
  758. if (cinfo->jpeg_color_space == JCS_YCCK) {
  759. cconvert->pub.color_convert = ycck_cmyk_convert;
  760. build_ycc_rgb_table(cinfo);
  761. } else if (cinfo->jpeg_color_space == JCS_CMYK) {
  762. cconvert->pub.color_convert = null_convert;
  763. } else
  764. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  765. break;
  766. default:
  767. /* Permit null conversion to same output space */
  768. if (cinfo->out_color_space == cinfo->jpeg_color_space) {
  769. cinfo->out_color_components = cinfo->num_components;
  770. cconvert->pub.color_convert = null_convert;
  771. } else /* unsupported non-null conversion */
  772. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  773. break;
  774. }
  775. if (cinfo->quantize_colors)
  776. cinfo->output_components = 1; /* single colormapped output component */
  777. else
  778. cinfo->output_components = cinfo->out_color_components;
  779. }