jcprepct.c 13 KB

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