blob: 1864dd6b8cd5d3b7bfe9562e684f25113ee4b207 [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jdsample.c
3 *
Thomas G. Lane489583f1996-02-07 00:00:00 +00004 * Copyright (C) 1991-1996, Thomas G. Lane.
Pierre Ossman59a39382009-03-09 13:15:56 +00005 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRC36a6eec2010-10-08 08:05:44 +00006 * Copyright (C) 2010, D. R. Commander.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00007 * This file is part of the Independent JPEG Group's software.
8 * For conditions of distribution and use, see the accompanying README file.
9 *
Thomas G. Lane88aeed41992-12-10 00:00:00 +000010 * This file contains upsampling routines.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000011 *
12 * Upsampling input data is counted in "row groups". A row group
13 * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
14 * sample rows of each component. Upsampling will normally produce
15 * max_v_samp_factor pixel rows from each row group (but this could vary
16 * if the upsampler is applying a scale factor of its own).
Thomas G. Lane88aeed41992-12-10 00:00:00 +000017 *
18 * An excellent reference for image resampling is
19 * Digital Image Warping, George Wolberg, 1990.
20 * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000021 */
22
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000023#define JPEG_INTERNALS
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000024#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000025#include "jpeglib.h"
Pierre Ossman59a39382009-03-09 13:15:56 +000026#include "jsimd.h"
DRC36a6eec2010-10-08 08:05:44 +000027#include "jpegcomp.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000028
29
30/* Pointer to routine to upsample a single component */
31typedef JMETHOD(void, upsample1_ptr,
32 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
33 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));
34
35/* Private subobject */
36
37typedef struct {
38 struct jpeg_upsampler pub; /* public fields */
39
40 /* Color conversion buffer. When using separate upsampling and color
41 * conversion steps, this buffer holds one upsampled row group until it
42 * has been color converted and output.
43 * Note: we do not allocate any storage for component(s) which are full-size,
44 * ie do not need rescaling. The corresponding entry of color_buf[] is
45 * simply set to point to the input data array, thereby avoiding copying.
46 */
47 JSAMPARRAY color_buf[MAX_COMPONENTS];
48
49 /* Per-component upsampling method pointers */
50 upsample1_ptr methods[MAX_COMPONENTS];
51
52 int next_row_out; /* counts rows emitted from color_buf */
53 JDIMENSION rows_to_go; /* counts rows remaining in image */
54
55 /* Height of an input row group for each component. */
56 int rowgroup_height[MAX_COMPONENTS];
57
58 /* These arrays save pixel expansion factors so that int_expand need not
59 * recompute them each time. They are unused for other upsampling methods.
60 */
61 UINT8 h_expand[MAX_COMPONENTS];
62 UINT8 v_expand[MAX_COMPONENTS];
63} my_upsampler;
64
65typedef my_upsampler * my_upsample_ptr;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000066
67
68/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000069 * Initialize for an upsampling pass.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000070 */
71
Thomas G. Lane489583f1996-02-07 00:00:00 +000072METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000073start_pass_upsample (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000074{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000075 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
76
77 /* Mark the conversion buffer empty */
78 upsample->next_row_out = cinfo->max_v_samp_factor;
79 /* Initialize total-height counter for detecting bottom of image */
80 upsample->rows_to_go = cinfo->output_height;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000081}
82
83
84/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000085 * Control routine to do upsampling (and color conversion).
Thomas G. Lane88aeed41992-12-10 00:00:00 +000086 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000087 * In this version we upsample each component independently.
88 * We upsample one row group into the conversion buffer, then apply
89 * color conversion a row at a time.
90 */
91
Thomas G. Lane489583f1996-02-07 00:00:00 +000092METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000093sep_upsample (j_decompress_ptr cinfo,
94 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
95 JDIMENSION in_row_groups_avail,
96 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
97 JDIMENSION out_rows_avail)
98{
99 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
100 int ci;
101 jpeg_component_info * compptr;
102 JDIMENSION num_rows;
103
104 /* Fill the conversion buffer, if it's empty */
105 if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
106 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
107 ci++, compptr++) {
108 /* Invoke per-component upsample method. Notice we pass a POINTER
109 * to color_buf[ci], so that fullsize_upsample can change it.
110 */
111 (*upsample->methods[ci]) (cinfo, compptr,
112 input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
113 upsample->color_buf + ci);
114 }
115 upsample->next_row_out = 0;
116 }
117
118 /* Color-convert and emit rows */
119
120 /* How many we have in the buffer: */
121 num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
122 /* Not more than the distance to the end of the image. Need this test
123 * in case the image height is not a multiple of max_v_samp_factor:
124 */
125 if (num_rows > upsample->rows_to_go)
126 num_rows = upsample->rows_to_go;
127 /* And not more than what the client can accept: */
128 out_rows_avail -= *out_row_ctr;
129 if (num_rows > out_rows_avail)
130 num_rows = out_rows_avail;
131
132 (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
133 (JDIMENSION) upsample->next_row_out,
134 output_buf + *out_row_ctr,
135 (int) num_rows);
136
137 /* Adjust counts */
138 *out_row_ctr += num_rows;
139 upsample->rows_to_go -= num_rows;
140 upsample->next_row_out += num_rows;
141 /* When the buffer is emptied, declare this input row group consumed */
142 if (upsample->next_row_out >= cinfo->max_v_samp_factor)
143 (*in_row_group_ctr)++;
144}
145
146
147/*
148 * These are the routines invoked by sep_upsample to upsample pixel values
149 * of a single component. One row group is processed per call.
150 */
151
152
153/*
154 * For full-size components, we just make color_buf[ci] point at the
155 * input buffer, and thus avoid copying any data. Note that this is
156 * safe only because sep_upsample doesn't declare the input row group
157 * "consumed" until we are done color converting and emitting it.
158 */
159
Thomas G. Lane489583f1996-02-07 00:00:00 +0000160METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000161fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
162 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
163{
164 *output_data_ptr = input_data;
165}
166
167
168/*
169 * This is a no-op version used for "uninteresting" components.
170 * These components will not be referenced by color conversion.
171 */
172
Thomas G. Lane489583f1996-02-07 00:00:00 +0000173METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000174noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
175 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
176{
177 *output_data_ptr = NULL; /* safety check */
178}
179
180
181/*
182 * This version handles any integral sampling ratios.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000183 * This is not used for typical JPEG files, so it need not be fast.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000184 * Nor, for that matter, is it particularly accurate: the algorithm is
185 * simple replication of the input pixel onto the corresponding output
186 * pixels. The hi-falutin sampling literature refers to this as a
187 * "box filter". A box filter tends to introduce visible artifacts,
188 * so if you are actually going to use 3:1 or 4:1 sampling ratios
189 * you would be well advised to improve this code.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000190 */
191
Thomas G. Lane489583f1996-02-07 00:00:00 +0000192METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000193int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
194 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000195{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000196 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
197 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000198 register JSAMPROW inptr, outptr;
199 register JSAMPLE invalue;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000200 register int h;
201 JSAMPROW outend;
202 int h_expand, v_expand;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000203 int inrow, outrow;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000204
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000205 h_expand = upsample->h_expand[compptr->component_index];
206 v_expand = upsample->v_expand[compptr->component_index];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000207
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000208 inrow = outrow = 0;
209 while (outrow < cinfo->max_v_samp_factor) {
210 /* Generate one output row with proper horizontal expansion */
211 inptr = input_data[inrow];
212 outptr = output_data[outrow];
213 outend = outptr + cinfo->output_width;
214 while (outptr < outend) {
215 invalue = *inptr++; /* don't need GETJSAMPLE() here */
216 for (h = h_expand; h > 0; h--) {
217 *outptr++ = invalue;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000218 }
219 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000220 /* Generate any additional output rows by duplicating the first one */
221 if (v_expand > 1) {
222 jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
223 v_expand-1, cinfo->output_width);
224 }
225 inrow++;
226 outrow += v_expand;
227 }
228}
229
230
231/*
232 * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
233 * It's still a box filter.
234 */
235
Thomas G. Lane489583f1996-02-07 00:00:00 +0000236METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000237h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
238 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
239{
240 JSAMPARRAY output_data = *output_data_ptr;
241 register JSAMPROW inptr, outptr;
242 register JSAMPLE invalue;
243 JSAMPROW outend;
244 int inrow;
245
246 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
247 inptr = input_data[inrow];
248 outptr = output_data[inrow];
249 outend = outptr + cinfo->output_width;
250 while (outptr < outend) {
251 invalue = *inptr++; /* don't need GETJSAMPLE() here */
252 *outptr++ = invalue;
253 *outptr++ = invalue;
254 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000255 }
256}
257
258
259/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000260 * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
261 * It's still a box filter.
262 */
263
Thomas G. Lane489583f1996-02-07 00:00:00 +0000264METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000265h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
266 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
267{
268 JSAMPARRAY output_data = *output_data_ptr;
269 register JSAMPROW inptr, outptr;
270 register JSAMPLE invalue;
271 JSAMPROW outend;
272 int inrow, outrow;
273
274 inrow = outrow = 0;
275 while (outrow < cinfo->max_v_samp_factor) {
276 inptr = input_data[inrow];
277 outptr = output_data[outrow];
278 outend = outptr + cinfo->output_width;
279 while (outptr < outend) {
280 invalue = *inptr++; /* don't need GETJSAMPLE() here */
281 *outptr++ = invalue;
282 *outptr++ = invalue;
283 }
284 jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
285 1, cinfo->output_width);
286 inrow++;
287 outrow += 2;
288 }
289}
290
291
292/*
293 * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000294 *
295 * The upsampling algorithm is linear interpolation between pixel centers,
296 * also known as a "triangle filter". This is a good compromise between
297 * speed and visual quality. The centers of the output pixels are 1/4 and 3/4
298 * of the way between input pixel centers.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000299 *
300 * A note about the "bias" calculations: when rounding fractional values to
301 * integer, we do not want to always round 0.5 up to the next integer.
302 * If we did that, we'd introduce a noticeable bias towards larger values.
303 * Instead, this code is arranged so that 0.5 will be rounded up or down at
304 * alternate pixel locations (a simple ordered dither pattern).
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000305 */
306
Thomas G. Lane489583f1996-02-07 00:00:00 +0000307METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000308h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
309 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000310{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000311 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000312 register JSAMPROW inptr, outptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000313 register int invalue;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000314 register JDIMENSION colctr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000315 int inrow;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000316
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000317 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000318 inptr = input_data[inrow];
319 outptr = output_data[inrow];
320 /* Special case for first column */
321 invalue = GETJSAMPLE(*inptr++);
322 *outptr++ = (JSAMPLE) invalue;
323 *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000324
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000325 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000326 /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
327 invalue = GETJSAMPLE(*inptr++) * 3;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000328 *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000329 *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
330 }
331
332 /* Special case for last column */
333 invalue = GETJSAMPLE(*inptr);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000334 *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000335 *outptr++ = (JSAMPLE) invalue;
336 }
337}
338
339
340/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000341 * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
342 * Again a triangle filter; see comments for h2v1 case, above.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000343 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000344 * It is OK for us to reference the adjacent input rows because we demanded
345 * context from the main buffer controller (see initialization code).
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000346 */
347
Thomas G. Lane489583f1996-02-07 00:00:00 +0000348METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000349h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
350 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000351{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000352 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000353 register JSAMPROW inptr0, inptr1, outptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000354#if BITS_IN_JSAMPLE == 8
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000355 register int thiscolsum, lastcolsum, nextcolsum;
356#else
357 register INT32 thiscolsum, lastcolsum, nextcolsum;
358#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000359 register JDIMENSION colctr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000360 int inrow, outrow, v;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000361
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000362 inrow = outrow = 0;
363 while (outrow < cinfo->max_v_samp_factor) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000364 for (v = 0; v < 2; v++) {
365 /* inptr0 points to nearest input row, inptr1 points to next nearest */
366 inptr0 = input_data[inrow];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000367 if (v == 0) /* next nearest is row above */
368 inptr1 = input_data[inrow-1];
369 else /* next nearest is row below */
370 inptr1 = input_data[inrow+1];
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000371 outptr = output_data[outrow++];
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000372
373 /* Special case for first column */
374 thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
375 nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
376 *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000377 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000378 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
379
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000380 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000381 /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
382 /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
383 nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
384 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 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;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000387 }
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000388
389 /* Special case for last column */
390 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000391 *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000392 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000393 inrow++;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000394 }
395}
396
397
398/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000399 * Module initialization routine for upsampling.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000400 */
401
Thomas G. Lane489583f1996-02-07 00:00:00 +0000402GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000403jinit_upsampler (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000404{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000405 my_upsample_ptr upsample;
406 int ci;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000407 jpeg_component_info * compptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000408 boolean need_buffer, do_fancy;
409 int h_in_group, v_in_group, h_out_group, v_out_group;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000410
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000411 upsample = (my_upsample_ptr)
412 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
413 SIZEOF(my_upsampler));
414 cinfo->upsample = (struct jpeg_upsampler *) upsample;
415 upsample->pub.start_pass = start_pass_upsample;
416 upsample->pub.upsample = sep_upsample;
417 upsample->pub.need_context_rows = FALSE; /* until we find out differently */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000418
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000419 if (cinfo->CCIR601_sampling) /* this isn't supported */
420 ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
421
422 /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
423 * so don't ask for it.
424 */
DRC49967cd2010-10-09 19:57:51 +0000425 do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000426
427 /* Verify we can handle the sampling factors, select per-component methods,
428 * and create storage as needed.
429 */
430 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
431 ci++, compptr++) {
432 /* Compute size of an "input group" after IDCT scaling. This many samples
433 * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
434 */
DRC49967cd2010-10-09 19:57:51 +0000435 h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
436 cinfo->_min_DCT_scaled_size;
437 v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
438 cinfo->_min_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000439 h_out_group = cinfo->max_h_samp_factor;
440 v_out_group = cinfo->max_v_samp_factor;
441 upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
442 need_buffer = TRUE;
443 if (! compptr->component_needed) {
444 /* Don't bother to upsample an uninteresting component. */
445 upsample->methods[ci] = noop_upsample;
446 need_buffer = FALSE;
447 } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
448 /* Fullsize components can be processed without any work. */
449 upsample->methods[ci] = fullsize_upsample;
450 need_buffer = FALSE;
451 } else if (h_in_group * 2 == h_out_group &&
452 v_in_group == v_out_group) {
453 /* Special cases for 2h1v upsampling */
Pierre Ossman59a39382009-03-09 13:15:56 +0000454 if (do_fancy && compptr->downsampled_width > 2) {
455 if (jsimd_can_h2v1_fancy_upsample())
456 upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
457 else
458 upsample->methods[ci] = h2v1_fancy_upsample;
459 } else {
460 if (jsimd_can_h2v1_upsample())
461 upsample->methods[ci] = jsimd_h2v1_upsample;
462 else
463 upsample->methods[ci] = h2v1_upsample;
464 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000465 } else if (h_in_group * 2 == h_out_group &&
466 v_in_group * 2 == v_out_group) {
467 /* Special cases for 2h2v upsampling */
468 if (do_fancy && compptr->downsampled_width > 2) {
Pierre Ossman59a39382009-03-09 13:15:56 +0000469 if (jsimd_can_h2v2_fancy_upsample())
470 upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
471 else
472 upsample->methods[ci] = h2v2_fancy_upsample;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000473 upsample->pub.need_context_rows = TRUE;
Pierre Ossman59a39382009-03-09 13:15:56 +0000474 } else {
475 if (jsimd_can_h2v2_upsample())
476 upsample->methods[ci] = jsimd_h2v2_upsample;
477 else
478 upsample->methods[ci] = h2v2_upsample;
479 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000480 } else if ((h_out_group % h_in_group) == 0 &&
481 (v_out_group % v_in_group) == 0) {
482 /* Generic integral-factors upsampling method */
483 upsample->methods[ci] = int_upsample;
484 upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
485 upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
486 } else
487 ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
488 if (need_buffer) {
489 upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
490 ((j_common_ptr) cinfo, JPOOL_IMAGE,
491 (JDIMENSION) jround_up((long) cinfo->output_width,
492 (long) cinfo->max_h_samp_factor),
493 (JDIMENSION) cinfo->max_v_samp_factor);
494 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000495 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000496}