jcprepct.cpp (15195B)
1 /* 2 * jcprepct.c 3 * 4 * Copyright (C) 1994, 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 compression preprocessing controller. 9 * This controller manages the color conversion, downsampling, 10 * and edge expansion steps. 11 * 12 * Most of the complexity here is associated with buffering input rows 13 * as required by the downsampler. See the comments at the head of 14 * jcsample.c for the downsampler's needs. 15 */ 16 17 #define JPEG_INTERNALS 18 #include "jinclude.h" 19 #include "jpeglib.h" 20 21 22 /* At present, jcsample.c can request context rows only for smoothing. 23 * In the future, we might also need context rows for CCIR601 sampling 24 * or other more-complex downsampling procedures. The code to support 25 * context rows should be compiled only if needed. 26 */ 27 #ifdef INPUT_SMOOTHING_SUPPORTED 28 #define CONTEXT_ROWS_SUPPORTED 29 #endif 30 31 32 /* 33 * For the simple (no-context-row) case, we just need to buffer one 34 * row group's worth of pixels for the downsampling step. At the bottom of 35 * the image, we pad to a full row group by replicating the last pixel row. 36 * The downsampler's last output row is then replicated if needed to pad 37 * out to a full iMCU row. 38 * 39 * When providing context rows, we must buffer three row groups' worth of 40 * pixels. Three row groups are physically allocated, but the row pointer 41 * arrays are made five row groups high, with the extra pointers above and 42 * below "wrapping around" to point to the last and first real row groups. 43 * This allows the downsampler to access the proper context rows. 44 * At the top and bottom of the image, we create dummy context rows by 45 * copying the first or last real pixel row. This copying could be avoided 46 * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the 47 * trouble on the compression side. 48 */ 49 50 51 /* Private buffer controller object */ 52 53 typedef struct { 54 struct jpeg_c_prep_controller pub;/* public fields */ 55 56 /* Downsampling input buffer. This buffer holds color-converted data 57 * until we have enough to do a downsample step. 58 */ 59 JSAMPARRAY color_buf[MAX_COMPONENTS]; 60 61 JDIMENSION rows_to_go; /* counts rows remaining in source image */ 62 int next_buf_row; /* index of next row to store in color_buf */ 63 64 #ifdef CONTEXT_ROWS_SUPPORTED /* only needed for context case */ 65 int this_row_group; /* starting row index of group to process */ 66 int next_buf_stop; /* downsample when we reach this index */ 67 #endif 68 } my_prep_controller; 69 70 typedef my_prep_controller * my_prep_ptr; 71 72 73 /* 74 * Initialize for a processing pass. 75 */ 76 77 METHODDEF void 78 start_pass_prep( j_compress_ptr cinfo, J_BUF_MODE pass_mode ) { 79 my_prep_ptr prep = (my_prep_ptr) cinfo->prep; 80 81 if ( pass_mode != JBUF_PASS_THRU ) { 82 ERREXIT( cinfo, JERR_BAD_BUFFER_MODE ); 83 } 84 85 /* Initialize total-height counter for detecting bottom of image */ 86 prep->rows_to_go = cinfo->image_height; 87 /* Mark the conversion buffer empty */ 88 prep->next_buf_row = 0; 89 #ifdef CONTEXT_ROWS_SUPPORTED 90 /* Preset additional state variables for context mode. 91 * These aren't used in non-context mode, so we needn't test which mode. 92 */ 93 prep->this_row_group = 0; 94 /* Set next_buf_stop to stop after two row groups have been read in. */ 95 prep->next_buf_stop = 2 * cinfo->max_v_samp_factor; 96 #endif 97 } 98 99 100 /* 101 * Expand an image vertically from height input_rows to height output_rows, 102 * by duplicating the bottom row. 103 */ 104 105 LOCAL void 106 expand_bottom_edge( JSAMPARRAY image_data, JDIMENSION num_cols, 107 int input_rows, int output_rows ) { 108 register int row; 109 110 for ( row = input_rows; row < output_rows; row++ ) { 111 jcopy_sample_rows( image_data, input_rows - 1, image_data, row, 112 1, num_cols ); 113 } 114 } 115 116 117 /* 118 * Process some data in the simple no-context case. 119 * 120 * Preprocessor output data is counted in "row groups". A row group 121 * is defined to be v_samp_factor sample rows of each component. 122 * Downsampling will produce this much data from each max_v_samp_factor 123 * input rows. 124 */ 125 126 METHODDEF void 127 pre_process_data( j_compress_ptr cinfo, 128 JSAMPARRAY input_buf, JDIMENSION * in_row_ctr, 129 JDIMENSION in_rows_avail, 130 JSAMPIMAGE output_buf, JDIMENSION * out_row_group_ctr, 131 JDIMENSION out_row_groups_avail ) { 132 my_prep_ptr prep = (my_prep_ptr) cinfo->prep; 133 int numrows, ci; 134 JDIMENSION inrows; 135 jpeg_component_info * compptr; 136 137 while ( *in_row_ctr < in_rows_avail && 138 *out_row_group_ctr < out_row_groups_avail ) { 139 /* Do color conversion to fill the conversion buffer. */ 140 inrows = in_rows_avail - *in_row_ctr; 141 numrows = cinfo->max_v_samp_factor - prep->next_buf_row; 142 numrows = (int) MIN( (JDIMENSION) numrows, inrows ); 143 ( *cinfo->cconvert->color_convert )( cinfo, input_buf + *in_row_ctr, 144 prep->color_buf, 145 (JDIMENSION) prep->next_buf_row, 146 numrows ); 147 *in_row_ctr += numrows; 148 prep->next_buf_row += numrows; 149 prep->rows_to_go -= numrows; 150 /* If at bottom of image, pad to fill the conversion buffer. */ 151 if ( ( prep->rows_to_go == 0 ) && 152 ( prep->next_buf_row < cinfo->max_v_samp_factor ) ) { 153 for ( ci = 0; ci < cinfo->num_components; ci++ ) { 154 expand_bottom_edge( prep->color_buf[ci], cinfo->image_width, 155 prep->next_buf_row, cinfo->max_v_samp_factor ); 156 } 157 prep->next_buf_row = cinfo->max_v_samp_factor; 158 } 159 /* If we've filled the conversion buffer, empty it. */ 160 if ( prep->next_buf_row == cinfo->max_v_samp_factor ) { 161 ( *cinfo->downsample->downsample )( cinfo, 162 prep->color_buf, (JDIMENSION) 0, 163 output_buf, *out_row_group_ctr ); 164 prep->next_buf_row = 0; 165 ( *out_row_group_ctr )++; 166 } 167 /* If at bottom of image, pad the output to a full iMCU height. 168 * Note we assume the caller is providing a one-iMCU-height output buffer! 169 */ 170 if ( ( prep->rows_to_go == 0 ) && 171 ( *out_row_group_ctr < out_row_groups_avail ) ) { 172 for ( ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 173 ci++, compptr++ ) { 174 expand_bottom_edge( output_buf[ci], 175 compptr->width_in_blocks * DCTSIZE, 176 (int) ( *out_row_group_ctr * compptr->v_samp_factor ), 177 (int) ( out_row_groups_avail * compptr->v_samp_factor ) ); 178 } 179 *out_row_group_ctr = out_row_groups_avail; 180 break; /* can exit outer loop without test */ 181 } 182 } 183 } 184 185 186 #ifdef CONTEXT_ROWS_SUPPORTED 187 188 /* 189 * Process some data in the context case. 190 */ 191 192 METHODDEF void 193 pre_process_context( j_compress_ptr cinfo, 194 JSAMPARRAY input_buf, JDIMENSION * in_row_ctr, 195 JDIMENSION in_rows_avail, 196 JSAMPIMAGE output_buf, JDIMENSION * out_row_group_ctr, 197 JDIMENSION out_row_groups_avail ) { 198 my_prep_ptr prep = (my_prep_ptr) cinfo->prep; 199 int numrows, ci; 200 int buf_height = cinfo->max_v_samp_factor * 3; 201 JDIMENSION inrows; 202 jpeg_component_info * compptr; 203 204 while ( *out_row_group_ctr < out_row_groups_avail ) { 205 if ( *in_row_ctr < in_rows_avail ) { 206 /* Do color conversion to fill the conversion buffer. */ 207 inrows = in_rows_avail - *in_row_ctr; 208 numrows = prep->next_buf_stop - prep->next_buf_row; 209 numrows = (int) MIN( (JDIMENSION) numrows, inrows ); 210 ( *cinfo->cconvert->color_convert )( cinfo, input_buf + *in_row_ctr, 211 prep->color_buf, 212 (JDIMENSION) prep->next_buf_row, 213 numrows ); 214 /* Pad at top of image, if first time through */ 215 if ( prep->rows_to_go == cinfo->image_height ) { 216 for ( ci = 0; ci < cinfo->num_components; ci++ ) { 217 int row; 218 for ( row = 1; row <= cinfo->max_v_samp_factor; row++ ) { 219 jcopy_sample_rows( prep->color_buf[ci], 0, 220 prep->color_buf[ci], -row, 221 1, cinfo->image_width ); 222 } 223 } 224 } 225 *in_row_ctr += numrows; 226 prep->next_buf_row += numrows; 227 prep->rows_to_go -= numrows; 228 } else { 229 /* Return for more data, unless we are at the bottom of the image. */ 230 if ( prep->rows_to_go != 0 ) { 231 break; 232 } 233 } 234 /* If at bottom of image, pad to fill the conversion buffer. */ 235 if ( ( prep->rows_to_go == 0 ) && 236 ( prep->next_buf_row < prep->next_buf_stop ) ) { 237 for ( ci = 0; ci < cinfo->num_components; ci++ ) { 238 expand_bottom_edge( prep->color_buf[ci], cinfo->image_width, 239 prep->next_buf_row, prep->next_buf_stop ); 240 } 241 prep->next_buf_row = prep->next_buf_stop; 242 } 243 /* If we've gotten enough data, downsample a row group. */ 244 if ( prep->next_buf_row == prep->next_buf_stop ) { 245 ( *cinfo->downsample->downsample )( cinfo, 246 prep->color_buf, 247 (JDIMENSION) prep->this_row_group, 248 output_buf, *out_row_group_ctr ); 249 ( *out_row_group_ctr )++; 250 /* Advance pointers with wraparound as necessary. */ 251 prep->this_row_group += cinfo->max_v_samp_factor; 252 if ( prep->this_row_group >= buf_height ) { 253 prep->this_row_group = 0; 254 } 255 if ( prep->next_buf_row >= buf_height ) { 256 prep->next_buf_row = 0; 257 } 258 prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor; 259 } 260 /* If at bottom of image, pad the output to a full iMCU height. 261 * Note we assume the caller is providing a one-iMCU-height output buffer! 262 */ 263 if ( ( prep->rows_to_go == 0 ) && 264 ( *out_row_group_ctr < out_row_groups_avail ) ) { 265 for ( ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 266 ci++, compptr++ ) { 267 expand_bottom_edge( output_buf[ci], 268 compptr->width_in_blocks * DCTSIZE, 269 (int) ( *out_row_group_ctr * compptr->v_samp_factor ), 270 (int) ( out_row_groups_avail * compptr->v_samp_factor ) ); 271 } 272 *out_row_group_ctr = out_row_groups_avail; 273 break; /* can exit outer loop without test */ 274 } 275 } 276 } 277 278 279 /* 280 * Create the wrapped-around downsampling input buffer needed for context mode. 281 */ 282 283 LOCAL void 284 create_context_buffer( j_compress_ptr cinfo ) { 285 my_prep_ptr prep = (my_prep_ptr) cinfo->prep; 286 int rgroup_height = cinfo->max_v_samp_factor; 287 int ci, i; 288 jpeg_component_info * compptr; 289 JSAMPARRAY true_buffer, fake_buffer; 290 291 /* Grab enough space for fake row pointers for all the components; 292 * we need five row groups' worth of pointers for each component. 293 */ 294 fake_buffer = (JSAMPARRAY) 295 ( *cinfo->mem->alloc_small )( (j_common_ptr) cinfo, JPOOL_IMAGE, 296 ( cinfo->num_components * 5 * rgroup_height ) * 297 SIZEOF( JSAMPROW ) ); 298 299 for ( ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 300 ci++, compptr++ ) { 301 /* Allocate the actual buffer space (3 row groups) for this component. 302 * We make the buffer wide enough to allow the downsampler to edge-expand 303 * horizontally within the buffer, if it so chooses. 304 */ 305 true_buffer = ( *cinfo->mem->alloc_sarray ) 306 ( (j_common_ptr) cinfo, JPOOL_IMAGE, 307 (JDIMENSION) ( ( (long) compptr->width_in_blocks * DCTSIZE * 308 cinfo->max_h_samp_factor ) / compptr->h_samp_factor ), 309 (JDIMENSION) ( 3 * rgroup_height ) ); 310 /* Copy true buffer row pointers into the middle of the fake row array */ 311 MEMCOPY( fake_buffer + rgroup_height, true_buffer, 312 3 * rgroup_height * SIZEOF( JSAMPROW ) ); 313 /* Fill in the above and below wraparound pointers */ 314 for ( i = 0; i < rgroup_height; i++ ) { 315 fake_buffer[i] = true_buffer[2 * rgroup_height + i]; 316 fake_buffer[4 * rgroup_height + i] = true_buffer[i]; 317 } 318 prep->color_buf[ci] = fake_buffer + rgroup_height; 319 fake_buffer += 5 * rgroup_height;/* point to space for next component */ 320 } 321 } 322 323 #endif /* CONTEXT_ROWS_SUPPORTED */ 324 325 326 /* 327 * Initialize preprocessing controller. 328 */ 329 330 GLOBAL void 331 jinit_c_prep_controller( j_compress_ptr cinfo, boolean need_full_buffer ) { 332 my_prep_ptr prep; 333 int ci; 334 jpeg_component_info * compptr; 335 336 if ( need_full_buffer ) {/* safety check */ 337 ERREXIT( cinfo, JERR_BAD_BUFFER_MODE ); 338 } 339 340 prep = (my_prep_ptr) 341 ( *cinfo->mem->alloc_small )( (j_common_ptr) cinfo, JPOOL_IMAGE, 342 SIZEOF( my_prep_controller ) ); 343 cinfo->prep = (struct jpeg_c_prep_controller *) prep; 344 prep->pub.start_pass = start_pass_prep; 345 346 /* Allocate the color conversion buffer. 347 * We make the buffer wide enough to allow the downsampler to edge-expand 348 * horizontally within the buffer, if it so chooses. 349 */ 350 if ( cinfo->downsample->need_context_rows ) { 351 /* Set up to provide context rows */ 352 #ifdef CONTEXT_ROWS_SUPPORTED 353 prep->pub.pre_process_data = pre_process_context; 354 create_context_buffer( cinfo ); 355 #else 356 ERREXIT( cinfo, JERR_NOT_COMPILED ); 357 #endif 358 } else { 359 /* No context, just make it tall enough for one row group */ 360 prep->pub.pre_process_data = pre_process_data; 361 for ( ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 362 ci++, compptr++ ) { 363 prep->color_buf[ci] = ( *cinfo->mem->alloc_sarray ) 364 ( (j_common_ptr) cinfo, JPOOL_IMAGE, 365 (JDIMENSION) ( ( (long) compptr->width_in_blocks * DCTSIZE * 366 cinfo->max_h_samp_factor ) / compptr->h_samp_factor ), 367 (JDIMENSION) cinfo->max_v_samp_factor ); 368 } 369 } 370 }