jdsample.c 18 KB

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