jccolor.c 23 KB

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