jdmerge.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. * Copyright 2009 Pierre Ossman <[email protected]> for Cendio AB
  7. * libjpeg-turbo Modifications:
  8. * Copyright (C) 2009, 2011, D. R. Commander.
  9. * For conditions of distribution and use, see the accompanying README file.
  10. *
  11. * This file contains code for merged upsampling/color conversion.
  12. *
  13. * This file combines functions from jdsample.c and jdcolor.c;
  14. * read those files first to understand what's going on.
  15. *
  16. * When the chroma components are to be upsampled by simple replication
  17. * (ie, box filtering), we can save some work in color conversion by
  18. * calculating all the output pixels corresponding to a pair of chroma
  19. * samples at one time. In the conversion equations
  20. * R = Y + K1 * Cr
  21. * G = Y + K2 * Cb + K3 * Cr
  22. * B = Y + K4 * Cb
  23. * only the Y term varies among the group of pixels corresponding to a pair
  24. * of chroma samples, so the rest of the terms can be calculated just once.
  25. * At typical sampling ratios, this eliminates half or three-quarters of the
  26. * multiplications needed for color conversion.
  27. *
  28. * This file currently provides implementations for the following cases:
  29. * YCbCr => RGB color conversion only.
  30. * Sampling ratios of 2h1v or 2h2v.
  31. * No scaling needed at upsample time.
  32. * Corner-aligned (non-CCIR601) sampling alignment.
  33. * Other special cases could be added, but in most applications these are
  34. * the only common cases. (For uncommon cases we fall back on the more
  35. * general code in jdsample.c and jdcolor.c.)
  36. */
  37. #define JPEG_INTERNALS
  38. #include "jinclude.h"
  39. #include "jpeglib.h"
  40. #include "jsimd.h"
  41. #include "config.h"
  42. #ifdef UPSAMPLE_MERGING_SUPPORTED
  43. /* Private subobject */
  44. typedef struct {
  45. struct jpeg_upsampler pub; /* public fields */
  46. /* Pointer to routine to do actual upsampling/conversion of one row group */
  47. JMETHOD(void, upmethod, (j_decompress_ptr cinfo,
  48. JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
  49. JSAMPARRAY output_buf));
  50. /* Private state for YCC->RGB conversion */
  51. int * Cr_r_tab; /* => table for Cr to R conversion */
  52. int * Cb_b_tab; /* => table for Cb to B conversion */
  53. INT32 * Cr_g_tab; /* => table for Cr to G conversion */
  54. INT32 * Cb_g_tab; /* => table for Cb to G conversion */
  55. /* For 2:1 vertical sampling, we produce two output rows at a time.
  56. * We need a "spare" row buffer to hold the second output row if the
  57. * application provides just a one-row buffer; we also use the spare
  58. * to discard the dummy last row if the image height is odd.
  59. */
  60. JSAMPROW spare_row;
  61. boolean spare_full; /* T if spare buffer is occupied */
  62. JDIMENSION out_row_width; /* samples per output row */
  63. JDIMENSION rows_to_go; /* counts rows remaining in image */
  64. } my_upsampler;
  65. typedef my_upsampler * my_upsample_ptr;
  66. #define SCALEBITS 16 /* speediest right-shift on some machines */
  67. #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
  68. #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
  69. /* Include inline routines for colorspace extensions */
  70. #include "jdmrgext.c"
  71. #undef RGB_RED
  72. #undef RGB_GREEN
  73. #undef RGB_BLUE
  74. #undef RGB_PIXELSIZE
  75. #define RGB_RED EXT_RGB_RED
  76. #define RGB_GREEN EXT_RGB_GREEN
  77. #define RGB_BLUE EXT_RGB_BLUE
  78. #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
  79. #define h2v1_merged_upsample_internal extrgb_h2v1_merged_upsample_internal
  80. #define h2v2_merged_upsample_internal extrgb_h2v2_merged_upsample_internal
  81. #include "jdmrgext.c"
  82. #undef RGB_RED
  83. #undef RGB_GREEN
  84. #undef RGB_BLUE
  85. #undef RGB_PIXELSIZE
  86. #undef h2v1_merged_upsample_internal
  87. #undef h2v2_merged_upsample_internal
  88. #define RGB_RED EXT_RGBX_RED
  89. #define RGB_GREEN EXT_RGBX_GREEN
  90. #define RGB_BLUE EXT_RGBX_BLUE
  91. #define RGB_ALPHA 3
  92. #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
  93. #define h2v1_merged_upsample_internal extrgbx_h2v1_merged_upsample_internal
  94. #define h2v2_merged_upsample_internal extrgbx_h2v2_merged_upsample_internal
  95. #include "jdmrgext.c"
  96. #undef RGB_RED
  97. #undef RGB_GREEN
  98. #undef RGB_BLUE
  99. #undef RGB_ALPHA
  100. #undef RGB_PIXELSIZE
  101. #undef h2v1_merged_upsample_internal
  102. #undef h2v2_merged_upsample_internal
  103. #define RGB_RED EXT_BGR_RED
  104. #define RGB_GREEN EXT_BGR_GREEN
  105. #define RGB_BLUE EXT_BGR_BLUE
  106. #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
  107. #define h2v1_merged_upsample_internal extbgr_h2v1_merged_upsample_internal
  108. #define h2v2_merged_upsample_internal extbgr_h2v2_merged_upsample_internal
  109. #include "jdmrgext.c"
  110. #undef RGB_RED
  111. #undef RGB_GREEN
  112. #undef RGB_BLUE
  113. #undef RGB_PIXELSIZE
  114. #undef h2v1_merged_upsample_internal
  115. #undef h2v2_merged_upsample_internal
  116. #define RGB_RED EXT_BGRX_RED
  117. #define RGB_GREEN EXT_BGRX_GREEN
  118. #define RGB_BLUE EXT_BGRX_BLUE
  119. #define RGB_ALPHA 3
  120. #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
  121. #define h2v1_merged_upsample_internal extbgrx_h2v1_merged_upsample_internal
  122. #define h2v2_merged_upsample_internal extbgrx_h2v2_merged_upsample_internal
  123. #include "jdmrgext.c"
  124. #undef RGB_RED
  125. #undef RGB_GREEN
  126. #undef RGB_BLUE
  127. #undef RGB_ALPHA
  128. #undef RGB_PIXELSIZE
  129. #undef h2v1_merged_upsample_internal
  130. #undef h2v2_merged_upsample_internal
  131. #define RGB_RED EXT_XBGR_RED
  132. #define RGB_GREEN EXT_XBGR_GREEN
  133. #define RGB_BLUE EXT_XBGR_BLUE
  134. #define RGB_ALPHA 0
  135. #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
  136. #define h2v1_merged_upsample_internal extxbgr_h2v1_merged_upsample_internal
  137. #define h2v2_merged_upsample_internal extxbgr_h2v2_merged_upsample_internal
  138. #include "jdmrgext.c"
  139. #undef RGB_RED
  140. #undef RGB_GREEN
  141. #undef RGB_BLUE
  142. #undef RGB_ALPHA
  143. #undef RGB_PIXELSIZE
  144. #undef h2v1_merged_upsample_internal
  145. #undef h2v2_merged_upsample_internal
  146. #define RGB_RED EXT_XRGB_RED
  147. #define RGB_GREEN EXT_XRGB_GREEN
  148. #define RGB_BLUE EXT_XRGB_BLUE
  149. #define RGB_ALPHA 0
  150. #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
  151. #define h2v1_merged_upsample_internal extxrgb_h2v1_merged_upsample_internal
  152. #define h2v2_merged_upsample_internal extxrgb_h2v2_merged_upsample_internal
  153. #include "jdmrgext.c"
  154. #undef RGB_RED
  155. #undef RGB_GREEN
  156. #undef RGB_BLUE
  157. #undef RGB_ALPHA
  158. #undef RGB_PIXELSIZE
  159. #undef h2v1_merged_upsample_internal
  160. #undef h2v2_merged_upsample_internal
  161. /*
  162. * Initialize tables for YCC->RGB colorspace conversion.
  163. * This is taken directly from jdcolor.c; see that file for more info.
  164. */
  165. LOCAL(void)
  166. build_ycc_rgb_table (j_decompress_ptr cinfo)
  167. {
  168. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  169. int i;
  170. INT32 x;
  171. SHIFT_TEMPS
  172. upsample->Cr_r_tab = (int *)
  173. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  174. (MAXJSAMPLE+1) * SIZEOF(int));
  175. upsample->Cb_b_tab = (int *)
  176. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  177. (MAXJSAMPLE+1) * SIZEOF(int));
  178. upsample->Cr_g_tab = (INT32 *)
  179. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  180. (MAXJSAMPLE+1) * SIZEOF(INT32));
  181. upsample->Cb_g_tab = (INT32 *)
  182. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  183. (MAXJSAMPLE+1) * SIZEOF(INT32));
  184. for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
  185. /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
  186. /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
  187. /* Cr=>R value is nearest int to 1.40200 * x */
  188. upsample->Cr_r_tab[i] = (int)
  189. RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
  190. /* Cb=>B value is nearest int to 1.77200 * x */
  191. upsample->Cb_b_tab[i] = (int)
  192. RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
  193. /* Cr=>G value is scaled-up -0.71414 * x */
  194. upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x;
  195. /* Cb=>G value is scaled-up -0.34414 * x */
  196. /* We also add in ONE_HALF so that need not do it in inner loop */
  197. upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
  198. }
  199. }
  200. /*
  201. * Initialize for an upsampling pass.
  202. */
  203. METHODDEF(void)
  204. start_pass_merged_upsample (j_decompress_ptr cinfo)
  205. {
  206. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  207. /* Mark the spare buffer empty */
  208. upsample->spare_full = FALSE;
  209. /* Initialize total-height counter for detecting bottom of image */
  210. upsample->rows_to_go = cinfo->output_height;
  211. }
  212. /*
  213. * Control routine to do upsampling (and color conversion).
  214. *
  215. * The control routine just handles the row buffering considerations.
  216. */
  217. METHODDEF(void)
  218. merged_2v_upsample (j_decompress_ptr cinfo,
  219. JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  220. JDIMENSION in_row_groups_avail,
  221. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  222. 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. jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
  231. 1, upsample->out_row_width);
  232. num_rows = 1;
  233. upsample->spare_full = FALSE;
  234. } else {
  235. /* Figure number of rows to return to caller. */
  236. num_rows = 2;
  237. /* Not more than the distance to the end of the image. */
  238. if (num_rows > upsample->rows_to_go)
  239. num_rows = upsample->rows_to_go;
  240. /* And not more than what the client can accept: */
  241. out_rows_avail -= *out_row_ctr;
  242. if (num_rows > out_rows_avail)
  243. num_rows = out_rows_avail;
  244. /* Create output pointer array for upsampler. */
  245. work_ptrs[0] = output_buf[*out_row_ctr];
  246. if (num_rows > 1) {
  247. work_ptrs[1] = output_buf[*out_row_ctr + 1];
  248. } else {
  249. work_ptrs[1] = upsample->spare_row;
  250. upsample->spare_full = TRUE;
  251. }
  252. /* Now do the upsampling. */
  253. (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
  254. }
  255. /* Adjust counts */
  256. *out_row_ctr += num_rows;
  257. upsample->rows_to_go -= num_rows;
  258. /* When the buffer is emptied, declare this input row group consumed */
  259. if (! upsample->spare_full)
  260. (*in_row_group_ctr)++;
  261. }
  262. METHODDEF(void)
  263. merged_1v_upsample (j_decompress_ptr cinfo,
  264. JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  265. JDIMENSION in_row_groups_avail,
  266. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  267. JDIMENSION out_rows_avail)
  268. /* 1:1 vertical sampling case: much easier, never need a spare row. */
  269. {
  270. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  271. /* Just do the upsampling. */
  272. (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
  273. output_buf + *out_row_ctr);
  274. /* Adjust counts */
  275. (*out_row_ctr)++;
  276. (*in_row_group_ctr)++;
  277. }
  278. /*
  279. * These are the routines invoked by the control routines to do
  280. * the actual upsampling/conversion. One row group is processed per call.
  281. *
  282. * Note: since we may be writing directly into application-supplied buffers,
  283. * we have to be honest about the output width; we can't assume the buffer
  284. * has been rounded up to an even width.
  285. */
  286. /*
  287. * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
  288. */
  289. METHODDEF(void)
  290. h2v1_merged_upsample (j_decompress_ptr cinfo,
  291. JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
  292. JSAMPARRAY output_buf)
  293. {
  294. switch (cinfo->out_color_space) {
  295. case JCS_EXT_RGB:
  296. extrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  297. output_buf);
  298. break;
  299. case JCS_EXT_RGBX:
  300. case JCS_EXT_RGBA:
  301. extrgbx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  302. output_buf);
  303. break;
  304. case JCS_EXT_BGR:
  305. extbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  306. output_buf);
  307. break;
  308. case JCS_EXT_BGRX:
  309. case JCS_EXT_BGRA:
  310. extbgrx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  311. output_buf);
  312. break;
  313. case JCS_EXT_XBGR:
  314. case JCS_EXT_ABGR:
  315. extxbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  316. output_buf);
  317. break;
  318. case JCS_EXT_XRGB:
  319. case JCS_EXT_ARGB:
  320. extxrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  321. output_buf);
  322. break;
  323. default:
  324. h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
  325. output_buf);
  326. break;
  327. }
  328. }
  329. /*
  330. * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
  331. */
  332. METHODDEF(void)
  333. h2v2_merged_upsample (j_decompress_ptr cinfo,
  334. JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
  335. 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. * Module initialization routine for merged upsampling/color conversion.
  374. *
  375. * NB: this is called under the conditions determined by use_merged_upsample()
  376. * in jdmaster.c. That routine MUST correspond to the actual capabilities
  377. * of this module; no safety checks are made here.
  378. */
  379. GLOBAL(void)
  380. jinit_merged_upsampler (j_decompress_ptr cinfo)
  381. {
  382. my_upsample_ptr upsample;
  383. upsample = (my_upsample_ptr)
  384. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  385. SIZEOF(my_upsampler));
  386. cinfo->upsample = (struct jpeg_upsampler *) upsample;
  387. upsample->pub.start_pass = start_pass_merged_upsample;
  388. upsample->pub.need_context_rows = FALSE;
  389. upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
  390. if (cinfo->max_v_samp_factor == 2) {
  391. upsample->pub.upsample = merged_2v_upsample;
  392. if (jsimd_can_h2v2_merged_upsample())
  393. upsample->upmethod = jsimd_h2v2_merged_upsample;
  394. else
  395. upsample->upmethod = h2v2_merged_upsample;
  396. /* Allocate a spare row buffer */
  397. upsample->spare_row = (JSAMPROW)
  398. (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  399. (size_t) (upsample->out_row_width * SIZEOF(JSAMPLE)));
  400. } else {
  401. upsample->pub.upsample = merged_1v_upsample;
  402. if (jsimd_can_h2v1_merged_upsample())
  403. upsample->upmethod = jsimd_h2v1_merged_upsample;
  404. else
  405. upsample->upmethod = h2v1_merged_upsample;
  406. /* No spare row needed */
  407. upsample->spare_row = NULL;
  408. }
  409. build_ycc_rgb_table(cinfo);
  410. }
  411. #endif /* UPSAMPLE_MERGING_SUPPORTED */