136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * jcprepct.c
336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane *
45de454b291f48382648a5d1dc2aa0fca8b5786d4DRC * This file is part of the Independent JPEG Group's software:
5489583f5165e05d37302e8eeec58104ea0109127Thomas G. Lane * Copyright (C) 1994-1996, Thomas G. Lane.
65de454b291f48382648a5d1dc2aa0fca8b5786d4DRC * It was modified by The libjpeg-turbo Project to include only code relevant
75de454b291f48382648a5d1dc2aa0fca8b5786d4DRC * to libjpeg-turbo.
836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * For conditions of distribution and use, see the accompanying README file.
936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane *
1036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * This file contains the compression preprocessing controller.
1136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * This controller manages the color conversion, downsampling,
1236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * and edge expansion steps.
1336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane *
1436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Most of the complexity here is associated with buffering input rows
1536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * as required by the downsampler.  See the comments at the head of
1636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * jcsample.c for the downsampler's needs.
1736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
1836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
1936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#define JPEG_INTERNALS
2036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#include "jinclude.h"
2136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#include "jpeglib.h"
2236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
2336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
2436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* At present, jcsample.c can request context rows only for smoothing.
2536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * In the future, we might also need context rows for CCIR601 sampling
2636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * or other more-complex downsampling procedures.  The code to support
2736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * context rows should be compiled only if needed.
2836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
2936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#ifdef INPUT_SMOOTHING_SUPPORTED
3036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#define CONTEXT_ROWS_SUPPORTED
3136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#endif
3236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
3336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
3436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
3536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * For the simple (no-context-row) case, we just need to buffer one
3636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * row group's worth of pixels for the downsampling step.  At the bottom of
3736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * the image, we pad to a full row group by replicating the last pixel row.
3836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * The downsampler's last output row is then replicated if needed to pad
3936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * out to a full iMCU row.
4036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane *
4136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * When providing context rows, we must buffer three row groups' worth of
4236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * pixels.  Three row groups are physically allocated, but the row pointer
4336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * arrays are made five row groups high, with the extra pointers above and
4436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * below "wrapping around" to point to the last and first real row groups.
4536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * This allows the downsampler to access the proper context rows.
4636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * At the top and bottom of the image, we create dummy context rows by
4736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * copying the first or last real pixel row.  This copying could be avoided
4836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the
4936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * trouble on the compression side.
5036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
5136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
5236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
5336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* Private buffer controller object */
5436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
5536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanetypedef struct {
5636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  struct jpeg_c_prep_controller pub; /* public fields */
5736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
5836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Downsampling input buffer.  This buffer holds color-converted data
5936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   * until we have enough to do a downsample step.
6036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   */
6136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  JSAMPARRAY color_buf[MAX_COMPONENTS];
6236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
63e5eaf37440b8e337ab150c017df7c03faf846c51DRC  JDIMENSION rows_to_go;        /* counts rows remaining in source image */
64e5eaf37440b8e337ab150c017df7c03faf846c51DRC  int next_buf_row;             /* index of next row to store in color_buf */
6536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
66e5eaf37440b8e337ab150c017df7c03faf846c51DRC#ifdef CONTEXT_ROWS_SUPPORTED   /* only needed for context case */
67e5eaf37440b8e337ab150c017df7c03faf846c51DRC  int this_row_group;           /* starting row index of group to process */
68e5eaf37440b8e337ab150c017df7c03faf846c51DRC  int next_buf_stop;            /* downsample when we reach this index */
6936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#endif
7036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane} my_prep_controller;
7136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
7236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanetypedef my_prep_controller * my_prep_ptr;
7336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
7436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
7536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
7636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Initialize for a processing pass.
7736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
7836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
79489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void)
8036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanestart_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
8136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
8236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
8336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
8436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (pass_mode != JBUF_PASS_THRU)
8536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
8636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
8736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Initialize total-height counter for detecting bottom of image */
8836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  prep->rows_to_go = cinfo->image_height;
8936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Mark the conversion buffer empty */
9036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  prep->next_buf_row = 0;
9136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#ifdef CONTEXT_ROWS_SUPPORTED
9236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Preset additional state variables for context mode.
9336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   * These aren't used in non-context mode, so we needn't test which mode.
9436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   */
9536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  prep->this_row_group = 0;
9636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Set next_buf_stop to stop after two row groups have been read in. */
9736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
9836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#endif
9936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
10036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
10136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
10236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
10336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Expand an image vertically from height input_rows to height output_rows,
10436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * by duplicating the bottom row.
10536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
10636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
107489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(void)
10836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Laneexpand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
109e5eaf37440b8e337ab150c017df7c03faf846c51DRC                    int input_rows, int output_rows)
11036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
11136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  register int row;
11236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
11336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  for (row = input_rows; row < output_rows; row++) {
11436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    jcopy_sample_rows(image_data, input_rows-1, image_data, row,
115e5eaf37440b8e337ab150c017df7c03faf846c51DRC                      1, num_cols);
11636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
11736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
11836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
11936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
12036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
12136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Process some data in the simple no-context case.
12236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane *
12336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Preprocessor output data is counted in "row groups".  A row group
12436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * is defined to be v_samp_factor sample rows of each component.
12536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Downsampling will produce this much data from each max_v_samp_factor
12636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * input rows.
12736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
12836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
129489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void)
13036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanepre_process_data (j_compress_ptr cinfo,
131e5eaf37440b8e337ab150c017df7c03faf846c51DRC                  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
132e5eaf37440b8e337ab150c017df7c03faf846c51DRC                  JDIMENSION in_rows_avail,
133e5eaf37440b8e337ab150c017df7c03faf846c51DRC                  JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
134e5eaf37440b8e337ab150c017df7c03faf846c51DRC                  JDIMENSION out_row_groups_avail)
13536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
13636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
13736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  int numrows, ci;
13836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  JDIMENSION inrows;
13936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  jpeg_component_info * compptr;
14036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
14136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  while (*in_row_ctr < in_rows_avail &&
142e5eaf37440b8e337ab150c017df7c03faf846c51DRC         *out_row_group_ctr < out_row_groups_avail) {
14336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* Do color conversion to fill the conversion buffer. */
14436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    inrows = in_rows_avail - *in_row_ctr;
14536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    numrows = cinfo->max_v_samp_factor - prep->next_buf_row;
14636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    numrows = (int) MIN((JDIMENSION) numrows, inrows);
14736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
148e5eaf37440b8e337ab150c017df7c03faf846c51DRC                                       prep->color_buf,
149e5eaf37440b8e337ab150c017df7c03faf846c51DRC                                       (JDIMENSION) prep->next_buf_row,
150e5eaf37440b8e337ab150c017df7c03faf846c51DRC                                       numrows);
15136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    *in_row_ctr += numrows;
15236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    prep->next_buf_row += numrows;
15336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    prep->rows_to_go -= numrows;
15436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* If at bottom of image, pad to fill the conversion buffer. */
15536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    if (prep->rows_to_go == 0 &&
156e5eaf37440b8e337ab150c017df7c03faf846c51DRC        prep->next_buf_row < cinfo->max_v_samp_factor) {
15736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      for (ci = 0; ci < cinfo->num_components; ci++) {
158e5eaf37440b8e337ab150c017df7c03faf846c51DRC        expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
159e5eaf37440b8e337ab150c017df7c03faf846c51DRC                           prep->next_buf_row, cinfo->max_v_samp_factor);
16036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      }
16136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      prep->next_buf_row = cinfo->max_v_samp_factor;
16236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    }
16336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* If we've filled the conversion buffer, empty it. */
16436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    if (prep->next_buf_row == cinfo->max_v_samp_factor) {
16536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      (*cinfo->downsample->downsample) (cinfo,
166e5eaf37440b8e337ab150c017df7c03faf846c51DRC                                        prep->color_buf, (JDIMENSION) 0,
167e5eaf37440b8e337ab150c017df7c03faf846c51DRC                                        output_buf, *out_row_group_ctr);
16836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      prep->next_buf_row = 0;
16936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      (*out_row_group_ctr)++;
17036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    }
17136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* If at bottom of image, pad the output to a full iMCU height.
17236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane     * Note we assume the caller is providing a one-iMCU-height output buffer!
17336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane     */
17436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    if (prep->rows_to_go == 0 &&
175e5eaf37440b8e337ab150c017df7c03faf846c51DRC        *out_row_group_ctr < out_row_groups_avail) {
17636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
177e5eaf37440b8e337ab150c017df7c03faf846c51DRC           ci++, compptr++) {
178e5eaf37440b8e337ab150c017df7c03faf846c51DRC        expand_bottom_edge(output_buf[ci],
179e5eaf37440b8e337ab150c017df7c03faf846c51DRC                           compptr->width_in_blocks * DCTSIZE,
180e5eaf37440b8e337ab150c017df7c03faf846c51DRC                           (int) (*out_row_group_ctr * compptr->v_samp_factor),
181e5eaf37440b8e337ab150c017df7c03faf846c51DRC                           (int) (out_row_groups_avail * compptr->v_samp_factor));
18236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      }
18336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      *out_row_group_ctr = out_row_groups_avail;
184e5eaf37440b8e337ab150c017df7c03faf846c51DRC      break;                    /* can exit outer loop without test */
18536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    }
18636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
18736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
18836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
18936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
19036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#ifdef CONTEXT_ROWS_SUPPORTED
19136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
19236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
19336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Process some data in the context case.
19436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
19536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
196489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void)
19736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanepre_process_context (j_compress_ptr cinfo,
198e5eaf37440b8e337ab150c017df7c03faf846c51DRC                     JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
199e5eaf37440b8e337ab150c017df7c03faf846c51DRC                     JDIMENSION in_rows_avail,
200e5eaf37440b8e337ab150c017df7c03faf846c51DRC                     JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
201e5eaf37440b8e337ab150c017df7c03faf846c51DRC                     JDIMENSION out_row_groups_avail)
20236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
20336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
20436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  int numrows, ci;
20536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  int buf_height = cinfo->max_v_samp_factor * 3;
20636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  JDIMENSION inrows;
20736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
20836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  while (*out_row_group_ctr < out_row_groups_avail) {
20936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    if (*in_row_ctr < in_rows_avail) {
21036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      /* Do color conversion to fill the conversion buffer. */
21136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      inrows = in_rows_avail - *in_row_ctr;
21236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      numrows = prep->next_buf_stop - prep->next_buf_row;
21336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      numrows = (int) MIN((JDIMENSION) numrows, inrows);
21436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
215e5eaf37440b8e337ab150c017df7c03faf846c51DRC                                         prep->color_buf,
216e5eaf37440b8e337ab150c017df7c03faf846c51DRC                                         (JDIMENSION) prep->next_buf_row,
217e5eaf37440b8e337ab150c017df7c03faf846c51DRC                                         numrows);
21836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      /* Pad at top of image, if first time through */
21936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      if (prep->rows_to_go == cinfo->image_height) {
220e5eaf37440b8e337ab150c017df7c03faf846c51DRC        for (ci = 0; ci < cinfo->num_components; ci++) {
221e5eaf37440b8e337ab150c017df7c03faf846c51DRC          int row;
222e5eaf37440b8e337ab150c017df7c03faf846c51DRC          for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
223e5eaf37440b8e337ab150c017df7c03faf846c51DRC            jcopy_sample_rows(prep->color_buf[ci], 0,
224e5eaf37440b8e337ab150c017df7c03faf846c51DRC                              prep->color_buf[ci], -row,
225e5eaf37440b8e337ab150c017df7c03faf846c51DRC                              1, cinfo->image_width);
226e5eaf37440b8e337ab150c017df7c03faf846c51DRC          }
227e5eaf37440b8e337ab150c017df7c03faf846c51DRC        }
22836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      }
22936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      *in_row_ctr += numrows;
23036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      prep->next_buf_row += numrows;
23136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      prep->rows_to_go -= numrows;
23236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    } else {
23336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      /* Return for more data, unless we are at the bottom of the image. */
23436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      if (prep->rows_to_go != 0)
235e5eaf37440b8e337ab150c017df7c03faf846c51DRC        break;
236489583f5165e05d37302e8eeec58104ea0109127Thomas G. Lane      /* When at bottom of image, pad to fill the conversion buffer. */
237489583f5165e05d37302e8eeec58104ea0109127Thomas G. Lane      if (prep->next_buf_row < prep->next_buf_stop) {
238e5eaf37440b8e337ab150c017df7c03faf846c51DRC        for (ci = 0; ci < cinfo->num_components; ci++) {
239e5eaf37440b8e337ab150c017df7c03faf846c51DRC          expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
240e5eaf37440b8e337ab150c017df7c03faf846c51DRC                             prep->next_buf_row, prep->next_buf_stop);
241e5eaf37440b8e337ab150c017df7c03faf846c51DRC        }
242e5eaf37440b8e337ab150c017df7c03faf846c51DRC        prep->next_buf_row = prep->next_buf_stop;
24336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      }
24436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    }
24536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* If we've gotten enough data, downsample a row group. */
24636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    if (prep->next_buf_row == prep->next_buf_stop) {
24736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      (*cinfo->downsample->downsample) (cinfo,
248e5eaf37440b8e337ab150c017df7c03faf846c51DRC                                        prep->color_buf,
249e5eaf37440b8e337ab150c017df7c03faf846c51DRC                                        (JDIMENSION) prep->this_row_group,
250e5eaf37440b8e337ab150c017df7c03faf846c51DRC                                        output_buf, *out_row_group_ctr);
25136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      (*out_row_group_ctr)++;
25236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      /* Advance pointers with wraparound as necessary. */
25336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      prep->this_row_group += cinfo->max_v_samp_factor;
25436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      if (prep->this_row_group >= buf_height)
255e5eaf37440b8e337ab150c017df7c03faf846c51DRC        prep->this_row_group = 0;
25636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      if (prep->next_buf_row >= buf_height)
257e5eaf37440b8e337ab150c017df7c03faf846c51DRC        prep->next_buf_row = 0;
25836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
25936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    }
26036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
26136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
26236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
26336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
26436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
26536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Create the wrapped-around downsampling input buffer needed for context mode.
26636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
26736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
268489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(void)
26936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanecreate_context_buffer (j_compress_ptr cinfo)
27036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
27136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
27236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  int rgroup_height = cinfo->max_v_samp_factor;
27336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  int ci, i;
27436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  jpeg_component_info * compptr;
27536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  JSAMPARRAY true_buffer, fake_buffer;
27636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
27736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Grab enough space for fake row pointers for all the components;
27836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   * we need five row groups' worth of pointers for each component.
27936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   */
28036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  fake_buffer = (JSAMPARRAY)
28136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
282e5eaf37440b8e337ab150c017df7c03faf846c51DRC                                (cinfo->num_components * 5 * rgroup_height) *
2835de454b291f48382648a5d1dc2aa0fca8b5786d4DRC                                sizeof(JSAMPROW));
28436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
28536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
28636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane       ci++, compptr++) {
28736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* Allocate the actual buffer space (3 row groups) for this component.
28836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane     * We make the buffer wide enough to allow the downsampler to edge-expand
28936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane     * horizontally within the buffer, if it so chooses.
29036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane     */
29136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    true_buffer = (*cinfo->mem->alloc_sarray)
29236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      ((j_common_ptr) cinfo, JPOOL_IMAGE,
29336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane       (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
294e5eaf37440b8e337ab150c017df7c03faf846c51DRC                      cinfo->max_h_samp_factor) / compptr->h_samp_factor),
29536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane       (JDIMENSION) (3 * rgroup_height));
29636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* Copy true buffer row pointers into the middle of the fake row array */
29736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    MEMCOPY(fake_buffer + rgroup_height, true_buffer,
2985de454b291f48382648a5d1dc2aa0fca8b5786d4DRC            3 * rgroup_height * sizeof(JSAMPROW));
29936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* Fill in the above and below wraparound pointers */
30036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    for (i = 0; i < rgroup_height; i++) {
30136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      fake_buffer[i] = true_buffer[2 * rgroup_height + i];
30236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      fake_buffer[4 * rgroup_height + i] = true_buffer[i];
30336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    }
30436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    prep->color_buf[ci] = fake_buffer + rgroup_height;
30536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    fake_buffer += 5 * rgroup_height; /* point to space for next component */
30636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
30736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
30836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
30936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#endif /* CONTEXT_ROWS_SUPPORTED */
31036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
31136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
31236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
31336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Initialize preprocessing controller.
31436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
31536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
316489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
31736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanejinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
31836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
31936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  my_prep_ptr prep;
32036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  int ci;
32136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  jpeg_component_info * compptr;
32236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
323e5eaf37440b8e337ab150c017df7c03faf846c51DRC  if (need_full_buffer)         /* safety check */
32436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
32536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
32636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  prep = (my_prep_ptr)
32736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
3285de454b291f48382648a5d1dc2aa0fca8b5786d4DRC                                sizeof(my_prep_controller));
32936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->prep = (struct jpeg_c_prep_controller *) prep;
33036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  prep->pub.start_pass = start_pass_prep;
33136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
33236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Allocate the color conversion buffer.
33336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   * We make the buffer wide enough to allow the downsampler to edge-expand
33436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   * horizontally within the buffer, if it so chooses.
33536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   */
33636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  if (cinfo->downsample->need_context_rows) {
33736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* Set up to provide context rows */
33836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#ifdef CONTEXT_ROWS_SUPPORTED
33936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    prep->pub.pre_process_data = pre_process_context;
34036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    create_context_buffer(cinfo);
34136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#else
34236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    ERREXIT(cinfo, JERR_NOT_COMPILED);
34336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#endif
34436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  } else {
34536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* No context, just make it tall enough for one row group */
34636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    prep->pub.pre_process_data = pre_process_data;
34736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
348e5eaf37440b8e337ab150c017df7c03faf846c51DRC         ci++, compptr++) {
34936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
350e5eaf37440b8e337ab150c017df7c03faf846c51DRC        ((j_common_ptr) cinfo, JPOOL_IMAGE,
351e5eaf37440b8e337ab150c017df7c03faf846c51DRC         (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
352e5eaf37440b8e337ab150c017df7c03faf846c51DRC                        cinfo->max_h_samp_factor) / compptr->h_samp_factor),
353e5eaf37440b8e337ab150c017df7c03faf846c51DRC         (JDIMENSION) cinfo->max_v_samp_factor);
35436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    }
35536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
35636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
357