jdmerge.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. * jdmerge.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1994-1996, Thomas G. Lane.
  6. * libjpeg-turbo Modifications:
  7. * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
  8. * Copyright (C) 2009, 2011, 2014-2015, D. R. Commander.
  9. * Copyright (C) 2013, Linaro Limited.
  10. * For conditions of distribution and use, see the accompanying README.ijg
  11. * file.
  12. *
  13. * This file contains code for merged upsampling/color conversion.
  14. *
  15. * This file combines functions from jdsample.c and jdcolor.c;
  16. * read those files first to understand what's going on.
  17. *
  18. * When the chroma components are to be upsampled by simple replication
  19. * (ie, box filtering), we can save some work in color conversion by
  20. * calculating all the output pixels corresponding to a pair of chroma
  21. * samples at one time. In the conversion equations
  22. * R = Y + K1 * Cr
  23. * G = Y + K2 * Cb + K3 * Cr
  24. * B = Y + K4 * Cb
  25. * only the Y term varies among the group of pixels corresponding to a pair
  26. * of chroma samples, so the rest of the terms can be calculated just once.
  27. * At typical sampling ratios, this eliminates half or three-quarters of the
  28. * multiplications needed for color conversion.
  29. *
  30. * This file currently provides implementations for the following cases:
  31. * YCbCr => RGB color conversion only.
  32. * Sampling ratios of 2h1v or 2h2v.
  33. * No scaling needed at upsample time.
  34. * Corner-aligned (non-CCIR601) sampling alignment.
  35. * Other special cases could be added, but in most applications these are
  36. * the only common cases. (For uncommon cases we fall back on the more
  37. * general code in jdsample.c and jdcolor.c.)
  38. */
  39. #define JPEG_INTERNALS
  40. #include "jinclude.h"
  41. #include "jpeglib.h"
  42. #include "jsimd.h"
  43. #include "jconfigint.h"
  44. #ifdef UPSAMPLE_MERGING_SUPPORTED
  45. /* Private subobject */
  46. typedef struct {
  47. struct jpeg_upsampler pub; /* public fields */
  48. /* Pointer to routine to do actual upsampling/conversion of one row group */
  49. void (*upmethod) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  50. JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf);
  51. /* Private state for YCC->RGB conversion */
  52. int *Cr_r_tab; /* => table for Cr to R conversion */
  53. int *Cb_b_tab; /* => table for Cb to B conversion */
  54. JLONG *Cr_g_tab; /* => table for Cr to G conversion */
  55. JLONG *Cb_g_tab; /* => table for Cb to G conversion */
  56. /* For 2:1 vertical sampling, we produce two output rows at a time.
  57. * We need a "spare" row buffer to hold the second output row if the
  58. * application provides just a one-row buffer; we also use the spare
  59. * to discard the dummy last row if the image height is odd.
  60. */
  61. JSAMPROW spare_row;
  62. boolean spare_full; /* T if spare buffer is occupied */
  63. JDIMENSION out_row_width; /* samples per output row */
  64. JDIMENSION rows_to_go; /* counts rows remaining in image */
  65. } my_upsampler;
  66. typedef my_upsampler *my_upsample_ptr;
  67. #define SCALEBITS 16 /* speediest right-shift on some machines */
  68. #define ONE_HALF ((JLONG) 1 << (SCALEBITS-1))
  69. #define FIX(x) ((JLONG) ((x) * (1L<<SCALEBITS) + 0.5))
  70. /* Include inline routines for colorspace extensions */
  71. #include "jdmrgext.c"
  72. #undef RGB_RED
  73. #undef RGB_GREEN
  74. #undef RGB_BLUE
  75. #undef RGB_PIXELSIZE
  76. #define RGB_RED EXT_RGB_RED
  77. #define RGB_GREEN EXT_RGB_GREEN
  78. #define RGB_BLUE EXT_RGB_BLUE
  79. #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
  80. #define h2v1_merged_upsample_internal extrgb_h2v1_merged_upsample_internal
  81. #define h2v2_merged_upsample_internal extrgb_h2v2_merged_upsample_internal
  82. #include "jdmrgext.c"
  83. #undef RGB_RED
  84. #undef RGB_GREEN
  85. #undef RGB_BLUE
  86. #undef RGB_PIXELSIZE
  87. #undef h2v1_merged_upsample_internal
  88. #undef h2v2_merged_upsample_internal
  89. #define RGB_RED EXT_RGBX_RED
  90. #define RGB_GREEN EXT_RGBX_GREEN
  91. #define RGB_BLUE EXT_RGBX_BLUE
  92. #define RGB_ALPHA 3
  93. #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
  94. #define h2v1_merged_upsample_internal extrgbx_h2v1_merged_upsample_internal
  95. #define h2v2_merged_upsample_internal extrgbx_h2v2_merged_upsample_internal
  96. #include "jdmrgext.c"
  97. #undef RGB_RED
  98. #undef RGB_GREEN
  99. #undef RGB_BLUE
  100. #undef RGB_ALPHA
  101. #undef RGB_PIXELSIZE
  102. #undef h2v1_merged_upsample_internal
  103. #undef h2v2_merged_upsample_internal
  104. #define RGB_RED EXT_BGR_RED
  105. #define RGB_GREEN EXT_BGR_GREEN
  106. #define RGB_BLUE EXT_BGR_BLUE
  107. #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
  108. #define h2v1_merged_upsample_internal extbgr_h2v1_merged_upsample_internal
  109. #define h2v2_merged_upsample_internal extbgr_h2v2_merged_upsample_internal
  110. #include "jdmrgext.c"
  111. #undef RGB_RED
  112. #undef RGB_GREEN
  113. #undef RGB_BLUE
  114. #undef RGB_PIXELSIZE
  115. #undef h2v1_merged_upsample_internal
  116. #undef h2v2_merged_upsample_internal
  117. #define RGB_RED EXT_BGRX_RED
  118. #define RGB_GREEN EXT_BGRX_GREEN
  119. #define RGB_BLUE EXT_BGRX_BLUE
  120. #define RGB_ALPHA 3
  121. #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
  122. #define h2v1_merged_upsample_internal extbgrx_h2v1_merged_upsample_internal
  123. #define h2v2_merged_upsample_internal extbgrx_h2v2_merged_upsample_internal
  124. #include "jdmrgext.c"
  125. #undef RGB_RED
  126. #undef RGB_GREEN
  127. #undef RGB_BLUE
  128. #undef RGB_ALPHA
  129. #undef RGB_PIXELSIZE
  130. #undef h2v1_merged_upsample_internal
  131. #undef h2v2_merged_upsample_internal
  132. #define RGB_RED EXT_XBGR_RED
  133. #define RGB_GREEN EXT_XBGR_GREEN
  134. #define RGB_BLUE EXT_XBGR_BLUE
  135. #define RGB_ALPHA 0
  136. #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
  137. #define h2v1_merged_upsample_internal extxbgr_h2v1_merged_upsample_internal
  138. #define h2v2_merged_upsample_internal extxbgr_h2v2_merged_upsample_internal
  139. #include "jdmrgext.c"
  140. #undef RGB_RED
  141. #undef RGB_GREEN
  142. #undef RGB_BLUE
  143. #undef RGB_ALPHA
  144. #undef RGB_PIXELSIZE
  145. #undef h2v1_merged_upsample_internal
  146. #undef h2v2_merged_upsample_internal
  147. #define RGB_RED EXT_XRGB_RED
  148. #define RGB_GREEN EXT_XRGB_GREEN
  149. #define RGB_BLUE EXT_XRGB_BLUE
  150. #define RGB_ALPHA 0
  151. #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
  152. #define h2v1_merged_upsample_internal extxrgb_h2v1_merged_upsample_internal
  153. #define h2v2_merged_upsample_internal extxrgb_h2v2_merged_upsample_internal
  154. #include "jdmrgext.c"
  155. #undef RGB_RED
  156. #undef RGB_GREEN
  157. #undef RGB_BLUE
  158. #undef RGB_ALPHA
  159. #undef RGB_PIXELSIZE
  160. #undef h2v1_merged_upsample_internal
  161. #undef h2v2_merged_upsample_internal
  162. /*
  163. * Initialize tables for YCC->RGB colorspace conversion.
  164. * This is taken directly from jdcolor.c; see that file for more info.
  165. */
  166. LOCAL(void)
  167. build_ycc_rgb_table (j_decompress_ptr cinfo)
  168. {
  169. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  170. int i;
  171. JLONG x;
  172. SHIFT_TEMPS
  173. upsample->Cr_r_tab = (int *)
  174. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  175. (MAXJSAMPLE+1) * sizeof(int));
  176. upsample->Cb_b_tab = (int *)
  177. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  178. (MAXJSAMPLE+1) * sizeof(int));
  179. upsample->Cr_g_tab = (JLONG *)
  180. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  181. (MAXJSAMPLE+1) * sizeof(JLONG));
  182. upsample->Cb_g_tab = (JLONG *)
  183. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  184. (MAXJSAMPLE+1) * sizeof(JLONG));
  185. for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
  186. /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
  187. /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
  188. /* Cr=>R value is nearest int to 1.40200 * x */
  189. upsample->Cr_r_tab[i] = (int)
  190. RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
  191. /* Cb=>B value is nearest int to 1.77200 * x */
  192. upsample->Cb_b_tab[i] = (int)
  193. RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
  194. /* Cr=>G value is scaled-up -0.71414 * x */
  195. upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x;
  196. /* Cb=>G value is scaled-up -0.34414 * x */
  197. /* We also add in ONE_HALF so that need not do it in inner loop */
  198. upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
  199. }
  200. }
  201. /*
  202. * Initialize for an upsampling pass.
  203. */
  204. METHODDEF(void)
  205. start_pass_merged_upsample (j_decompress_ptr cinfo)
  206. {
  207. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  208. /* Mark the spare buffer empty */
  209. upsample->spare_full = FALSE;
  210. /* Initialize total-height counter for detecting bottom of image */
  211. upsample->rows_to_go = cinfo->output_height;
  212. }
  213. /*
  214. * Control routine to do upsampling (and color conversion).
  215. *
  216. * The control routine just handles the row buffering considerations.
  217. */
  218. METHODDEF(void)
  219. merged_2v_upsample (j_decompress_ptr cinfo,
  220. JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  221. JDIMENSION in_row_groups_avail,
  222. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  223. JDIMENSION out_rows_avail)
  224. /* 2:1 vertical sampling case: may need a spare row. */
  225. {
  226. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  227. JSAMPROW work_ptrs[2];
  228. JDIMENSION num_rows; /* number of rows returned to caller */
  229. if (upsample->spare_full) {
  230. /* If we have a spare row saved from a previous cycle, just return it. */
  231. JDIMENSION size = upsample->out_row_width;
  232. if (cinfo->out_color_space == JCS_RGB565)
  233. size = cinfo->output_width * 2;
  234. jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
  235. 1, size);
  236. num_rows = 1;
  237. upsample->spare_full = FALSE;
  238. } else {
  239. /* Figure number of rows to return to caller. */
  240. num_rows = 2;
  241. /* Not more than the distance to the end of the image. */
  242. if (num_rows > upsample->rows_to_go)
  243. num_rows = upsample->rows_to_go;
  244. /* And not more than what the client can accept: */
  245. out_rows_avail -= *out_row_ctr;
  246. if (num_rows > out_rows_avail)
  247. num_rows = out_rows_avail;
  248. /* Create output pointer array for upsampler. */
  249. work_ptrs[0] = output_buf[*out_row_ctr];
  250. if (num_rows > 1) {
  251. work_ptrs[1] = output_buf[*out_row_ctr + 1];
  252. } else {
  253. work_ptrs[1] = upsample->spare_row;
  254. upsample->spare_full = TRUE;
  255. }
  256. /* Now do the upsampling. */
  257. (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
  258. }
  259. /* Adjust counts */
  260. *out_row_ctr += num_rows;
  261. upsample->rows_to_go -= num_rows;
  262. /* When the buffer is emptied, declare this input row group consumed */
  263. if (! upsample->spare_full)
  264. (*in_row_group_ctr)++;
  265. }
  266. METHODDEF(void)
  267. merged_1v_upsample (j_decompress_ptr cinfo,
  268. JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  269. JDIMENSION in_row_groups_avail,
  270. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  271. JDIMENSION out_rows_avail)
  272. /* 1:1 vertical sampling case: much easier, never need a spare row. */
  273. {
  274. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  275. /* Just do the upsampling. */
  276. (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
  277. output_buf + *out_row_ctr);
  278. /* Adjust counts */
  279. (*out_row_ctr)++;
  280. (*in_row_group_ctr)++;
  281. }
  282. /*
  283. * These are the routines invoked by the control routines to do
  284. * the actual upsampling/conversion. One row group is processed per call.
  285. *
  286. * Note: since we may be writing directly into application-supplied buffers,
  287. * we have to be honest about the output width; we can't assume the buffer
  288. * has been rounded up to an even width.
  289. */
  290. /*
  291. * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
  292. */
  293. METHODDEF(void)
  294. h2v1_merged_upsample (j_decompress_ptr cinfo,
  295. JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
  296. JSAMPARRAY output_buf)
  297. {
  298. switch (cinfo->out_color_space) {
  299. case JCS_EXT_RGB:
  300. extrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  301. output_buf);
  302. break;
  303. case JCS_EXT_RGBX:
  304. case JCS_EXT_RGBA:
  305. extrgbx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  306. output_buf);
  307. break;
  308. case JCS_EXT_BGR:
  309. extbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  310. output_buf);
  311. break;
  312. case JCS_EXT_BGRX:
  313. case JCS_EXT_BGRA:
  314. extbgrx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  315. output_buf);
  316. break;
  317. case JCS_EXT_XBGR:
  318. case JCS_EXT_ABGR:
  319. extxbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  320. output_buf);
  321. break;
  322. case JCS_EXT_XRGB:
  323. case JCS_EXT_ARGB:
  324. extxrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  325. output_buf);
  326. break;
  327. default:
  328. h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  329. output_buf);
  330. break;
  331. }
  332. }
  333. /*
  334. * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
  335. */
  336. METHODDEF(void)
  337. h2v2_merged_upsample (j_decompress_ptr cinfo,
  338. JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
  339. JSAMPARRAY output_buf)
  340. {
  341. switch (cinfo->out_color_space) {
  342. case JCS_EXT_RGB:
  343. extrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  344. output_buf);
  345. break;
  346. case JCS_EXT_RGBX:
  347. case JCS_EXT_RGBA:
  348. extrgbx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  349. output_buf);
  350. break;
  351. case JCS_EXT_BGR:
  352. extbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  353. output_buf);
  354. break;
  355. case JCS_EXT_BGRX:
  356. case JCS_EXT_BGRA:
  357. extbgrx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  358. output_buf);
  359. break;
  360. case JCS_EXT_XBGR:
  361. case JCS_EXT_ABGR:
  362. extxbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  363. output_buf);
  364. break;
  365. case JCS_EXT_XRGB:
  366. case JCS_EXT_ARGB:
  367. extxrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  368. output_buf);
  369. break;
  370. default:
  371. h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  372. output_buf);
  373. break;
  374. }
  375. }
  376. /*
  377. * RGB565 conversion
  378. */
  379. #define PACK_SHORT_565_LE(r, g, b) ((((r) << 8) & 0xF800) | \
  380. (((g) << 3) & 0x7E0) | ((b) >> 3))
  381. #define PACK_SHORT_565_BE(r, g, b) (((r) & 0xF8) | ((g) >> 5) | \
  382. (((g) << 11) & 0xE000) | \
  383. (((b) << 5) & 0x1F00))
  384. #define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l)
  385. #define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r)
  386. #define PACK_NEED_ALIGNMENT(ptr) (((size_t)(ptr)) & 3)
  387. #define WRITE_TWO_PIXELS_LE(addr, pixels) { \
  388. ((INT16*)(addr))[0] = (INT16)(pixels); \
  389. ((INT16*)(addr))[1] = (INT16)((pixels) >> 16); \
  390. }
  391. #define WRITE_TWO_PIXELS_BE(addr, pixels) { \
  392. ((INT16*)(addr))[1] = (INT16)(pixels); \
  393. ((INT16*)(addr))[0] = (INT16)((pixels) >> 16); \
  394. }
  395. #define DITHER_565_R(r, dither) ((r) + ((dither) & 0xFF))
  396. #define DITHER_565_G(g, dither) ((g) + (((dither) & 0xFF) >> 1))
  397. #define DITHER_565_B(b, dither) ((b) + ((dither) & 0xFF))
  398. /* Declarations for ordered dithering
  399. *
  400. * We use a 4x4 ordered dither array packed into 32 bits. This array is
  401. * sufficent for dithering RGB888 to RGB565.
  402. */
  403. #define DITHER_MASK 0x3
  404. #define DITHER_ROTATE(x) ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
  405. static const JLONG dither_matrix[4] = {
  406. 0x0008020A,
  407. 0x0C040E06,
  408. 0x030B0109,
  409. 0x0F070D05
  410. };
  411. /* Include inline routines for RGB565 conversion */
  412. #define PACK_SHORT_565 PACK_SHORT_565_LE
  413. #define PACK_TWO_PIXELS PACK_TWO_PIXELS_LE
  414. #define WRITE_TWO_PIXELS WRITE_TWO_PIXELS_LE
  415. #define h2v1_merged_upsample_565_internal h2v1_merged_upsample_565_le
  416. #define h2v1_merged_upsample_565D_internal h2v1_merged_upsample_565D_le
  417. #define h2v2_merged_upsample_565_internal h2v2_merged_upsample_565_le
  418. #define h2v2_merged_upsample_565D_internal h2v2_merged_upsample_565D_le
  419. #include "jdmrg565.c"
  420. #undef PACK_SHORT_565
  421. #undef PACK_TWO_PIXELS
  422. #undef WRITE_TWO_PIXELS
  423. #undef h2v1_merged_upsample_565_internal
  424. #undef h2v1_merged_upsample_565D_internal
  425. #undef h2v2_merged_upsample_565_internal
  426. #undef h2v2_merged_upsample_565D_internal
  427. #define PACK_SHORT_565 PACK_SHORT_565_BE
  428. #define PACK_TWO_PIXELS PACK_TWO_PIXELS_BE
  429. #define WRITE_TWO_PIXELS WRITE_TWO_PIXELS_BE
  430. #define h2v1_merged_upsample_565_internal h2v1_merged_upsample_565_be
  431. #define h2v1_merged_upsample_565D_internal h2v1_merged_upsample_565D_be
  432. #define h2v2_merged_upsample_565_internal h2v2_merged_upsample_565_be
  433. #define h2v2_merged_upsample_565D_internal h2v2_merged_upsample_565D_be
  434. #include "jdmrg565.c"
  435. #undef PACK_SHORT_565
  436. #undef PACK_TWO_PIXELS
  437. #undef WRITE_TWO_PIXELS
  438. #undef h2v1_merged_upsample_565_internal
  439. #undef h2v1_merged_upsample_565D_internal
  440. #undef h2v2_merged_upsample_565_internal
  441. #undef h2v2_merged_upsample_565D_internal
  442. static INLINE boolean is_big_endian(void)
  443. {
  444. int test_value = 1;
  445. if(*(char *)&test_value != 1)
  446. return TRUE;
  447. return FALSE;
  448. }
  449. METHODDEF(void)
  450. h2v1_merged_upsample_565 (j_decompress_ptr cinfo,
  451. JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
  452. JSAMPARRAY output_buf)
  453. {
  454. if (is_big_endian())
  455. h2v1_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
  456. output_buf);
  457. else
  458. h2v1_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
  459. output_buf);
  460. }
  461. METHODDEF(void)
  462. h2v1_merged_upsample_565D (j_decompress_ptr cinfo,
  463. JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
  464. JSAMPARRAY output_buf)
  465. {
  466. if (is_big_endian())
  467. h2v1_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
  468. output_buf);
  469. else
  470. h2v1_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
  471. output_buf);
  472. }
  473. METHODDEF(void)
  474. h2v2_merged_upsample_565 (j_decompress_ptr cinfo,
  475. JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
  476. JSAMPARRAY output_buf)
  477. {
  478. if (is_big_endian())
  479. h2v2_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
  480. output_buf);
  481. else
  482. h2v2_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
  483. output_buf);
  484. }
  485. METHODDEF(void)
  486. h2v2_merged_upsample_565D (j_decompress_ptr cinfo,
  487. JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
  488. JSAMPARRAY output_buf)
  489. {
  490. if (is_big_endian())
  491. h2v2_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
  492. output_buf);
  493. else
  494. h2v2_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
  495. output_buf);
  496. }
  497. /*
  498. * Module initialization routine for merged upsampling/color conversion.
  499. *
  500. * NB: this is called under the conditions determined by use_merged_upsample()
  501. * in jdmaster.c. That routine MUST correspond to the actual capabilities
  502. * of this module; no safety checks are made here.
  503. */
  504. GLOBAL(void)
  505. jinit_merged_upsampler (j_decompress_ptr cinfo)
  506. {
  507. my_upsample_ptr upsample;
  508. upsample = (my_upsample_ptr)
  509. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  510. sizeof(my_upsampler));
  511. cinfo->upsample = (struct jpeg_upsampler *) upsample;
  512. upsample->pub.start_pass = start_pass_merged_upsample;
  513. upsample->pub.need_context_rows = FALSE;
  514. upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
  515. if (cinfo->max_v_samp_factor == 2) {
  516. upsample->pub.upsample = merged_2v_upsample;
  517. if (jsimd_can_h2v2_merged_upsample())
  518. upsample->upmethod = jsimd_h2v2_merged_upsample;
  519. else
  520. upsample->upmethod = h2v2_merged_upsample;
  521. if (cinfo->out_color_space == JCS_RGB565) {
  522. if (cinfo->dither_mode != JDITHER_NONE) {
  523. upsample->upmethod = h2v2_merged_upsample_565D;
  524. } else {
  525. upsample->upmethod = h2v2_merged_upsample_565;
  526. }
  527. }
  528. /* Allocate a spare row buffer */
  529. upsample->spare_row = (JSAMPROW)
  530. (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  531. (size_t) (upsample->out_row_width * sizeof(JSAMPLE)));
  532. } else {
  533. upsample->pub.upsample = merged_1v_upsample;
  534. if (jsimd_can_h2v1_merged_upsample())
  535. upsample->upmethod = jsimd_h2v1_merged_upsample;
  536. else
  537. upsample->upmethod = h2v1_merged_upsample;
  538. if (cinfo->out_color_space == JCS_RGB565) {
  539. if (cinfo->dither_mode != JDITHER_NONE) {
  540. upsample->upmethod = h2v1_merged_upsample_565D;
  541. } else {
  542. upsample->upmethod = h2v1_merged_upsample_565;
  543. }
  544. }
  545. /* No spare row needed */
  546. upsample->spare_row = NULL;
  547. }
  548. build_ycc_rgb_table(cinfo);
  549. }
  550. #endif /* UPSAMPLE_MERGING_SUPPORTED */