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