jdmerge.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * jdmerge.c
  3. *
  4. * Copyright (C) 1994-1996, Thomas G. Lane.
  5. * Modified 2013-2015 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. /* Private subobject */
  40. typedef struct {
  41. struct jpeg_upsampler pub; /* public fields */
  42. /* Pointer to routine to do actual upsampling/conversion of one row group */
  43. JMETHOD(void, upmethod, (j_decompress_ptr cinfo,
  44. JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
  45. JSAMPARRAY output_buf));
  46. /* Private state for YCC->RGB conversion */
  47. int * Cr_r_tab; /* => table for Cr to R conversion */
  48. int * Cb_b_tab; /* => table for Cb to B conversion */
  49. INT32 * Cr_g_tab; /* => table for Cr to G conversion */
  50. INT32 * Cb_g_tab; /* => table for Cb to G conversion */
  51. /* For 2:1 vertical sampling, we produce two output rows at a time.
  52. * We need a "spare" row buffer to hold the second output row if the
  53. * application provides just a one-row buffer; we also use the spare
  54. * to discard the dummy last row if the image height is odd.
  55. */
  56. JSAMPROW spare_row;
  57. boolean spare_full; /* T if spare buffer is occupied */
  58. JDIMENSION out_row_width; /* samples per output row */
  59. JDIMENSION rows_to_go; /* counts rows remaining in image */
  60. } my_upsampler;
  61. typedef my_upsampler * my_upsample_ptr;
  62. #define SCALEBITS 16 /* speediest right-shift on some machines */
  63. #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
  64. #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
  65. /*
  66. * Initialize tables for YCbCr->RGB and BG_YCC->RGB colorspace conversion.
  67. * This is taken directly from jdcolor.c; see that file for more info.
  68. */
  69. LOCAL(void)
  70. build_ycc_rgb_table (j_decompress_ptr cinfo)
  71. /* Normal case, sYCC */
  72. {
  73. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  74. int i;
  75. INT32 x;
  76. SHIFT_TEMPS
  77. upsample->Cr_r_tab = (int *)
  78. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  79. (MAXJSAMPLE+1) * SIZEOF(int));
  80. upsample->Cb_b_tab = (int *)
  81. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  82. (MAXJSAMPLE+1) * SIZEOF(int));
  83. upsample->Cr_g_tab = (INT32 *)
  84. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  85. (MAXJSAMPLE+1) * SIZEOF(INT32));
  86. upsample->Cb_g_tab = (INT32 *)
  87. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  88. (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)
  94. RIGHT_SHIFT(FIX(1.402) * x + ONE_HALF, SCALEBITS);
  95. /* Cb=>B value is nearest int to 1.772 * x */
  96. upsample->Cb_b_tab[i] = (int)
  97. RIGHT_SHIFT(FIX(1.772) * x + ONE_HALF, SCALEBITS);
  98. /* Cr=>G value is scaled-up -0.714136286 * x */
  99. upsample->Cr_g_tab[i] = (- FIX(0.714136286)) * x;
  100. /* Cb=>G value is scaled-up -0.344136286 * x */
  101. /* We also add in ONE_HALF so that need not do it in inner loop */
  102. upsample->Cb_g_tab[i] = (- FIX(0.344136286)) * x + ONE_HALF;
  103. }
  104. }
  105. LOCAL(void)
  106. build_bg_ycc_rgb_table (j_decompress_ptr cinfo)
  107. /* Wide gamut case, bg-sYCC */
  108. {
  109. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  110. int i;
  111. INT32 x;
  112. SHIFT_TEMPS
  113. upsample->Cr_r_tab = (int *)
  114. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  115. (MAXJSAMPLE+1) * SIZEOF(int));
  116. upsample->Cb_b_tab = (int *)
  117. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  118. (MAXJSAMPLE+1) * SIZEOF(int));
  119. upsample->Cr_g_tab = (INT32 *)
  120. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  121. (MAXJSAMPLE+1) * SIZEOF(INT32));
  122. upsample->Cb_g_tab = (INT32 *)
  123. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  124. (MAXJSAMPLE+1) * SIZEOF(INT32));
  125. for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
  126. /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
  127. /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
  128. /* Cr=>R value is nearest int to 2.804 * x */
  129. upsample->Cr_r_tab[i] = (int)
  130. RIGHT_SHIFT(FIX(2.804) * x + ONE_HALF, SCALEBITS);
  131. /* Cb=>B value is nearest int to 3.544 * x */
  132. upsample->Cb_b_tab[i] = (int)
  133. RIGHT_SHIFT(FIX(3.544) * x + ONE_HALF, SCALEBITS);
  134. /* Cr=>G value is scaled-up -1.428272572 * x */
  135. upsample->Cr_g_tab[i] = (- FIX(1.428272572)) * x;
  136. /* Cb=>G value is scaled-up -0.688272572 * x */
  137. /* We also add in ONE_HALF so that need not do it in inner loop */
  138. upsample->Cb_g_tab[i] = (- FIX(0.688272572)) * x + ONE_HALF;
  139. }
  140. }
  141. /*
  142. * Initialize for an upsampling pass.
  143. */
  144. METHODDEF(void)
  145. start_pass_merged_upsample (j_decompress_ptr cinfo)
  146. {
  147. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  148. /* Mark the spare buffer empty */
  149. upsample->spare_full = FALSE;
  150. /* Initialize total-height counter for detecting bottom of image */
  151. upsample->rows_to_go = cinfo->output_height;
  152. }
  153. /*
  154. * Control routine to do upsampling (and color conversion).
  155. *
  156. * The control routine just handles the row buffering considerations.
  157. */
  158. METHODDEF(void)
  159. merged_2v_upsample (j_decompress_ptr cinfo,
  160. JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  161. JDIMENSION in_row_groups_avail,
  162. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  163. JDIMENSION out_rows_avail)
  164. /* 2:1 vertical sampling case: may need a spare row. */
  165. {
  166. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  167. JSAMPROW work_ptrs[2];
  168. JDIMENSION num_rows; /* number of rows returned to caller */
  169. if (upsample->spare_full) {
  170. /* If we have a spare row saved from a previous cycle, just return it. */
  171. jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
  172. 1, upsample->out_row_width);
  173. num_rows = 1;
  174. upsample->spare_full = FALSE;
  175. } else {
  176. /* Figure number of rows to return to caller. */
  177. num_rows = 2;
  178. /* Not more than the distance to the end of the image. */
  179. if (num_rows > upsample->rows_to_go)
  180. num_rows = upsample->rows_to_go;
  181. /* And not more than what the client can accept: */
  182. out_rows_avail -= *out_row_ctr;
  183. if (num_rows > out_rows_avail)
  184. num_rows = out_rows_avail;
  185. /* Create output pointer array for upsampler. */
  186. work_ptrs[0] = output_buf[*out_row_ctr];
  187. if (num_rows > 1) {
  188. work_ptrs[1] = output_buf[*out_row_ctr + 1];
  189. } else {
  190. work_ptrs[1] = upsample->spare_row;
  191. upsample->spare_full = TRUE;
  192. }
  193. /* Now do the upsampling. */
  194. (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
  195. }
  196. /* Adjust counts */
  197. *out_row_ctr += num_rows;
  198. upsample->rows_to_go -= num_rows;
  199. /* When the buffer is emptied, declare this input row group consumed */
  200. if (! upsample->spare_full)
  201. (*in_row_group_ctr)++;
  202. }
  203. METHODDEF(void)
  204. merged_1v_upsample (j_decompress_ptr cinfo,
  205. JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  206. JDIMENSION in_row_groups_avail,
  207. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  208. JDIMENSION out_rows_avail)
  209. /* 1:1 vertical sampling case: much easier, never need a spare row. */
  210. {
  211. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  212. /* Just do the upsampling. */
  213. (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
  214. output_buf + *out_row_ctr);
  215. /* Adjust counts */
  216. (*out_row_ctr)++;
  217. (*in_row_group_ctr)++;
  218. }
  219. /*
  220. * These are the routines invoked by the control routines to do
  221. * the actual upsampling/conversion. One row group is processed per call.
  222. *
  223. * Note: since we may be writing directly into application-supplied buffers,
  224. * we have to be honest about the output width; we can't assume the buffer
  225. * has been rounded up to an even width.
  226. */
  227. /*
  228. * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
  229. */
  230. METHODDEF(void)
  231. h2v1_merged_upsample (j_decompress_ptr cinfo,
  232. JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
  233. JSAMPARRAY output_buf)
  234. {
  235. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  236. register int y, cred, cgreen, cblue;
  237. int cb, cr;
  238. register JSAMPROW outptr;
  239. JSAMPROW inptr0, inptr1, inptr2;
  240. JDIMENSION col;
  241. /* copy these pointers into registers if possible */
  242. register JSAMPLE * range_limit = cinfo->sample_range_limit;
  243. int * Crrtab = upsample->Cr_r_tab;
  244. int * Cbbtab = upsample->Cb_b_tab;
  245. INT32 * Crgtab = upsample->Cr_g_tab;
  246. INT32 * Cbgtab = upsample->Cb_g_tab;
  247. SHIFT_TEMPS
  248. inptr0 = input_buf[0][in_row_group_ctr];
  249. inptr1 = input_buf[1][in_row_group_ctr];
  250. inptr2 = input_buf[2][in_row_group_ctr];
  251. outptr = output_buf[0];
  252. /* Loop for each pair of output pixels */
  253. for (col = cinfo->output_width >> 1; col > 0; col--) {
  254. /* Do the chroma part of the calculation */
  255. cb = GETJSAMPLE(*inptr1++);
  256. cr = GETJSAMPLE(*inptr2++);
  257. cred = Crrtab[cr];
  258. cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
  259. cblue = Cbbtab[cb];
  260. /* Fetch 2 Y values and emit 2 pixels */
  261. y = GETJSAMPLE(*inptr0++);
  262. outptr[RGB_RED] = range_limit[y + cred];
  263. outptr[RGB_GREEN] = range_limit[y + cgreen];
  264. outptr[RGB_BLUE] = range_limit[y + cblue];
  265. outptr += RGB_PIXELSIZE;
  266. y = GETJSAMPLE(*inptr0++);
  267. outptr[RGB_RED] = range_limit[y + cred];
  268. outptr[RGB_GREEN] = range_limit[y + cgreen];
  269. outptr[RGB_BLUE] = range_limit[y + cblue];
  270. outptr += RGB_PIXELSIZE;
  271. }
  272. /* If image width is odd, do the last output column separately */
  273. if (cinfo->output_width & 1) {
  274. cb = GETJSAMPLE(*inptr1);
  275. cr = GETJSAMPLE(*inptr2);
  276. cred = Crrtab[cr];
  277. cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
  278. cblue = Cbbtab[cb];
  279. y = GETJSAMPLE(*inptr0);
  280. outptr[RGB_RED] = range_limit[y + cred];
  281. outptr[RGB_GREEN] = range_limit[y + cgreen];
  282. outptr[RGB_BLUE] = range_limit[y + cblue];
  283. }
  284. }
  285. /*
  286. * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
  287. */
  288. METHODDEF(void)
  289. h2v2_merged_upsample (j_decompress_ptr cinfo,
  290. JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
  291. JSAMPARRAY output_buf)
  292. {
  293. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  294. register int y, cred, cgreen, cblue;
  295. int cb, cr;
  296. register JSAMPROW outptr0, outptr1;
  297. JSAMPROW inptr00, inptr01, inptr1, inptr2;
  298. JDIMENSION col;
  299. /* copy these pointers into registers if possible */
  300. register JSAMPLE * range_limit = cinfo->sample_range_limit;
  301. int * Crrtab = upsample->Cr_r_tab;
  302. int * Cbbtab = upsample->Cb_b_tab;
  303. INT32 * Crgtab = upsample->Cr_g_tab;
  304. INT32 * Cbgtab = upsample->Cb_g_tab;
  305. SHIFT_TEMPS
  306. inptr00 = input_buf[0][in_row_group_ctr*2];
  307. inptr01 = input_buf[0][in_row_group_ctr*2 + 1];
  308. inptr1 = input_buf[1][in_row_group_ctr];
  309. inptr2 = input_buf[2][in_row_group_ctr];
  310. outptr0 = output_buf[0];
  311. outptr1 = output_buf[1];
  312. /* Loop for each group of output pixels */
  313. for (col = cinfo->output_width >> 1; col > 0; col--) {
  314. /* Do the chroma part of the calculation */
  315. cb = GETJSAMPLE(*inptr1++);
  316. cr = GETJSAMPLE(*inptr2++);
  317. cred = Crrtab[cr];
  318. cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
  319. cblue = Cbbtab[cb];
  320. /* Fetch 4 Y values and emit 4 pixels */
  321. y = GETJSAMPLE(*inptr00++);
  322. outptr0[RGB_RED] = range_limit[y + cred];
  323. outptr0[RGB_GREEN] = range_limit[y + cgreen];
  324. outptr0[RGB_BLUE] = range_limit[y + cblue];
  325. outptr0 += RGB_PIXELSIZE;
  326. y = GETJSAMPLE(*inptr00++);
  327. outptr0[RGB_RED] = range_limit[y + cred];
  328. outptr0[RGB_GREEN] = range_limit[y + cgreen];
  329. outptr0[RGB_BLUE] = range_limit[y + cblue];
  330. outptr0 += RGB_PIXELSIZE;
  331. y = GETJSAMPLE(*inptr01++);
  332. outptr1[RGB_RED] = range_limit[y + cred];
  333. outptr1[RGB_GREEN] = range_limit[y + cgreen];
  334. outptr1[RGB_BLUE] = range_limit[y + cblue];
  335. outptr1 += RGB_PIXELSIZE;
  336. y = GETJSAMPLE(*inptr01++);
  337. outptr1[RGB_RED] = range_limit[y + cred];
  338. outptr1[RGB_GREEN] = range_limit[y + cgreen];
  339. outptr1[RGB_BLUE] = range_limit[y + cblue];
  340. outptr1 += RGB_PIXELSIZE;
  341. }
  342. /* If image width is odd, do the last output column separately */
  343. if (cinfo->output_width & 1) {
  344. cb = GETJSAMPLE(*inptr1);
  345. cr = GETJSAMPLE(*inptr2);
  346. cred = Crrtab[cr];
  347. cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
  348. cblue = Cbbtab[cb];
  349. y = GETJSAMPLE(*inptr00);
  350. outptr0[RGB_RED] = range_limit[y + cred];
  351. outptr0[RGB_GREEN] = range_limit[y + cgreen];
  352. outptr0[RGB_BLUE] = range_limit[y + cblue];
  353. y = GETJSAMPLE(*inptr01);
  354. outptr1[RGB_RED] = range_limit[y + cred];
  355. outptr1[RGB_GREEN] = range_limit[y + cgreen];
  356. outptr1[RGB_BLUE] = range_limit[y + cblue];
  357. }
  358. }
  359. /*
  360. * Module initialization routine for merged upsampling/color conversion.
  361. *
  362. * NB: this is called under the conditions determined by use_merged_upsample()
  363. * in jdmaster.c. That routine MUST correspond to the actual capabilities
  364. * of this module; no safety checks are made here.
  365. */
  366. GLOBAL(void)
  367. jinit_merged_upsampler (j_decompress_ptr cinfo)
  368. {
  369. my_upsample_ptr upsample;
  370. upsample = (my_upsample_ptr)
  371. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  372. SIZEOF(my_upsampler));
  373. cinfo->upsample = &upsample->pub;
  374. upsample->pub.start_pass = start_pass_merged_upsample;
  375. upsample->pub.need_context_rows = FALSE;
  376. upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
  377. if (cinfo->max_v_samp_factor == 2) {
  378. upsample->pub.upsample = merged_2v_upsample;
  379. upsample->upmethod = h2v2_merged_upsample;
  380. /* Allocate a spare row buffer */
  381. upsample->spare_row = (JSAMPROW)
  382. (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  383. (size_t) (upsample->out_row_width * SIZEOF(JSAMPLE)));
  384. } else {
  385. upsample->pub.upsample = merged_1v_upsample;
  386. upsample->upmethod = h2v1_merged_upsample;
  387. /* No spare row needed */
  388. upsample->spare_row = NULL;
  389. }
  390. if (cinfo->jpeg_color_space == JCS_BG_YCC)
  391. build_bg_ycc_rgb_table(cinfo);
  392. else
  393. build_ycc_rgb_table(cinfo);
  394. }
  395. #endif /* UPSAMPLE_MERGING_SUPPORTED */