jcsample.c 19 KB

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