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