blob: fc4014b15151309a81452e5325b792fbbc210572 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jcmainct.c
3 *
DRC5033f3e2014-05-18 18:33:44 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane489583f1996-02-07 00:00:00 +00005 * Copyright (C) 1994-1996, Thomas G. Lane.
DRC5033f3e2014-05-18 18:33:44 +00006 * It was modified by The libjpeg-turbo Project to include only code relevant
7 * to libjpeg-turbo.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00008 * For conditions of distribution and use, see the accompanying README file.
9 *
10 * This file contains the main buffer controller for compression.
11 * The main buffer lies between the pre-processor and the JPEG
12 * compressor proper; it holds downsampled data in the JPEG colorspace.
13 */
14
15#define JPEG_INTERNALS
16#include "jinclude.h"
17#include "jpeglib.h"
18
19
20/* Note: currently, there is no operating mode in which a full-image buffer
21 * is needed at this step. If there were, that mode could not be used with
22 * "raw data" input, since this module is bypassed in that case. However,
23 * we've left the code here for possible use in special applications.
24 */
25#undef FULL_MAIN_BUFFER_SUPPORTED
26
27
28/* Private buffer controller object */
29
30typedef struct {
31 struct jpeg_c_main_controller pub; /* public fields */
32
DRCe5eaf372014-05-09 18:00:32 +000033 JDIMENSION cur_iMCU_row; /* number of current iMCU row */
34 JDIMENSION rowgroup_ctr; /* counts row groups received in iMCU row */
35 boolean suspended; /* remember if we suspended output */
36 J_BUF_MODE pass_mode; /* current operating mode */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000037
38 /* If using just a strip buffer, this points to the entire set of buffers
39 * (we allocate one for each component). In the full-image case, this
40 * points to the currently accessible strips of the virtual arrays.
41 */
42 JSAMPARRAY buffer[MAX_COMPONENTS];
43
44#ifdef FULL_MAIN_BUFFER_SUPPORTED
45 /* If using full-image storage, this array holds pointers to virtual-array
46 * control blocks for each component. Unused if not full-image storage.
47 */
48 jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
49#endif
50} my_main_controller;
51
52typedef my_main_controller * my_main_ptr;
53
54
55/* Forward declarations */
Thomas G. Lane489583f1996-02-07 00:00:00 +000056METHODDEF(void) process_data_simple_main
DRCbc56b752014-05-16 10:43:44 +000057 (j_compress_ptr cinfo, JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
58 JDIMENSION in_rows_avail);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000059#ifdef FULL_MAIN_BUFFER_SUPPORTED
Thomas G. Lane489583f1996-02-07 00:00:00 +000060METHODDEF(void) process_data_buffer_main
DRCbc56b752014-05-16 10:43:44 +000061 (j_compress_ptr cinfo, JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
62 JDIMENSION in_rows_avail);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000063#endif
64
65
66/*
67 * Initialize for a processing pass.
68 */
69
Thomas G. Lane489583f1996-02-07 00:00:00 +000070METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000071start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
72{
DRC7ed7b572012-02-07 22:51:49 +000073 my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000074
75 /* Do nothing in raw-data mode. */
76 if (cinfo->raw_data_in)
77 return;
78
DRCe5eaf372014-05-09 18:00:32 +000079 main_ptr->cur_iMCU_row = 0; /* initialize counters */
DRC7ed7b572012-02-07 22:51:49 +000080 main_ptr->rowgroup_ctr = 0;
81 main_ptr->suspended = FALSE;
DRCe5eaf372014-05-09 18:00:32 +000082 main_ptr->pass_mode = pass_mode; /* save mode for use by process_data */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000083
84 switch (pass_mode) {
85 case JBUF_PASS_THRU:
86#ifdef FULL_MAIN_BUFFER_SUPPORTED
DRC7ed7b572012-02-07 22:51:49 +000087 if (main_ptr->whole_image[0] != NULL)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000088 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
89#endif
DRC7ed7b572012-02-07 22:51:49 +000090 main_ptr->pub.process_data = process_data_simple_main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000091 break;
92#ifdef FULL_MAIN_BUFFER_SUPPORTED
93 case JBUF_SAVE_SOURCE:
94 case JBUF_CRANK_DEST:
95 case JBUF_SAVE_AND_PASS:
DRC7ed7b572012-02-07 22:51:49 +000096 if (main_ptr->whole_image[0] == NULL)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000097 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
DRC7ed7b572012-02-07 22:51:49 +000098 main_ptr->pub.process_data = process_data_buffer_main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000099 break;
100#endif
101 default:
102 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
103 break;
104 }
105}
106
107
108/*
109 * Process some data.
110 * This routine handles the simple pass-through mode,
111 * where we have only a strip buffer.
112 */
113
Thomas G. Lane489583f1996-02-07 00:00:00 +0000114METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000115process_data_simple_main (j_compress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000116 JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
117 JDIMENSION in_rows_avail)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000118{
DRC7ed7b572012-02-07 22:51:49 +0000119 my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000120
DRC7ed7b572012-02-07 22:51:49 +0000121 while (main_ptr->cur_iMCU_row < cinfo->total_iMCU_rows) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000122 /* Read input data if we haven't filled the main buffer yet */
DRC7ed7b572012-02-07 22:51:49 +0000123 if (main_ptr->rowgroup_ctr < DCTSIZE)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000124 (*cinfo->prep->pre_process_data) (cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000125 input_buf, in_row_ctr, in_rows_avail,
126 main_ptr->buffer, &main_ptr->rowgroup_ctr,
127 (JDIMENSION) DCTSIZE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000128
129 /* If we don't have a full iMCU row buffered, return to application for
130 * more data. Note that preprocessor will always pad to fill the iMCU row
131 * at the bottom of the image.
132 */
DRC7ed7b572012-02-07 22:51:49 +0000133 if (main_ptr->rowgroup_ctr != DCTSIZE)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000134 return;
135
136 /* Send the completed row to the compressor */
DRC7ed7b572012-02-07 22:51:49 +0000137 if (! (*cinfo->coef->compress_data) (cinfo, main_ptr->buffer)) {
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000138 /* If compressor did not consume the whole row, then we must need to
139 * suspend processing and return to the application. In this situation
140 * we pretend we didn't yet consume the last input row; otherwise, if
141 * it happened to be the last row of the image, the application would
142 * think we were done.
143 */
DRC7ed7b572012-02-07 22:51:49 +0000144 if (! main_ptr->suspended) {
DRCe5eaf372014-05-09 18:00:32 +0000145 (*in_row_ctr)--;
146 main_ptr->suspended = TRUE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000147 }
148 return;
149 }
150 /* We did finish the row. Undo our little suspension hack if a previous
151 * call suspended; then mark the main buffer empty.
152 */
DRC7ed7b572012-02-07 22:51:49 +0000153 if (main_ptr->suspended) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000154 (*in_row_ctr)++;
DRC7ed7b572012-02-07 22:51:49 +0000155 main_ptr->suspended = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000156 }
DRC7ed7b572012-02-07 22:51:49 +0000157 main_ptr->rowgroup_ctr = 0;
158 main_ptr->cur_iMCU_row++;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000159 }
160}
161
162
163#ifdef FULL_MAIN_BUFFER_SUPPORTED
164
165/*
166 * Process some data.
167 * This routine handles all of the modes that use a full-size buffer.
168 */
169
Thomas G. Lane489583f1996-02-07 00:00:00 +0000170METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000171process_data_buffer_main (j_compress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000172 JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
173 JDIMENSION in_rows_avail)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000174{
DRC2dcd4b52012-12-01 22:33:54 +0000175 my_main_ptr main_ptr = (my_main_ptr) cinfo->main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000176 int ci;
177 jpeg_component_info *compptr;
DRC7ed7b572012-02-07 22:51:49 +0000178 boolean writing = (main_ptr->pass_mode != JBUF_CRANK_DEST);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000179
DRC7ed7b572012-02-07 22:51:49 +0000180 while (main_ptr->cur_iMCU_row < cinfo->total_iMCU_rows) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000181 /* Realign the virtual buffers if at the start of an iMCU row. */
DRC7ed7b572012-02-07 22:51:49 +0000182 if (main_ptr->rowgroup_ctr == 0) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000183 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
DRCe5eaf372014-05-09 18:00:32 +0000184 ci++, compptr++) {
185 main_ptr->buffer[ci] = (*cinfo->mem->access_virt_sarray)
186 ((j_common_ptr) cinfo, main_ptr->whole_image[ci],
187 main_ptr->cur_iMCU_row * (compptr->v_samp_factor * DCTSIZE),
188 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE), writing);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000189 }
190 /* In a read pass, pretend we just read some source data. */
191 if (! writing) {
DRCe5eaf372014-05-09 18:00:32 +0000192 *in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE;
193 main_ptr->rowgroup_ctr = DCTSIZE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000194 }
195 }
196
197 /* If a write pass, read input data until the current iMCU row is full. */
198 /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
199 if (writing) {
200 (*cinfo->prep->pre_process_data) (cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000201 input_buf, in_row_ctr, in_rows_avail,
202 main_ptr->buffer, &main_ptr->rowgroup_ctr,
203 (JDIMENSION) DCTSIZE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000204 /* Return to application if we need more data to fill the iMCU row. */
DRC7ed7b572012-02-07 22:51:49 +0000205 if (main_ptr->rowgroup_ctr < DCTSIZE)
DRCe5eaf372014-05-09 18:00:32 +0000206 return;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000207 }
208
209 /* Emit data, unless this is a sink-only pass. */
DRC7ed7b572012-02-07 22:51:49 +0000210 if (main_ptr->pass_mode != JBUF_SAVE_SOURCE) {
211 if (! (*cinfo->coef->compress_data) (cinfo, main_ptr->buffer)) {
DRCe5eaf372014-05-09 18:00:32 +0000212 /* If compressor did not consume the whole row, then we must need to
213 * suspend processing and return to the application. In this situation
214 * we pretend we didn't yet consume the last input row; otherwise, if
215 * it happened to be the last row of the image, the application would
216 * think we were done.
217 */
218 if (! main_ptr->suspended) {
219 (*in_row_ctr)--;
220 main_ptr->suspended = TRUE;
221 }
222 return;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000223 }
224 /* We did finish the row. Undo our little suspension hack if a previous
225 * call suspended; then mark the main buffer empty.
226 */
DRC7ed7b572012-02-07 22:51:49 +0000227 if (main_ptr->suspended) {
DRCe5eaf372014-05-09 18:00:32 +0000228 (*in_row_ctr)++;
229 main_ptr->suspended = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000230 }
231 }
232
233 /* If get here, we are done with this iMCU row. Mark buffer empty. */
DRC7ed7b572012-02-07 22:51:49 +0000234 main_ptr->rowgroup_ctr = 0;
235 main_ptr->cur_iMCU_row++;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000236 }
237}
238
239#endif /* FULL_MAIN_BUFFER_SUPPORTED */
240
241
242/*
243 * Initialize main buffer controller.
244 */
245
Thomas G. Lane489583f1996-02-07 00:00:00 +0000246GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000247jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
248{
DRC7ed7b572012-02-07 22:51:49 +0000249 my_main_ptr main_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000250 int ci;
251 jpeg_component_info *compptr;
252
DRC7ed7b572012-02-07 22:51:49 +0000253 main_ptr = (my_main_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000254 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000255 sizeof(my_main_controller));
DRC7ed7b572012-02-07 22:51:49 +0000256 cinfo->main = (struct jpeg_c_main_controller *) main_ptr;
257 main_ptr->pub.start_pass = start_pass_main;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000258
259 /* We don't need to create a buffer in raw-data mode. */
260 if (cinfo->raw_data_in)
261 return;
262
263 /* Create the buffer. It holds downsampled data, so each component
264 * may be of a different size.
265 */
266 if (need_full_buffer) {
267#ifdef FULL_MAIN_BUFFER_SUPPORTED
268 /* Allocate a full-image virtual array for each component */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000269 /* Note we pad the bottom to a multiple of the iMCU height */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000270 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
DRCe5eaf372014-05-09 18:00:32 +0000271 ci++, compptr++) {
DRC7ed7b572012-02-07 22:51:49 +0000272 main_ptr->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
DRCe5eaf372014-05-09 18:00:32 +0000273 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
274 compptr->width_in_blocks * DCTSIZE,
275 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
276 (long) compptr->v_samp_factor) * DCTSIZE,
277 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000278 }
279#else
280 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
281#endif
282 } else {
283#ifdef FULL_MAIN_BUFFER_SUPPORTED
DRC7ed7b572012-02-07 22:51:49 +0000284 main_ptr->whole_image[0] = NULL; /* flag for no virtual arrays */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000285#endif
286 /* Allocate a strip buffer for each component */
287 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
DRCe5eaf372014-05-09 18:00:32 +0000288 ci++, compptr++) {
DRC7ed7b572012-02-07 22:51:49 +0000289 main_ptr->buffer[ci] = (*cinfo->mem->alloc_sarray)
DRCe5eaf372014-05-09 18:00:32 +0000290 ((j_common_ptr) cinfo, JPOOL_IMAGE,
291 compptr->width_in_blocks * DCTSIZE,
292 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000293 }
294 }
295}