jdsample.c 17 KB

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