jdpostct.c 9.5 KB

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