jcprepct.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * jcprepct.c
  3. *
  4. * Copyright (C) 1994, 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. // leave this as first line for PCH reasons...
  17. //
  18. #include "../server/exe_headers.h"
  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. jpeg_component_info * compptr;
  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], 0,
  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. }
  209. /* If at bottom of image, pad to fill the conversion buffer. */
  210. if (prep->rows_to_go == 0 &&
  211. prep->next_buf_row < prep->next_buf_stop) {
  212. for (ci = 0; ci < cinfo->num_components; ci++) {
  213. expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
  214. prep->next_buf_row, prep->next_buf_stop);
  215. }
  216. prep->next_buf_row = prep->next_buf_stop;
  217. }
  218. /* If we've gotten enough data, downsample a row group. */
  219. if (prep->next_buf_row == prep->next_buf_stop) {
  220. (*cinfo->downsample->downsample) (cinfo,
  221. prep->color_buf,
  222. (JDIMENSION) prep->this_row_group,
  223. output_buf, *out_row_group_ctr);
  224. (*out_row_group_ctr)++;
  225. /* Advance pointers with wraparound as necessary. */
  226. prep->this_row_group += cinfo->max_v_samp_factor;
  227. if (prep->this_row_group >= buf_height)
  228. prep->this_row_group = 0;
  229. if (prep->next_buf_row >= buf_height)
  230. prep->next_buf_row = 0;
  231. prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
  232. }
  233. /* If at bottom of image, pad the output to a full iMCU height.
  234. * Note we assume the caller is providing a one-iMCU-height output buffer!
  235. */
  236. if (prep->rows_to_go == 0 &&
  237. *out_row_group_ctr < out_row_groups_avail) {
  238. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  239. ci++, compptr++) {
  240. expand_bottom_edge(output_buf[ci],
  241. compptr->width_in_blocks * DCTSIZE,
  242. (int) (*out_row_group_ctr * compptr->v_samp_factor),
  243. (int) (out_row_groups_avail * compptr->v_samp_factor));
  244. }
  245. *out_row_group_ctr = out_row_groups_avail;
  246. break; /* can exit outer loop without test */
  247. }
  248. }
  249. }
  250. /*
  251. * Create the wrapped-around downsampling input buffer needed for context mode.
  252. */
  253. LOCAL void
  254. create_context_buffer (j_compress_ptr cinfo)
  255. {
  256. my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  257. int rgroup_height = cinfo->max_v_samp_factor;
  258. int ci, i;
  259. jpeg_component_info * compptr;
  260. JSAMPARRAY true_buffer, fake_buffer;
  261. /* Grab enough space for fake row pointers for all the components;
  262. * we need five row groups' worth of pointers for each component.
  263. */
  264. fake_buffer = (JSAMPARRAY)
  265. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  266. (cinfo->num_components * 5 * rgroup_height) *
  267. SIZEOF(JSAMPROW));
  268. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  269. ci++, compptr++) {
  270. /* Allocate the actual buffer space (3 row groups) for this component.
  271. * We make the buffer wide enough to allow the downsampler to edge-expand
  272. * horizontally within the buffer, if it so chooses.
  273. */
  274. true_buffer = (*cinfo->mem->alloc_sarray)
  275. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  276. (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
  277. cinfo->max_h_samp_factor) / compptr->h_samp_factor),
  278. (JDIMENSION) (3 * rgroup_height));
  279. /* Copy true buffer row pointers into the middle of the fake row array */
  280. MEMCOPY(fake_buffer + rgroup_height, true_buffer,
  281. 3 * rgroup_height * SIZEOF(JSAMPROW));
  282. /* Fill in the above and below wraparound pointers */
  283. for (i = 0; i < rgroup_height; i++) {
  284. fake_buffer[i] = true_buffer[2 * rgroup_height + i];
  285. fake_buffer[4 * rgroup_height + i] = true_buffer[i];
  286. }
  287. prep->color_buf[ci] = fake_buffer + rgroup_height;
  288. fake_buffer += 5 * rgroup_height; /* point to space for next component */
  289. }
  290. }
  291. #endif /* CONTEXT_ROWS_SUPPORTED */
  292. /*
  293. * Initialize preprocessing controller.
  294. */
  295. GLOBAL void
  296. jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  297. {
  298. my_prep_ptr prep;
  299. int ci;
  300. jpeg_component_info * compptr;
  301. if (need_full_buffer) /* safety check */
  302. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  303. prep = (my_prep_ptr)
  304. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  305. SIZEOF(my_prep_controller));
  306. cinfo->prep = (struct jpeg_c_prep_controller *) prep;
  307. prep->pub.start_pass = start_pass_prep;
  308. /* Allocate the color conversion buffer.
  309. * We make the buffer wide enough to allow the downsampler to edge-expand
  310. * horizontally within the buffer, if it so chooses.
  311. */
  312. if (cinfo->downsample->need_context_rows) {
  313. /* Set up to provide context rows */
  314. #ifdef CONTEXT_ROWS_SUPPORTED
  315. prep->pub.pre_process_data = pre_process_context;
  316. create_context_buffer(cinfo);
  317. #else
  318. ERREXIT(cinfo, JERR_NOT_COMPILED);
  319. #endif
  320. } else {
  321. /* No context, just make it tall enough for one row group */
  322. prep->pub.pre_process_data = pre_process_data;
  323. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  324. ci++, compptr++) {
  325. prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
  326. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  327. (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
  328. cinfo->max_h_samp_factor) / compptr->h_samp_factor),
  329. (JDIMENSION) cinfo->max_v_samp_factor);
  330. }
  331. }
  332. }