Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
9.0 KiB

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. /*
  4. * jcmainct.c
  5. *
  6. * Copyright (C) 1994-1996, Thomas G. Lane.
  7. * This file is part of the Independent JPEG Group's software.
  8. * For conditions of distribution and use, see the accompanying README file.
  9. *
  10. * This file contains the main buffer controller for compression.
  11. * The main buffer lies between the pre-processor and the JPEG
  12. * compressor proper; it holds downsampled data in the JPEG colorspace.
  13. */
  14. #define JPEG_INTERNALS
  15. #include "jinclude.h"
  16. #include "jpeglib.h"
  17. /* Note: currently, there is no operating mode in which a full-image buffer
  18. * is needed at this step. If there were, that mode could not be used with
  19. * "raw data" input, since this module is bypassed in that case. However,
  20. * we've left the code here for possible use in special applications.
  21. */
  22. #undef FULL_MAIN_BUFFER_SUPPORTED
  23. /* Private buffer controller object */
  24. typedef struct {
  25. struct jpeg_c_main_controller pub; /* public fields */
  26. JDIMENSION cur_iMCU_row; /* number of current iMCU row */
  27. JDIMENSION rowgroup_ctr; /* counts row groups received in iMCU row */
  28. boolean suspended; /* remember if we suspended output */
  29. J_BUF_MODE pass_mode; /* current operating mode */
  30. /* If using just a strip buffer, this points to the entire set of buffers
  31. * (we allocate one for each component). In the full-image case, this
  32. * points to the currently accessible strips of the virtual arrays.
  33. */
  34. JSAMPARRAY buffer[MAX_COMPONENTS];
  35. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  36. /* If using full-image storage, this array holds pointers to virtual-array
  37. * control blocks for each component. Unused if not full-image storage.
  38. */
  39. jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
  40. #endif
  41. } my_main_controller;
  42. typedef my_main_controller * my_main_ptr;
  43. /* Forward declarations */
  44. METHODDEF(void) process_data_simple_main
  45. JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
  46. JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
  47. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  48. METHODDEF(void) process_data_buffer_main
  49. JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
  50. JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
  51. #endif
  52. /*
  53. * Initialize for a processing pass.
  54. */
  55. METHODDEF(void)
  56. start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  57. {
  58. my_main_ptr main = (my_main_ptr) cinfo->main;
  59. /* Do nothing in raw-data mode. */
  60. if (cinfo->raw_data_in)
  61. return;
  62. main->cur_iMCU_row = 0; /* initialize counters */
  63. main->rowgroup_ctr = 0;
  64. main->suspended = FALSE;
  65. main->pass_mode = pass_mode; /* save mode for use by process_data */
  66. switch (pass_mode) {
  67. case JBUF_PASS_THRU:
  68. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  69. if (main->whole_image[0] != NULL)
  70. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  71. #endif
  72. main->pub.process_data = process_data_simple_main;
  73. break;
  74. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  75. case JBUF_SAVE_SOURCE:
  76. case JBUF_CRANK_DEST:
  77. case JBUF_SAVE_AND_PASS:
  78. if (main->whole_image[0] == NULL)
  79. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  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. {
  98. my_main_ptr main = (my_main_ptr) cinfo->main;
  99. while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
  100. /* Read input data if we haven't filled the main buffer yet */
  101. if (main->rowgroup_ctr < DCTSIZE)
  102. (*cinfo->prep->pre_process_data) (cinfo,
  103. input_buf, in_row_ctr, in_rows_avail,
  104. main->buffer, &main->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 (main->rowgroup_ctr != DCTSIZE)
  111. return;
  112. /* Send the completed row to the compressor */
  113. if (! (*cinfo->coef->compress_data) (cinfo, main->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 (! main->suspended) {
  121. (*in_row_ctr)--;
  122. main->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 (main->suspended) {
  130. (*in_row_ctr)++;
  131. main->suspended = FALSE;
  132. }
  133. main->rowgroup_ctr = 0;
  134. main->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. 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. /* Create the buffer. It holds downsampled data, so each component
  225. * may be of a different size.
  226. */
  227. if (need_full_buffer) {
  228. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  229. /* Allocate a full-image virtual array for each component */
  230. /* Note we pad the bottom to a multiple of the iMCU height */
  231. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  232. ci++, compptr++) {
  233. main->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
  234. ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  235. compptr->width_in_blocks * DCTSIZE,
  236. (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  237. (long) compptr->v_samp_factor) * DCTSIZE,
  238. (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
  239. }
  240. #else
  241. ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  242. #endif
  243. } else {
  244. #ifdef FULL_MAIN_BUFFER_SUPPORTED
  245. main->whole_image[0] = NULL; /* flag for no virtual arrays */
  246. #endif
  247. /* Allocate a strip buffer for each component */
  248. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  249. ci++, compptr++) {
  250. main->buffer[ci] = (*cinfo->mem->alloc_sarray)
  251. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  252. compptr->width_in_blocks * DCTSIZE,
  253. (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
  254. }
  255. }
  256. }