/media/libjpeg/jdsample.c

http://github.com/zpao/v8monkey · C · 496 lines · 293 code · 64 blank · 139 comment · 53 complexity · 76a68aa00f7242cc7a3ee16762b1a369 MD5 · raw file

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