media/libjpeg/jdpostct.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * jdpostct.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 decompression postprocessing controller.
michael@0 9 * This controller manages the upsampling, color conversion, and color
michael@0 10 * quantization/reduction steps; specifically, it controls the buffering
michael@0 11 * between upsample/color conversion and color quantization/reduction.
michael@0 12 *
michael@0 13 * If no color quantization/reduction is required, then this module has no
michael@0 14 * work to do, and it just hands off to the upsample/color conversion code.
michael@0 15 * An integrated upsample/convert/quantize process would replace this module
michael@0 16 * entirely.
michael@0 17 */
michael@0 18
michael@0 19 #define JPEG_INTERNALS
michael@0 20 #include "jinclude.h"
michael@0 21 #include "jpeglib.h"
michael@0 22
michael@0 23
michael@0 24 /* Private buffer controller object */
michael@0 25
michael@0 26 typedef struct {
michael@0 27 struct jpeg_d_post_controller pub; /* public fields */
michael@0 28
michael@0 29 /* Color quantization source buffer: this holds output data from
michael@0 30 * the upsample/color conversion step to be passed to the quantizer.
michael@0 31 * For two-pass color quantization, we need a full-image buffer;
michael@0 32 * for one-pass operation, a strip buffer is sufficient.
michael@0 33 */
michael@0 34 jvirt_sarray_ptr whole_image; /* virtual array, or NULL if one-pass */
michael@0 35 JSAMPARRAY buffer; /* strip buffer, or current strip of virtual */
michael@0 36 JDIMENSION strip_height; /* buffer size in rows */
michael@0 37 /* for two-pass mode only: */
michael@0 38 JDIMENSION starting_row; /* row # of first row in current strip */
michael@0 39 JDIMENSION next_row; /* index of next row to fill/empty in strip */
michael@0 40 } my_post_controller;
michael@0 41
michael@0 42 typedef my_post_controller * my_post_ptr;
michael@0 43
michael@0 44
michael@0 45 /* Forward declarations */
michael@0 46 METHODDEF(void) post_process_1pass
michael@0 47 JPP((j_decompress_ptr cinfo,
michael@0 48 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
michael@0 49 JDIMENSION in_row_groups_avail,
michael@0 50 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
michael@0 51 JDIMENSION out_rows_avail));
michael@0 52 #ifdef QUANT_2PASS_SUPPORTED
michael@0 53 METHODDEF(void) post_process_prepass
michael@0 54 JPP((j_decompress_ptr cinfo,
michael@0 55 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
michael@0 56 JDIMENSION in_row_groups_avail,
michael@0 57 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
michael@0 58 JDIMENSION out_rows_avail));
michael@0 59 METHODDEF(void) post_process_2pass
michael@0 60 JPP((j_decompress_ptr cinfo,
michael@0 61 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
michael@0 62 JDIMENSION in_row_groups_avail,
michael@0 63 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
michael@0 64 JDIMENSION out_rows_avail));
michael@0 65 #endif
michael@0 66
michael@0 67
michael@0 68 /*
michael@0 69 * Initialize for a processing pass.
michael@0 70 */
michael@0 71
michael@0 72 METHODDEF(void)
michael@0 73 start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
michael@0 74 {
michael@0 75 my_post_ptr post = (my_post_ptr) cinfo->post;
michael@0 76
michael@0 77 switch (pass_mode) {
michael@0 78 case JBUF_PASS_THRU:
michael@0 79 if (cinfo->quantize_colors) {
michael@0 80 /* Single-pass processing with color quantization. */
michael@0 81 post->pub.post_process_data = post_process_1pass;
michael@0 82 /* We could be doing buffered-image output before starting a 2-pass
michael@0 83 * color quantization; in that case, jinit_d_post_controller did not
michael@0 84 * allocate a strip buffer. Use the virtual-array buffer as workspace.
michael@0 85 */
michael@0 86 if (post->buffer == NULL) {
michael@0 87 post->buffer = (*cinfo->mem->access_virt_sarray)
michael@0 88 ((j_common_ptr) cinfo, post->whole_image,
michael@0 89 (JDIMENSION) 0, post->strip_height, TRUE);
michael@0 90 }
michael@0 91 } else {
michael@0 92 /* For single-pass processing without color quantization,
michael@0 93 * I have no work to do; just call the upsampler directly.
michael@0 94 */
michael@0 95 post->pub.post_process_data = cinfo->upsample->upsample;
michael@0 96 }
michael@0 97 break;
michael@0 98 #ifdef QUANT_2PASS_SUPPORTED
michael@0 99 case JBUF_SAVE_AND_PASS:
michael@0 100 /* First pass of 2-pass quantization */
michael@0 101 if (post->whole_image == NULL)
michael@0 102 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
michael@0 103 post->pub.post_process_data = post_process_prepass;
michael@0 104 break;
michael@0 105 case JBUF_CRANK_DEST:
michael@0 106 /* Second pass of 2-pass quantization */
michael@0 107 if (post->whole_image == NULL)
michael@0 108 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
michael@0 109 post->pub.post_process_data = post_process_2pass;
michael@0 110 break;
michael@0 111 #endif /* QUANT_2PASS_SUPPORTED */
michael@0 112 default:
michael@0 113 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
michael@0 114 break;
michael@0 115 }
michael@0 116 post->starting_row = post->next_row = 0;
michael@0 117 }
michael@0 118
michael@0 119
michael@0 120 /*
michael@0 121 * Process some data in the one-pass (strip buffer) case.
michael@0 122 * This is used for color precision reduction as well as one-pass quantization.
michael@0 123 */
michael@0 124
michael@0 125 METHODDEF(void)
michael@0 126 post_process_1pass (j_decompress_ptr cinfo,
michael@0 127 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
michael@0 128 JDIMENSION in_row_groups_avail,
michael@0 129 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
michael@0 130 JDIMENSION out_rows_avail)
michael@0 131 {
michael@0 132 my_post_ptr post = (my_post_ptr) cinfo->post;
michael@0 133 JDIMENSION num_rows, max_rows;
michael@0 134
michael@0 135 /* Fill the buffer, but not more than what we can dump out in one go. */
michael@0 136 /* Note we rely on the upsampler to detect bottom of image. */
michael@0 137 max_rows = out_rows_avail - *out_row_ctr;
michael@0 138 if (max_rows > post->strip_height)
michael@0 139 max_rows = post->strip_height;
michael@0 140 num_rows = 0;
michael@0 141 (*cinfo->upsample->upsample) (cinfo,
michael@0 142 input_buf, in_row_group_ctr, in_row_groups_avail,
michael@0 143 post->buffer, &num_rows, max_rows);
michael@0 144 /* Quantize and emit data. */
michael@0 145 (*cinfo->cquantize->color_quantize) (cinfo,
michael@0 146 post->buffer, output_buf + *out_row_ctr, (int) num_rows);
michael@0 147 *out_row_ctr += num_rows;
michael@0 148 }
michael@0 149
michael@0 150
michael@0 151 #ifdef QUANT_2PASS_SUPPORTED
michael@0 152
michael@0 153 /*
michael@0 154 * Process some data in the first pass of 2-pass quantization.
michael@0 155 */
michael@0 156
michael@0 157 METHODDEF(void)
michael@0 158 post_process_prepass (j_decompress_ptr cinfo,
michael@0 159 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
michael@0 160 JDIMENSION in_row_groups_avail,
michael@0 161 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
michael@0 162 JDIMENSION out_rows_avail)
michael@0 163 {
michael@0 164 my_post_ptr post = (my_post_ptr) cinfo->post;
michael@0 165 JDIMENSION old_next_row, num_rows;
michael@0 166
michael@0 167 /* Reposition virtual buffer if at start of strip. */
michael@0 168 if (post->next_row == 0) {
michael@0 169 post->buffer = (*cinfo->mem->access_virt_sarray)
michael@0 170 ((j_common_ptr) cinfo, post->whole_image,
michael@0 171 post->starting_row, post->strip_height, TRUE);
michael@0 172 }
michael@0 173
michael@0 174 /* Upsample some data (up to a strip height's worth). */
michael@0 175 old_next_row = post->next_row;
michael@0 176 (*cinfo->upsample->upsample) (cinfo,
michael@0 177 input_buf, in_row_group_ctr, in_row_groups_avail,
michael@0 178 post->buffer, &post->next_row, post->strip_height);
michael@0 179
michael@0 180 /* Allow quantizer to scan new data. No data is emitted, */
michael@0 181 /* but we advance out_row_ctr so outer loop can tell when we're done. */
michael@0 182 if (post->next_row > old_next_row) {
michael@0 183 num_rows = post->next_row - old_next_row;
michael@0 184 (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row,
michael@0 185 (JSAMPARRAY) NULL, (int) num_rows);
michael@0 186 *out_row_ctr += num_rows;
michael@0 187 }
michael@0 188
michael@0 189 /* Advance if we filled the strip. */
michael@0 190 if (post->next_row >= post->strip_height) {
michael@0 191 post->starting_row += post->strip_height;
michael@0 192 post->next_row = 0;
michael@0 193 }
michael@0 194 }
michael@0 195
michael@0 196
michael@0 197 /*
michael@0 198 * Process some data in the second pass of 2-pass quantization.
michael@0 199 */
michael@0 200
michael@0 201 METHODDEF(void)
michael@0 202 post_process_2pass (j_decompress_ptr cinfo,
michael@0 203 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
michael@0 204 JDIMENSION in_row_groups_avail,
michael@0 205 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
michael@0 206 JDIMENSION out_rows_avail)
michael@0 207 {
michael@0 208 my_post_ptr post = (my_post_ptr) cinfo->post;
michael@0 209 JDIMENSION num_rows, max_rows;
michael@0 210
michael@0 211 /* Reposition virtual buffer if at start of strip. */
michael@0 212 if (post->next_row == 0) {
michael@0 213 post->buffer = (*cinfo->mem->access_virt_sarray)
michael@0 214 ((j_common_ptr) cinfo, post->whole_image,
michael@0 215 post->starting_row, post->strip_height, FALSE);
michael@0 216 }
michael@0 217
michael@0 218 /* Determine number of rows to emit. */
michael@0 219 num_rows = post->strip_height - post->next_row; /* available in strip */
michael@0 220 max_rows = out_rows_avail - *out_row_ctr; /* available in output area */
michael@0 221 if (num_rows > max_rows)
michael@0 222 num_rows = max_rows;
michael@0 223 /* We have to check bottom of image here, can't depend on upsampler. */
michael@0 224 max_rows = cinfo->output_height - post->starting_row;
michael@0 225 if (num_rows > max_rows)
michael@0 226 num_rows = max_rows;
michael@0 227
michael@0 228 /* Quantize and emit data. */
michael@0 229 (*cinfo->cquantize->color_quantize) (cinfo,
michael@0 230 post->buffer + post->next_row, output_buf + *out_row_ctr,
michael@0 231 (int) num_rows);
michael@0 232 *out_row_ctr += num_rows;
michael@0 233
michael@0 234 /* Advance if we filled the strip. */
michael@0 235 post->next_row += num_rows;
michael@0 236 if (post->next_row >= post->strip_height) {
michael@0 237 post->starting_row += post->strip_height;
michael@0 238 post->next_row = 0;
michael@0 239 }
michael@0 240 }
michael@0 241
michael@0 242 #endif /* QUANT_2PASS_SUPPORTED */
michael@0 243
michael@0 244
michael@0 245 /*
michael@0 246 * Initialize postprocessing controller.
michael@0 247 */
michael@0 248
michael@0 249 GLOBAL(void)
michael@0 250 jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
michael@0 251 {
michael@0 252 my_post_ptr post;
michael@0 253
michael@0 254 post = (my_post_ptr)
michael@0 255 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 256 SIZEOF(my_post_controller));
michael@0 257 cinfo->post = (struct jpeg_d_post_controller *) post;
michael@0 258 post->pub.start_pass = start_pass_dpost;
michael@0 259 post->whole_image = NULL; /* flag for no virtual arrays */
michael@0 260 post->buffer = NULL; /* flag for no strip buffer */
michael@0 261
michael@0 262 /* Create the quantization buffer, if needed */
michael@0 263 if (cinfo->quantize_colors) {
michael@0 264 /* The buffer strip height is max_v_samp_factor, which is typically
michael@0 265 * an efficient number of rows for upsampling to return.
michael@0 266 * (In the presence of output rescaling, we might want to be smarter?)
michael@0 267 */
michael@0 268 post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor;
michael@0 269 if (need_full_buffer) {
michael@0 270 /* Two-pass color quantization: need full-image storage. */
michael@0 271 /* We round up the number of rows to a multiple of the strip height. */
michael@0 272 #ifdef QUANT_2PASS_SUPPORTED
michael@0 273 post->whole_image = (*cinfo->mem->request_virt_sarray)
michael@0 274 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
michael@0 275 cinfo->output_width * cinfo->out_color_components,
michael@0 276 (JDIMENSION) jround_up((long) cinfo->output_height,
michael@0 277 (long) post->strip_height),
michael@0 278 post->strip_height);
michael@0 279 #else
michael@0 280 ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
michael@0 281 #endif /* QUANT_2PASS_SUPPORTED */
michael@0 282 } else {
michael@0 283 /* One-pass color quantization: just make a strip buffer. */
michael@0 284 post->buffer = (*cinfo->mem->alloc_sarray)
michael@0 285 ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 286 cinfo->output_width * cinfo->out_color_components,
michael@0 287 post->strip_height);
michael@0 288 }
michael@0 289 }
michael@0 290 }

mercurial