kusano 7d535a
/*
kusano 7d535a
 * jdsample.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1991-1996, Thomas G. Lane.
kusano 7d535a
 * Modified 2002-2008 by Guido Vollbeding.
kusano 7d535a
 * This file is part of the Independent JPEG Group's software.
kusano 7d535a
 * For conditions of distribution and use, see the accompanying README file.
kusano 7d535a
 *
kusano 7d535a
 * This file contains upsampling routines.
kusano 7d535a
 *
kusano 7d535a
 * Upsampling input data is counted in "row groups".  A row group
kusano 7d535a
 * is defined to be (v_samp_factor * DCT_v_scaled_size / min_DCT_v_scaled_size)
kusano 7d535a
 * sample rows of each component.  Upsampling will normally produce
kusano 7d535a
 * max_v_samp_factor pixel rows from each row group (but this could vary
kusano 7d535a
 * if the upsampler is applying a scale factor of its own).
kusano 7d535a
 *
kusano 7d535a
 * An excellent reference for image resampling is
kusano 7d535a
 *   Digital Image Warping, George Wolberg, 1990.
kusano 7d535a
 *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define JPEG_INTERNALS
kusano 7d535a
#include "jinclude.h"
kusano 7d535a
#include "jpeglib.h"
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Pointer to routine to upsample a single component */
kusano 7d535a
typedef JMETHOD(void, upsample1_ptr,
kusano 7d535a
		(j_decompress_ptr cinfo, jpeg_component_info * compptr,
kusano 7d535a
		 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));
kusano 7d535a
kusano 7d535a
/* Private subobject */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  struct jpeg_upsampler pub;	/* public fields */
kusano 7d535a
kusano 7d535a
  /* Color conversion buffer.  When using separate upsampling and color
kusano 7d535a
   * conversion steps, this buffer holds one upsampled row group until it
kusano 7d535a
   * has been color converted and output.
kusano 7d535a
   * Note: we do not allocate any storage for component(s) which are full-size,
kusano 7d535a
   * ie do not need rescaling.  The corresponding entry of color_buf[] is
kusano 7d535a
   * simply set to point to the input data array, thereby avoiding copying.
kusano 7d535a
   */
kusano 7d535a
  JSAMPARRAY color_buf[MAX_COMPONENTS];
kusano 7d535a
kusano 7d535a
  /* Per-component upsampling method pointers */
kusano 7d535a
  upsample1_ptr methods[MAX_COMPONENTS];
kusano 7d535a
kusano 7d535a
  int next_row_out;		/* counts rows emitted from color_buf */
kusano 7d535a
  JDIMENSION rows_to_go;	/* counts rows remaining in image */
kusano 7d535a
kusano 7d535a
  /* Height of an input row group for each component. */
kusano 7d535a
  int rowgroup_height[MAX_COMPONENTS];
kusano 7d535a
kusano 7d535a
  /* These arrays save pixel expansion factors so that int_expand need not
kusano 7d535a
   * recompute them each time.  They are unused for other upsampling methods.
kusano 7d535a
   */
kusano 7d535a
  UINT8 h_expand[MAX_COMPONENTS];
kusano 7d535a
  UINT8 v_expand[MAX_COMPONENTS];
kusano 7d535a
} my_upsampler;
kusano 7d535a
kusano 7d535a
typedef my_upsampler * my_upsample_ptr;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize for an upsampling pass.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
start_pass_upsample (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
kusano 7d535a
kusano 7d535a
  /* Mark the conversion buffer empty */
kusano 7d535a
  upsample->next_row_out = cinfo->max_v_samp_factor;
kusano 7d535a
  /* Initialize total-height counter for detecting bottom of image */
kusano 7d535a
  upsample->rows_to_go = cinfo->output_height;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Control routine to do upsampling (and color conversion).
kusano 7d535a
 *
kusano 7d535a
 * In this version we upsample each component independently.
kusano 7d535a
 * We upsample one row group into the conversion buffer, then apply
kusano 7d535a
 * color conversion a row at a time.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
sep_upsample (j_decompress_ptr cinfo,
kusano 7d535a
	      JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
kusano 7d535a
	      JDIMENSION in_row_groups_avail,
kusano 7d535a
	      JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
kusano 7d535a
	      JDIMENSION out_rows_avail)
kusano 7d535a
{
kusano 7d535a
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
kusano 7d535a
  int ci;
kusano 7d535a
  jpeg_component_info * compptr;
kusano 7d535a
  JDIMENSION num_rows;
kusano 7d535a
kusano 7d535a
  /* Fill the conversion buffer, if it's empty */
kusano 7d535a
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
kusano 7d535a
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
	 ci++, compptr++) {
kusano 7d535a
      /* Invoke per-component upsample method.  Notice we pass a POINTER
kusano 7d535a
       * to color_buf[ci], so that fullsize_upsample can change it.
kusano 7d535a
       */
kusano 7d535a
      (*upsample->methods[ci]) (cinfo, compptr,
kusano 7d535a
	input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
kusano 7d535a
	upsample->color_buf + ci);
kusano 7d535a
    }
kusano 7d535a
    upsample->next_row_out = 0;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Color-convert and emit rows */
kusano 7d535a
kusano 7d535a
  /* How many we have in the buffer: */
kusano 7d535a
  num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
kusano 7d535a
  /* Not more than the distance to the end of the image.  Need this test
kusano 7d535a
   * in case the image height is not a multiple of max_v_samp_factor:
kusano 7d535a
   */
kusano 7d535a
  if (num_rows > upsample->rows_to_go) 
kusano 7d535a
    num_rows = upsample->rows_to_go;
kusano 7d535a
  /* And not more than what the client can accept: */
kusano 7d535a
  out_rows_avail -= *out_row_ctr;
kusano 7d535a
  if (num_rows > out_rows_avail)
kusano 7d535a
    num_rows = out_rows_avail;
kusano 7d535a
kusano 7d535a
  (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
kusano 7d535a
				     (JDIMENSION) upsample->next_row_out,
kusano 7d535a
				     output_buf + *out_row_ctr,
kusano 7d535a
				     (int) num_rows);
kusano 7d535a
kusano 7d535a
  /* Adjust counts */
kusano 7d535a
  *out_row_ctr += num_rows;
kusano 7d535a
  upsample->rows_to_go -= num_rows;
kusano 7d535a
  upsample->next_row_out += num_rows;
kusano 7d535a
  /* When the buffer is emptied, declare this input row group consumed */
kusano 7d535a
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
kusano 7d535a
    (*in_row_group_ctr)++;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * These are the routines invoked by sep_upsample to upsample pixel values
kusano 7d535a
 * of a single component.  One row group is processed per call.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * For full-size components, we just make color_buf[ci] point at the
kusano 7d535a
 * input buffer, and thus avoid copying any data.  Note that this is
kusano 7d535a
 * safe only because sep_upsample doesn't declare the input row group
kusano 7d535a
 * "consumed" until we are done color converting and emitting it.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
kusano 7d535a
		   JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
kusano 7d535a
{
kusano 7d535a
  *output_data_ptr = input_data;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * This is a no-op version used for "uninteresting" components.
kusano 7d535a
 * These components will not be referenced by color conversion.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
kusano 7d535a
	       JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
kusano 7d535a
{
kusano 7d535a
  *output_data_ptr = NULL;	/* safety check */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * This version handles any integral sampling ratios.
kusano 7d535a
 * This is not used for typical JPEG files, so it need not be fast.
kusano 7d535a
 * Nor, for that matter, is it particularly accurate: the algorithm is
kusano 7d535a
 * simple replication of the input pixel onto the corresponding output
kusano 7d535a
 * pixels.  The hi-falutin sampling literature refers to this as a
kusano 7d535a
 * "box filter".  A box filter tends to introduce visible artifacts,
kusano 7d535a
 * so if you are actually going to use 3:1 or 4:1 sampling ratios
kusano 7d535a
 * you would be well advised to improve this code.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
kusano 7d535a
	      JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
kusano 7d535a
{
kusano 7d535a
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
kusano 7d535a
  JSAMPARRAY output_data = *output_data_ptr;
kusano 7d535a
  register JSAMPROW inptr, outptr;
kusano 7d535a
  register JSAMPLE invalue;
kusano 7d535a
  register int h;
kusano 7d535a
  JSAMPROW outend;
kusano 7d535a
  int h_expand, v_expand;
kusano 7d535a
  int inrow, outrow;
kusano 7d535a
kusano 7d535a
  h_expand = upsample->h_expand[compptr->component_index];
kusano 7d535a
  v_expand = upsample->v_expand[compptr->component_index];
kusano 7d535a
kusano 7d535a
  inrow = outrow = 0;
kusano 7d535a
  while (outrow < cinfo->max_v_samp_factor) {
kusano 7d535a
    /* Generate one output row with proper horizontal expansion */
kusano 7d535a
    inptr = input_data[inrow];
kusano 7d535a
    outptr = output_data[outrow];
kusano 7d535a
    outend = outptr + cinfo->output_width;
kusano 7d535a
    while (outptr < outend) {
kusano 7d535a
      invalue = *inptr++;	/* don't need GETJSAMPLE() here */
kusano 7d535a
      for (h = h_expand; h > 0; h--) {
kusano 7d535a
	*outptr++ = invalue;
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
    /* Generate any additional output rows by duplicating the first one */
kusano 7d535a
    if (v_expand > 1) {
kusano 7d535a
      jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
kusano 7d535a
			v_expand-1, cinfo->output_width);
kusano 7d535a
    }
kusano 7d535a
    inrow++;
kusano 7d535a
    outrow += v_expand;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
kusano 7d535a
 * It's still a box filter.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
kusano 7d535a
	       JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
kusano 7d535a
{
kusano 7d535a
  JSAMPARRAY output_data = *output_data_ptr;
kusano 7d535a
  register JSAMPROW inptr, outptr;
kusano 7d535a
  register JSAMPLE invalue;
kusano 7d535a
  JSAMPROW outend;
kusano 7d535a
  int outrow;
kusano 7d535a
kusano 7d535a
  for (outrow = 0; outrow < cinfo->max_v_samp_factor; outrow++) {
kusano 7d535a
    inptr = input_data[outrow];
kusano 7d535a
    outptr = output_data[outrow];
kusano 7d535a
    outend = outptr + cinfo->output_width;
kusano 7d535a
    while (outptr < outend) {
kusano 7d535a
      invalue = *inptr++;	/* don't need GETJSAMPLE() here */
kusano 7d535a
      *outptr++ = invalue;
kusano 7d535a
      *outptr++ = invalue;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
kusano 7d535a
 * It's still a box filter.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
kusano 7d535a
	       JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
kusano 7d535a
{
kusano 7d535a
  JSAMPARRAY output_data = *output_data_ptr;
kusano 7d535a
  register JSAMPROW inptr, outptr;
kusano 7d535a
  register JSAMPLE invalue;
kusano 7d535a
  JSAMPROW outend;
kusano 7d535a
  int inrow, outrow;
kusano 7d535a
kusano 7d535a
  inrow = outrow = 0;
kusano 7d535a
  while (outrow < cinfo->max_v_samp_factor) {
kusano 7d535a
    inptr = input_data[inrow];
kusano 7d535a
    outptr = output_data[outrow];
kusano 7d535a
    outend = outptr + cinfo->output_width;
kusano 7d535a
    while (outptr < outend) {
kusano 7d535a
      invalue = *inptr++;	/* don't need GETJSAMPLE() here */
kusano 7d535a
      *outptr++ = invalue;
kusano 7d535a
      *outptr++ = invalue;
kusano 7d535a
    }
kusano 7d535a
    jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
kusano 7d535a
		      1, cinfo->output_width);
kusano 7d535a
    inrow++;
kusano 7d535a
    outrow += 2;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Module initialization routine for upsampling.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jinit_upsampler (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_upsample_ptr upsample;
kusano 7d535a
  int ci;
kusano 7d535a
  jpeg_component_info * compptr;
kusano 7d535a
  boolean need_buffer;
kusano 7d535a
  int h_in_group, v_in_group, h_out_group, v_out_group;
kusano 7d535a
kusano 7d535a
  upsample = (my_upsample_ptr)
kusano 7d535a
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				SIZEOF(my_upsampler));
kusano 7d535a
  cinfo->upsample = (struct jpeg_upsampler *) upsample;
kusano 7d535a
  upsample->pub.start_pass = start_pass_upsample;
kusano 7d535a
  upsample->pub.upsample = sep_upsample;
kusano 7d535a
  upsample->pub.need_context_rows = FALSE; /* until we find out differently */
kusano 7d535a
kusano 7d535a
  if (cinfo->CCIR601_sampling)	/* this isn't supported */
kusano 7d535a
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
kusano 7d535a
kusano 7d535a
  /* Verify we can handle the sampling factors, select per-component methods,
kusano 7d535a
   * and create storage as needed.
kusano 7d535a
   */
kusano 7d535a
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
       ci++, compptr++) {
kusano 7d535a
    /* Compute size of an "input group" after IDCT scaling.  This many samples
kusano 7d535a
     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
kusano 7d535a
     */
kusano 7d535a
    h_in_group = (compptr->h_samp_factor * compptr->DCT_h_scaled_size) /
kusano 7d535a
		 cinfo->min_DCT_h_scaled_size;
kusano 7d535a
    v_in_group = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
kusano 7d535a
		 cinfo->min_DCT_v_scaled_size;
kusano 7d535a
    h_out_group = cinfo->max_h_samp_factor;
kusano 7d535a
    v_out_group = cinfo->max_v_samp_factor;
kusano 7d535a
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
kusano 7d535a
    need_buffer = TRUE;
kusano 7d535a
    if (! compptr->component_needed) {
kusano 7d535a
      /* Don't bother to upsample an uninteresting component. */
kusano 7d535a
      upsample->methods[ci] = noop_upsample;
kusano 7d535a
      need_buffer = FALSE;
kusano 7d535a
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
kusano 7d535a
      /* Fullsize components can be processed without any work. */
kusano 7d535a
      upsample->methods[ci] = fullsize_upsample;
kusano 7d535a
      need_buffer = FALSE;
kusano 7d535a
    } else if (h_in_group * 2 == h_out_group &&
kusano 7d535a
	       v_in_group == v_out_group) {
kusano 7d535a
      /* Special case for 2h1v upsampling */
kusano 7d535a
      upsample->methods[ci] = h2v1_upsample;
kusano 7d535a
    } else if (h_in_group * 2 == h_out_group &&
kusano 7d535a
	       v_in_group * 2 == v_out_group) {
kusano 7d535a
      /* Special case for 2h2v upsampling */
kusano 7d535a
      upsample->methods[ci] = h2v2_upsample;
kusano 7d535a
    } else if ((h_out_group % h_in_group) == 0 &&
kusano 7d535a
	       (v_out_group % v_in_group) == 0) {
kusano 7d535a
      /* Generic integral-factors upsampling method */
kusano 7d535a
      upsample->methods[ci] = int_upsample;
kusano 7d535a
      upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
kusano 7d535a
      upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
kusano 7d535a
    } else
kusano 7d535a
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
kusano 7d535a
    if (need_buffer) {
kusano 7d535a
      upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
kusano 7d535a
	((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
	 (JDIMENSION) jround_up((long) cinfo->output_width,
kusano 7d535a
				(long) cinfo->max_h_samp_factor),
kusano 7d535a
	 (JDIMENSION) cinfo->max_v_samp_factor);
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}