0N/A/*
0N/A * reserved comment block
0N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
0N/A * jdsample.c
0N/A *
0N/A * Copyright (C) 1991-1996, Thomas G. Lane.
0N/A * This file is part of the Independent JPEG Group's software.
0N/A * For conditions of distribution and use, see the accompanying README file.
0N/A *
0N/A * This file contains upsampling routines.
0N/A *
0N/A * Upsampling input data is counted in "row groups". A row group
0N/A * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
0N/A * sample rows of each component. Upsampling will normally produce
0N/A * max_v_samp_factor pixel rows from each row group (but this could vary
0N/A * if the upsampler is applying a scale factor of its own).
0N/A *
0N/A * An excellent reference for image resampling is
0N/A * Digital Image Warping, George Wolberg, 1990.
0N/A * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
0N/A */
0N/A
0N/A#define JPEG_INTERNALS
0N/A#include "jinclude.h"
0N/A#include "jpeglib.h"
0N/A
0N/A
0N/A/* Pointer to routine to upsample a single component */
0N/Atypedef JMETHOD(void, upsample1_ptr,
0N/A (j_decompress_ptr cinfo, jpeg_component_info * compptr,
0N/A JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));
0N/A
0N/A/* Private subobject */
0N/A
0N/Atypedef struct {
0N/A struct jpeg_upsampler pub; /* public fields */
0N/A
0N/A /* Color conversion buffer. When using separate upsampling and color
0N/A * conversion steps, this buffer holds one upsampled row group until it
0N/A * has been color converted and output.
0N/A * Note: we do not allocate any storage for component(s) which are full-size,
0N/A * ie do not need rescaling. The corresponding entry of color_buf[] is
0N/A * simply set to point to the input data array, thereby avoiding copying.
0N/A */
0N/A JSAMPARRAY color_buf[MAX_COMPONENTS];
0N/A
0N/A /* Per-component upsampling method pointers */
0N/A upsample1_ptr methods[MAX_COMPONENTS];
0N/A
0N/A int next_row_out; /* counts rows emitted from color_buf */
0N/A JDIMENSION rows_to_go; /* counts rows remaining in image */
0N/A
0N/A /* Height of an input row group for each component. */
0N/A int rowgroup_height[MAX_COMPONENTS];
0N/A
0N/A /* These arrays save pixel expansion factors so that int_expand need not
0N/A * recompute them each time. They are unused for other upsampling methods.
0N/A */
0N/A UINT8 h_expand[MAX_COMPONENTS];
0N/A UINT8 v_expand[MAX_COMPONENTS];
0N/A} my_upsampler;
0N/A
0N/Atypedef my_upsampler * my_upsample_ptr;
0N/A
0N/A
0N/A/*
0N/A * Initialize for an upsampling pass.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Astart_pass_upsample (j_decompress_ptr cinfo)
0N/A{
0N/A my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
0N/A
0N/A /* Mark the conversion buffer empty */
0N/A upsample->next_row_out = cinfo->max_v_samp_factor;
0N/A /* Initialize total-height counter for detecting bottom of image */
0N/A upsample->rows_to_go = cinfo->output_height;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Control routine to do upsampling (and color conversion).
0N/A *
0N/A * In this version we upsample each component independently.
0N/A * We upsample one row group into the conversion buffer, then apply
0N/A * color conversion a row at a time.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Asep_upsample (j_decompress_ptr cinfo,
0N/A JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
0N/A JDIMENSION in_row_groups_avail,
0N/A JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
0N/A JDIMENSION out_rows_avail)
0N/A{
0N/A my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
0N/A int ci;
0N/A jpeg_component_info * compptr;
0N/A JDIMENSION num_rows;
0N/A
0N/A /* Fill the conversion buffer, if it's empty */
0N/A if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
0N/A for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
0N/A ci++, compptr++) {
0N/A /* Invoke per-component upsample method. Notice we pass a POINTER
0N/A * to color_buf[ci], so that fullsize_upsample can change it.
0N/A */
0N/A (*upsample->methods[ci]) (cinfo, compptr,
0N/A input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
0N/A upsample->color_buf + ci);
0N/A }
0N/A upsample->next_row_out = 0;
0N/A }
0N/A
0N/A /* Color-convert and emit rows */
0N/A
0N/A /* How many we have in the buffer: */
0N/A num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
0N/A /* Not more than the distance to the end of the image. Need this test
0N/A * in case the image height is not a multiple of max_v_samp_factor:
0N/A */
0N/A if (num_rows > upsample->rows_to_go)
0N/A num_rows = upsample->rows_to_go;
0N/A /* And not more than what the client can accept: */
0N/A out_rows_avail -= *out_row_ctr;
0N/A if (num_rows > out_rows_avail)
0N/A num_rows = out_rows_avail;
0N/A
0N/A (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
0N/A (JDIMENSION) upsample->next_row_out,
0N/A output_buf + *out_row_ctr,
0N/A (int) num_rows);
0N/A
0N/A /* Adjust counts */
0N/A *out_row_ctr += num_rows;
0N/A upsample->rows_to_go -= num_rows;
0N/A upsample->next_row_out += num_rows;
0N/A /* When the buffer is emptied, declare this input row group consumed */
0N/A if (upsample->next_row_out >= cinfo->max_v_samp_factor)
0N/A (*in_row_group_ctr)++;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * These are the routines invoked by sep_upsample to upsample pixel values
0N/A * of a single component. One row group is processed per call.
0N/A */
0N/A
0N/A
0N/A/*
0N/A * For full-size components, we just make color_buf[ci] point at the
0N/A * input buffer, and thus avoid copying any data. Note that this is
0N/A * safe only because sep_upsample doesn't declare the input row group
0N/A * "consumed" until we are done color converting and emitting it.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Afullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
0N/A JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
0N/A{
0N/A *output_data_ptr = input_data;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * This is a no-op version used for "uninteresting" components.
0N/A * These components will not be referenced by color conversion.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Anoop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
0N/A JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
0N/A{
0N/A *output_data_ptr = NULL; /* safety check */
0N/A}
0N/A
0N/A
0N/A/*
0N/A * This version handles any integral sampling ratios.
0N/A * This is not used for typical JPEG files, so it need not be fast.
0N/A * Nor, for that matter, is it particularly accurate: the algorithm is
0N/A * simple replication of the input pixel onto the corresponding output
0N/A * pixels. The hi-falutin sampling literature refers to this as a
0N/A * "box filter". A box filter tends to introduce visible artifacts,
0N/A * so if you are actually going to use 3:1 or 4:1 sampling ratios
0N/A * you would be well advised to improve this code.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Aint_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
0N/A JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
0N/A{
0N/A my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
0N/A JSAMPARRAY output_data = *output_data_ptr;
0N/A register JSAMPROW inptr, outptr;
0N/A register JSAMPLE invalue;
0N/A register int h;
0N/A JSAMPROW outend;
0N/A int h_expand, v_expand;
0N/A int inrow, outrow;
0N/A
0N/A h_expand = upsample->h_expand[compptr->component_index];
0N/A v_expand = upsample->v_expand[compptr->component_index];
0N/A
0N/A inrow = outrow = 0;
0N/A while (outrow < cinfo->max_v_samp_factor) {
0N/A /* Generate one output row with proper horizontal expansion */
0N/A inptr = input_data[inrow];
0N/A outptr = output_data[outrow];
0N/A outend = outptr + cinfo->output_width;
0N/A while (outptr < outend) {
0N/A invalue = *inptr++; /* don't need GETJSAMPLE() here */
0N/A for (h = h_expand; h > 0; h--) {
0N/A *outptr++ = invalue;
0N/A }
0N/A }
0N/A /* Generate any additional output rows by duplicating the first one */
0N/A if (v_expand > 1) {
0N/A jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
0N/A v_expand-1, cinfo->output_width);
0N/A }
0N/A inrow++;
0N/A outrow += v_expand;
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
0N/A * It's still a box filter.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Ah2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
0N/A JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
0N/A{
0N/A JSAMPARRAY output_data = *output_data_ptr;
0N/A register JSAMPROW inptr, outptr;
0N/A register JSAMPLE invalue;
0N/A JSAMPROW outend;
0N/A int inrow;
0N/A
0N/A for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
0N/A inptr = input_data[inrow];
0N/A outptr = output_data[inrow];
0N/A outend = outptr + cinfo->output_width;
0N/A while (outptr < outend) {
0N/A invalue = *inptr++; /* don't need GETJSAMPLE() here */
0N/A *outptr++ = invalue;
0N/A *outptr++ = invalue;
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
0N/A * It's still a box filter.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Ah2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
0N/A JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
0N/A{
0N/A JSAMPARRAY output_data = *output_data_ptr;
0N/A register JSAMPROW inptr, outptr;
0N/A register JSAMPLE invalue;
0N/A JSAMPROW outend;
0N/A int inrow, outrow;
0N/A
0N/A inrow = outrow = 0;
0N/A while (outrow < cinfo->max_v_samp_factor) {
0N/A inptr = input_data[inrow];
0N/A outptr = output_data[outrow];
0N/A outend = outptr + cinfo->output_width;
0N/A while (outptr < outend) {
0N/A invalue = *inptr++; /* don't need GETJSAMPLE() here */
0N/A *outptr++ = invalue;
0N/A *outptr++ = invalue;
0N/A }
0N/A jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
0N/A 1, cinfo->output_width);
0N/A inrow++;
0N/A outrow += 2;
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
0N/A *
0N/A * The upsampling algorithm is linear interpolation between pixel centers,
0N/A * also known as a "triangle filter". This is a good compromise between
0N/A * speed and visual quality. The centers of the output pixels are 1/4 and 3/4
0N/A * of the way between input pixel centers.
0N/A *
0N/A * A note about the "bias" calculations: when rounding fractional values to
0N/A * integer, we do not want to always round 0.5 up to the next integer.
0N/A * If we did that, we'd introduce a noticeable bias towards larger values.
0N/A * Instead, this code is arranged so that 0.5 will be rounded up or down at
0N/A * alternate pixel locations (a simple ordered dither pattern).
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Ah2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
0N/A JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
0N/A{
0N/A JSAMPARRAY output_data = *output_data_ptr;
0N/A register JSAMPROW inptr, outptr;
0N/A register int invalue;
0N/A register JDIMENSION colctr;
0N/A int inrow;
0N/A
0N/A for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
0N/A inptr = input_data[inrow];
0N/A outptr = output_data[inrow];
0N/A /* Special case for first column */
0N/A invalue = GETJSAMPLE(*inptr++);
0N/A *outptr++ = (JSAMPLE) invalue;
0N/A *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
0N/A
0N/A for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
0N/A /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
0N/A invalue = GETJSAMPLE(*inptr++) * 3;
0N/A *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
0N/A *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
0N/A }
0N/A
0N/A /* Special case for last column */
0N/A invalue = GETJSAMPLE(*inptr);
0N/A *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
0N/A *outptr++ = (JSAMPLE) invalue;
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
0N/A * Again a triangle filter; see comments for h2v1 case, above.
0N/A *
0N/A * It is OK for us to reference the adjacent input rows because we demanded
0N/A * context from the main buffer controller (see initialization code).
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Ah2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
0N/A JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
0N/A{
0N/A JSAMPARRAY output_data = *output_data_ptr;
0N/A register JSAMPROW inptr0, inptr1, outptr;
0N/A#if BITS_IN_JSAMPLE == 8
0N/A register int thiscolsum, lastcolsum, nextcolsum;
0N/A#else
0N/A register INT32 thiscolsum, lastcolsum, nextcolsum;
0N/A#endif
0N/A register JDIMENSION colctr;
0N/A int inrow, outrow, v;
0N/A
0N/A inrow = outrow = 0;
0N/A while (outrow < cinfo->max_v_samp_factor) {
0N/A for (v = 0; v < 2; v++) {
0N/A /* inptr0 points to nearest input row, inptr1 points to next nearest */
0N/A inptr0 = input_data[inrow];
0N/A if (v == 0) /* next nearest is row above */
0N/A inptr1 = input_data[inrow-1];
0N/A else /* next nearest is row below */
0N/A inptr1 = input_data[inrow+1];
0N/A outptr = output_data[outrow++];
0N/A
0N/A /* Special case for first column */
0N/A thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
0N/A nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
0N/A *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
0N/A *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
0N/A lastcolsum = thiscolsum; thiscolsum = nextcolsum;
0N/A
0N/A for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
0N/A /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
0N/A /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
0N/A nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
0N/A *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
0N/A *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
0N/A lastcolsum = thiscolsum; thiscolsum = nextcolsum;
0N/A }
0N/A
0N/A /* Special case for last column */
0N/A *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
0N/A *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
0N/A }
0N/A inrow++;
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Module initialization routine for upsampling.
0N/A */
0N/A
0N/AGLOBAL(void)
0N/Ajinit_upsampler (j_decompress_ptr cinfo)
0N/A{
0N/A my_upsample_ptr upsample;
0N/A int ci;
0N/A jpeg_component_info * compptr;
0N/A boolean need_buffer, do_fancy;
0N/A int h_in_group, v_in_group, h_out_group, v_out_group;
0N/A
0N/A upsample = (my_upsample_ptr)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A SIZEOF(my_upsampler));
0N/A cinfo->upsample = (struct jpeg_upsampler *) upsample;
0N/A upsample->pub.start_pass = start_pass_upsample;
0N/A upsample->pub.upsample = sep_upsample;
0N/A upsample->pub.need_context_rows = FALSE; /* until we find out differently */
0N/A
0N/A if (cinfo->CCIR601_sampling) /* this isn't supported */
0N/A ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
0N/A
0N/A /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
0N/A * so don't ask for it.
0N/A */
0N/A do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1;
0N/A
0N/A /* Verify we can handle the sampling factors, select per-component methods,
0N/A * and create storage as needed.
0N/A */
0N/A for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
0N/A ci++, compptr++) {
0N/A /* Compute size of an "input group" after IDCT scaling. This many samples
0N/A * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
0N/A */
0N/A h_in_group = (compptr->h_samp_factor * compptr->DCT_scaled_size) /
0N/A cinfo->min_DCT_scaled_size;
0N/A v_in_group = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
0N/A cinfo->min_DCT_scaled_size;
0N/A h_out_group = cinfo->max_h_samp_factor;
0N/A v_out_group = cinfo->max_v_samp_factor;
0N/A upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
0N/A need_buffer = TRUE;
0N/A if (! compptr->component_needed) {
0N/A /* Don't bother to upsample an uninteresting component. */
0N/A upsample->methods[ci] = noop_upsample;
0N/A need_buffer = FALSE;
0N/A } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
0N/A /* Fullsize components can be processed without any work. */
0N/A upsample->methods[ci] = fullsize_upsample;
0N/A need_buffer = FALSE;
0N/A } else if (h_in_group * 2 == h_out_group &&
0N/A v_in_group == v_out_group) {
0N/A /* Special cases for 2h1v upsampling */
0N/A if (do_fancy && compptr->downsampled_width > 2)
0N/A upsample->methods[ci] = h2v1_fancy_upsample;
0N/A else
0N/A upsample->methods[ci] = h2v1_upsample;
0N/A } else if (h_in_group * 2 == h_out_group &&
0N/A v_in_group * 2 == v_out_group) {
0N/A /* Special cases for 2h2v upsampling */
0N/A if (do_fancy && compptr->downsampled_width > 2) {
0N/A upsample->methods[ci] = h2v2_fancy_upsample;
0N/A upsample->pub.need_context_rows = TRUE;
0N/A } else
0N/A upsample->methods[ci] = h2v2_upsample;
0N/A } else if ((h_out_group % h_in_group) == 0 &&
0N/A (v_out_group % v_in_group) == 0) {
0N/A /* Generic integral-factors upsampling method */
0N/A upsample->methods[ci] = int_upsample;
0N/A upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
0N/A upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
0N/A } else
0N/A ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
0N/A if (need_buffer) {
0N/A upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
0N/A ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A (JDIMENSION) jround_up((long) cinfo->output_width,
0N/A (long) cinfo->max_h_samp_factor),
0N/A (JDIMENSION) cinfo->max_v_samp_factor);
0N/A }
0N/A }
0N/A}