jdmerge.c 14 KB

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