jccolor.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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,
  206. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  207. JDIMENSION output_row, int num_rows)
  208. {
  209. switch (cinfo->in_color_space) {
  210. case JCS_EXT_RGB:
  211. extrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
  212. num_rows);
  213. break;
  214. case JCS_EXT_RGBX:
  215. case JCS_EXT_RGBA:
  216. extrgbx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
  217. num_rows);
  218. break;
  219. case JCS_EXT_BGR:
  220. extbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
  221. num_rows);
  222. break;
  223. case JCS_EXT_BGRX:
  224. case JCS_EXT_BGRA:
  225. extbgrx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
  226. num_rows);
  227. break;
  228. case JCS_EXT_XBGR:
  229. case JCS_EXT_ABGR:
  230. extxbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
  231. num_rows);
  232. break;
  233. case JCS_EXT_XRGB:
  234. case JCS_EXT_ARGB:
  235. extxrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
  236. num_rows);
  237. break;
  238. default:
  239. rgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
  240. num_rows);
  241. break;
  242. }
  243. }
  244. /**************** Cases other than RGB -> YCbCr **************/
  245. /*
  246. * Convert some rows of samples to the JPEG colorspace.
  247. */
  248. METHODDEF(void)
  249. rgb_gray_convert (j_compress_ptr cinfo,
  250. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  251. JDIMENSION output_row, int num_rows)
  252. {
  253. switch (cinfo->in_color_space) {
  254. case JCS_EXT_RGB:
  255. extrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
  256. num_rows);
  257. break;
  258. case JCS_EXT_RGBX:
  259. case JCS_EXT_RGBA:
  260. extrgbx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
  261. num_rows);
  262. break;
  263. case JCS_EXT_BGR:
  264. extbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
  265. num_rows);
  266. break;
  267. case JCS_EXT_BGRX:
  268. case JCS_EXT_BGRA:
  269. extbgrx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
  270. num_rows);
  271. break;
  272. case JCS_EXT_XBGR:
  273. case JCS_EXT_ABGR:
  274. extxbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
  275. num_rows);
  276. break;
  277. case JCS_EXT_XRGB:
  278. case JCS_EXT_ARGB:
  279. extxrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
  280. num_rows);
  281. break;
  282. default:
  283. rgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
  284. num_rows);
  285. break;
  286. }
  287. }
  288. /*
  289. * Extended RGB to plain RGB conversion
  290. */
  291. METHODDEF(void)
  292. rgb_rgb_convert (j_compress_ptr cinfo,
  293. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  294. JDIMENSION output_row, int num_rows)
  295. {
  296. switch (cinfo->in_color_space) {
  297. case JCS_EXT_RGB:
  298. extrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
  299. num_rows);
  300. break;
  301. case JCS_EXT_RGBX:
  302. case JCS_EXT_RGBA:
  303. extrgbx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
  304. num_rows);
  305. break;
  306. case JCS_EXT_BGR:
  307. extbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
  308. num_rows);
  309. break;
  310. case JCS_EXT_BGRX:
  311. case JCS_EXT_BGRA:
  312. extbgrx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
  313. num_rows);
  314. break;
  315. case JCS_EXT_XBGR:
  316. case JCS_EXT_ABGR:
  317. extxbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
  318. num_rows);
  319. break;
  320. case JCS_EXT_XRGB:
  321. case JCS_EXT_ARGB:
  322. extxrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
  323. num_rows);
  324. break;
  325. default:
  326. rgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
  327. num_rows);
  328. break;
  329. }
  330. }
  331. /*
  332. * Convert some rows of samples to the JPEG colorspace.
  333. * This version handles Adobe-style CMYK->YCCK conversion,
  334. * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
  335. * conversion as above, while passing K (black) unchanged.
  336. * We assume rgb_ycc_start has been called.
  337. */
  338. METHODDEF(void)
  339. cmyk_ycck_convert (j_compress_ptr cinfo,
  340. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  341. JDIMENSION output_row, int num_rows)
  342. {
  343. my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  344. register int r, g, b;
  345. register JLONG *ctab = cconvert->rgb_ycc_tab;
  346. register JSAMPROW inptr;
  347. register JSAMPROW outptr0, outptr1, outptr2, outptr3;
  348. register JDIMENSION col;
  349. JDIMENSION num_cols = cinfo->image_width;
  350. while (--num_rows >= 0) {
  351. inptr = *input_buf++;
  352. outptr0 = output_buf[0][output_row];
  353. outptr1 = output_buf[1][output_row];
  354. outptr2 = output_buf[2][output_row];
  355. outptr3 = output_buf[3][output_row];
  356. output_row++;
  357. for (col = 0; col < num_cols; col++) {
  358. r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
  359. g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
  360. b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
  361. /* K passes through as-is */
  362. outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
  363. inptr += 4;
  364. /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  365. * must be too; we do not need an explicit range-limiting operation.
  366. * Hence the value being shifted is never negative, and we don't
  367. * need the general RIGHT_SHIFT macro.
  368. */
  369. /* Y */
  370. outptr0[col] = (JSAMPLE)
  371. ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  372. >> SCALEBITS);
  373. /* Cb */
  374. outptr1[col] = (JSAMPLE)
  375. ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  376. >> SCALEBITS);
  377. /* Cr */
  378. outptr2[col] = (JSAMPLE)
  379. ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  380. >> SCALEBITS);
  381. }
  382. }
  383. }
  384. /*
  385. * Convert some rows of samples to the JPEG colorspace.
  386. * This version handles grayscale output with no conversion.
  387. * The source can be either plain grayscale or YCbCr (since Y == gray).
  388. */
  389. METHODDEF(void)
  390. grayscale_convert (j_compress_ptr cinfo,
  391. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  392. JDIMENSION output_row, int num_rows)
  393. {
  394. register JSAMPROW inptr;
  395. register JSAMPROW outptr;
  396. register JDIMENSION col;
  397. JDIMENSION num_cols = cinfo->image_width;
  398. int instride = cinfo->input_components;
  399. while (--num_rows >= 0) {
  400. inptr = *input_buf++;
  401. outptr = output_buf[0][output_row];
  402. output_row++;
  403. for (col = 0; col < num_cols; col++) {
  404. outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
  405. inptr += instride;
  406. }
  407. }
  408. }
  409. /*
  410. * Convert some rows of samples to the JPEG colorspace.
  411. * This version handles multi-component colorspaces without conversion.
  412. * We assume input_components == num_components.
  413. */
  414. METHODDEF(void)
  415. null_convert (j_compress_ptr cinfo,
  416. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  417. JDIMENSION output_row, int num_rows)
  418. {
  419. register JSAMPROW inptr;
  420. register JSAMPROW outptr, outptr0, outptr1, outptr2, outptr3;
  421. register JDIMENSION col;
  422. register int ci;
  423. int nc = cinfo->num_components;
  424. JDIMENSION num_cols = cinfo->image_width;
  425. if (nc == 3) {
  426. while (--num_rows >= 0) {
  427. inptr = *input_buf++;
  428. outptr0 = output_buf[0][output_row];
  429. outptr1 = output_buf[1][output_row];
  430. outptr2 = output_buf[2][output_row];
  431. output_row++;
  432. for (col = 0; col < num_cols; col++) {
  433. outptr0[col] = *inptr++;
  434. outptr1[col] = *inptr++;
  435. outptr2[col] = *inptr++;
  436. }
  437. }
  438. } else if (nc == 4) {
  439. while (--num_rows >= 0) {
  440. inptr = *input_buf++;
  441. outptr0 = output_buf[0][output_row];
  442. outptr1 = output_buf[1][output_row];
  443. outptr2 = output_buf[2][output_row];
  444. outptr3 = output_buf[3][output_row];
  445. output_row++;
  446. for (col = 0; col < num_cols; col++) {
  447. outptr0[col] = *inptr++;
  448. outptr1[col] = *inptr++;
  449. outptr2[col] = *inptr++;
  450. outptr3[col] = *inptr++;
  451. }
  452. }
  453. } else {
  454. while (--num_rows >= 0) {
  455. /* It seems fastest to make a separate pass for each component. */
  456. for (ci = 0; ci < nc; ci++) {
  457. inptr = *input_buf;
  458. outptr = output_buf[ci][output_row];
  459. for (col = 0; col < num_cols; col++) {
  460. outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
  461. inptr += nc;
  462. }
  463. }
  464. input_buf++;
  465. output_row++;
  466. }
  467. }
  468. }
  469. /*
  470. * Empty method for start_pass.
  471. */
  472. METHODDEF(void)
  473. null_method (j_compress_ptr cinfo)
  474. {
  475. /* no work needed */
  476. }
  477. /*
  478. * Module initialization routine for input colorspace conversion.
  479. */
  480. GLOBAL(void)
  481. jinit_color_converter (j_compress_ptr cinfo)
  482. {
  483. my_cconvert_ptr cconvert;
  484. cconvert = (my_cconvert_ptr)
  485. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  486. sizeof(my_color_converter));
  487. cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
  488. /* set start_pass to null method until we find out differently */
  489. cconvert->pub.start_pass = null_method;
  490. /* Make sure input_components agrees with in_color_space */
  491. switch (cinfo->in_color_space) {
  492. case JCS_GRAYSCALE:
  493. if (cinfo->input_components != 1)
  494. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  495. break;
  496. case JCS_RGB:
  497. case JCS_EXT_RGB:
  498. case JCS_EXT_RGBX:
  499. case JCS_EXT_BGR:
  500. case JCS_EXT_BGRX:
  501. case JCS_EXT_XBGR:
  502. case JCS_EXT_XRGB:
  503. case JCS_EXT_RGBA:
  504. case JCS_EXT_BGRA:
  505. case JCS_EXT_ABGR:
  506. case JCS_EXT_ARGB:
  507. if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])
  508. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  509. break;
  510. case JCS_YCbCr:
  511. if (cinfo->input_components != 3)
  512. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  513. break;
  514. case JCS_CMYK:
  515. case JCS_YCCK:
  516. if (cinfo->input_components != 4)
  517. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  518. break;
  519. default: /* JCS_UNKNOWN can be anything */
  520. if (cinfo->input_components < 1)
  521. ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  522. break;
  523. }
  524. /* Check num_components, set conversion method based on requested space */
  525. switch (cinfo->jpeg_color_space) {
  526. case JCS_GRAYSCALE:
  527. if (cinfo->num_components != 1)
  528. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  529. if (cinfo->in_color_space == JCS_GRAYSCALE)
  530. cconvert->pub.color_convert = grayscale_convert;
  531. else if (cinfo->in_color_space == JCS_RGB ||
  532. cinfo->in_color_space == JCS_EXT_RGB ||
  533. cinfo->in_color_space == JCS_EXT_RGBX ||
  534. cinfo->in_color_space == JCS_EXT_BGR ||
  535. cinfo->in_color_space == JCS_EXT_BGRX ||
  536. cinfo->in_color_space == JCS_EXT_XBGR ||
  537. cinfo->in_color_space == JCS_EXT_XRGB ||
  538. cinfo->in_color_space == JCS_EXT_RGBA ||
  539. cinfo->in_color_space == JCS_EXT_BGRA ||
  540. cinfo->in_color_space == JCS_EXT_ABGR ||
  541. cinfo->in_color_space == JCS_EXT_ARGB) {
  542. if (jsimd_can_rgb_gray())
  543. cconvert->pub.color_convert = jsimd_rgb_gray_convert;
  544. else {
  545. cconvert->pub.start_pass = rgb_ycc_start;
  546. cconvert->pub.color_convert = rgb_gray_convert;
  547. }
  548. } else if (cinfo->in_color_space == JCS_YCbCr)
  549. cconvert->pub.color_convert = grayscale_convert;
  550. else
  551. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  552. break;
  553. case JCS_RGB:
  554. if (cinfo->num_components != 3)
  555. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  556. if (rgb_red[cinfo->in_color_space] == 0 &&
  557. rgb_green[cinfo->in_color_space] == 1 &&
  558. rgb_blue[cinfo->in_color_space] == 2 &&
  559. rgb_pixelsize[cinfo->in_color_space] == 3) {
  560. #if defined(__mips__)
  561. if (jsimd_c_can_null_convert())
  562. cconvert->pub.color_convert = jsimd_c_null_convert;
  563. else
  564. #endif
  565. cconvert->pub.color_convert = null_convert;
  566. } else if (cinfo->in_color_space == JCS_RGB ||
  567. cinfo->in_color_space == JCS_EXT_RGB ||
  568. cinfo->in_color_space == JCS_EXT_RGBX ||
  569. cinfo->in_color_space == JCS_EXT_BGR ||
  570. cinfo->in_color_space == JCS_EXT_BGRX ||
  571. cinfo->in_color_space == JCS_EXT_XBGR ||
  572. cinfo->in_color_space == JCS_EXT_XRGB ||
  573. cinfo->in_color_space == JCS_EXT_RGBA ||
  574. cinfo->in_color_space == JCS_EXT_BGRA ||
  575. cinfo->in_color_space == JCS_EXT_ABGR ||
  576. cinfo->in_color_space == JCS_EXT_ARGB)
  577. cconvert->pub.color_convert = rgb_rgb_convert;
  578. else
  579. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  580. break;
  581. case JCS_YCbCr:
  582. if (cinfo->num_components != 3)
  583. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  584. if (cinfo->in_color_space == JCS_RGB ||
  585. cinfo->in_color_space == JCS_EXT_RGB ||
  586. cinfo->in_color_space == JCS_EXT_RGBX ||
  587. cinfo->in_color_space == JCS_EXT_BGR ||
  588. cinfo->in_color_space == JCS_EXT_BGRX ||
  589. cinfo->in_color_space == JCS_EXT_XBGR ||
  590. cinfo->in_color_space == JCS_EXT_XRGB ||
  591. cinfo->in_color_space == JCS_EXT_RGBA ||
  592. cinfo->in_color_space == JCS_EXT_BGRA ||
  593. cinfo->in_color_space == JCS_EXT_ABGR ||
  594. cinfo->in_color_space == JCS_EXT_ARGB) {
  595. if (jsimd_can_rgb_ycc())
  596. cconvert->pub.color_convert = jsimd_rgb_ycc_convert;
  597. else {
  598. cconvert->pub.start_pass = rgb_ycc_start;
  599. cconvert->pub.color_convert = rgb_ycc_convert;
  600. }
  601. } else if (cinfo->in_color_space == JCS_YCbCr) {
  602. #if defined(__mips__)
  603. if (jsimd_c_can_null_convert())
  604. cconvert->pub.color_convert = jsimd_c_null_convert;
  605. else
  606. #endif
  607. cconvert->pub.color_convert = null_convert;
  608. } else
  609. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  610. break;
  611. case JCS_CMYK:
  612. if (cinfo->num_components != 4)
  613. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  614. if (cinfo->in_color_space == JCS_CMYK) {
  615. #if defined(__mips__)
  616. if (jsimd_c_can_null_convert())
  617. cconvert->pub.color_convert = jsimd_c_null_convert;
  618. else
  619. #endif
  620. cconvert->pub.color_convert = null_convert;
  621. } else
  622. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  623. break;
  624. case JCS_YCCK:
  625. if (cinfo->num_components != 4)
  626. ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  627. if (cinfo->in_color_space == JCS_CMYK) {
  628. cconvert->pub.start_pass = rgb_ycc_start;
  629. cconvert->pub.color_convert = cmyk_ycck_convert;
  630. } else if (cinfo->in_color_space == JCS_YCCK) {
  631. #if defined(__mips__)
  632. if (jsimd_c_can_null_convert())
  633. cconvert->pub.color_convert = jsimd_c_null_convert;
  634. else
  635. #endif
  636. cconvert->pub.color_convert = null_convert;
  637. } else
  638. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  639. break;
  640. default: /* allow null conversion of JCS_UNKNOWN */
  641. if (cinfo->jpeg_color_space != cinfo->in_color_space ||
  642. cinfo->num_components != cinfo->input_components)
  643. ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  644. #if defined(__mips__)
  645. if (jsimd_c_can_null_convert())
  646. cconvert->pub.color_convert = jsimd_c_null_convert;
  647. else
  648. #endif
  649. cconvert->pub.color_convert = null_convert;
  650. break;
  651. }
  652. }