blob: a1d19e7b852771cdc9d918c471136fe024fe23d5 [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
DRC36a6eec2010-10-08 08:05:44 +00008 * Copyright (C) 2010, D. R. Commander.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00009 * For conditions of distribution and use, see the accompanying README file.
10 *
Thomas G. Lane88aeed41992-12-10 00:00:00 +000011 * This file contains upsampling routines.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000012 *
13 * Upsampling input data is counted in "row groups". A row group
14 * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
15 * sample rows of each component. Upsampling will normally produce
16 * max_v_samp_factor pixel rows from each row group (but this could vary
17 * if the upsampler is applying a scale factor of its own).
Thomas G. Lane88aeed41992-12-10 00:00:00 +000018 *
19 * An excellent reference for image resampling is
20 * Digital Image Warping, George Wolberg, 1990.
21 * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000022 */
23
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000024#define JPEG_INTERNALS
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000025#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000026#include "jpeglib.h"
Pierre Ossman59a39382009-03-09 13:15:56 +000027#include "jsimd.h"
DRC36a6eec2010-10-08 08:05:44 +000028#include "jpegcomp.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000029
30
31/* Pointer to routine to upsample a single component */
DRCbc56b752014-05-16 10:43:44 +000032typedef void (*upsample1_ptr) (j_decompress_ptr cinfo,
33 jpeg_component_info * compptr,
34 JSAMPARRAY input_data,
35 JSAMPARRAY * output_data_ptr);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000036
37/* Private subobject */
38
39typedef struct {
DRCe5eaf372014-05-09 18:00:32 +000040 struct jpeg_upsampler pub; /* public fields */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000041
42 /* Color conversion buffer. When using separate upsampling and color
43 * conversion steps, this buffer holds one upsampled row group until it
44 * has been color converted and output.
45 * Note: we do not allocate any storage for component(s) which are full-size,
46 * ie do not need rescaling. The corresponding entry of color_buf[] is
47 * simply set to point to the input data array, thereby avoiding copying.
48 */
49 JSAMPARRAY color_buf[MAX_COMPONENTS];
50
51 /* Per-component upsampling method pointers */
52 upsample1_ptr methods[MAX_COMPONENTS];
53
DRCe5eaf372014-05-09 18:00:32 +000054 int next_row_out; /* counts rows emitted from color_buf */
55 JDIMENSION rows_to_go; /* counts rows remaining in image */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000056
57 /* Height of an input row group for each component. */
58 int rowgroup_height[MAX_COMPONENTS];
59
60 /* These arrays save pixel expansion factors so that int_expand need not
61 * recompute them each time. They are unused for other upsampling methods.
62 */
63 UINT8 h_expand[MAX_COMPONENTS];
64 UINT8 v_expand[MAX_COMPONENTS];
65} my_upsampler;
66
67typedef my_upsampler * my_upsample_ptr;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000068
69
70/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000071 * Initialize for an upsampling pass.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000072 */
73
Thomas G. Lane489583f1996-02-07 00:00:00 +000074METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000075start_pass_upsample (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000076{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000077 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
78
79 /* Mark the conversion buffer empty */
80 upsample->next_row_out = cinfo->max_v_samp_factor;
81 /* Initialize total-height counter for detecting bottom of image */
82 upsample->rows_to_go = cinfo->output_height;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000083}
84
85
86/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000087 * Control routine to do upsampling (and color conversion).
Thomas G. Lane88aeed41992-12-10 00:00:00 +000088 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000089 * In this version we upsample each component independently.
90 * We upsample one row group into the conversion buffer, then apply
91 * color conversion a row at a time.
92 */
93
Thomas G. Lane489583f1996-02-07 00:00:00 +000094METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000095sep_upsample (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +000096 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
97 JDIMENSION in_row_groups_avail,
98 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
99 JDIMENSION out_rows_avail)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000100{
101 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
102 int ci;
103 jpeg_component_info * compptr;
104 JDIMENSION num_rows;
105
106 /* Fill the conversion buffer, if it's empty */
107 if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
108 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
DRCe5eaf372014-05-09 18:00:32 +0000109 ci++, compptr++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000110 /* Invoke per-component upsample method. Notice we pass a POINTER
111 * to color_buf[ci], so that fullsize_upsample can change it.
112 */
113 (*upsample->methods[ci]) (cinfo, compptr,
DRCe5eaf372014-05-09 18:00:32 +0000114 input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
115 upsample->color_buf + ci);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000116 }
117 upsample->next_row_out = 0;
118 }
119
120 /* Color-convert and emit rows */
121
122 /* How many we have in the buffer: */
123 num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
124 /* Not more than the distance to the end of the image. Need this test
125 * in case the image height is not a multiple of max_v_samp_factor:
126 */
DRCe5eaf372014-05-09 18:00:32 +0000127 if (num_rows > upsample->rows_to_go)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000128 num_rows = upsample->rows_to_go;
129 /* And not more than what the client can accept: */
130 out_rows_avail -= *out_row_ctr;
131 if (num_rows > out_rows_avail)
132 num_rows = out_rows_avail;
133
134 (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
DRCe5eaf372014-05-09 18:00:32 +0000135 (JDIMENSION) upsample->next_row_out,
136 output_buf + *out_row_ctr,
137 (int) num_rows);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000138
139 /* Adjust counts */
140 *out_row_ctr += num_rows;
141 upsample->rows_to_go -= num_rows;
142 upsample->next_row_out += num_rows;
143 /* When the buffer is emptied, declare this input row group consumed */
144 if (upsample->next_row_out >= cinfo->max_v_samp_factor)
145 (*in_row_group_ctr)++;
146}
147
148
149/*
150 * These are the routines invoked by sep_upsample to upsample pixel values
151 * of a single component. One row group is processed per call.
152 */
153
154
155/*
156 * For full-size components, we just make color_buf[ci] point at the
157 * input buffer, and thus avoid copying any data. Note that this is
158 * safe only because sep_upsample doesn't declare the input row group
159 * "consumed" until we are done color converting and emitting it.
160 */
161
Thomas G. Lane489583f1996-02-07 00:00:00 +0000162METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000163fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000164 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000165{
166 *output_data_ptr = input_data;
167}
168
169
170/*
171 * This is a no-op version used for "uninteresting" components.
172 * These components will not be referenced by color conversion.
173 */
174
Thomas G. Lane489583f1996-02-07 00:00:00 +0000175METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000176noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000177 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000178{
DRCe5eaf372014-05-09 18:00:32 +0000179 *output_data_ptr = NULL; /* safety check */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000180}
181
182
183/*
184 * This version handles any integral sampling ratios.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000185 * This is not used for typical JPEG files, so it need not be fast.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000186 * Nor, for that matter, is it particularly accurate: the algorithm is
187 * simple replication of the input pixel onto the corresponding output
188 * pixels. The hi-falutin sampling literature refers to this as a
189 * "box filter". A box filter tends to introduce visible artifacts,
190 * so if you are actually going to use 3:1 or 4:1 sampling ratios
191 * you would be well advised to improve this code.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000192 */
193
Thomas G. Lane489583f1996-02-07 00:00:00 +0000194METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000195int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000196 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000197{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000198 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
199 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000200 register JSAMPROW inptr, outptr;
201 register JSAMPLE invalue;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000202 register int h;
203 JSAMPROW outend;
204 int h_expand, v_expand;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000205 int inrow, outrow;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000206
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000207 h_expand = upsample->h_expand[compptr->component_index];
208 v_expand = upsample->v_expand[compptr->component_index];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000209
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000210 inrow = outrow = 0;
211 while (outrow < cinfo->max_v_samp_factor) {
212 /* Generate one output row with proper horizontal expansion */
213 inptr = input_data[inrow];
214 outptr = output_data[outrow];
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 for (h = h_expand; h > 0; h--) {
DRCe5eaf372014-05-09 18:00:32 +0000219 *outptr++ = invalue;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000220 }
221 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000222 /* Generate any additional output rows by duplicating the first one */
223 if (v_expand > 1) {
224 jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
DRCe5eaf372014-05-09 18:00:32 +0000225 v_expand-1, cinfo->output_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000226 }
227 inrow++;
228 outrow += v_expand;
229 }
230}
231
232
233/*
234 * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
235 * It's still a box filter.
236 */
237
Thomas G. Lane489583f1996-02-07 00:00:00 +0000238METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000239h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000240 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000241{
242 JSAMPARRAY output_data = *output_data_ptr;
243 register JSAMPROW inptr, outptr;
244 register JSAMPLE invalue;
245 JSAMPROW outend;
246 int inrow;
247
248 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
249 inptr = input_data[inrow];
250 outptr = output_data[inrow];
251 outend = outptr + cinfo->output_width;
252 while (outptr < outend) {
DRCe5eaf372014-05-09 18:00:32 +0000253 invalue = *inptr++; /* don't need GETJSAMPLE() here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000254 *outptr++ = invalue;
255 *outptr++ = invalue;
256 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000257 }
258}
259
260
261/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000262 * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
263 * It's still a box filter.
264 */
265
Thomas G. Lane489583f1996-02-07 00:00:00 +0000266METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000267h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000268 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000269{
270 JSAMPARRAY output_data = *output_data_ptr;
271 register JSAMPROW inptr, outptr;
272 register JSAMPLE invalue;
273 JSAMPROW outend;
274 int inrow, outrow;
275
276 inrow = outrow = 0;
277 while (outrow < cinfo->max_v_samp_factor) {
278 inptr = input_data[inrow];
279 outptr = output_data[outrow];
280 outend = outptr + cinfo->output_width;
281 while (outptr < outend) {
DRCe5eaf372014-05-09 18:00:32 +0000282 invalue = *inptr++; /* don't need GETJSAMPLE() here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000283 *outptr++ = invalue;
284 *outptr++ = invalue;
285 }
286 jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
DRCe5eaf372014-05-09 18:00:32 +0000287 1, cinfo->output_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000288 inrow++;
289 outrow += 2;
290 }
291}
292
293
294/*
295 * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000296 *
297 * The upsampling algorithm is linear interpolation between pixel centers,
298 * also known as a "triangle filter". This is a good compromise between
299 * speed and visual quality. The centers of the output pixels are 1/4 and 3/4
300 * of the way between input pixel centers.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000301 *
302 * A note about the "bias" calculations: when rounding fractional values to
303 * integer, we do not want to always round 0.5 up to the next integer.
304 * If we did that, we'd introduce a noticeable bias towards larger values.
305 * Instead, this code is arranged so that 0.5 will be rounded up or down at
306 * alternate pixel locations (a simple ordered dither pattern).
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000307 */
308
Thomas G. Lane489583f1996-02-07 00:00:00 +0000309METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000310h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000311 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000312{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000313 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000314 register JSAMPROW inptr, outptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000315 register int invalue;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000316 register JDIMENSION colctr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000317 int inrow;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000318
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000319 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000320 inptr = input_data[inrow];
321 outptr = output_data[inrow];
322 /* Special case for first column */
323 invalue = GETJSAMPLE(*inptr++);
324 *outptr++ = (JSAMPLE) invalue;
325 *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000326
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000327 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000328 /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
329 invalue = GETJSAMPLE(*inptr++) * 3;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000330 *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000331 *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
332 }
333
334 /* Special case for last column */
335 invalue = GETJSAMPLE(*inptr);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000336 *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000337 *outptr++ = (JSAMPLE) invalue;
338 }
339}
340
341
342/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000343 * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
344 * Again a triangle filter; see comments for h2v1 case, above.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000345 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000346 * It is OK for us to reference the adjacent input rows because we demanded
347 * context from the main buffer controller (see initialization code).
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000348 */
349
Thomas G. Lane489583f1996-02-07 00:00:00 +0000350METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000351h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000352 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000353{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000354 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000355 register JSAMPROW inptr0, inptr1, outptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000356#if BITS_IN_JSAMPLE == 8
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000357 register int thiscolsum, lastcolsum, nextcolsum;
358#else
359 register INT32 thiscolsum, lastcolsum, nextcolsum;
360#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000361 register JDIMENSION colctr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000362 int inrow, outrow, v;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000363
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000364 inrow = outrow = 0;
365 while (outrow < cinfo->max_v_samp_factor) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000366 for (v = 0; v < 2; v++) {
367 /* inptr0 points to nearest input row, inptr1 points to next nearest */
368 inptr0 = input_data[inrow];
DRCe5eaf372014-05-09 18:00:32 +0000369 if (v == 0) /* next nearest is row above */
370 inptr1 = input_data[inrow-1];
371 else /* next nearest is row below */
372 inptr1 = input_data[inrow+1];
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000373 outptr = output_data[outrow++];
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000374
375 /* Special case for first column */
376 thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
377 nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
378 *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000379 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000380 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
381
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000382 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
DRCe5eaf372014-05-09 18:00:32 +0000383 /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
384 /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
385 nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
386 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
387 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
388 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000389 }
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000390
391 /* Special case for last column */
392 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000393 *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000394 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000395 inrow++;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000396 }
397}
398
399
400/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000401 * Module initialization routine for upsampling.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000402 */
403
Thomas G. Lane489583f1996-02-07 00:00:00 +0000404GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000405jinit_upsampler (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000406{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000407 my_upsample_ptr upsample;
408 int ci;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000409 jpeg_component_info * compptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000410 boolean need_buffer, do_fancy;
411 int h_in_group, v_in_group, h_out_group, v_out_group;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000412
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000413 upsample = (my_upsample_ptr)
414 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRCe5eaf372014-05-09 18:00:32 +0000415 SIZEOF(my_upsampler));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000416 cinfo->upsample = (struct jpeg_upsampler *) upsample;
417 upsample->pub.start_pass = start_pass_upsample;
418 upsample->pub.upsample = sep_upsample;
419 upsample->pub.need_context_rows = FALSE; /* until we find out differently */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000420
DRCe5eaf372014-05-09 18:00:32 +0000421 if (cinfo->CCIR601_sampling) /* this isn't supported */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000422 ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
423
424 /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
425 * so don't ask for it.
426 */
DRC49967cd2010-10-09 19:57:51 +0000427 do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000428
429 /* Verify we can handle the sampling factors, select per-component methods,
430 * and create storage as needed.
431 */
432 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
433 ci++, compptr++) {
434 /* Compute size of an "input group" after IDCT scaling. This many samples
435 * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
436 */
DRC49967cd2010-10-09 19:57:51 +0000437 h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
DRCe5eaf372014-05-09 18:00:32 +0000438 cinfo->_min_DCT_scaled_size;
DRC49967cd2010-10-09 19:57:51 +0000439 v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
DRCe5eaf372014-05-09 18:00:32 +0000440 cinfo->_min_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000441 h_out_group = cinfo->max_h_samp_factor;
442 v_out_group = cinfo->max_v_samp_factor;
443 upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
444 need_buffer = TRUE;
445 if (! compptr->component_needed) {
446 /* Don't bother to upsample an uninteresting component. */
447 upsample->methods[ci] = noop_upsample;
448 need_buffer = FALSE;
449 } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
450 /* Fullsize components can be processed without any work. */
451 upsample->methods[ci] = fullsize_upsample;
452 need_buffer = FALSE;
453 } else if (h_in_group * 2 == h_out_group &&
DRCe5eaf372014-05-09 18:00:32 +0000454 v_in_group == v_out_group) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000455 /* Special cases for 2h1v upsampling */
Pierre Ossman59a39382009-03-09 13:15:56 +0000456 if (do_fancy && compptr->downsampled_width > 2) {
DRCe5eaf372014-05-09 18:00:32 +0000457 if (jsimd_can_h2v1_fancy_upsample())
458 upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
459 else
460 upsample->methods[ci] = h2v1_fancy_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000461 } else {
DRCe5eaf372014-05-09 18:00:32 +0000462 if (jsimd_can_h2v1_upsample())
463 upsample->methods[ci] = jsimd_h2v1_upsample;
464 else
465 upsample->methods[ci] = h2v1_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000466 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000467 } else if (h_in_group * 2 == h_out_group &&
DRCe5eaf372014-05-09 18:00:32 +0000468 v_in_group * 2 == v_out_group) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000469 /* Special cases for 2h2v upsampling */
470 if (do_fancy && compptr->downsampled_width > 2) {
DRCe5eaf372014-05-09 18:00:32 +0000471 if (jsimd_can_h2v2_fancy_upsample())
472 upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
473 else
474 upsample->methods[ci] = h2v2_fancy_upsample;
475 upsample->pub.need_context_rows = TRUE;
Pierre Ossman59a39382009-03-09 13:15:56 +0000476 } else {
DRCe5eaf372014-05-09 18:00:32 +0000477 if (jsimd_can_h2v2_upsample())
478 upsample->methods[ci] = jsimd_h2v2_upsample;
479 else
480 upsample->methods[ci] = h2v2_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000481 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000482 } else if ((h_out_group % h_in_group) == 0 &&
DRCe5eaf372014-05-09 18:00:32 +0000483 (v_out_group % v_in_group) == 0) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000484 /* Generic integral-factors upsampling method */
485 upsample->methods[ci] = int_upsample;
486 upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
487 upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
488 } else
489 ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
490 if (need_buffer) {
491 upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
DRCe5eaf372014-05-09 18:00:32 +0000492 ((j_common_ptr) cinfo, JPOOL_IMAGE,
493 (JDIMENSION) jround_up((long) cinfo->output_width,
494 (long) cinfo->max_h_samp_factor),
495 (JDIMENSION) cinfo->max_v_samp_factor);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000496 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000497 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000498}