kusano 7d535a
/*
kusano 7d535a
 * jcprepct.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1994-1996, Thomas G. Lane.
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 the compression preprocessing controller.
kusano 7d535a
 * This controller manages the color conversion, downsampling,
kusano 7d535a
 * and edge expansion steps.
kusano 7d535a
 *
kusano 7d535a
 * Most of the complexity here is associated with buffering input rows
kusano 7d535a
 * as required by the downsampler.  See the comments at the head of
kusano 7d535a
 * jcsample.c for the downsampler's needs.
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
/* At present, jcsample.c can request context rows only for smoothing.
kusano 7d535a
 * In the future, we might also need context rows for CCIR601 sampling
kusano 7d535a
 * or other more-complex downsampling procedures.  The code to support
kusano 7d535a
 * context rows should be compiled only if needed.
kusano 7d535a
 */
kusano 7d535a
#ifdef INPUT_SMOOTHING_SUPPORTED
kusano 7d535a
#define CONTEXT_ROWS_SUPPORTED
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * For the simple (no-context-row) case, we just need to buffer one
kusano 7d535a
 * row group's worth of pixels for the downsampling step.  At the bottom of
kusano 7d535a
 * the image, we pad to a full row group by replicating the last pixel row.
kusano 7d535a
 * The downsampler's last output row is then replicated if needed to pad
kusano 7d535a
 * out to a full iMCU row.
kusano 7d535a
 *
kusano 7d535a
 * When providing context rows, we must buffer three row groups' worth of
kusano 7d535a
 * pixels.  Three row groups are physically allocated, but the row pointer
kusano 7d535a
 * arrays are made five row groups high, with the extra pointers above and
kusano 7d535a
 * below "wrapping around" to point to the last and first real row groups.
kusano 7d535a
 * This allows the downsampler to access the proper context rows.
kusano 7d535a
 * At the top and bottom of the image, we create dummy context rows by
kusano 7d535a
 * copying the first or last real pixel row.  This copying could be avoided
kusano 7d535a
 * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the
kusano 7d535a
 * trouble on the compression side.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Private buffer controller object */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  struct jpeg_c_prep_controller pub; /* public fields */
kusano 7d535a
kusano 7d535a
  /* Downsampling input buffer.  This buffer holds color-converted data
kusano 7d535a
   * until we have enough to do a downsample step.
kusano 7d535a
   */
kusano 7d535a
  JSAMPARRAY color_buf[MAX_COMPONENTS];
kusano 7d535a
kusano 7d535a
  JDIMENSION rows_to_go;	/* counts rows remaining in source image */
kusano 7d535a
  int next_buf_row;		/* index of next row to store in color_buf */
kusano 7d535a
kusano 7d535a
#ifdef CONTEXT_ROWS_SUPPORTED	/* only needed for context case */
kusano 7d535a
  int this_row_group;		/* starting row index of group to process */
kusano 7d535a
  int next_buf_stop;		/* downsample when we reach this index */
kusano 7d535a
#endif
kusano 7d535a
} my_prep_controller;
kusano 7d535a
kusano 7d535a
typedef my_prep_controller * my_prep_ptr;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize for a processing pass.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
kusano 7d535a
{
kusano 7d535a
  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
kusano 7d535a
kusano 7d535a
  if (pass_mode != JBUF_PASS_THRU)
kusano 7d535a
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
kusano 7d535a
kusano 7d535a
  /* Initialize total-height counter for detecting bottom of image */
kusano 7d535a
  prep->rows_to_go = cinfo->image_height;
kusano 7d535a
  /* Mark the conversion buffer empty */
kusano 7d535a
  prep->next_buf_row = 0;
kusano 7d535a
#ifdef CONTEXT_ROWS_SUPPORTED
kusano 7d535a
  /* Preset additional state variables for context mode.
kusano 7d535a
   * These aren't used in non-context mode, so we needn't test which mode.
kusano 7d535a
   */
kusano 7d535a
  prep->this_row_group = 0;
kusano 7d535a
  /* Set next_buf_stop to stop after two row groups have been read in. */
kusano 7d535a
  prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
kusano 7d535a
#endif
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Expand an image vertically from height input_rows to height output_rows,
kusano 7d535a
 * by duplicating the bottom row.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
kusano 7d535a
		    int input_rows, int output_rows)
kusano 7d535a
{
kusano 7d535a
  register int row;
kusano 7d535a
kusano 7d535a
  for (row = input_rows; row < output_rows; row++) {
kusano 7d535a
    jcopy_sample_rows(image_data, input_rows-1, image_data, row,
kusano 7d535a
		      1, num_cols);
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Process some data in the simple no-context case.
kusano 7d535a
 *
kusano 7d535a
 * Preprocessor output data is counted in "row groups".  A row group
kusano 7d535a
 * is defined to be v_samp_factor sample rows of each component.
kusano 7d535a
 * Downsampling will produce this much data from each max_v_samp_factor
kusano 7d535a
 * input rows.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
pre_process_data (j_compress_ptr cinfo,
kusano 7d535a
		  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
kusano 7d535a
		  JDIMENSION in_rows_avail,
kusano 7d535a
		  JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
kusano 7d535a
		  JDIMENSION out_row_groups_avail)
kusano 7d535a
{
kusano 7d535a
  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
kusano 7d535a
  int numrows, ci;
kusano 7d535a
  JDIMENSION inrows;
kusano 7d535a
  jpeg_component_info * compptr;
kusano 7d535a
kusano 7d535a
  while (*in_row_ctr < in_rows_avail &&
kusano 7d535a
	 *out_row_group_ctr < out_row_groups_avail) {
kusano 7d535a
    /* Do color conversion to fill the conversion buffer. */
kusano 7d535a
    inrows = in_rows_avail - *in_row_ctr;
kusano 7d535a
    numrows = cinfo->max_v_samp_factor - prep->next_buf_row;
kusano 7d535a
    numrows = (int) MIN((JDIMENSION) numrows, inrows);
kusano 7d535a
    (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
kusano 7d535a
				       prep->color_buf,
kusano 7d535a
				       (JDIMENSION) prep->next_buf_row,
kusano 7d535a
				       numrows);
kusano 7d535a
    *in_row_ctr += numrows;
kusano 7d535a
    prep->next_buf_row += numrows;
kusano 7d535a
    prep->rows_to_go -= numrows;
kusano 7d535a
    /* If at bottom of image, pad to fill the conversion buffer. */
kusano 7d535a
    if (prep->rows_to_go == 0 &&
kusano 7d535a
	prep->next_buf_row < cinfo->max_v_samp_factor) {
kusano 7d535a
      for (ci = 0; ci < cinfo->num_components; ci++) {
kusano 7d535a
	expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
kusano 7d535a
			   prep->next_buf_row, cinfo->max_v_samp_factor);
kusano 7d535a
      }
kusano 7d535a
      prep->next_buf_row = cinfo->max_v_samp_factor;
kusano 7d535a
    }
kusano 7d535a
    /* If we've filled the conversion buffer, empty it. */
kusano 7d535a
    if (prep->next_buf_row == cinfo->max_v_samp_factor) {
kusano 7d535a
      (*cinfo->downsample->downsample) (cinfo,
kusano 7d535a
					prep->color_buf, (JDIMENSION) 0,
kusano 7d535a
					output_buf, *out_row_group_ctr);
kusano 7d535a
      prep->next_buf_row = 0;
kusano 7d535a
      (*out_row_group_ctr)++;
kusano 7d535a
    }
kusano 7d535a
    /* If at bottom of image, pad the output to a full iMCU height.
kusano 7d535a
     * Note we assume the caller is providing a one-iMCU-height output buffer!
kusano 7d535a
     */
kusano 7d535a
    if (prep->rows_to_go == 0 &&
kusano 7d535a
	*out_row_group_ctr < out_row_groups_avail) {
kusano 7d535a
      for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
	   ci++, compptr++) {
kusano 7d535a
	numrows = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
kusano 7d535a
		  cinfo->min_DCT_v_scaled_size;
kusano 7d535a
	expand_bottom_edge(output_buf[ci],
kusano 7d535a
			   compptr->width_in_blocks * compptr->DCT_h_scaled_size,
kusano 7d535a
			   (int) (*out_row_group_ctr * numrows),
kusano 7d535a
			   (int) (out_row_groups_avail * numrows));
kusano 7d535a
      }
kusano 7d535a
      *out_row_group_ctr = out_row_groups_avail;
kusano 7d535a
      break;			/* can exit outer loop without test */
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
#ifdef CONTEXT_ROWS_SUPPORTED
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Process some data in the context case.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
pre_process_context (j_compress_ptr cinfo,
kusano 7d535a
		     JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
kusano 7d535a
		     JDIMENSION in_rows_avail,
kusano 7d535a
		     JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
kusano 7d535a
		     JDIMENSION out_row_groups_avail)
kusano 7d535a
{
kusano 7d535a
  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
kusano 7d535a
  int numrows, ci;
kusano 7d535a
  int buf_height = cinfo->max_v_samp_factor * 3;
kusano 7d535a
  JDIMENSION inrows;
kusano 7d535a
kusano 7d535a
  while (*out_row_group_ctr < out_row_groups_avail) {
kusano 7d535a
    if (*in_row_ctr < in_rows_avail) {
kusano 7d535a
      /* Do color conversion to fill the conversion buffer. */
kusano 7d535a
      inrows = in_rows_avail - *in_row_ctr;
kusano 7d535a
      numrows = prep->next_buf_stop - prep->next_buf_row;
kusano 7d535a
      numrows = (int) MIN((JDIMENSION) numrows, inrows);
kusano 7d535a
      (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
kusano 7d535a
					 prep->color_buf,
kusano 7d535a
					 (JDIMENSION) prep->next_buf_row,
kusano 7d535a
					 numrows);
kusano 7d535a
      /* Pad at top of image, if first time through */
kusano 7d535a
      if (prep->rows_to_go == cinfo->image_height) {
kusano 7d535a
	for (ci = 0; ci < cinfo->num_components; ci++) {
kusano 7d535a
	  int row;
kusano 7d535a
	  for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
kusano 7d535a
	    jcopy_sample_rows(prep->color_buf[ci], 0,
kusano 7d535a
			      prep->color_buf[ci], -row,
kusano 7d535a
			      1, cinfo->image_width);
kusano 7d535a
	  }
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
      *in_row_ctr += numrows;
kusano 7d535a
      prep->next_buf_row += numrows;
kusano 7d535a
      prep->rows_to_go -= numrows;
kusano 7d535a
    } else {
kusano 7d535a
      /* Return for more data, unless we are at the bottom of the image. */
kusano 7d535a
      if (prep->rows_to_go != 0)
kusano 7d535a
	break;
kusano 7d535a
      /* When at bottom of image, pad to fill the conversion buffer. */
kusano 7d535a
      if (prep->next_buf_row < prep->next_buf_stop) {
kusano 7d535a
	for (ci = 0; ci < cinfo->num_components; ci++) {
kusano 7d535a
	  expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
kusano 7d535a
			     prep->next_buf_row, prep->next_buf_stop);
kusano 7d535a
	}
kusano 7d535a
	prep->next_buf_row = prep->next_buf_stop;
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
    /* If we've gotten enough data, downsample a row group. */
kusano 7d535a
    if (prep->next_buf_row == prep->next_buf_stop) {
kusano 7d535a
      (*cinfo->downsample->downsample) (cinfo,
kusano 7d535a
					prep->color_buf,
kusano 7d535a
					(JDIMENSION) prep->this_row_group,
kusano 7d535a
					output_buf, *out_row_group_ctr);
kusano 7d535a
      (*out_row_group_ctr)++;
kusano 7d535a
      /* Advance pointers with wraparound as necessary. */
kusano 7d535a
      prep->this_row_group += cinfo->max_v_samp_factor;
kusano 7d535a
      if (prep->this_row_group >= buf_height)
kusano 7d535a
	prep->this_row_group = 0;
kusano 7d535a
      if (prep->next_buf_row >= buf_height)
kusano 7d535a
	prep->next_buf_row = 0;
kusano 7d535a
      prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Create the wrapped-around downsampling input buffer needed for context mode.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
create_context_buffer (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
kusano 7d535a
  int rgroup_height = cinfo->max_v_samp_factor;
kusano 7d535a
  int ci, i;
kusano 7d535a
  jpeg_component_info * compptr;
kusano 7d535a
  JSAMPARRAY true_buffer, fake_buffer;
kusano 7d535a
kusano 7d535a
  /* Grab enough space for fake row pointers for all the components;
kusano 7d535a
   * we need five row groups' worth of pointers for each component.
kusano 7d535a
   */
kusano 7d535a
  fake_buffer = (JSAMPARRAY)
kusano 7d535a
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				(cinfo->num_components * 5 * rgroup_height) *
kusano 7d535a
				SIZEOF(JSAMPROW));
kusano 7d535a
kusano 7d535a
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
       ci++, compptr++) {
kusano 7d535a
    /* Allocate the actual buffer space (3 row groups) for this component.
kusano 7d535a
     * We make the buffer wide enough to allow the downsampler to edge-expand
kusano 7d535a
     * horizontally within the buffer, if it so chooses.
kusano 7d535a
     */
kusano 7d535a
    true_buffer = (*cinfo->mem->alloc_sarray)
kusano 7d535a
      ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
       (JDIMENSION) (((long) compptr->width_in_blocks *
kusano 7d535a
		      cinfo->min_DCT_h_scaled_size *
kusano 7d535a
		      cinfo->max_h_samp_factor) / compptr->h_samp_factor),
kusano 7d535a
       (JDIMENSION) (3 * rgroup_height));
kusano 7d535a
    /* Copy true buffer row pointers into the middle of the fake row array */
kusano 7d535a
    MEMCOPY(fake_buffer + rgroup_height, true_buffer,
kusano 7d535a
	    3 * rgroup_height * SIZEOF(JSAMPROW));
kusano 7d535a
    /* Fill in the above and below wraparound pointers */
kusano 7d535a
    for (i = 0; i < rgroup_height; i++) {
kusano 7d535a
      fake_buffer[i] = true_buffer[2 * rgroup_height + i];
kusano 7d535a
      fake_buffer[4 * rgroup_height + i] = true_buffer[i];
kusano 7d535a
    }
kusano 7d535a
    prep->color_buf[ci] = fake_buffer + rgroup_height;
kusano 7d535a
    fake_buffer += 5 * rgroup_height; /* point to space for next component */
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif /* CONTEXT_ROWS_SUPPORTED */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize preprocessing controller.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
kusano 7d535a
{
kusano 7d535a
  my_prep_ptr prep;
kusano 7d535a
  int ci;
kusano 7d535a
  jpeg_component_info * compptr;
kusano 7d535a
kusano 7d535a
  if (need_full_buffer)		/* safety check */
kusano 7d535a
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
kusano 7d535a
kusano 7d535a
  prep = (my_prep_ptr)
kusano 7d535a
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				SIZEOF(my_prep_controller));
kusano 7d535a
  cinfo->prep = (struct jpeg_c_prep_controller *) prep;
kusano 7d535a
  prep->pub.start_pass = start_pass_prep;
kusano 7d535a
kusano 7d535a
  /* Allocate the color conversion buffer.
kusano 7d535a
   * We make the buffer wide enough to allow the downsampler to edge-expand
kusano 7d535a
   * horizontally within the buffer, if it so chooses.
kusano 7d535a
   */
kusano 7d535a
  if (cinfo->downsample->need_context_rows) {
kusano 7d535a
    /* Set up to provide context rows */
kusano 7d535a
#ifdef CONTEXT_ROWS_SUPPORTED
kusano 7d535a
    prep->pub.pre_process_data = pre_process_context;
kusano 7d535a
    create_context_buffer(cinfo);
kusano 7d535a
#else
kusano 7d535a
    ERREXIT(cinfo, JERR_NOT_COMPILED);
kusano 7d535a
#endif
kusano 7d535a
  } else {
kusano 7d535a
    /* No context, just make it tall enough for one row group */
kusano 7d535a
    prep->pub.pre_process_data = pre_process_data;
kusano 7d535a
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
	 ci++, compptr++) {
kusano 7d535a
      prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
kusano 7d535a
	((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
	 (JDIMENSION) (((long) compptr->width_in_blocks *
kusano 7d535a
			cinfo->min_DCT_h_scaled_size *
kusano 7d535a
			cinfo->max_h_samp_factor) / compptr->h_samp_factor),
kusano 7d535a
	 (JDIMENSION) cinfo->max_v_samp_factor);
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}