jdcolor.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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,
  225. JSAMPIMAGE input_buf, JDIMENSION input_row,
  226. JSAMPARRAY output_buf, int num_rows)
  227. {
  228. switch (cinfo->out_color_space) {
  229. case JCS_EXT_RGB:
  230. ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
  231. num_rows);
  232. break;
  233. case JCS_EXT_RGBX:
  234. case JCS_EXT_RGBA:
  235. ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
  236. num_rows);
  237. break;
  238. case JCS_EXT_BGR:
  239. ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
  240. num_rows);
  241. break;
  242. case JCS_EXT_BGRX:
  243. case JCS_EXT_BGRA:
  244. ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
  245. num_rows);
  246. break;
  247. case JCS_EXT_XBGR:
  248. case JCS_EXT_ABGR:
  249. ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
  250. num_rows);
  251. break;
  252. case JCS_EXT_XRGB:
  253. case JCS_EXT_ARGB:
  254. ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
  255. num_rows);
  256. break;
  257. default:
  258. ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
  259. num_rows);
  260. break;
  261. }
  262. }
  263. /**************** Cases other than YCbCr -> RGB **************/
  264. /*
  265. * Initialize for RGB->grayscale colorspace conversion.
  266. */
  267. LOCAL(void)
  268. build_rgb_y_table (j_decompress_ptr cinfo)
  269. {
  270. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  271. JLONG *rgb_y_tab;
  272. JLONG i;
  273. /* Allocate and fill in the conversion tables. */
  274. cconvert->rgb_y_tab = rgb_y_tab = (JLONG *)
  275. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  276. (TABLE_SIZE * sizeof(JLONG)));
  277. for (i = 0; i <= MAXJSAMPLE; i++) {
  278. rgb_y_tab[i+R_Y_OFF] = FIX(0.29900) * i;
  279. rgb_y_tab[i+G_Y_OFF] = FIX(0.58700) * i;
  280. rgb_y_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
  281. }
  282. }
  283. /*
  284. * Convert RGB to grayscale.
  285. */
  286. METHODDEF(void)
  287. rgb_gray_convert (j_decompress_ptr cinfo,
  288. JSAMPIMAGE input_buf, JDIMENSION input_row,
  289. JSAMPARRAY output_buf, int num_rows)
  290. {
  291. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  292. register int r, g, b;
  293. register JLONG *ctab = cconvert->rgb_y_tab;
  294. register JSAMPROW outptr;
  295. register JSAMPROW inptr0, inptr1, inptr2;
  296. register JDIMENSION col;
  297. JDIMENSION num_cols = cinfo->output_width;
  298. while (--num_rows >= 0) {
  299. inptr0 = input_buf[0][input_row];
  300. inptr1 = input_buf[1][input_row];
  301. inptr2 = input_buf[2][input_row];
  302. input_row++;
  303. outptr = *output_buf++;
  304. for (col = 0; col < num_cols; col++) {
  305. r = GETJSAMPLE(inptr0[col]);
  306. g = GETJSAMPLE(inptr1[col]);
  307. b = GETJSAMPLE(inptr2[col]);
  308. /* Y */
  309. outptr[col] = (JSAMPLE)
  310. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  311. >> SCALEBITS);
  312. }
  313. }
  314. }
  315. /*
  316. * Color conversion for no colorspace change: just copy the data,
  317. * converting from separate-planes to interleaved representation.
  318. */
  319. METHODDEF(void)
  320. null_convert (j_decompress_ptr cinfo,
  321. JSAMPIMAGE input_buf, JDIMENSION input_row,
  322. JSAMPARRAY output_buf, int num_rows)
  323. {
  324. register JSAMPROW inptr, inptr0, inptr1, inptr2, inptr3, outptr;
  325. register JDIMENSION col;
  326. register int num_components = cinfo->num_components;
  327. JDIMENSION num_cols = cinfo->output_width;
  328. int ci;
  329. if (num_components == 3) {
  330. while (--num_rows >= 0) {
  331. inptr0 = input_buf[0][input_row];
  332. inptr1 = input_buf[1][input_row];
  333. inptr2 = input_buf[2][input_row];
  334. input_row++;
  335. outptr = *output_buf++;
  336. for (col = 0; col < num_cols; col++) {
  337. *outptr++ = inptr0[col];
  338. *outptr++ = inptr1[col];
  339. *outptr++ = inptr2[col];
  340. }
  341. }
  342. } else if (num_components == 4) {
  343. while (--num_rows >= 0) {
  344. inptr0 = input_buf[0][input_row];
  345. inptr1 = input_buf[1][input_row];
  346. inptr2 = input_buf[2][input_row];
  347. inptr3 = input_buf[3][input_row];
  348. input_row++;
  349. outptr = *output_buf++;
  350. for (col = 0; col < num_cols; col++) {
  351. *outptr++ = inptr0[col];
  352. *outptr++ = inptr1[col];
  353. *outptr++ = inptr2[col];
  354. *outptr++ = inptr3[col];
  355. }
  356. }
  357. } else {
  358. while (--num_rows >= 0) {
  359. for (ci = 0; ci < num_components; ci++) {
  360. inptr = input_buf[ci][input_row];
  361. outptr = *output_buf;
  362. for (col = 0; col < num_cols; col++) {
  363. outptr[ci] = inptr[col];
  364. outptr += num_components;
  365. }
  366. }
  367. output_buf++;
  368. input_row++;
  369. }
  370. }
  371. }
  372. /*
  373. * Color conversion for grayscale: just copy the data.
  374. * This also works for YCbCr -> grayscale conversion, in which
  375. * we just copy the Y (luminance) component and ignore chrominance.
  376. */
  377. METHODDEF(void)
  378. grayscale_convert (j_decompress_ptr cinfo,
  379. JSAMPIMAGE input_buf, JDIMENSION input_row,
  380. JSAMPARRAY output_buf, int num_rows)
  381. {
  382. jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
  383. num_rows, cinfo->output_width);
  384. }
  385. /*
  386. * Convert grayscale to RGB
  387. */
  388. METHODDEF(void)
  389. gray_rgb_convert (j_decompress_ptr cinfo,
  390. JSAMPIMAGE input_buf, JDIMENSION input_row,
  391. JSAMPARRAY output_buf, int num_rows)
  392. {
  393. switch (cinfo->out_color_space) {
  394. case JCS_EXT_RGB:
  395. gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
  396. num_rows);
  397. break;
  398. case JCS_EXT_RGBX:
  399. case JCS_EXT_RGBA:
  400. gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
  401. num_rows);
  402. break;
  403. case JCS_EXT_BGR:
  404. gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
  405. num_rows);
  406. break;
  407. case JCS_EXT_BGRX:
  408. case JCS_EXT_BGRA:
  409. gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
  410. num_rows);
  411. break;
  412. case JCS_EXT_XBGR:
  413. case JCS_EXT_ABGR:
  414. gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
  415. num_rows);
  416. break;
  417. case JCS_EXT_XRGB:
  418. case JCS_EXT_ARGB:
  419. gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
  420. num_rows);
  421. break;
  422. default:
  423. gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
  424. num_rows);
  425. break;
  426. }
  427. }
  428. /*
  429. * Convert plain RGB to extended RGB
  430. */
  431. METHODDEF(void)
  432. rgb_rgb_convert (j_decompress_ptr cinfo,
  433. JSAMPIMAGE input_buf, JDIMENSION input_row,
  434. JSAMPARRAY output_buf, int num_rows)
  435. {
  436. switch (cinfo->out_color_space) {
  437. case JCS_EXT_RGB:
  438. rgb_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
  439. num_rows);
  440. break;
  441. case JCS_EXT_RGBX:
  442. case JCS_EXT_RGBA:
  443. rgb_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
  444. num_rows);
  445. break;
  446. case JCS_EXT_BGR:
  447. rgb_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
  448. num_rows);
  449. break;
  450. case JCS_EXT_BGRX:
  451. case JCS_EXT_BGRA:
  452. rgb_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
  453. num_rows);
  454. break;
  455. case JCS_EXT_XBGR:
  456. case JCS_EXT_ABGR:
  457. rgb_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
  458. num_rows);
  459. break;
  460. case JCS_EXT_XRGB:
  461. case JCS_EXT_ARGB:
  462. rgb_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
  463. num_rows);
  464. break;
  465. default:
  466. rgb_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
  467. num_rows);
  468. break;
  469. }
  470. }
  471. /*
  472. * Adobe-style YCCK->CMYK conversion.
  473. * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
  474. * conversion as above, while passing K (black) unchanged.
  475. * We assume build_ycc_rgb_table has been called.
  476. */
  477. METHODDEF(void)
  478. ycck_cmyk_convert (j_decompress_ptr cinfo,
  479. JSAMPIMAGE input_buf, JDIMENSION input_row,
  480. JSAMPARRAY output_buf, int num_rows)
  481. {
  482. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  483. register int y, cb, cr;
  484. register JSAMPROW outptr;
  485. register JSAMPROW inptr0, inptr1, inptr2, inptr3;
  486. register JDIMENSION col;
  487. JDIMENSION num_cols = cinfo->output_width;
  488. /* copy these pointers into registers if possible */
  489. register JSAMPLE *range_limit = cinfo->sample_range_limit;
  490. register int *Crrtab = cconvert->Cr_r_tab;
  491. register int *Cbbtab = cconvert->Cb_b_tab;
  492. register JLONG *Crgtab = cconvert->Cr_g_tab;
  493. register JLONG *Cbgtab = cconvert->Cb_g_tab;
  494. SHIFT_TEMPS
  495. while (--num_rows >= 0) {
  496. inptr0 = input_buf[0][input_row];
  497. inptr1 = input_buf[1][input_row];
  498. inptr2 = input_buf[2][input_row];
  499. inptr3 = input_buf[3][input_row];
  500. input_row++;
  501. outptr = *output_buf++;
  502. for (col = 0; col < num_cols; col++) {
  503. y = GETJSAMPLE(inptr0[col]);
  504. cb = GETJSAMPLE(inptr1[col]);
  505. cr = GETJSAMPLE(inptr2[col]);
  506. /* Range-limiting is essential due to noise introduced by DCT losses. */
  507. outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
  508. outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
  509. ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
  510. SCALEBITS)))];
  511. outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
  512. /* K passes through unchanged */
  513. outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
  514. outptr += 4;
  515. }
  516. }
  517. }
  518. /*
  519. * RGB565 conversion
  520. */
  521. #define PACK_SHORT_565_LE(r, g, b) ((((r) << 8) & 0xF800) | \
  522. (((g) << 3) & 0x7E0) | ((b) >> 3))
  523. #define PACK_SHORT_565_BE(r, g, b) (((r) & 0xF8) | ((g) >> 5) | \
  524. (((g) << 11) & 0xE000) | \
  525. (((b) << 5) & 0x1F00))
  526. #define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l)
  527. #define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r)
  528. #define PACK_NEED_ALIGNMENT(ptr) (((size_t)(ptr)) & 3)
  529. #define WRITE_TWO_ALIGNED_PIXELS(addr, pixels) ((*(int *)(addr)) = pixels)
  530. #define DITHER_565_R(r, dither) ((r) + ((dither) & 0xFF))
  531. #define DITHER_565_G(g, dither) ((g) + (((dither) & 0xFF) >> 1))
  532. #define DITHER_565_B(b, dither) ((b) + ((dither) & 0xFF))
  533. /* Declarations for ordered dithering
  534. *
  535. * We use a 4x4 ordered dither array packed into 32 bits. This array is
  536. * sufficent for dithering RGB888 to RGB565.
  537. */
  538. #define DITHER_MASK 0x3
  539. #define DITHER_ROTATE(x) ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
  540. static const JLONG dither_matrix[4] = {
  541. 0x0008020A,
  542. 0x0C040E06,
  543. 0x030B0109,
  544. 0x0F070D05
  545. };
  546. static INLINE boolean is_big_endian(void)
  547. {
  548. int test_value = 1;
  549. if(*(char *)&test_value != 1)
  550. return TRUE;
  551. return FALSE;
  552. }
  553. /* Include inline routines for RGB565 conversion */
  554. #define PACK_SHORT_565 PACK_SHORT_565_LE
  555. #define PACK_TWO_PIXELS PACK_TWO_PIXELS_LE
  556. #define ycc_rgb565_convert_internal ycc_rgb565_convert_le
  557. #define ycc_rgb565D_convert_internal ycc_rgb565D_convert_le
  558. #define rgb_rgb565_convert_internal rgb_rgb565_convert_le
  559. #define rgb_rgb565D_convert_internal rgb_rgb565D_convert_le
  560. #define gray_rgb565_convert_internal gray_rgb565_convert_le
  561. #define gray_rgb565D_convert_internal gray_rgb565D_convert_le
  562. #include "jdcol565.c"
  563. #undef PACK_SHORT_565
  564. #undef PACK_TWO_PIXELS
  565. #undef ycc_rgb565_convert_internal
  566. #undef ycc_rgb565D_convert_internal
  567. #undef rgb_rgb565_convert_internal
  568. #undef rgb_rgb565D_convert_internal
  569. #undef gray_rgb565_convert_internal
  570. #undef gray_rgb565D_convert_internal
  571. #define PACK_SHORT_565 PACK_SHORT_565_BE
  572. #define PACK_TWO_PIXELS PACK_TWO_PIXELS_BE
  573. #define ycc_rgb565_convert_internal ycc_rgb565_convert_be
  574. #define ycc_rgb565D_convert_internal ycc_rgb565D_convert_be
  575. #define rgb_rgb565_convert_internal rgb_rgb565_convert_be
  576. #define rgb_rgb565D_convert_internal rgb_rgb565D_convert_be
  577. #define gray_rgb565_convert_internal gray_rgb565_convert_be
  578. #define gray_rgb565D_convert_internal gray_rgb565D_convert_be
  579. #include "jdcol565.c"
  580. #undef PACK_SHORT_565
  581. #undef PACK_TWO_PIXELS
  582. #undef ycc_rgb565_convert_internal
  583. #undef ycc_rgb565D_convert_internal
  584. #undef rgb_rgb565_convert_internal
  585. #undef rgb_rgb565D_convert_internal
  586. #undef gray_rgb565_convert_internal
  587. #undef gray_rgb565D_convert_internal
  588. METHODDEF(void)
  589. ycc_rgb565_convert (j_decompress_ptr cinfo,
  590. JSAMPIMAGE input_buf, JDIMENSION input_row,
  591. JSAMPARRAY output_buf, int num_rows)
  592. {
  593. if (is_big_endian())
  594. ycc_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
  595. else
  596. ycc_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
  597. }
  598. METHODDEF(void)
  599. ycc_rgb565D_convert (j_decompress_ptr cinfo,
  600. JSAMPIMAGE input_buf, JDIMENSION input_row,
  601. JSAMPARRAY output_buf, int num_rows)
  602. {
  603. if (is_big_endian())
  604. ycc_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
  605. else
  606. ycc_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
  607. }
  608. METHODDEF(void)
  609. rgb_rgb565_convert (j_decompress_ptr cinfo,
  610. JSAMPIMAGE input_buf, JDIMENSION input_row,
  611. JSAMPARRAY output_buf, int num_rows)
  612. {
  613. if (is_big_endian())
  614. rgb_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
  615. else
  616. rgb_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
  617. }
  618. METHODDEF(void)
  619. rgb_rgb565D_convert (j_decompress_ptr cinfo,
  620. JSAMPIMAGE input_buf, JDIMENSION input_row,
  621. JSAMPARRAY output_buf, int num_rows)
  622. {
  623. if (is_big_endian())
  624. rgb_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
  625. else
  626. rgb_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
  627. }
  628. METHODDEF(void)
  629. gray_rgb565_convert (j_decompress_ptr cinfo,
  630. JSAMPIMAGE input_buf, JDIMENSION input_row,
  631. JSAMPARRAY output_buf, int num_rows)
  632. {
  633. if (is_big_endian())
  634. gray_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
  635. else
  636. gray_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
  637. }
  638. METHODDEF(void)
  639. gray_rgb565D_convert (j_decompress_ptr cinfo,
  640. JSAMPIMAGE input_buf, JDIMENSION input_row,
  641. JSAMPARRAY output_buf, int num_rows)
  642. {
  643. if (is_big_endian())
  644. gray_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
  645. else
  646. gray_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
  647. }
  648. /*
  649. * Empty method for start_pass.
  650. */
  651. METHODDEF(void)
  652. start_pass_dcolor (j_decompress_ptr cinfo)
  653. {
  654. /* no work needed */
  655. }
  656. /*
  657. * Module initialization routine for output colorspace conversion.
  658. */
  659. GLOBAL(void)
  660. jinit_color_deconverter (j_decompress_ptr cinfo)
  661. {
  662. my_cconvert_ptr cconvert;
  663. int ci;
  664. cconvert = (my_cconvert_ptr)
  665. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  666. sizeof(my_color_deconverter));
  667. cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
  668. cconvert->pub.start_pass = start_pass_dcolor;
  669. /* Make sure num_components agrees with jpeg_color_space */
  670. switch (cinfo->jpeg_color_space) {
  671. case JCS_GRAYSCALE:
  672. if (cinfo->num_components != 1)
  673. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  674. break;
  675. case JCS_RGB:
  676. case JCS_YCbCr:
  677. if (cinfo->num_components != 3)
  678. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  679. break;
  680. case JCS_CMYK:
  681. case JCS_YCCK:
  682. if (cinfo->num_components != 4)
  683. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  684. break;
  685. default: /* JCS_UNKNOWN can be anything */
  686. if (cinfo->num_components < 1)
  687. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  688. break;
  689. }
  690. /* Set out_color_components and conversion method based on requested space.
  691. * Also clear the component_needed flags for any unused components,
  692. * so that earlier pipeline stages can avoid useless computation.
  693. */
  694. switch (cinfo->out_color_space) {
  695. case JCS_GRAYSCALE:
  696. cinfo->out_color_components = 1;
  697. if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
  698. cinfo->jpeg_color_space == JCS_YCbCr) {
  699. cconvert->pub.color_convert = grayscale_convert;
  700. /* For color->grayscale conversion, only the Y (0) component is needed */
  701. for (ci = 1; ci < cinfo->num_components; ci++)
  702. cinfo->comp_info[ci].component_needed = FALSE;
  703. } else if (cinfo->jpeg_color_space == JCS_RGB) {
  704. cconvert->pub.color_convert = rgb_gray_convert;
  705. build_rgb_y_table(cinfo);
  706. } else
  707. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  708. break;
  709. case JCS_RGB:
  710. case JCS_EXT_RGB:
  711. case JCS_EXT_RGBX:
  712. case JCS_EXT_BGR:
  713. case JCS_EXT_BGRX:
  714. case JCS_EXT_XBGR:
  715. case JCS_EXT_XRGB:
  716. case JCS_EXT_RGBA:
  717. case JCS_EXT_BGRA:
  718. case JCS_EXT_ABGR:
  719. case JCS_EXT_ARGB:
  720. cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
  721. if (cinfo->jpeg_color_space == JCS_YCbCr) {
  722. if (jsimd_can_ycc_rgb())
  723. cconvert->pub.color_convert = jsimd_ycc_rgb_convert;
  724. else {
  725. cconvert->pub.color_convert = ycc_rgb_convert;
  726. build_ycc_rgb_table(cinfo);
  727. }
  728. } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
  729. cconvert->pub.color_convert = gray_rgb_convert;
  730. } else if (cinfo->jpeg_color_space == JCS_RGB) {
  731. if (rgb_red[cinfo->out_color_space] == 0 &&
  732. rgb_green[cinfo->out_color_space] == 1 &&
  733. rgb_blue[cinfo->out_color_space] == 2 &&
  734. rgb_pixelsize[cinfo->out_color_space] == 3)
  735. cconvert->pub.color_convert = null_convert;
  736. else
  737. cconvert->pub.color_convert = rgb_rgb_convert;
  738. } else
  739. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  740. break;
  741. case JCS_RGB565:
  742. cinfo->out_color_components = 3;
  743. if (cinfo->dither_mode == JDITHER_NONE) {
  744. if (cinfo->jpeg_color_space == JCS_YCbCr) {
  745. if (jsimd_can_ycc_rgb565())
  746. cconvert->pub.color_convert = jsimd_ycc_rgb565_convert;
  747. else {
  748. cconvert->pub.color_convert = ycc_rgb565_convert;
  749. build_ycc_rgb_table(cinfo);
  750. }
  751. } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
  752. cconvert->pub.color_convert = gray_rgb565_convert;
  753. } else if (cinfo->jpeg_color_space == JCS_RGB) {
  754. cconvert->pub.color_convert = rgb_rgb565_convert;
  755. } else
  756. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  757. } else {
  758. /* only ordered dithering is supported */
  759. if (cinfo->jpeg_color_space == JCS_YCbCr) {
  760. cconvert->pub.color_convert = ycc_rgb565D_convert;
  761. build_ycc_rgb_table(cinfo);
  762. } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
  763. cconvert->pub.color_convert = gray_rgb565D_convert;
  764. } else if (cinfo->jpeg_color_space == JCS_RGB) {
  765. cconvert->pub.color_convert = rgb_rgb565D_convert;
  766. } else
  767. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  768. }
  769. break;
  770. case JCS_CMYK:
  771. cinfo->out_color_components = 4;
  772. if (cinfo->jpeg_color_space == JCS_YCCK) {
  773. cconvert->pub.color_convert = ycck_cmyk_convert;
  774. build_ycc_rgb_table(cinfo);
  775. } else if (cinfo->jpeg_color_space == JCS_CMYK) {
  776. cconvert->pub.color_convert = null_convert;
  777. } else
  778. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  779. break;
  780. default:
  781. /* Permit null conversion to same output space */
  782. if (cinfo->out_color_space == cinfo->jpeg_color_space) {
  783. cinfo->out_color_components = cinfo->num_components;
  784. cconvert->pub.color_convert = null_convert;
  785. } else /* unsupported non-null conversion */
  786. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  787. break;
  788. }
  789. if (cinfo->quantize_colors)
  790. cinfo->output_components = 1; /* single colormapped output component */
  791. else
  792. cinfo->output_components = cinfo->out_color_components;
  793. }