jdsample.c 18 KB

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