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