jdmerge.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * jdmerge.c
  3. *
  4. * Copyright (C) 1994-1996, Thomas G. Lane.
  5. * Modified 2013-2020 by Guido Vollbeding.
  6. * This file is part of the Independent JPEG Group's software.
  7. * For conditions of distribution and use, see the accompanying README file.
  8. *
  9. * This file contains code for merged upsampling/color conversion.
  10. *
  11. * This file combines functions from jdsample.c and jdcolor.c;
  12. * read those files first to understand what's going on.
  13. *
  14. * When the chroma components are to be upsampled by simple replication
  15. * (ie, box filtering), we can save some work in color conversion by
  16. * calculating all the output pixels corresponding to a pair of chroma
  17. * samples at one time. In the conversion equations
  18. * R = Y + K1 * Cr
  19. * G = Y + K2 * Cb + K3 * Cr
  20. * B = Y + K4 * Cb
  21. * only the Y term varies among the group of pixels corresponding to a pair
  22. * of chroma samples, so the rest of the terms can be calculated just once.
  23. * At typical sampling ratios, this eliminates half or three-quarters of the
  24. * multiplications needed for color conversion.
  25. *
  26. * This file currently provides implementations for the following cases:
  27. * YCC => RGB color conversion only (YCbCr or BG_YCC).
  28. * Sampling ratios of 2h1v or 2h2v.
  29. * No scaling needed at upsample time.
  30. * Corner-aligned (non-CCIR601) sampling alignment.
  31. * Other special cases could be added, but in most applications these are
  32. * the only common cases. (For uncommon cases we fall back on the more
  33. * general code in jdsample.c and jdcolor.c.)
  34. */
  35. #define JPEG_INTERNALS
  36. #include "jinclude.h"
  37. #include "jpeglib.h"
  38. #ifdef UPSAMPLE_MERGING_SUPPORTED
  39. #if RANGE_BITS < 2
  40. /* Deliberate syntax err */
  41. Sorry, this code requires 2 or more range extension bits.
  42. #endif
  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. /*
  70. * Initialize tables for YCbCr->RGB and BG_YCC->RGB colorspace conversion.
  71. * This is taken directly from jdcolor.c; see that file for more info.
  72. */
  73. LOCAL(void)
  74. build_ycc_rgb_table (j_decompress_ptr cinfo)
  75. /* Normal case, sYCC */
  76. {
  77. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  78. int i;
  79. INT32 x;
  80. SHIFT_TEMPS
  81. upsample->Cr_r_tab = (int *) (*cinfo->mem->alloc_small)
  82. ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
  83. upsample->Cb_b_tab = (int *) (*cinfo->mem->alloc_small)
  84. ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
  85. upsample->Cr_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
  86. ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
  87. upsample->Cb_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
  88. ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
  89. for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
  90. /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
  91. /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
  92. /* Cr=>R value is nearest int to 1.402 * x */
  93. upsample->Cr_r_tab[i] = (int) DESCALE(FIX(1.402) * x, SCALEBITS);
  94. /* Cb=>B value is nearest int to 1.772 * x */
  95. upsample->Cb_b_tab[i] = (int) DESCALE(FIX(1.772) * x, SCALEBITS);
  96. /* Cr=>G value is scaled-up -0.714136286 * x */
  97. upsample->Cr_g_tab[i] = (- FIX(0.714136286)) * x;
  98. /* Cb=>G value is scaled-up -0.344136286 * x */
  99. /* We also add in ONE_HALF so that need not do it in inner loop */
  100. upsample->Cb_g_tab[i] = (- FIX(0.344136286)) * x + ONE_HALF;
  101. }
  102. }
  103. LOCAL(void)
  104. build_bg_ycc_rgb_table (j_decompress_ptr cinfo)
  105. /* Wide gamut case, bg-sYCC */
  106. {
  107. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  108. int i;
  109. INT32 x;
  110. SHIFT_TEMPS
  111. upsample->Cr_r_tab = (int *) (*cinfo->mem->alloc_small)
  112. ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
  113. upsample->Cb_b_tab = (int *) (*cinfo->mem->alloc_small)
  114. ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
  115. upsample->Cr_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
  116. ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
  117. upsample->Cb_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
  118. ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
  119. for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
  120. /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
  121. /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
  122. /* Cr=>R value is nearest int to 2.804 * x */
  123. upsample->Cr_r_tab[i] = (int) DESCALE(FIX(2.804) * x, SCALEBITS);
  124. /* Cb=>B value is nearest int to 3.544 * x */
  125. upsample->Cb_b_tab[i] = (int) DESCALE(FIX(3.544) * x, SCALEBITS);
  126. /* Cr=>G value is scaled-up -1.428272572 * x */
  127. upsample->Cr_g_tab[i] = (- FIX(1.428272572)) * x;
  128. /* Cb=>G value is scaled-up -0.688272572 * x */
  129. /* We also add in ONE_HALF so that need not do it in inner loop */
  130. upsample->Cb_g_tab[i] = (- FIX(0.688272572)) * x + ONE_HALF;
  131. }
  132. }
  133. /*
  134. * Initialize for an upsampling pass.
  135. */
  136. METHODDEF(void)
  137. start_pass_merged_upsample (j_decompress_ptr cinfo)
  138. {
  139. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  140. /* Mark the spare buffer empty */
  141. upsample->spare_full = FALSE;
  142. /* Initialize total-height counter for detecting bottom of image */
  143. upsample->rows_to_go = cinfo->output_height;
  144. }
  145. /*
  146. * Control routine to do upsampling (and color conversion).
  147. *
  148. * The control routine just handles the row buffering considerations.
  149. */
  150. METHODDEF(void)
  151. merged_2v_upsample (j_decompress_ptr cinfo,
  152. JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  153. JDIMENSION in_row_groups_avail,
  154. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  155. JDIMENSION out_rows_avail)
  156. /* 2:1 vertical sampling case: may need a spare row. */
  157. {
  158. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  159. JSAMPROW work_ptrs[2];
  160. JDIMENSION num_rows; /* number of rows returned to caller */
  161. if (upsample->spare_full) {
  162. /* If we have a spare row saved from a previous cycle, just return it. */
  163. jcopy_sample_rows(& upsample->spare_row, output_buf + *out_row_ctr,
  164. 1, upsample->out_row_width);
  165. num_rows = 1;
  166. upsample->spare_full = FALSE;
  167. } else {
  168. /* Figure number of rows to return to caller. */
  169. num_rows = 2;
  170. /* Not more than the distance to the end of the image. */
  171. if (num_rows > upsample->rows_to_go)
  172. num_rows = upsample->rows_to_go;
  173. /* And not more than what the client can accept: */
  174. out_rows_avail -= *out_row_ctr;
  175. if (num_rows > out_rows_avail)
  176. num_rows = out_rows_avail;
  177. /* Create output pointer array for upsampler. */
  178. work_ptrs[0] = output_buf[*out_row_ctr];
  179. if (num_rows > 1) {
  180. work_ptrs[1] = output_buf[*out_row_ctr + 1];
  181. } else {
  182. work_ptrs[1] = upsample->spare_row;
  183. upsample->spare_full = TRUE;
  184. }
  185. /* Now do the upsampling. */
  186. (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
  187. }
  188. /* Adjust counts */
  189. *out_row_ctr += num_rows;
  190. upsample->rows_to_go -= num_rows;
  191. /* When the buffer is emptied, declare this input row group consumed */
  192. if (! upsample->spare_full)
  193. (*in_row_group_ctr)++;
  194. }
  195. METHODDEF(void)
  196. merged_1v_upsample (j_decompress_ptr cinfo,
  197. JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  198. JDIMENSION in_row_groups_avail,
  199. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  200. JDIMENSION out_rows_avail)
  201. /* 1:1 vertical sampling case: much easier, never need a spare row. */
  202. {
  203. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  204. /* Just do the upsampling. */
  205. (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
  206. output_buf + *out_row_ctr);
  207. /* Adjust counts */
  208. (*out_row_ctr)++;
  209. (*in_row_group_ctr)++;
  210. }
  211. /*
  212. * These are the routines invoked by the control routines to do
  213. * the actual upsampling/conversion. One row group is processed per call.
  214. *
  215. * Note: since we may be writing directly into application-supplied buffers,
  216. * we have to be honest about the output width; we can't assume the buffer
  217. * has been rounded up to an even width.
  218. */
  219. /*
  220. * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
  221. */
  222. METHODDEF(void)
  223. h2v1_merged_upsample (j_decompress_ptr cinfo,
  224. JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
  225. JSAMPARRAY output_buf)
  226. {
  227. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  228. register int y, cred, cgreen, cblue;
  229. int cb, cr;
  230. register JSAMPROW outptr;
  231. JSAMPROW inptr0, inptr1, inptr2;
  232. JDIMENSION col;
  233. /* copy these pointers into registers if possible */
  234. register JSAMPLE * range_limit = cinfo->sample_range_limit;
  235. int * Crrtab = upsample->Cr_r_tab;
  236. int * Cbbtab = upsample->Cb_b_tab;
  237. INT32 * Crgtab = upsample->Cr_g_tab;
  238. INT32 * Cbgtab = upsample->Cb_g_tab;
  239. SHIFT_TEMPS
  240. inptr0 = input_buf[0][in_row_group_ctr];
  241. inptr1 = input_buf[1][in_row_group_ctr];
  242. inptr2 = input_buf[2][in_row_group_ctr];
  243. outptr = output_buf[0];
  244. /* Loop for each pair of output pixels */
  245. for (col = cinfo->output_width >> 1; col > 0; col--) {
  246. /* Do the chroma part of the calculation */
  247. cb = GETJSAMPLE(*inptr1++);
  248. cr = GETJSAMPLE(*inptr2++);
  249. cred = Crrtab[cr];
  250. cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
  251. cblue = Cbbtab[cb];
  252. /* Fetch 2 Y values and emit 2 pixels */
  253. y = GETJSAMPLE(*inptr0++);
  254. outptr[RGB_RED] = range_limit[y + cred];
  255. outptr[RGB_GREEN] = range_limit[y + cgreen];
  256. outptr[RGB_BLUE] = range_limit[y + cblue];
  257. outptr += RGB_PIXELSIZE;
  258. y = GETJSAMPLE(*inptr0++);
  259. outptr[RGB_RED] = range_limit[y + cred];
  260. outptr[RGB_GREEN] = range_limit[y + cgreen];
  261. outptr[RGB_BLUE] = range_limit[y + cblue];
  262. outptr += RGB_PIXELSIZE;
  263. }
  264. /* If image width is odd, do the last output column separately */
  265. if (cinfo->output_width & 1) {
  266. cb = GETJSAMPLE(*inptr1);
  267. cr = GETJSAMPLE(*inptr2);
  268. cred = Crrtab[cr];
  269. cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
  270. cblue = Cbbtab[cb];
  271. y = GETJSAMPLE(*inptr0);
  272. outptr[RGB_RED] = range_limit[y + cred];
  273. outptr[RGB_GREEN] = range_limit[y + cgreen];
  274. outptr[RGB_BLUE] = range_limit[y + cblue];
  275. }
  276. }
  277. /*
  278. * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
  279. */
  280. METHODDEF(void)
  281. h2v2_merged_upsample (j_decompress_ptr cinfo,
  282. JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
  283. JSAMPARRAY output_buf)
  284. {
  285. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  286. register int y, cred, cgreen, cblue;
  287. int cb, cr;
  288. register JSAMPROW outptr0, outptr1;
  289. JSAMPROW inptr00, inptr01, inptr1, inptr2;
  290. JDIMENSION col;
  291. /* copy these pointers into registers if possible */
  292. register JSAMPLE * range_limit = cinfo->sample_range_limit;
  293. int * Crrtab = upsample->Cr_r_tab;
  294. int * Cbbtab = upsample->Cb_b_tab;
  295. INT32 * Crgtab = upsample->Cr_g_tab;
  296. INT32 * Cbgtab = upsample->Cb_g_tab;
  297. SHIFT_TEMPS
  298. inptr00 = input_buf[0][in_row_group_ctr*2];
  299. inptr01 = input_buf[0][in_row_group_ctr*2 + 1];
  300. inptr1 = input_buf[1][in_row_group_ctr];
  301. inptr2 = input_buf[2][in_row_group_ctr];
  302. outptr0 = output_buf[0];
  303. outptr1 = output_buf[1];
  304. /* Loop for each group of output pixels */
  305. for (col = cinfo->output_width >> 1; col > 0; col--) {
  306. /* Do the chroma part of the calculation */
  307. cb = GETJSAMPLE(*inptr1++);
  308. cr = GETJSAMPLE(*inptr2++);
  309. cred = Crrtab[cr];
  310. cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
  311. cblue = Cbbtab[cb];
  312. /* Fetch 4 Y values and emit 4 pixels */
  313. y = GETJSAMPLE(*inptr00++);
  314. outptr0[RGB_RED] = range_limit[y + cred];
  315. outptr0[RGB_GREEN] = range_limit[y + cgreen];
  316. outptr0[RGB_BLUE] = range_limit[y + cblue];
  317. outptr0 += RGB_PIXELSIZE;
  318. y = GETJSAMPLE(*inptr00++);
  319. outptr0[RGB_RED] = range_limit[y + cred];
  320. outptr0[RGB_GREEN] = range_limit[y + cgreen];
  321. outptr0[RGB_BLUE] = range_limit[y + cblue];
  322. outptr0 += RGB_PIXELSIZE;
  323. y = GETJSAMPLE(*inptr01++);
  324. outptr1[RGB_RED] = range_limit[y + cred];
  325. outptr1[RGB_GREEN] = range_limit[y + cgreen];
  326. outptr1[RGB_BLUE] = range_limit[y + cblue];
  327. outptr1 += RGB_PIXELSIZE;
  328. y = GETJSAMPLE(*inptr01++);
  329. outptr1[RGB_RED] = range_limit[y + cred];
  330. outptr1[RGB_GREEN] = range_limit[y + cgreen];
  331. outptr1[RGB_BLUE] = range_limit[y + cblue];
  332. outptr1 += RGB_PIXELSIZE;
  333. }
  334. /* If image width is odd, do the last output column separately */
  335. if (cinfo->output_width & 1) {
  336. cb = GETJSAMPLE(*inptr1);
  337. cr = GETJSAMPLE(*inptr2);
  338. cred = Crrtab[cr];
  339. cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
  340. cblue = Cbbtab[cb];
  341. y = GETJSAMPLE(*inptr00);
  342. outptr0[RGB_RED] = range_limit[y + cred];
  343. outptr0[RGB_GREEN] = range_limit[y + cgreen];
  344. outptr0[RGB_BLUE] = range_limit[y + cblue];
  345. y = GETJSAMPLE(*inptr01);
  346. outptr1[RGB_RED] = range_limit[y + cred];
  347. outptr1[RGB_GREEN] = range_limit[y + cgreen];
  348. outptr1[RGB_BLUE] = range_limit[y + cblue];
  349. }
  350. }
  351. /*
  352. * Module initialization routine for merged upsampling/color conversion.
  353. *
  354. * NB: this is called under the conditions determined by use_merged_upsample()
  355. * in jdmaster.c. That routine MUST correspond to the actual capabilities
  356. * of this module; no safety checks are made here.
  357. */
  358. GLOBAL(void)
  359. jinit_merged_upsampler (j_decompress_ptr cinfo)
  360. {
  361. my_upsample_ptr upsample;
  362. upsample = (my_upsample_ptr) (*cinfo->mem->alloc_small)
  363. ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_upsampler));
  364. cinfo->upsample = &upsample->pub;
  365. upsample->pub.start_pass = start_pass_merged_upsample;
  366. upsample->pub.need_context_rows = FALSE;
  367. upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
  368. if (cinfo->max_v_samp_factor == 2) {
  369. upsample->pub.upsample = merged_2v_upsample;
  370. upsample->upmethod = h2v2_merged_upsample;
  371. /* Allocate a spare row buffer */
  372. upsample->spare_row = (JSAMPROW) (*cinfo->mem->alloc_large)
  373. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  374. (size_t) upsample->out_row_width * SIZEOF(JSAMPLE));
  375. } else {
  376. upsample->pub.upsample = merged_1v_upsample;
  377. upsample->upmethod = h2v1_merged_upsample;
  378. /* No spare row needed */
  379. upsample->spare_row = NULL;
  380. }
  381. if (cinfo->jpeg_color_space == JCS_BG_YCC)
  382. build_bg_ycc_rgb_table(cinfo);
  383. else
  384. build_ycc_rgb_table(cinfo);
  385. }
  386. #endif /* UPSAMPLE_MERGING_SUPPORTED */