nuclear@2: /* nuclear@2: * jcsample.c nuclear@2: * nuclear@2: * Copyright (C) 1991-1996, Thomas G. Lane. nuclear@2: * This file is part of the Independent JPEG Group's software. nuclear@2: * For conditions of distribution and use, see the accompanying README file. nuclear@2: * nuclear@2: * This file contains downsampling routines. nuclear@2: * nuclear@2: * Downsampling input data is counted in "row groups". A row group nuclear@2: * is defined to be max_v_samp_factor pixel rows of each component, nuclear@2: * from which the downsampler produces v_samp_factor sample rows. nuclear@2: * A single row group is processed in each call to the downsampler module. nuclear@2: * nuclear@2: * The downsampler is responsible for edge-expansion of its output data nuclear@2: * to fill an integral number of DCT blocks horizontally. The source buffer nuclear@2: * may be modified if it is helpful for this purpose (the source buffer is nuclear@2: * allocated wide enough to correspond to the desired output width). nuclear@2: * The caller (the prep controller) is responsible for vertical padding. nuclear@2: * nuclear@2: * The downsampler may request "context rows" by setting need_context_rows nuclear@2: * during startup. In this case, the input arrays will contain at least nuclear@2: * one row group's worth of pixels above and below the passed-in data; nuclear@2: * the caller will create dummy rows at image top and bottom by replicating nuclear@2: * the first or last real pixel row. nuclear@2: * nuclear@2: * An excellent reference for image resampling is nuclear@2: * Digital Image Warping, George Wolberg, 1990. nuclear@2: * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7. nuclear@2: * nuclear@2: * The downsampling algorithm used here is a simple average of the source nuclear@2: * pixels covered by the output pixel. The hi-falutin sampling literature nuclear@2: * refers to this as a "box filter". In general the characteristics of a box nuclear@2: * filter are not very good, but for the specific cases we normally use (1:1 nuclear@2: * and 2:1 ratios) the box is equivalent to a "triangle filter" which is not nuclear@2: * nearly so bad. If you intend to use other sampling ratios, you'd be well nuclear@2: * advised to improve this code. nuclear@2: * nuclear@2: * A simple input-smoothing capability is provided. This is mainly intended nuclear@2: * for cleaning up color-dithered GIF input files (if you find it inadequate, nuclear@2: * we suggest using an external filtering program such as pnmconvol). When nuclear@2: * enabled, each input pixel P is replaced by a weighted sum of itself and its nuclear@2: * eight neighbors. P's weight is 1-8*SF and each neighbor's weight is SF, nuclear@2: * where SF = (smoothing_factor / 1024). nuclear@2: * Currently, smoothing is only supported for 2h2v sampling factors. nuclear@2: */ nuclear@2: nuclear@2: #define JPEG_INTERNALS nuclear@2: #include "jinclude.h" nuclear@2: #include "jpeglib.h" nuclear@2: nuclear@2: nuclear@2: /* Pointer to routine to downsample a single component */ nuclear@2: typedef JMETHOD(void, downsample1_ptr, nuclear@2: (j_compress_ptr cinfo, jpeg_component_info * compptr, nuclear@2: JSAMPARRAY input_data, JSAMPARRAY output_data)); nuclear@2: nuclear@2: /* Private subobject */ nuclear@2: nuclear@2: typedef struct { nuclear@2: struct jpeg_downsampler pub; /* public fields */ nuclear@2: nuclear@2: /* Downsampling method pointers, one per component */ nuclear@2: downsample1_ptr methods[MAX_COMPONENTS]; nuclear@2: } my_downsampler; nuclear@2: nuclear@2: typedef my_downsampler * my_downsample_ptr; nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Initialize for a downsampling pass. nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: start_pass_downsample (j_compress_ptr cinfo) nuclear@2: { nuclear@2: /* no work for now */ nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Expand a component horizontally from width input_cols to width output_cols, nuclear@2: * by duplicating the rightmost samples. nuclear@2: */ nuclear@2: nuclear@2: LOCAL(void) nuclear@2: expand_right_edge (JSAMPARRAY image_data, int num_rows, nuclear@2: JDIMENSION input_cols, JDIMENSION output_cols) nuclear@2: { nuclear@2: register JSAMPROW ptr; nuclear@2: register JSAMPLE pixval; nuclear@2: register int count; nuclear@2: int row; nuclear@2: int numcols = (int) (output_cols - input_cols); nuclear@2: nuclear@2: if (numcols > 0) { nuclear@2: for (row = 0; row < num_rows; row++) { nuclear@2: ptr = image_data[row] + input_cols; nuclear@2: pixval = ptr[-1]; /* don't need GETJSAMPLE() here */ nuclear@2: for (count = numcols; count > 0; count--) nuclear@2: *ptr++ = pixval; nuclear@2: } nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Do downsampling for a whole row group (all components). nuclear@2: * nuclear@2: * In this version we simply downsample each component independently. nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: sep_downsample (j_compress_ptr cinfo, nuclear@2: JSAMPIMAGE input_buf, JDIMENSION in_row_index, nuclear@2: JSAMPIMAGE output_buf, JDIMENSION out_row_group_index) nuclear@2: { nuclear@2: my_downsample_ptr downsample = (my_downsample_ptr) cinfo->downsample; nuclear@2: int ci; nuclear@2: jpeg_component_info * compptr; nuclear@2: JSAMPARRAY in_ptr, out_ptr; nuclear@2: nuclear@2: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; nuclear@2: ci++, compptr++) { nuclear@2: in_ptr = input_buf[ci] + in_row_index; nuclear@2: out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor); nuclear@2: (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr); nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Downsample pixel values of a single component. nuclear@2: * One row group is processed per call. nuclear@2: * This version handles arbitrary integral sampling ratios, without smoothing. nuclear@2: * Note that this version is not actually used for customary sampling ratios. nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: int_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, nuclear@2: JSAMPARRAY input_data, JSAMPARRAY output_data) nuclear@2: { nuclear@2: int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v; nuclear@2: JDIMENSION outcol, outcol_h; /* outcol_h == outcol*h_expand */ nuclear@2: JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE; nuclear@2: JSAMPROW inptr, outptr; nuclear@2: INT32 outvalue; nuclear@2: nuclear@2: h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor; nuclear@2: v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor; nuclear@2: numpix = h_expand * v_expand; nuclear@2: numpix2 = numpix/2; nuclear@2: nuclear@2: /* Expand input data enough to let all the output samples be generated nuclear@2: * by the standard loop. Special-casing padded output would be more nuclear@2: * efficient. nuclear@2: */ nuclear@2: expand_right_edge(input_data, cinfo->max_v_samp_factor, nuclear@2: cinfo->image_width, output_cols * h_expand); nuclear@2: nuclear@2: inrow = 0; nuclear@2: for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) { nuclear@2: outptr = output_data[outrow]; nuclear@2: for (outcol = 0, outcol_h = 0; outcol < output_cols; nuclear@2: outcol++, outcol_h += h_expand) { nuclear@2: outvalue = 0; nuclear@2: for (v = 0; v < v_expand; v++) { nuclear@2: inptr = input_data[inrow+v] + outcol_h; nuclear@2: for (h = 0; h < h_expand; h++) { nuclear@2: outvalue += (INT32) GETJSAMPLE(*inptr++); nuclear@2: } nuclear@2: } nuclear@2: *outptr++ = (JSAMPLE) ((outvalue + numpix2) / numpix); nuclear@2: } nuclear@2: inrow += v_expand; nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Downsample pixel values of a single component. nuclear@2: * This version handles the special case of a full-size component, nuclear@2: * without smoothing. nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: fullsize_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, nuclear@2: JSAMPARRAY input_data, JSAMPARRAY output_data) nuclear@2: { nuclear@2: /* Copy the data */ nuclear@2: jcopy_sample_rows(input_data, 0, output_data, 0, nuclear@2: cinfo->max_v_samp_factor, cinfo->image_width); nuclear@2: /* Edge-expand */ nuclear@2: expand_right_edge(output_data, cinfo->max_v_samp_factor, nuclear@2: cinfo->image_width, compptr->width_in_blocks * DCTSIZE); nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Downsample pixel values of a single component. nuclear@2: * This version handles the common case of 2:1 horizontal and 1:1 vertical, nuclear@2: * without smoothing. nuclear@2: * nuclear@2: * A note about the "bias" calculations: when rounding fractional values to nuclear@2: * integer, we do not want to always round 0.5 up to the next integer. nuclear@2: * If we did that, we'd introduce a noticeable bias towards larger values. nuclear@2: * Instead, this code is arranged so that 0.5 will be rounded up or down at nuclear@2: * alternate pixel locations (a simple ordered dither pattern). nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, nuclear@2: JSAMPARRAY input_data, JSAMPARRAY output_data) nuclear@2: { nuclear@2: int outrow; nuclear@2: JDIMENSION outcol; nuclear@2: JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE; nuclear@2: register JSAMPROW inptr, outptr; nuclear@2: register int bias; nuclear@2: nuclear@2: /* Expand input data enough to let all the output samples be generated nuclear@2: * by the standard loop. Special-casing padded output would be more nuclear@2: * efficient. nuclear@2: */ nuclear@2: expand_right_edge(input_data, cinfo->max_v_samp_factor, nuclear@2: cinfo->image_width, output_cols * 2); nuclear@2: nuclear@2: for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) { nuclear@2: outptr = output_data[outrow]; nuclear@2: inptr = input_data[outrow]; nuclear@2: bias = 0; /* bias = 0,1,0,1,... for successive samples */ nuclear@2: for (outcol = 0; outcol < output_cols; outcol++) { nuclear@2: *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr) + GETJSAMPLE(inptr[1]) nuclear@2: + bias) >> 1); nuclear@2: bias ^= 1; /* 0=>1, 1=>0 */ nuclear@2: inptr += 2; nuclear@2: } nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Downsample pixel values of a single component. nuclear@2: * This version handles the standard case of 2:1 horizontal and 2:1 vertical, nuclear@2: * without smoothing. nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, nuclear@2: JSAMPARRAY input_data, JSAMPARRAY output_data) nuclear@2: { nuclear@2: int inrow, outrow; nuclear@2: JDIMENSION outcol; nuclear@2: JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE; nuclear@2: register JSAMPROW inptr0, inptr1, outptr; nuclear@2: register int bias; nuclear@2: nuclear@2: /* Expand input data enough to let all the output samples be generated nuclear@2: * by the standard loop. Special-casing padded output would be more nuclear@2: * efficient. nuclear@2: */ nuclear@2: expand_right_edge(input_data, cinfo->max_v_samp_factor, nuclear@2: cinfo->image_width, output_cols * 2); nuclear@2: nuclear@2: inrow = 0; nuclear@2: for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) { nuclear@2: outptr = output_data[outrow]; nuclear@2: inptr0 = input_data[inrow]; nuclear@2: inptr1 = input_data[inrow+1]; nuclear@2: bias = 1; /* bias = 1,2,1,2,... for successive samples */ nuclear@2: for (outcol = 0; outcol < output_cols; outcol++) { nuclear@2: *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) + nuclear@2: GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]) nuclear@2: + bias) >> 2); nuclear@2: bias ^= 3; /* 1=>2, 2=>1 */ nuclear@2: inptr0 += 2; inptr1 += 2; nuclear@2: } nuclear@2: inrow += 2; nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: nuclear@2: #ifdef INPUT_SMOOTHING_SUPPORTED nuclear@2: nuclear@2: /* nuclear@2: * Downsample pixel values of a single component. nuclear@2: * This version handles the standard case of 2:1 horizontal and 2:1 vertical, nuclear@2: * with smoothing. One row of context is required. nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, nuclear@2: JSAMPARRAY input_data, JSAMPARRAY output_data) nuclear@2: { nuclear@2: int inrow, outrow; nuclear@2: JDIMENSION colctr; nuclear@2: JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE; nuclear@2: register JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr; nuclear@2: INT32 membersum, neighsum, memberscale, neighscale; nuclear@2: nuclear@2: /* Expand input data enough to let all the output samples be generated nuclear@2: * by the standard loop. Special-casing padded output would be more nuclear@2: * efficient. nuclear@2: */ nuclear@2: expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2, nuclear@2: cinfo->image_width, output_cols * 2); nuclear@2: nuclear@2: /* We don't bother to form the individual "smoothed" input pixel values; nuclear@2: * we can directly compute the output which is the average of the four nuclear@2: * smoothed values. Each of the four member pixels contributes a fraction nuclear@2: * (1-8*SF) to its own smoothed image and a fraction SF to each of the three nuclear@2: * other smoothed pixels, therefore a total fraction (1-5*SF)/4 to the final nuclear@2: * output. The four corner-adjacent neighbor pixels contribute a fraction nuclear@2: * SF to just one smoothed pixel, or SF/4 to the final output; while the nuclear@2: * eight edge-adjacent neighbors contribute SF to each of two smoothed nuclear@2: * pixels, or SF/2 overall. In order to use integer arithmetic, these nuclear@2: * factors are scaled by 2^16 = 65536. nuclear@2: * Also recall that SF = smoothing_factor / 1024. nuclear@2: */ nuclear@2: nuclear@2: memberscale = 16384 - cinfo->smoothing_factor * 80; /* scaled (1-5*SF)/4 */ nuclear@2: neighscale = cinfo->smoothing_factor * 16; /* scaled SF/4 */ nuclear@2: nuclear@2: inrow = 0; nuclear@2: for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) { nuclear@2: outptr = output_data[outrow]; nuclear@2: inptr0 = input_data[inrow]; nuclear@2: inptr1 = input_data[inrow+1]; nuclear@2: above_ptr = input_data[inrow-1]; nuclear@2: below_ptr = input_data[inrow+2]; nuclear@2: nuclear@2: /* Special case for first column: pretend column -1 is same as column 0 */ nuclear@2: membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) + nuclear@2: GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]); nuclear@2: neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) + nuclear@2: GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) + nuclear@2: GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[2]) + nuclear@2: GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[2]); nuclear@2: neighsum += neighsum; nuclear@2: neighsum += GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[2]) + nuclear@2: GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[2]); nuclear@2: membersum = membersum * memberscale + neighsum * neighscale; nuclear@2: *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16); nuclear@2: inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2; nuclear@2: nuclear@2: for (colctr = output_cols - 2; colctr > 0; colctr--) { nuclear@2: /* sum of pixels directly mapped to this output element */ nuclear@2: membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) + nuclear@2: GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]); nuclear@2: /* sum of edge-neighbor pixels */ nuclear@2: neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) + nuclear@2: GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) + nuclear@2: GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[2]) + nuclear@2: GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[2]); nuclear@2: /* The edge-neighbors count twice as much as corner-neighbors */ nuclear@2: neighsum += neighsum; nuclear@2: /* Add in the corner-neighbors */ nuclear@2: neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[2]) + nuclear@2: GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[2]); nuclear@2: /* form final output scaled up by 2^16 */ nuclear@2: membersum = membersum * memberscale + neighsum * neighscale; nuclear@2: /* round, descale and output it */ nuclear@2: *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16); nuclear@2: inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2; nuclear@2: } nuclear@2: nuclear@2: /* Special case for last column */ nuclear@2: membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) + nuclear@2: GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]); nuclear@2: neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) + nuclear@2: GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) + nuclear@2: GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[1]) + nuclear@2: GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[1]); nuclear@2: neighsum += neighsum; nuclear@2: neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[1]) + nuclear@2: GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[1]); nuclear@2: membersum = membersum * memberscale + neighsum * neighscale; nuclear@2: *outptr = (JSAMPLE) ((membersum + 32768) >> 16); nuclear@2: nuclear@2: inrow += 2; nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Downsample pixel values of a single component. nuclear@2: * This version handles the special case of a full-size component, nuclear@2: * with smoothing. One row of context is required. nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr, nuclear@2: JSAMPARRAY input_data, JSAMPARRAY output_data) nuclear@2: { nuclear@2: int outrow; nuclear@2: JDIMENSION colctr; nuclear@2: JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE; nuclear@2: register JSAMPROW inptr, above_ptr, below_ptr, outptr; nuclear@2: INT32 membersum, neighsum, memberscale, neighscale; nuclear@2: int colsum, lastcolsum, nextcolsum; nuclear@2: nuclear@2: /* Expand input data enough to let all the output samples be generated nuclear@2: * by the standard loop. Special-casing padded output would be more nuclear@2: * efficient. nuclear@2: */ nuclear@2: expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2, nuclear@2: cinfo->image_width, output_cols); nuclear@2: nuclear@2: /* Each of the eight neighbor pixels contributes a fraction SF to the nuclear@2: * smoothed pixel, while the main pixel contributes (1-8*SF). In order nuclear@2: * to use integer arithmetic, these factors are multiplied by 2^16 = 65536. nuclear@2: * Also recall that SF = smoothing_factor / 1024. nuclear@2: */ nuclear@2: nuclear@2: memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */ nuclear@2: neighscale = cinfo->smoothing_factor * 64; /* scaled SF */ nuclear@2: nuclear@2: for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) { nuclear@2: outptr = output_data[outrow]; nuclear@2: inptr = input_data[outrow]; nuclear@2: above_ptr = input_data[outrow-1]; nuclear@2: below_ptr = input_data[outrow+1]; nuclear@2: nuclear@2: /* Special case for first column */ nuclear@2: colsum = GETJSAMPLE(*above_ptr++) + GETJSAMPLE(*below_ptr++) + nuclear@2: GETJSAMPLE(*inptr); nuclear@2: membersum = GETJSAMPLE(*inptr++); nuclear@2: nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) + nuclear@2: GETJSAMPLE(*inptr); nuclear@2: neighsum = colsum + (colsum - membersum) + nextcolsum; nuclear@2: membersum = membersum * memberscale + neighsum * neighscale; nuclear@2: *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16); nuclear@2: lastcolsum = colsum; colsum = nextcolsum; nuclear@2: nuclear@2: for (colctr = output_cols - 2; colctr > 0; colctr--) { nuclear@2: membersum = GETJSAMPLE(*inptr++); nuclear@2: above_ptr++; below_ptr++; nuclear@2: nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) + nuclear@2: GETJSAMPLE(*inptr); nuclear@2: neighsum = lastcolsum + (colsum - membersum) + nextcolsum; nuclear@2: membersum = membersum * memberscale + neighsum * neighscale; nuclear@2: *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16); nuclear@2: lastcolsum = colsum; colsum = nextcolsum; nuclear@2: } nuclear@2: nuclear@2: /* Special case for last column */ nuclear@2: membersum = GETJSAMPLE(*inptr); nuclear@2: neighsum = lastcolsum + (colsum - membersum) + colsum; nuclear@2: membersum = membersum * memberscale + neighsum * neighscale; nuclear@2: *outptr = (JSAMPLE) ((membersum + 32768) >> 16); nuclear@2: nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: #endif /* INPUT_SMOOTHING_SUPPORTED */ nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Module initialization routine for downsampling. nuclear@2: * Note that we must select a routine for each component. nuclear@2: */ nuclear@2: nuclear@2: GLOBAL(void) nuclear@2: jinit_downsampler (j_compress_ptr cinfo) nuclear@2: { nuclear@2: my_downsample_ptr downsample; nuclear@2: int ci; nuclear@2: jpeg_component_info * compptr; nuclear@2: boolean smoothok = TRUE; nuclear@2: nuclear@2: downsample = (my_downsample_ptr) nuclear@2: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, nuclear@2: SIZEOF(my_downsampler)); nuclear@2: cinfo->downsample = (struct jpeg_downsampler *) downsample; nuclear@2: downsample->pub.start_pass = start_pass_downsample; nuclear@2: downsample->pub.downsample = sep_downsample; nuclear@2: downsample->pub.need_context_rows = FALSE; nuclear@2: nuclear@2: if (cinfo->CCIR601_sampling) nuclear@2: ERREXIT(cinfo, JERR_CCIR601_NOTIMPL); nuclear@2: nuclear@2: /* Verify we can handle the sampling factors, and set up method pointers */ nuclear@2: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; nuclear@2: ci++, compptr++) { nuclear@2: if (compptr->h_samp_factor == cinfo->max_h_samp_factor && nuclear@2: compptr->v_samp_factor == cinfo->max_v_samp_factor) { nuclear@2: #ifdef INPUT_SMOOTHING_SUPPORTED nuclear@2: if (cinfo->smoothing_factor) { nuclear@2: downsample->methods[ci] = fullsize_smooth_downsample; nuclear@2: downsample->pub.need_context_rows = TRUE; nuclear@2: } else nuclear@2: #endif nuclear@2: downsample->methods[ci] = fullsize_downsample; nuclear@2: } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor && nuclear@2: compptr->v_samp_factor == cinfo->max_v_samp_factor) { nuclear@2: smoothok = FALSE; nuclear@2: downsample->methods[ci] = h2v1_downsample; nuclear@2: } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor && nuclear@2: compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) { nuclear@2: #ifdef INPUT_SMOOTHING_SUPPORTED nuclear@2: if (cinfo->smoothing_factor) { nuclear@2: downsample->methods[ci] = h2v2_smooth_downsample; nuclear@2: downsample->pub.need_context_rows = TRUE; nuclear@2: } else nuclear@2: #endif nuclear@2: downsample->methods[ci] = h2v2_downsample; nuclear@2: } else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 && nuclear@2: (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) { nuclear@2: smoothok = FALSE; nuclear@2: downsample->methods[ci] = int_downsample; nuclear@2: } else nuclear@2: ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL); nuclear@2: } nuclear@2: nuclear@2: #ifdef INPUT_SMOOTHING_SUPPORTED nuclear@2: if (cinfo->smoothing_factor && !smoothok) nuclear@2: TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL); nuclear@2: #endif nuclear@2: }