blob: 275296670ae4bab2de65a768b6a093db8e0c71ac [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.
DRC5ef46302014-05-18 20:04:47 +00009 * Copyright (C) 2014, MIPS Technologies, Inc., California
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000010 * For conditions of distribution and use, see the accompanying README file.
11 *
Thomas G. Lane88aeed41992-12-10 00:00:00 +000012 * This file contains upsampling routines.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000013 *
14 * Upsampling input data is counted in "row groups". A row group
15 * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
16 * sample rows of each component. Upsampling will normally produce
17 * max_v_samp_factor pixel rows from each row group (but this could vary
18 * if the upsampler is applying a scale factor of its own).
Thomas G. Lane88aeed41992-12-10 00:00:00 +000019 *
20 * An excellent reference for image resampling is
21 * Digital Image Warping, George Wolberg, 1990.
22 * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000023 */
24
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000025#define JPEG_INTERNALS
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000026#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000027#include "jpeglib.h"
Pierre Ossman59a39382009-03-09 13:15:56 +000028#include "jsimd.h"
DRC36a6eec2010-10-08 08:05:44 +000029#include "jpegcomp.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000030
31
32/* Pointer to routine to upsample a single component */
DRCbc56b752014-05-16 10:43:44 +000033typedef void (*upsample1_ptr) (j_decompress_ptr cinfo,
34 jpeg_component_info * compptr,
35 JSAMPARRAY input_data,
36 JSAMPARRAY * output_data_ptr);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000037
38/* Private subobject */
39
40typedef struct {
DRCe5eaf372014-05-09 18:00:32 +000041 struct jpeg_upsampler pub; /* public fields */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000042
43 /* Color conversion buffer. When using separate upsampling and color
44 * conversion steps, this buffer holds one upsampled row group until it
45 * has been color converted and output.
46 * Note: we do not allocate any storage for component(s) which are full-size,
47 * ie do not need rescaling. The corresponding entry of color_buf[] is
48 * simply set to point to the input data array, thereby avoiding copying.
49 */
50 JSAMPARRAY color_buf[MAX_COMPONENTS];
51
52 /* Per-component upsampling method pointers */
53 upsample1_ptr methods[MAX_COMPONENTS];
54
DRCe5eaf372014-05-09 18:00:32 +000055 int next_row_out; /* counts rows emitted from color_buf */
56 JDIMENSION rows_to_go; /* counts rows remaining in image */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000057
58 /* Height of an input row group for each component. */
59 int rowgroup_height[MAX_COMPONENTS];
60
61 /* These arrays save pixel expansion factors so that int_expand need not
62 * recompute them each time. They are unused for other upsampling methods.
63 */
64 UINT8 h_expand[MAX_COMPONENTS];
65 UINT8 v_expand[MAX_COMPONENTS];
66} my_upsampler;
67
68typedef my_upsampler * my_upsample_ptr;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000069
70
71/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000072 * Initialize for an upsampling pass.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000073 */
74
Thomas G. Lane489583f1996-02-07 00:00:00 +000075METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000076start_pass_upsample (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000077{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000078 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
79
80 /* Mark the conversion buffer empty */
81 upsample->next_row_out = cinfo->max_v_samp_factor;
82 /* Initialize total-height counter for detecting bottom of image */
83 upsample->rows_to_go = cinfo->output_height;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000084}
85
86
87/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000088 * Control routine to do upsampling (and color conversion).
Thomas G. Lane88aeed41992-12-10 00:00:00 +000089 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000090 * In this version we upsample each component independently.
91 * We upsample one row group into the conversion buffer, then apply
92 * color conversion a row at a time.
93 */
94
Thomas G. Lane489583f1996-02-07 00:00:00 +000095METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000096sep_upsample (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +000097 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
98 JDIMENSION in_row_groups_avail,
99 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
100 JDIMENSION out_rows_avail)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000101{
102 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
103 int ci;
104 jpeg_component_info * compptr;
105 JDIMENSION num_rows;
106
107 /* Fill the conversion buffer, if it's empty */
108 if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
109 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
DRCe5eaf372014-05-09 18:00:32 +0000110 ci++, compptr++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000111 /* Invoke per-component upsample method. Notice we pass a POINTER
112 * to color_buf[ci], so that fullsize_upsample can change it.
113 */
114 (*upsample->methods[ci]) (cinfo, compptr,
DRCe5eaf372014-05-09 18:00:32 +0000115 input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
116 upsample->color_buf + ci);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000117 }
118 upsample->next_row_out = 0;
119 }
120
121 /* Color-convert and emit rows */
122
123 /* How many we have in the buffer: */
124 num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
125 /* Not more than the distance to the end of the image. Need this test
126 * in case the image height is not a multiple of max_v_samp_factor:
127 */
DRCe5eaf372014-05-09 18:00:32 +0000128 if (num_rows > upsample->rows_to_go)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000129 num_rows = upsample->rows_to_go;
130 /* And not more than what the client can accept: */
131 out_rows_avail -= *out_row_ctr;
132 if (num_rows > out_rows_avail)
133 num_rows = out_rows_avail;
134
135 (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
DRCe5eaf372014-05-09 18:00:32 +0000136 (JDIMENSION) upsample->next_row_out,
137 output_buf + *out_row_ctr,
138 (int) num_rows);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000139
140 /* Adjust counts */
141 *out_row_ctr += num_rows;
142 upsample->rows_to_go -= num_rows;
143 upsample->next_row_out += num_rows;
144 /* When the buffer is emptied, declare this input row group consumed */
145 if (upsample->next_row_out >= cinfo->max_v_samp_factor)
146 (*in_row_group_ctr)++;
147}
148
149
150/*
151 * These are the routines invoked by sep_upsample to upsample pixel values
152 * of a single component. One row group is processed per call.
153 */
154
155
156/*
157 * For full-size components, we just make color_buf[ci] point at the
158 * input buffer, and thus avoid copying any data. Note that this is
159 * safe only because sep_upsample doesn't declare the input row group
160 * "consumed" until we are done color converting and emitting it.
161 */
162
Thomas G. Lane489583f1996-02-07 00:00:00 +0000163METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000164fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000165 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000166{
167 *output_data_ptr = input_data;
168}
169
170
171/*
172 * This is a no-op version used for "uninteresting" components.
173 * These components will not be referenced by color conversion.
174 */
175
Thomas G. Lane489583f1996-02-07 00:00:00 +0000176METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000177noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000178 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000179{
DRCe5eaf372014-05-09 18:00:32 +0000180 *output_data_ptr = NULL; /* safety check */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000181}
182
183
184/*
185 * This version handles any integral sampling ratios.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000186 * This is not used for typical JPEG files, so it need not be fast.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000187 * Nor, for that matter, is it particularly accurate: the algorithm is
188 * simple replication of the input pixel onto the corresponding output
189 * pixels. The hi-falutin sampling literature refers to this as a
190 * "box filter". A box filter tends to introduce visible artifacts,
191 * so if you are actually going to use 3:1 or 4:1 sampling ratios
192 * you would be well advised to improve this code.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000193 */
194
Thomas G. Lane489583f1996-02-07 00:00:00 +0000195METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000196int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000197 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000198{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000199 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
200 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000201 register JSAMPROW inptr, outptr;
202 register JSAMPLE invalue;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000203 register int h;
204 JSAMPROW outend;
205 int h_expand, v_expand;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000206 int inrow, outrow;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000207
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000208 h_expand = upsample->h_expand[compptr->component_index];
209 v_expand = upsample->v_expand[compptr->component_index];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000210
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000211 inrow = outrow = 0;
212 while (outrow < cinfo->max_v_samp_factor) {
213 /* Generate one output row with proper horizontal expansion */
214 inptr = input_data[inrow];
215 outptr = output_data[outrow];
216 outend = outptr + cinfo->output_width;
217 while (outptr < outend) {
DRCe5eaf372014-05-09 18:00:32 +0000218 invalue = *inptr++; /* don't need GETJSAMPLE() here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000219 for (h = h_expand; h > 0; h--) {
DRCe5eaf372014-05-09 18:00:32 +0000220 *outptr++ = invalue;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000221 }
222 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000223 /* Generate any additional output rows by duplicating the first one */
224 if (v_expand > 1) {
225 jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
DRCe5eaf372014-05-09 18:00:32 +0000226 v_expand-1, cinfo->output_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000227 }
228 inrow++;
229 outrow += v_expand;
230 }
231}
232
233
234/*
235 * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
236 * It's still a box filter.
237 */
238
Thomas G. Lane489583f1996-02-07 00:00:00 +0000239METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000240h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000241 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000242{
243 JSAMPARRAY output_data = *output_data_ptr;
244 register JSAMPROW inptr, outptr;
245 register JSAMPLE invalue;
246 JSAMPROW outend;
247 int inrow;
248
249 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
250 inptr = input_data[inrow];
251 outptr = output_data[inrow];
252 outend = outptr + cinfo->output_width;
253 while (outptr < outend) {
DRCe5eaf372014-05-09 18:00:32 +0000254 invalue = *inptr++; /* don't need GETJSAMPLE() here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000255 *outptr++ = invalue;
256 *outptr++ = invalue;
257 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000258 }
259}
260
261
262/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000263 * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
264 * It's still a box filter.
265 */
266
Thomas G. Lane489583f1996-02-07 00:00:00 +0000267METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000268h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000269 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000270{
271 JSAMPARRAY output_data = *output_data_ptr;
272 register JSAMPROW inptr, outptr;
273 register JSAMPLE invalue;
274 JSAMPROW outend;
275 int inrow, outrow;
276
277 inrow = outrow = 0;
278 while (outrow < cinfo->max_v_samp_factor) {
279 inptr = input_data[inrow];
280 outptr = output_data[outrow];
281 outend = outptr + cinfo->output_width;
282 while (outptr < outend) {
DRCe5eaf372014-05-09 18:00:32 +0000283 invalue = *inptr++; /* don't need GETJSAMPLE() here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000284 *outptr++ = invalue;
285 *outptr++ = invalue;
286 }
287 jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
DRCe5eaf372014-05-09 18:00:32 +0000288 1, cinfo->output_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000289 inrow++;
290 outrow += 2;
291 }
292}
293
294
295/*
296 * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000297 *
298 * The upsampling algorithm is linear interpolation between pixel centers,
299 * also known as a "triangle filter". This is a good compromise between
300 * speed and visual quality. The centers of the output pixels are 1/4 and 3/4
301 * of the way between input pixel centers.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000302 *
303 * A note about the "bias" calculations: when rounding fractional values to
304 * integer, we do not want to always round 0.5 up to the next integer.
305 * If we did that, we'd introduce a noticeable bias towards larger values.
306 * Instead, this code is arranged so that 0.5 will be rounded up or down at
307 * alternate pixel locations (a simple ordered dither pattern).
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000308 */
309
Thomas G. Lane489583f1996-02-07 00:00:00 +0000310METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000311h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000312 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000313{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000314 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000315 register JSAMPROW inptr, outptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000316 register int invalue;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000317 register JDIMENSION colctr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000318 int inrow;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000319
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000320 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000321 inptr = input_data[inrow];
322 outptr = output_data[inrow];
323 /* Special case for first column */
324 invalue = GETJSAMPLE(*inptr++);
325 *outptr++ = (JSAMPLE) invalue;
326 *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000327
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000328 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000329 /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
330 invalue = GETJSAMPLE(*inptr++) * 3;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000331 *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000332 *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
333 }
334
335 /* Special case for last column */
336 invalue = GETJSAMPLE(*inptr);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000337 *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000338 *outptr++ = (JSAMPLE) invalue;
339 }
340}
341
342
343/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000344 * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
345 * Again a triangle filter; see comments for h2v1 case, above.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000346 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000347 * It is OK for us to reference the adjacent input rows because we demanded
348 * context from the main buffer controller (see initialization code).
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000349 */
350
Thomas G. Lane489583f1996-02-07 00:00:00 +0000351METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000352h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000353 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000354{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000355 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000356 register JSAMPROW inptr0, inptr1, outptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000357#if BITS_IN_JSAMPLE == 8
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000358 register int thiscolsum, lastcolsum, nextcolsum;
359#else
360 register INT32 thiscolsum, lastcolsum, nextcolsum;
361#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000362 register JDIMENSION colctr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000363 int inrow, outrow, v;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000364
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000365 inrow = outrow = 0;
366 while (outrow < cinfo->max_v_samp_factor) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000367 for (v = 0; v < 2; v++) {
368 /* inptr0 points to nearest input row, inptr1 points to next nearest */
369 inptr0 = input_data[inrow];
DRCe5eaf372014-05-09 18:00:32 +0000370 if (v == 0) /* next nearest is row above */
371 inptr1 = input_data[inrow-1];
372 else /* next nearest is row below */
373 inptr1 = input_data[inrow+1];
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000374 outptr = output_data[outrow++];
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000375
376 /* Special case for first column */
377 thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
378 nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
379 *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000380 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000381 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
382
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000383 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
DRCe5eaf372014-05-09 18:00:32 +0000384 /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
385 /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
386 nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
387 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
388 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
389 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000390 }
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000391
392 /* Special case for last column */
393 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000394 *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000395 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000396 inrow++;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000397 }
398}
399
400
401/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000402 * Module initialization routine for upsampling.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000403 */
404
Thomas G. Lane489583f1996-02-07 00:00:00 +0000405GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000406jinit_upsampler (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000407{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000408 my_upsample_ptr upsample;
409 int ci;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000410 jpeg_component_info * compptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000411 boolean need_buffer, do_fancy;
412 int h_in_group, v_in_group, h_out_group, v_out_group;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000413
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000414 upsample = (my_upsample_ptr)
415 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000416 sizeof(my_upsampler));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000417 cinfo->upsample = (struct jpeg_upsampler *) upsample;
418 upsample->pub.start_pass = start_pass_upsample;
419 upsample->pub.upsample = sep_upsample;
420 upsample->pub.need_context_rows = FALSE; /* until we find out differently */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000421
DRCe5eaf372014-05-09 18:00:32 +0000422 if (cinfo->CCIR601_sampling) /* this isn't supported */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000423 ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
424
425 /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
426 * so don't ask for it.
427 */
DRC49967cd2010-10-09 19:57:51 +0000428 do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000429
430 /* Verify we can handle the sampling factors, select per-component methods,
431 * and create storage as needed.
432 */
433 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
434 ci++, compptr++) {
435 /* Compute size of an "input group" after IDCT scaling. This many samples
436 * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
437 */
DRC49967cd2010-10-09 19:57:51 +0000438 h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
DRCe5eaf372014-05-09 18:00:32 +0000439 cinfo->_min_DCT_scaled_size;
DRC49967cd2010-10-09 19:57:51 +0000440 v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
DRCe5eaf372014-05-09 18:00:32 +0000441 cinfo->_min_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000442 h_out_group = cinfo->max_h_samp_factor;
443 v_out_group = cinfo->max_v_samp_factor;
444 upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
445 need_buffer = TRUE;
446 if (! compptr->component_needed) {
447 /* Don't bother to upsample an uninteresting component. */
448 upsample->methods[ci] = noop_upsample;
449 need_buffer = FALSE;
450 } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
451 /* Fullsize components can be processed without any work. */
452 upsample->methods[ci] = fullsize_upsample;
453 need_buffer = FALSE;
454 } else if (h_in_group * 2 == h_out_group &&
DRCe5eaf372014-05-09 18:00:32 +0000455 v_in_group == v_out_group) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000456 /* Special cases for 2h1v upsampling */
Pierre Ossman59a39382009-03-09 13:15:56 +0000457 if (do_fancy && compptr->downsampled_width > 2) {
DRCe5eaf372014-05-09 18:00:32 +0000458 if (jsimd_can_h2v1_fancy_upsample())
459 upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
460 else
461 upsample->methods[ci] = h2v1_fancy_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000462 } else {
DRCe5eaf372014-05-09 18:00:32 +0000463 if (jsimd_can_h2v1_upsample())
464 upsample->methods[ci] = jsimd_h2v1_upsample;
465 else
466 upsample->methods[ci] = h2v1_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000467 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000468 } else if (h_in_group * 2 == h_out_group &&
DRCe5eaf372014-05-09 18:00:32 +0000469 v_in_group * 2 == v_out_group) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000470 /* Special cases for 2h2v upsampling */
471 if (do_fancy && compptr->downsampled_width > 2) {
DRCe5eaf372014-05-09 18:00:32 +0000472 if (jsimd_can_h2v2_fancy_upsample())
473 upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
474 else
475 upsample->methods[ci] = h2v2_fancy_upsample;
476 upsample->pub.need_context_rows = TRUE;
Pierre Ossman59a39382009-03-09 13:15:56 +0000477 } else {
DRCe5eaf372014-05-09 18:00:32 +0000478 if (jsimd_can_h2v2_upsample())
479 upsample->methods[ci] = jsimd_h2v2_upsample;
480 else
481 upsample->methods[ci] = h2v2_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000482 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000483 } else if ((h_out_group % h_in_group) == 0 &&
DRCe5eaf372014-05-09 18:00:32 +0000484 (v_out_group % v_in_group) == 0) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000485 /* Generic integral-factors upsampling method */
DRC5ef46302014-05-18 20:04:47 +0000486#if defined(__mips__)
487 if (jsimd_can_int_upsample())
488 upsample->methods[ci] = jsimd_int_upsample;
489 else
490#endif
491 upsample->methods[ci] = int_upsample;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000492 upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
493 upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
494 } else
495 ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
496 if (need_buffer) {
497 upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
DRCe5eaf372014-05-09 18:00:32 +0000498 ((j_common_ptr) cinfo, JPOOL_IMAGE,
499 (JDIMENSION) jround_up((long) cinfo->output_width,
500 (long) cinfo->max_h_samp_factor),
501 (JDIMENSION) cinfo->max_v_samp_factor);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000502 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000503 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000504}