jdpostct.cpp 9.9 KB

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