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