dbf-halloween2015
diff libs/libjpeg/jdsample.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/jdsample.c Sun Nov 01 00:36:56 2015 +0200 1.3 @@ -0,0 +1,478 @@ 1.4 +/* 1.5 + * jdsample.c 1.6 + * 1.7 + * Copyright (C) 1991-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 upsampling routines. 1.12 + * 1.13 + * Upsampling input data is counted in "row groups". A row group 1.14 + * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size) 1.15 + * sample rows of each component. Upsampling will normally produce 1.16 + * max_v_samp_factor pixel rows from each row group (but this could vary 1.17 + * if the upsampler is applying a scale factor of its own). 1.18 + * 1.19 + * An excellent reference for image resampling is 1.20 + * Digital Image Warping, George Wolberg, 1990. 1.21 + * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7. 1.22 + */ 1.23 + 1.24 +#define JPEG_INTERNALS 1.25 +#include "jinclude.h" 1.26 +#include "jpeglib.h" 1.27 + 1.28 + 1.29 +/* Pointer to routine to upsample a single component */ 1.30 +typedef JMETHOD(void, upsample1_ptr, 1.31 + (j_decompress_ptr cinfo, jpeg_component_info * compptr, 1.32 + JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)); 1.33 + 1.34 +/* Private subobject */ 1.35 + 1.36 +typedef struct { 1.37 + struct jpeg_upsampler pub; /* public fields */ 1.38 + 1.39 + /* Color conversion buffer. When using separate upsampling and color 1.40 + * conversion steps, this buffer holds one upsampled row group until it 1.41 + * has been color converted and output. 1.42 + * Note: we do not allocate any storage for component(s) which are full-size, 1.43 + * ie do not need rescaling. The corresponding entry of color_buf[] is 1.44 + * simply set to point to the input data array, thereby avoiding copying. 1.45 + */ 1.46 + JSAMPARRAY color_buf[MAX_COMPONENTS]; 1.47 + 1.48 + /* Per-component upsampling method pointers */ 1.49 + upsample1_ptr methods[MAX_COMPONENTS]; 1.50 + 1.51 + int next_row_out; /* counts rows emitted from color_buf */ 1.52 + JDIMENSION rows_to_go; /* counts rows remaining in image */ 1.53 + 1.54 + /* Height of an input row group for each component. */ 1.55 + int rowgroup_height[MAX_COMPONENTS]; 1.56 + 1.57 + /* These arrays save pixel expansion factors so that int_expand need not 1.58 + * recompute them each time. They are unused for other upsampling methods. 1.59 + */ 1.60 + UINT8 h_expand[MAX_COMPONENTS]; 1.61 + UINT8 v_expand[MAX_COMPONENTS]; 1.62 +} my_upsampler; 1.63 + 1.64 +typedef my_upsampler * my_upsample_ptr; 1.65 + 1.66 + 1.67 +/* 1.68 + * Initialize for an upsampling pass. 1.69 + */ 1.70 + 1.71 +METHODDEF(void) 1.72 +start_pass_upsample (j_decompress_ptr cinfo) 1.73 +{ 1.74 + my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; 1.75 + 1.76 + /* Mark the conversion buffer empty */ 1.77 + upsample->next_row_out = cinfo->max_v_samp_factor; 1.78 + /* Initialize total-height counter for detecting bottom of image */ 1.79 + upsample->rows_to_go = cinfo->output_height; 1.80 +} 1.81 + 1.82 + 1.83 +/* 1.84 + * Control routine to do upsampling (and color conversion). 1.85 + * 1.86 + * In this version we upsample each component independently. 1.87 + * We upsample one row group into the conversion buffer, then apply 1.88 + * color conversion a row at a time. 1.89 + */ 1.90 + 1.91 +METHODDEF(void) 1.92 +sep_upsample (j_decompress_ptr cinfo, 1.93 + JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, 1.94 + JDIMENSION in_row_groups_avail, 1.95 + JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, 1.96 + JDIMENSION out_rows_avail) 1.97 +{ 1.98 + my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; 1.99 + int ci; 1.100 + jpeg_component_info * compptr; 1.101 + JDIMENSION num_rows; 1.102 + 1.103 + /* Fill the conversion buffer, if it's empty */ 1.104 + if (upsample->next_row_out >= cinfo->max_v_samp_factor) { 1.105 + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 1.106 + ci++, compptr++) { 1.107 + /* Invoke per-component upsample method. Notice we pass a POINTER 1.108 + * to color_buf[ci], so that fullsize_upsample can change it. 1.109 + */ 1.110 + (*upsample->methods[ci]) (cinfo, compptr, 1.111 + input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]), 1.112 + upsample->color_buf + ci); 1.113 + } 1.114 + upsample->next_row_out = 0; 1.115 + } 1.116 + 1.117 + /* Color-convert and emit rows */ 1.118 + 1.119 + /* How many we have in the buffer: */ 1.120 + num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out); 1.121 + /* Not more than the distance to the end of the image. Need this test 1.122 + * in case the image height is not a multiple of max_v_samp_factor: 1.123 + */ 1.124 + if (num_rows > upsample->rows_to_go) 1.125 + num_rows = upsample->rows_to_go; 1.126 + /* And not more than what the client can accept: */ 1.127 + out_rows_avail -= *out_row_ctr; 1.128 + if (num_rows > out_rows_avail) 1.129 + num_rows = out_rows_avail; 1.130 + 1.131 + (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf, 1.132 + (JDIMENSION) upsample->next_row_out, 1.133 + output_buf + *out_row_ctr, 1.134 + (int) num_rows); 1.135 + 1.136 + /* Adjust counts */ 1.137 + *out_row_ctr += num_rows; 1.138 + upsample->rows_to_go -= num_rows; 1.139 + upsample->next_row_out += num_rows; 1.140 + /* When the buffer is emptied, declare this input row group consumed */ 1.141 + if (upsample->next_row_out >= cinfo->max_v_samp_factor) 1.142 + (*in_row_group_ctr)++; 1.143 +} 1.144 + 1.145 + 1.146 +/* 1.147 + * These are the routines invoked by sep_upsample to upsample pixel values 1.148 + * of a single component. One row group is processed per call. 1.149 + */ 1.150 + 1.151 + 1.152 +/* 1.153 + * For full-size components, we just make color_buf[ci] point at the 1.154 + * input buffer, and thus avoid copying any data. Note that this is 1.155 + * safe only because sep_upsample doesn't declare the input row group 1.156 + * "consumed" until we are done color converting and emitting it. 1.157 + */ 1.158 + 1.159 +METHODDEF(void) 1.160 +fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, 1.161 + JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) 1.162 +{ 1.163 + *output_data_ptr = input_data; 1.164 +} 1.165 + 1.166 + 1.167 +/* 1.168 + * This is a no-op version used for "uninteresting" components. 1.169 + * These components will not be referenced by color conversion. 1.170 + */ 1.171 + 1.172 +METHODDEF(void) 1.173 +noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, 1.174 + JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) 1.175 +{ 1.176 + *output_data_ptr = NULL; /* safety check */ 1.177 +} 1.178 + 1.179 + 1.180 +/* 1.181 + * This version handles any integral sampling ratios. 1.182 + * This is not used for typical JPEG files, so it need not be fast. 1.183 + * Nor, for that matter, is it particularly accurate: the algorithm is 1.184 + * simple replication of the input pixel onto the corresponding output 1.185 + * pixels. The hi-falutin sampling literature refers to this as a 1.186 + * "box filter". A box filter tends to introduce visible artifacts, 1.187 + * so if you are actually going to use 3:1 or 4:1 sampling ratios 1.188 + * you would be well advised to improve this code. 1.189 + */ 1.190 + 1.191 +METHODDEF(void) 1.192 +int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, 1.193 + JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) 1.194 +{ 1.195 + my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; 1.196 + JSAMPARRAY output_data = *output_data_ptr; 1.197 + register JSAMPROW inptr, outptr; 1.198 + register JSAMPLE invalue; 1.199 + register int h; 1.200 + JSAMPROW outend; 1.201 + int h_expand, v_expand; 1.202 + int inrow, outrow; 1.203 + 1.204 + h_expand = upsample->h_expand[compptr->component_index]; 1.205 + v_expand = upsample->v_expand[compptr->component_index]; 1.206 + 1.207 + inrow = outrow = 0; 1.208 + while (outrow < cinfo->max_v_samp_factor) { 1.209 + /* Generate one output row with proper horizontal expansion */ 1.210 + inptr = input_data[inrow]; 1.211 + outptr = output_data[outrow]; 1.212 + outend = outptr + cinfo->output_width; 1.213 + while (outptr < outend) { 1.214 + invalue = *inptr++; /* don't need GETJSAMPLE() here */ 1.215 + for (h = h_expand; h > 0; h--) { 1.216 + *outptr++ = invalue; 1.217 + } 1.218 + } 1.219 + /* Generate any additional output rows by duplicating the first one */ 1.220 + if (v_expand > 1) { 1.221 + jcopy_sample_rows(output_data, outrow, output_data, outrow+1, 1.222 + v_expand-1, cinfo->output_width); 1.223 + } 1.224 + inrow++; 1.225 + outrow += v_expand; 1.226 + } 1.227 +} 1.228 + 1.229 + 1.230 +/* 1.231 + * Fast processing for the common case of 2:1 horizontal and 1:1 vertical. 1.232 + * It's still a box filter. 1.233 + */ 1.234 + 1.235 +METHODDEF(void) 1.236 +h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, 1.237 + JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) 1.238 +{ 1.239 + JSAMPARRAY output_data = *output_data_ptr; 1.240 + register JSAMPROW inptr, outptr; 1.241 + register JSAMPLE invalue; 1.242 + JSAMPROW outend; 1.243 + int inrow; 1.244 + 1.245 + for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) { 1.246 + inptr = input_data[inrow]; 1.247 + outptr = output_data[inrow]; 1.248 + outend = outptr + cinfo->output_width; 1.249 + while (outptr < outend) { 1.250 + invalue = *inptr++; /* don't need GETJSAMPLE() here */ 1.251 + *outptr++ = invalue; 1.252 + *outptr++ = invalue; 1.253 + } 1.254 + } 1.255 +} 1.256 + 1.257 + 1.258 +/* 1.259 + * Fast processing for the common case of 2:1 horizontal and 2:1 vertical. 1.260 + * It's still a box filter. 1.261 + */ 1.262 + 1.263 +METHODDEF(void) 1.264 +h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, 1.265 + JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) 1.266 +{ 1.267 + JSAMPARRAY output_data = *output_data_ptr; 1.268 + register JSAMPROW inptr, outptr; 1.269 + register JSAMPLE invalue; 1.270 + JSAMPROW outend; 1.271 + int inrow, outrow; 1.272 + 1.273 + inrow = outrow = 0; 1.274 + while (outrow < cinfo->max_v_samp_factor) { 1.275 + inptr = input_data[inrow]; 1.276 + outptr = output_data[outrow]; 1.277 + outend = outptr + cinfo->output_width; 1.278 + while (outptr < outend) { 1.279 + invalue = *inptr++; /* don't need GETJSAMPLE() here */ 1.280 + *outptr++ = invalue; 1.281 + *outptr++ = invalue; 1.282 + } 1.283 + jcopy_sample_rows(output_data, outrow, output_data, outrow+1, 1.284 + 1, cinfo->output_width); 1.285 + inrow++; 1.286 + outrow += 2; 1.287 + } 1.288 +} 1.289 + 1.290 + 1.291 +/* 1.292 + * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical. 1.293 + * 1.294 + * The upsampling algorithm is linear interpolation between pixel centers, 1.295 + * also known as a "triangle filter". This is a good compromise between 1.296 + * speed and visual quality. The centers of the output pixels are 1/4 and 3/4 1.297 + * of the way between input pixel centers. 1.298 + * 1.299 + * A note about the "bias" calculations: when rounding fractional values to 1.300 + * integer, we do not want to always round 0.5 up to the next integer. 1.301 + * If we did that, we'd introduce a noticeable bias towards larger values. 1.302 + * Instead, this code is arranged so that 0.5 will be rounded up or down at 1.303 + * alternate pixel locations (a simple ordered dither pattern). 1.304 + */ 1.305 + 1.306 +METHODDEF(void) 1.307 +h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, 1.308 + JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) 1.309 +{ 1.310 + JSAMPARRAY output_data = *output_data_ptr; 1.311 + register JSAMPROW inptr, outptr; 1.312 + register int invalue; 1.313 + register JDIMENSION colctr; 1.314 + int inrow; 1.315 + 1.316 + for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) { 1.317 + inptr = input_data[inrow]; 1.318 + outptr = output_data[inrow]; 1.319 + /* Special case for first column */ 1.320 + invalue = GETJSAMPLE(*inptr++); 1.321 + *outptr++ = (JSAMPLE) invalue; 1.322 + *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2); 1.323 + 1.324 + for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) { 1.325 + /* General case: 3/4 * nearer pixel + 1/4 * further pixel */ 1.326 + invalue = GETJSAMPLE(*inptr++) * 3; 1.327 + *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2); 1.328 + *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2); 1.329 + } 1.330 + 1.331 + /* Special case for last column */ 1.332 + invalue = GETJSAMPLE(*inptr); 1.333 + *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2); 1.334 + *outptr++ = (JSAMPLE) invalue; 1.335 + } 1.336 +} 1.337 + 1.338 + 1.339 +/* 1.340 + * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical. 1.341 + * Again a triangle filter; see comments for h2v1 case, above. 1.342 + * 1.343 + * It is OK for us to reference the adjacent input rows because we demanded 1.344 + * context from the main buffer controller (see initialization code). 1.345 + */ 1.346 + 1.347 +METHODDEF(void) 1.348 +h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, 1.349 + JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) 1.350 +{ 1.351 + JSAMPARRAY output_data = *output_data_ptr; 1.352 + register JSAMPROW inptr0, inptr1, outptr; 1.353 +#if BITS_IN_JSAMPLE == 8 1.354 + register int thiscolsum, lastcolsum, nextcolsum; 1.355 +#else 1.356 + register INT32 thiscolsum, lastcolsum, nextcolsum; 1.357 +#endif 1.358 + register JDIMENSION colctr; 1.359 + int inrow, outrow, v; 1.360 + 1.361 + inrow = outrow = 0; 1.362 + while (outrow < cinfo->max_v_samp_factor) { 1.363 + for (v = 0; v < 2; v++) { 1.364 + /* inptr0 points to nearest input row, inptr1 points to next nearest */ 1.365 + inptr0 = input_data[inrow]; 1.366 + if (v == 0) /* next nearest is row above */ 1.367 + inptr1 = input_data[inrow-1]; 1.368 + else /* next nearest is row below */ 1.369 + inptr1 = input_data[inrow+1]; 1.370 + outptr = output_data[outrow++]; 1.371 + 1.372 + /* Special case for first column */ 1.373 + thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++); 1.374 + nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++); 1.375 + *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4); 1.376 + *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4); 1.377 + lastcolsum = thiscolsum; thiscolsum = nextcolsum; 1.378 + 1.379 + for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) { 1.380 + /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */ 1.381 + /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */ 1.382 + nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++); 1.383 + *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4); 1.384 + *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4); 1.385 + lastcolsum = thiscolsum; thiscolsum = nextcolsum; 1.386 + } 1.387 + 1.388 + /* Special case for last column */ 1.389 + *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4); 1.390 + *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4); 1.391 + } 1.392 + inrow++; 1.393 + } 1.394 +} 1.395 + 1.396 + 1.397 +/* 1.398 + * Module initialization routine for upsampling. 1.399 + */ 1.400 + 1.401 +GLOBAL(void) 1.402 +jinit_upsampler (j_decompress_ptr cinfo) 1.403 +{ 1.404 + my_upsample_ptr upsample; 1.405 + int ci; 1.406 + jpeg_component_info * compptr; 1.407 + boolean need_buffer, do_fancy; 1.408 + int h_in_group, v_in_group, h_out_group, v_out_group; 1.409 + 1.410 + upsample = (my_upsample_ptr) 1.411 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.412 + SIZEOF(my_upsampler)); 1.413 + cinfo->upsample = (struct jpeg_upsampler *) upsample; 1.414 + upsample->pub.start_pass = start_pass_upsample; 1.415 + upsample->pub.upsample = sep_upsample; 1.416 + upsample->pub.need_context_rows = FALSE; /* until we find out differently */ 1.417 + 1.418 + if (cinfo->CCIR601_sampling) /* this isn't supported */ 1.419 + ERREXIT(cinfo, JERR_CCIR601_NOTIMPL); 1.420 + 1.421 + /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1, 1.422 + * so don't ask for it. 1.423 + */ 1.424 + do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1; 1.425 + 1.426 + /* Verify we can handle the sampling factors, select per-component methods, 1.427 + * and create storage as needed. 1.428 + */ 1.429 + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 1.430 + ci++, compptr++) { 1.431 + /* Compute size of an "input group" after IDCT scaling. This many samples 1.432 + * are to be converted to max_h_samp_factor * max_v_samp_factor pixels. 1.433 + */ 1.434 + h_in_group = (compptr->h_samp_factor * compptr->DCT_scaled_size) / 1.435 + cinfo->min_DCT_scaled_size; 1.436 + v_in_group = (compptr->v_samp_factor * compptr->DCT_scaled_size) / 1.437 + cinfo->min_DCT_scaled_size; 1.438 + h_out_group = cinfo->max_h_samp_factor; 1.439 + v_out_group = cinfo->max_v_samp_factor; 1.440 + upsample->rowgroup_height[ci] = v_in_group; /* save for use later */ 1.441 + need_buffer = TRUE; 1.442 + if (! compptr->component_needed) { 1.443 + /* Don't bother to upsample an uninteresting component. */ 1.444 + upsample->methods[ci] = noop_upsample; 1.445 + need_buffer = FALSE; 1.446 + } else if (h_in_group == h_out_group && v_in_group == v_out_group) { 1.447 + /* Fullsize components can be processed without any work. */ 1.448 + upsample->methods[ci] = fullsize_upsample; 1.449 + need_buffer = FALSE; 1.450 + } else if (h_in_group * 2 == h_out_group && 1.451 + v_in_group == v_out_group) { 1.452 + /* Special cases for 2h1v upsampling */ 1.453 + if (do_fancy && compptr->downsampled_width > 2) 1.454 + upsample->methods[ci] = h2v1_fancy_upsample; 1.455 + else 1.456 + upsample->methods[ci] = h2v1_upsample; 1.457 + } else if (h_in_group * 2 == h_out_group && 1.458 + v_in_group * 2 == v_out_group) { 1.459 + /* Special cases for 2h2v upsampling */ 1.460 + if (do_fancy && compptr->downsampled_width > 2) { 1.461 + upsample->methods[ci] = h2v2_fancy_upsample; 1.462 + upsample->pub.need_context_rows = TRUE; 1.463 + } else 1.464 + upsample->methods[ci] = h2v2_upsample; 1.465 + } else if ((h_out_group % h_in_group) == 0 && 1.466 + (v_out_group % v_in_group) == 0) { 1.467 + /* Generic integral-factors upsampling method */ 1.468 + upsample->methods[ci] = int_upsample; 1.469 + upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group); 1.470 + upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group); 1.471 + } else 1.472 + ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL); 1.473 + if (need_buffer) { 1.474 + upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray) 1.475 + ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.476 + (JDIMENSION) jround_up((long) cinfo->output_width, 1.477 + (long) cinfo->max_h_samp_factor), 1.478 + (JDIMENSION) cinfo->max_v_samp_factor); 1.479 + } 1.480 + } 1.481 +}