blob: d8c376b60bdcfb559c45e9a51deecbe733553a98 [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jcsample.c
3 *
DRCda13af62014-05-18 17:52:06 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane489583f1996-02-07 00:00:00 +00005 * Copyright (C) 1991-1996, Thomas G. Lane.
DRCda13af62014-05-18 17:52:06 +00006 * libjpeg-turbo Modifications:
Pierre Ossman59a39382009-03-09 13:15:56 +00007 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00008 * For conditions of distribution and use, see the accompanying README file.
9 *
Thomas G. Lane88aeed41992-12-10 00:00:00 +000010 * This file contains downsampling routines.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000011 *
12 * Downsampling input data is counted in "row groups". A row group
13 * is defined to be max_v_samp_factor pixel rows of each component,
14 * from which the downsampler produces v_samp_factor sample rows.
15 * A single row group is processed in each call to the downsampler module.
16 *
17 * The downsampler is responsible for edge-expansion of its output data
18 * to fill an integral number of DCT blocks horizontally. The source buffer
19 * may be modified if it is helpful for this purpose (the source buffer is
20 * allocated wide enough to correspond to the desired output width).
21 * The caller (the prep controller) is responsible for vertical padding.
22 *
23 * The downsampler may request "context rows" by setting need_context_rows
24 * during startup. In this case, the input arrays will contain at least
25 * one row group's worth of pixels above and below the passed-in data;
26 * the caller will create dummy rows at image top and bottom by replicating
27 * the first or last real pixel row.
Thomas G. Lane88aeed41992-12-10 00:00:00 +000028 *
29 * An excellent reference for image resampling is
30 * Digital Image Warping, George Wolberg, 1990.
31 * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
32 *
33 * The downsampling algorithm used here is a simple average of the source
34 * pixels covered by the output pixel. The hi-falutin sampling literature
35 * refers to this as a "box filter". In general the characteristics of a box
36 * filter are not very good, but for the specific cases we normally use (1:1
37 * and 2:1 ratios) the box is equivalent to a "triangle filter" which is not
38 * nearly so bad. If you intend to use other sampling ratios, you'd be well
39 * advised to improve this code.
40 *
41 * A simple input-smoothing capability is provided. This is mainly intended
42 * for cleaning up color-dithered GIF input files (if you find it inadequate,
43 * we suggest using an external filtering program such as pnmconvol). When
44 * enabled, each input pixel P is replaced by a weighted sum of itself and its
45 * eight neighbors. P's weight is 1-8*SF and each neighbor's weight is SF,
46 * where SF = (smoothing_factor / 1024).
47 * Currently, smoothing is only supported for 2h2v sampling factors.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000048 */
49
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000050#define JPEG_INTERNALS
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000051#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000052#include "jpeglib.h"
Pierre Ossman59a39382009-03-09 13:15:56 +000053#include "jsimd.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000054
55
56/* Pointer to routine to downsample a single component */
DRCbc56b752014-05-16 10:43:44 +000057typedef void (*downsample1_ptr) (j_compress_ptr cinfo,
58 jpeg_component_info * compptr,
59 JSAMPARRAY input_data,
60 JSAMPARRAY output_data);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000061
62/* Private subobject */
63
64typedef struct {
DRCb7753512014-05-11 09:36:25 +000065 struct jpeg_downsampler pub; /* public fields */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000066
67 /* Downsampling method pointers, one per component */
68 downsample1_ptr methods[MAX_COMPONENTS];
69} my_downsampler;
70
71typedef my_downsampler * my_downsample_ptr;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000072
73
74/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000075 * Initialize for a downsampling pass.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000076 */
77
Thomas G. Lane489583f1996-02-07 00:00:00 +000078METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000079start_pass_downsample (j_compress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000080{
81 /* no work for now */
82}
83
84
85/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000086 * Expand a component horizontally from width input_cols to width output_cols,
87 * by duplicating the rightmost samples.
88 */
89
Thomas G. Lane489583f1996-02-07 00:00:00 +000090LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000091expand_right_edge (JSAMPARRAY image_data, int num_rows,
DRCb7753512014-05-11 09:36:25 +000092 JDIMENSION input_cols, JDIMENSION output_cols)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000093{
94 register JSAMPROW ptr;
95 register JSAMPLE pixval;
96 register int count;
97 int row;
98 int numcols = (int) (output_cols - input_cols);
99
100 if (numcols > 0) {
101 for (row = 0; row < num_rows; row++) {
102 ptr = image_data[row] + input_cols;
DRCb7753512014-05-11 09:36:25 +0000103 pixval = ptr[-1]; /* don't need GETJSAMPLE() here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000104 for (count = numcols; count > 0; count--)
DRCb7753512014-05-11 09:36:25 +0000105 *ptr++ = pixval;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000106 }
107 }
108}
109
110
111/*
112 * Do downsampling for a whole row group (all components).
113 *
114 * In this version we simply downsample each component independently.
115 */
116
Thomas G. Lane489583f1996-02-07 00:00:00 +0000117METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000118sep_downsample (j_compress_ptr cinfo,
DRCb7753512014-05-11 09:36:25 +0000119 JSAMPIMAGE input_buf, JDIMENSION in_row_index,
120 JSAMPIMAGE output_buf, JDIMENSION out_row_group_index)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000121{
122 my_downsample_ptr downsample = (my_downsample_ptr) cinfo->downsample;
123 int ci;
124 jpeg_component_info * compptr;
125 JSAMPARRAY in_ptr, out_ptr;
126
127 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
128 ci++, compptr++) {
129 in_ptr = input_buf[ci] + in_row_index;
130 out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor);
131 (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr);
132 }
133}
134
135
136/*
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000137 * Downsample pixel values of a single component.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000138 * One row group is processed per call.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000139 * This version handles arbitrary integral sampling ratios, without smoothing.
140 * Note that this version is not actually used for customary sampling ratios.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000141 */
142
Thomas G. Lane489583f1996-02-07 00:00:00 +0000143METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000144int_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
DRCb7753512014-05-11 09:36:25 +0000145 JSAMPARRAY input_data, JSAMPARRAY output_data)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000146{
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000147 int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
DRCb7753512014-05-11 09:36:25 +0000148 JDIMENSION outcol, outcol_h; /* outcol_h == outcol*h_expand */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000149 JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000150 JSAMPROW inptr, outptr;
151 INT32 outvalue;
152
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000153 h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
154 v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
155 numpix = h_expand * v_expand;
156 numpix2 = numpix/2;
157
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000158 /* Expand input data enough to let all the output samples be generated
159 * by the standard loop. Special-casing padded output would be more
160 * efficient.
161 */
162 expand_right_edge(input_data, cinfo->max_v_samp_factor,
DRCb7753512014-05-11 09:36:25 +0000163 cinfo->image_width, output_cols * h_expand);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000164
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000165 inrow = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000166 for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000167 outptr = output_data[outrow];
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000168 for (outcol = 0, outcol_h = 0; outcol < output_cols;
DRCb7753512014-05-11 09:36:25 +0000169 outcol++, outcol_h += h_expand) {
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000170 outvalue = 0;
171 for (v = 0; v < v_expand; v++) {
DRCb7753512014-05-11 09:36:25 +0000172 inptr = input_data[inrow+v] + outcol_h;
173 for (h = 0; h < h_expand; h++) {
174 outvalue += (INT32) GETJSAMPLE(*inptr++);
175 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000176 }
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000177 *outptr++ = (JSAMPLE) ((outvalue + numpix2) / numpix);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000178 }
179 inrow += v_expand;
180 }
181}
182
183
184/*
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000185 * Downsample pixel values of a single component.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000186 * This version handles the special case of a full-size component,
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000187 * without smoothing.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000188 */
189
Thomas G. Lane489583f1996-02-07 00:00:00 +0000190METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000191fullsize_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
DRCb7753512014-05-11 09:36:25 +0000192 JSAMPARRAY input_data, JSAMPARRAY output_data)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000193{
194 /* Copy the data */
195 jcopy_sample_rows(input_data, 0, output_data, 0,
DRCb7753512014-05-11 09:36:25 +0000196 cinfo->max_v_samp_factor, cinfo->image_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000197 /* Edge-expand */
198 expand_right_edge(output_data, cinfo->max_v_samp_factor,
DRCb7753512014-05-11 09:36:25 +0000199 cinfo->image_width, compptr->width_in_blocks * DCTSIZE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000200}
201
202
203/*
204 * Downsample pixel values of a single component.
205 * This version handles the common case of 2:1 horizontal and 1:1 vertical,
206 * without smoothing.
207 *
208 * A note about the "bias" calculations: when rounding fractional values to
209 * integer, we do not want to always round 0.5 up to the next integer.
210 * If we did that, we'd introduce a noticeable bias towards larger values.
211 * Instead, this code is arranged so that 0.5 will be rounded up or down at
212 * alternate pixel locations (a simple ordered dither pattern).
213 */
214
Thomas G. Lane489583f1996-02-07 00:00:00 +0000215METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000216h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
DRCb7753512014-05-11 09:36:25 +0000217 JSAMPARRAY input_data, JSAMPARRAY output_data)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000218{
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000219 int outrow;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000220 JDIMENSION outcol;
221 JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000222 register JSAMPROW inptr, outptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000223 register int bias;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000224
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000225 /* Expand input data enough to let all the output samples be generated
226 * by the standard loop. Special-casing padded output would be more
227 * efficient.
228 */
229 expand_right_edge(input_data, cinfo->max_v_samp_factor,
DRCb7753512014-05-11 09:36:25 +0000230 cinfo->image_width, output_cols * 2);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000231
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000232 for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000233 outptr = output_data[outrow];
234 inptr = input_data[outrow];
DRCb7753512014-05-11 09:36:25 +0000235 bias = 0; /* bias = 0,1,0,1,... for successive samples */
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000236 for (outcol = 0; outcol < output_cols; outcol++) {
237 *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr) + GETJSAMPLE(inptr[1])
DRCb7753512014-05-11 09:36:25 +0000238 + bias) >> 1);
239 bias ^= 1; /* 0=>1, 1=>0 */
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000240 inptr += 2;
241 }
242 }
243}
244
245
246/*
247 * Downsample pixel values of a single component.
248 * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
249 * without smoothing.
250 */
251
Thomas G. Lane489583f1996-02-07 00:00:00 +0000252METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000253h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
DRCb7753512014-05-11 09:36:25 +0000254 JSAMPARRAY input_data, JSAMPARRAY output_data)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000255{
256 int inrow, outrow;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000257 JDIMENSION outcol;
258 JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000259 register JSAMPROW inptr0, inptr1, outptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000260 register int bias;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000261
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000262 /* Expand input data enough to let all the output samples be generated
263 * by the standard loop. Special-casing padded output would be more
264 * efficient.
265 */
266 expand_right_edge(input_data, cinfo->max_v_samp_factor,
DRCb7753512014-05-11 09:36:25 +0000267 cinfo->image_width, output_cols * 2);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000268
269 inrow = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000270 for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000271 outptr = output_data[outrow];
272 inptr0 = input_data[inrow];
273 inptr1 = input_data[inrow+1];
DRCb7753512014-05-11 09:36:25 +0000274 bias = 1; /* bias = 1,2,1,2,... for successive samples */
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000275 for (outcol = 0; outcol < output_cols; outcol++) {
276 *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
DRCb7753512014-05-11 09:36:25 +0000277 GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1])
278 + bias) >> 2);
279 bias ^= 3; /* 1=>2, 2=>1 */
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000280 inptr0 += 2; inptr1 += 2;
281 }
282 inrow += 2;
283 }
284}
285
286
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000287#ifdef INPUT_SMOOTHING_SUPPORTED
288
289/*
290 * Downsample pixel values of a single component.
291 * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000292 * with smoothing. One row of context is required.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000293 */
294
Thomas G. Lane489583f1996-02-07 00:00:00 +0000295METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000296h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
DRCb7753512014-05-11 09:36:25 +0000297 JSAMPARRAY input_data, JSAMPARRAY output_data)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000298{
299 int inrow, outrow;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000300 JDIMENSION colctr;
301 JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000302 register JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr;
303 INT32 membersum, neighsum, memberscale, neighscale;
304
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000305 /* Expand input data enough to let all the output samples be generated
306 * by the standard loop. Special-casing padded output would be more
307 * efficient.
308 */
309 expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
DRCb7753512014-05-11 09:36:25 +0000310 cinfo->image_width, output_cols * 2);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000311
312 /* We don't bother to form the individual "smoothed" input pixel values;
313 * we can directly compute the output which is the average of the four
314 * smoothed values. Each of the four member pixels contributes a fraction
315 * (1-8*SF) to its own smoothed image and a fraction SF to each of the three
316 * other smoothed pixels, therefore a total fraction (1-5*SF)/4 to the final
317 * output. The four corner-adjacent neighbor pixels contribute a fraction
318 * SF to just one smoothed pixel, or SF/4 to the final output; while the
319 * eight edge-adjacent neighbors contribute SF to each of two smoothed
320 * pixels, or SF/2 overall. In order to use integer arithmetic, these
321 * factors are scaled by 2^16 = 65536.
322 * Also recall that SF = smoothing_factor / 1024.
323 */
324
325 memberscale = 16384 - cinfo->smoothing_factor * 80; /* scaled (1-5*SF)/4 */
326 neighscale = cinfo->smoothing_factor * 16; /* scaled SF/4 */
327
328 inrow = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000329 for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000330 outptr = output_data[outrow];
331 inptr0 = input_data[inrow];
332 inptr1 = input_data[inrow+1];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000333 above_ptr = input_data[inrow-1];
334 below_ptr = input_data[inrow+2];
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000335
336 /* Special case for first column: pretend column -1 is same as column 0 */
337 membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
DRCb7753512014-05-11 09:36:25 +0000338 GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000339 neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
DRCb7753512014-05-11 09:36:25 +0000340 GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
341 GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[2]) +
342 GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[2]);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000343 neighsum += neighsum;
344 neighsum += GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[2]) +
DRCb7753512014-05-11 09:36:25 +0000345 GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[2]);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000346 membersum = membersum * memberscale + neighsum * neighscale;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000347 *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000348 inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
349
350 for (colctr = output_cols - 2; colctr > 0; colctr--) {
351 /* sum of pixels directly mapped to this output element */
352 membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
DRCb7753512014-05-11 09:36:25 +0000353 GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000354 /* sum of edge-neighbor pixels */
355 neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
DRCb7753512014-05-11 09:36:25 +0000356 GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
357 GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[2]) +
358 GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[2]);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000359 /* The edge-neighbors count twice as much as corner-neighbors */
360 neighsum += neighsum;
361 /* Add in the corner-neighbors */
362 neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[2]) +
DRCb7753512014-05-11 09:36:25 +0000363 GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[2]);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000364 /* form final output scaled up by 2^16 */
365 membersum = membersum * memberscale + neighsum * neighscale;
366 /* round, descale and output it */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000367 *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000368 inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
369 }
370
371 /* Special case for last column */
372 membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
DRCb7753512014-05-11 09:36:25 +0000373 GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000374 neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
DRCb7753512014-05-11 09:36:25 +0000375 GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
376 GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[1]) +
377 GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[1]);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000378 neighsum += neighsum;
379 neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[1]) +
DRCb7753512014-05-11 09:36:25 +0000380 GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[1]);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000381 membersum = membersum * memberscale + neighsum * neighscale;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000382 *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000383
384 inrow += 2;
385 }
386}
387
388
389/*
390 * Downsample pixel values of a single component.
391 * This version handles the special case of a full-size component,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000392 * with smoothing. One row of context is required.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000393 */
394
Thomas G. Lane489583f1996-02-07 00:00:00 +0000395METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000396fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
DRCb7753512014-05-11 09:36:25 +0000397 JSAMPARRAY input_data, JSAMPARRAY output_data)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000398{
399 int outrow;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000400 JDIMENSION colctr;
401 JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000402 register JSAMPROW inptr, above_ptr, below_ptr, outptr;
403 INT32 membersum, neighsum, memberscale, neighscale;
404 int colsum, lastcolsum, nextcolsum;
405
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000406 /* Expand input data enough to let all the output samples be generated
407 * by the standard loop. Special-casing padded output would be more
408 * efficient.
409 */
410 expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
DRCb7753512014-05-11 09:36:25 +0000411 cinfo->image_width, output_cols);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000412
413 /* Each of the eight neighbor pixels contributes a fraction SF to the
414 * smoothed pixel, while the main pixel contributes (1-8*SF). In order
415 * to use integer arithmetic, these factors are multiplied by 2^16 = 65536.
416 * Also recall that SF = smoothing_factor / 1024.
417 */
418
419 memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */
420 neighscale = cinfo->smoothing_factor * 64; /* scaled SF */
421
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000422 for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000423 outptr = output_data[outrow];
424 inptr = input_data[outrow];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000425 above_ptr = input_data[outrow-1];
426 below_ptr = input_data[outrow+1];
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000427
428 /* Special case for first column */
429 colsum = GETJSAMPLE(*above_ptr++) + GETJSAMPLE(*below_ptr++) +
DRCb7753512014-05-11 09:36:25 +0000430 GETJSAMPLE(*inptr);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000431 membersum = GETJSAMPLE(*inptr++);
432 nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
DRCb7753512014-05-11 09:36:25 +0000433 GETJSAMPLE(*inptr);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000434 neighsum = colsum + (colsum - membersum) + nextcolsum;
435 membersum = membersum * memberscale + neighsum * neighscale;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000436 *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000437 lastcolsum = colsum; colsum = nextcolsum;
438
439 for (colctr = output_cols - 2; colctr > 0; colctr--) {
440 membersum = GETJSAMPLE(*inptr++);
441 above_ptr++; below_ptr++;
442 nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
DRCb7753512014-05-11 09:36:25 +0000443 GETJSAMPLE(*inptr);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000444 neighsum = lastcolsum + (colsum - membersum) + nextcolsum;
445 membersum = membersum * memberscale + neighsum * neighscale;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000446 *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000447 lastcolsum = colsum; colsum = nextcolsum;
448 }
449
450 /* Special case for last column */
451 membersum = GETJSAMPLE(*inptr);
452 neighsum = lastcolsum + (colsum - membersum) + colsum;
453 membersum = membersum * memberscale + neighsum * neighscale;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000454 *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000455
456 }
457}
458
459#endif /* INPUT_SMOOTHING_SUPPORTED */
460
461
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000462/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000463 * Module initialization routine for downsampling.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000464 * Note that we must select a routine for each component.
465 */
466
Thomas G. Lane489583f1996-02-07 00:00:00 +0000467GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000468jinit_downsampler (j_compress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000469{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000470 my_downsample_ptr downsample;
471 int ci;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000472 jpeg_component_info * compptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000473 boolean smoothok = TRUE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000474
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000475 downsample = (my_downsample_ptr)
476 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000477 sizeof(my_downsampler));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000478 cinfo->downsample = (struct jpeg_downsampler *) downsample;
479 downsample->pub.start_pass = start_pass_downsample;
480 downsample->pub.downsample = sep_downsample;
481 downsample->pub.need_context_rows = FALSE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000482
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000483 if (cinfo->CCIR601_sampling)
484 ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
485
486 /* Verify we can handle the sampling factors, and set up method pointers */
487 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
488 ci++, compptr++) {
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000489 if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&
DRCb7753512014-05-11 09:36:25 +0000490 compptr->v_samp_factor == cinfo->max_v_samp_factor) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000491#ifdef INPUT_SMOOTHING_SUPPORTED
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000492 if (cinfo->smoothing_factor) {
DRCb7753512014-05-11 09:36:25 +0000493 downsample->methods[ci] = fullsize_smooth_downsample;
494 downsample->pub.need_context_rows = TRUE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000495 } else
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000496#endif
DRCb7753512014-05-11 09:36:25 +0000497 downsample->methods[ci] = fullsize_downsample;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000498 } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
DRCb7753512014-05-11 09:36:25 +0000499 compptr->v_samp_factor == cinfo->max_v_samp_factor) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000500 smoothok = FALSE;
Pierre Ossman59a39382009-03-09 13:15:56 +0000501 if (jsimd_can_h2v1_downsample())
502 downsample->methods[ci] = jsimd_h2v1_downsample;
503 else
504 downsample->methods[ci] = h2v1_downsample;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000505 } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
DRCb7753512014-05-11 09:36:25 +0000506 compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000507#ifdef INPUT_SMOOTHING_SUPPORTED
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000508 if (cinfo->smoothing_factor) {
DRCa3c3bbc2014-05-15 18:22:24 +0000509#if defined(__mips__)
DRC6a61c1e2014-05-14 15:00:10 +0000510 if (jsimd_can_h2v2_smooth_downsample())
511 downsample->methods[ci] = jsimd_h2v2_smooth_downsample;
512 else
DRCa3c3bbc2014-05-15 18:22:24 +0000513#endif
DRC6a61c1e2014-05-14 15:00:10 +0000514 downsample->methods[ci] = h2v2_smooth_downsample;
DRCb7753512014-05-11 09:36:25 +0000515 downsample->pub.need_context_rows = TRUE;
DRC3b489c32014-05-14 14:57:01 +0000516 } else {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000517#endif
DRCb7753512014-05-11 09:36:25 +0000518 if (jsimd_can_h2v2_downsample())
519 downsample->methods[ci] = jsimd_h2v2_downsample;
520 else
521 downsample->methods[ci] = h2v2_downsample;
DRC3b489c32014-05-14 14:57:01 +0000522 }
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000523 } else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&
DRCb7753512014-05-11 09:36:25 +0000524 (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000525 smoothok = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000526 downsample->methods[ci] = int_downsample;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000527 } else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000528 ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000529 }
530
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000531#ifdef INPUT_SMOOTHING_SUPPORTED
532 if (cinfo->smoothing_factor && !smoothok)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000533 TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000534#endif
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000535}