Source code of Windows XP (NT5)
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.

357 lines
12 KiB

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. /*
  4. * jcprepct.c
  5. *
  6. * Copyright (C) 1994-1996, Thomas G. Lane.
  7. * This file is part of the Independent JPEG Group's software.
  8. * For conditions of distribution and use, see the accompanying README file.
  9. *
  10. * This file contains the compression preprocessing controller.
  11. * This controller manages the color conversion, downsampling,
  12. * and edge expansion steps.
  13. *
  14. * Most of the complexity here is associated with buffering input rows
  15. * as required by the downsampler. See the comments at the head of
  16. * jcsample.c for the downsampler's needs.
  17. */
  18. #define JPEG_INTERNALS
  19. #include "jinclude.h"
  20. #include "jpeglib.h"
  21. /* At present, jcsample.c can request context rows only for smoothing.
  22. * In the future, we might also need context rows for CCIR601 sampling
  23. * or other more-complex downsampling procedures. The code to support
  24. * context rows should be compiled only if needed.
  25. */
  26. #ifdef INPUT_SMOOTHING_SUPPORTED
  27. #define CONTEXT_ROWS_SUPPORTED
  28. #endif
  29. /*
  30. * For the simple (no-context-row) case, we just need to buffer one
  31. * row group's worth of pixels for the downsampling step. At the bottom of
  32. * the image, we pad to a full row group by replicating the last pixel row.
  33. * The downsampler's last output row is then replicated if needed to pad
  34. * out to a full iMCU row.
  35. *
  36. * When providing context rows, we must buffer three row groups' worth of
  37. * pixels. Three row groups are physically allocated, but the row pointer
  38. * arrays are made five row groups high, with the extra pointers above and
  39. * below "wrapping around" to point to the last and first real row groups.
  40. * This allows the downsampler to access the proper context rows.
  41. * At the top and bottom of the image, we create dummy context rows by
  42. * copying the first or last real pixel row. This copying could be avoided
  43. * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the
  44. * trouble on the compression side.
  45. */
  46. /* Private buffer controller object */
  47. typedef struct {
  48. struct jpeg_c_prep_controller pub; /* public fields */
  49. /* Downsampling input buffer. This buffer holds color-converted data
  50. * until we have enough to do a downsample step.
  51. */
  52. JSAMPARRAY color_buf[MAX_COMPONENTS];
  53. JDIMENSION rows_to_go; /* counts rows remaining in source image */
  54. int next_buf_row; /* index of next row to store in color_buf */
  55. #ifdef CONTEXT_ROWS_SUPPORTED /* only needed for context case */
  56. int this_row_group; /* starting row index of group to process */
  57. int next_buf_stop; /* downsample when we reach this index */
  58. #endif
  59. } my_prep_controller;
  60. typedef my_prep_controller * my_prep_ptr;
  61. /*
  62. * Initialize for a processing pass.
  63. */
  64. METHODDEF(void)
  65. start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  66. {
  67. my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  68. if (pass_mode != JBUF_PASS_THRU)
  69. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  70. /* Initialize total-height counter for detecting bottom of image */
  71. prep->rows_to_go = cinfo->image_height;
  72. /* Mark the conversion buffer empty */
  73. prep->next_buf_row = 0;
  74. #ifdef CONTEXT_ROWS_SUPPORTED
  75. /* Preset additional state variables for context mode.
  76. * These aren't used in non-context mode, so we needn't test which mode.
  77. */
  78. prep->this_row_group = 0;
  79. /* Set next_buf_stop to stop after two row groups have been read in. */
  80. prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
  81. #endif
  82. }
  83. /*
  84. * Expand an image vertically from height input_rows to height output_rows,
  85. * by duplicating the bottom row.
  86. */
  87. LOCAL(void)
  88. expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
  89. int input_rows, int output_rows)
  90. {
  91. register int row;
  92. for (row = input_rows; row < output_rows; row++) {
  93. jcopy_sample_rows(image_data, input_rows-1, image_data, row,
  94. 1, num_cols);
  95. }
  96. }
  97. /*
  98. * Process some data in the simple no-context case.
  99. *
  100. * Preprocessor output data is counted in "row groups". A row group
  101. * is defined to be v_samp_factor sample rows of each component.
  102. * Downsampling will produce this much data from each max_v_samp_factor
  103. * input rows.
  104. */
  105. METHODDEF(void)
  106. pre_process_data (j_compress_ptr cinfo,
  107. JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  108. 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 &&
  150. *out_row_group_ctr < out_row_groups_avail) {
  151. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  152. ci++, compptr++) {
  153. expand_bottom_edge(output_buf[ci],
  154. compptr->width_in_blocks * DCTSIZE,
  155. (int) (*out_row_group_ctr * compptr->v_samp_factor),
  156. (int) (out_row_groups_avail * compptr->v_samp_factor));
  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 * DCTSIZE *
  259. cinfo->max_h_samp_factor) / compptr->h_samp_factor),
  260. (JDIMENSION) (3 * rgroup_height));
  261. /* Copy true buffer row pointers into the middle of the fake row array */
  262. MEMCOPY(fake_buffer + rgroup_height, true_buffer,
  263. 3 * rgroup_height * SIZEOF(JSAMPROW));
  264. /* Fill in the above and below wraparound pointers */
  265. for (i = 0; i < rgroup_height; i++) {
  266. fake_buffer[i] = true_buffer[2 * rgroup_height + i];
  267. fake_buffer[4 * rgroup_height + i] = true_buffer[i];
  268. }
  269. prep->color_buf[ci] = fake_buffer + rgroup_height;
  270. fake_buffer += 5 * rgroup_height; /* point to space for next component */
  271. }
  272. }
  273. #endif /* CONTEXT_ROWS_SUPPORTED */
  274. /*
  275. * Initialize preprocessing controller.
  276. */
  277. GLOBAL(void)
  278. jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  279. {
  280. my_prep_ptr prep;
  281. int ci;
  282. jpeg_component_info * compptr;
  283. if (need_full_buffer) /* safety check */
  284. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  285. prep = (my_prep_ptr)
  286. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  287. SIZEOF(my_prep_controller));
  288. cinfo->prep = (struct jpeg_c_prep_controller *) prep;
  289. prep->pub.start_pass = start_pass_prep;
  290. /* Allocate the color conversion buffer.
  291. * We make the buffer wide enough to allow the downsampler to edge-expand
  292. * horizontally within the buffer, if it so chooses.
  293. */
  294. if (cinfo->downsample->need_context_rows) {
  295. /* Set up to provide context rows */
  296. #ifdef CONTEXT_ROWS_SUPPORTED
  297. prep->pub.pre_process_data = pre_process_context;
  298. create_context_buffer(cinfo);
  299. #else
  300. ERREXIT(cinfo, JERR_NOT_COMPILED);
  301. #endif
  302. } else {
  303. /* No context, just make it tall enough for one row group */
  304. prep->pub.pre_process_data = pre_process_data;
  305. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  306. ci++, compptr++) {
  307. prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
  308. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  309. (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
  310. cinfo->max_h_samp_factor) / compptr->h_samp_factor),
  311. (JDIMENSION) cinfo->max_v_samp_factor);
  312. }
  313. }
  314. }