jccolor.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*
  2. * jccolor.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1991-1996, Thomas G. Lane.
  6. * libjpeg-turbo Modifications:
  7. * Copyright 2009 Pierre Ossman <[email protected]> for Cendio AB
  8. * Copyright (C) 2009-2012, D. R. Commander.
  9. * For conditions of distribution and use, see the accompanying README file.
  10. *
  11. * This file contains input colorspace conversion routines.
  12. */
  13. #define JPEG_INTERNALS
  14. #include "jinclude.h"
  15. #include "jpeglib.h"
  16. #include "jsimd.h"
  17. #include "config.h"
  18. /* Private subobject */
  19. typedef struct {
  20. struct jpeg_color_converter pub; /* public fields */
  21. /* Private state for RGB->YCC conversion */
  22. INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */
  23. } my_color_converter;
  24. typedef my_color_converter * my_cconvert_ptr;
  25. /**************** RGB -> YCbCr conversion: most common case **************/
  26. /*
  27. * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
  28. * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
  29. * The conversion equations to be implemented are therefore
  30. * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
  31. * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
  32. * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE
  33. * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
  34. * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
  35. * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and
  36. * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
  37. * were not represented exactly. Now we sacrifice exact representation of
  38. * maximum red and maximum blue in order to get exact grayscales.
  39. *
  40. * To avoid floating-point arithmetic, we represent the fractional constants
  41. * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
  42. * the products by 2^16, with appropriate rounding, to get the correct answer.
  43. *
  44. * For even more speed, we avoid doing any multiplications in the inner loop
  45. * by precalculating the constants times R,G,B for all possible values.
  46. * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
  47. * for 12-bit samples it is still acceptable. It's not very reasonable for
  48. * 16-bit samples, but if you want lossless storage you shouldn't be changing
  49. * colorspace anyway.
  50. * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
  51. * in the tables to save adding them separately in the inner loop.
  52. */
  53. #define SCALEBITS 16 /* speediest right-shift on some machines */
  54. #define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS)
  55. #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
  56. #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
  57. /* We allocate one big table and divide it up into eight parts, instead of
  58. * doing eight alloc_small requests. This lets us use a single table base
  59. * address, which can be held in a register in the inner loops on many
  60. * machines (more than can hold all eight addresses, anyway).
  61. */
  62. #define R_Y_OFF 0 /* offset to R => Y section */
  63. #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
  64. #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
  65. #define R_CB_OFF (3*(MAXJSAMPLE+1))
  66. #define G_CB_OFF (4*(MAXJSAMPLE+1))
  67. #define B_CB_OFF (5*(MAXJSAMPLE+1))
  68. #define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */
  69. #define G_CR_OFF (6*(MAXJSAMPLE+1))
  70. #define B_CR_OFF (7*(MAXJSAMPLE+1))
  71. #define TABLE_SIZE (8*(MAXJSAMPLE+1))
  72. /* Include inline routines for colorspace extensions */
  73. #include "jccolext.c"
  74. #undef RGB_RED
  75. #undef RGB_GREEN
  76. #undef RGB_BLUE
  77. #undef RGB_PIXELSIZE
  78. #define RGB_RED EXT_RGB_RED
  79. #define RGB_GREEN EXT_RGB_GREEN
  80. #define RGB_BLUE EXT_RGB_BLUE
  81. #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
  82. #define rgb_ycc_convert_internal extrgb_ycc_convert_internal
  83. #define rgb_gray_convert_internal extrgb_gray_convert_internal
  84. #define rgb_rgb_convert_internal extrgb_rgb_convert_internal
  85. #include "jccolext.c"
  86. #undef RGB_RED
  87. #undef RGB_GREEN
  88. #undef RGB_BLUE
  89. #undef RGB_PIXELSIZE
  90. #undef rgb_ycc_convert_internal
  91. #undef rgb_gray_convert_internal
  92. #undef rgb_rgb_convert_internal
  93. #define RGB_RED EXT_RGBX_RED
  94. #define RGB_GREEN EXT_RGBX_GREEN
  95. #define RGB_BLUE EXT_RGBX_BLUE
  96. #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
  97. #define rgb_ycc_convert_internal extrgbx_ycc_convert_internal
  98. #define rgb_gray_convert_internal extrgbx_gray_convert_internal
  99. #define rgb_rgb_convert_internal extrgbx_rgb_convert_internal
  100. #include "jccolext.c"
  101. #undef RGB_RED
  102. #undef RGB_GREEN
  103. #undef RGB_BLUE
  104. #undef RGB_PIXELSIZE
  105. #undef rgb_ycc_convert_internal
  106. #undef rgb_gray_convert_internal
  107. #undef rgb_rgb_convert_internal
  108. #define RGB_RED EXT_BGR_RED
  109. #define RGB_GREEN EXT_BGR_GREEN
  110. #define RGB_BLUE EXT_BGR_BLUE
  111. #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
  112. #define rgb_ycc_convert_internal extbgr_ycc_convert_internal
  113. #define rgb_gray_convert_internal extbgr_gray_convert_internal
  114. #define rgb_rgb_convert_internal extbgr_rgb_convert_internal
  115. #include "jccolext.c"
  116. #undef RGB_RED
  117. #undef RGB_GREEN
  118. #undef RGB_BLUE
  119. #undef RGB_PIXELSIZE
  120. #undef rgb_ycc_convert_internal
  121. #undef rgb_gray_convert_internal
  122. #undef rgb_rgb_convert_internal
  123. #define RGB_RED EXT_BGRX_RED
  124. #define RGB_GREEN EXT_BGRX_GREEN
  125. #define RGB_BLUE EXT_BGRX_BLUE
  126. #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
  127. #define rgb_ycc_convert_internal extbgrx_ycc_convert_internal
  128. #define rgb_gray_convert_internal extbgrx_gray_convert_internal
  129. #define rgb_rgb_convert_internal extbgrx_rgb_convert_internal
  130. #include "jccolext.c"
  131. #undef RGB_RED
  132. #undef RGB_GREEN
  133. #undef RGB_BLUE
  134. #undef RGB_PIXELSIZE
  135. #undef rgb_ycc_convert_internal
  136. #undef rgb_gray_convert_internal
  137. #undef rgb_rgb_convert_internal
  138. #define RGB_RED EXT_XBGR_RED
  139. #define RGB_GREEN EXT_XBGR_GREEN
  140. #define RGB_BLUE EXT_XBGR_BLUE
  141. #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
  142. #define rgb_ycc_convert_internal extxbgr_ycc_convert_internal
  143. #define rgb_gray_convert_internal extxbgr_gray_convert_internal
  144. #define rgb_rgb_convert_internal extxbgr_rgb_convert_internal
  145. #include "jccolext.c"
  146. #undef RGB_RED
  147. #undef RGB_GREEN
  148. #undef RGB_BLUE
  149. #undef RGB_PIXELSIZE
  150. #undef rgb_ycc_convert_internal
  151. #undef rgb_gray_convert_internal
  152. #undef rgb_rgb_convert_internal
  153. #define RGB_RED EXT_XRGB_RED
  154. #define RGB_GREEN EXT_XRGB_GREEN
  155. #define RGB_BLUE EXT_XRGB_BLUE
  156. #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
  157. #define rgb_ycc_convert_internal extxrgb_ycc_convert_internal
  158. #define rgb_gray_convert_internal extxrgb_gray_convert_internal
  159. #define rgb_rgb_convert_internal extxrgb_rgb_convert_internal
  160. #include "jccolext.c"
  161. #undef RGB_RED
  162. #undef RGB_GREEN
  163. #undef RGB_BLUE
  164. #undef RGB_PIXELSIZE
  165. #undef rgb_ycc_convert_internal
  166. #undef rgb_gray_convert_internal
  167. #undef rgb_rgb_convert_internal
  168. /*
  169. * Initialize for RGB->YCC colorspace conversion.
  170. */
  171. METHODDEF(void)
  172. rgb_ycc_start (j_compress_ptr cinfo)
  173. {
  174. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  175. INT32 * rgb_ycc_tab;
  176. INT32 i;
  177. /* Allocate and fill in the conversion tables. */
  178. cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
  179. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  180. (TABLE_SIZE * SIZEOF(INT32)));
  181. for (i = 0; i <= MAXJSAMPLE; i++) {
  182. rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
  183. rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
  184. rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
  185. rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
  186. rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
  187. /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
  188. * This ensures that the maximum output will round to MAXJSAMPLE
  189. * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
  190. */
  191. rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
  192. /* B=>Cb and R=>Cr tables are the same
  193. rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
  194. */
  195. rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
  196. rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
  197. }
  198. }
  199. /*
  200. * Convert some rows of samples to the JPEG colorspace.
  201. */
  202. METHODDEF(void)
  203. rgb_ycc_convert (j_compress_ptr cinfo,
  204. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  205. JDIMENSION output_row, int num_rows)
  206. {
  207. switch (cinfo->in_color_space) {
  208. case JCS_EXT_RGB:
  209. extrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
  210. num_rows);
  211. break;
  212. case JCS_EXT_RGBX:
  213. case JCS_EXT_RGBA:
  214. extrgbx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
  215. num_rows);
  216. break;
  217. case JCS_EXT_BGR:
  218. extbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
  219. num_rows);
  220. break;
  221. case JCS_EXT_BGRX:
  222. case JCS_EXT_BGRA:
  223. extbgrx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
  224. num_rows);
  225. break;
  226. case JCS_EXT_XBGR:
  227. case JCS_EXT_ABGR:
  228. extxbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
  229. num_rows);
  230. break;
  231. case JCS_EXT_XRGB:
  232. case JCS_EXT_ARGB:
  233. extxrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
  234. num_rows);
  235. break;
  236. default:
  237. rgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
  238. num_rows);
  239. break;
  240. }
  241. }
  242. /**************** Cases other than RGB -> YCbCr **************/
  243. /*
  244. * Convert some rows of samples to the JPEG colorspace.
  245. */
  246. METHODDEF(void)
  247. rgb_gray_convert (j_compress_ptr cinfo,
  248. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  249. JDIMENSION output_row, int num_rows)
  250. {
  251. switch (cinfo->in_color_space) {
  252. case JCS_EXT_RGB:
  253. extrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
  254. num_rows);
  255. break;
  256. case JCS_EXT_RGBX:
  257. case JCS_EXT_RGBA:
  258. extrgbx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
  259. num_rows);
  260. break;
  261. case JCS_EXT_BGR:
  262. extbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
  263. num_rows);
  264. break;
  265. case JCS_EXT_BGRX:
  266. case JCS_EXT_BGRA:
  267. extbgrx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
  268. num_rows);
  269. break;
  270. case JCS_EXT_XBGR:
  271. case JCS_EXT_ABGR:
  272. extxbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
  273. num_rows);
  274. break;
  275. case JCS_EXT_XRGB:
  276. case JCS_EXT_ARGB:
  277. extxrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
  278. num_rows);
  279. break;
  280. default:
  281. rgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
  282. num_rows);
  283. break;
  284. }
  285. }
  286. /*
  287. * Extended RGB to plain RGB conversion
  288. */
  289. METHODDEF(void)
  290. rgb_rgb_convert (j_compress_ptr cinfo,
  291. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  292. JDIMENSION output_row, int num_rows)
  293. {
  294. switch (cinfo->in_color_space) {
  295. case JCS_EXT_RGB:
  296. extrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
  297. num_rows);
  298. break;
  299. case JCS_EXT_RGBX:
  300. case JCS_EXT_RGBA:
  301. extrgbx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
  302. num_rows);
  303. break;
  304. case JCS_EXT_BGR:
  305. extbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
  306. num_rows);
  307. break;
  308. case JCS_EXT_BGRX:
  309. case JCS_EXT_BGRA:
  310. extbgrx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
  311. num_rows);
  312. break;
  313. case JCS_EXT_XBGR:
  314. case JCS_EXT_ABGR:
  315. extxbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
  316. num_rows);
  317. break;
  318. case JCS_EXT_XRGB:
  319. case JCS_EXT_ARGB:
  320. extxrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
  321. num_rows);
  322. break;
  323. default:
  324. rgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
  325. num_rows);
  326. break;
  327. }
  328. }
  329. /*
  330. * Convert some rows of samples to the JPEG colorspace.
  331. * This version handles Adobe-style CMYK->YCCK conversion,
  332. * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
  333. * conversion as above, while passing K (black) unchanged.
  334. * We assume rgb_ycc_start has been called.
  335. */
  336. METHODDEF(void)
  337. cmyk_ycck_convert (j_compress_ptr cinfo,
  338. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  339. JDIMENSION output_row, int num_rows)
  340. {
  341. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  342. register int r, g, b;
  343. register INT32 * ctab = cconvert->rgb_ycc_tab;
  344. register JSAMPROW inptr;
  345. register JSAMPROW outptr0, outptr1, outptr2, outptr3;
  346. register JDIMENSION col;
  347. JDIMENSION num_cols = cinfo->image_width;
  348. while (--num_rows >= 0) {
  349. inptr = *input_buf++;
  350. outptr0 = output_buf[0][output_row];
  351. outptr1 = output_buf[1][output_row];
  352. outptr2 = output_buf[2][output_row];
  353. outptr3 = output_buf[3][output_row];
  354. output_row++;
  355. for (col = 0; col < num_cols; col++) {
  356. r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
  357. g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
  358. b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
  359. /* K passes through as-is */
  360. outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
  361. inptr += 4;
  362. /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  363. * must be too; we do not need an explicit range-limiting operation.
  364. * Hence the value being shifted is never negative, and we don't
  365. * need the general RIGHT_SHIFT macro.
  366. */
  367. /* Y */
  368. outptr0[col] = (JSAMPLE)
  369. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  370. >> SCALEBITS);
  371. /* Cb */
  372. outptr1[col] = (JSAMPLE)
  373. ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  374. >> SCALEBITS);
  375. /* Cr */
  376. outptr2[col] = (JSAMPLE)
  377. ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  378. >> SCALEBITS);
  379. }
  380. }
  381. }
  382. /*
  383. * Convert some rows of samples to the JPEG colorspace.
  384. * This version handles grayscale output with no conversion.
  385. * The source can be either plain grayscale or YCbCr (since Y == gray).
  386. */
  387. METHODDEF(void)
  388. grayscale_convert (j_compress_ptr cinfo,
  389. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  390. JDIMENSION output_row, int num_rows)
  391. {
  392. register JSAMPROW inptr;
  393. register JSAMPROW outptr;
  394. register JDIMENSION col;
  395. JDIMENSION num_cols = cinfo->image_width;
  396. int instride = cinfo->input_components;
  397. while (--num_rows >= 0) {
  398. inptr = *input_buf++;
  399. outptr = output_buf[0][output_row];
  400. output_row++;
  401. for (col = 0; col < num_cols; col++) {
  402. outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
  403. inptr += instride;
  404. }
  405. }
  406. }
  407. /*
  408. * Convert some rows of samples to the JPEG colorspace.
  409. * This version handles multi-component colorspaces without conversion.
  410. * We assume input_components == num_components.
  411. */
  412. METHODDEF(void)
  413. null_convert (j_compress_ptr cinfo,
  414. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  415. JDIMENSION output_row, int num_rows)
  416. {
  417. register JSAMPROW inptr;
  418. register JSAMPROW outptr;
  419. register JDIMENSION col;
  420. register int ci;
  421. int nc = cinfo->num_components;
  422. JDIMENSION num_cols = cinfo->image_width;
  423. while (--num_rows >= 0) {
  424. /* It seems fastest to make a separate pass for each component. */
  425. for (ci = 0; ci < nc; ci++) {
  426. inptr = *input_buf;
  427. outptr = output_buf[ci][output_row];
  428. for (col = 0; col < num_cols; col++) {
  429. outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
  430. inptr += nc;
  431. }
  432. }
  433. input_buf++;
  434. output_row++;
  435. }
  436. }
  437. /*
  438. * Empty method for start_pass.
  439. */
  440. METHODDEF(void)
  441. null_method (j_compress_ptr cinfo)
  442. {
  443. /* no work needed */
  444. }
  445. /*
  446. * Module initialization routine for input colorspace conversion.
  447. */
  448. GLOBAL(void)
  449. jinit_color_converter (j_compress_ptr cinfo)
  450. {
  451. my_cconvert_ptr cconvert;
  452. cconvert = (my_cconvert_ptr)
  453. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  454. SIZEOF(my_color_converter));
  455. cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
  456. /* set start_pass to null method until we find out differently */
  457. cconvert->pub.start_pass = null_method;
  458. /* Make sure input_components agrees with in_color_space */
  459. switch (cinfo->in_color_space) {
  460. case JCS_GRAYSCALE:
  461. if (cinfo->input_components != 1)
  462. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  463. break;
  464. case JCS_RGB:
  465. case JCS_EXT_RGB:
  466. case JCS_EXT_RGBX:
  467. case JCS_EXT_BGR:
  468. case JCS_EXT_BGRX:
  469. case JCS_EXT_XBGR:
  470. case JCS_EXT_XRGB:
  471. case JCS_EXT_RGBA:
  472. case JCS_EXT_BGRA:
  473. case JCS_EXT_ABGR:
  474. case JCS_EXT_ARGB:
  475. if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])
  476. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  477. break;
  478. case JCS_YCbCr:
  479. if (cinfo->input_components != 3)
  480. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  481. break;
  482. case JCS_CMYK:
  483. case JCS_YCCK:
  484. if (cinfo->input_components != 4)
  485. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  486. break;
  487. default: /* JCS_UNKNOWN can be anything */
  488. if (cinfo->input_components < 1)
  489. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  490. break;
  491. }
  492. /* Check num_components, set conversion method based on requested space */
  493. switch (cinfo->jpeg_color_space) {
  494. case JCS_GRAYSCALE:
  495. if (cinfo->num_components != 1)
  496. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  497. if (cinfo->in_color_space == JCS_GRAYSCALE)
  498. cconvert->pub.color_convert = grayscale_convert;
  499. else if (cinfo->in_color_space == JCS_RGB ||
  500. cinfo->in_color_space == JCS_EXT_RGB ||
  501. cinfo->in_color_space == JCS_EXT_RGBX ||
  502. cinfo->in_color_space == JCS_EXT_BGR ||
  503. cinfo->in_color_space == JCS_EXT_BGRX ||
  504. cinfo->in_color_space == JCS_EXT_XBGR ||
  505. cinfo->in_color_space == JCS_EXT_XRGB ||
  506. cinfo->in_color_space == JCS_EXT_RGBA ||
  507. cinfo->in_color_space == JCS_EXT_BGRA ||
  508. cinfo->in_color_space == JCS_EXT_ABGR ||
  509. cinfo->in_color_space == JCS_EXT_ARGB) {
  510. if (jsimd_can_rgb_gray())
  511. cconvert->pub.color_convert = jsimd_rgb_gray_convert;
  512. else {
  513. cconvert->pub.start_pass = rgb_ycc_start;
  514. cconvert->pub.color_convert = rgb_gray_convert;
  515. }
  516. } else if (cinfo->in_color_space == JCS_YCbCr)
  517. cconvert->pub.color_convert = grayscale_convert;
  518. else
  519. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  520. break;
  521. case JCS_RGB:
  522. if (cinfo->num_components != 3)
  523. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  524. if (rgb_red[cinfo->in_color_space] == 0 &&
  525. rgb_green[cinfo->in_color_space] == 1 &&
  526. rgb_blue[cinfo->in_color_space] == 2 &&
  527. rgb_pixelsize[cinfo->in_color_space] == 3)
  528. cconvert->pub.color_convert = null_convert;
  529. else if (cinfo->in_color_space == JCS_RGB ||
  530. cinfo->in_color_space == JCS_EXT_RGB ||
  531. cinfo->in_color_space == JCS_EXT_RGBX ||
  532. cinfo->in_color_space == JCS_EXT_BGR ||
  533. cinfo->in_color_space == JCS_EXT_BGRX ||
  534. cinfo->in_color_space == JCS_EXT_XBGR ||
  535. cinfo->in_color_space == JCS_EXT_XRGB ||
  536. cinfo->in_color_space == JCS_EXT_RGBA ||
  537. cinfo->in_color_space == JCS_EXT_BGRA ||
  538. cinfo->in_color_space == JCS_EXT_ABGR ||
  539. cinfo->in_color_space == JCS_EXT_ARGB)
  540. cconvert->pub.color_convert = rgb_rgb_convert;
  541. else
  542. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  543. break;
  544. case JCS_YCbCr:
  545. if (cinfo->num_components != 3)
  546. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  547. if (cinfo->in_color_space == JCS_RGB ||
  548. cinfo->in_color_space == JCS_EXT_RGB ||
  549. cinfo->in_color_space == JCS_EXT_RGBX ||
  550. cinfo->in_color_space == JCS_EXT_BGR ||
  551. cinfo->in_color_space == JCS_EXT_BGRX ||
  552. cinfo->in_color_space == JCS_EXT_XBGR ||
  553. cinfo->in_color_space == JCS_EXT_XRGB ||
  554. cinfo->in_color_space == JCS_EXT_RGBA ||
  555. cinfo->in_color_space == JCS_EXT_BGRA ||
  556. cinfo->in_color_space == JCS_EXT_ABGR ||
  557. cinfo->in_color_space == JCS_EXT_ARGB) {
  558. if (jsimd_can_rgb_ycc())
  559. cconvert->pub.color_convert = jsimd_rgb_ycc_convert;
  560. else {
  561. cconvert->pub.start_pass = rgb_ycc_start;
  562. cconvert->pub.color_convert = rgb_ycc_convert;
  563. }
  564. } else if (cinfo->in_color_space == JCS_YCbCr)
  565. cconvert->pub.color_convert = null_convert;
  566. else
  567. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  568. break;
  569. case JCS_CMYK:
  570. if (cinfo->num_components != 4)
  571. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  572. if (cinfo->in_color_space == JCS_CMYK)
  573. cconvert->pub.color_convert = null_convert;
  574. else
  575. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  576. break;
  577. case JCS_YCCK:
  578. if (cinfo->num_components != 4)
  579. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  580. if (cinfo->in_color_space == JCS_CMYK) {
  581. cconvert->pub.start_pass = rgb_ycc_start;
  582. cconvert->pub.color_convert = cmyk_ycck_convert;
  583. } else if (cinfo->in_color_space == JCS_YCCK)
  584. cconvert->pub.color_convert = null_convert;
  585. else
  586. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  587. break;
  588. default: /* allow null conversion of JCS_UNKNOWN */
  589. if (cinfo->jpeg_color_space != cinfo->in_color_space ||
  590. cinfo->num_components != cinfo->input_components)
  591. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  592. cconvert->pub.color_convert = null_convert;
  593. break;
  594. }
  595. }