Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

403 lines
14 KiB

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