jcprepct.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. * libjpeg-turbo Modifications:
  7. * Copyright (C) 2022, D. R. Commander.
  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, int input_rows,
  90. 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, 1,
  95. 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, JSAMPARRAY input_buf,
  108. JDIMENSION *in_row_ctr, 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 && *out_row_group_ctr < out_row_groups_avail) {
  150. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  151. ci++, compptr++) {
  152. expand_bottom_edge(output_buf[ci], compptr->width_in_blocks * DCTSIZE,
  153. (int)(*out_row_group_ctr * compptr->v_samp_factor),
  154. (int)(out_row_groups_avail * compptr->v_samp_factor));
  155. }
  156. *out_row_group_ctr = out_row_groups_avail;
  157. break; /* can exit outer loop without test */
  158. }
  159. }
  160. }
  161. #ifdef CONTEXT_ROWS_SUPPORTED
  162. /*
  163. * Process some data in the context case.
  164. */
  165. METHODDEF(void)
  166. pre_process_context(j_compress_ptr cinfo, JSAMPARRAY input_buf,
  167. JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail,
  168. JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
  169. JDIMENSION out_row_groups_avail)
  170. {
  171. my_prep_ptr prep = (my_prep_ptr)cinfo->prep;
  172. int numrows, ci;
  173. int buf_height = cinfo->max_v_samp_factor * 3;
  174. JDIMENSION inrows;
  175. while (*out_row_group_ctr < out_row_groups_avail) {
  176. if (*in_row_ctr < in_rows_avail) {
  177. /* Do color conversion to fill the conversion buffer. */
  178. inrows = in_rows_avail - *in_row_ctr;
  179. numrows = prep->next_buf_stop - prep->next_buf_row;
  180. numrows = (int)MIN((JDIMENSION)numrows, inrows);
  181. (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
  182. prep->color_buf,
  183. (JDIMENSION)prep->next_buf_row,
  184. numrows);
  185. /* Pad at top of image, if first time through */
  186. if (prep->rows_to_go == cinfo->image_height) {
  187. for (ci = 0; ci < cinfo->num_components; ci++) {
  188. int row;
  189. for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
  190. jcopy_sample_rows(prep->color_buf[ci], 0, prep->color_buf[ci],
  191. -row, 1, cinfo->image_width);
  192. }
  193. }
  194. }
  195. *in_row_ctr += numrows;
  196. prep->next_buf_row += numrows;
  197. prep->rows_to_go -= numrows;
  198. } else {
  199. /* Return for more data, unless we are at the bottom of the image. */
  200. if (prep->rows_to_go != 0)
  201. break;
  202. /* When at bottom of image, pad to fill the conversion buffer. */
  203. if (prep->next_buf_row < prep->next_buf_stop) {
  204. for (ci = 0; ci < cinfo->num_components; ci++) {
  205. expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
  206. prep->next_buf_row, prep->next_buf_stop);
  207. }
  208. prep->next_buf_row = prep->next_buf_stop;
  209. }
  210. }
  211. /* If we've gotten enough data, downsample a row group. */
  212. if (prep->next_buf_row == prep->next_buf_stop) {
  213. (*cinfo->downsample->downsample) (cinfo, prep->color_buf,
  214. (JDIMENSION)prep->this_row_group,
  215. output_buf, *out_row_group_ctr);
  216. (*out_row_group_ctr)++;
  217. /* Advance pointers with wraparound as necessary. */
  218. prep->this_row_group += cinfo->max_v_samp_factor;
  219. if (prep->this_row_group >= buf_height)
  220. prep->this_row_group = 0;
  221. if (prep->next_buf_row >= buf_height)
  222. prep->next_buf_row = 0;
  223. prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
  224. }
  225. }
  226. }
  227. /*
  228. * Create the wrapped-around downsampling input buffer needed for context mode.
  229. */
  230. LOCAL(void)
  231. create_context_buffer(j_compress_ptr cinfo)
  232. {
  233. my_prep_ptr prep = (my_prep_ptr)cinfo->prep;
  234. int rgroup_height = cinfo->max_v_samp_factor;
  235. int ci, i;
  236. jpeg_component_info *compptr;
  237. JSAMPARRAY true_buffer, fake_buffer;
  238. /* Grab enough space for fake row pointers for all the components;
  239. * we need five row groups' worth of pointers for each component.
  240. */
  241. fake_buffer = (JSAMPARRAY)
  242. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  243. (cinfo->num_components * 5 * rgroup_height) *
  244. sizeof(JSAMPROW));
  245. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  246. ci++, compptr++) {
  247. /* Allocate the actual buffer space (3 row groups) for this component.
  248. * We make the buffer wide enough to allow the downsampler to edge-expand
  249. * horizontally within the buffer, if it so chooses.
  250. */
  251. true_buffer = (*cinfo->mem->alloc_sarray)
  252. ((j_common_ptr)cinfo, JPOOL_IMAGE,
  253. (JDIMENSION)(((long)compptr->width_in_blocks * DCTSIZE *
  254. cinfo->max_h_samp_factor) / compptr->h_samp_factor),
  255. (JDIMENSION)(3 * rgroup_height));
  256. /* Copy true buffer row pointers into the middle of the fake row array */
  257. memcpy(fake_buffer + rgroup_height, true_buffer,
  258. 3 * rgroup_height * sizeof(JSAMPROW));
  259. /* Fill in the above and below wraparound pointers */
  260. for (i = 0; i < rgroup_height; i++) {
  261. fake_buffer[i] = true_buffer[2 * rgroup_height + i];
  262. fake_buffer[4 * rgroup_height + i] = true_buffer[i];
  263. }
  264. prep->color_buf[ci] = fake_buffer + rgroup_height;
  265. fake_buffer += 5 * rgroup_height; /* point to space for next component */
  266. }
  267. }
  268. #endif /* CONTEXT_ROWS_SUPPORTED */
  269. /*
  270. * Initialize preprocessing controller.
  271. */
  272. GLOBAL(void)
  273. jinit_c_prep_controller(j_compress_ptr cinfo, boolean need_full_buffer)
  274. {
  275. my_prep_ptr prep;
  276. int ci;
  277. jpeg_component_info *compptr;
  278. if (need_full_buffer) /* safety check */
  279. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  280. prep = (my_prep_ptr)
  281. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  282. sizeof(my_prep_controller));
  283. cinfo->prep = (struct jpeg_c_prep_controller *)prep;
  284. prep->pub.start_pass = start_pass_prep;
  285. /* Allocate the color conversion buffer.
  286. * We make the buffer wide enough to allow the downsampler to edge-expand
  287. * horizontally within the buffer, if it so chooses.
  288. */
  289. if (cinfo->downsample->need_context_rows) {
  290. /* Set up to provide context rows */
  291. #ifdef CONTEXT_ROWS_SUPPORTED
  292. prep->pub.pre_process_data = pre_process_context;
  293. create_context_buffer(cinfo);
  294. #else
  295. ERREXIT(cinfo, JERR_NOT_COMPILED);
  296. #endif
  297. } else {
  298. /* No context, just make it tall enough for one row group */
  299. prep->pub.pre_process_data = pre_process_data;
  300. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  301. ci++, compptr++) {
  302. prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
  303. ((j_common_ptr)cinfo, JPOOL_IMAGE,
  304. (JDIMENSION)(((long)compptr->width_in_blocks * DCTSIZE *
  305. cinfo->max_h_samp_factor) / compptr->h_samp_factor),
  306. (JDIMENSION)cinfo->max_v_samp_factor);
  307. }
  308. }
  309. }