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.

293 lines
9.5 KiB

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. /*
  4. * jdpostct.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 decompression postprocessing controller.
  11. * This controller manages the upsampling, color conversion, and color
  12. * quantization/reduction steps; specifically, it controls the buffering
  13. * between upsample/color conversion and color quantization/reduction.
  14. *
  15. * If no color quantization/reduction is required, then this module has no
  16. * work to do, and it just hands off to the upsample/color conversion code.
  17. * An integrated upsample/convert/quantize process would replace this module
  18. * entirely.
  19. */
  20. #define JPEG_INTERNALS
  21. #include "jinclude.h"
  22. #include "jpeglib.h"
  23. /* Private buffer controller object */
  24. typedef struct {
  25. struct jpeg_d_post_controller pub; /* public fields */
  26. /* Color quantization source buffer: this holds output data from
  27. * the upsample/color conversion step to be passed to the quantizer.
  28. * For two-pass color quantization, we need a full-image buffer;
  29. * for one-pass operation, a strip buffer is sufficient.
  30. */
  31. jvirt_sarray_ptr whole_image; /* virtual array, or NULL if one-pass */
  32. JSAMPARRAY buffer; /* strip buffer, or current strip of virtual */
  33. JDIMENSION strip_height; /* buffer size in rows */
  34. /* for two-pass mode only: */
  35. JDIMENSION starting_row; /* row # of first row in current strip */
  36. JDIMENSION next_row; /* index of next row to fill/empty in strip */
  37. } my_post_controller;
  38. typedef my_post_controller * my_post_ptr;
  39. /* Forward declarations */
  40. METHODDEF(void) post_process_1pass
  41. JPP((j_decompress_ptr cinfo,
  42. JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  43. JDIMENSION in_row_groups_avail,
  44. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  45. JDIMENSION out_rows_avail));
  46. #ifdef QUANT_2PASS_SUPPORTED
  47. METHODDEF(void) post_process_prepass
  48. JPP((j_decompress_ptr cinfo,
  49. JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  50. JDIMENSION in_row_groups_avail,
  51. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  52. JDIMENSION out_rows_avail));
  53. METHODDEF(void) post_process_2pass
  54. JPP((j_decompress_ptr cinfo,
  55. JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  56. JDIMENSION in_row_groups_avail,
  57. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  58. JDIMENSION out_rows_avail));
  59. #endif
  60. /*
  61. * Initialize for a processing pass.
  62. */
  63. METHODDEF(void)
  64. start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
  65. {
  66. my_post_ptr post = (my_post_ptr) cinfo->post;
  67. switch (pass_mode) {
  68. case JBUF_PASS_THRU:
  69. if (cinfo->quantize_colors) {
  70. /* Single-pass processing with color quantization. */
  71. post->pub.post_process_data = post_process_1pass;
  72. /* We could be doing buffered-image output before starting a 2-pass
  73. * color quantization; in that case, jinit_d_post_controller did not
  74. * allocate a strip buffer. Use the virtual-array buffer as workspace.
  75. */
  76. if (post->buffer == NULL) {
  77. post->buffer = (*cinfo->mem->access_virt_sarray)
  78. ((j_common_ptr) cinfo, post->whole_image,
  79. (JDIMENSION) 0, post->strip_height, TRUE);
  80. }
  81. } else {
  82. /* For single-pass processing without color quantization,
  83. * I have no work to do; just call the upsampler directly.
  84. */
  85. post->pub.post_process_data = cinfo->upsample->upsample;
  86. }
  87. break;
  88. #ifdef QUANT_2PASS_SUPPORTED
  89. case JBUF_SAVE_AND_PASS:
  90. /* First pass of 2-pass quantization */
  91. if (post->whole_image == NULL)
  92. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  93. post->pub.post_process_data = post_process_prepass;
  94. break;
  95. case JBUF_CRANK_DEST:
  96. /* Second pass of 2-pass quantization */
  97. if (post->whole_image == NULL)
  98. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  99. post->pub.post_process_data = post_process_2pass;
  100. break;
  101. #endif /* QUANT_2PASS_SUPPORTED */
  102. default:
  103. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  104. break;
  105. }
  106. post->starting_row = post->next_row = 0;
  107. }
  108. /*
  109. * Process some data in the one-pass (strip buffer) case.
  110. * This is used for color precision reduction as well as one-pass quantization.
  111. */
  112. METHODDEF(void)
  113. post_process_1pass (j_decompress_ptr cinfo,
  114. JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  115. JDIMENSION in_row_groups_avail,
  116. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  117. JDIMENSION out_rows_avail)
  118. {
  119. my_post_ptr post = (my_post_ptr) cinfo->post;
  120. JDIMENSION num_rows, max_rows;
  121. /* Fill the buffer, but not more than what we can dump out in one go. */
  122. /* Note we rely on the upsampler to detect bottom of image. */
  123. max_rows = out_rows_avail - *out_row_ctr;
  124. if (max_rows > post->strip_height)
  125. max_rows = post->strip_height;
  126. num_rows = 0;
  127. (*cinfo->upsample->upsample) (cinfo,
  128. input_buf, in_row_group_ctr, in_row_groups_avail,
  129. post->buffer, &num_rows, max_rows);
  130. /* Quantize and emit data. */
  131. (*cinfo->cquantize->color_quantize) (cinfo,
  132. post->buffer, output_buf + *out_row_ctr, (int) num_rows);
  133. *out_row_ctr += num_rows;
  134. }
  135. #ifdef QUANT_2PASS_SUPPORTED
  136. /*
  137. * Process some data in the first pass of 2-pass quantization.
  138. */
  139. METHODDEF(void)
  140. post_process_prepass (j_decompress_ptr cinfo,
  141. JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  142. JDIMENSION in_row_groups_avail,
  143. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  144. JDIMENSION out_rows_avail)
  145. {
  146. my_post_ptr post = (my_post_ptr) cinfo->post;
  147. JDIMENSION old_next_row, num_rows;
  148. /* Reposition virtual buffer if at start of strip. */
  149. if (post->next_row == 0) {
  150. post->buffer = (*cinfo->mem->access_virt_sarray)
  151. ((j_common_ptr) cinfo, post->whole_image,
  152. post->starting_row, post->strip_height, TRUE);
  153. }
  154. /* Upsample some data (up to a strip height's worth). */
  155. old_next_row = post->next_row;
  156. (*cinfo->upsample->upsample) (cinfo,
  157. input_buf, in_row_group_ctr, in_row_groups_avail,
  158. post->buffer, &post->next_row, post->strip_height);
  159. /* Allow quantizer to scan new data. No data is emitted, */
  160. /* but we advance out_row_ctr so outer loop can tell when we're done. */
  161. if (post->next_row > old_next_row) {
  162. num_rows = post->next_row - old_next_row;
  163. (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row,
  164. (JSAMPARRAY) NULL, (int) num_rows);
  165. *out_row_ctr += num_rows;
  166. }
  167. /* Advance if we filled the strip. */
  168. if (post->next_row >= post->strip_height) {
  169. post->starting_row += post->strip_height;
  170. post->next_row = 0;
  171. }
  172. }
  173. /*
  174. * Process some data in the second pass of 2-pass quantization.
  175. */
  176. METHODDEF(void)
  177. post_process_2pass (j_decompress_ptr cinfo,
  178. JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  179. JDIMENSION in_row_groups_avail,
  180. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  181. JDIMENSION out_rows_avail)
  182. {
  183. my_post_ptr post = (my_post_ptr) cinfo->post;
  184. JDIMENSION num_rows, max_rows;
  185. /* Reposition virtual buffer if at start of strip. */
  186. if (post->next_row == 0) {
  187. post->buffer = (*cinfo->mem->access_virt_sarray)
  188. ((j_common_ptr) cinfo, post->whole_image,
  189. post->starting_row, post->strip_height, FALSE);
  190. }
  191. /* Determine number of rows to emit. */
  192. num_rows = post->strip_height - post->next_row; /* available in strip */
  193. max_rows = out_rows_avail - *out_row_ctr; /* available in output area */
  194. if (num_rows > max_rows)
  195. num_rows = max_rows;
  196. /* We have to check bottom of image here, can't depend on upsampler. */
  197. max_rows = cinfo->output_height - post->starting_row;
  198. if (num_rows > max_rows)
  199. num_rows = max_rows;
  200. /* Quantize and emit data. */
  201. (*cinfo->cquantize->color_quantize) (cinfo,
  202. post->buffer + post->next_row, output_buf + *out_row_ctr,
  203. (int) num_rows);
  204. *out_row_ctr += num_rows;
  205. /* Advance if we filled the strip. */
  206. post->next_row += num_rows;
  207. if (post->next_row >= post->strip_height) {
  208. post->starting_row += post->strip_height;
  209. post->next_row = 0;
  210. }
  211. }
  212. #endif /* QUANT_2PASS_SUPPORTED */
  213. /*
  214. * Initialize postprocessing controller.
  215. */
  216. GLOBAL(void)
  217. jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
  218. {
  219. my_post_ptr post;
  220. post = (my_post_ptr)
  221. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  222. SIZEOF(my_post_controller));
  223. cinfo->post = (struct jpeg_d_post_controller *) post;
  224. post->pub.start_pass = start_pass_dpost;
  225. post->whole_image = NULL; /* flag for no virtual arrays */
  226. post->buffer = NULL; /* flag for no strip buffer */
  227. /* Create the quantization buffer, if needed */
  228. if (cinfo->quantize_colors) {
  229. /* The buffer strip height is max_v_samp_factor, which is typically
  230. * an efficient number of rows for upsampling to return.
  231. * (In the presence of output rescaling, we might want to be smarter?)
  232. */
  233. post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor;
  234. if (need_full_buffer) {
  235. /* Two-pass color quantization: need full-image storage. */
  236. /* We round up the number of rows to a multiple of the strip height. */
  237. #ifdef QUANT_2PASS_SUPPORTED
  238. post->whole_image = (*cinfo->mem->request_virt_sarray)
  239. ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  240. cinfo->output_width * cinfo->out_color_components,
  241. (JDIMENSION) jround_up((long) cinfo->output_height,
  242. (long) post->strip_height),
  243. post->strip_height);
  244. #else
  245. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  246. #endif /* QUANT_2PASS_SUPPORTED */
  247. } else {
  248. /* One-pass color quantization: just make a strip buffer. */
  249. post->buffer = (*cinfo->mem->alloc_sarray)
  250. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  251. cinfo->output_width * cinfo->out_color_components,
  252. post->strip_height);
  253. }
  254. }
  255. }