jdsample.c 18 KB

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