7 * Copyright (C) 1991-1994, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
11 * For conditions of distribution and use, see the accompanying README file.
15 * This file contains upsampling routines.
19 * Upsampling input data is counted in "row groups". A row group
21 * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
23 * sample rows of each component. Upsampling will normally produce
25 * max_v_samp_factor pixel rows from each row group (but this could vary
27 * if the upsampler is applying a scale factor of its own).
31 * An excellent reference for image resampling is
33 * Digital Image Warping, George Wolberg, 1990.
35 * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
41 #define JPEG_INTERNALS
45 #include "radiant_jpeglib.h"
51 /* Pointer to routine to upsample a single component */
53 typedef JMETHOD(void, upsample1_ptr,
55 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
57 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));
61 /* Private subobject */
67 struct jpeg_upsampler pub; /* public fields */
71 /* Color conversion buffer. When using separate upsampling and color
73 * conversion steps, this buffer holds one upsampled row group until it
75 * has been color converted and output.
77 * Note: we do not allocate any storage for component(s) which are full-size,
79 * ie do not need rescaling. The corresponding entry of color_buf[] is
81 * simply set to point to the input data array, thereby avoiding copying.
85 JSAMPARRAY color_buf[MAX_COMPONENTS];
89 /* Per-component upsampling method pointers */
91 upsample1_ptr methods[MAX_COMPONENTS];
95 int next_row_out; /* counts rows emitted from color_buf */
97 JDIMENSION rows_to_go; /* counts rows remaining in image */
101 /* Height of an input row group for each component. */
103 int rowgroup_height[MAX_COMPONENTS];
107 /* These arrays save pixel expansion factors so that int_expand need not
109 * recompute them each time. They are unused for other upsampling methods.
113 UINT8 h_expand[MAX_COMPONENTS];
115 UINT8 v_expand[MAX_COMPONENTS];
121 typedef my_upsampler * my_upsample_ptr;
129 * Initialize for an upsampling pass.
137 start_pass_upsample (j_decompress_ptr cinfo)
141 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
145 /* Mark the conversion buffer empty */
147 upsample->next_row_out = cinfo->max_v_samp_factor;
149 /* Initialize total-height counter for detecting bottom of image */
151 upsample->rows_to_go = cinfo->output_height;
161 * Control routine to do upsampling (and color conversion).
165 * In this version we upsample each component independently.
167 * We upsample one row group into the conversion buffer, then apply
169 * color conversion a row at a time.
177 sep_upsample (j_decompress_ptr cinfo,
179 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
181 JDIMENSION in_row_groups_avail,
183 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
185 JDIMENSION out_rows_avail)
189 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
193 jpeg_component_info * compptr;
199 /* Fill the conversion buffer, if it's empty */
201 if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
203 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
207 /* Invoke per-component upsample method. Notice we pass a POINTER
209 * to color_buf[ci], so that fullsize_upsample can change it.
213 (*upsample->methods[ci]) (cinfo, compptr,
215 input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
217 upsample->color_buf + ci);
221 upsample->next_row_out = 0;
227 /* Color-convert and emit rows */
231 /* How many we have in the buffer: */
233 num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
235 /* Not more than the distance to the end of the image. Need this test
237 * in case the image height is not a multiple of max_v_samp_factor:
241 if (num_rows > upsample->rows_to_go)
243 num_rows = upsample->rows_to_go;
245 /* And not more than what the client can accept: */
247 out_rows_avail -= *out_row_ctr;
249 if (num_rows > out_rows_avail)
251 num_rows = out_rows_avail;
255 (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
257 (JDIMENSION) upsample->next_row_out,
259 output_buf + *out_row_ctr,
267 *out_row_ctr += num_rows;
269 upsample->rows_to_go -= num_rows;
271 upsample->next_row_out += num_rows;
273 /* When the buffer is emptied, declare this input row group consumed */
275 if (upsample->next_row_out >= cinfo->max_v_samp_factor)
277 (*in_row_group_ctr)++;
287 * These are the routines invoked by sep_upsample to upsample pixel values
289 * of a single component. One row group is processed per call.
299 * For full-size components, we just make color_buf[ci] point at the
301 * input buffer, and thus avoid copying any data. Note that this is
303 * safe only because sep_upsample doesn't declare the input row group
305 * "consumed" until we are done color converting and emitting it.
313 fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
315 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
319 *output_data_ptr = input_data;
329 * This is a no-op version used for "uninteresting" components.
331 * These components will not be referenced by color conversion.
339 noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
341 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
345 *output_data_ptr = NULL; /* safety check */
355 * This version handles any integral sampling ratios.
357 * This is not used for typical JPEG files, so it need not be fast.
359 * Nor, for that matter, is it particularly accurate: the algorithm is
361 * simple replication of the input pixel onto the corresponding output
363 * pixels. The hi-falutin sampling literature refers to this as a
365 * "box filter". A box filter tends to introduce visible artifacts,
367 * so if you are actually going to use 3:1 or 4:1 sampling ratios
369 * you would be well advised to improve this code.
377 int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
379 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
383 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
385 JSAMPARRAY output_data = *output_data_ptr;
387 register JSAMPROW inptr, outptr;
389 register JSAMPLE invalue;
395 int h_expand, v_expand;
401 h_expand = upsample->h_expand[compptr->component_index];
403 v_expand = upsample->v_expand[compptr->component_index];
409 while (outrow < cinfo->max_v_samp_factor) {
411 /* Generate one output row with proper horizontal expansion */
413 inptr = input_data[inrow];
415 outptr = output_data[outrow];
417 outend = outptr + cinfo->output_width;
419 while (outptr < outend) {
421 invalue = *inptr++; /* don't need GETJSAMPLE() here */
423 for (h = h_expand; h > 0; h--) {
431 /* Generate any additional output rows by duplicating the first one */
435 jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
437 v_expand-1, cinfo->output_width);
455 * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
457 * It's still a box filter.
465 h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
467 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
471 JSAMPARRAY output_data = *output_data_ptr;
473 register JSAMPROW inptr, outptr;
475 register JSAMPLE invalue;
483 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
485 inptr = input_data[inrow];
487 outptr = output_data[inrow];
489 outend = outptr + cinfo->output_width;
491 while (outptr < outend) {
493 invalue = *inptr++; /* don't need GETJSAMPLE() here */
511 * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
513 * It's still a box filter.
521 h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
523 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
527 JSAMPARRAY output_data = *output_data_ptr;
529 register JSAMPROW inptr, outptr;
531 register JSAMPLE invalue;
541 while (outrow < cinfo->max_v_samp_factor) {
543 inptr = input_data[inrow];
545 outptr = output_data[outrow];
547 outend = outptr + cinfo->output_width;
549 while (outptr < outend) {
551 invalue = *inptr++; /* don't need GETJSAMPLE() here */
559 jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
561 1, cinfo->output_width);
577 * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
581 * The upsampling algorithm is linear interpolation between pixel centers,
583 * also known as a "triangle filter". This is a good compromise between
585 * speed and visual quality. The centers of the output pixels are 1/4 and 3/4
587 * of the way between input pixel centers.
591 * A note about the "bias" calculations: when rounding fractional values to
593 * integer, we do not want to always round 0.5 up to the next integer.
595 * If we did that, we'd introduce a noticeable bias towards larger values.
597 * Instead, this code is arranged so that 0.5 will be rounded up or down at
599 * alternate pixel locations (a simple ordered dither pattern).
607 h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
609 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
613 JSAMPARRAY output_data = *output_data_ptr;
615 register JSAMPROW inptr, outptr;
617 register int invalue;
619 register JDIMENSION colctr;
625 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
627 inptr = input_data[inrow];
629 outptr = output_data[inrow];
631 /* Special case for first column */
633 invalue = GETJSAMPLE(*inptr++);
635 *outptr++ = (JSAMPLE) invalue;
637 *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
641 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
643 /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
645 invalue = GETJSAMPLE(*inptr++) * 3;
647 *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
649 *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
655 /* Special case for last column */
657 invalue = GETJSAMPLE(*inptr);
659 *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
661 *outptr++ = (JSAMPLE) invalue;
673 * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
675 * Again a triangle filter; see comments for h2v1 case, above.
679 * It is OK for us to reference the adjacent input rows because we demanded
681 * context from the main buffer controller (see initialization code).
689 h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
691 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
695 JSAMPARRAY output_data = *output_data_ptr;
697 register JSAMPROW inptr0, inptr1, outptr;
699 #if BITS_IN_JSAMPLE == 8
701 register int thiscolsum, lastcolsum, nextcolsum;
705 register INT32 thiscolsum, lastcolsum, nextcolsum;
709 register JDIMENSION colctr;
711 int inrow, outrow, v;
717 while (outrow < cinfo->max_v_samp_factor) {
719 for (v = 0; v < 2; v++) {
721 /* inptr0 points to nearest input row, inptr1 points to next nearest */
723 inptr0 = input_data[inrow];
725 if (v == 0) /* next nearest is row above */
727 inptr1 = input_data[inrow-1];
729 else /* next nearest is row below */
731 inptr1 = input_data[inrow+1];
733 outptr = output_data[outrow++];
737 /* Special case for first column */
739 thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
741 nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
743 *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
745 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
747 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
751 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
753 /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
755 /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
757 nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
759 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
761 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
763 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
769 /* Special case for last column */
771 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
773 *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
789 * Module initialization routine for upsampling.
797 jinit_upsampler (j_decompress_ptr cinfo)
801 my_upsample_ptr upsample;
805 jpeg_component_info * compptr;
807 boolean need_buffer, do_fancy;
809 int h_in_group, v_in_group, h_out_group, v_out_group;
813 upsample = (my_upsample_ptr)
815 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
817 SIZEOF(my_upsampler));
819 cinfo->upsample = (struct jpeg_upsampler *) upsample;
821 upsample->pub.start_pass = start_pass_upsample;
823 upsample->pub.upsample = sep_upsample;
825 upsample->pub.need_context_rows = FALSE; /* until we find out differently */
829 if (cinfo->CCIR601_sampling) /* this isn't supported */
831 ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
835 /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
837 * so don't ask for it.
841 do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1;
845 /* Verify we can handle the sampling factors, select per-component methods,
847 * and create storage as needed.
851 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
855 /* Compute size of an "input group" after IDCT scaling. This many samples
857 * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
861 h_in_group = (compptr->h_samp_factor * compptr->DCT_scaled_size) /
863 cinfo->min_DCT_scaled_size;
865 v_in_group = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
867 cinfo->min_DCT_scaled_size;
869 h_out_group = cinfo->max_h_samp_factor;
871 v_out_group = cinfo->max_v_samp_factor;
873 upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
877 if (! compptr->component_needed) {
879 /* Don't bother to upsample an uninteresting component. */
881 upsample->methods[ci] = noop_upsample;
885 } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
887 /* Fullsize components can be processed without any work. */
889 upsample->methods[ci] = fullsize_upsample;
893 } else if (h_in_group * 2 == h_out_group &&
895 v_in_group == v_out_group) {
897 /* Special cases for 2h1v upsampling */
899 if (do_fancy && compptr->downsampled_width > 2)
901 upsample->methods[ci] = h2v1_fancy_upsample;
905 upsample->methods[ci] = h2v1_upsample;
907 } else if (h_in_group * 2 == h_out_group &&
909 v_in_group * 2 == v_out_group) {
911 /* Special cases for 2h2v upsampling */
913 if (do_fancy && compptr->downsampled_width > 2) {
915 upsample->methods[ci] = h2v2_fancy_upsample;
917 upsample->pub.need_context_rows = TRUE;
921 upsample->methods[ci] = h2v2_upsample;
923 } else if ((h_out_group % h_in_group) == 0 &&
925 (v_out_group % v_in_group) == 0) {
927 /* Generic integral-factors upsampling method */
929 upsample->methods[ci] = int_upsample;
931 upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
933 upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
937 ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
941 upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
943 ((j_common_ptr) cinfo, JPOOL_IMAGE,
945 (JDIMENSION) jround_up((long) cinfo->output_width,
947 (long) cinfo->max_h_samp_factor),
949 (JDIMENSION) cinfo->max_v_samp_factor);