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.

522 lines
18 KiB

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. /*
  4. * jcsample.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 downsampling routines.
  11. *
  12. * Downsampling input data is counted in "row groups". A row group
  13. * is defined to be max_v_samp_factor pixel rows of each component,
  14. * from which the downsampler produces v_samp_factor sample rows.
  15. * A single row group is processed in each call to the downsampler module.
  16. *
  17. * The downsampler is responsible for edge-expansion of its output data
  18. * to fill an integral number of DCT blocks horizontally. The source buffer
  19. * may be modified if it is helpful for this purpose (the source buffer is
  20. * allocated wide enough to correspond to the desired output width).
  21. * The caller (the prep controller) is responsible for vertical padding.
  22. *
  23. * The downsampler may request "context rows" by setting need_context_rows
  24. * during startup. In this case, the input arrays will contain at least
  25. * one row group's worth of pixels above and below the passed-in data;
  26. * the caller will create dummy rows at image top and bottom by replicating
  27. * the first or last real pixel row.
  28. *
  29. * An excellent reference for image resampling is
  30. * Digital Image Warping, George Wolberg, 1990.
  31. * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
  32. *
  33. * The downsampling algorithm used here is a simple average of the source
  34. * pixels covered by the output pixel. The hi-falutin sampling literature
  35. * refers to this as a "box filter". In general the characteristics of a box
  36. * filter are not very good, but for the specific cases we normally use (1:1
  37. * and 2:1 ratios) the box is equivalent to a "triangle filter" which is not
  38. * nearly so bad. If you intend to use other sampling ratios, you'd be well
  39. * advised to improve this code.
  40. *
  41. * A simple input-smoothing capability is provided. This is mainly intended
  42. * for cleaning up color-dithered GIF input files (if you find it inadequate,
  43. * we suggest using an external filtering program such as pnmconvol). When
  44. * enabled, each input pixel P is replaced by a weighted sum of itself and its
  45. * eight neighbors. P's weight is 1-8*SF and each neighbor's weight is SF,
  46. * where SF = (smoothing_factor / 1024).
  47. * Currently, smoothing is only supported for 2h2v sampling factors.
  48. */
  49. #define JPEG_INTERNALS
  50. #include "jinclude.h"
  51. #include "jpeglib.h"
  52. /* Pointer to routine to downsample a single component */
  53. typedef JMETHOD(void, downsample1_ptr,
  54. (j_compress_ptr cinfo, jpeg_component_info * compptr,
  55. JSAMPARRAY input_data, JSAMPARRAY output_data));
  56. /* Private subobject */
  57. typedef struct {
  58. struct jpeg_downsampler pub; /* public fields */
  59. /* Downsampling method pointers, one per component */
  60. downsample1_ptr methods[MAX_COMPONENTS];
  61. } my_downsampler;
  62. typedef my_downsampler * my_downsample_ptr;
  63. /*
  64. * Initialize for a downsampling pass.
  65. */
  66. METHODDEF(void)
  67. start_pass_downsample (j_compress_ptr cinfo)
  68. {
  69. /* no work for now */
  70. }
  71. /*
  72. * Expand a component horizontally from width input_cols to width output_cols,
  73. * by duplicating the rightmost samples.
  74. */
  75. LOCAL(void)
  76. expand_right_edge (JSAMPARRAY image_data, int num_rows,
  77. JDIMENSION input_cols, JDIMENSION output_cols)
  78. {
  79. register JSAMPROW ptr;
  80. register JSAMPLE pixval;
  81. register int count;
  82. int row;
  83. int numcols = (int) (output_cols - input_cols);
  84. if (numcols > 0) {
  85. for (row = 0; row < num_rows; row++) {
  86. ptr = image_data[row] + input_cols;
  87. pixval = ptr[-1]; /* don't need GETJSAMPLE() here */
  88. for (count = numcols; count > 0; count--)
  89. *ptr++ = pixval;
  90. }
  91. }
  92. }
  93. /*
  94. * Do downsampling for a whole row group (all components).
  95. *
  96. * In this version we simply downsample each component independently.
  97. */
  98. METHODDEF(void)
  99. sep_downsample (j_compress_ptr cinfo,
  100. JSAMPIMAGE input_buf, JDIMENSION in_row_index,
  101. JSAMPIMAGE output_buf, JDIMENSION out_row_group_index)
  102. {
  103. my_downsample_ptr downsample = (my_downsample_ptr) cinfo->downsample;
  104. int ci;
  105. jpeg_component_info * compptr;
  106. JSAMPARRAY in_ptr, out_ptr;
  107. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  108. ci++, compptr++) {
  109. in_ptr = input_buf[ci] + in_row_index;
  110. out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor);
  111. (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr);
  112. }
  113. }
  114. /*
  115. * Downsample pixel values of a single component.
  116. * One row group is processed per call.
  117. * This version handles arbitrary integral sampling ratios, without smoothing.
  118. * Note that this version is not actually used for customary sampling ratios.
  119. */
  120. METHODDEF(void)
  121. int_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
  122. JSAMPARRAY input_data, JSAMPARRAY output_data)
  123. {
  124. int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
  125. JDIMENSION outcol, outcol_h; /* outcol_h == outcol*h_expand */
  126. JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  127. JSAMPROW inptr, outptr;
  128. INT32 outvalue;
  129. h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
  130. v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
  131. numpix = h_expand * v_expand;
  132. numpix2 = numpix/2;
  133. /* Expand input data enough to let all the output samples be generated
  134. * by the standard loop. Special-casing padded output would be more
  135. * efficient.
  136. */
  137. expand_right_edge(input_data, cinfo->max_v_samp_factor,
  138. cinfo->image_width, output_cols * h_expand);
  139. inrow = 0;
  140. for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
  141. outptr = output_data[outrow];
  142. for (outcol = 0, outcol_h = 0; outcol < output_cols;
  143. outcol++, outcol_h += h_expand) {
  144. outvalue = 0;
  145. for (v = 0; v < v_expand; v++) {
  146. inptr = input_data[inrow+v] + outcol_h;
  147. for (h = 0; h < h_expand; h++) {
  148. outvalue += (INT32) GETJSAMPLE(*inptr++);
  149. }
  150. }
  151. *outptr++ = (JSAMPLE) ((outvalue + numpix2) / numpix);
  152. }
  153. inrow += v_expand;
  154. }
  155. }
  156. /*
  157. * Downsample pixel values of a single component.
  158. * This version handles the special case of a full-size component,
  159. * without smoothing.
  160. */
  161. METHODDEF(void)
  162. fullsize_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
  163. JSAMPARRAY input_data, JSAMPARRAY output_data)
  164. {
  165. /* Copy the data */
  166. jcopy_sample_rows(input_data, 0, output_data, 0,
  167. cinfo->max_v_samp_factor, cinfo->image_width);
  168. /* Edge-expand */
  169. expand_right_edge(output_data, cinfo->max_v_samp_factor,
  170. cinfo->image_width, compptr->width_in_blocks * DCTSIZE);
  171. }
  172. /*
  173. * Downsample pixel values of a single component.
  174. * This version handles the common case of 2:1 horizontal and 1:1 vertical,
  175. * without smoothing.
  176. *
  177. * A note about the "bias" calculations: when rounding fractional values to
  178. * integer, we do not want to always round 0.5 up to the next integer.
  179. * If we did that, we'd introduce a noticeable bias towards larger values.
  180. * Instead, this code is arranged so that 0.5 will be rounded up or down at
  181. * alternate pixel locations (a simple ordered dither pattern).
  182. */
  183. METHODDEF(void)
  184. h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
  185. JSAMPARRAY input_data, JSAMPARRAY output_data)
  186. {
  187. int outrow;
  188. JDIMENSION outcol;
  189. JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  190. register JSAMPROW inptr, outptr;
  191. register int bias;
  192. /* Expand input data enough to let all the output samples be generated
  193. * by the standard loop. Special-casing padded output would be more
  194. * efficient.
  195. */
  196. expand_right_edge(input_data, cinfo->max_v_samp_factor,
  197. cinfo->image_width, output_cols * 2);
  198. for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
  199. outptr = output_data[outrow];
  200. inptr = input_data[outrow];
  201. bias = 0; /* bias = 0,1,0,1,... for successive samples */
  202. for (outcol = 0; outcol < output_cols; outcol++) {
  203. *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr) + GETJSAMPLE(inptr[1])
  204. + bias) >> 1);
  205. bias ^= 1; /* 0=>1, 1=>0 */
  206. inptr += 2;
  207. }
  208. }
  209. }
  210. /*
  211. * Downsample pixel values of a single component.
  212. * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
  213. * without smoothing.
  214. */
  215. METHODDEF(void)
  216. h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
  217. JSAMPARRAY input_data, JSAMPARRAY output_data)
  218. {
  219. int inrow, outrow;
  220. JDIMENSION outcol;
  221. JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  222. register JSAMPROW inptr0, inptr1, outptr;
  223. register int bias;
  224. /* Expand input data enough to let all the output samples be generated
  225. * by the standard loop. Special-casing padded output would be more
  226. * efficient.
  227. */
  228. expand_right_edge(input_data, cinfo->max_v_samp_factor,
  229. cinfo->image_width, output_cols * 2);
  230. inrow = 0;
  231. for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
  232. outptr = output_data[outrow];
  233. inptr0 = input_data[inrow];
  234. inptr1 = input_data[inrow+1];
  235. bias = 1; /* bias = 1,2,1,2,... for successive samples */
  236. for (outcol = 0; outcol < output_cols; outcol++) {
  237. *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
  238. GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1])
  239. + bias) >> 2);
  240. bias ^= 3; /* 1=>2, 2=>1 */
  241. inptr0 += 2; inptr1 += 2;
  242. }
  243. inrow += 2;
  244. }
  245. }
  246. #ifdef INPUT_SMOOTHING_SUPPORTED
  247. /*
  248. * Downsample pixel values of a single component.
  249. * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
  250. * with smoothing. One row of context is required.
  251. */
  252. METHODDEF(void)
  253. h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
  254. JSAMPARRAY input_data, JSAMPARRAY output_data)
  255. {
  256. int inrow, outrow;
  257. JDIMENSION colctr;
  258. JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  259. register JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr;
  260. INT32 membersum, neighsum, memberscale, neighscale;
  261. /* Expand input data enough to let all the output samples be generated
  262. * by the standard loop. Special-casing padded output would be more
  263. * efficient.
  264. */
  265. expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
  266. cinfo->image_width, output_cols * 2);
  267. /* We don't bother to form the individual "smoothed" input pixel values;
  268. * we can directly compute the output which is the average of the four
  269. * smoothed values. Each of the four member pixels contributes a fraction
  270. * (1-8*SF) to its own smoothed image and a fraction SF to each of the three
  271. * other smoothed pixels, therefore a total fraction (1-5*SF)/4 to the final
  272. * output. The four corner-adjacent neighbor pixels contribute a fraction
  273. * SF to just one smoothed pixel, or SF/4 to the final output; while the
  274. * eight edge-adjacent neighbors contribute SF to each of two smoothed
  275. * pixels, or SF/2 overall. In order to use integer arithmetic, these
  276. * factors are scaled by 2^16 = 65536.
  277. * Also recall that SF = smoothing_factor / 1024.
  278. */
  279. memberscale = 16384 - cinfo->smoothing_factor * 80; /* scaled (1-5*SF)/4 */
  280. neighscale = cinfo->smoothing_factor * 16; /* scaled SF/4 */
  281. inrow = 0;
  282. for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
  283. outptr = output_data[outrow];
  284. inptr0 = input_data[inrow];
  285. inptr1 = input_data[inrow+1];
  286. above_ptr = input_data[inrow-1];
  287. below_ptr = input_data[inrow+2];
  288. /* Special case for first column: pretend column -1 is same as column 0 */
  289. membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
  290. GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
  291. neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
  292. GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
  293. GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[2]) +
  294. GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[2]);
  295. neighsum += neighsum;
  296. neighsum += GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[2]) +
  297. GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[2]);
  298. membersum = membersum * memberscale + neighsum * neighscale;
  299. *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
  300. inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
  301. for (colctr = output_cols - 2; colctr > 0; colctr--) {
  302. /* sum of pixels directly mapped to this output element */
  303. membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
  304. GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
  305. /* sum of edge-neighbor pixels */
  306. neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
  307. GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
  308. GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[2]) +
  309. GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[2]);
  310. /* The edge-neighbors count twice as much as corner-neighbors */
  311. neighsum += neighsum;
  312. /* Add in the corner-neighbors */
  313. neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[2]) +
  314. GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[2]);
  315. /* form final output scaled up by 2^16 */
  316. membersum = membersum * memberscale + neighsum * neighscale;
  317. /* round, descale and output it */
  318. *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
  319. inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
  320. }
  321. /* Special case for last column */
  322. membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
  323. GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
  324. neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
  325. GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
  326. GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[1]) +
  327. GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[1]);
  328. neighsum += neighsum;
  329. neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[1]) +
  330. GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[1]);
  331. membersum = membersum * memberscale + neighsum * neighscale;
  332. *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
  333. inrow += 2;
  334. }
  335. }
  336. /*
  337. * Downsample pixel values of a single component.
  338. * This version handles the special case of a full-size component,
  339. * with smoothing. One row of context is required.
  340. */
  341. METHODDEF(void)
  342. fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
  343. JSAMPARRAY input_data, JSAMPARRAY output_data)
  344. {
  345. int outrow;
  346. JDIMENSION colctr;
  347. JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  348. register JSAMPROW inptr, above_ptr, below_ptr, outptr;
  349. INT32 membersum, neighsum, memberscale, neighscale;
  350. int colsum, lastcolsum, nextcolsum;
  351. /* Expand input data enough to let all the output samples be generated
  352. * by the standard loop. Special-casing padded output would be more
  353. * efficient.
  354. */
  355. expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
  356. cinfo->image_width, output_cols);
  357. /* Each of the eight neighbor pixels contributes a fraction SF to the
  358. * smoothed pixel, while the main pixel contributes (1-8*SF). In order
  359. * to use integer arithmetic, these factors are multiplied by 2^16 = 65536.
  360. * Also recall that SF = smoothing_factor / 1024.
  361. */
  362. memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */
  363. neighscale = cinfo->smoothing_factor * 64; /* scaled SF */
  364. for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
  365. outptr = output_data[outrow];
  366. inptr = input_data[outrow];
  367. above_ptr = input_data[outrow-1];
  368. below_ptr = input_data[outrow+1];
  369. /* Special case for first column */
  370. colsum = GETJSAMPLE(*above_ptr++) + GETJSAMPLE(*below_ptr++) +
  371. GETJSAMPLE(*inptr);
  372. membersum = GETJSAMPLE(*inptr++);
  373. nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
  374. GETJSAMPLE(*inptr);
  375. neighsum = colsum + (colsum - membersum) + nextcolsum;
  376. membersum = membersum * memberscale + neighsum * neighscale;
  377. *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
  378. lastcolsum = colsum; colsum = nextcolsum;
  379. for (colctr = output_cols - 2; colctr > 0; colctr--) {
  380. membersum = GETJSAMPLE(*inptr++);
  381. above_ptr++; below_ptr++;
  382. nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
  383. GETJSAMPLE(*inptr);
  384. neighsum = lastcolsum + (colsum - membersum) + nextcolsum;
  385. membersum = membersum * memberscale + neighsum * neighscale;
  386. *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
  387. lastcolsum = colsum; colsum = nextcolsum;
  388. }
  389. /* Special case for last column */
  390. membersum = GETJSAMPLE(*inptr);
  391. neighsum = lastcolsum + (colsum - membersum) + colsum;
  392. membersum = membersum * memberscale + neighsum * neighscale;
  393. *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
  394. }
  395. }
  396. #endif /* INPUT_SMOOTHING_SUPPORTED */
  397. /*
  398. * Module initialization routine for downsampling.
  399. * Note that we must select a routine for each component.
  400. */
  401. GLOBAL(void)
  402. jinit_downsampler (j_compress_ptr cinfo)
  403. {
  404. my_downsample_ptr downsample;
  405. int ci;
  406. jpeg_component_info * compptr;
  407. boolean smoothok = TRUE;
  408. downsample = (my_downsample_ptr)
  409. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  410. SIZEOF(my_downsampler));
  411. cinfo->downsample = (struct jpeg_downsampler *) downsample;
  412. downsample->pub.start_pass = start_pass_downsample;
  413. downsample->pub.downsample = sep_downsample;
  414. downsample->pub.need_context_rows = FALSE;
  415. if (cinfo->CCIR601_sampling)
  416. ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
  417. /* Verify we can handle the sampling factors, and set up method pointers */
  418. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  419. ci++, compptr++) {
  420. if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&
  421. compptr->v_samp_factor == cinfo->max_v_samp_factor) {
  422. #ifdef INPUT_SMOOTHING_SUPPORTED
  423. if (cinfo->smoothing_factor) {
  424. downsample->methods[ci] = fullsize_smooth_downsample;
  425. downsample->pub.need_context_rows = TRUE;
  426. } else
  427. #endif
  428. downsample->methods[ci] = fullsize_downsample;
  429. } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
  430. compptr->v_samp_factor == cinfo->max_v_samp_factor) {
  431. smoothok = FALSE;
  432. downsample->methods[ci] = h2v1_downsample;
  433. } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
  434. compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) {
  435. #ifdef INPUT_SMOOTHING_SUPPORTED
  436. if (cinfo->smoothing_factor) {
  437. downsample->methods[ci] = h2v2_smooth_downsample;
  438. downsample->pub.need_context_rows = TRUE;
  439. } else
  440. #endif
  441. downsample->methods[ci] = h2v2_downsample;
  442. } else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&
  443. (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) {
  444. smoothok = FALSE;
  445. downsample->methods[ci] = int_downsample;
  446. } else
  447. ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
  448. }
  449. #ifdef INPUT_SMOOTHING_SUPPORTED
  450. if (cinfo->smoothing_factor && !smoothok)
  451. TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL);
  452. #endif
  453. }