blob: c7315c99b1311ddc6e2a12e3a33fdb731d795de6 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jcmainct.c
3 *
Thomas G. Lanea8b67c41995-03-15 00:00:00 +00004 * Copyright (C) 1994-1995, Thomas G. Lane.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00005 * 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
13#define JPEG_INTERNALS
14#include "jinclude.h"
15#include "jpeglib.h"
16
17
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
25
26/* Private buffer controller object */
27
28typedef struct {
29 struct jpeg_c_main_controller pub; /* public fields */
30
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000031 JDIMENSION cur_iMCU_row; /* number of current iMCU row */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000032 JDIMENSION rowgroup_ctr; /* counts row groups received in iMCU row */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000033 boolean suspended; /* remember if we suspended output */
34 J_BUF_MODE pass_mode; /* current operating mode */
35
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.
39 */
40 JSAMPARRAY buffer[MAX_COMPONENTS];
41
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.
45 */
46 jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
47#endif
48} my_main_controller;
49
50typedef my_main_controller * my_main_ptr;
51
52
53/* Forward declarations */
54METHODDEF 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
58METHODDEF void process_data_buffer_main
59 JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
60 JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
61#endif
62
63
64/*
65 * Initialize for a processing pass.
66 */
67
68METHODDEF void
69start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
70{
71 my_main_ptr main = (my_main_ptr) cinfo->main;
72
73 /* Do nothing in raw-data mode. */
74 if (cinfo->raw_data_in)
75 return;
76
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000077 main->cur_iMCU_row = 0; /* initialize counters */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000078 main->rowgroup_ctr = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000079 main->suspended = FALSE;
80 main->pass_mode = pass_mode; /* save mode for use by process_data */
81
82 switch (pass_mode) {
83 case JBUF_PASS_THRU:
84#ifdef FULL_MAIN_BUFFER_SUPPORTED
85 if (main->whole_image[0] != NULL)
86 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
87#endif
88 main->pub.process_data = process_data_simple_main;
89 break;
90#ifdef FULL_MAIN_BUFFER_SUPPORTED
91 case JBUF_SAVE_SOURCE:
92 case JBUF_CRANK_DEST:
93 case JBUF_SAVE_AND_PASS:
94 if (main->whole_image[0] == NULL)
95 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
96 main->pub.process_data = process_data_buffer_main;
97 break;
98#endif
99 default:
100 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
101 break;
102 }
103}
104
105
106/*
107 * Process some data.
108 * This routine handles the simple pass-through mode,
109 * where we have only a strip buffer.
110 */
111
112METHODDEF void
113process_data_simple_main (j_compress_ptr cinfo,
114 JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
115 JDIMENSION in_rows_avail)
116{
117 my_main_ptr main = (my_main_ptr) cinfo->main;
118
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000119 while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000120 /* Read input data if we haven't filled the main buffer yet */
121 if (main->rowgroup_ctr < DCTSIZE)
122 (*cinfo->prep->pre_process_data) (cinfo,
123 input_buf, in_row_ctr, in_rows_avail,
124 main->buffer, &main->rowgroup_ctr,
125 (JDIMENSION) DCTSIZE);
126
127 /* If we don't have a full iMCU row buffered, return to application for
128 * more data. Note that preprocessor will always pad to fill the iMCU row
129 * at the bottom of the image.
130 */
131 if (main->rowgroup_ctr != DCTSIZE)
132 return;
133
134 /* Send the completed row to the compressor */
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000135 if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
136 /* If compressor did not consume the whole row, then we must need to
137 * suspend processing and return to the application. In this situation
138 * we pretend we didn't yet consume the last input row; otherwise, if
139 * it happened to be the last row of the image, the application would
140 * think we were done.
141 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000142 if (! main->suspended) {
143 (*in_row_ctr)--;
144 main->suspended = TRUE;
145 }
146 return;
147 }
148 /* We did finish the row. Undo our little suspension hack if a previous
149 * call suspended; then mark the main buffer empty.
150 */
151 if (main->suspended) {
152 (*in_row_ctr)++;
153 main->suspended = FALSE;
154 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000155 main->rowgroup_ctr = 0;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000156 main->cur_iMCU_row++;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000157 }
158}
159
160
161#ifdef FULL_MAIN_BUFFER_SUPPORTED
162
163/*
164 * Process some data.
165 * This routine handles all of the modes that use a full-size buffer.
166 */
167
168METHODDEF void
169process_data_buffer_main (j_compress_ptr cinfo,
170 JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
171 JDIMENSION in_rows_avail)
172{
173 my_main_ptr main = (my_main_ptr) cinfo->main;
174 int ci;
175 jpeg_component_info *compptr;
176 boolean writing = (main->pass_mode != JBUF_CRANK_DEST);
177
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000178 while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000179 /* Realign the virtual buffers if at the start of an iMCU row. */
180 if (main->rowgroup_ctr == 0) {
181 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
182 ci++, compptr++) {
183 main->buffer[ci] = (*cinfo->mem->access_virt_sarray)
184 ((j_common_ptr) cinfo, main->whole_image[ci],
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000185 main->cur_iMCU_row * (compptr->v_samp_factor * DCTSIZE), writing);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000186 }
187 /* In a read pass, pretend we just read some source data. */
188 if (! writing) {
189 *in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE;
190 main->rowgroup_ctr = DCTSIZE;
191 }
192 }
193
194 /* If a write pass, read input data until the current iMCU row is full. */
195 /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
196 if (writing) {
197 (*cinfo->prep->pre_process_data) (cinfo,
198 input_buf, in_row_ctr, in_rows_avail,
199 main->buffer, &main->rowgroup_ctr,
200 (JDIMENSION) DCTSIZE);
201 /* Return to application if we need more data to fill the iMCU row. */
202 if (main->rowgroup_ctr < DCTSIZE)
203 return;
204 }
205
206 /* Emit data, unless this is a sink-only pass. */
207 if (main->pass_mode != JBUF_SAVE_SOURCE) {
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000208 if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
209 /* If compressor did not consume the whole row, then we must need to
210 * suspend processing and return to the application. In this situation
211 * we pretend we didn't yet consume the last input row; otherwise, if
212 * it happened to be the last row of the image, the application would
213 * think we were done.
214 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000215 if (! main->suspended) {
216 (*in_row_ctr)--;
217 main->suspended = TRUE;
218 }
219 return;
220 }
221 /* We did finish the row. Undo our little suspension hack if a previous
222 * call suspended; then mark the main buffer empty.
223 */
224 if (main->suspended) {
225 (*in_row_ctr)++;
226 main->suspended = FALSE;
227 }
228 }
229
230 /* If get here, we are done with this iMCU row. Mark buffer empty. */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000231 main->rowgroup_ctr = 0;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000232 main->cur_iMCU_row++;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000233 }
234}
235
236#endif /* FULL_MAIN_BUFFER_SUPPORTED */
237
238
239/*
240 * Initialize main buffer controller.
241 */
242
243GLOBAL void
244jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
245{
246 my_main_ptr main;
247 int ci;
248 jpeg_component_info *compptr;
249
250 main = (my_main_ptr)
251 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
252 SIZEOF(my_main_controller));
253 cinfo->main = (struct jpeg_c_main_controller *) main;
254 main->pub.start_pass = start_pass_main;
255
256 /* We don't need to create a buffer in raw-data mode. */
257 if (cinfo->raw_data_in)
258 return;
259
260 /* Create the buffer. It holds downsampled data, so each component
261 * may be of a different size.
262 */
263 if (need_full_buffer) {
264#ifdef FULL_MAIN_BUFFER_SUPPORTED
265 /* Allocate a full-image virtual array for each component */
266 /* Note we implicitly pad the bottom to a multiple of the iMCU height */
267 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
268 ci++, compptr++) {
269 main->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
270 ((j_common_ptr) cinfo, JPOOL_IMAGE,
271 compptr->width_in_blocks * DCTSIZE,
272 compptr->height_in_blocks * DCTSIZE,
273 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
274 }
275#else
276 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
277#endif
278 } else {
279#ifdef FULL_MAIN_BUFFER_SUPPORTED
280 main->whole_image[0] = NULL; /* flag for no virtual arrays */
281#endif
282 /* Allocate a strip buffer for each component */
283 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
284 ci++, compptr++) {
285 main->buffer[ci] = (*cinfo->mem->alloc_sarray)
286 ((j_common_ptr) cinfo, JPOOL_IMAGE,
287 compptr->width_in_blocks * DCTSIZE,
288 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
289 }
290 }
291}