4 * Copyright (C) 1994-1998, 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.
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.
13 #define JPEG_INTERNALS
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.
23 #undef FULL_MAIN_BUFFER_SUPPORTED
26 /* Private buffer controller object */
29 struct jpeg_c_main_controller pub; /* public fields */
31 JDIMENSION cur_iMCU_row; /* number of current iMCU row */
32 JDIMENSION rowgroup_ctr; /* counts row groups received in iMCU row */
33 boolean suspended; /* remember if we suspended output */
34 J_BUF_MODE pass_mode; /* current operating mode */
36 /* If using just a strip buffer, this points to the entire set of buffers
37 * (we allocate one for each component). In the full-image case, this
38 * points to the currently accessible strips of the virtual arrays.
40 JSAMPARRAY buffer[MAX_COMPONENTS];
42 #ifdef FULL_MAIN_BUFFER_SUPPORTED
43 /* If using full-image storage, this array holds pointers to virtual-array
44 * control blocks for each component. Unused if not full-image storage.
46 jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
50 typedef my_main_controller * my_main_ptr;
53 /* Forward declarations */
54 METHODDEF(void) process_data_simple_main
55 JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
56 JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
57 #ifdef FULL_MAIN_BUFFER_SUPPORTED
58 METHODDEF(void) process_data_buffer_main
59 JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
60 JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
65 * Initialize for a processing pass.
69 start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
71 my_main_ptr mainPtr = (my_main_ptr) cinfo->main;
73 /* Do nothing in raw-data mode. */
74 if (cinfo->raw_data_in)
77 mainPtr->cur_iMCU_row = 0; /* initialize counters */
78 mainPtr->rowgroup_ctr = 0;
79 mainPtr->suspended = FALSE;
80 mainPtr->pass_mode = pass_mode; /* save mode for use by process_data */
84 #ifdef FULL_MAIN_BUFFER_SUPPORTED
85 if (mainPtr->whole_image[0] != NULL)
86 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
88 mainPtr->pub.process_data = process_data_simple_main;
90 #ifdef FULL_MAIN_BUFFER_SUPPORTED
91 case JBUF_SAVE_SOURCE:
93 case JBUF_SAVE_AND_PASS:
94 if (mainPtr->whole_image[0] == NULL)
95 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
96 mainPtr->pub.process_data = process_data_buffer_main;
100 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
108 * This routine handles the simple pass-through mode,
109 * where we have only a strip buffer.
113 process_data_simple_main (j_compress_ptr cinfo,
114 JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
115 JDIMENSION in_rows_avail)
117 my_main_ptr mainPtr = (my_main_ptr) cinfo->main;
118 JDIMENSION data_unit = (JDIMENSION)(cinfo->data_unit);
120 while (mainPtr->cur_iMCU_row < cinfo->total_iMCU_rows) {
121 /* Read input data if we haven't filled the main buffer yet */
122 if (mainPtr->rowgroup_ctr < data_unit)
123 (*cinfo->prep->pre_process_data) (cinfo,
124 input_buf, in_row_ctr, in_rows_avail,
125 mainPtr->buffer, &mainPtr->rowgroup_ctr,
126 (JDIMENSION) data_unit);
128 /* If we don't have a full iMCU row buffered, return to application for
129 * more data. Note that preprocessor will always pad to fill the iMCU row
130 * at the bottom of the image.
132 if (mainPtr->rowgroup_ctr != data_unit)
135 /* Send the completed row to the compressor */
136 if (! (*cinfo->codec->compress_data) (cinfo, mainPtr->buffer)) {
137 /* If compressor did not consume the whole row, then we must need to
138 * suspend processing and return to the application. In this situation
139 * we pretend we didn't yet consume the last input row; otherwise, if
140 * it happened to be the last row of the image, the application would
141 * think we were done.
143 if (! mainPtr->suspended) {
145 mainPtr->suspended = TRUE;
149 /* We did finish the row. Undo our little suspension hack if a previous
150 * call suspended; then mark the main buffer empty.
152 if (mainPtr->suspended) {
154 mainPtr->suspended = FALSE;
156 mainPtr->rowgroup_ctr = 0;
157 mainPtr->cur_iMCU_row++;
162 #ifdef FULL_MAIN_BUFFER_SUPPORTED
166 * This routine handles all of the modes that use a full-size buffer.
170 process_data_buffer_main (j_compress_ptr cinfo,
171 JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
172 JDIMENSION in_rows_avail)
174 my_main_ptr main = (my_main_ptr) cinfo->main;
176 jpeg_component_info *compptr;
177 boolean writing = (mainPtr->pass_mode != JBUF_CRANK_DEST);
178 JDIMENSION data_unit = (JDIMENSION)(cinfo->data_unit);
180 while (mainPtr->cur_iMCU_row < cinfo->total_iMCU_rows) {
181 /* Realign the virtual buffers if at the start of an iMCU row. */
182 if (mainPtr->rowgroup_ctr == 0) {
183 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
185 mainPtr->buffer[ci] = (*cinfo->mem->access_virt_sarray)
186 ((j_common_ptr) cinfo, mainPtr->whole_image[ci],
187 mainPtr->cur_iMCU_row * (compptr->v_samp_factor * data_unit),
188 (JDIMENSION) (compptr->v_samp_factor * data_unit), writing);
190 /* In a read pass, pretend we just read some source data. */
192 *in_row_ctr += cinfo->max_v_samp_factor * data_unit;
193 mainPtr->rowgroup_ctr = data_unit;
197 /* If a write pass, read input data until the current iMCU row is full. */
198 /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
200 (*cinfo->prep->pre_process_data) (cinfo,
201 input_buf, in_row_ctr, in_rows_avail,
202 mainPtr->buffer, &mainPtr->rowgroup_ctr,
203 (JDIMENSION) data_unit);
204 /* Return to application if we need more data to fill the iMCU row. */
205 if (mainPtr->rowgroup_ctr < data_unit)
209 /* Emit data, unless this is a sink-only pass. */
210 if (mainPtr->pass_mode != JBUF_SAVE_SOURCE) {
211 if (! (*cinfo->codec->compress_data) (cinfo, mainPtr->buffer)) {
212 /* If compressor did not consume the whole row, then we must need to
213 * suspend processing and return to the application. In this situation
214 * we pretend we didn't yet consume the last input row; otherwise, if
215 * it happened to be the last row of the image, the application would
216 * think we were done.
218 if (! mainPtr->suspended) {
220 mainPtr->suspended = TRUE;
224 /* We did finish the row. Undo our little suspension hack if a previous
225 * call suspended; then mark the main buffer empty.
227 if (mainPtr->suspended) {
229 mainPtr->suspended = FALSE;
233 /* If get here, we are done with this iMCU row. Mark buffer empty. */
234 mainPtr->rowgroup_ctr = 0;
235 mainPtr->cur_iMCU_row++;
239 #endif /* FULL_MAIN_BUFFER_SUPPORTED */
243 * Initialize main buffer controller.
247 jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
251 jpeg_component_info *compptr;
252 int data_unit = cinfo->data_unit;
254 mainPtr = (my_main_ptr)
255 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
256 SIZEOF(my_main_controller));
257 cinfo->main = (struct jpeg_c_main_controller *) mainPtr;
258 mainPtr->pub.start_pass = start_pass_main;
260 /* We don't need to create a buffer in raw-data mode. */
261 if (cinfo->raw_data_in)
264 /* Create the buffer. It holds downsampled data, so each component
265 * may be of a different size.
267 if (need_full_buffer) {
268 #ifdef FULL_MAIN_BUFFER_SUPPORTED
269 /* Allocate a full-image virtual array for each component */
270 /* Note we pad the bottom to a multiple of the iMCU height */
271 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
273 mainPtr->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
274 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
275 compptr->width_in_data_units * data_unit,
276 (JDIMENSION) jround_up((long) compptr->height_in_data_units,
277 (long) compptr->v_samp_factor) * data_unit,
278 (JDIMENSION) (compptr->v_samp_factor * data_unit));
281 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
284 #ifdef FULL_MAIN_BUFFER_SUPPORTED
285 mainPtr->whole_image[0] = NULL; /* flag for no virtual arrays */
287 /* Allocate a strip buffer for each component */
288 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
290 mainPtr->buffer[ci] = (*cinfo->mem->alloc_sarray)
291 ((j_common_ptr) cinfo, JPOOL_IMAGE,
292 compptr->width_in_data_units * data_unit,
293 (JDIMENSION) (compptr->v_samp_factor * data_unit));