istereo2

annotate libs/libjpeg/jcprepct.c @ 35:643f4ab609a4

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