jdmerge.c 14 KB

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