1e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#if !defined(_FX_JPEG_TURBO_)
2e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/*
3e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * jcsample.c
4e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov *
5e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Copyright (C) 1991-1996, Thomas G. Lane.
6e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * This file is part of the Independent JPEG Group's software.
7e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * For conditions of distribution and use, see the accompanying README file.
8e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov *
9e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * This file contains downsampling routines.
10e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov *
11e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Downsampling input data is counted in "row groups".  A row group
12e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * is defined to be max_v_samp_factor pixel rows of each component,
13e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * from which the downsampler produces v_samp_factor sample rows.
14e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * A single row group is processed in each call to the downsampler module.
15e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov *
16e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * The downsampler is responsible for edge-expansion of its output data
17e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * to fill an integral number of DCT blocks horizontally.  The source buffer
18e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * may be modified if it is helpful for this purpose (the source buffer is
19e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * allocated wide enough to correspond to the desired output width).
20e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * The caller (the prep controller) is responsible for vertical padding.
21e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov *
22e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * The downsampler may request "context rows" by setting need_context_rows
23e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * during startup.  In this case, the input arrays will contain at least
24e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * one row group's worth of pixels above and below the passed-in data;
25e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * the caller will create dummy rows at image top and bottom by replicating
26e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * the first or last real pixel row.
27e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov *
28e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * An excellent reference for image resampling is
29e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov *   Digital Image Warping, George Wolberg, 1990.
30e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
31e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov *
32e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * The downsampling algorithm used here is a simple average of the source
33e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * pixels covered by the output pixel.  The hi-falutin sampling literature
34e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * refers to this as a "box filter".  In general the characteristics of a box
35e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * filter are not very good, but for the specific cases we normally use (1:1
36e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * and 2:1 ratios) the box is equivalent to a "triangle filter" which is not
37e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * nearly so bad.  If you intend to use other sampling ratios, you'd be well
38e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * advised to improve this code.
39e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov *
40e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * A simple input-smoothing capability is provided.  This is mainly intended
41e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * for cleaning up color-dithered GIF input files (if you find it inadequate,
42e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * we suggest using an external filtering program such as pnmconvol).  When
43e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * enabled, each input pixel P is replaced by a weighted sum of itself and its
44e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * eight neighbors.  P's weight is 1-8*SF and each neighbor's weight is SF,
45e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * where SF = (smoothing_factor / 1024).
46e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Currently, smoothing is only supported for 2h2v sampling factors.
47e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */
48e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
49e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#define JPEG_INTERNALS
50e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#include "jinclude.h"
51e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#include "jpeglib.h"
52e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
53e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
54e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/* Pointer to routine to downsample a single component */
55e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovtypedef JMETHOD(void, downsample1_ptr,
56e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		(j_compress_ptr cinfo, jpeg_component_info * compptr,
57e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		 JSAMPARRAY input_data, JSAMPARRAY output_data));
58e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
59e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/* Private subobject */
60e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
61e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovtypedef struct {
62e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  struct jpeg_downsampler pub;	/* public fields */
63e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
64e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  /* Downsampling method pointers, one per component */
65e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  downsample1_ptr methods[MAX_COMPONENTS];
66e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov} my_downsampler;
67e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
68e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovtypedef my_downsampler * my_downsample_ptr;
69e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
70e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
71e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/*
72e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Initialize for a downsampling pass.
73e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */
74e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
75e6986e1e8d4a57987f47c215490cb080a65ee29aSvet GanovMETHODDEF(void)
76e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovstart_pass_downsample (j_compress_ptr cinfo)
77e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov{
78e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  /* no work for now */
79e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
80e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
81e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
82e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/*
83e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Expand a component horizontally from width input_cols to width output_cols,
84e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * by duplicating the rightmost samples.
85e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */
86e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
87e6986e1e8d4a57987f47c215490cb080a65ee29aSvet GanovLOCAL(void)
88e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovexpand_right_edge (JSAMPARRAY image_data, int num_rows,
89e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		   JDIMENSION input_cols, JDIMENSION output_cols)
90e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov{
91e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  register JSAMPROW ptr;
92e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  register JSAMPLE pixval;
93e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  register int count;
94e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  int row;
95e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  int numcols = (int) (output_cols - input_cols);
96e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
97e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  if (numcols > 0) {
98e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    for (row = 0; row < num_rows; row++) {
99e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      ptr = image_data[row] + input_cols;
100e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      pixval = ptr[-1];		/* don't need GETJSAMPLE() here */
101e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      for (count = numcols; count > 0; count--)
102e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	*ptr++ = pixval;
103e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    }
104e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  }
105e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
106e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
107e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
108e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/*
109e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Do downsampling for a whole row group (all components).
110e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov *
111e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * In this version we simply downsample each component independently.
112e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */
113e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
114e6986e1e8d4a57987f47c215490cb080a65ee29aSvet GanovMETHODDEF(void)
115e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovsep_downsample (j_compress_ptr cinfo,
116e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		JSAMPIMAGE input_buf, JDIMENSION in_row_index,
117e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		JSAMPIMAGE output_buf, JDIMENSION out_row_group_index)
118e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov{
119e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  my_downsample_ptr downsample = (my_downsample_ptr) cinfo->downsample;
120e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  int ci;
121e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  jpeg_component_info * compptr;
122e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  JSAMPARRAY in_ptr, out_ptr;
123e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
124e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
125e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov       ci++, compptr++) {
126e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    in_ptr = input_buf[ci] + in_row_index;
127e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor);
128e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr);
129e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  }
130e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
131e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
132e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
133e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/*
134e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Downsample pixel values of a single component.
135e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * One row group is processed per call.
136e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * This version handles arbitrary integral sampling ratios, without smoothing.
137e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Note that this version is not actually used for customary sampling ratios.
138e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */
139e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
140e6986e1e8d4a57987f47c215490cb080a65ee29aSvet GanovMETHODDEF(void)
141e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovint_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
142e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		JSAMPARRAY input_data, JSAMPARRAY output_data)
143e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov{
144e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
145e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  JDIMENSION outcol, outcol_h;	/* outcol_h == outcol*h_expand */
146e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
147e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  JSAMPROW inptr, outptr;
148e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  INT32 outvalue;
149e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
150e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
151e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
152e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  numpix = h_expand * v_expand;
153e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  numpix2 = numpix/2;
154e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
155e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  /* Expand input data enough to let all the output samples be generated
156e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * by the standard loop.  Special-casing padded output would be more
157e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * efficient.
158e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   */
159e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  expand_right_edge(input_data, cinfo->max_v_samp_factor,
160e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		    cinfo->image_width, output_cols * h_expand);
161e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
162e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  inrow = 0;
163e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
164e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    outptr = output_data[outrow];
165e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    for (outcol = 0, outcol_h = 0; outcol < output_cols;
166e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	 outcol++, outcol_h += h_expand) {
167e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      outvalue = 0;
168e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      for (v = 0; v < v_expand; v++) {
169e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	inptr = input_data[inrow+v] + outcol_h;
170e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	for (h = 0; h < h_expand; h++) {
171e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	  outvalue += (INT32) GETJSAMPLE(*inptr++);
172e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	}
173e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      }
174e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      *outptr++ = (JSAMPLE) ((outvalue + numpix2) / numpix);
175e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    }
176e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    inrow += v_expand;
177e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  }
178e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
179e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
180e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
181e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/*
182e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Downsample pixel values of a single component.
183e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * This version handles the special case of a full-size component,
184e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * without smoothing.
185e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */
186e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
187e6986e1e8d4a57987f47c215490cb080a65ee29aSvet GanovMETHODDEF(void)
188e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovfullsize_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
189e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		     JSAMPARRAY input_data, JSAMPARRAY output_data)
190e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov{
191e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  /* Copy the data */
192e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  jcopy_sample_rows(input_data, 0, output_data, 0,
193e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		    cinfo->max_v_samp_factor, cinfo->image_width);
194e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  /* Edge-expand */
195e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  expand_right_edge(output_data, cinfo->max_v_samp_factor,
196e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		    cinfo->image_width, compptr->width_in_blocks * DCTSIZE);
197e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
198e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
199e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
200e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/*
201e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Downsample pixel values of a single component.
202e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * This version handles the common case of 2:1 horizontal and 1:1 vertical,
203e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * without smoothing.
204e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov *
205e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * A note about the "bias" calculations: when rounding fractional values to
206e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * integer, we do not want to always round 0.5 up to the next integer.
207e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * If we did that, we'd introduce a noticeable bias towards larger values.
208e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Instead, this code is arranged so that 0.5 will be rounded up or down at
209e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * alternate pixel locations (a simple ordered dither pattern).
210e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */
211e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
212e6986e1e8d4a57987f47c215490cb080a65ee29aSvet GanovMETHODDEF(void)
213e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovh2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
214e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		 JSAMPARRAY input_data, JSAMPARRAY output_data)
215e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov{
216e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  int outrow;
217e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  JDIMENSION outcol;
218e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
219e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  register JSAMPROW inptr, outptr;
220e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  register int bias;
221e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
222e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  /* Expand input data enough to let all the output samples be generated
223e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * by the standard loop.  Special-casing padded output would be more
224e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * efficient.
225e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   */
226e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  expand_right_edge(input_data, cinfo->max_v_samp_factor,
227e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		    cinfo->image_width, output_cols * 2);
228e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
229e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
230e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    outptr = output_data[outrow];
231e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    inptr = input_data[outrow];
232e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    bias = 0;			/* bias = 0,1,0,1,... for successive samples */
233e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    for (outcol = 0; outcol < output_cols; outcol++) {
234e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr) + GETJSAMPLE(inptr[1])
235e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov			      + bias) >> 1);
236e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      bias ^= 1;		/* 0=>1, 1=>0 */
237e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      inptr += 2;
238e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    }
239e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  }
240e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
241e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
242e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
243e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/*
244e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Downsample pixel values of a single component.
245e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
246e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * without smoothing.
247e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */
248e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
249e6986e1e8d4a57987f47c215490cb080a65ee29aSvet GanovMETHODDEF(void)
250e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovh2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
251e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		 JSAMPARRAY input_data, JSAMPARRAY output_data)
252e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov{
253e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  int inrow, outrow;
254e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  JDIMENSION outcol;
255e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
256e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  register JSAMPROW inptr0, inptr1, outptr;
257e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  register int bias;
258e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
259e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  /* Expand input data enough to let all the output samples be generated
260e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * by the standard loop.  Special-casing padded output would be more
261e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * efficient.
262e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   */
263e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  expand_right_edge(input_data, cinfo->max_v_samp_factor,
264e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		    cinfo->image_width, output_cols * 2);
265e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
266e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  inrow = 0;
267e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
268e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    outptr = output_data[outrow];
269e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    inptr0 = input_data[inrow];
270e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    inptr1 = input_data[inrow+1];
271e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    bias = 1;			/* bias = 1,2,1,2,... for successive samples */
272e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    for (outcol = 0; outcol < output_cols; outcol++) {
273e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
274e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov			      GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1])
275e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov			      + bias) >> 2);
276e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      bias ^= 3;		/* 1=>2, 2=>1 */
277e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      inptr0 += 2; inptr1 += 2;
278e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    }
279e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    inrow += 2;
280e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  }
281e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
282e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
283e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
284e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#ifdef INPUT_SMOOTHING_SUPPORTED
285e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
286e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/*
287e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Downsample pixel values of a single component.
288e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
289e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * with smoothing.  One row of context is required.
290e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */
291e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
292e6986e1e8d4a57987f47c215490cb080a65ee29aSvet GanovMETHODDEF(void)
293e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovh2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
294e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov			JSAMPARRAY input_data, JSAMPARRAY output_data)
295e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov{
296e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  int inrow, outrow;
297e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  JDIMENSION colctr;
298e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
299e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  register JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr;
300e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  INT32 membersum, neighsum, memberscale, neighscale;
301e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
302e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  /* Expand input data enough to let all the output samples be generated
303e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * by the standard loop.  Special-casing padded output would be more
304e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * efficient.
305e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   */
306e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
307e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		    cinfo->image_width, output_cols * 2);
308e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
309e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  /* We don't bother to form the individual "smoothed" input pixel values;
310e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * we can directly compute the output which is the average of the four
311e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * smoothed values.  Each of the four member pixels contributes a fraction
312e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * (1-8*SF) to its own smoothed image and a fraction SF to each of the three
313e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * other smoothed pixels, therefore a total fraction (1-5*SF)/4 to the final
314e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * output.  The four corner-adjacent neighbor pixels contribute a fraction
315e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * SF to just one smoothed pixel, or SF/4 to the final output; while the
316e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * eight edge-adjacent neighbors contribute SF to each of two smoothed
317e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * pixels, or SF/2 overall.  In order to use integer arithmetic, these
318e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * factors are scaled by 2^16 = 65536.
319e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * Also recall that SF = smoothing_factor / 1024.
320e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   */
321e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
322e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  memberscale = 16384 - cinfo->smoothing_factor * 80; /* scaled (1-5*SF)/4 */
323e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  neighscale = cinfo->smoothing_factor * 16; /* scaled SF/4 */
324e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
325e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  inrow = 0;
326e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
327e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    outptr = output_data[outrow];
328e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    inptr0 = input_data[inrow];
329e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    inptr1 = input_data[inrow+1];
330e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    above_ptr = input_data[inrow-1];
331e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    below_ptr = input_data[inrow+2];
332e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
333e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    /* Special case for first column: pretend column -1 is same as column 0 */
334e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
335e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
336e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
337e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	       GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
338e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	       GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[2]) +
339e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	       GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[2]);
340e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    neighsum += neighsum;
341e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    neighsum += GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[2]) +
342e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[2]);
343e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    membersum = membersum * memberscale + neighsum * neighscale;
344e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
345e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
346e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
347e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    for (colctr = output_cols - 2; colctr > 0; colctr--) {
348e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      /* sum of pixels directly mapped to this output element */
349e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
350e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		  GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
351e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      /* sum of edge-neighbor pixels */
352e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
353e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		 GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
354e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		 GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[2]) +
355e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		 GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[2]);
356e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      /* The edge-neighbors count twice as much as corner-neighbors */
357e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      neighsum += neighsum;
358e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      /* Add in the corner-neighbors */
359e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[2]) +
360e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		  GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[2]);
361e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      /* form final output scaled up by 2^16 */
362e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      membersum = membersum * memberscale + neighsum * neighscale;
363e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      /* round, descale and output it */
364e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
365e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
366e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    }
367e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
368e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    /* Special case for last column */
369e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
370e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
371e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
372e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	       GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
373e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	       GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[1]) +
374e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	       GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[1]);
375e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    neighsum += neighsum;
376e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[1]) +
377e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[1]);
378e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    membersum = membersum * memberscale + neighsum * neighscale;
379e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
380e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
381e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    inrow += 2;
382e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  }
383e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
384e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
385e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
386e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/*
387e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Downsample pixel values of a single component.
388e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * This version handles the special case of a full-size component,
389e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * with smoothing.  One row of context is required.
390e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */
391e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
392e6986e1e8d4a57987f47c215490cb080a65ee29aSvet GanovMETHODDEF(void)
393e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovfullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
394e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov			    JSAMPARRAY input_data, JSAMPARRAY output_data)
395e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov{
396e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  int outrow;
397e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  JDIMENSION colctr;
398e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
399e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  register JSAMPROW inptr, above_ptr, below_ptr, outptr;
400e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  INT32 membersum, neighsum, memberscale, neighscale;
401e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  int colsum, lastcolsum, nextcolsum;
402e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
403e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  /* Expand input data enough to let all the output samples be generated
404e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * by the standard loop.  Special-casing padded output would be more
405e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * efficient.
406e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   */
407e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
408e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		    cinfo->image_width, output_cols);
409e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
410e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  /* Each of the eight neighbor pixels contributes a fraction SF to the
411e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * smoothed pixel, while the main pixel contributes (1-8*SF).  In order
412e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * to use integer arithmetic, these factors are multiplied by 2^16 = 65536.
413e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   * Also recall that SF = smoothing_factor / 1024.
414e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov   */
415e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
416e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */
417e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  neighscale = cinfo->smoothing_factor * 64; /* scaled SF */
418e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
419e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
420e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    outptr = output_data[outrow];
421e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    inptr = input_data[outrow];
422e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    above_ptr = input_data[outrow-1];
423e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    below_ptr = input_data[outrow+1];
424e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
425e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    /* Special case for first column */
426e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    colsum = GETJSAMPLE(*above_ptr++) + GETJSAMPLE(*below_ptr++) +
427e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	     GETJSAMPLE(*inptr);
428e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    membersum = GETJSAMPLE(*inptr++);
429e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
430e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		 GETJSAMPLE(*inptr);
431e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    neighsum = colsum + (colsum - membersum) + nextcolsum;
432e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    membersum = membersum * memberscale + neighsum * neighscale;
433e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
434e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    lastcolsum = colsum; colsum = nextcolsum;
435e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
436e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    for (colctr = output_cols - 2; colctr > 0; colctr--) {
437e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      membersum = GETJSAMPLE(*inptr++);
438e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      above_ptr++; below_ptr++;
439e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
440e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov		   GETJSAMPLE(*inptr);
441e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      neighsum = lastcolsum + (colsum - membersum) + nextcolsum;
442e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      membersum = membersum * memberscale + neighsum * neighscale;
443e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
444e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      lastcolsum = colsum; colsum = nextcolsum;
445e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    }
446e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
447e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    /* Special case for last column */
448e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    membersum = GETJSAMPLE(*inptr);
449e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    neighsum = lastcolsum + (colsum - membersum) + colsum;
450e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    membersum = membersum * memberscale + neighsum * neighscale;
451e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
452e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
453e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  }
454e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
455e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
456e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#endif /* INPUT_SMOOTHING_SUPPORTED */
457e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
458e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
459e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/*
460e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Module initialization routine for downsampling.
461e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Note that we must select a routine for each component.
462e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */
463e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
464e6986e1e8d4a57987f47c215490cb080a65ee29aSvet GanovGLOBAL(void)
465e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovjinit_downsampler (j_compress_ptr cinfo)
466e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov{
467e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  my_downsample_ptr downsample;
468e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  int ci;
469e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  jpeg_component_info * compptr;
470e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  boolean smoothok = TRUE;
471e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
472e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  downsample = (my_downsample_ptr)
473e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
474e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov				SIZEOF(my_downsampler));
475e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  cinfo->downsample = (struct jpeg_downsampler *) downsample;
476e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  downsample->pub.start_pass = start_pass_downsample;
477e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  downsample->pub.downsample = sep_downsample;
478e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  downsample->pub.need_context_rows = FALSE;
479e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
480e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  if (cinfo->CCIR601_sampling)
481e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
482e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
483e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  /* Verify we can handle the sampling factors, and set up method pointers */
484e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
485e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov       ci++, compptr++) {
486e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&
487e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	compptr->v_samp_factor == cinfo->max_v_samp_factor) {
488e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#ifdef INPUT_SMOOTHING_SUPPORTED
489e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      if (cinfo->smoothing_factor) {
490e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	downsample->methods[ci] = fullsize_smooth_downsample;
491e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	downsample->pub.need_context_rows = TRUE;
492e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      } else
493e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#endif
494e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	downsample->methods[ci] = fullsize_downsample;
495e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
496e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	       compptr->v_samp_factor == cinfo->max_v_samp_factor) {
497e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      smoothok = FALSE;
498e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      downsample->methods[ci] = h2v1_downsample;
499e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
500e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	       compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) {
501e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#ifdef INPUT_SMOOTHING_SUPPORTED
502e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      if (cinfo->smoothing_factor) {
503e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	downsample->methods[ci] = h2v2_smooth_downsample;
504e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	downsample->pub.need_context_rows = TRUE;
505e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      } else
506e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#endif
507e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	downsample->methods[ci] = h2v2_downsample;
508e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    } else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&
509e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov	       (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) {
510e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      smoothok = FALSE;
511e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      downsample->methods[ci] = int_downsample;
512e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    } else
513e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
514e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  }
515e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
516e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#ifdef INPUT_SMOOTHING_SUPPORTED
517e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  if (cinfo->smoothing_factor && !smoothok)
518e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL);
519e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#endif
520e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
521e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
522e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#endif //_FX_JPEG_TURBO_
523