jdmerge.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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, JSAMPIMAGE input_buf,
  220. JDIMENSION *in_row_group_ctr,
  221. JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
  222. JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
  223. /* 2:1 vertical sampling case: may need a spare row. */
  224. {
  225. my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
  226. JSAMPROW work_ptrs[2];
  227. JDIMENSION num_rows; /* number of rows returned to caller */
  228. if (upsample->spare_full) {
  229. /* If we have a spare row saved from a previous cycle, just return it. */
  230. JDIMENSION size = upsample->out_row_width;
  231. if (cinfo->out_color_space == JCS_RGB565)
  232. size = cinfo->output_width * 2;
  233. jcopy_sample_rows(&upsample->spare_row, 0, output_buf + *out_row_ctr, 0, 1,
  234. size);
  235. num_rows = 1;
  236. upsample->spare_full = FALSE;
  237. } else {
  238. /* Figure number of rows to return to caller. */
  239. num_rows = 2;
  240. /* Not more than the distance to the end of the image. */
  241. if (num_rows > upsample->rows_to_go)
  242. num_rows = upsample->rows_to_go;
  243. /* And not more than what the client can accept: */
  244. out_rows_avail -= *out_row_ctr;
  245. if (num_rows > out_rows_avail)
  246. num_rows = out_rows_avail;
  247. /* Create output pointer array for upsampler. */
  248. work_ptrs[0] = output_buf[*out_row_ctr];
  249. if (num_rows > 1) {
  250. work_ptrs[1] = output_buf[*out_row_ctr + 1];
  251. } else {
  252. work_ptrs[1] = upsample->spare_row;
  253. upsample->spare_full = TRUE;
  254. }
  255. /* Now do the upsampling. */
  256. (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
  257. }
  258. /* Adjust counts */
  259. *out_row_ctr += num_rows;
  260. upsample->rows_to_go -= num_rows;
  261. /* When the buffer is emptied, declare this input row group consumed */
  262. if (!upsample->spare_full)
  263. (*in_row_group_ctr)++;
  264. }
  265. METHODDEF(void)
  266. merged_1v_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  267. JDIMENSION *in_row_group_ctr,
  268. JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
  269. JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
  270. /* 1:1 vertical sampling case: much easier, never need a spare row. */
  271. {
  272. my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
  273. /* Just do the upsampling. */
  274. (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
  275. output_buf + *out_row_ctr);
  276. /* Adjust counts */
  277. (*out_row_ctr)++;
  278. (*in_row_group_ctr)++;
  279. }
  280. /*
  281. * These are the routines invoked by the control routines to do
  282. * the actual upsampling/conversion. One row group is processed per call.
  283. *
  284. * Note: since we may be writing directly into application-supplied buffers,
  285. * we have to be honest about the output width; we can't assume the buffer
  286. * has been rounded up to an even width.
  287. */
  288. /*
  289. * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
  290. */
  291. METHODDEF(void)
  292. h2v1_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  293. JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
  294. {
  295. switch (cinfo->out_color_space) {
  296. case JCS_EXT_RGB:
  297. extrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  298. output_buf);
  299. break;
  300. case JCS_EXT_RGBX:
  301. case JCS_EXT_RGBA:
  302. extrgbx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  303. output_buf);
  304. break;
  305. case JCS_EXT_BGR:
  306. extbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  307. output_buf);
  308. break;
  309. case JCS_EXT_BGRX:
  310. case JCS_EXT_BGRA:
  311. extbgrx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  312. output_buf);
  313. break;
  314. case JCS_EXT_XBGR:
  315. case JCS_EXT_ABGR:
  316. extxbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  317. output_buf);
  318. break;
  319. case JCS_EXT_XRGB:
  320. case JCS_EXT_ARGB:
  321. extxrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  322. output_buf);
  323. break;
  324. default:
  325. h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  326. output_buf);
  327. break;
  328. }
  329. }
  330. /*
  331. * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
  332. */
  333. METHODDEF(void)
  334. h2v2_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  335. JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
  336. {
  337. switch (cinfo->out_color_space) {
  338. case JCS_EXT_RGB:
  339. extrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  340. output_buf);
  341. break;
  342. case JCS_EXT_RGBX:
  343. case JCS_EXT_RGBA:
  344. extrgbx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  345. output_buf);
  346. break;
  347. case JCS_EXT_BGR:
  348. extbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  349. output_buf);
  350. break;
  351. case JCS_EXT_BGRX:
  352. case JCS_EXT_BGRA:
  353. extbgrx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  354. output_buf);
  355. break;
  356. case JCS_EXT_XBGR:
  357. case JCS_EXT_ABGR:
  358. extxbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  359. output_buf);
  360. break;
  361. case JCS_EXT_XRGB:
  362. case JCS_EXT_ARGB:
  363. extxrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  364. output_buf);
  365. break;
  366. default:
  367. h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  368. output_buf);
  369. break;
  370. }
  371. }
  372. /*
  373. * RGB565 conversion
  374. */
  375. #define PACK_SHORT_565_LE(r, g, b) ((((r) << 8) & 0xF800) | \
  376. (((g) << 3) & 0x7E0) | ((b) >> 3))
  377. #define PACK_SHORT_565_BE(r, g, b) (((r) & 0xF8) | ((g) >> 5) | \
  378. (((g) << 11) & 0xE000) | \
  379. (((b) << 5) & 0x1F00))
  380. #define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l)
  381. #define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r)
  382. #define WRITE_TWO_PIXELS_LE(addr, pixels) { \
  383. ((INT16 *)(addr))[0] = (INT16)(pixels); \
  384. ((INT16 *)(addr))[1] = (INT16)((pixels) >> 16); \
  385. }
  386. #define WRITE_TWO_PIXELS_BE(addr, pixels) { \
  387. ((INT16 *)(addr))[1] = (INT16)(pixels); \
  388. ((INT16 *)(addr))[0] = (INT16)((pixels) >> 16); \
  389. }
  390. #define DITHER_565_R(r, dither) ((r) + ((dither) & 0xFF))
  391. #define DITHER_565_G(g, dither) ((g) + (((dither) & 0xFF) >> 1))
  392. #define DITHER_565_B(b, dither) ((b) + ((dither) & 0xFF))
  393. /* Declarations for ordered dithering
  394. *
  395. * We use a 4x4 ordered dither array packed into 32 bits. This array is
  396. * sufficient for dithering RGB888 to RGB565.
  397. */
  398. #define DITHER_MASK 0x3
  399. #define DITHER_ROTATE(x) ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
  400. static const JLONG dither_matrix[4] = {
  401. 0x0008020A,
  402. 0x0C040E06,
  403. 0x030B0109,
  404. 0x0F070D05
  405. };
  406. /* Include inline routines for RGB565 conversion */
  407. #define PACK_SHORT_565 PACK_SHORT_565_LE
  408. #define PACK_TWO_PIXELS PACK_TWO_PIXELS_LE
  409. #define WRITE_TWO_PIXELS WRITE_TWO_PIXELS_LE
  410. #define h2v1_merged_upsample_565_internal h2v1_merged_upsample_565_le
  411. #define h2v1_merged_upsample_565D_internal h2v1_merged_upsample_565D_le
  412. #define h2v2_merged_upsample_565_internal h2v2_merged_upsample_565_le
  413. #define h2v2_merged_upsample_565D_internal h2v2_merged_upsample_565D_le
  414. #include "jdmrg565.c"
  415. #undef PACK_SHORT_565
  416. #undef PACK_TWO_PIXELS
  417. #undef WRITE_TWO_PIXELS
  418. #undef h2v1_merged_upsample_565_internal
  419. #undef h2v1_merged_upsample_565D_internal
  420. #undef h2v2_merged_upsample_565_internal
  421. #undef h2v2_merged_upsample_565D_internal
  422. #define PACK_SHORT_565 PACK_SHORT_565_BE
  423. #define PACK_TWO_PIXELS PACK_TWO_PIXELS_BE
  424. #define WRITE_TWO_PIXELS WRITE_TWO_PIXELS_BE
  425. #define h2v1_merged_upsample_565_internal h2v1_merged_upsample_565_be
  426. #define h2v1_merged_upsample_565D_internal h2v1_merged_upsample_565D_be
  427. #define h2v2_merged_upsample_565_internal h2v2_merged_upsample_565_be
  428. #define h2v2_merged_upsample_565D_internal h2v2_merged_upsample_565D_be
  429. #include "jdmrg565.c"
  430. #undef PACK_SHORT_565
  431. #undef PACK_TWO_PIXELS
  432. #undef WRITE_TWO_PIXELS
  433. #undef h2v1_merged_upsample_565_internal
  434. #undef h2v1_merged_upsample_565D_internal
  435. #undef h2v2_merged_upsample_565_internal
  436. #undef h2v2_merged_upsample_565D_internal
  437. static INLINE boolean is_big_endian(void)
  438. {
  439. int test_value = 1;
  440. if (*(char *)&test_value != 1)
  441. return TRUE;
  442. return FALSE;
  443. }
  444. METHODDEF(void)
  445. h2v1_merged_upsample_565(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  446. JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
  447. {
  448. if (is_big_endian())
  449. h2v1_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
  450. output_buf);
  451. else
  452. h2v1_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
  453. output_buf);
  454. }
  455. METHODDEF(void)
  456. h2v1_merged_upsample_565D(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  457. JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
  458. {
  459. if (is_big_endian())
  460. h2v1_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
  461. output_buf);
  462. else
  463. h2v1_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
  464. output_buf);
  465. }
  466. METHODDEF(void)
  467. h2v2_merged_upsample_565(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  468. JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
  469. {
  470. if (is_big_endian())
  471. h2v2_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
  472. output_buf);
  473. else
  474. h2v2_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
  475. output_buf);
  476. }
  477. METHODDEF(void)
  478. h2v2_merged_upsample_565D(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  479. JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
  480. {
  481. if (is_big_endian())
  482. h2v2_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
  483. output_buf);
  484. else
  485. h2v2_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
  486. output_buf);
  487. }
  488. /*
  489. * Module initialization routine for merged upsampling/color conversion.
  490. *
  491. * NB: this is called under the conditions determined by use_merged_upsample()
  492. * in jdmaster.c. That routine MUST correspond to the actual capabilities
  493. * of this module; no safety checks are made here.
  494. */
  495. GLOBAL(void)
  496. jinit_merged_upsampler(j_decompress_ptr cinfo)
  497. {
  498. my_upsample_ptr upsample;
  499. upsample = (my_upsample_ptr)
  500. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  501. sizeof(my_upsampler));
  502. cinfo->upsample = (struct jpeg_upsampler *)upsample;
  503. upsample->pub.start_pass = start_pass_merged_upsample;
  504. upsample->pub.need_context_rows = FALSE;
  505. upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
  506. if (cinfo->max_v_samp_factor == 2) {
  507. upsample->pub.upsample = merged_2v_upsample;
  508. if (jsimd_can_h2v2_merged_upsample())
  509. upsample->upmethod = jsimd_h2v2_merged_upsample;
  510. else
  511. upsample->upmethod = h2v2_merged_upsample;
  512. if (cinfo->out_color_space == JCS_RGB565) {
  513. if (cinfo->dither_mode != JDITHER_NONE) {
  514. upsample->upmethod = h2v2_merged_upsample_565D;
  515. } else {
  516. upsample->upmethod = h2v2_merged_upsample_565;
  517. }
  518. }
  519. /* Allocate a spare row buffer */
  520. upsample->spare_row = (JSAMPROW)
  521. (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  522. (size_t)(upsample->out_row_width * sizeof(JSAMPLE)));
  523. } else {
  524. upsample->pub.upsample = merged_1v_upsample;
  525. if (jsimd_can_h2v1_merged_upsample())
  526. upsample->upmethod = jsimd_h2v1_merged_upsample;
  527. else
  528. upsample->upmethod = h2v1_merged_upsample;
  529. if (cinfo->out_color_space == JCS_RGB565) {
  530. if (cinfo->dither_mode != JDITHER_NONE) {
  531. upsample->upmethod = h2v1_merged_upsample_565D;
  532. } else {
  533. upsample->upmethod = h2v1_merged_upsample_565;
  534. }
  535. }
  536. /* No spare row needed */
  537. upsample->spare_row = NULL;
  538. }
  539. build_ycc_rgb_table(cinfo);
  540. }
  541. #endif /* UPSAMPLE_MERGING_SUPPORTED */