Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

371 lines
12 KiB

  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. #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. expand_bottom_edge(output_buf[ci],
  152. 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,
  167. JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  168. JDIMENSION in_rows_avail,
  169. JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
  170. JDIMENSION out_row_groups_avail)
  171. {
  172. my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  173. int numrows, ci;
  174. int buf_height = cinfo->max_v_samp_factor * 3;
  175. JDIMENSION inrows;
  176. jpeg_component_info * compptr;
  177. while (*out_row_group_ctr < out_row_groups_avail) {
  178. if (*in_row_ctr < in_rows_avail) {
  179. /* Do color conversion to fill the conversion buffer. */
  180. inrows = in_rows_avail - *in_row_ctr;
  181. numrows = prep->next_buf_stop - prep->next_buf_row;
  182. numrows = (int) MIN((JDIMENSION) numrows, inrows);
  183. (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
  184. prep->color_buf,
  185. (JDIMENSION) prep->next_buf_row,
  186. numrows);
  187. /* Pad at top of image, if first time through */
  188. if (prep->rows_to_go == cinfo->image_height) {
  189. for (ci = 0; ci < cinfo->num_components; ci++) {
  190. int row;
  191. for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
  192. jcopy_sample_rows(prep->color_buf[ci], 0,
  193. prep->color_buf[ci], -row,
  194. 1, cinfo->image_width);
  195. }
  196. }
  197. }
  198. *in_row_ctr += numrows;
  199. prep->next_buf_row += numrows;
  200. prep->rows_to_go -= numrows;
  201. } else {
  202. /* Return for more data, unless we are at the bottom of the image. */
  203. if (prep->rows_to_go != 0)
  204. break;
  205. }
  206. /* If at bottom of image, pad to fill the conversion buffer. */
  207. if (prep->rows_to_go == 0 &&
  208. 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. /* 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. /* If at bottom of image, pad the output to a full iMCU height.
  231. * Note we assume the caller is providing a one-iMCU-height output buffer!
  232. */
  233. if (prep->rows_to_go == 0 &&
  234. *out_row_group_ctr < out_row_groups_avail) {
  235. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  236. ci++, compptr++) {
  237. expand_bottom_edge(output_buf[ci],
  238. compptr->width_in_blocks * DCTSIZE,
  239. (int) (*out_row_group_ctr * compptr->v_samp_factor),
  240. (int) (out_row_groups_avail * compptr->v_samp_factor));
  241. }
  242. *out_row_group_ctr = out_row_groups_avail;
  243. break; /* can exit outer loop without test */
  244. }
  245. }
  246. }
  247. /*
  248. * Create the wrapped-around downsampling input buffer needed for context mode.
  249. */
  250. LOCAL void
  251. create_context_buffer (j_compress_ptr cinfo)
  252. {
  253. my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  254. int rgroup_height = cinfo->max_v_samp_factor;
  255. int ci, i;
  256. jpeg_component_info * compptr;
  257. JSAMPARRAY true_buffer, fake_buffer;
  258. /* Grab enough space for fake row pointers for all the components;
  259. * we need five row groups' worth of pointers for each component.
  260. */
  261. fake_buffer = (JSAMPARRAY)
  262. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  263. (cinfo->num_components * 5 * rgroup_height) *
  264. SIZEOF(JSAMPROW));
  265. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  266. ci++, compptr++) {
  267. /* Allocate the actual buffer space (3 row groups) for this component.
  268. * We make the buffer wide enough to allow the downsampler to edge-expand
  269. * horizontally within the buffer, if it so chooses.
  270. */
  271. true_buffer = (*cinfo->mem->alloc_sarray)
  272. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  273. (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
  274. cinfo->max_h_samp_factor) / compptr->h_samp_factor),
  275. (JDIMENSION) (3 * rgroup_height));
  276. /* Copy true buffer row pointers into the middle of the fake row array */
  277. MEMCOPY(fake_buffer + rgroup_height, true_buffer,
  278. 3 * rgroup_height * SIZEOF(JSAMPROW));
  279. /* Fill in the above and below wraparound pointers */
  280. for (i = 0; i < rgroup_height; i++) {
  281. fake_buffer[i] = true_buffer[2 * rgroup_height + i];
  282. fake_buffer[4 * rgroup_height + i] = true_buffer[i];
  283. }
  284. prep->color_buf[ci] = fake_buffer + rgroup_height;
  285. fake_buffer += 5 * rgroup_height; /* point to space for next component */
  286. }
  287. }
  288. #endif /* CONTEXT_ROWS_SUPPORTED */
  289. /*
  290. * Initialize preprocessing controller.
  291. */
  292. GLOBAL void
  293. jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  294. {
  295. my_prep_ptr prep;
  296. int ci;
  297. jpeg_component_info * compptr;
  298. if (need_full_buffer) /* safety check */
  299. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  300. prep = (my_prep_ptr)
  301. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  302. SIZEOF(my_prep_controller));
  303. cinfo->prep = (struct jpeg_c_prep_controller *) prep;
  304. prep->pub.start_pass = start_pass_prep;
  305. /* Allocate the color conversion buffer.
  306. * We make the buffer wide enough to allow the downsampler to edge-expand
  307. * horizontally within the buffer, if it so chooses.
  308. */
  309. if (cinfo->downsample->need_context_rows) {
  310. /* Set up to provide context rows */
  311. #ifdef CONTEXT_ROWS_SUPPORTED
  312. prep->pub.pre_process_data = pre_process_context;
  313. create_context_buffer(cinfo);
  314. #else
  315. ERREXIT(cinfo, JERR_NOT_COMPILED);
  316. #endif
  317. } else {
  318. /* No context, just make it tall enough for one row group */
  319. prep->pub.pre_process_data = pre_process_data;
  320. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  321. ci++, compptr++) {
  322. prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
  323. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  324. (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
  325. cinfo->max_h_samp_factor) / compptr->h_samp_factor),
  326. (JDIMENSION) cinfo->max_v_samp_factor);
  327. }
  328. }
  329. }