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