4 * Copyright (C) 1994-1996, Thomas G. Lane.
5 * Modified 2003-2012 by Guido Vollbeding.
6 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
9 * This file contains the main buffer controller for compression.
10 * The main buffer lies between the pre-processor and the JPEG
11 * compressor proper; it holds downsampled data in the JPEG colorspace.
14 #define JPEG_INTERNALS
19 /* Note: currently, there is no operating mode in which a full-image buffer
20 * is needed at this step. If there were, that mode could not be used with
21 * "raw data" input, since this module is bypassed in that case. However,
22 * we've left the code here for possible use in special applications.
24 #undef FULL_MAIN_BUFFER_SUPPORTED
27 /* Private buffer controller object */
30 struct jpeg_c_main_controller pub
; /* public fields */
32 JDIMENSION cur_iMCU_row
; /* number of current iMCU row */
33 JDIMENSION rowgroup_ctr
; /* counts row groups received in iMCU row */
34 boolean suspended
; /* remember if we suspended output */
35 J_BUF_MODE pass_mode
; /* current operating mode */
37 /* If using just a strip buffer, this points to the entire set of buffers
38 * (we allocate one for each component). In the full-image case, this
39 * points to the currently accessible strips of the virtual arrays.
41 JSAMPARRAY buffer
[MAX_COMPONENTS
];
43 #ifdef FULL_MAIN_BUFFER_SUPPORTED
44 /* If using full-image storage, this array holds pointers to virtual-array
45 * control blocks for each component. Unused if not full-image storage.
47 jvirt_sarray_ptr whole_image
[MAX_COMPONENTS
];
51 typedef my_main_controller
* my_main_ptr
;
54 /* Forward declarations */
55 METHODDEF(void) process_data_simple_main
56 JPP((j_compress_ptr cinfo
, JSAMPARRAY input_buf
,
57 JDIMENSION
*in_row_ctr
, JDIMENSION in_rows_avail
));
58 #ifdef FULL_MAIN_BUFFER_SUPPORTED
59 METHODDEF(void) process_data_buffer_main
60 JPP((j_compress_ptr cinfo
, JSAMPARRAY input_buf
,
61 JDIMENSION
*in_row_ctr
, JDIMENSION in_rows_avail
));
66 * Initialize for a processing pass.
70 start_pass_main (j_compress_ptr cinfo
, J_BUF_MODE pass_mode
)
72 my_main_ptr mainp
= (my_main_ptr
) cinfo
->main
;
74 /* Do nothing in raw-data mode. */
75 if (cinfo
->raw_data_in
)
78 mainp
->cur_iMCU_row
= 0; /* initialize counters */
79 mainp
->rowgroup_ctr
= 0;
80 mainp
->suspended
= FALSE
;
81 mainp
->pass_mode
= pass_mode
; /* save mode for use by process_data */
85 #ifdef FULL_MAIN_BUFFER_SUPPORTED
86 if (mainp
->whole_image
[0] != NULL
)
87 ERREXIT(cinfo
, JERR_BAD_BUFFER_MODE
);
89 mainp
->pub
.process_data
= process_data_simple_main
;
91 #ifdef FULL_MAIN_BUFFER_SUPPORTED
92 case JBUF_SAVE_SOURCE
:
94 case JBUF_SAVE_AND_PASS
:
95 if (mainp
->whole_image
[0] == NULL
)
96 ERREXIT(cinfo
, JERR_BAD_BUFFER_MODE
);
97 mainp
->pub
.process_data
= process_data_buffer_main
;
101 ERREXIT(cinfo
, JERR_BAD_BUFFER_MODE
);
109 * This routine handles the simple pass-through mode,
110 * where we have only a strip buffer.
114 process_data_simple_main (j_compress_ptr cinfo
,
115 JSAMPARRAY input_buf
, JDIMENSION
*in_row_ctr
,
116 JDIMENSION in_rows_avail
)
118 my_main_ptr mainp
= (my_main_ptr
) cinfo
->main
;
120 while (mainp
->cur_iMCU_row
< cinfo
->total_iMCU_rows
) {
121 /* Read input data if we haven't filled the main buffer yet */
122 if (mainp
->rowgroup_ctr
< (JDIMENSION
) cinfo
->min_DCT_v_scaled_size
)
123 (*cinfo
->prep
->pre_process_data
) (cinfo
,
124 input_buf
, in_row_ctr
, in_rows_avail
,
125 mainp
->buffer
, &mainp
->rowgroup_ctr
,
126 (JDIMENSION
) cinfo
->min_DCT_v_scaled_size
);
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 (mainp
->rowgroup_ctr
!= (JDIMENSION
) cinfo
->min_DCT_v_scaled_size
)
135 /* Send the completed row to the compressor */
136 if (! (*cinfo
->coef
->compress_data
) (cinfo
, mainp
->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 (! mainp
->suspended
) {
145 mainp
->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 (mainp
->suspended
) {
154 mainp
->suspended
= FALSE
;
156 mainp
->rowgroup_ctr
= 0;
157 mainp
->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 mainp
= (my_main_ptr
) cinfo
->main
;
176 jpeg_component_info
*compptr
;
177 boolean writing
= (mainp
->pass_mode
!= JBUF_CRANK_DEST
);
179 while (mainp
->cur_iMCU_row
< cinfo
->total_iMCU_rows
) {
180 /* Realign the virtual buffers if at the start of an iMCU row. */
181 if (mainp
->rowgroup_ctr
== 0) {
182 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
184 mainp
->buffer
[ci
] = (*cinfo
->mem
->access_virt_sarray
)
185 ((j_common_ptr
) cinfo
, mainp
->whole_image
[ci
], mainp
->cur_iMCU_row
*
186 ((JDIMENSION
) (compptr
->v_samp_factor
* cinfo
->min_DCT_v_scaled_size
)),
187 (JDIMENSION
) (compptr
->v_samp_factor
* cinfo
->min_DCT_v_scaled_size
),
190 /* In a read pass, pretend we just read some source data. */
192 *in_row_ctr
+= (JDIMENSION
)
193 (cinfo
->max_v_samp_factor
* cinfo
->min_DCT_v_scaled_size
);
194 mainp
->rowgroup_ctr
= (JDIMENSION
) cinfo
->min_DCT_v_scaled_size
;
198 /* If a write pass, read input data until the current iMCU row is full. */
199 /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
201 (*cinfo
->prep
->pre_process_data
) (cinfo
,
202 input_buf
, in_row_ctr
, in_rows_avail
,
203 mainp
->buffer
, &mainp
->rowgroup_ctr
,
204 (JDIMENSION
) cinfo
->min_DCT_v_scaled_size
);
205 /* Return to application if we need more data to fill the iMCU row. */
206 if (mainp
->rowgroup_ctr
< (JDIMENSION
) cinfo
->min_DCT_v_scaled_size
)
210 /* Emit data, unless this is a sink-only pass. */
211 if (mainp
->pass_mode
!= JBUF_SAVE_SOURCE
) {
212 if (! (*cinfo
->coef
->compress_data
) (cinfo
, mainp
->buffer
)) {
213 /* If compressor did not consume the whole row, then we must need to
214 * suspend processing and return to the application. In this situation
215 * we pretend we didn't yet consume the last input row; otherwise, if
216 * it happened to be the last row of the image, the application would
217 * think we were done.
219 if (! mainp
->suspended
) {
221 mainp
->suspended
= TRUE
;
225 /* We did finish the row. Undo our little suspension hack if a previous
226 * call suspended; then mark the main buffer empty.
228 if (mainp
->suspended
) {
230 mainp
->suspended
= FALSE
;
234 /* If get here, we are done with this iMCU row. Mark buffer empty. */
235 mainp
->rowgroup_ctr
= 0;
236 mainp
->cur_iMCU_row
++;
240 #endif /* FULL_MAIN_BUFFER_SUPPORTED */
244 * Initialize main buffer controller.
248 jinit_c_main_controller (j_compress_ptr cinfo
, boolean need_full_buffer
)
252 jpeg_component_info
*compptr
;
254 mainp
= (my_main_ptr
)
255 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
256 SIZEOF(my_main_controller
));
257 cinfo
->main
= &mainp
->pub
;
258 mainp
->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 mainp
->whole_image
[ci
] = (*cinfo
->mem
->request_virt_sarray
)
274 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, FALSE
,
275 compptr
->width_in_blocks
* ((JDIMENSION
) compptr
->DCT_h_scaled_size
),
276 ((JDIMENSION
) jround_up((long) compptr
->height_in_blocks
,
277 (long) compptr
->v_samp_factor
)) *
278 ((JDIMENSION
) cinfo
->min_DCT_v_scaled_size
),
279 (JDIMENSION
) (compptr
->v_samp_factor
* compptr
->DCT_v_scaled_size
));
282 ERREXIT(cinfo
, JERR_BAD_BUFFER_MODE
);
285 #ifdef FULL_MAIN_BUFFER_SUPPORTED
286 mainp
->whole_image
[0] = NULL
; /* flag for no virtual arrays */
288 /* Allocate a strip buffer for each component */
289 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
291 mainp
->buffer
[ci
] = (*cinfo
->mem
->alloc_sarray
)
292 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
293 compptr
->width_in_blocks
* ((JDIMENSION
) compptr
->DCT_h_scaled_size
),
294 (JDIMENSION
) (compptr
->v_samp_factor
* compptr
->DCT_v_scaled_size
));