2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: jdsample.c /main/2 1996/05/09 03:49:53 drk $ */
27 * Copyright (C) 1991-1996, Thomas G. Lane.
28 * This file is part of the Independent JPEG Group's software.
29 * For conditions of distribution and use, see the accompanying README file.
31 * This file contains upsampling routines.
33 * Upsampling input data is counted in "row groups". A row group
34 * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
35 * sample rows of each component. Upsampling will normally produce
36 * max_v_samp_factor pixel rows from each row group (but this could vary
37 * if the upsampler is applying a scale factor of its own).
39 * An excellent reference for image resampling is
40 * Digital Image Warping, George Wolberg, 1990.
41 * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
44 #define JPEG_INTERNALS
49 /* Pointer to routine to upsample a single component */
50 typedef JMETHOD(void, upsample1_ptr,
51 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
52 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));
54 /* Private subobject */
57 struct jpeg_upsampler pub; /* public fields */
59 /* Color conversion buffer. When using separate upsampling and color
60 * conversion steps, this buffer holds one upsampled row group until it
61 * has been color converted and output.
62 * Note: we do not allocate any storage for component(s) which are full-size,
63 * ie do not need rescaling. The corresponding entry of color_buf[] is
64 * simply set to point to the input data array, thereby avoiding copying.
66 JSAMPARRAY color_buf[MAX_COMPONENTS];
68 /* Per-component upsampling method pointers */
69 upsample1_ptr methods[MAX_COMPONENTS];
71 int next_row_out; /* counts rows emitted from color_buf */
72 JDIMENSION rows_to_go; /* counts rows remaining in image */
74 /* Height of an input row group for each component. */
75 int rowgroup_height[MAX_COMPONENTS];
77 /* These arrays save pixel expansion factors so that int_expand need not
78 * recompute them each time. They are unused for other upsampling methods.
80 UINT8 h_expand[MAX_COMPONENTS];
81 UINT8 v_expand[MAX_COMPONENTS];
84 typedef my_upsampler * my_upsample_ptr;
88 * Initialize for an upsampling pass.
92 start_pass_upsample (j_decompress_ptr cinfo)
94 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
96 /* Mark the conversion buffer empty */
97 upsample->next_row_out = cinfo->max_v_samp_factor;
98 /* Initialize total-height counter for detecting bottom of image */
99 upsample->rows_to_go = cinfo->output_height;
104 * Control routine to do upsampling (and color conversion).
106 * In this version we upsample each component independently.
107 * We upsample one row group into the conversion buffer, then apply
108 * color conversion a row at a time.
112 sep_upsample (j_decompress_ptr cinfo,
113 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
114 JDIMENSION in_row_groups_avail,
115 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
116 JDIMENSION out_rows_avail)
118 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
120 jpeg_component_info * compptr;
123 /* Fill the conversion buffer, if it's empty */
124 if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
125 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
127 /* Invoke per-component upsample method. Notice we pass a POINTER
128 * to color_buf[ci], so that fullsize_upsample can change it.
130 (*upsample->methods[ci]) (cinfo, compptr,
131 input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
132 upsample->color_buf + ci);
134 upsample->next_row_out = 0;
137 /* Color-convert and emit rows */
139 /* How many we have in the buffer: */
140 num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
141 /* Not more than the distance to the end of the image. Need this test
142 * in case the image height is not a multiple of max_v_samp_factor:
144 if (num_rows > upsample->rows_to_go)
145 num_rows = upsample->rows_to_go;
146 /* And not more than what the client can accept: */
147 out_rows_avail -= *out_row_ctr;
148 if (num_rows > out_rows_avail)
149 num_rows = out_rows_avail;
151 (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
152 (JDIMENSION) upsample->next_row_out,
153 output_buf + *out_row_ctr,
157 *out_row_ctr += num_rows;
158 upsample->rows_to_go -= num_rows;
159 upsample->next_row_out += num_rows;
160 /* When the buffer is emptied, declare this input row group consumed */
161 if (upsample->next_row_out >= cinfo->max_v_samp_factor)
162 (*in_row_group_ctr)++;
167 * These are the routines invoked by sep_upsample to upsample pixel values
168 * of a single component. One row group is processed per call.
173 * For full-size components, we just make color_buf[ci] point at the
174 * input buffer, and thus avoid copying any data. Note that this is
175 * safe only because sep_upsample doesn't declare the input row group
176 * "consumed" until we are done color converting and emitting it.
180 fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
181 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
183 *output_data_ptr = input_data;
188 * This is a no-op version used for "uninteresting" components.
189 * These components will not be referenced by color conversion.
193 noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
194 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
196 *output_data_ptr = NULL; /* safety check */
201 * This version handles any integral sampling ratios.
202 * This is not used for typical JPEG files, so it need not be fast.
203 * Nor, for that matter, is it particularly accurate: the algorithm is
204 * simple replication of the input pixel onto the corresponding output
205 * pixels. The hi-falutin sampling literature refers to this as a
206 * "box filter". A box filter tends to introduce visible artifacts,
207 * so if you are actually going to use 3:1 or 4:1 sampling ratios
208 * you would be well advised to improve this code.
212 int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
213 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
215 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
216 JSAMPARRAY output_data = *output_data_ptr;
217 JSAMPROW inptr, outptr;
221 int h_expand, v_expand;
224 h_expand = upsample->h_expand[compptr->component_index];
225 v_expand = upsample->v_expand[compptr->component_index];
228 while (outrow < cinfo->max_v_samp_factor) {
229 /* Generate one output row with proper horizontal expansion */
230 inptr = input_data[inrow];
231 outptr = output_data[outrow];
232 outend = outptr + cinfo->output_width;
233 while (outptr < outend) {
234 invalue = *inptr++; /* don't need GETJSAMPLE() here */
235 for (h = h_expand; h > 0; h--) {
239 /* Generate any additional output rows by duplicating the first one */
241 jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
242 v_expand-1, cinfo->output_width);
251 * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
252 * It's still a box filter.
256 h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
257 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
259 JSAMPARRAY output_data = *output_data_ptr;
260 JSAMPROW inptr, outptr;
265 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
266 inptr = input_data[inrow];
267 outptr = output_data[inrow];
268 outend = outptr + cinfo->output_width;
269 while (outptr < outend) {
270 invalue = *inptr++; /* don't need GETJSAMPLE() here */
279 * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
280 * It's still a box filter.
284 h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
285 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
287 JSAMPARRAY output_data = *output_data_ptr;
288 JSAMPROW inptr, outptr;
294 while (outrow < cinfo->max_v_samp_factor) {
295 inptr = input_data[inrow];
296 outptr = output_data[outrow];
297 outend = outptr + cinfo->output_width;
298 while (outptr < outend) {
299 invalue = *inptr++; /* don't need GETJSAMPLE() here */
303 jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
304 1, cinfo->output_width);
312 * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
314 * The upsampling algorithm is linear interpolation between pixel centers,
315 * also known as a "triangle filter". This is a good compromise between
316 * speed and visual quality. The centers of the output pixels are 1/4 and 3/4
317 * of the way between input pixel centers.
319 * A note about the "bias" calculations: when rounding fractional values to
320 * integer, we do not want to always round 0.5 up to the next integer.
321 * If we did that, we'd introduce a noticeable bias towards larger values.
322 * Instead, this code is arranged so that 0.5 will be rounded up or down at
323 * alternate pixel locations (a simple ordered dither pattern).
327 h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
328 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
330 JSAMPARRAY output_data = *output_data_ptr;
331 JSAMPROW inptr, outptr;
336 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
337 inptr = input_data[inrow];
338 outptr = output_data[inrow];
339 /* Special case for first column */
340 invalue = GETJSAMPLE(*inptr++);
341 *outptr++ = (JSAMPLE) invalue;
342 *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
344 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
345 /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
346 invalue = GETJSAMPLE(*inptr++) * 3;
347 *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
348 *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
351 /* Special case for last column */
352 invalue = GETJSAMPLE(*inptr);
353 *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
354 *outptr++ = (JSAMPLE) invalue;
360 * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
361 * Again a triangle filter; see comments for h2v1 case, above.
363 * It is OK for us to reference the adjacent input rows because we demanded
364 * context from the main buffer controller (see initialization code).
368 h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
369 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
371 JSAMPARRAY output_data = *output_data_ptr;
372 JSAMPROW inptr0, inptr1, outptr;
373 #if BITS_IN_JSAMPLE == 8
374 int thiscolsum, lastcolsum, nextcolsum;
376 INT32 thiscolsum, lastcolsum, nextcolsum;
379 int inrow, outrow, v;
382 while (outrow < cinfo->max_v_samp_factor) {
383 for (v = 0; v < 2; v++) {
384 /* inptr0 points to nearest input row, inptr1 points to next nearest */
385 inptr0 = input_data[inrow];
386 if (v == 0) /* next nearest is row above */
387 inptr1 = input_data[inrow-1];
388 else /* next nearest is row below */
389 inptr1 = input_data[inrow+1];
390 outptr = output_data[outrow++];
392 /* Special case for first column */
393 thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
394 nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
395 *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
396 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
397 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
399 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
400 /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
401 /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
402 nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
403 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
404 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
405 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
408 /* Special case for last column */
409 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
410 *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
418 * Module initialization routine for upsampling.
422 jinit_upsampler (j_decompress_ptr cinfo)
424 my_upsample_ptr upsample;
426 jpeg_component_info * compptr;
427 boolean need_buffer, do_fancy;
428 int h_in_group, v_in_group, h_out_group, v_out_group;
430 upsample = (my_upsample_ptr)
431 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
432 SIZEOF(my_upsampler));
433 cinfo->upsample = (struct jpeg_upsampler *) upsample;
434 upsample->pub.start_pass = start_pass_upsample;
435 upsample->pub.upsample = sep_upsample;
436 upsample->pub.need_context_rows = FALSE; /* until we find out differently */
438 if (cinfo->CCIR601_sampling) /* this isn't supported */
439 ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
441 /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
442 * so don't ask for it.
444 do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1;
446 /* Verify we can handle the sampling factors, select per-component methods,
447 * and create storage as needed.
449 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
451 /* Compute size of an "input group" after IDCT scaling. This many samples
452 * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
454 h_in_group = (compptr->h_samp_factor * compptr->DCT_scaled_size) /
455 cinfo->min_DCT_scaled_size;
456 v_in_group = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
457 cinfo->min_DCT_scaled_size;
458 h_out_group = cinfo->max_h_samp_factor;
459 v_out_group = cinfo->max_v_samp_factor;
460 upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
462 if (! compptr->component_needed) {
463 /* Don't bother to upsample an uninteresting component. */
464 upsample->methods[ci] = noop_upsample;
466 } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467 /* Fullsize components can be processed without any work. */
468 upsample->methods[ci] = fullsize_upsample;
470 } else if (h_in_group * 2 == h_out_group &&
471 v_in_group == v_out_group) {
472 /* Special cases for 2h1v upsampling */
473 if (do_fancy && compptr->downsampled_width > 2)
474 upsample->methods[ci] = h2v1_fancy_upsample;
476 upsample->methods[ci] = h2v1_upsample;
477 } else if (h_in_group * 2 == h_out_group &&
478 v_in_group * 2 == v_out_group) {
479 /* Special cases for 2h2v upsampling */
480 if (do_fancy && compptr->downsampled_width > 2) {
481 upsample->methods[ci] = h2v2_fancy_upsample;
482 upsample->pub.need_context_rows = TRUE;
484 upsample->methods[ci] = h2v2_upsample;
485 } else if ((h_out_group % h_in_group) == 0 &&
486 (v_out_group % v_in_group) == 0) {
487 /* Generic integral-factors upsampling method */
488 upsample->methods[ci] = int_upsample;
489 upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
490 upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
492 ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
494 upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
495 ((j_common_ptr) cinfo, JPOOL_IMAGE,
496 (JDIMENSION) jround_up((long) cinfo->output_width,
497 (long) cinfo->max_h_samp_factor),
498 (JDIMENSION) cinfo->max_v_samp_factor);