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