jcmainct.c 9.1 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. {
  56. // bk001204 - don't use main...
  57. my_main_ptr jmain = (my_main_ptr) cinfo->main;
  58. /* Do nothing in raw-data mode. */
  59. if (cinfo->raw_data_in)
  60. return;
  61. jmain->cur_iMCU_row = 0; /* initialize counters */
  62. jmain->rowgroup_ctr = 0;
  63. jmain->suspended = FALSE;
  64. jmain->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 (jmain->whole_image[0] != NULL)
  69. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  70. #endif
  71. jmain->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 (jmain->whole_image[0] == NULL)
  78. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  79. jmain->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. // bk001204 - don't use main
  98. my_main_ptr jmain = (my_main_ptr) cinfo->main;
  99. while (jmain->cur_iMCU_row < cinfo->total_iMCU_rows) {
  100. /* Read input data if we haven't filled the main buffer yet */
  101. if (jmain->rowgroup_ctr < DCTSIZE)
  102. (*cinfo->prep->pre_process_data) (cinfo,
  103. input_buf, in_row_ctr, in_rows_avail,
  104. jmain->buffer, &jmain->rowgroup_ctr,
  105. (JDIMENSION) DCTSIZE);
  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 (jmain->rowgroup_ctr != DCTSIZE)
  111. return;
  112. /* Send the completed row to the compressor */
  113. if (! (*cinfo->coef->compress_data) (cinfo, jmain->buffer)) {
  114. /* If compressor did not consume the whole row, then we must need to
  115. * suspend processing and return to the application. In this situation
  116. * we pretend we didn't yet consume the last input row; otherwise, if
  117. * it happened to be the last row of the image, the application would
  118. * think we were done.
  119. */
  120. if (! jmain->suspended) {
  121. (*in_row_ctr)--;
  122. jmain->suspended = TRUE;
  123. }
  124. return;
  125. }
  126. /* We did finish the row. Undo our little suspension hack if a previous
  127. * call suspended; then mark the main buffer empty.
  128. */
  129. if (jmain->suspended) {
  130. (*in_row_ctr)++;
  131. jmain->suspended = FALSE;
  132. }
  133. jmain->rowgroup_ctr = 0;
  134. jmain->cur_iMCU_row++;
  135. }
  136. }
  137. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  138. /*
  139. * Process some data.
  140. * This routine handles all of the modes that use a full-size buffer.
  141. */
  142. METHODDEF void
  143. process_data_buffer_main (j_compress_ptr cinfo,
  144. JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  145. JDIMENSION in_rows_avail)
  146. {
  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. /* Emit data, unless this is a sink-only pass. */
  179. if (main->pass_mode != JBUF_SAVE_SOURCE) {
  180. if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
  181. /* If compressor did not consume the whole row, then we must need to
  182. * suspend processing and return to the application. In this situation
  183. * we pretend we didn't yet consume the last input row; otherwise, if
  184. * it happened to be the last row of the image, the application would
  185. * think we were done.
  186. */
  187. if (! main->suspended) {
  188. (*in_row_ctr)--;
  189. main->suspended = TRUE;
  190. }
  191. return;
  192. }
  193. /* We did finish the row. Undo our little suspension hack if a previous
  194. * call suspended; then mark the main buffer empty.
  195. */
  196. if (main->suspended) {
  197. (*in_row_ctr)++;
  198. main->suspended = FALSE;
  199. }
  200. }
  201. /* If get here, we are done with this iMCU row. Mark buffer empty. */
  202. main->rowgroup_ctr = 0;
  203. main->cur_iMCU_row++;
  204. }
  205. }
  206. #endif /* FULL_MAIN_BUFFER_SUPPORTED */
  207. /*
  208. * Initialize main buffer controller.
  209. */
  210. GLOBAL void
  211. jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  212. {
  213. // bk001204 - don't use main
  214. my_main_ptr jmain;
  215. int ci;
  216. jpeg_component_info *compptr;
  217. jmain = (my_main_ptr)
  218. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  219. SIZEOF(my_main_controller));
  220. cinfo->main = (struct jpeg_c_main_controller *) jmain;
  221. jmain->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. jmain->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. jmain->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. jmain->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. }