blob: b1378e1512933af4cff49383f9909f6e1e04c36b [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jdsample.c
3 *
DRCa73e8702012-12-31 02:52:30 +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.
DRCa6ef2822013-09-28 03:23:49 +00006 * libjpeg-turbo Modifications:
Pierre Ossman59a39382009-03-09 13:15:56 +00007 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRCce0dd942016-02-06 12:18:44 -06008 * Copyright (C) 2010, 2015-2016, D. R. Commander.
DRC123f7252016-05-24 10:23:56 -05009 * Copyright (C) 2014, MIPS Technologies, Inc., California.
DRC3ab68cf2016-02-19 18:32:10 -060010 * Copyright (C) 2015, Google, Inc.
DRC7e3acc02015-10-10 10:25:46 -050011 * For conditions of distribution and use, see the accompanying README.ijg
12 * file.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000013 *
Thomas G. Lane88aeed41992-12-10 00:00:00 +000014 * This file contains upsampling routines.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000015 *
16 * Upsampling input data is counted in "row groups". A row group
17 * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
18 * sample rows of each component. Upsampling will normally produce
19 * max_v_samp_factor pixel rows from each row group (but this could vary
20 * if the upsampler is applying a scale factor of its own).
Thomas G. Lane88aeed41992-12-10 00:00:00 +000021 *
22 * An excellent reference for image resampling is
23 * Digital Image Warping, George Wolberg, 1990.
24 * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000025 */
26
DRCce0dd942016-02-06 12:18:44 -060027#include "jinclude.h"
DRCeb32cc12015-06-25 03:44:36 +000028#include "jdsample.h"
Pierre Ossman59a39382009-03-09 13:15:56 +000029#include "jsimd.h"
DRC36a6eec2010-10-08 08:05:44 +000030#include "jpegcomp.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000031
32
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000033
34/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000035 * Initialize for an upsampling pass.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000036 */
37
Thomas G. Lane489583f1996-02-07 00:00:00 +000038METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000039start_pass_upsample (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000040{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000041 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
42
43 /* Mark the conversion buffer empty */
44 upsample->next_row_out = cinfo->max_v_samp_factor;
45 /* Initialize total-height counter for detecting bottom of image */
46 upsample->rows_to_go = cinfo->output_height;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000047}
48
49
50/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000051 * Control routine to do upsampling (and color conversion).
Thomas G. Lane88aeed41992-12-10 00:00:00 +000052 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000053 * In this version we upsample each component independently.
54 * We upsample one row group into the conversion buffer, then apply
55 * color conversion a row at a time.
56 */
57
Thomas G. Lane489583f1996-02-07 00:00:00 +000058METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000059sep_upsample (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +000060 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
61 JDIMENSION in_row_groups_avail,
62 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
63 JDIMENSION out_rows_avail)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000064{
65 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
66 int ci;
DRCbd498032016-02-19 08:53:33 -060067 jpeg_component_info *compptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000068 JDIMENSION num_rows;
69
70 /* Fill the conversion buffer, if it's empty */
71 if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
72 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
DRCe5eaf372014-05-09 18:00:32 +000073 ci++, compptr++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000074 /* Invoke per-component upsample method. Notice we pass a POINTER
75 * to color_buf[ci], so that fullsize_upsample can change it.
76 */
77 (*upsample->methods[ci]) (cinfo, compptr,
DRCe5eaf372014-05-09 18:00:32 +000078 input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
79 upsample->color_buf + ci);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000080 }
81 upsample->next_row_out = 0;
82 }
83
84 /* Color-convert and emit rows */
85
86 /* How many we have in the buffer: */
87 num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
88 /* Not more than the distance to the end of the image. Need this test
89 * in case the image height is not a multiple of max_v_samp_factor:
90 */
DRCe5eaf372014-05-09 18:00:32 +000091 if (num_rows > upsample->rows_to_go)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000092 num_rows = upsample->rows_to_go;
93 /* And not more than what the client can accept: */
94 out_rows_avail -= *out_row_ctr;
95 if (num_rows > out_rows_avail)
96 num_rows = out_rows_avail;
97
98 (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
DRCe5eaf372014-05-09 18:00:32 +000099 (JDIMENSION) upsample->next_row_out,
100 output_buf + *out_row_ctr,
101 (int) num_rows);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000102
103 /* Adjust counts */
104 *out_row_ctr += num_rows;
105 upsample->rows_to_go -= num_rows;
106 upsample->next_row_out += num_rows;
107 /* When the buffer is emptied, declare this input row group consumed */
108 if (upsample->next_row_out >= cinfo->max_v_samp_factor)
109 (*in_row_group_ctr)++;
110}
111
112
113/*
114 * These are the routines invoked by sep_upsample to upsample pixel values
115 * of a single component. One row group is processed per call.
116 */
117
118
119/*
120 * For full-size components, we just make color_buf[ci] point at the
121 * input buffer, and thus avoid copying any data. Note that this is
122 * safe only because sep_upsample doesn't declare the input row group
123 * "consumed" until we are done color converting and emitting it.
124 */
125
Thomas G. Lane489583f1996-02-07 00:00:00 +0000126METHODDEF(void)
DRCbd498032016-02-19 08:53:33 -0600127fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info *compptr,
128 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000129{
130 *output_data_ptr = input_data;
131}
132
133
134/*
135 * This is a no-op version used for "uninteresting" components.
136 * These components will not be referenced by color conversion.
137 */
138
Thomas G. Lane489583f1996-02-07 00:00:00 +0000139METHODDEF(void)
DRCbd498032016-02-19 08:53:33 -0600140noop_upsample (j_decompress_ptr cinfo, jpeg_component_info *compptr,
141 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000142{
DRCe5eaf372014-05-09 18:00:32 +0000143 *output_data_ptr = NULL; /* safety check */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000144}
145
146
147/*
148 * This version handles any integral sampling ratios.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000149 * This is not used for typical JPEG files, so it need not be fast.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000150 * Nor, for that matter, is it particularly accurate: the algorithm is
151 * simple replication of the input pixel onto the corresponding output
152 * pixels. The hi-falutin sampling literature refers to this as a
153 * "box filter". A box filter tends to introduce visible artifacts,
154 * so if you are actually going to use 3:1 or 4:1 sampling ratios
155 * you would be well advised to improve this code.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000156 */
157
Thomas G. Lane489583f1996-02-07 00:00:00 +0000158METHODDEF(void)
DRCbd498032016-02-19 08:53:33 -0600159int_upsample (j_decompress_ptr cinfo, jpeg_component_info *compptr,
160 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000161{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000162 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
163 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000164 register JSAMPROW inptr, outptr;
165 register JSAMPLE invalue;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000166 register int h;
167 JSAMPROW outend;
168 int h_expand, v_expand;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000169 int inrow, outrow;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000170
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000171 h_expand = upsample->h_expand[compptr->component_index];
172 v_expand = upsample->v_expand[compptr->component_index];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000173
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000174 inrow = outrow = 0;
175 while (outrow < cinfo->max_v_samp_factor) {
176 /* Generate one output row with proper horizontal expansion */
177 inptr = input_data[inrow];
178 outptr = output_data[outrow];
179 outend = outptr + cinfo->output_width;
180 while (outptr < outend) {
DRCe5eaf372014-05-09 18:00:32 +0000181 invalue = *inptr++; /* don't need GETJSAMPLE() here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000182 for (h = h_expand; h > 0; h--) {
DRCe5eaf372014-05-09 18:00:32 +0000183 *outptr++ = invalue;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000184 }
185 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000186 /* Generate any additional output rows by duplicating the first one */
187 if (v_expand > 1) {
188 jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
DRCe5eaf372014-05-09 18:00:32 +0000189 v_expand-1, cinfo->output_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000190 }
191 inrow++;
192 outrow += v_expand;
193 }
194}
195
196
197/*
198 * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
199 * It's still a box filter.
200 */
201
Thomas G. Lane489583f1996-02-07 00:00:00 +0000202METHODDEF(void)
DRCbd498032016-02-19 08:53:33 -0600203h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info *compptr,
204 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000205{
206 JSAMPARRAY output_data = *output_data_ptr;
207 register JSAMPROW inptr, outptr;
208 register JSAMPLE invalue;
209 JSAMPROW outend;
210 int inrow;
211
212 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
213 inptr = input_data[inrow];
214 outptr = output_data[inrow];
215 outend = outptr + cinfo->output_width;
216 while (outptr < outend) {
DRCe5eaf372014-05-09 18:00:32 +0000217 invalue = *inptr++; /* don't need GETJSAMPLE() here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000218 *outptr++ = invalue;
219 *outptr++ = invalue;
220 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000221 }
222}
223
224
225/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000226 * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
227 * It's still a box filter.
228 */
229
Thomas G. Lane489583f1996-02-07 00:00:00 +0000230METHODDEF(void)
DRCbd498032016-02-19 08:53:33 -0600231h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info *compptr,
232 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000233{
234 JSAMPARRAY output_data = *output_data_ptr;
235 register JSAMPROW inptr, outptr;
236 register JSAMPLE invalue;
237 JSAMPROW outend;
238 int inrow, outrow;
239
240 inrow = outrow = 0;
241 while (outrow < cinfo->max_v_samp_factor) {
242 inptr = input_data[inrow];
243 outptr = output_data[outrow];
244 outend = outptr + cinfo->output_width;
245 while (outptr < outend) {
DRCe5eaf372014-05-09 18:00:32 +0000246 invalue = *inptr++; /* don't need GETJSAMPLE() here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000247 *outptr++ = invalue;
248 *outptr++ = invalue;
249 }
250 jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
DRCe5eaf372014-05-09 18:00:32 +0000251 1, cinfo->output_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000252 inrow++;
253 outrow += 2;
254 }
255}
256
257
258/*
259 * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000260 *
261 * The upsampling algorithm is linear interpolation between pixel centers,
262 * also known as a "triangle filter". This is a good compromise between
263 * speed and visual quality. The centers of the output pixels are 1/4 and 3/4
264 * of the way between input pixel centers.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000265 *
266 * A note about the "bias" calculations: when rounding fractional values to
267 * integer, we do not want to always round 0.5 up to the next integer.
268 * If we did that, we'd introduce a noticeable bias towards larger values.
269 * Instead, this code is arranged so that 0.5 will be rounded up or down at
270 * alternate pixel locations (a simple ordered dither pattern).
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000271 */
272
Thomas G. Lane489583f1996-02-07 00:00:00 +0000273METHODDEF(void)
DRCbd498032016-02-19 08:53:33 -0600274h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info *compptr,
275 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000276{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000277 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000278 register JSAMPROW inptr, outptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000279 register int invalue;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000280 register JDIMENSION colctr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000281 int inrow;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000282
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000283 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000284 inptr = input_data[inrow];
285 outptr = output_data[inrow];
286 /* Special case for first column */
287 invalue = GETJSAMPLE(*inptr++);
288 *outptr++ = (JSAMPLE) invalue;
289 *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000290
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000291 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000292 /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
293 invalue = GETJSAMPLE(*inptr++) * 3;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000294 *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000295 *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
296 }
297
298 /* Special case for last column */
299 invalue = GETJSAMPLE(*inptr);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000300 *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000301 *outptr++ = (JSAMPLE) invalue;
302 }
303}
304
305
306/*
Kornel LesiƄski628c1682016-07-10 19:01:51 +0100307 * Fancy processing for 1:1 horizontal and 2:1 vertical (4:4:0 subsampling).
308 *
309 * This is a less common case, but it can be encountered when losslessly
310 * rotating/transposing a JPEG file that uses 4:2:2 chroma subsampling.
311 */
312
313METHODDEF(void)
314h1v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info *compptr,
315 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
316{
317 JSAMPARRAY output_data = *output_data_ptr;
318 JSAMPROW inptr0, inptr1, outptr;
319#if BITS_IN_JSAMPLE == 8
320 int thiscolsum;
321#else
322 JLONG thiscolsum;
323#endif
324 JDIMENSION colctr;
325 int inrow, outrow, v;
326
327 inrow = outrow = 0;
328 while (outrow < cinfo->max_v_samp_factor) {
329 for (v = 0; v < 2; v++) {
330 /* inptr0 points to nearest input row, inptr1 points to next nearest */
331 inptr0 = input_data[inrow];
332 if (v == 0) /* next nearest is row above */
333 inptr1 = input_data[inrow-1];
334 else /* next nearest is row below */
335 inptr1 = input_data[inrow+1];
336 outptr = output_data[outrow++];
337
338 for(colctr = 0; colctr < compptr->downsampled_width; colctr++) {
339 thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
340 *outptr++ = (JSAMPLE) ((thiscolsum + 1) >> 2);
341 }
342 }
343 inrow++;
344 }
345}
346
347
348/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000349 * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
350 * Again a triangle filter; see comments for h2v1 case, above.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000351 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000352 * It is OK for us to reference the adjacent input rows because we demanded
353 * context from the main buffer controller (see initialization code).
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000354 */
355
Thomas G. Lane489583f1996-02-07 00:00:00 +0000356METHODDEF(void)
DRCbd498032016-02-19 08:53:33 -0600357h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info *compptr,
358 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000359{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000360 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000361 register JSAMPROW inptr0, inptr1, outptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000362#if BITS_IN_JSAMPLE == 8
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000363 register int thiscolsum, lastcolsum, nextcolsum;
364#else
DRC1e32fe32015-10-14 17:32:39 -0500365 register JLONG thiscolsum, lastcolsum, nextcolsum;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000366#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000367 register JDIMENSION colctr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000368 int inrow, outrow, v;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000369
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000370 inrow = outrow = 0;
371 while (outrow < cinfo->max_v_samp_factor) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000372 for (v = 0; v < 2; v++) {
373 /* inptr0 points to nearest input row, inptr1 points to next nearest */
374 inptr0 = input_data[inrow];
DRCe5eaf372014-05-09 18:00:32 +0000375 if (v == 0) /* next nearest is row above */
376 inptr1 = input_data[inrow-1];
377 else /* next nearest is row below */
378 inptr1 = input_data[inrow+1];
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000379 outptr = output_data[outrow++];
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000380
381 /* Special case for first column */
382 thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
383 nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
384 *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000385 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000386 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
387
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000388 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
DRCe5eaf372014-05-09 18:00:32 +0000389 /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
390 /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
391 nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
392 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
393 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
394 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000395 }
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000396
397 /* Special case for last column */
398 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000399 *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000400 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000401 inrow++;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000402 }
403}
404
405
406/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000407 * Module initialization routine for upsampling.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000408 */
409
Thomas G. Lane489583f1996-02-07 00:00:00 +0000410GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000411jinit_upsampler (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000412{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000413 my_upsample_ptr upsample;
414 int ci;
DRCbd498032016-02-19 08:53:33 -0600415 jpeg_component_info *compptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000416 boolean need_buffer, do_fancy;
417 int h_in_group, v_in_group, h_out_group, v_out_group;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000418
DRC3ab68cf2016-02-19 18:32:10 -0600419 if (!cinfo->master->jinit_upsampler_no_alloc) {
420 upsample = (my_upsample_ptr)
421 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
422 sizeof(my_upsampler));
423 cinfo->upsample = (struct jpeg_upsampler *) upsample;
424 upsample->pub.start_pass = start_pass_upsample;
425 upsample->pub.upsample = sep_upsample;
426 upsample->pub.need_context_rows = FALSE; /* until we find out differently */
427 } else
428 upsample = (my_upsample_ptr) cinfo->upsample;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000429
DRCe5eaf372014-05-09 18:00:32 +0000430 if (cinfo->CCIR601_sampling) /* this isn't supported */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000431 ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
432
433 /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
434 * so don't ask for it.
435 */
DRC49967cd2010-10-09 19:57:51 +0000436 do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000437
438 /* Verify we can handle the sampling factors, select per-component methods,
439 * and create storage as needed.
440 */
441 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
442 ci++, compptr++) {
443 /* Compute size of an "input group" after IDCT scaling. This many samples
444 * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
445 */
DRC49967cd2010-10-09 19:57:51 +0000446 h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
DRCe5eaf372014-05-09 18:00:32 +0000447 cinfo->_min_DCT_scaled_size;
DRC49967cd2010-10-09 19:57:51 +0000448 v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
DRCe5eaf372014-05-09 18:00:32 +0000449 cinfo->_min_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000450 h_out_group = cinfo->max_h_samp_factor;
451 v_out_group = cinfo->max_v_samp_factor;
452 upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
453 need_buffer = TRUE;
454 if (! compptr->component_needed) {
455 /* Don't bother to upsample an uninteresting component. */
456 upsample->methods[ci] = noop_upsample;
457 need_buffer = FALSE;
458 } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
459 /* Fullsize components can be processed without any work. */
460 upsample->methods[ci] = fullsize_upsample;
461 need_buffer = FALSE;
462 } else if (h_in_group * 2 == h_out_group &&
DRCe5eaf372014-05-09 18:00:32 +0000463 v_in_group == v_out_group) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000464 /* Special cases for 2h1v upsampling */
Pierre Ossman59a39382009-03-09 13:15:56 +0000465 if (do_fancy && compptr->downsampled_width > 2) {
DRCe5eaf372014-05-09 18:00:32 +0000466 if (jsimd_can_h2v1_fancy_upsample())
467 upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
468 else
469 upsample->methods[ci] = h2v1_fancy_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000470 } else {
DRCe5eaf372014-05-09 18:00:32 +0000471 if (jsimd_can_h2v1_upsample())
472 upsample->methods[ci] = jsimd_h2v1_upsample;
473 else
474 upsample->methods[ci] = h2v1_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000475 }
Kornel LesiƄski628c1682016-07-10 19:01:51 +0100476 } else if (h_in_group == h_out_group &&
477 v_in_group * 2 == v_out_group && do_fancy) {
478 /* Non-fancy upsampling is handled by the generic method */
479 upsample->methods[ci] = h1v2_fancy_upsample;
480 upsample->pub.need_context_rows = TRUE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000481 } else if (h_in_group * 2 == h_out_group &&
DRCe5eaf372014-05-09 18:00:32 +0000482 v_in_group * 2 == v_out_group) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000483 /* Special cases for 2h2v upsampling */
484 if (do_fancy && compptr->downsampled_width > 2) {
DRCe5eaf372014-05-09 18:00:32 +0000485 if (jsimd_can_h2v2_fancy_upsample())
486 upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
487 else
488 upsample->methods[ci] = h2v2_fancy_upsample;
489 upsample->pub.need_context_rows = TRUE;
Pierre Ossman59a39382009-03-09 13:15:56 +0000490 } else {
DRCe5eaf372014-05-09 18:00:32 +0000491 if (jsimd_can_h2v2_upsample())
492 upsample->methods[ci] = jsimd_h2v2_upsample;
493 else
494 upsample->methods[ci] = h2v2_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000495 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000496 } else if ((h_out_group % h_in_group) == 0 &&
DRCe5eaf372014-05-09 18:00:32 +0000497 (v_out_group % v_in_group) == 0) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000498 /* Generic integral-factors upsampling method */
DRC5ef46302014-05-18 20:04:47 +0000499#if defined(__mips__)
500 if (jsimd_can_int_upsample())
501 upsample->methods[ci] = jsimd_int_upsample;
502 else
503#endif
504 upsample->methods[ci] = int_upsample;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000505 upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
506 upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
507 } else
508 ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
DRC3ab68cf2016-02-19 18:32:10 -0600509 if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000510 upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
DRCe5eaf372014-05-09 18:00:32 +0000511 ((j_common_ptr) cinfo, JPOOL_IMAGE,
512 (JDIMENSION) jround_up((long) cinfo->output_width,
513 (long) cinfo->max_h_samp_factor),
514 (JDIMENSION) cinfo->max_v_samp_factor);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000515 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000516 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000517}