istereo

annotate libs/libjpeg/jcprepct.c @ 26:862a3329a8f0

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