jdcolor.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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 = inptr0[col];
  304. g = inptr1[col];
  305. b = 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 = inptr0[col];
  496. cb = inptr1[col];
  497. cr = 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];
  506. outptr += 4;
  507. }
  508. }
  509. }
  510. /*
  511. * RGB565 conversion
  512. */
  513. #define PACK_SHORT_565_LE(r, g, b) \
  514. ((((r) << 8) & 0xF800) | (((g) << 3) & 0x7E0) | ((b) >> 3))
  515. #define PACK_SHORT_565_BE(r, g, b) \
  516. (((r) & 0xF8) | ((g) >> 5) | (((g) << 11) & 0xE000) | (((b) << 5) & 0x1F00))
  517. #define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l)
  518. #define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r)
  519. #define PACK_NEED_ALIGNMENT(ptr) (((size_t)(ptr)) & 3)
  520. #define WRITE_TWO_ALIGNED_PIXELS(addr, pixels) ((*(int *)(addr)) = pixels)
  521. #define DITHER_565_R(r, dither) ((r) + ((dither) & 0xFF))
  522. #define DITHER_565_G(g, dither) ((g) + (((dither) & 0xFF) >> 1))
  523. #define DITHER_565_B(b, dither) ((b) + ((dither) & 0xFF))
  524. /* Declarations for ordered dithering
  525. *
  526. * We use a 4x4 ordered dither array packed into 32 bits. This array is
  527. * sufficient for dithering RGB888 to RGB565.
  528. */
  529. #define DITHER_MASK 0x3
  530. #define DITHER_ROTATE(x) ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
  531. static const JLONG dither_matrix[4] = {
  532. 0x0008020A,
  533. 0x0C040E06,
  534. 0x030B0109,
  535. 0x0F070D05
  536. };
  537. static INLINE boolean is_big_endian(void)
  538. {
  539. int test_value = 1;
  540. if (*(char *)&test_value != 1)
  541. return TRUE;
  542. return FALSE;
  543. }
  544. /* Include inline routines for RGB565 conversion */
  545. #define PACK_SHORT_565 PACK_SHORT_565_LE
  546. #define PACK_TWO_PIXELS PACK_TWO_PIXELS_LE
  547. #define ycc_rgb565_convert_internal ycc_rgb565_convert_le
  548. #define ycc_rgb565D_convert_internal ycc_rgb565D_convert_le
  549. #define rgb_rgb565_convert_internal rgb_rgb565_convert_le
  550. #define rgb_rgb565D_convert_internal rgb_rgb565D_convert_le
  551. #define gray_rgb565_convert_internal gray_rgb565_convert_le
  552. #define gray_rgb565D_convert_internal gray_rgb565D_convert_le
  553. #include "jdcol565.c"
  554. #undef PACK_SHORT_565
  555. #undef PACK_TWO_PIXELS
  556. #undef ycc_rgb565_convert_internal
  557. #undef ycc_rgb565D_convert_internal
  558. #undef rgb_rgb565_convert_internal
  559. #undef rgb_rgb565D_convert_internal
  560. #undef gray_rgb565_convert_internal
  561. #undef gray_rgb565D_convert_internal
  562. #define PACK_SHORT_565 PACK_SHORT_565_BE
  563. #define PACK_TWO_PIXELS PACK_TWO_PIXELS_BE
  564. #define ycc_rgb565_convert_internal ycc_rgb565_convert_be
  565. #define ycc_rgb565D_convert_internal ycc_rgb565D_convert_be
  566. #define rgb_rgb565_convert_internal rgb_rgb565_convert_be
  567. #define rgb_rgb565D_convert_internal rgb_rgb565D_convert_be
  568. #define gray_rgb565_convert_internal gray_rgb565_convert_be
  569. #define gray_rgb565D_convert_internal gray_rgb565D_convert_be
  570. #include "jdcol565.c"
  571. #undef PACK_SHORT_565
  572. #undef PACK_TWO_PIXELS
  573. #undef ycc_rgb565_convert_internal
  574. #undef ycc_rgb565D_convert_internal
  575. #undef rgb_rgb565_convert_internal
  576. #undef rgb_rgb565D_convert_internal
  577. #undef gray_rgb565_convert_internal
  578. #undef gray_rgb565D_convert_internal
  579. METHODDEF(void)
  580. ycc_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  581. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  582. {
  583. if (is_big_endian())
  584. ycc_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
  585. else
  586. ycc_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
  587. }
  588. METHODDEF(void)
  589. ycc_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  590. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  591. {
  592. if (is_big_endian())
  593. ycc_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
  594. else
  595. ycc_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
  596. }
  597. METHODDEF(void)
  598. rgb_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  599. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  600. {
  601. if (is_big_endian())
  602. rgb_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
  603. else
  604. rgb_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
  605. }
  606. METHODDEF(void)
  607. rgb_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  608. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  609. {
  610. if (is_big_endian())
  611. rgb_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
  612. else
  613. rgb_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
  614. }
  615. METHODDEF(void)
  616. gray_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  617. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  618. {
  619. if (is_big_endian())
  620. gray_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
  621. else
  622. gray_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
  623. }
  624. METHODDEF(void)
  625. gray_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  626. JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
  627. {
  628. if (is_big_endian())
  629. gray_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
  630. else
  631. gray_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
  632. }
  633. /*
  634. * Empty method for start_pass.
  635. */
  636. METHODDEF(void)
  637. start_pass_dcolor(j_decompress_ptr cinfo)
  638. {
  639. /* no work needed */
  640. }
  641. /*
  642. * Module initialization routine for output colorspace conversion.
  643. */
  644. GLOBAL(void)
  645. jinit_color_deconverter(j_decompress_ptr cinfo)
  646. {
  647. my_cconvert_ptr cconvert;
  648. int ci;
  649. cconvert = (my_cconvert_ptr)
  650. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  651. sizeof(my_color_deconverter));
  652. cinfo->cconvert = (struct jpeg_color_deconverter *)cconvert;
  653. cconvert->pub.start_pass = start_pass_dcolor;
  654. /* Make sure num_components agrees with jpeg_color_space */
  655. switch (cinfo->jpeg_color_space) {
  656. case JCS_GRAYSCALE:
  657. if (cinfo->num_components != 1)
  658. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  659. break;
  660. case JCS_RGB:
  661. case JCS_YCbCr:
  662. if (cinfo->num_components != 3)
  663. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  664. break;
  665. case JCS_CMYK:
  666. case JCS_YCCK:
  667. if (cinfo->num_components != 4)
  668. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  669. break;
  670. default: /* JCS_UNKNOWN can be anything */
  671. if (cinfo->num_components < 1)
  672. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  673. break;
  674. }
  675. /* Set out_color_components and conversion method based on requested space.
  676. * Also clear the component_needed flags for any unused components,
  677. * so that earlier pipeline stages can avoid useless computation.
  678. */
  679. switch (cinfo->out_color_space) {
  680. case JCS_GRAYSCALE:
  681. cinfo->out_color_components = 1;
  682. if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
  683. cinfo->jpeg_color_space == JCS_YCbCr) {
  684. cconvert->pub.color_convert = grayscale_convert;
  685. /* For color->grayscale conversion, only the Y (0) component is needed */
  686. for (ci = 1; ci < cinfo->num_components; ci++)
  687. cinfo->comp_info[ci].component_needed = FALSE;
  688. } else if (cinfo->jpeg_color_space == JCS_RGB) {
  689. cconvert->pub.color_convert = rgb_gray_convert;
  690. build_rgb_y_table(cinfo);
  691. } else
  692. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  693. break;
  694. case JCS_RGB:
  695. case JCS_EXT_RGB:
  696. case JCS_EXT_RGBX:
  697. case JCS_EXT_BGR:
  698. case JCS_EXT_BGRX:
  699. case JCS_EXT_XBGR:
  700. case JCS_EXT_XRGB:
  701. case JCS_EXT_RGBA:
  702. case JCS_EXT_BGRA:
  703. case JCS_EXT_ABGR:
  704. case JCS_EXT_ARGB:
  705. cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
  706. if (cinfo->jpeg_color_space == JCS_YCbCr) {
  707. if (jsimd_can_ycc_rgb())
  708. cconvert->pub.color_convert = jsimd_ycc_rgb_convert;
  709. else {
  710. cconvert->pub.color_convert = ycc_rgb_convert;
  711. build_ycc_rgb_table(cinfo);
  712. }
  713. } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
  714. cconvert->pub.color_convert = gray_rgb_convert;
  715. } else if (cinfo->jpeg_color_space == JCS_RGB) {
  716. if (rgb_red[cinfo->out_color_space] == 0 &&
  717. rgb_green[cinfo->out_color_space] == 1 &&
  718. rgb_blue[cinfo->out_color_space] == 2 &&
  719. rgb_pixelsize[cinfo->out_color_space] == 3)
  720. cconvert->pub.color_convert = null_convert;
  721. else
  722. cconvert->pub.color_convert = rgb_rgb_convert;
  723. } else
  724. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  725. break;
  726. case JCS_RGB565:
  727. cinfo->out_color_components = 3;
  728. if (cinfo->dither_mode == JDITHER_NONE) {
  729. if (cinfo->jpeg_color_space == JCS_YCbCr) {
  730. if (jsimd_can_ycc_rgb565())
  731. cconvert->pub.color_convert = jsimd_ycc_rgb565_convert;
  732. else {
  733. cconvert->pub.color_convert = ycc_rgb565_convert;
  734. build_ycc_rgb_table(cinfo);
  735. }
  736. } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
  737. cconvert->pub.color_convert = gray_rgb565_convert;
  738. } else if (cinfo->jpeg_color_space == JCS_RGB) {
  739. cconvert->pub.color_convert = rgb_rgb565_convert;
  740. } else
  741. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  742. } else {
  743. /* only ordered dithering is supported */
  744. if (cinfo->jpeg_color_space == JCS_YCbCr) {
  745. cconvert->pub.color_convert = ycc_rgb565D_convert;
  746. build_ycc_rgb_table(cinfo);
  747. } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
  748. cconvert->pub.color_convert = gray_rgb565D_convert;
  749. } else if (cinfo->jpeg_color_space == JCS_RGB) {
  750. cconvert->pub.color_convert = rgb_rgb565D_convert;
  751. } else
  752. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  753. }
  754. break;
  755. case JCS_CMYK:
  756. cinfo->out_color_components = 4;
  757. if (cinfo->jpeg_color_space == JCS_YCCK) {
  758. cconvert->pub.color_convert = ycck_cmyk_convert;
  759. build_ycc_rgb_table(cinfo);
  760. } else if (cinfo->jpeg_color_space == JCS_CMYK) {
  761. cconvert->pub.color_convert = null_convert;
  762. } else
  763. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  764. break;
  765. default:
  766. /* Permit null conversion to same output space */
  767. if (cinfo->out_color_space == cinfo->jpeg_color_space) {
  768. cinfo->out_color_components = cinfo->num_components;
  769. cconvert->pub.color_convert = null_convert;
  770. } else /* unsupported non-null conversion */
  771. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  772. break;
  773. }
  774. if (cinfo->quantize_colors)
  775. cinfo->output_components = 1; /* single colormapped output component */
  776. else
  777. cinfo->output_components = cinfo->out_color_components;
  778. }