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