blob: 612eb48ca2b952ce5f7e9406a7c10379354720fa [file] [log] [blame]
Parker Schuhebf887e2016-01-10 18:04:04 -08001/*
2 * jcmainct.c
3 *
4 * Copyright (C) 1994-1996, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
7 *
8 * This file contains the main buffer controller for compression.
9 * The main buffer lies between the pre-processor and the JPEG
10 * compressor proper; it holds downsampled data in the JPEG colorspace.
11 */
12
13#define JPEG_INTERNALS
14#include "jinclude.h"
15#include "jpeglib.h"
16
17
18/* Note: currently, there is no operating mode in which a full-image buffer
19 * is needed at this step. If there were, that mode could not be used with
20 * "raw data" input, since this module is bypassed in that case. However,
21 * we've left the code here for possible use in special applications.
22 */
23#undef FULL_MAIN_BUFFER_SUPPORTED
24
25
26/* Private buffer controller object */
27
28typedef struct {
29 struct jpeg_c_main_controller pub; /* public fields */
30
31 JDIMENSION cur_iMCU_row; /* number of current iMCU row */
32 JDIMENSION rowgroup_ctr; /* counts row groups received in iMCU row */
33 boolean suspended; /* remember if we suspended output */
34 J_BUF_MODE pass_mode; /* current operating mode */
35
36 /* If using just a strip buffer, this points to the entire set of buffers
37 * (we allocate one for each component). In the full-image case, this
38 * points to the currently accessible strips of the virtual arrays.
39 */
40 JSAMPARRAY buffer[MAX_COMPONENTS];
41
42#ifdef FULL_MAIN_BUFFER_SUPPORTED
43 /* If using full-image storage, this array holds pointers to virtual-array
44 * control blocks for each component. Unused if not full-image storage.
45 */
46 jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
47#endif
48} my_main_controller;
49
50typedef my_main_controller * my_main_ptr;
51
52
53/* Forward declarations */
54METHODDEF(void) process_data_simple_main
55 JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
56 JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
57#ifdef FULL_MAIN_BUFFER_SUPPORTED
58METHODDEF(void) process_data_buffer_main
59 JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
60 JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
61#endif
62
63
64/*
65 * Initialize for a processing pass.
66 */
67
68METHODDEF(void)
69start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
70{
Brian Silverman2c0b1702016-03-13 14:23:23 -040071 my_main_ptr jmain = (my_main_ptr) cinfo->main;
Parker Schuhebf887e2016-01-10 18:04:04 -080072
73 /* Do nothing in raw-data mode. */
74 if (cinfo->raw_data_in)
75 return;
76
Brian Silverman2c0b1702016-03-13 14:23:23 -040077 jmain->cur_iMCU_row = 0; /* initialize counters */
78 jmain->rowgroup_ctr = 0;
79 jmain->suspended = FALSE;
80 jmain->pass_mode = pass_mode; /* save mode for use by process_data */
Parker Schuhebf887e2016-01-10 18:04:04 -080081
82 switch (pass_mode) {
83 case JBUF_PASS_THRU:
84#ifdef FULL_MAIN_BUFFER_SUPPORTED
Brian Silverman2c0b1702016-03-13 14:23:23 -040085 if (jmain->whole_image[0] != NULL)
Parker Schuhebf887e2016-01-10 18:04:04 -080086 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
87#endif
Brian Silverman2c0b1702016-03-13 14:23:23 -040088 jmain->pub.process_data = process_data_simple_main;
Parker Schuhebf887e2016-01-10 18:04:04 -080089 break;
90#ifdef FULL_MAIN_BUFFER_SUPPORTED
91 case JBUF_SAVE_SOURCE:
92 case JBUF_CRANK_DEST:
93 case JBUF_SAVE_AND_PASS:
Brian Silverman2c0b1702016-03-13 14:23:23 -040094 if (jmain->whole_image[0] == NULL)
Parker Schuhebf887e2016-01-10 18:04:04 -080095 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
Brian Silverman2c0b1702016-03-13 14:23:23 -040096 jmain->pub.process_data = process_data_buffer_main;
Parker Schuhebf887e2016-01-10 18:04:04 -080097 break;
98#endif
99 default:
100 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
101 break;
102 }
103}
104
105
106/*
107 * Process some data.
108 * This routine handles the simple pass-through mode,
109 * where we have only a strip buffer.
110 */
111
112METHODDEF(void)
113process_data_simple_main (j_compress_ptr cinfo,
114 JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
115 JDIMENSION in_rows_avail)
116{
Brian Silverman2c0b1702016-03-13 14:23:23 -0400117 my_main_ptr jmain = (my_main_ptr) cinfo->main;
Parker Schuhebf887e2016-01-10 18:04:04 -0800118
Brian Silverman2c0b1702016-03-13 14:23:23 -0400119 while (jmain->cur_iMCU_row < cinfo->total_iMCU_rows) {
120 /* Read input data if we haven't filled the jmain buffer yet */
121 if (jmain->rowgroup_ctr < (JDIMENSION) cinfo->min_DCT_v_scaled_size)
Parker Schuhebf887e2016-01-10 18:04:04 -0800122 (*cinfo->prep->pre_process_data) (cinfo,
123 input_buf, in_row_ctr, in_rows_avail,
Brian Silverman2c0b1702016-03-13 14:23:23 -0400124 jmain->buffer, &jmain->rowgroup_ctr,
Parker Schuhebf887e2016-01-10 18:04:04 -0800125 (JDIMENSION) cinfo->min_DCT_v_scaled_size);
126
127 /* If we don't have a full iMCU row buffered, return to application for
128 * more data. Note that preprocessor will always pad to fill the iMCU row
129 * at the bottom of the image.
130 */
Brian Silverman2c0b1702016-03-13 14:23:23 -0400131 if (jmain->rowgroup_ctr != (JDIMENSION) cinfo->min_DCT_v_scaled_size)
Parker Schuhebf887e2016-01-10 18:04:04 -0800132 return;
133
134 /* Send the completed row to the compressor */
Brian Silverman2c0b1702016-03-13 14:23:23 -0400135 if (! (*cinfo->coef->compress_data) (cinfo, jmain->buffer)) {
Parker Schuhebf887e2016-01-10 18:04:04 -0800136 /* If compressor did not consume the whole row, then we must need to
137 * suspend processing and return to the application. In this situation
138 * we pretend we didn't yet consume the last input row; otherwise, if
139 * it happened to be the last row of the image, the application would
140 * think we were done.
141 */
Brian Silverman2c0b1702016-03-13 14:23:23 -0400142 if (! jmain->suspended) {
Parker Schuhebf887e2016-01-10 18:04:04 -0800143 (*in_row_ctr)--;
Brian Silverman2c0b1702016-03-13 14:23:23 -0400144 jmain->suspended = TRUE;
Parker Schuhebf887e2016-01-10 18:04:04 -0800145 }
146 return;
147 }
148 /* We did finish the row. Undo our little suspension hack if a previous
Brian Silverman2c0b1702016-03-13 14:23:23 -0400149 * call suspended; then mark the jmain buffer empty.
Parker Schuhebf887e2016-01-10 18:04:04 -0800150 */
Brian Silverman2c0b1702016-03-13 14:23:23 -0400151 if (jmain->suspended) {
Parker Schuhebf887e2016-01-10 18:04:04 -0800152 (*in_row_ctr)++;
Brian Silverman2c0b1702016-03-13 14:23:23 -0400153 jmain->suspended = FALSE;
Parker Schuhebf887e2016-01-10 18:04:04 -0800154 }
Brian Silverman2c0b1702016-03-13 14:23:23 -0400155 jmain->rowgroup_ctr = 0;
156 jmain->cur_iMCU_row++;
Parker Schuhebf887e2016-01-10 18:04:04 -0800157 }
158}
159
160
161#ifdef FULL_MAIN_BUFFER_SUPPORTED
162
163/*
164 * Process some data.
165 * This routine handles all of the modes that use a full-size buffer.
166 */
167
168METHODDEF(void)
169process_data_buffer_main (j_compress_ptr cinfo,
170 JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
171 JDIMENSION in_rows_avail)
172{
Brian Silverman2c0b1702016-03-13 14:23:23 -0400173 my_main_ptr jmain = (my_main_ptr) cinfo->main;
Parker Schuhebf887e2016-01-10 18:04:04 -0800174 int ci;
175 jpeg_component_info *compptr;
Brian Silverman2c0b1702016-03-13 14:23:23 -0400176 boolean writing = (jmain->pass_mode != JBUF_CRANK_DEST);
Parker Schuhebf887e2016-01-10 18:04:04 -0800177
Brian Silverman2c0b1702016-03-13 14:23:23 -0400178 while (jmain->cur_iMCU_row < cinfo->total_iMCU_rows) {
Parker Schuhebf887e2016-01-10 18:04:04 -0800179 /* Realign the virtual buffers if at the start of an iMCU row. */
Brian Silverman2c0b1702016-03-13 14:23:23 -0400180 if (jmain->rowgroup_ctr == 0) {
Parker Schuhebf887e2016-01-10 18:04:04 -0800181 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
182 ci++, compptr++) {
Brian Silverman2c0b1702016-03-13 14:23:23 -0400183 jmain->buffer[ci] = (*cinfo->mem->access_virt_sarray)
184 ((j_common_ptr) cinfo, jmain->whole_image[ci],
185 jmain->cur_iMCU_row * (compptr->v_samp_factor * DCTSIZE),
Parker Schuhebf887e2016-01-10 18:04:04 -0800186 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE), writing);
187 }
188 /* In a read pass, pretend we just read some source data. */
189 if (! writing) {
190 *in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE;
Brian Silverman2c0b1702016-03-13 14:23:23 -0400191 jmain->rowgroup_ctr = DCTSIZE;
Parker Schuhebf887e2016-01-10 18:04:04 -0800192 }
193 }
194
195 /* If a write pass, read input data until the current iMCU row is full. */
196 /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
197 if (writing) {
198 (*cinfo->prep->pre_process_data) (cinfo,
199 input_buf, in_row_ctr, in_rows_avail,
Brian Silverman2c0b1702016-03-13 14:23:23 -0400200 jmain->buffer, &jmain->rowgroup_ctr,
Parker Schuhebf887e2016-01-10 18:04:04 -0800201 (JDIMENSION) DCTSIZE);
202 /* Return to application if we need more data to fill the iMCU row. */
Brian Silverman2c0b1702016-03-13 14:23:23 -0400203 if (jmain->rowgroup_ctr < DCTSIZE)
Parker Schuhebf887e2016-01-10 18:04:04 -0800204 return;
205 }
206
207 /* Emit data, unless this is a sink-only pass. */
Brian Silverman2c0b1702016-03-13 14:23:23 -0400208 if (jmain->pass_mode != JBUF_SAVE_SOURCE) {
209 if (! (*cinfo->coef->compress_data) (cinfo, jmain->buffer)) {
Parker Schuhebf887e2016-01-10 18:04:04 -0800210 /* If compressor did not consume the whole row, then we must need to
211 * suspend processing and return to the application. In this situation
212 * we pretend we didn't yet consume the last input row; otherwise, if
213 * it happened to be the last row of the image, the application would
214 * think we were done.
215 */
Brian Silverman2c0b1702016-03-13 14:23:23 -0400216 if (! jmain->suspended) {
Parker Schuhebf887e2016-01-10 18:04:04 -0800217 (*in_row_ctr)--;
Brian Silverman2c0b1702016-03-13 14:23:23 -0400218 jmain->suspended = TRUE;
Parker Schuhebf887e2016-01-10 18:04:04 -0800219 }
220 return;
221 }
222 /* We did finish the row. Undo our little suspension hack if a previous
Brian Silverman2c0b1702016-03-13 14:23:23 -0400223 * call suspended; then mark the jmain buffer empty.
Parker Schuhebf887e2016-01-10 18:04:04 -0800224 */
Brian Silverman2c0b1702016-03-13 14:23:23 -0400225 if (jmain->suspended) {
Parker Schuhebf887e2016-01-10 18:04:04 -0800226 (*in_row_ctr)++;
Brian Silverman2c0b1702016-03-13 14:23:23 -0400227 jmain->suspended = FALSE;
Parker Schuhebf887e2016-01-10 18:04:04 -0800228 }
229 }
230
231 /* If get here, we are done with this iMCU row. Mark buffer empty. */
Brian Silverman2c0b1702016-03-13 14:23:23 -0400232 jmain->rowgroup_ctr = 0;
233 jmain->cur_iMCU_row++;
Parker Schuhebf887e2016-01-10 18:04:04 -0800234 }
235}
236
237#endif /* FULL_MAIN_BUFFER_SUPPORTED */
238
239
240/*
Brian Silverman2c0b1702016-03-13 14:23:23 -0400241 * Initialize jmain buffer controller.
Parker Schuhebf887e2016-01-10 18:04:04 -0800242 */
243
244GLOBAL(void)
245jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
246{
Brian Silverman2c0b1702016-03-13 14:23:23 -0400247 my_main_ptr jmain;
Parker Schuhebf887e2016-01-10 18:04:04 -0800248 int ci;
249 jpeg_component_info *compptr;
250
Brian Silverman2c0b1702016-03-13 14:23:23 -0400251 jmain = (my_main_ptr)
Parker Schuhebf887e2016-01-10 18:04:04 -0800252 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
253 SIZEOF(my_main_controller));
Brian Silverman2c0b1702016-03-13 14:23:23 -0400254 cinfo->main = (struct jpeg_c_main_controller *) jmain;
255 jmain->pub.start_pass = start_pass_main;
Parker Schuhebf887e2016-01-10 18:04:04 -0800256
257 /* We don't need to create a buffer in raw-data mode. */
258 if (cinfo->raw_data_in)
259 return;
260
261 /* Create the buffer. It holds downsampled data, so each component
262 * may be of a different size.
263 */
264 if (need_full_buffer) {
265#ifdef FULL_MAIN_BUFFER_SUPPORTED
266 /* Allocate a full-image virtual array for each component */
267 /* Note we pad the bottom to a multiple of the iMCU height */
268 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
269 ci++, compptr++) {
Brian Silverman2c0b1702016-03-13 14:23:23 -0400270 jmain->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
Parker Schuhebf887e2016-01-10 18:04:04 -0800271 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
272 compptr->width_in_blocks * compptr->DCT_h_scaled_size,
273 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
274 (long) compptr->v_samp_factor) * DCTSIZE,
275 (JDIMENSION) (compptr->v_samp_factor * compptr->DCT_v_scaled_size));
276 }
277#else
278 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
279#endif
280 } else {
281#ifdef FULL_MAIN_BUFFER_SUPPORTED
Brian Silverman2c0b1702016-03-13 14:23:23 -0400282 jmain->whole_image[0] = NULL; /* flag for no virtual arrays */
Parker Schuhebf887e2016-01-10 18:04:04 -0800283#endif
284 /* Allocate a strip buffer for each component */
285 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
286 ci++, compptr++) {
Brian Silverman2c0b1702016-03-13 14:23:23 -0400287 jmain->buffer[ci] = (*cinfo->mem->alloc_sarray)
Parker Schuhebf887e2016-01-10 18:04:04 -0800288 ((j_common_ptr) cinfo, JPOOL_IMAGE,
289 compptr->width_in_blocks * compptr->DCT_h_scaled_size,
290 (JDIMENSION) (compptr->v_samp_factor * compptr->DCT_v_scaled_size));
291 }
292 }
293}