jcmainct.cpp 9.5 KB

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