jdsample.c 17 KB

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