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