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