blob: 4e0b8b4077d3c22cadd28d323c1bd568ca0f751a [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
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00006 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
8 *
Thomas G. Lane88aeed41992-12-10 00:00:00 +00009 * This file contains upsampling routines.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000010 *
11 * Upsampling input data is counted in "row groups". A row group
12 * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
13 * sample rows of each component. Upsampling will normally produce
14 * max_v_samp_factor pixel rows from each row group (but this could vary
15 * if the upsampler is applying a scale factor of its own).
Thomas G. Lane88aeed41992-12-10 00:00:00 +000016 *
17 * An excellent reference for image resampling is
18 * Digital Image Warping, George Wolberg, 1990.
19 * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000020 */
21
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000022#define JPEG_INTERNALS
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000023#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000024#include "jpeglib.h"
Pierre Ossman59a39382009-03-09 13:15:56 +000025#include "jsimd.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000026
27
28/* Pointer to routine to upsample a single component */
29typedef JMETHOD(void, upsample1_ptr,
30 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
31 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));
32
33/* Private subobject */
34
35typedef struct {
36 struct jpeg_upsampler pub; /* public fields */
37
38 /* Color conversion buffer. When using separate upsampling and color
39 * conversion steps, this buffer holds one upsampled row group until it
40 * has been color converted and output.
41 * Note: we do not allocate any storage for component(s) which are full-size,
42 * ie do not need rescaling. The corresponding entry of color_buf[] is
43 * simply set to point to the input data array, thereby avoiding copying.
44 */
45 JSAMPARRAY color_buf[MAX_COMPONENTS];
46
47 /* Per-component upsampling method pointers */
48 upsample1_ptr methods[MAX_COMPONENTS];
49
50 int next_row_out; /* counts rows emitted from color_buf */
51 JDIMENSION rows_to_go; /* counts rows remaining in image */
52
53 /* Height of an input row group for each component. */
54 int rowgroup_height[MAX_COMPONENTS];
55
56 /* These arrays save pixel expansion factors so that int_expand need not
57 * recompute them each time. They are unused for other upsampling methods.
58 */
59 UINT8 h_expand[MAX_COMPONENTS];
60 UINT8 v_expand[MAX_COMPONENTS];
61} my_upsampler;
62
63typedef my_upsampler * my_upsample_ptr;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000064
65
66/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000067 * Initialize for an upsampling pass.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000068 */
69
Thomas G. Lane489583f1996-02-07 00:00:00 +000070METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000071start_pass_upsample (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000072{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000073 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
74
75 /* Mark the conversion buffer empty */
76 upsample->next_row_out = cinfo->max_v_samp_factor;
77 /* Initialize total-height counter for detecting bottom of image */
78 upsample->rows_to_go = cinfo->output_height;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000079}
80
81
82/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000083 * Control routine to do upsampling (and color conversion).
Thomas G. Lane88aeed41992-12-10 00:00:00 +000084 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000085 * In this version we upsample each component independently.
86 * We upsample one row group into the conversion buffer, then apply
87 * color conversion a row at a time.
88 */
89
Thomas G. Lane489583f1996-02-07 00:00:00 +000090METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000091sep_upsample (j_decompress_ptr cinfo,
92 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
93 JDIMENSION in_row_groups_avail,
94 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
95 JDIMENSION out_rows_avail)
96{
97 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
98 int ci;
99 jpeg_component_info * compptr;
100 JDIMENSION num_rows;
101
102 /* Fill the conversion buffer, if it's empty */
103 if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
104 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
105 ci++, compptr++) {
106 /* Invoke per-component upsample method. Notice we pass a POINTER
107 * to color_buf[ci], so that fullsize_upsample can change it.
108 */
109 (*upsample->methods[ci]) (cinfo, compptr,
110 input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
111 upsample->color_buf + ci);
112 }
113 upsample->next_row_out = 0;
114 }
115
116 /* Color-convert and emit rows */
117
118 /* How many we have in the buffer: */
119 num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
120 /* Not more than the distance to the end of the image. Need this test
121 * in case the image height is not a multiple of max_v_samp_factor:
122 */
123 if (num_rows > upsample->rows_to_go)
124 num_rows = upsample->rows_to_go;
125 /* And not more than what the client can accept: */
126 out_rows_avail -= *out_row_ctr;
127 if (num_rows > out_rows_avail)
128 num_rows = out_rows_avail;
129
130 (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
131 (JDIMENSION) upsample->next_row_out,
132 output_buf + *out_row_ctr,
133 (int) num_rows);
134
135 /* Adjust counts */
136 *out_row_ctr += num_rows;
137 upsample->rows_to_go -= num_rows;
138 upsample->next_row_out += num_rows;
139 /* When the buffer is emptied, declare this input row group consumed */
140 if (upsample->next_row_out >= cinfo->max_v_samp_factor)
141 (*in_row_group_ctr)++;
142}
143
144
145/*
146 * These are the routines invoked by sep_upsample to upsample pixel values
147 * of a single component. One row group is processed per call.
148 */
149
150
151/*
152 * For full-size components, we just make color_buf[ci] point at the
153 * input buffer, and thus avoid copying any data. Note that this is
154 * safe only because sep_upsample doesn't declare the input row group
155 * "consumed" until we are done color converting and emitting it.
156 */
157
Thomas G. Lane489583f1996-02-07 00:00:00 +0000158METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000159fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
160 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
161{
162 *output_data_ptr = input_data;
163}
164
165
166/*
167 * This is a no-op version used for "uninteresting" components.
168 * These components will not be referenced by color conversion.
169 */
170
Thomas G. Lane489583f1996-02-07 00:00:00 +0000171METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000172noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
173 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
174{
175 *output_data_ptr = NULL; /* safety check */
176}
177
178
179/*
180 * This version handles any integral sampling ratios.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000181 * This is not used for typical JPEG files, so it need not be fast.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000182 * Nor, for that matter, is it particularly accurate: the algorithm is
183 * simple replication of the input pixel onto the corresponding output
184 * pixels. The hi-falutin sampling literature refers to this as a
185 * "box filter". A box filter tends to introduce visible artifacts,
186 * so if you are actually going to use 3:1 or 4:1 sampling ratios
187 * you would be well advised to improve this code.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000188 */
189
Thomas G. Lane489583f1996-02-07 00:00:00 +0000190METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000191int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
192 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000193{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000194 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
195 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000196 register JSAMPROW inptr, outptr;
197 register JSAMPLE invalue;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000198 register int h;
199 JSAMPROW outend;
200 int h_expand, v_expand;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000201 int inrow, outrow;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000202
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000203 h_expand = upsample->h_expand[compptr->component_index];
204 v_expand = upsample->v_expand[compptr->component_index];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000205
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000206 inrow = outrow = 0;
207 while (outrow < cinfo->max_v_samp_factor) {
208 /* Generate one output row with proper horizontal expansion */
209 inptr = input_data[inrow];
210 outptr = output_data[outrow];
211 outend = outptr + cinfo->output_width;
212 while (outptr < outend) {
213 invalue = *inptr++; /* don't need GETJSAMPLE() here */
214 for (h = h_expand; h > 0; h--) {
215 *outptr++ = invalue;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000216 }
217 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000218 /* Generate any additional output rows by duplicating the first one */
219 if (v_expand > 1) {
220 jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
221 v_expand-1, cinfo->output_width);
222 }
223 inrow++;
224 outrow += v_expand;
225 }
226}
227
228
229/*
230 * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
231 * It's still a box filter.
232 */
233
Thomas G. Lane489583f1996-02-07 00:00:00 +0000234METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000235h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
236 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
237{
238 JSAMPARRAY output_data = *output_data_ptr;
239 register JSAMPROW inptr, outptr;
240 register JSAMPLE invalue;
241 JSAMPROW outend;
242 int inrow;
243
244 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
245 inptr = input_data[inrow];
246 outptr = output_data[inrow];
247 outend = outptr + cinfo->output_width;
248 while (outptr < outend) {
249 invalue = *inptr++; /* don't need GETJSAMPLE() here */
250 *outptr++ = invalue;
251 *outptr++ = invalue;
252 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000253 }
254}
255
256
257/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000258 * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
259 * It's still a box filter.
260 */
261
Thomas G. Lane489583f1996-02-07 00:00:00 +0000262METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000263h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
264 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
265{
266 JSAMPARRAY output_data = *output_data_ptr;
267 register JSAMPROW inptr, outptr;
268 register JSAMPLE invalue;
269 JSAMPROW outend;
270 int inrow, outrow;
271
272 inrow = outrow = 0;
273 while (outrow < cinfo->max_v_samp_factor) {
274 inptr = input_data[inrow];
275 outptr = output_data[outrow];
276 outend = outptr + cinfo->output_width;
277 while (outptr < outend) {
278 invalue = *inptr++; /* don't need GETJSAMPLE() here */
279 *outptr++ = invalue;
280 *outptr++ = invalue;
281 }
282 jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
283 1, cinfo->output_width);
284 inrow++;
285 outrow += 2;
286 }
287}
288
289
290/*
291 * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000292 *
293 * The upsampling algorithm is linear interpolation between pixel centers,
294 * also known as a "triangle filter". This is a good compromise between
295 * speed and visual quality. The centers of the output pixels are 1/4 and 3/4
296 * of the way between input pixel centers.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000297 *
298 * A note about the "bias" calculations: when rounding fractional values to
299 * integer, we do not want to always round 0.5 up to the next integer.
300 * If we did that, we'd introduce a noticeable bias towards larger values.
301 * Instead, this code is arranged so that 0.5 will be rounded up or down at
302 * alternate pixel locations (a simple ordered dither pattern).
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000303 */
304
Thomas G. Lane489583f1996-02-07 00:00:00 +0000305METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000306h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
307 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000308{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000309 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000310 register JSAMPROW inptr, outptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000311 register int invalue;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000312 register JDIMENSION colctr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000313 int inrow;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000314
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000315 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000316 inptr = input_data[inrow];
317 outptr = output_data[inrow];
318 /* Special case for first column */
319 invalue = GETJSAMPLE(*inptr++);
320 *outptr++ = (JSAMPLE) invalue;
321 *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000322
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000323 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000324 /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
325 invalue = GETJSAMPLE(*inptr++) * 3;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000326 *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000327 *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
328 }
329
330 /* Special case for last column */
331 invalue = GETJSAMPLE(*inptr);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000332 *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000333 *outptr++ = (JSAMPLE) invalue;
334 }
335}
336
337
338/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000339 * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
340 * Again a triangle filter; see comments for h2v1 case, above.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000341 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000342 * It is OK for us to reference the adjacent input rows because we demanded
343 * context from the main buffer controller (see initialization code).
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000344 */
345
Thomas G. Lane489583f1996-02-07 00:00:00 +0000346METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000347h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
348 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000349{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000350 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000351 register JSAMPROW inptr0, inptr1, outptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000352#if BITS_IN_JSAMPLE == 8
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000353 register int thiscolsum, lastcolsum, nextcolsum;
354#else
355 register INT32 thiscolsum, lastcolsum, nextcolsum;
356#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000357 register JDIMENSION colctr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000358 int inrow, outrow, v;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000359
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000360 inrow = outrow = 0;
361 while (outrow < cinfo->max_v_samp_factor) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000362 for (v = 0; v < 2; v++) {
363 /* inptr0 points to nearest input row, inptr1 points to next nearest */
364 inptr0 = input_data[inrow];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000365 if (v == 0) /* next nearest is row above */
366 inptr1 = input_data[inrow-1];
367 else /* next nearest is row below */
368 inptr1 = input_data[inrow+1];
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000369 outptr = output_data[outrow++];
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000370
371 /* Special case for first column */
372 thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
373 nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
374 *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000375 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000376 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
377
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000378 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000379 /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
380 /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
381 nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
382 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000383 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000384 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000385 }
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000386
387 /* Special case for last column */
388 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000389 *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000390 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000391 inrow++;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000392 }
393}
394
395
396/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000397 * Module initialization routine for upsampling.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000398 */
399
Thomas G. Lane489583f1996-02-07 00:00:00 +0000400GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000401jinit_upsampler (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000402{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000403 my_upsample_ptr upsample;
404 int ci;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000405 jpeg_component_info * compptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000406 boolean need_buffer, do_fancy;
407 int h_in_group, v_in_group, h_out_group, v_out_group;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000408
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000409 upsample = (my_upsample_ptr)
410 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
411 SIZEOF(my_upsampler));
412 cinfo->upsample = (struct jpeg_upsampler *) upsample;
413 upsample->pub.start_pass = start_pass_upsample;
414 upsample->pub.upsample = sep_upsample;
415 upsample->pub.need_context_rows = FALSE; /* until we find out differently */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000416
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000417 if (cinfo->CCIR601_sampling) /* this isn't supported */
418 ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
419
420 /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
421 * so don't ask for it.
422 */
423 do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1;
424
425 /* Verify we can handle the sampling factors, select per-component methods,
426 * and create storage as needed.
427 */
428 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
429 ci++, compptr++) {
430 /* Compute size of an "input group" after IDCT scaling. This many samples
431 * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
432 */
433 h_in_group = (compptr->h_samp_factor * compptr->DCT_scaled_size) /
434 cinfo->min_DCT_scaled_size;
435 v_in_group = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
436 cinfo->min_DCT_scaled_size;
437 h_out_group = cinfo->max_h_samp_factor;
438 v_out_group = cinfo->max_v_samp_factor;
439 upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
440 need_buffer = TRUE;
441 if (! compptr->component_needed) {
442 /* Don't bother to upsample an uninteresting component. */
443 upsample->methods[ci] = noop_upsample;
444 need_buffer = FALSE;
445 } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
446 /* Fullsize components can be processed without any work. */
447 upsample->methods[ci] = fullsize_upsample;
448 need_buffer = FALSE;
449 } else if (h_in_group * 2 == h_out_group &&
450 v_in_group == v_out_group) {
451 /* Special cases for 2h1v upsampling */
Pierre Ossman59a39382009-03-09 13:15:56 +0000452 if (do_fancy && compptr->downsampled_width > 2) {
453 if (jsimd_can_h2v1_fancy_upsample())
454 upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
455 else
456 upsample->methods[ci] = h2v1_fancy_upsample;
457 } else {
458 if (jsimd_can_h2v1_upsample())
459 upsample->methods[ci] = jsimd_h2v1_upsample;
460 else
461 upsample->methods[ci] = h2v1_upsample;
462 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000463 } else if (h_in_group * 2 == h_out_group &&
464 v_in_group * 2 == v_out_group) {
465 /* Special cases for 2h2v upsampling */
466 if (do_fancy && compptr->downsampled_width > 2) {
Pierre Ossman59a39382009-03-09 13:15:56 +0000467 if (jsimd_can_h2v2_fancy_upsample())
468 upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
469 else
470 upsample->methods[ci] = h2v2_fancy_upsample;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000471 upsample->pub.need_context_rows = TRUE;
Pierre Ossman59a39382009-03-09 13:15:56 +0000472 } else {
473 if (jsimd_can_h2v2_upsample())
474 upsample->methods[ci] = jsimd_h2v2_upsample;
475 else
476 upsample->methods[ci] = h2v2_upsample;
477 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000478 } else if ((h_out_group % h_in_group) == 0 &&
479 (v_out_group % v_in_group) == 0) {
480 /* Generic integral-factors upsampling method */
481 upsample->methods[ci] = int_upsample;
482 upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
483 upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
484 } else
485 ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
486 if (need_buffer) {
487 upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
488 ((j_common_ptr) cinfo, JPOOL_IMAGE,
489 (JDIMENSION) jround_up((long) cinfo->output_width,
490 (long) cinfo->max_h_samp_factor),
491 (JDIMENSION) cinfo->max_v_samp_factor);
492 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000493 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000494}