jcprepct.c 12 KB

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