jdpostct.c 11 KB

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