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.

481 lines
16 KiB

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. /*
  4. * jdsample.c
  5. *
  6. * Copyright (C) 1991-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 upsampling routines.
  11. *
  12. * Upsampling input data is counted in "row groups". A row group
  13. * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
  14. * sample rows of each component. Upsampling will normally produce
  15. * max_v_samp_factor pixel rows from each row group (but this could vary
  16. * if the upsampler is applying a scale factor of its own).
  17. *
  18. * An excellent reference for image resampling is
  19. * Digital Image Warping, George Wolberg, 1990.
  20. * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
  21. */
  22. #define JPEG_INTERNALS
  23. #include "jinclude.h"
  24. #include "jpeglib.h"
  25. /* Pointer to routine to upsample a single component */
  26. typedef JMETHOD(void, upsample1_ptr,
  27. (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  28. JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));
  29. /* Private subobject */
  30. typedef struct {
  31. struct jpeg_upsampler pub; /* public fields */
  32. /* Color conversion buffer. When using separate upsampling and color
  33. * conversion steps, this buffer holds one upsampled row group until it
  34. * has been color converted and output.
  35. * Note: we do not allocate any storage for component(s) which are full-size,
  36. * ie do not need rescaling. The corresponding entry of color_buf[] is
  37. * simply set to point to the input data array, thereby avoiding copying.
  38. */
  39. JSAMPARRAY color_buf[MAX_COMPONENTS];
  40. /* Per-component upsampling method pointers */
  41. upsample1_ptr methods[MAX_COMPONENTS];
  42. int next_row_out; /* counts rows emitted from color_buf */
  43. JDIMENSION rows_to_go; /* counts rows remaining in image */
  44. /* Height of an input row group for each component. */
  45. int rowgroup_height[MAX_COMPONENTS];
  46. /* These arrays save pixel expansion factors so that int_expand need not
  47. * recompute them each time. They are unused for other upsampling methods.
  48. */
  49. UINT8 h_expand[MAX_COMPONENTS];
  50. UINT8 v_expand[MAX_COMPONENTS];
  51. } my_upsampler;
  52. typedef my_upsampler * my_upsample_ptr;
  53. /*
  54. * Initialize for an upsampling pass.
  55. */
  56. METHODDEF(void)
  57. start_pass_upsample (j_decompress_ptr cinfo)
  58. {
  59. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  60. /* Mark the conversion buffer empty */
  61. upsample->next_row_out = cinfo->max_v_samp_factor;
  62. /* Initialize total-height counter for detecting bottom of image */
  63. upsample->rows_to_go = cinfo->output_height;
  64. }
  65. /*
  66. * Control routine to do upsampling (and color conversion).
  67. *
  68. * In this version we upsample each component independently.
  69. * We upsample one row group into the conversion buffer, then apply
  70. * color conversion a row at a time.
  71. */
  72. METHODDEF(void)
  73. sep_upsample (j_decompress_ptr cinfo,
  74. JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  75. JDIMENSION in_row_groups_avail,
  76. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  77. JDIMENSION out_rows_avail)
  78. {
  79. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  80. int ci;
  81. jpeg_component_info * compptr;
  82. JDIMENSION num_rows;
  83. /* Fill the conversion buffer, if it's empty */
  84. if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
  85. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  86. ci++, compptr++) {
  87. /* Invoke per-component upsample method. Notice we pass a POINTER
  88. * to color_buf[ci], so that fullsize_upsample can change it.
  89. */
  90. (*upsample->methods[ci]) (cinfo, compptr,
  91. input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
  92. upsample->color_buf + ci);
  93. }
  94. upsample->next_row_out = 0;
  95. }
  96. /* Color-convert and emit rows */
  97. /* How many we have in the buffer: */
  98. num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
  99. /* Not more than the distance to the end of the image. Need this test
  100. * in case the image height is not a multiple of max_v_samp_factor:
  101. */
  102. if (num_rows > upsample->rows_to_go)
  103. num_rows = upsample->rows_to_go;
  104. /* And not more than what the client can accept: */
  105. out_rows_avail -= *out_row_ctr;
  106. if (num_rows > out_rows_avail)
  107. num_rows = out_rows_avail;
  108. (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
  109. (JDIMENSION) upsample->next_row_out,
  110. output_buf + *out_row_ctr,
  111. (int) num_rows);
  112. /* Adjust counts */
  113. *out_row_ctr += num_rows;
  114. upsample->rows_to_go -= num_rows;
  115. upsample->next_row_out += num_rows;
  116. /* When the buffer is emptied, declare this input row group consumed */
  117. if (upsample->next_row_out >= cinfo->max_v_samp_factor)
  118. (*in_row_group_ctr)++;
  119. }
  120. /*
  121. * These are the routines invoked by sep_upsample to upsample pixel values
  122. * of a single component. One row group is processed per call.
  123. */
  124. /*
  125. * For full-size components, we just make color_buf[ci] point at the
  126. * input buffer, and thus avoid copying any data. Note that this is
  127. * safe only because sep_upsample doesn't declare the input row group
  128. * "consumed" until we are done color converting and emitting it.
  129. */
  130. METHODDEF(void)
  131. fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  132. JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  133. {
  134. *output_data_ptr = input_data;
  135. }
  136. /*
  137. * This is a no-op version used for "uninteresting" components.
  138. * These components will not be referenced by color conversion.
  139. */
  140. METHODDEF(void)
  141. noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  142. JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  143. {
  144. *output_data_ptr = NULL; /* safety check */
  145. }
  146. /*
  147. * This version handles any integral sampling ratios.
  148. * This is not used for typical JPEG files, so it need not be fast.
  149. * Nor, for that matter, is it particularly accurate: the algorithm is
  150. * simple replication of the input pixel onto the corresponding output
  151. * pixels. The hi-falutin sampling literature refers to this as a
  152. * "box filter". A box filter tends to introduce visible artifacts,
  153. * so if you are actually going to use 3:1 or 4:1 sampling ratios
  154. * you would be well advised to improve this code.
  155. */
  156. METHODDEF(void)
  157. int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  158. JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  159. {
  160. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  161. JSAMPARRAY output_data = *output_data_ptr;
  162. register JSAMPROW inptr, outptr;
  163. register JSAMPLE invalue;
  164. register int h;
  165. JSAMPROW outend;
  166. int h_expand, v_expand;
  167. int inrow, outrow;
  168. h_expand = upsample->h_expand[compptr->component_index];
  169. v_expand = upsample->v_expand[compptr->component_index];
  170. inrow = outrow = 0;
  171. while (outrow < cinfo->max_v_samp_factor) {
  172. /* Generate one output row with proper horizontal expansion */
  173. inptr = input_data[inrow];
  174. outptr = output_data[outrow];
  175. outend = outptr + cinfo->output_width;
  176. while (outptr < outend) {
  177. invalue = *inptr++; /* don't need GETJSAMPLE() here */
  178. for (h = h_expand; h > 0; h--) {
  179. *outptr++ = invalue;
  180. }
  181. }
  182. /* Generate any additional output rows by duplicating the first one */
  183. if (v_expand > 1) {
  184. jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
  185. v_expand-1, cinfo->output_width);
  186. }
  187. inrow++;
  188. outrow += v_expand;
  189. }
  190. }
  191. /*
  192. * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
  193. * It's still a box filter.
  194. */
  195. METHODDEF(void)
  196. h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  197. JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  198. {
  199. JSAMPARRAY output_data = *output_data_ptr;
  200. register JSAMPROW inptr, outptr;
  201. register JSAMPLE invalue;
  202. JSAMPROW outend;
  203. int inrow;
  204. for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
  205. inptr = input_data[inrow];
  206. outptr = output_data[inrow];
  207. outend = outptr + cinfo->output_width;
  208. while (outptr < outend) {
  209. invalue = *inptr++; /* don't need GETJSAMPLE() here */
  210. *outptr++ = invalue;
  211. *outptr++ = invalue;
  212. }
  213. }
  214. }
  215. /*
  216. * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
  217. * It's still a box filter.
  218. */
  219. METHODDEF(void)
  220. h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  221. JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  222. {
  223. JSAMPARRAY output_data = *output_data_ptr;
  224. register JSAMPROW inptr, outptr;
  225. register JSAMPLE invalue;
  226. JSAMPROW outend;
  227. int inrow, outrow;
  228. inrow = outrow = 0;
  229. while (outrow < cinfo->max_v_samp_factor) {
  230. inptr = input_data[inrow];
  231. outptr = output_data[outrow];
  232. outend = outptr + cinfo->output_width;
  233. while (outptr < outend) {
  234. invalue = *inptr++; /* don't need GETJSAMPLE() here */
  235. *outptr++ = invalue;
  236. *outptr++ = invalue;
  237. }
  238. jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
  239. 1, cinfo->output_width);
  240. inrow++;
  241. outrow += 2;
  242. }
  243. }
  244. /*
  245. * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
  246. *
  247. * The upsampling algorithm is linear interpolation between pixel centers,
  248. * also known as a "triangle filter". This is a good compromise between
  249. * speed and visual quality. The centers of the output pixels are 1/4 and 3/4
  250. * of the way between input pixel centers.
  251. *
  252. * A note about the "bias" calculations: when rounding fractional values to
  253. * integer, we do not want to always round 0.5 up to the next integer.
  254. * If we did that, we'd introduce a noticeable bias towards larger values.
  255. * Instead, this code is arranged so that 0.5 will be rounded up or down at
  256. * alternate pixel locations (a simple ordered dither pattern).
  257. */
  258. METHODDEF(void)
  259. h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  260. JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  261. {
  262. JSAMPARRAY output_data = *output_data_ptr;
  263. register JSAMPROW inptr, outptr;
  264. register int invalue;
  265. register JDIMENSION colctr;
  266. int inrow;
  267. for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
  268. inptr = input_data[inrow];
  269. outptr = output_data[inrow];
  270. /* Special case for first column */
  271. invalue = GETJSAMPLE(*inptr++);
  272. *outptr++ = (JSAMPLE) invalue;
  273. *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
  274. for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
  275. /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
  276. invalue = GETJSAMPLE(*inptr++) * 3;
  277. *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
  278. *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
  279. }
  280. /* Special case for last column */
  281. invalue = GETJSAMPLE(*inptr);
  282. *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
  283. *outptr++ = (JSAMPLE) invalue;
  284. }
  285. }
  286. /*
  287. * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
  288. * Again a triangle filter; see comments for h2v1 case, above.
  289. *
  290. * It is OK for us to reference the adjacent input rows because we demanded
  291. * context from the main buffer controller (see initialization code).
  292. */
  293. METHODDEF(void)
  294. h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  295. JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  296. {
  297. JSAMPARRAY output_data = *output_data_ptr;
  298. register JSAMPROW inptr0, inptr1, outptr;
  299. #if BITS_IN_JSAMPLE == 8
  300. register int thiscolsum, lastcolsum, nextcolsum;
  301. #else
  302. register INT32 thiscolsum, lastcolsum, nextcolsum;
  303. #endif
  304. register JDIMENSION colctr;
  305. int inrow, outrow, v;
  306. inrow = outrow = 0;
  307. while (outrow < cinfo->max_v_samp_factor) {
  308. for (v = 0; v < 2; v++) {
  309. /* inptr0 points to nearest input row, inptr1 points to next nearest */
  310. inptr0 = input_data[inrow];
  311. if (v == 0) /* next nearest is row above */
  312. inptr1 = input_data[inrow-1];
  313. else /* next nearest is row below */
  314. inptr1 = input_data[inrow+1];
  315. outptr = output_data[outrow++];
  316. /* Special case for first column */
  317. thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
  318. nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
  319. *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
  320. *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
  321. lastcolsum = thiscolsum; thiscolsum = nextcolsum;
  322. for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
  323. /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
  324. /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
  325. nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
  326. *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
  327. *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
  328. lastcolsum = thiscolsum; thiscolsum = nextcolsum;
  329. }
  330. /* Special case for last column */
  331. *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
  332. *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
  333. }
  334. inrow++;
  335. }
  336. }
  337. /*
  338. * Module initialization routine for upsampling.
  339. */
  340. GLOBAL(void)
  341. jinit_upsampler (j_decompress_ptr cinfo)
  342. {
  343. my_upsample_ptr upsample;
  344. int ci;
  345. jpeg_component_info * compptr;
  346. boolean need_buffer, do_fancy;
  347. int h_in_group, v_in_group, h_out_group, v_out_group;
  348. upsample = (my_upsample_ptr)
  349. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  350. SIZEOF(my_upsampler));
  351. cinfo->upsample = (struct jpeg_upsampler *) upsample;
  352. upsample->pub.start_pass = start_pass_upsample;
  353. upsample->pub.upsample = sep_upsample;
  354. upsample->pub.need_context_rows = FALSE; /* until we find out differently */
  355. if (cinfo->CCIR601_sampling) /* this isn't supported */
  356. ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
  357. /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
  358. * so don't ask for it.
  359. */
  360. do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1;
  361. /* Verify we can handle the sampling factors, select per-component methods,
  362. * and create storage as needed.
  363. */
  364. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  365. ci++, compptr++) {
  366. /* Compute size of an "input group" after IDCT scaling. This many samples
  367. * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
  368. */
  369. h_in_group = (compptr->h_samp_factor * compptr->DCT_scaled_size) /
  370. cinfo->min_DCT_scaled_size;
  371. v_in_group = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
  372. cinfo->min_DCT_scaled_size;
  373. h_out_group = cinfo->max_h_samp_factor;
  374. v_out_group = cinfo->max_v_samp_factor;
  375. upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
  376. need_buffer = TRUE;
  377. if (! compptr->component_needed) {
  378. /* Don't bother to upsample an uninteresting component. */
  379. upsample->methods[ci] = noop_upsample;
  380. need_buffer = FALSE;
  381. } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
  382. /* Fullsize components can be processed without any work. */
  383. upsample->methods[ci] = fullsize_upsample;
  384. need_buffer = FALSE;
  385. } else if (h_in_group * 2 == h_out_group &&
  386. v_in_group == v_out_group) {
  387. /* Special cases for 2h1v upsampling */
  388. if (do_fancy && compptr->downsampled_width > 2)
  389. upsample->methods[ci] = h2v1_fancy_upsample;
  390. else
  391. upsample->methods[ci] = h2v1_upsample;
  392. } else if (h_in_group * 2 == h_out_group &&
  393. v_in_group * 2 == v_out_group) {
  394. /* Special cases for 2h2v upsampling */
  395. if (do_fancy && compptr->downsampled_width > 2) {
  396. upsample->methods[ci] = h2v2_fancy_upsample;
  397. upsample->pub.need_context_rows = TRUE;
  398. } else
  399. upsample->methods[ci] = h2v2_upsample;
  400. } else if ((h_out_group % h_in_group) == 0 &&
  401. (v_out_group % v_in_group) == 0) {
  402. /* Generic integral-factors upsampling method */
  403. upsample->methods[ci] = int_upsample;
  404. upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
  405. upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
  406. } else
  407. ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
  408. if (need_buffer) {
  409. upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
  410. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  411. (JDIMENSION) jround_up((long) cinfo->output_width,
  412. (long) cinfo->max_h_samp_factor),
  413. (JDIMENSION) cinfo->max_v_samp_factor);
  414. }
  415. }
  416. }