blob: 5a0549a1b6d52f4910f04e73d78738a3dd126121 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jcmainct.c
3 *
Thomas G. Lane489583f1996-02-07 00:00:00 +00004 * Copyright (C) 1994-1996, Thomas G. Lane.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00005 * 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
DRCe5eaf372014-05-09 18:00:32 +000031 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 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000035
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 */
Thomas G. Lane489583f1996-02-07 00:00:00 +000054METHODDEF(void) process_data_simple_main
DRCe5eaf372014-05-09 18:00:32 +000055 JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
56 JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000057#ifdef FULL_MAIN_BUFFER_SUPPORTED
Thomas G. Lane489583f1996-02-07 00:00:00 +000058METHODDEF(void) process_data_buffer_main
DRCe5eaf372014-05-09 18:00:32 +000059 JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
60 JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000061#endif
62
63
64/*
65 * Initialize for a processing pass.
66 */
67
Thomas G. Lane489583f1996-02-07 00:00:00 +000068METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000069start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
70{
DRC7ed7b572012-02-07 22:51:49 +000071 my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000072
73 /* Do nothing in raw-data mode. */
74 if (cinfo->raw_data_in)
75 return;
76
DRCe5eaf372014-05-09 18:00:32 +000077 main_ptr->cur_iMCU_row = 0; /* initialize counters */
DRC7ed7b572012-02-07 22:51:49 +000078 main_ptr->rowgroup_ctr = 0;
79 main_ptr->suspended = FALSE;
DRCe5eaf372014-05-09 18:00:32 +000080 main_ptr->pass_mode = pass_mode; /* save mode for use by process_data */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000081
82 switch (pass_mode) {
83 case JBUF_PASS_THRU:
84#ifdef FULL_MAIN_BUFFER_SUPPORTED
DRC7ed7b572012-02-07 22:51:49 +000085 if (main_ptr->whole_image[0] != NULL)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000086 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
87#endif
DRC7ed7b572012-02-07 22:51:49 +000088 main_ptr->pub.process_data = process_data_simple_main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000089 break;
90#ifdef FULL_MAIN_BUFFER_SUPPORTED
91 case JBUF_SAVE_SOURCE:
92 case JBUF_CRANK_DEST:
93 case JBUF_SAVE_AND_PASS:
DRC7ed7b572012-02-07 22:51:49 +000094 if (main_ptr->whole_image[0] == NULL)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000095 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
DRC7ed7b572012-02-07 22:51:49 +000096 main_ptr->pub.process_data = process_data_buffer_main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000097 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
Thomas G. Lane489583f1996-02-07 00:00:00 +0000112METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000113process_data_simple_main (j_compress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000114 JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
115 JDIMENSION in_rows_avail)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000116{
DRC7ed7b572012-02-07 22:51:49 +0000117 my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000118
DRC7ed7b572012-02-07 22:51:49 +0000119 while (main_ptr->cur_iMCU_row < cinfo->total_iMCU_rows) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000120 /* Read input data if we haven't filled the main buffer yet */
DRC7ed7b572012-02-07 22:51:49 +0000121 if (main_ptr->rowgroup_ctr < DCTSIZE)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000122 (*cinfo->prep->pre_process_data) (cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000123 input_buf, in_row_ctr, in_rows_avail,
124 main_ptr->buffer, &main_ptr->rowgroup_ctr,
125 (JDIMENSION) DCTSIZE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000126
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 */
DRC7ed7b572012-02-07 22:51:49 +0000131 if (main_ptr->rowgroup_ctr != DCTSIZE)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000132 return;
133
134 /* Send the completed row to the compressor */
DRC7ed7b572012-02-07 22:51:49 +0000135 if (! (*cinfo->coef->compress_data) (cinfo, main_ptr->buffer)) {
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000136 /* 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 */
DRC7ed7b572012-02-07 22:51:49 +0000142 if (! main_ptr->suspended) {
DRCe5eaf372014-05-09 18:00:32 +0000143 (*in_row_ctr)--;
144 main_ptr->suspended = TRUE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000145 }
146 return;
147 }
148 /* We did finish the row. Undo our little suspension hack if a previous
149 * call suspended; then mark the main buffer empty.
150 */
DRC7ed7b572012-02-07 22:51:49 +0000151 if (main_ptr->suspended) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000152 (*in_row_ctr)++;
DRC7ed7b572012-02-07 22:51:49 +0000153 main_ptr->suspended = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000154 }
DRC7ed7b572012-02-07 22:51:49 +0000155 main_ptr->rowgroup_ctr = 0;
156 main_ptr->cur_iMCU_row++;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000157 }
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
Thomas G. Lane489583f1996-02-07 00:00:00 +0000168METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000169process_data_buffer_main (j_compress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000170 JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
171 JDIMENSION in_rows_avail)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000172{
DRC2dcd4b52012-12-01 22:33:54 +0000173 my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000174 int ci;
175 jpeg_component_info *compptr;
DRC7ed7b572012-02-07 22:51:49 +0000176 boolean writing = (main_ptr->pass_mode != JBUF_CRANK_DEST);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000177
DRC7ed7b572012-02-07 22:51:49 +0000178 while (main_ptr->cur_iMCU_row < cinfo->total_iMCU_rows) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000179 /* Realign the virtual buffers if at the start of an iMCU row. */
DRC7ed7b572012-02-07 22:51:49 +0000180 if (main_ptr->rowgroup_ctr == 0) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000181 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
DRCe5eaf372014-05-09 18:00:32 +0000182 ci++, compptr++) {
183 main_ptr->buffer[ci] = (*cinfo->mem->access_virt_sarray)
184 ((j_common_ptr) cinfo, main_ptr->whole_image[ci],
185 main_ptr->cur_iMCU_row * (compptr->v_samp_factor * DCTSIZE),
186 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE), writing);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000187 }
188 /* In a read pass, pretend we just read some source data. */
189 if (! writing) {
DRCe5eaf372014-05-09 18:00:32 +0000190 *in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE;
191 main_ptr->rowgroup_ctr = DCTSIZE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000192 }
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,
DRCe5eaf372014-05-09 18:00:32 +0000199 input_buf, in_row_ctr, in_rows_avail,
200 main_ptr->buffer, &main_ptr->rowgroup_ctr,
201 (JDIMENSION) DCTSIZE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000202 /* Return to application if we need more data to fill the iMCU row. */
DRC7ed7b572012-02-07 22:51:49 +0000203 if (main_ptr->rowgroup_ctr < DCTSIZE)
DRCe5eaf372014-05-09 18:00:32 +0000204 return;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000205 }
206
207 /* Emit data, unless this is a sink-only pass. */
DRC7ed7b572012-02-07 22:51:49 +0000208 if (main_ptr->pass_mode != JBUF_SAVE_SOURCE) {
209 if (! (*cinfo->coef->compress_data) (cinfo, main_ptr->buffer)) {
DRCe5eaf372014-05-09 18:00:32 +0000210 /* 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 */
216 if (! main_ptr->suspended) {
217 (*in_row_ctr)--;
218 main_ptr->suspended = TRUE;
219 }
220 return;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000221 }
222 /* We did finish the row. Undo our little suspension hack if a previous
223 * call suspended; then mark the main buffer empty.
224 */
DRC7ed7b572012-02-07 22:51:49 +0000225 if (main_ptr->suspended) {
DRCe5eaf372014-05-09 18:00:32 +0000226 (*in_row_ctr)++;
227 main_ptr->suspended = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000228 }
229 }
230
231 /* If get here, we are done with this iMCU row. Mark buffer empty. */
DRC7ed7b572012-02-07 22:51:49 +0000232 main_ptr->rowgroup_ctr = 0;
233 main_ptr->cur_iMCU_row++;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000234 }
235}
236
237#endif /* FULL_MAIN_BUFFER_SUPPORTED */
238
239
240/*
241 * Initialize main buffer controller.
242 */
243
Thomas G. Lane489583f1996-02-07 00:00:00 +0000244GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000245jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
246{
DRC7ed7b572012-02-07 22:51:49 +0000247 my_main_ptr main_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000248 int ci;
249 jpeg_component_info *compptr;
250
DRC7ed7b572012-02-07 22:51:49 +0000251 main_ptr = (my_main_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000252 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRCe5eaf372014-05-09 18:00:32 +0000253 SIZEOF(my_main_controller));
DRC7ed7b572012-02-07 22:51:49 +0000254 cinfo->main = (struct jpeg_c_main_controller *) main_ptr;
255 main_ptr->pub.start_pass = start_pass_main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000256
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 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000267 /* Note we pad the bottom to a multiple of the iMCU height */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000268 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
DRCe5eaf372014-05-09 18:00:32 +0000269 ci++, compptr++) {
DRC7ed7b572012-02-07 22:51:49 +0000270 main_ptr->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
DRCe5eaf372014-05-09 18:00:32 +0000271 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
272 compptr->width_in_blocks * DCTSIZE,
273 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
274 (long) compptr->v_samp_factor) * DCTSIZE,
275 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000276 }
277#else
278 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
279#endif
280 } else {
281#ifdef FULL_MAIN_BUFFER_SUPPORTED
DRC7ed7b572012-02-07 22:51:49 +0000282 main_ptr->whole_image[0] = NULL; /* flag for no virtual arrays */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000283#endif
284 /* Allocate a strip buffer for each component */
285 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
DRCe5eaf372014-05-09 18:00:32 +0000286 ci++, compptr++) {
DRC7ed7b572012-02-07 22:51:49 +0000287 main_ptr->buffer[ci] = (*cinfo->mem->alloc_sarray)
DRCe5eaf372014-05-09 18:00:32 +0000288 ((j_common_ptr) cinfo, JPOOL_IMAGE,
289 compptr->width_in_blocks * DCTSIZE,
290 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000291 }
292 }
293}