jcprepct.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * jcprepct.c
  3. *
  4. * Copyright (C) 1994-1996, Thomas G. Lane.
  5. * Modified 2003-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 the compression preprocessing controller.
  10. * This controller manages the color conversion, downsampling,
  11. * and edge expansion steps.
  12. *
  13. * Most of the complexity here is associated with buffering input rows
  14. * as required by the downsampler. See the comments at the head of
  15. * jcsample.c for the downsampler's needs.
  16. */
  17. #define JPEG_INTERNALS
  18. #include "jinclude.h"
  19. #include "jpeglib.h"
  20. /* At present, jcsample.c can request context rows only for smoothing.
  21. * In the future, we might also need context rows for CCIR601 sampling
  22. * or other more-complex downsampling procedures. The code to support
  23. * context rows should be compiled only if needed.
  24. */
  25. #ifdef INPUT_SMOOTHING_SUPPORTED
  26. #define CONTEXT_ROWS_SUPPORTED
  27. #endif
  28. /*
  29. * For the simple (no-context-row) case, we just need to buffer one
  30. * row group's worth of pixels for the downsampling step. At the bottom of
  31. * the image, we pad to a full row group by replicating the last pixel row.
  32. * The downsampler's last output row is then replicated if needed to pad
  33. * out to a full iMCU row.
  34. *
  35. * When providing context rows, we must buffer three row groups' worth of
  36. * pixels. Three row groups are physically allocated, but the row pointer
  37. * arrays are made five row groups high, with the extra pointers above and
  38. * below "wrapping around" to point to the last and first real row groups.
  39. * This allows the downsampler to access the proper context rows.
  40. * At the top and bottom of the image, we create dummy context rows by
  41. * copying the first or last real pixel row. This copying could be avoided
  42. * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the
  43. * trouble on the compression side.
  44. */
  45. /* Private buffer controller object */
  46. typedef struct {
  47. struct jpeg_c_prep_controller pub; /* public fields */
  48. /* Downsampling input buffer. This buffer holds color-converted data
  49. * until we have enough to do a downsample step.
  50. */
  51. JSAMPARRAY color_buf[MAX_COMPONENTS];
  52. JDIMENSION rows_to_go; /* counts rows remaining in source image */
  53. int next_buf_row; /* index of next row to store in color_buf */
  54. #ifdef CONTEXT_ROWS_SUPPORTED /* only needed for context case */
  55. int this_row_group; /* starting row index of group to process */
  56. int next_buf_stop; /* downsample when we reach this index */
  57. #endif
  58. } my_prep_controller;
  59. typedef my_prep_controller * my_prep_ptr;
  60. /*
  61. * Initialize for a processing pass.
  62. */
  63. METHODDEF(void)
  64. start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  65. {
  66. my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  67. if (pass_mode != JBUF_PASS_THRU)
  68. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  69. /* Initialize total-height counter for detecting bottom of image */
  70. prep->rows_to_go = cinfo->image_height;
  71. /* Mark the conversion buffer empty */
  72. prep->next_buf_row = 0;
  73. #ifdef CONTEXT_ROWS_SUPPORTED
  74. /* Preset additional state variables for context mode.
  75. * These aren't used in non-context mode, so we needn't test which mode.
  76. */
  77. prep->this_row_group = 0;
  78. /* Set next_buf_stop to stop after two row groups have been read in. */
  79. prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
  80. #endif
  81. }
  82. /*
  83. * Expand an image vertically from height input_rows to height output_rows,
  84. * by duplicating the bottom row.
  85. */
  86. LOCAL(void)
  87. expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
  88. int input_rows, int output_rows)
  89. {
  90. register int row;
  91. for (row = input_rows; row < output_rows; row++) {
  92. jcopy_sample_rows(image_data + input_rows - 1,
  93. image_data + row,
  94. 1, num_cols);
  95. }
  96. }
  97. /*
  98. * Process some data in the simple no-context case.
  99. *
  100. * Preprocessor output data is counted in "row groups". A row group
  101. * is defined to be v_samp_factor sample rows of each component.
  102. * Downsampling will produce this much data from each max_v_samp_factor
  103. * input rows.
  104. */
  105. METHODDEF(void)
  106. pre_process_data (j_compress_ptr cinfo,
  107. JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  108. JDIMENSION in_rows_avail,
  109. JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
  110. JDIMENSION out_row_groups_avail)
  111. {
  112. my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  113. int numrows, ci;
  114. JDIMENSION inrows;
  115. jpeg_component_info * compptr;
  116. while (*in_row_ctr < in_rows_avail &&
  117. *out_row_group_ctr < out_row_groups_avail) {
  118. /* Do color conversion to fill the conversion buffer. */
  119. inrows = in_rows_avail - *in_row_ctr;
  120. numrows = cinfo->max_v_samp_factor - prep->next_buf_row;
  121. numrows = (int) MIN((JDIMENSION) numrows, inrows);
  122. (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
  123. prep->color_buf,
  124. (JDIMENSION) prep->next_buf_row,
  125. numrows);
  126. *in_row_ctr += numrows;
  127. prep->next_buf_row += numrows;
  128. prep->rows_to_go -= numrows;
  129. /* If at bottom of image, pad to fill the conversion buffer. */
  130. if (prep->rows_to_go == 0 &&
  131. prep->next_buf_row < cinfo->max_v_samp_factor) {
  132. for (ci = 0; ci < cinfo->num_components; ci++) {
  133. expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
  134. prep->next_buf_row, cinfo->max_v_samp_factor);
  135. }
  136. prep->next_buf_row = cinfo->max_v_samp_factor;
  137. }
  138. /* If we've filled the conversion buffer, empty it. */
  139. if (prep->next_buf_row == cinfo->max_v_samp_factor) {
  140. (*cinfo->downsample->downsample) (cinfo,
  141. prep->color_buf, (JDIMENSION) 0,
  142. output_buf, *out_row_group_ctr);
  143. prep->next_buf_row = 0;
  144. (*out_row_group_ctr)++;
  145. }
  146. /* If at bottom of image, pad the output to a full iMCU height.
  147. * Note we assume the caller is providing a one-iMCU-height output buffer!
  148. */
  149. if (prep->rows_to_go == 0 &&
  150. *out_row_group_ctr < out_row_groups_avail) {
  151. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  152. ci++, compptr++) {
  153. numrows = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
  154. cinfo->min_DCT_v_scaled_size;
  155. expand_bottom_edge(output_buf[ci],
  156. compptr->width_in_blocks * compptr->DCT_h_scaled_size,
  157. (int) (*out_row_group_ctr * numrows),
  158. (int) (out_row_groups_avail * numrows));
  159. }
  160. *out_row_group_ctr = out_row_groups_avail;
  161. break; /* can exit outer loop without test */
  162. }
  163. }
  164. }
  165. #ifdef CONTEXT_ROWS_SUPPORTED
  166. /*
  167. * Process some data in the context case.
  168. */
  169. METHODDEF(void)
  170. pre_process_context (j_compress_ptr cinfo,
  171. JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  172. JDIMENSION in_rows_avail,
  173. JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
  174. JDIMENSION out_row_groups_avail)
  175. {
  176. my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  177. int numrows, ci;
  178. int buf_height = cinfo->max_v_samp_factor * 3;
  179. JDIMENSION inrows;
  180. while (*out_row_group_ctr < out_row_groups_avail) {
  181. if (*in_row_ctr < in_rows_avail) {
  182. /* Do color conversion to fill the conversion buffer. */
  183. inrows = in_rows_avail - *in_row_ctr;
  184. numrows = prep->next_buf_stop - prep->next_buf_row;
  185. numrows = (int) MIN((JDIMENSION) numrows, inrows);
  186. (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
  187. prep->color_buf,
  188. (JDIMENSION) prep->next_buf_row,
  189. numrows);
  190. /* Pad at top of image, if first time through */
  191. if (prep->rows_to_go == cinfo->image_height) {
  192. for (ci = 0; ci < cinfo->num_components; ci++) {
  193. int row;
  194. for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
  195. jcopy_sample_rows(prep->color_buf[ci],
  196. prep->color_buf[ci] - row,
  197. 1, cinfo->image_width);
  198. }
  199. }
  200. }
  201. *in_row_ctr += numrows;
  202. prep->next_buf_row += numrows;
  203. prep->rows_to_go -= numrows;
  204. } else {
  205. /* Return for more data, unless we are at the bottom of the image. */
  206. if (prep->rows_to_go != 0)
  207. break;
  208. /* When at bottom of image, pad to fill the conversion buffer. */
  209. if (prep->next_buf_row < prep->next_buf_stop) {
  210. for (ci = 0; ci < cinfo->num_components; ci++) {
  211. expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
  212. prep->next_buf_row, prep->next_buf_stop);
  213. }
  214. prep->next_buf_row = prep->next_buf_stop;
  215. }
  216. }
  217. /* If we've gotten enough data, downsample a row group. */
  218. if (prep->next_buf_row == prep->next_buf_stop) {
  219. (*cinfo->downsample->downsample) (cinfo,
  220. prep->color_buf,
  221. (JDIMENSION) prep->this_row_group,
  222. output_buf, *out_row_group_ctr);
  223. (*out_row_group_ctr)++;
  224. /* Advance pointers with wraparound as necessary. */
  225. prep->this_row_group += cinfo->max_v_samp_factor;
  226. if (prep->this_row_group >= buf_height)
  227. prep->this_row_group = 0;
  228. if (prep->next_buf_row >= buf_height)
  229. prep->next_buf_row = 0;
  230. prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
  231. }
  232. }
  233. }
  234. /*
  235. * Create the wrapped-around downsampling input buffer needed for context mode.
  236. */
  237. LOCAL(void)
  238. create_context_buffer (j_compress_ptr cinfo)
  239. {
  240. my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  241. int rgroup_height = cinfo->max_v_samp_factor;
  242. int ci, i;
  243. jpeg_component_info * compptr;
  244. JSAMPARRAY true_buffer, fake_buffer;
  245. /* Grab enough space for fake row pointers for all the components;
  246. * we need five row groups' worth of pointers for each component.
  247. */
  248. fake_buffer = (JSAMPARRAY) (*cinfo->mem->alloc_small)
  249. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  250. (cinfo->num_components * 5 * rgroup_height) * SIZEOF(JSAMPROW));
  251. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  252. ci++, compptr++) {
  253. /* Allocate the actual buffer space (3 row groups) for this component.
  254. * We make the buffer wide enough to allow the downsampler to edge-expand
  255. * horizontally within the buffer, if it so chooses.
  256. */
  257. true_buffer = (*cinfo->mem->alloc_sarray)
  258. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  259. (JDIMENSION) (((long) compptr->width_in_blocks *
  260. cinfo->min_DCT_h_scaled_size *
  261. cinfo->max_h_samp_factor) / compptr->h_samp_factor),
  262. (JDIMENSION) (3 * rgroup_height));
  263. /* Copy true buffer row pointers into the middle of the fake row array */
  264. MEMCOPY(fake_buffer + rgroup_height, true_buffer,
  265. 3 * rgroup_height * SIZEOF(JSAMPROW));
  266. /* Fill in the above and below wraparound pointers */
  267. for (i = 0; i < rgroup_height; i++) {
  268. fake_buffer[i] = true_buffer[2 * rgroup_height + i];
  269. fake_buffer[4 * rgroup_height + i] = true_buffer[i];
  270. }
  271. prep->color_buf[ci] = fake_buffer + rgroup_height;
  272. fake_buffer += 5 * rgroup_height; /* point to space for next component */
  273. }
  274. }
  275. #endif /* CONTEXT_ROWS_SUPPORTED */
  276. /*
  277. * Initialize preprocessing controller.
  278. */
  279. GLOBAL(void)
  280. jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  281. {
  282. my_prep_ptr prep;
  283. int ci;
  284. jpeg_component_info * compptr;
  285. if (need_full_buffer) /* safety check */
  286. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  287. prep = (my_prep_ptr) (*cinfo->mem->alloc_small)
  288. ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_prep_controller));
  289. cinfo->prep = &prep->pub;
  290. prep->pub.start_pass = start_pass_prep;
  291. /* Allocate the color conversion buffer.
  292. * We make the buffer wide enough to allow the downsampler to edge-expand
  293. * horizontally within the buffer, if it so chooses.
  294. */
  295. if (cinfo->downsample->need_context_rows) {
  296. /* Set up to provide context rows */
  297. #ifdef CONTEXT_ROWS_SUPPORTED
  298. prep->pub.pre_process_data = pre_process_context;
  299. create_context_buffer(cinfo);
  300. #else
  301. ERREXIT(cinfo, JERR_NOT_COMPILED);
  302. #endif
  303. } else {
  304. /* No context, just make it tall enough for one row group */
  305. prep->pub.pre_process_data = pre_process_data;
  306. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  307. ci++, compptr++) {
  308. prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
  309. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  310. (JDIMENSION) (((long) compptr->width_in_blocks *
  311. cinfo->min_DCT_h_scaled_size *
  312. cinfo->max_h_samp_factor) / compptr->h_samp_factor),
  313. (JDIMENSION) cinfo->max_v_samp_factor);
  314. }
  315. }
  316. }