jdpostct.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 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. (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  42. JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail,
  43. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  44. JDIMENSION out_rows_avail);
  45. #ifdef QUANT_2PASS_SUPPORTED
  46. METHODDEF(void) post_process_prepass
  47. (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  48. JDIMENSION *in_row_group_ctr, 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. (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
  53. JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail,
  54. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  55. JDIMENSION out_rows_avail);
  56. #endif
  57. /*
  58. * Initialize for a processing pass.
  59. */
  60. METHODDEF(void)
  61. start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
  62. {
  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. post->pub.post_process_data = post_process_prepass;
  91. break;
  92. case JBUF_CRANK_DEST:
  93. /* Second pass of 2-pass quantization */
  94. if (post->whole_image == NULL)
  95. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  96. post->pub.post_process_data = post_process_2pass;
  97. break;
  98. #endif /* QUANT_2PASS_SUPPORTED */
  99. default:
  100. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  101. break;
  102. }
  103. post->starting_row = post->next_row = 0;
  104. }
  105. /*
  106. * Process some data in the one-pass (strip buffer) case.
  107. * This is used for color precision reduction as well as one-pass quantization.
  108. */
  109. METHODDEF(void)
  110. post_process_1pass (j_decompress_ptr cinfo,
  111. JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  112. JDIMENSION in_row_groups_avail,
  113. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  114. JDIMENSION out_rows_avail)
  115. {
  116. my_post_ptr post = (my_post_ptr) cinfo->post;
  117. JDIMENSION num_rows, max_rows;
  118. /* Fill the buffer, but not more than what we can dump out in one go. */
  119. /* Note we rely on the upsampler to detect bottom of image. */
  120. max_rows = out_rows_avail - *out_row_ctr;
  121. if (max_rows > post->strip_height)
  122. max_rows = post->strip_height;
  123. num_rows = 0;
  124. (*cinfo->upsample->upsample) (cinfo,
  125. input_buf, in_row_group_ctr, in_row_groups_avail,
  126. post->buffer, &num_rows, max_rows);
  127. /* Quantize and emit data. */
  128. (*cinfo->cquantize->color_quantize) (cinfo,
  129. post->buffer, output_buf + *out_row_ctr, (int) num_rows);
  130. *out_row_ctr += num_rows;
  131. }
  132. #ifdef QUANT_2PASS_SUPPORTED
  133. /*
  134. * Process some data in the first pass of 2-pass quantization.
  135. */
  136. METHODDEF(void)
  137. post_process_prepass (j_decompress_ptr cinfo,
  138. JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  139. JDIMENSION in_row_groups_avail,
  140. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  141. JDIMENSION out_rows_avail)
  142. {
  143. my_post_ptr post = (my_post_ptr) cinfo->post;
  144. JDIMENSION old_next_row, num_rows;
  145. /* Reposition virtual buffer if at start of strip. */
  146. if (post->next_row == 0) {
  147. post->buffer = (*cinfo->mem->access_virt_sarray)
  148. ((j_common_ptr) cinfo, post->whole_image,
  149. post->starting_row, post->strip_height, TRUE);
  150. }
  151. /* Upsample some data (up to a strip height's worth). */
  152. old_next_row = post->next_row;
  153. (*cinfo->upsample->upsample) (cinfo,
  154. input_buf, in_row_group_ctr, in_row_groups_avail,
  155. post->buffer, &post->next_row, post->strip_height);
  156. /* Allow quantizer to scan new data. No data is emitted, */
  157. /* but we advance out_row_ctr so outer loop can tell when we're done. */
  158. if (post->next_row > old_next_row) {
  159. num_rows = post->next_row - old_next_row;
  160. (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row,
  161. (JSAMPARRAY) NULL, (int) num_rows);
  162. *out_row_ctr += num_rows;
  163. }
  164. /* Advance if we filled the strip. */
  165. if (post->next_row >= post->strip_height) {
  166. post->starting_row += post->strip_height;
  167. post->next_row = 0;
  168. }
  169. }
  170. /*
  171. * Process some data in the second pass of 2-pass quantization.
  172. */
  173. METHODDEF(void)
  174. post_process_2pass (j_decompress_ptr cinfo,
  175. JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  176. JDIMENSION in_row_groups_avail,
  177. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  178. JDIMENSION out_rows_avail)
  179. {
  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. /* We have to check bottom of image here, can't depend on upsampler. */
  194. max_rows = cinfo->output_height - post->starting_row;
  195. if (num_rows > max_rows)
  196. num_rows = max_rows;
  197. /* Quantize and emit data. */
  198. (*cinfo->cquantize->color_quantize) (cinfo,
  199. post->buffer + post->next_row, output_buf + *out_row_ctr,
  200. (int) num_rows);
  201. *out_row_ctr += num_rows;
  202. /* Advance if we filled the strip. */
  203. post->next_row += num_rows;
  204. if (post->next_row >= post->strip_height) {
  205. post->starting_row += post->strip_height;
  206. post->next_row = 0;
  207. }
  208. }
  209. #endif /* QUANT_2PASS_SUPPORTED */
  210. /*
  211. * Initialize postprocessing controller.
  212. */
  213. GLOBAL(void)
  214. jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
  215. {
  216. my_post_ptr post;
  217. post = (my_post_ptr)
  218. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  219. sizeof(my_post_controller));
  220. cinfo->post = (struct jpeg_d_post_controller *) post;
  221. post->pub.start_pass = start_pass_dpost;
  222. post->whole_image = NULL; /* flag for no virtual arrays */
  223. post->buffer = NULL; /* flag for no strip buffer */
  224. /* Create the quantization buffer, if needed */
  225. if (cinfo->quantize_colors) {
  226. /* The buffer strip height is max_v_samp_factor, which is typically
  227. * an efficient number of rows for upsampling to return.
  228. * (In the presence of output rescaling, we might want to be smarter?)
  229. */
  230. post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor;
  231. if (need_full_buffer) {
  232. /* Two-pass color quantization: need full-image storage. */
  233. /* We round up the number of rows to a multiple of the strip height. */
  234. #ifdef QUANT_2PASS_SUPPORTED
  235. post->whole_image = (*cinfo->mem->request_virt_sarray)
  236. ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  237. cinfo->output_width * cinfo->out_color_components,
  238. (JDIMENSION) jround_up((long) cinfo->output_height,
  239. (long) post->strip_height),
  240. post->strip_height);
  241. #else
  242. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  243. #endif /* QUANT_2PASS_SUPPORTED */
  244. } else {
  245. /* One-pass color quantization: just make a strip buffer. */
  246. post->buffer = (*cinfo->mem->alloc_sarray)
  247. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  248. cinfo->output_width * cinfo->out_color_components,
  249. post->strip_height);
  250. }
  251. }
  252. }