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