jdsample.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * jdsample.c
  3. *
  4. * Copyright (C) 1991-1996, Thomas G. Lane.
  5. * Modified 2002-2020 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, JSAMPIMAGE 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. /* Don't bother to upsample an uninteresting component. */
  87. if (! compptr->component_needed)
  88. continue;
  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, JSAMPIMAGE output_data_ptr)
  135. {
  136. *output_data_ptr = input_data;
  137. }
  138. /*
  139. * This version handles any integral sampling ratios.
  140. * This is not used for typical JPEG files, so it need not be fast.
  141. * Nor, for that matter, is it particularly accurate: the algorithm is
  142. * simple replication of the input pixel onto the corresponding output
  143. * pixels. The hi-falutin sampling literature refers to this as a
  144. * "box filter". A box filter tends to introduce visible artifacts,
  145. * so if you are actually going to use 3:1 or 4:1 sampling ratios
  146. * you would be well advised to improve this code.
  147. */
  148. METHODDEF(void)
  149. int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  150. JSAMPARRAY input_data, JSAMPIMAGE output_data_ptr)
  151. {
  152. my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  153. JSAMPARRAY output_data, output_end;
  154. register JSAMPROW inptr, outptr;
  155. register JSAMPLE invalue;
  156. register int h;
  157. JSAMPROW outend;
  158. int h_expand, v_expand;
  159. h_expand = upsample->h_expand[compptr->component_index];
  160. v_expand = upsample->v_expand[compptr->component_index];
  161. output_data = *output_data_ptr;
  162. output_end = output_data + cinfo->max_v_samp_factor;
  163. for (; output_data < output_end; output_data += v_expand) {
  164. /* Generate one output row with proper horizontal expansion */
  165. inptr = *input_data++;
  166. outptr = *output_data;
  167. outend = outptr + cinfo->output_width;
  168. while (outptr < outend) {
  169. invalue = *inptr++; /* don't need GETJSAMPLE() here */
  170. for (h = h_expand; h > 0; h--) {
  171. *outptr++ = invalue;
  172. }
  173. }
  174. /* Generate any additional output rows by duplicating the first one */
  175. if (v_expand > 1) {
  176. jcopy_sample_rows(output_data, output_data + 1,
  177. v_expand - 1, cinfo->output_width);
  178. }
  179. }
  180. }
  181. /*
  182. * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
  183. * It's still a box filter.
  184. */
  185. METHODDEF(void)
  186. h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  187. JSAMPARRAY input_data, JSAMPIMAGE output_data_ptr)
  188. {
  189. JSAMPARRAY output_data = *output_data_ptr;
  190. register JSAMPROW inptr, outptr;
  191. register JSAMPLE invalue;
  192. JSAMPROW outend;
  193. int outrow;
  194. for (outrow = 0; outrow < cinfo->max_v_samp_factor; outrow++) {
  195. inptr = input_data[outrow];
  196. outptr = output_data[outrow];
  197. outend = outptr + cinfo->output_width;
  198. while (outptr < outend) {
  199. invalue = *inptr++; /* don't need GETJSAMPLE() here */
  200. *outptr++ = invalue;
  201. *outptr++ = invalue;
  202. }
  203. }
  204. }
  205. /*
  206. * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
  207. * It's still a box filter.
  208. */
  209. METHODDEF(void)
  210. h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  211. JSAMPARRAY input_data, JSAMPIMAGE output_data_ptr)
  212. {
  213. JSAMPARRAY output_data, output_end;
  214. register JSAMPROW inptr, outptr;
  215. register JSAMPLE invalue;
  216. JSAMPROW outend;
  217. output_data = *output_data_ptr;
  218. output_end = output_data + cinfo->max_v_samp_factor;
  219. for (; output_data < output_end; output_data += 2) {
  220. inptr = *input_data++;
  221. outptr = *output_data;
  222. outend = outptr + cinfo->output_width;
  223. while (outptr < outend) {
  224. invalue = *inptr++; /* don't need GETJSAMPLE() here */
  225. *outptr++ = invalue;
  226. *outptr++ = invalue;
  227. }
  228. jcopy_sample_rows(output_data, output_data + 1,
  229. 1, cinfo->output_width);
  230. }
  231. }
  232. /*
  233. * Module initialization routine for upsampling.
  234. */
  235. GLOBAL(void)
  236. jinit_upsampler (j_decompress_ptr cinfo)
  237. {
  238. my_upsample_ptr upsample;
  239. int ci;
  240. jpeg_component_info * compptr;
  241. int h_in_group, v_in_group, h_out_group, v_out_group;
  242. upsample = (my_upsample_ptr) (*cinfo->mem->alloc_small)
  243. ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_upsampler));
  244. cinfo->upsample = &upsample->pub;
  245. upsample->pub.start_pass = start_pass_upsample;
  246. upsample->pub.upsample = sep_upsample;
  247. upsample->pub.need_context_rows = FALSE; /* until we find out differently */
  248. if (cinfo->CCIR601_sampling) /* this isn't supported */
  249. ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
  250. /* Verify we can handle the sampling factors, select per-component methods,
  251. * and create storage as needed.
  252. */
  253. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  254. ci++, compptr++) {
  255. /* Don't bother to upsample an uninteresting component. */
  256. if (! compptr->component_needed)
  257. continue;
  258. /* Compute size of an "input group" after IDCT scaling. This many samples
  259. * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
  260. */
  261. h_in_group = (compptr->h_samp_factor * compptr->DCT_h_scaled_size) /
  262. cinfo->min_DCT_h_scaled_size;
  263. v_in_group = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
  264. cinfo->min_DCT_v_scaled_size;
  265. h_out_group = cinfo->max_h_samp_factor;
  266. v_out_group = cinfo->max_v_samp_factor;
  267. upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
  268. if (h_in_group == h_out_group && v_in_group == v_out_group) {
  269. /* Fullsize components can be processed without any work. */
  270. upsample->methods[ci] = fullsize_upsample;
  271. continue; /* don't need to allocate buffer */
  272. }
  273. if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
  274. /* Special case for 2h1v upsampling */
  275. upsample->methods[ci] = h2v1_upsample;
  276. } else if (h_in_group * 2 == h_out_group &&
  277. v_in_group * 2 == v_out_group) {
  278. /* Special case for 2h2v upsampling */
  279. upsample->methods[ci] = h2v2_upsample;
  280. } else if ((h_out_group % h_in_group) == 0 &&
  281. (v_out_group % v_in_group) == 0) {
  282. /* Generic integral-factors upsampling method */
  283. upsample->methods[ci] = int_upsample;
  284. upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
  285. upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
  286. } else
  287. ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
  288. upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
  289. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  290. (JDIMENSION) jround_up((long) cinfo->output_width,
  291. (long) cinfo->max_h_samp_factor),
  292. (JDIMENSION) cinfo->max_v_samp_factor);
  293. }
  294. }