0N/A * reserved comment block 0N/A * DO NOT REMOVE OR ALTER! 0N/A * Copyright (C) 1991-1996, Thomas G. Lane. 0N/A * This file is part of the Independent JPEG Group's software. 0N/A * For conditions of distribution and use, see the accompanying README file. 0N/A * This file contains downsampling routines. 0N/A * Downsampling input data is counted in "row groups". A row group 0N/A * is defined to be max_v_samp_factor pixel rows of each component, 0N/A * from which the downsampler produces v_samp_factor sample rows. 0N/A * A single row group is processed in each call to the downsampler module. 0N/A * The downsampler is responsible for edge-expansion of its output data 0N/A * to fill an integral number of DCT blocks horizontally. The source buffer 0N/A * may be modified if it is helpful for this purpose (the source buffer is 0N/A * allocated wide enough to correspond to the desired output width). 0N/A * The caller (the prep controller) is responsible for vertical padding. 0N/A * The downsampler may request "context rows" by setting need_context_rows 0N/A * during startup. In this case, the input arrays will contain at least 0N/A * one row group's worth of pixels above and below the passed-in data; 0N/A * the caller will create dummy rows at image top and bottom by replicating 0N/A * the first or last real pixel row. 0N/A * An excellent reference for image resampling is 0N/A * Digital Image Warping, George Wolberg, 1990. 0N/A * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7. 0N/A * The downsampling algorithm used here is a simple average of the source 0N/A * pixels covered by the output pixel. The hi-falutin sampling literature 0N/A * refers to this as a "box filter". In general the characteristics of a box 0N/A * filter are not very good, but for the specific cases we normally use (1:1 0N/A * and 2:1 ratios) the box is equivalent to a "triangle filter" which is not 0N/A * nearly so bad. If you intend to use other sampling ratios, you'd be well 0N/A * advised to improve this code. 0N/A * A simple input-smoothing capability is provided. This is mainly intended 0N/A * for cleaning up color-dithered GIF input files (if you find it inadequate, 0N/A * we suggest using an external filtering program such as pnmconvol). When 0N/A * enabled, each input pixel P is replaced by a weighted sum of itself and its 0N/A * eight neighbors. P's weight is 1-8*SF and each neighbor's weight is SF, 0N/A * where SF = (smoothing_factor / 1024). 0N/A * Currently, smoothing is only supported for 2h2v sampling factors. 0N/A/* Pointer to routine to downsample a single component */ 0N/A/* Private subobject */ 0N/A /* Downsampling method pointers, one per component */ 0N/A * Initialize for a downsampling pass. 0N/A /* no work for now */ 0N/A * Expand a component horizontally from width input_cols to width output_cols, 0N/A * by duplicating the rightmost samples. 0N/A * Do downsampling for a whole row group (all components). 0N/A * In this version we simply downsample each component independently. 0N/A * Downsample pixel values of a single component. 0N/A * One row group is processed per call. 0N/A * This version handles arbitrary integral sampling ratios, without smoothing. 0N/A * Note that this version is not actually used for customary sampling ratios. 0N/A /* Expand input data enough to let all the output samples be generated 0N/A * by the standard loop. Special-casing padded output would be more 0N/A * Downsample pixel values of a single component. 0N/A * This version handles the special case of a full-size component, 0N/A * without smoothing. 0N/A * Downsample pixel values of a single component. 0N/A * This version handles the common case of 2:1 horizontal and 1:1 vertical, 0N/A * without smoothing. 0N/A * A note about the "bias" calculations: when rounding fractional values to 0N/A * integer, we do not want to always round 0.5 up to the next integer. 0N/A * If we did that, we'd introduce a noticeable bias towards larger values. 0N/A * Instead, this code is arranged so that 0.5 will be rounded up or down at 0N/A * alternate pixel locations (a simple ordered dither pattern). 0N/A /* Expand input data enough to let all the output samples be generated 0N/A * by the standard loop. Special-casing padded output would be more 0N/A bias = 0;
/* bias = 0,1,0,1,... for successive samples */ 0N/A * Downsample pixel values of a single component. 0N/A * This version handles the standard case of 2:1 horizontal and 2:1 vertical, 0N/A * without smoothing. 0N/A /* Expand input data enough to let all the output samples be generated 0N/A * by the standard loop. Special-casing padded output would be more 0N/A bias =
1;
/* bias = 1,2,1,2,... for successive samples */ 0N/A * Downsample pixel values of a single component. 0N/A * This version handles the standard case of 2:1 horizontal and 2:1 vertical, 0N/A * with smoothing. One row of context is required. 0N/A /* Expand input data enough to let all the output samples be generated 0N/A * by the standard loop. Special-casing padded output would be more 0N/A /* We don't bother to form the individual "smoothed" input pixel values; 0N/A * we can directly compute the output which is the average of the four 0N/A * smoothed values. Each of the four member pixels contributes a fraction 0N/A * (1-8*SF) to its own smoothed image and a fraction SF to each of the three 0N/A * other smoothed pixels, therefore a total fraction (1-5*SF)/4 to the final 0N/A * output. The four corner-adjacent neighbor pixels contribute a fraction 0N/A * SF to just one smoothed pixel, or SF/4 to the final output; while the 0N/A * eight edge-adjacent neighbors contribute SF to each of two smoothed 0N/A * pixels, or SF/2 overall. In order to use integer arithmetic, these 0N/A * factors are scaled by 2^16 = 65536. 0N/A * Also recall that SF = smoothing_factor / 1024. 0N/A /* Special case for first column: pretend column -1 is same as column 0 */ 0N/A /* sum of pixels directly mapped to this output element */ 0N/A /* sum of edge-neighbor pixels */ 0N/A /* The edge-neighbors count twice as much as corner-neighbors */ 0N/A /* Add in the corner-neighbors */ 0N/A /* form final output scaled up by 2^16 */ 0N/A /* round, descale and output it */ 0N/A /* Special case for last column */ 0N/A * Downsample pixel values of a single component. 0N/A * This version handles the special case of a full-size component, 0N/A * with smoothing. One row of context is required. 0N/A /* Expand input data enough to let all the output samples be generated 0N/A * by the standard loop. Special-casing padded output would be more 0N/A /* Each of the eight neighbor pixels contributes a fraction SF to the 0N/A * smoothed pixel, while the main pixel contributes (1-8*SF). In order 0N/A * to use integer arithmetic, these factors are multiplied by 2^16 = 65536. 0N/A * Also recall that SF = smoothing_factor / 1024. 0N/A /* Special case for first column */ 0N/A /* Special case for last column */ 0N/A#
endif /* INPUT_SMOOTHING_SUPPORTED */ 0N/A * Module initialization routine for downsampling. 0N/A * Note that we must select a routine for each component. 0N/A /* Verify we can handle the sampling factors, and set up method pointers */