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