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