jccolor.c 23 KB

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