dbf-halloween2015
diff libs/libjpeg/jdpostct.c @ 1:c3f5c32cb210
barfed all the libraries in the source tree to make porting easier
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 01 Nov 2015 00:36:56 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/libjpeg/jdpostct.c Sun Nov 01 00:36:56 2015 +0200 1.3 @@ -0,0 +1,290 @@ 1.4 +/* 1.5 + * jdpostct.c 1.6 + * 1.7 + * Copyright (C) 1994-1996, Thomas G. Lane. 1.8 + * This file is part of the Independent JPEG Group's software. 1.9 + * For conditions of distribution and use, see the accompanying README file. 1.10 + * 1.11 + * This file contains the decompression postprocessing controller. 1.12 + * This controller manages the upsampling, color conversion, and color 1.13 + * quantization/reduction steps; specifically, it controls the buffering 1.14 + * between upsample/color conversion and color quantization/reduction. 1.15 + * 1.16 + * If no color quantization/reduction is required, then this module has no 1.17 + * work to do, and it just hands off to the upsample/color conversion code. 1.18 + * An integrated upsample/convert/quantize process would replace this module 1.19 + * entirely. 1.20 + */ 1.21 + 1.22 +#define JPEG_INTERNALS 1.23 +#include "jinclude.h" 1.24 +#include "jpeglib.h" 1.25 + 1.26 + 1.27 +/* Private buffer controller object */ 1.28 + 1.29 +typedef struct { 1.30 + struct jpeg_d_post_controller pub; /* public fields */ 1.31 + 1.32 + /* Color quantization source buffer: this holds output data from 1.33 + * the upsample/color conversion step to be passed to the quantizer. 1.34 + * For two-pass color quantization, we need a full-image buffer; 1.35 + * for one-pass operation, a strip buffer is sufficient. 1.36 + */ 1.37 + jvirt_sarray_ptr whole_image; /* virtual array, or NULL if one-pass */ 1.38 + JSAMPARRAY buffer; /* strip buffer, or current strip of virtual */ 1.39 + JDIMENSION strip_height; /* buffer size in rows */ 1.40 + /* for two-pass mode only: */ 1.41 + JDIMENSION starting_row; /* row # of first row in current strip */ 1.42 + JDIMENSION next_row; /* index of next row to fill/empty in strip */ 1.43 +} my_post_controller; 1.44 + 1.45 +typedef my_post_controller * my_post_ptr; 1.46 + 1.47 + 1.48 +/* Forward declarations */ 1.49 +METHODDEF(void) post_process_1pass 1.50 + JPP((j_decompress_ptr cinfo, 1.51 + JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, 1.52 + JDIMENSION in_row_groups_avail, 1.53 + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, 1.54 + JDIMENSION out_rows_avail)); 1.55 +#ifdef QUANT_2PASS_SUPPORTED 1.56 +METHODDEF(void) post_process_prepass 1.57 + JPP((j_decompress_ptr cinfo, 1.58 + JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, 1.59 + JDIMENSION in_row_groups_avail, 1.60 + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, 1.61 + JDIMENSION out_rows_avail)); 1.62 +METHODDEF(void) post_process_2pass 1.63 + JPP((j_decompress_ptr cinfo, 1.64 + JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, 1.65 + JDIMENSION in_row_groups_avail, 1.66 + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, 1.67 + JDIMENSION out_rows_avail)); 1.68 +#endif 1.69 + 1.70 + 1.71 +/* 1.72 + * Initialize for a processing pass. 1.73 + */ 1.74 + 1.75 +METHODDEF(void) 1.76 +start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode) 1.77 +{ 1.78 + my_post_ptr post = (my_post_ptr) cinfo->post; 1.79 + 1.80 + switch (pass_mode) { 1.81 + case JBUF_PASS_THRU: 1.82 + if (cinfo->quantize_colors) { 1.83 + /* Single-pass processing with color quantization. */ 1.84 + post->pub.post_process_data = post_process_1pass; 1.85 + /* We could be doing buffered-image output before starting a 2-pass 1.86 + * color quantization; in that case, jinit_d_post_controller did not 1.87 + * allocate a strip buffer. Use the virtual-array buffer as workspace. 1.88 + */ 1.89 + if (post->buffer == NULL) { 1.90 + post->buffer = (*cinfo->mem->access_virt_sarray) 1.91 + ((j_common_ptr) cinfo, post->whole_image, 1.92 + (JDIMENSION) 0, post->strip_height, TRUE); 1.93 + } 1.94 + } else { 1.95 + /* For single-pass processing without color quantization, 1.96 + * I have no work to do; just call the upsampler directly. 1.97 + */ 1.98 + post->pub.post_process_data = cinfo->upsample->upsample; 1.99 + } 1.100 + break; 1.101 +#ifdef QUANT_2PASS_SUPPORTED 1.102 + case JBUF_SAVE_AND_PASS: 1.103 + /* First pass of 2-pass quantization */ 1.104 + if (post->whole_image == NULL) 1.105 + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); 1.106 + post->pub.post_process_data = post_process_prepass; 1.107 + break; 1.108 + case JBUF_CRANK_DEST: 1.109 + /* Second pass of 2-pass quantization */ 1.110 + if (post->whole_image == NULL) 1.111 + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); 1.112 + post->pub.post_process_data = post_process_2pass; 1.113 + break; 1.114 +#endif /* QUANT_2PASS_SUPPORTED */ 1.115 + default: 1.116 + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); 1.117 + break; 1.118 + } 1.119 + post->starting_row = post->next_row = 0; 1.120 +} 1.121 + 1.122 + 1.123 +/* 1.124 + * Process some data in the one-pass (strip buffer) case. 1.125 + * This is used for color precision reduction as well as one-pass quantization. 1.126 + */ 1.127 + 1.128 +METHODDEF(void) 1.129 +post_process_1pass (j_decompress_ptr cinfo, 1.130 + JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, 1.131 + JDIMENSION in_row_groups_avail, 1.132 + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, 1.133 + JDIMENSION out_rows_avail) 1.134 +{ 1.135 + my_post_ptr post = (my_post_ptr) cinfo->post; 1.136 + JDIMENSION num_rows, max_rows; 1.137 + 1.138 + /* Fill the buffer, but not more than what we can dump out in one go. */ 1.139 + /* Note we rely on the upsampler to detect bottom of image. */ 1.140 + max_rows = out_rows_avail - *out_row_ctr; 1.141 + if (max_rows > post->strip_height) 1.142 + max_rows = post->strip_height; 1.143 + num_rows = 0; 1.144 + (*cinfo->upsample->upsample) (cinfo, 1.145 + input_buf, in_row_group_ctr, in_row_groups_avail, 1.146 + post->buffer, &num_rows, max_rows); 1.147 + /* Quantize and emit data. */ 1.148 + (*cinfo->cquantize->color_quantize) (cinfo, 1.149 + post->buffer, output_buf + *out_row_ctr, (int) num_rows); 1.150 + *out_row_ctr += num_rows; 1.151 +} 1.152 + 1.153 + 1.154 +#ifdef QUANT_2PASS_SUPPORTED 1.155 + 1.156 +/* 1.157 + * Process some data in the first pass of 2-pass quantization. 1.158 + */ 1.159 + 1.160 +METHODDEF(void) 1.161 +post_process_prepass (j_decompress_ptr cinfo, 1.162 + JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, 1.163 + JDIMENSION in_row_groups_avail, 1.164 + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, 1.165 + JDIMENSION out_rows_avail) 1.166 +{ 1.167 + my_post_ptr post = (my_post_ptr) cinfo->post; 1.168 + JDIMENSION old_next_row, num_rows; 1.169 + 1.170 + /* Reposition virtual buffer if at start of strip. */ 1.171 + if (post->next_row == 0) { 1.172 + post->buffer = (*cinfo->mem->access_virt_sarray) 1.173 + ((j_common_ptr) cinfo, post->whole_image, 1.174 + post->starting_row, post->strip_height, TRUE); 1.175 + } 1.176 + 1.177 + /* Upsample some data (up to a strip height's worth). */ 1.178 + old_next_row = post->next_row; 1.179 + (*cinfo->upsample->upsample) (cinfo, 1.180 + input_buf, in_row_group_ctr, in_row_groups_avail, 1.181 + post->buffer, &post->next_row, post->strip_height); 1.182 + 1.183 + /* Allow quantizer to scan new data. No data is emitted, */ 1.184 + /* but we advance out_row_ctr so outer loop can tell when we're done. */ 1.185 + if (post->next_row > old_next_row) { 1.186 + num_rows = post->next_row - old_next_row; 1.187 + (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row, 1.188 + (JSAMPARRAY) NULL, (int) num_rows); 1.189 + *out_row_ctr += num_rows; 1.190 + } 1.191 + 1.192 + /* Advance if we filled the strip. */ 1.193 + if (post->next_row >= post->strip_height) { 1.194 + post->starting_row += post->strip_height; 1.195 + post->next_row = 0; 1.196 + } 1.197 +} 1.198 + 1.199 + 1.200 +/* 1.201 + * Process some data in the second pass of 2-pass quantization. 1.202 + */ 1.203 + 1.204 +METHODDEF(void) 1.205 +post_process_2pass (j_decompress_ptr cinfo, 1.206 + JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, 1.207 + JDIMENSION in_row_groups_avail, 1.208 + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, 1.209 + JDIMENSION out_rows_avail) 1.210 +{ 1.211 + my_post_ptr post = (my_post_ptr) cinfo->post; 1.212 + JDIMENSION num_rows, max_rows; 1.213 + 1.214 + /* Reposition virtual buffer if at start of strip. */ 1.215 + if (post->next_row == 0) { 1.216 + post->buffer = (*cinfo->mem->access_virt_sarray) 1.217 + ((j_common_ptr) cinfo, post->whole_image, 1.218 + post->starting_row, post->strip_height, FALSE); 1.219 + } 1.220 + 1.221 + /* Determine number of rows to emit. */ 1.222 + num_rows = post->strip_height - post->next_row; /* available in strip */ 1.223 + max_rows = out_rows_avail - *out_row_ctr; /* available in output area */ 1.224 + if (num_rows > max_rows) 1.225 + num_rows = max_rows; 1.226 + /* We have to check bottom of image here, can't depend on upsampler. */ 1.227 + max_rows = cinfo->output_height - post->starting_row; 1.228 + if (num_rows > max_rows) 1.229 + num_rows = max_rows; 1.230 + 1.231 + /* Quantize and emit data. */ 1.232 + (*cinfo->cquantize->color_quantize) (cinfo, 1.233 + post->buffer + post->next_row, output_buf + *out_row_ctr, 1.234 + (int) num_rows); 1.235 + *out_row_ctr += num_rows; 1.236 + 1.237 + /* Advance if we filled the strip. */ 1.238 + post->next_row += num_rows; 1.239 + if (post->next_row >= post->strip_height) { 1.240 + post->starting_row += post->strip_height; 1.241 + post->next_row = 0; 1.242 + } 1.243 +} 1.244 + 1.245 +#endif /* QUANT_2PASS_SUPPORTED */ 1.246 + 1.247 + 1.248 +/* 1.249 + * Initialize postprocessing controller. 1.250 + */ 1.251 + 1.252 +GLOBAL(void) 1.253 +jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer) 1.254 +{ 1.255 + my_post_ptr post; 1.256 + 1.257 + post = (my_post_ptr) 1.258 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.259 + SIZEOF(my_post_controller)); 1.260 + cinfo->post = (struct jpeg_d_post_controller *) post; 1.261 + post->pub.start_pass = start_pass_dpost; 1.262 + post->whole_image = NULL; /* flag for no virtual arrays */ 1.263 + post->buffer = NULL; /* flag for no strip buffer */ 1.264 + 1.265 + /* Create the quantization buffer, if needed */ 1.266 + if (cinfo->quantize_colors) { 1.267 + /* The buffer strip height is max_v_samp_factor, which is typically 1.268 + * an efficient number of rows for upsampling to return. 1.269 + * (In the presence of output rescaling, we might want to be smarter?) 1.270 + */ 1.271 + post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor; 1.272 + if (need_full_buffer) { 1.273 + /* Two-pass color quantization: need full-image storage. */ 1.274 + /* We round up the number of rows to a multiple of the strip height. */ 1.275 +#ifdef QUANT_2PASS_SUPPORTED 1.276 + post->whole_image = (*cinfo->mem->request_virt_sarray) 1.277 + ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE, 1.278 + cinfo->output_width * cinfo->out_color_components, 1.279 + (JDIMENSION) jround_up((long) cinfo->output_height, 1.280 + (long) post->strip_height), 1.281 + post->strip_height); 1.282 +#else 1.283 + ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); 1.284 +#endif /* QUANT_2PASS_SUPPORTED */ 1.285 + } else { 1.286 + /* One-pass color quantization: just make a strip buffer. */ 1.287 + post->buffer = (*cinfo->mem->alloc_sarray) 1.288 + ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.289 + cinfo->output_width * cinfo->out_color_components, 1.290 + post->strip_height); 1.291 + } 1.292 + } 1.293 +}