Audio plugin host https://kx.studio/carla
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.

520 lines
19KB

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