jdmerge.c 19 KB

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