jcmainct.cpp 11 KB

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