jdpostct.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. #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. my_post_ptr post = (my_post_ptr) cinfo->post;
  64. switch ( pass_mode ) {
  65. case JBUF_PASS_THRU:
  66. if ( cinfo->quantize_colors ) {
  67. /* Single-pass processing with color quantization. */
  68. post->pub.post_process_data = post_process_1pass;
  69. /* We could be doing buffered-image output before starting a 2-pass
  70. * color quantization; in that case, jinit_d_post_controller did not
  71. * allocate a strip buffer. Use the virtual-array buffer as workspace.
  72. */
  73. if ( post->buffer == NULL ) {
  74. post->buffer = ( *cinfo->mem->access_virt_sarray )
  75. ( (j_common_ptr) cinfo, post->whole_image,
  76. (JDIMENSION) 0, post->strip_height, TRUE );
  77. }
  78. } else {
  79. /* For single-pass processing without color quantization,
  80. * I have no work to do; just call the upsampler directly.
  81. */
  82. post->pub.post_process_data = cinfo->upsample->upsample;
  83. }
  84. break;
  85. #ifdef QUANT_2PASS_SUPPORTED
  86. case JBUF_SAVE_AND_PASS:
  87. /* First pass of 2-pass quantization */
  88. if ( post->whole_image == NULL ) {
  89. ERREXIT( cinfo, JERR_BAD_BUFFER_MODE );
  90. }
  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. }
  98. post->pub.post_process_data = post_process_2pass;
  99. break;
  100. #endif /* QUANT_2PASS_SUPPORTED */
  101. default:
  102. ERREXIT( cinfo, JERR_BAD_BUFFER_MODE );
  103. break;
  104. }
  105. post->starting_row = post->next_row = 0;
  106. }
  107. /*
  108. * Process some data in the one-pass (strip buffer) case.
  109. * This is used for color precision reduction as well as one-pass quantization.
  110. */
  111. METHODDEF void
  112. post_process_1pass( j_decompress_ptr cinfo,
  113. JSAMPIMAGE input_buf, JDIMENSION * in_row_group_ctr,
  114. JDIMENSION in_row_groups_avail,
  115. JSAMPARRAY output_buf, JDIMENSION * out_row_ctr,
  116. JDIMENSION out_rows_avail ) {
  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. }
  125. num_rows = 0;
  126. ( *cinfo->upsample->upsample )( cinfo,
  127. input_buf, in_row_group_ctr, in_row_groups_avail,
  128. post->buffer, &num_rows, max_rows );
  129. /* Quantize and emit data. */
  130. ( *cinfo->cquantize->color_quantize )( cinfo,
  131. post->buffer, output_buf + *out_row_ctr, (int) num_rows );
  132. *out_row_ctr += num_rows;
  133. }
  134. #ifdef QUANT_2PASS_SUPPORTED
  135. /*
  136. * Process some data in the first pass of 2-pass quantization.
  137. */
  138. METHODDEF void
  139. post_process_prepass( j_decompress_ptr cinfo,
  140. JSAMPIMAGE input_buf, JDIMENSION * in_row_group_ctr,
  141. JDIMENSION in_row_groups_avail,
  142. JSAMPARRAY output_buf, JDIMENSION * out_row_ctr,
  143. JDIMENSION out_rows_avail ) {
  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. my_post_ptr post = (my_post_ptr) cinfo->post;
  181. JDIMENSION num_rows, max_rows;
  182. /* Reposition virtual buffer if at start of strip. */
  183. if ( post->next_row == 0 ) {
  184. post->buffer = ( *cinfo->mem->access_virt_sarray )
  185. ( (j_common_ptr) cinfo, post->whole_image,
  186. post->starting_row, post->strip_height, FALSE );
  187. }
  188. /* Determine number of rows to emit. */
  189. num_rows = post->strip_height - post->next_row;/* available in strip */
  190. max_rows = out_rows_avail - *out_row_ctr;/* available in output area */
  191. if ( num_rows > max_rows ) {
  192. num_rows = max_rows;
  193. }
  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. }
  199. /* Quantize and emit data. */
  200. ( *cinfo->cquantize->color_quantize )( cinfo,
  201. post->buffer + post->next_row, output_buf + *out_row_ctr,
  202. (int) num_rows );
  203. *out_row_ctr += num_rows;
  204. /* Advance if we filled the strip. */
  205. post->next_row += num_rows;
  206. if ( post->next_row >= post->strip_height ) {
  207. post->starting_row += post->strip_height;
  208. post->next_row = 0;
  209. }
  210. }
  211. #endif /* QUANT_2PASS_SUPPORTED */
  212. /*
  213. * Initialize postprocessing controller.
  214. */
  215. GLOBAL void
  216. jinit_d_post_controller( j_decompress_ptr cinfo, boolean need_full_buffer ) {
  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. }