jcmainct.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * jcmainct.c
  3. *
  4. * Copyright (C) 1994-1996, Thomas G. Lane.
  5. * Modified 2003-2012 by Guido Vollbeding.
  6. * This file is part of the Independent JPEG Group's software.
  7. * For conditions of distribution and use, see the accompanying README file.
  8. *
  9. * This file contains the main buffer controller for compression.
  10. * The main buffer lies between the pre-processor and the JPEG
  11. * compressor proper; it holds downsampled data in the JPEG colorspace.
  12. */
  13. #define JPEG_INTERNALS
  14. #include "jinclude.h"
  15. #include "jpeglib.h"
  16. /* Note: currently, there is no operating mode in which a full-image buffer
  17. * is needed at this step. If there were, that mode could not be used with
  18. * "raw data" input, since this module is bypassed in that case. However,
  19. * we've left the code here for possible use in special applications.
  20. */
  21. #undef FULL_MAIN_BUFFER_SUPPORTED
  22. /* Private buffer controller object */
  23. typedef struct {
  24. struct jpeg_c_main_controller pub; /* public fields */
  25. JDIMENSION cur_iMCU_row; /* number of current iMCU row */
  26. JDIMENSION rowgroup_ctr; /* counts row groups received in iMCU row */
  27. boolean suspended; /* remember if we suspended output */
  28. J_BUF_MODE pass_mode; /* current operating mode */
  29. /* If using just a strip buffer, this points to the entire set of buffers
  30. * (we allocate one for each component). In the full-image case, this
  31. * points to the currently accessible strips of the virtual arrays.
  32. */
  33. JSAMPARRAY buffer[MAX_COMPONENTS];
  34. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  35. /* If using full-image storage, this array holds pointers to virtual-array
  36. * control blocks for each component. Unused if not full-image storage.
  37. */
  38. jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
  39. #endif
  40. } my_main_controller;
  41. typedef my_main_controller * my_main_ptr;
  42. /* Forward declarations */
  43. METHODDEF(void) process_data_simple_main
  44. JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
  45. JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
  46. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  47. METHODDEF(void) process_data_buffer_main
  48. JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
  49. JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
  50. #endif
  51. /*
  52. * Initialize for a processing pass.
  53. */
  54. METHODDEF(void)
  55. start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  56. {
  57. my_main_ptr mainp = (my_main_ptr) cinfo->main;
  58. /* Do nothing in raw-data mode. */
  59. if (cinfo->raw_data_in)
  60. return;
  61. mainp->cur_iMCU_row = 0; /* initialize counters */
  62. mainp->rowgroup_ctr = 0;
  63. mainp->suspended = FALSE;
  64. mainp->pass_mode = pass_mode; /* save mode for use by process_data */
  65. switch (pass_mode) {
  66. case JBUF_PASS_THRU:
  67. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  68. if (mainp->whole_image[0] != NULL)
  69. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  70. #endif
  71. mainp->pub.process_data = process_data_simple_main;
  72. break;
  73. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  74. case JBUF_SAVE_SOURCE:
  75. case JBUF_CRANK_DEST:
  76. case JBUF_SAVE_AND_PASS:
  77. if (mainp->whole_image[0] == NULL)
  78. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  79. mainp->pub.process_data = process_data_buffer_main;
  80. break;
  81. #endif
  82. default:
  83. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  84. break;
  85. }
  86. }
  87. /*
  88. * Process some data.
  89. * This routine handles the simple pass-through mode,
  90. * where we have only a strip buffer.
  91. */
  92. METHODDEF(void)
  93. process_data_simple_main (j_compress_ptr cinfo,
  94. JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  95. JDIMENSION in_rows_avail)
  96. {
  97. my_main_ptr mainp = (my_main_ptr) cinfo->main;
  98. while (mainp->cur_iMCU_row < cinfo->total_iMCU_rows) {
  99. /* Read input data if we haven't filled the main buffer yet */
  100. if (mainp->rowgroup_ctr < (JDIMENSION) cinfo->min_DCT_v_scaled_size)
  101. (*cinfo->prep->pre_process_data) (cinfo,
  102. input_buf, in_row_ctr, in_rows_avail,
  103. mainp->buffer, &mainp->rowgroup_ctr,
  104. (JDIMENSION) cinfo->min_DCT_v_scaled_size);
  105. /* If we don't have a full iMCU row buffered, return to application for
  106. * more data. Note that preprocessor will always pad to fill the iMCU row
  107. * at the bottom of the image.
  108. */
  109. if (mainp->rowgroup_ctr != (JDIMENSION) cinfo->min_DCT_v_scaled_size)
  110. return;
  111. /* Send the completed row to the compressor */
  112. if (! (*cinfo->coef->compress_data) (cinfo, mainp->buffer)) {
  113. /* If compressor did not consume the whole row, then we must need to
  114. * suspend processing and return to the application. In this situation
  115. * we pretend we didn't yet consume the last input row; otherwise, if
  116. * it happened to be the last row of the image, the application would
  117. * think we were done.
  118. */
  119. if (! mainp->suspended) {
  120. (*in_row_ctr)--;
  121. mainp->suspended = TRUE;
  122. }
  123. return;
  124. }
  125. /* We did finish the row. Undo our little suspension hack if a previous
  126. * call suspended; then mark the main buffer empty.
  127. */
  128. if (mainp->suspended) {
  129. (*in_row_ctr)++;
  130. mainp->suspended = FALSE;
  131. }
  132. mainp->rowgroup_ctr = 0;
  133. mainp->cur_iMCU_row++;
  134. }
  135. }
  136. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  137. /*
  138. * Process some data.
  139. * This routine handles all of the modes that use a full-size buffer.
  140. */
  141. METHODDEF(void)
  142. process_data_buffer_main (j_compress_ptr cinfo,
  143. JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  144. JDIMENSION in_rows_avail)
  145. {
  146. my_main_ptr mainp = (my_main_ptr) cinfo->main;
  147. int ci;
  148. jpeg_component_info *compptr;
  149. boolean writing = (mainp->pass_mode != JBUF_CRANK_DEST);
  150. while (mainp->cur_iMCU_row < cinfo->total_iMCU_rows) {
  151. /* Realign the virtual buffers if at the start of an iMCU row. */
  152. if (mainp->rowgroup_ctr == 0) {
  153. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  154. ci++, compptr++) {
  155. mainp->buffer[ci] = (*cinfo->mem->access_virt_sarray)
  156. ((j_common_ptr) cinfo, mainp->whole_image[ci], mainp->cur_iMCU_row *
  157. ((JDIMENSION) (compptr->v_samp_factor * cinfo->min_DCT_v_scaled_size)),
  158. (JDIMENSION) (compptr->v_samp_factor * cinfo->min_DCT_v_scaled_size),
  159. writing);
  160. }
  161. /* In a read pass, pretend we just read some source data. */
  162. if (! writing) {
  163. *in_row_ctr += (JDIMENSION)
  164. (cinfo->max_v_samp_factor * cinfo->min_DCT_v_scaled_size);
  165. mainp->rowgroup_ctr = (JDIMENSION) cinfo->min_DCT_v_scaled_size;
  166. }
  167. }
  168. /* If a write pass, read input data until the current iMCU row is full. */
  169. /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
  170. if (writing) {
  171. (*cinfo->prep->pre_process_data) (cinfo,
  172. input_buf, in_row_ctr, in_rows_avail,
  173. mainp->buffer, &mainp->rowgroup_ctr,
  174. (JDIMENSION) cinfo->min_DCT_v_scaled_size);
  175. /* Return to application if we need more data to fill the iMCU row. */
  176. if (mainp->rowgroup_ctr < (JDIMENSION) cinfo->min_DCT_v_scaled_size)
  177. return;
  178. }
  179. /* Emit data, unless this is a sink-only pass. */
  180. if (mainp->pass_mode != JBUF_SAVE_SOURCE) {
  181. if (! (*cinfo->coef->compress_data) (cinfo, mainp->buffer)) {
  182. /* If compressor did not consume the whole row, then we must need to
  183. * suspend processing and return to the application. In this situation
  184. * we pretend we didn't yet consume the last input row; otherwise, if
  185. * it happened to be the last row of the image, the application would
  186. * think we were done.
  187. */
  188. if (! mainp->suspended) {
  189. (*in_row_ctr)--;
  190. mainp->suspended = TRUE;
  191. }
  192. return;
  193. }
  194. /* We did finish the row. Undo our little suspension hack if a previous
  195. * call suspended; then mark the main buffer empty.
  196. */
  197. if (mainp->suspended) {
  198. (*in_row_ctr)++;
  199. mainp->suspended = FALSE;
  200. }
  201. }
  202. /* If get here, we are done with this iMCU row. Mark buffer empty. */
  203. mainp->rowgroup_ctr = 0;
  204. mainp->cur_iMCU_row++;
  205. }
  206. }
  207. #endif /* FULL_MAIN_BUFFER_SUPPORTED */
  208. /*
  209. * Initialize main buffer controller.
  210. */
  211. GLOBAL(void)
  212. jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  213. {
  214. my_main_ptr mainp;
  215. int ci;
  216. jpeg_component_info *compptr;
  217. mainp = (my_main_ptr)
  218. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  219. SIZEOF(my_main_controller));
  220. cinfo->main = &mainp->pub;
  221. mainp->pub.start_pass = start_pass_main;
  222. /* We don't need to create a buffer in raw-data mode. */
  223. if (cinfo->raw_data_in)
  224. return;
  225. /* Create the buffer. It holds downsampled data, so each component
  226. * may be of a different size.
  227. */
  228. if (need_full_buffer) {
  229. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  230. /* Allocate a full-image virtual array for each component */
  231. /* Note we pad the bottom to a multiple of the iMCU height */
  232. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  233. ci++, compptr++) {
  234. mainp->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
  235. ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  236. compptr->width_in_blocks * ((JDIMENSION) compptr->DCT_h_scaled_size),
  237. ((JDIMENSION) jround_up((long) compptr->height_in_blocks,
  238. (long) compptr->v_samp_factor)) *
  239. ((JDIMENSION) cinfo->min_DCT_v_scaled_size),
  240. (JDIMENSION) (compptr->v_samp_factor * compptr->DCT_v_scaled_size));
  241. }
  242. #else
  243. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  244. #endif
  245. } else {
  246. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  247. mainp->whole_image[0] = NULL; /* flag for no virtual arrays */
  248. #endif
  249. /* Allocate a strip buffer for each component */
  250. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  251. ci++, compptr++) {
  252. mainp->buffer[ci] = (*cinfo->mem->alloc_sarray)
  253. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  254. compptr->width_in_blocks * ((JDIMENSION) compptr->DCT_h_scaled_size),
  255. (JDIMENSION) (compptr->v_samp_factor * compptr->DCT_v_scaled_size));
  256. }
  257. }
  258. }