jdsample.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * jdsample.c
  3. *
  4. * Copyright (C) 1991-1996, Thomas G. Lane.
  5. * Modified 2002-2015 by Guido Vollbeding.
  6. * This file is part of the Independent JPEG Group's software.
  7. * For conditions of distribution and use, see the accompanying README file.
  8. *
  9. * This file contains upsampling routines.
  10. *
  11. * Upsampling input data is counted in "row groups". A row group
  12. * is defined to be (v_samp_factor * DCT_v_scaled_size / min_DCT_v_scaled_size)
  13. * sample rows of each component. Upsampling will normally produce
  14. * max_v_samp_factor pixel rows from each row group (but this could vary
  15. * if the upsampler is applying a scale factor of its own).
  16. *
  17. * An excellent reference for image resampling is
  18. * Digital Image Warping, George Wolberg, 1990.
  19. * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
  20. */
  21. #define JPEG_INTERNALS
  22. #include "jinclude.h"
  23. #include "jpeglib.h"
  24. /* Pointer to routine to upsample a single component */
  25. typedef JMETHOD(void, upsample1_ptr,
  26. (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  27. JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));
  28. /* Private subobject */
  29. typedef struct {
  30. struct jpeg_upsampler pub; /* public fields */
  31. /* Color conversion buffer. When using separate upsampling and color
  32. * conversion steps, this buffer holds one upsampled row group until it
  33. * has been color converted and output.
  34. * Note: we do not allocate any storage for component(s) which are full-size,
  35. * ie do not need rescaling. The corresponding entry of color_buf[] is
  36. * simply set to point to the input data array, thereby avoiding copying.
  37. */
  38. JSAMPARRAY color_buf[MAX_COMPONENTS];
  39. /* Per-component upsampling method pointers */
  40. upsample1_ptr methods[MAX_COMPONENTS];
  41. int next_row_out; /* counts rows emitted from color_buf */
  42. JDIMENSION rows_to_go; /* counts rows remaining in image */
  43. /* Height of an input row group for each component. */
  44. int rowgroup_height[MAX_COMPONENTS];
  45. /* These arrays save pixel expansion factors so that int_expand need not
  46. * recompute them each time. They are unused for other upsampling methods.
  47. */
  48. UINT8 h_expand[MAX_COMPONENTS];
  49. UINT8 v_expand[MAX_COMPONENTS];
  50. } my_upsampler;
  51. typedef my_upsampler * my_upsample_ptr;
  52. /*
  53. * Initialize for an upsampling pass.
  54. */
  55. METHODDEF(void)
  56. start_pass_upsample (j_decompress_ptr cinfo)
  57. {
  58. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  59. /* Mark the conversion buffer empty */
  60. upsample->next_row_out = cinfo->max_v_samp_factor;
  61. /* Initialize total-height counter for detecting bottom of image */
  62. upsample->rows_to_go = cinfo->output_height;
  63. }
  64. /*
  65. * Control routine to do upsampling (and color conversion).
  66. *
  67. * In this version we upsample each component independently.
  68. * We upsample one row group into the conversion buffer, then apply
  69. * color conversion a row at a time.
  70. */
  71. METHODDEF(void)
  72. sep_upsample (j_decompress_ptr cinfo,
  73. JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  74. JDIMENSION in_row_groups_avail,
  75. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  76. JDIMENSION out_rows_avail)
  77. {
  78. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  79. int ci;
  80. jpeg_component_info * compptr;
  81. JDIMENSION num_rows;
  82. /* Fill the conversion buffer, if it's empty */
  83. if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
  84. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  85. ci++, compptr++) {
  86. /* Invoke per-component upsample method. Notice we pass a POINTER
  87. * to color_buf[ci], so that fullsize_upsample can change it.
  88. */
  89. (*upsample->methods[ci]) (cinfo, compptr,
  90. input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
  91. upsample->color_buf + ci);
  92. }
  93. upsample->next_row_out = 0;
  94. }
  95. /* Color-convert and emit rows */
  96. /* How many we have in the buffer: */
  97. num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
  98. /* Not more than the distance to the end of the image. Need this test
  99. * in case the image height is not a multiple of max_v_samp_factor:
  100. */
  101. if (num_rows > upsample->rows_to_go)
  102. num_rows = upsample->rows_to_go;
  103. /* And not more than what the client can accept: */
  104. out_rows_avail -= *out_row_ctr;
  105. if (num_rows > out_rows_avail)
  106. num_rows = out_rows_avail;
  107. (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
  108. (JDIMENSION) upsample->next_row_out,
  109. output_buf + *out_row_ctr,
  110. (int) num_rows);
  111. /* Adjust counts */
  112. *out_row_ctr += num_rows;
  113. upsample->rows_to_go -= num_rows;
  114. upsample->next_row_out += num_rows;
  115. /* When the buffer is emptied, declare this input row group consumed */
  116. if (upsample->next_row_out >= cinfo->max_v_samp_factor)
  117. (*in_row_group_ctr)++;
  118. }
  119. /*
  120. * These are the routines invoked by sep_upsample to upsample pixel values
  121. * of a single component. One row group is processed per call.
  122. */
  123. /*
  124. * For full-size components, we just make color_buf[ci] point at the
  125. * input buffer, and thus avoid copying any data. Note that this is
  126. * safe only because sep_upsample doesn't declare the input row group
  127. * "consumed" until we are done color converting and emitting it.
  128. */
  129. METHODDEF(void)
  130. fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  131. JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  132. {
  133. *output_data_ptr = input_data;
  134. }
  135. /*
  136. * This is a no-op version used for "uninteresting" components.
  137. * These components will not be referenced by color conversion.
  138. */
  139. METHODDEF(void)
  140. noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  141. JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  142. {
  143. *output_data_ptr = NULL; /* safety check */
  144. }
  145. /*
  146. * This version handles any integral sampling ratios.
  147. * This is not used for typical JPEG files, so it need not be fast.
  148. * Nor, for that matter, is it particularly accurate: the algorithm is
  149. * simple replication of the input pixel onto the corresponding output
  150. * pixels. The hi-falutin sampling literature refers to this as a
  151. * "box filter". A box filter tends to introduce visible artifacts,
  152. * so if you are actually going to use 3:1 or 4:1 sampling ratios
  153. * you would be well advised to improve this code.
  154. */
  155. METHODDEF(void)
  156. int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  157. JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  158. {
  159. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  160. JSAMPARRAY output_data = *output_data_ptr;
  161. register JSAMPROW inptr, outptr;
  162. register JSAMPLE invalue;
  163. register int h;
  164. JSAMPROW outend;
  165. int h_expand, v_expand;
  166. int inrow, outrow;
  167. h_expand = upsample->h_expand[compptr->component_index];
  168. v_expand = upsample->v_expand[compptr->component_index];
  169. inrow = outrow = 0;
  170. while (outrow < cinfo->max_v_samp_factor) {
  171. /* Generate one output row with proper horizontal expansion */
  172. inptr = input_data[inrow];
  173. outptr = output_data[outrow];
  174. outend = outptr + cinfo->output_width;
  175. while (outptr < outend) {
  176. invalue = *inptr++; /* don't need GETJSAMPLE() here */
  177. for (h = h_expand; h > 0; h--) {
  178. *outptr++ = invalue;
  179. }
  180. }
  181. /* Generate any additional output rows by duplicating the first one */
  182. if (v_expand > 1) {
  183. jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
  184. v_expand-1, cinfo->output_width);
  185. }
  186. inrow++;
  187. outrow += v_expand;
  188. }
  189. }
  190. /*
  191. * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
  192. * It's still a box filter.
  193. */
  194. METHODDEF(void)
  195. h2v1_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 outrow;
  203. for (outrow = 0; outrow < cinfo->max_v_samp_factor; outrow++) {
  204. inptr = input_data[outrow];
  205. outptr = output_data[outrow];
  206. outend = outptr + cinfo->output_width;
  207. while (outptr < outend) {
  208. invalue = *inptr++; /* don't need GETJSAMPLE() here */
  209. *outptr++ = invalue;
  210. *outptr++ = invalue;
  211. }
  212. }
  213. }
  214. /*
  215. * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
  216. * It's still a box filter.
  217. */
  218. METHODDEF(void)
  219. h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  220. JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  221. {
  222. JSAMPARRAY output_data = *output_data_ptr;
  223. register JSAMPROW inptr, outptr;
  224. register JSAMPLE invalue;
  225. JSAMPROW outend;
  226. int inrow, outrow;
  227. inrow = outrow = 0;
  228. while (outrow < cinfo->max_v_samp_factor) {
  229. inptr = input_data[inrow];
  230. outptr = output_data[outrow];
  231. outend = outptr + cinfo->output_width;
  232. while (outptr < outend) {
  233. invalue = *inptr++; /* don't need GETJSAMPLE() here */
  234. *outptr++ = invalue;
  235. *outptr++ = invalue;
  236. }
  237. jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
  238. 1, cinfo->output_width);
  239. inrow++;
  240. outrow += 2;
  241. }
  242. }
  243. /*
  244. * Module initialization routine for upsampling.
  245. */
  246. GLOBAL(void)
  247. jinit_upsampler (j_decompress_ptr cinfo)
  248. {
  249. my_upsample_ptr upsample;
  250. int ci;
  251. jpeg_component_info * compptr;
  252. int h_in_group, v_in_group, h_out_group, v_out_group;
  253. upsample = (my_upsample_ptr)
  254. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  255. SIZEOF(my_upsampler));
  256. cinfo->upsample = &upsample->pub;
  257. upsample->pub.start_pass = start_pass_upsample;
  258. upsample->pub.upsample = sep_upsample;
  259. upsample->pub.need_context_rows = FALSE; /* until we find out differently */
  260. if (cinfo->CCIR601_sampling) /* this isn't supported */
  261. ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
  262. /* Verify we can handle the sampling factors, select per-component methods,
  263. * and create storage as needed.
  264. */
  265. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  266. ci++, compptr++) {
  267. /* Compute size of an "input group" after IDCT scaling. This many samples
  268. * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
  269. */
  270. h_in_group = (compptr->h_samp_factor * compptr->DCT_h_scaled_size) /
  271. cinfo->min_DCT_h_scaled_size;
  272. v_in_group = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
  273. cinfo->min_DCT_v_scaled_size;
  274. h_out_group = cinfo->max_h_samp_factor;
  275. v_out_group = cinfo->max_v_samp_factor;
  276. upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
  277. if (! compptr->component_needed) {
  278. /* Don't bother to upsample an uninteresting component. */
  279. upsample->methods[ci] = noop_upsample;
  280. continue; /* don't need to allocate buffer */
  281. }
  282. if (h_in_group == h_out_group && v_in_group == v_out_group) {
  283. /* Fullsize components can be processed without any work. */
  284. upsample->methods[ci] = fullsize_upsample;
  285. continue; /* don't need to allocate buffer */
  286. }
  287. if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
  288. /* Special case for 2h1v upsampling */
  289. upsample->methods[ci] = h2v1_upsample;
  290. } else if (h_in_group * 2 == h_out_group &&
  291. v_in_group * 2 == v_out_group) {
  292. /* Special case for 2h2v upsampling */
  293. upsample->methods[ci] = h2v2_upsample;
  294. } else if ((h_out_group % h_in_group) == 0 &&
  295. (v_out_group % v_in_group) == 0) {
  296. /* Generic integral-factors upsampling method */
  297. upsample->methods[ci] = int_upsample;
  298. upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
  299. upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
  300. } else
  301. ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
  302. upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
  303. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  304. (JDIMENSION) jround_up((long) cinfo->output_width,
  305. (long) cinfo->max_h_samp_factor),
  306. (JDIMENSION) cinfo->max_v_samp_factor);
  307. }
  308. }