shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * jcmaster.c
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file was part of the Independent JPEG Group's software:
shun-iwasawa 82a8f5
 * Copyright (C) 1991-1997, Thomas G. Lane.
shun-iwasawa 82a8f5
 * Modified 2003-2010 by Guido Vollbeding.
shun-iwasawa 82a8f5
 * libjpeg-turbo Modifications:
shun-iwasawa 82a8f5
 * Copyright (C) 2010, 2016, 2018, D. R. Commander.
shun-iwasawa 82a8f5
 * For conditions of distribution and use, see the accompanying README.ijg
shun-iwasawa 82a8f5
 * file.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file contains master control logic for the JPEG compressor.
shun-iwasawa 82a8f5
 * These routines are concerned with parameter validation, initial setup,
shun-iwasawa 82a8f5
 * and inter-pass control (determining the number of passes and the work
shun-iwasawa 82a8f5
 * to be done in each pass).
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define JPEG_INTERNALS
shun-iwasawa 82a8f5
#include "jinclude.h"
shun-iwasawa 82a8f5
#include "jpeglib.h"
shun-iwasawa 82a8f5
#include "jpegcomp.h"
shun-iwasawa 82a8f5
#include "jconfigint.h"
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Private state */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef enum {
shun-iwasawa 82a8f5
  main_pass,                    /* input data, also do first output step */
shun-iwasawa 82a8f5
  huff_opt_pass,                /* Huffman code optimization pass */
shun-iwasawa 82a8f5
  output_pass                   /* data output pass */
shun-iwasawa 82a8f5
} c_pass_type;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef struct {
shun-iwasawa 82a8f5
  struct jpeg_comp_master pub;  /* public fields */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  c_pass_type pass_type;        /* the type of the current pass */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  int pass_number;              /* # of passes completed */
shun-iwasawa 82a8f5
  int total_passes;             /* total # of passes needed */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  int scan_number;              /* current index in scan_info[] */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /*
shun-iwasawa 82a8f5
   * This is here so we can add libjpeg-turbo version/build information to the
shun-iwasawa 82a8f5
   * global string table without introducing a new global symbol.  Adding this
shun-iwasawa 82a8f5
   * information to the global string table allows one to examine a binary
shun-iwasawa 82a8f5
   * object and determine which version of libjpeg-turbo it was built from or
shun-iwasawa 82a8f5
   * linked against.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  const char *jpeg_version;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
} my_comp_master;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef my_comp_master *my_master_ptr;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Support routines that do various essential calculations.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#if JPEG_LIB_VERSION >= 70
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Compute JPEG image dimensions and related values.
shun-iwasawa 82a8f5
 * NOTE: this is exported for possible use by application.
shun-iwasawa 82a8f5
 * Hence it mustn't do anything that can't be done twice.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
GLOBAL(void)
shun-iwasawa 82a8f5
jpeg_calc_jpeg_dimensions(j_compress_ptr cinfo)
shun-iwasawa 82a8f5
/* Do computations that are needed before master selection phase */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  /* Hardwire it to "no scaling" */
shun-iwasawa 82a8f5
  cinfo->jpeg_width = cinfo->image_width;
shun-iwasawa 82a8f5
  cinfo->jpeg_height = cinfo->image_height;
shun-iwasawa 82a8f5
  cinfo->min_DCT_h_scaled_size = DCTSIZE;
shun-iwasawa 82a8f5
  cinfo->min_DCT_v_scaled_size = DCTSIZE;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
initial_setup(j_compress_ptr cinfo, boolean transcode_only)
shun-iwasawa 82a8f5
/* Do computations that are needed before master selection phase */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int ci;
shun-iwasawa 82a8f5
  jpeg_component_info *compptr;
shun-iwasawa 82a8f5
  long samplesperrow;
shun-iwasawa 82a8f5
  JDIMENSION jd_samplesperrow;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#if JPEG_LIB_VERSION >= 70
shun-iwasawa 82a8f5
#if JPEG_LIB_VERSION >= 80
shun-iwasawa 82a8f5
  if (!transcode_only)
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
    jpeg_calc_jpeg_dimensions(cinfo);
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Sanity check on image dimensions */
shun-iwasawa 82a8f5
  if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0 ||
shun-iwasawa 82a8f5
      cinfo->num_components <= 0 || cinfo->input_components <= 0)
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_EMPTY_IMAGE);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Make sure image isn't bigger than I can handle */
shun-iwasawa 82a8f5
  if ((long)cinfo->_jpeg_height > (long)JPEG_MAX_DIMENSION ||
shun-iwasawa 82a8f5
      (long)cinfo->_jpeg_width > (long)JPEG_MAX_DIMENSION)
shun-iwasawa 82a8f5
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int)JPEG_MAX_DIMENSION);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Width of an input scanline must be representable as JDIMENSION. */
shun-iwasawa 82a8f5
  samplesperrow = (long)cinfo->image_width * (long)cinfo->input_components;
shun-iwasawa 82a8f5
  jd_samplesperrow = (JDIMENSION)samplesperrow;
shun-iwasawa 82a8f5
  if ((long)jd_samplesperrow != samplesperrow)
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* For now, precision must match compiled-in value... */
shun-iwasawa 82a8f5
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
shun-iwasawa 82a8f5
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Check that number of components won't exceed internal array sizes */
shun-iwasawa 82a8f5
  if (cinfo->num_components > MAX_COMPONENTS)
shun-iwasawa 82a8f5
    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
shun-iwasawa 82a8f5
             MAX_COMPONENTS);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Compute maximum sampling factors; check factor validity */
shun-iwasawa 82a8f5
  cinfo->max_h_samp_factor = 1;
shun-iwasawa 82a8f5
  cinfo->max_v_samp_factor = 1;
shun-iwasawa 82a8f5
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
shun-iwasawa 82a8f5
       ci++, compptr++) {
shun-iwasawa 82a8f5
    if (compptr->h_samp_factor <= 0 ||
shun-iwasawa 82a8f5
        compptr->h_samp_factor > MAX_SAMP_FACTOR ||
shun-iwasawa 82a8f5
        compptr->v_samp_factor <= 0 ||
shun-iwasawa 82a8f5
        compptr->v_samp_factor > MAX_SAMP_FACTOR)
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_BAD_SAMPLING);
shun-iwasawa 82a8f5
    cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
shun-iwasawa 82a8f5
                                   compptr->h_samp_factor);
shun-iwasawa 82a8f5
    cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
shun-iwasawa 82a8f5
                                   compptr->v_samp_factor);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Compute dimensions of components */
shun-iwasawa 82a8f5
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
shun-iwasawa 82a8f5
       ci++, compptr++) {
shun-iwasawa 82a8f5
    /* Fill in the correct component_index value; don't rely on application */
shun-iwasawa 82a8f5
    compptr->component_index = ci;
shun-iwasawa 82a8f5
    /* For compression, we never do DCT scaling. */
shun-iwasawa 82a8f5
#if JPEG_LIB_VERSION >= 70
shun-iwasawa 82a8f5
    compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE;
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
    compptr->DCT_scaled_size = DCTSIZE;
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
    /* Size in DCT blocks */
shun-iwasawa 82a8f5
    compptr->width_in_blocks = (JDIMENSION)
shun-iwasawa 82a8f5
      jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor,
shun-iwasawa 82a8f5
                    (long)(cinfo->max_h_samp_factor * DCTSIZE));
shun-iwasawa 82a8f5
    compptr->height_in_blocks = (JDIMENSION)
shun-iwasawa 82a8f5
      jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor,
shun-iwasawa 82a8f5
                    (long)(cinfo->max_v_samp_factor * DCTSIZE));
shun-iwasawa 82a8f5
    /* Size in samples */
shun-iwasawa 82a8f5
    compptr->downsampled_width = (JDIMENSION)
shun-iwasawa 82a8f5
      jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor,
shun-iwasawa 82a8f5
                    (long)cinfo->max_h_samp_factor);
shun-iwasawa 82a8f5
    compptr->downsampled_height = (JDIMENSION)
shun-iwasawa 82a8f5
      jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor,
shun-iwasawa 82a8f5
                    (long)cinfo->max_v_samp_factor);
shun-iwasawa 82a8f5
    /* Mark component needed (this flag isn't actually used for compression) */
shun-iwasawa 82a8f5
    compptr->component_needed = TRUE;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Compute number of fully interleaved MCU rows (number of times that
shun-iwasawa 82a8f5
   * main controller will call coefficient controller).
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  cinfo->total_iMCU_rows = (JDIMENSION)
shun-iwasawa 82a8f5
    jdiv_round_up((long)cinfo->_jpeg_height,
shun-iwasawa 82a8f5
                  (long)(cinfo->max_v_samp_factor * DCTSIZE));
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifdef C_MULTISCAN_FILES_SUPPORTED
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
validate_script(j_compress_ptr cinfo)
shun-iwasawa 82a8f5
/* Verify that the scan script in cinfo->scan_info[] is valid; also
shun-iwasawa 82a8f5
 * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  const jpeg_scan_info *scanptr;
shun-iwasawa 82a8f5
  int scanno, ncomps, ci, coefi, thisi;
shun-iwasawa 82a8f5
  int Ss, Se, Ah, Al;
shun-iwasawa 82a8f5
  boolean component_sent[MAX_COMPONENTS];
shun-iwasawa 82a8f5
#ifdef C_PROGRESSIVE_SUPPORTED
shun-iwasawa 82a8f5
  int *last_bitpos_ptr;
shun-iwasawa 82a8f5
  int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
shun-iwasawa 82a8f5
  /* -1 until that coefficient has been seen; then last Al for it */
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (cinfo->num_scans <= 0)
shun-iwasawa 82a8f5
    ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
shun-iwasawa 82a8f5
   * for progressive JPEG, no scan can have this.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  scanptr = cinfo->scan_info;
shun-iwasawa 82a8f5
  if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2 - 1) {
shun-iwasawa 82a8f5
#ifdef C_PROGRESSIVE_SUPPORTED
shun-iwasawa 82a8f5
    cinfo->progressive_mode = TRUE;
shun-iwasawa 82a8f5
    last_bitpos_ptr = &last_bitpos[0][0];
shun-iwasawa 82a8f5
    for (ci = 0; ci < cinfo->num_components; ci++)
shun-iwasawa 82a8f5
      for (coefi = 0; coefi < DCTSIZE2; coefi++)
shun-iwasawa 82a8f5
        *last_bitpos_ptr++ = -1;
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_NOT_COMPILED);
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    cinfo->progressive_mode = FALSE;
shun-iwasawa 82a8f5
    for (ci = 0; ci < cinfo->num_components; ci++)
shun-iwasawa 82a8f5
      component_sent[ci] = FALSE;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
shun-iwasawa 82a8f5
    /* Validate component indexes */
shun-iwasawa 82a8f5
    ncomps = scanptr->comps_in_scan;
shun-iwasawa 82a8f5
    if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
shun-iwasawa 82a8f5
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
shun-iwasawa 82a8f5
    for (ci = 0; ci < ncomps; ci++) {
shun-iwasawa 82a8f5
      thisi = scanptr->component_index[ci];
shun-iwasawa 82a8f5
      if (thisi < 0 || thisi >= cinfo->num_components)
shun-iwasawa 82a8f5
        ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
shun-iwasawa 82a8f5
      /* Components must appear in SOF order within each scan */
shun-iwasawa 82a8f5
      if (ci > 0 && thisi <= scanptr->component_index[ci - 1])
shun-iwasawa 82a8f5
        ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    /* Validate progression parameters */
shun-iwasawa 82a8f5
    Ss = scanptr->Ss;
shun-iwasawa 82a8f5
    Se = scanptr->Se;
shun-iwasawa 82a8f5
    Ah = scanptr->Ah;
shun-iwasawa 82a8f5
    Al = scanptr->Al;
shun-iwasawa 82a8f5
    if (cinfo->progressive_mode) {
shun-iwasawa 82a8f5
#ifdef C_PROGRESSIVE_SUPPORTED
shun-iwasawa 82a8f5
      /* Rec. ITU-T T.81 | ISO/IEC 10918-1 simply gives the ranges 0..13 for Ah
shun-iwasawa 82a8f5
       * and Al, but that seems wrong: the upper bound ought to depend on data
shun-iwasawa 82a8f5
       * precision.  Perhaps they really meant 0..N+1 for N-bit precision.
shun-iwasawa 82a8f5
       * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
shun-iwasawa 82a8f5
       * out-of-range reconstructed DC values during the first DC scan,
shun-iwasawa 82a8f5
       * which might cause problems for some decoders.
shun-iwasawa 82a8f5
       */
shun-iwasawa 82a8f5
#if BITS_IN_JSAMPLE == 8
shun-iwasawa 82a8f5
#define MAX_AH_AL  10
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
#define MAX_AH_AL  13
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
      if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
shun-iwasawa 82a8f5
          Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
shun-iwasawa 82a8f5
        ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
shun-iwasawa 82a8f5
      if (Ss == 0) {
shun-iwasawa 82a8f5
        if (Se != 0)            /* DC and AC together not OK */
shun-iwasawa 82a8f5
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
shun-iwasawa 82a8f5
      } else {
shun-iwasawa 82a8f5
        if (ncomps != 1)        /* AC scans must be for only one component */
shun-iwasawa 82a8f5
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
      for (ci = 0; ci < ncomps; ci++) {
shun-iwasawa 82a8f5
        last_bitpos_ptr = &last_bitpos[scanptr->component_index[ci]][0];
shun-iwasawa 82a8f5
        if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
shun-iwasawa 82a8f5
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
shun-iwasawa 82a8f5
        for (coefi = Ss; coefi <= Se; coefi++) {
shun-iwasawa 82a8f5
          if (last_bitpos_ptr[coefi] < 0) {
shun-iwasawa 82a8f5
            /* first scan of this coefficient */
shun-iwasawa 82a8f5
            if (Ah != 0)
shun-iwasawa 82a8f5
              ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
shun-iwasawa 82a8f5
          } else {
shun-iwasawa 82a8f5
            /* not first scan */
shun-iwasawa 82a8f5
            if (Ah != last_bitpos_ptr[coefi] || Al != Ah - 1)
shun-iwasawa 82a8f5
              ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
shun-iwasawa 82a8f5
          }
shun-iwasawa 82a8f5
          last_bitpos_ptr[coefi] = Al;
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      /* For sequential JPEG, all progression parameters must be these: */
shun-iwasawa 82a8f5
      if (Ss != 0 || Se != DCTSIZE2 - 1 || Ah != 0 || Al != 0)
shun-iwasawa 82a8f5
        ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
shun-iwasawa 82a8f5
      /* Make sure components are not sent twice */
shun-iwasawa 82a8f5
      for (ci = 0; ci < ncomps; ci++) {
shun-iwasawa 82a8f5
        thisi = scanptr->component_index[ci];
shun-iwasawa 82a8f5
        if (component_sent[thisi])
shun-iwasawa 82a8f5
          ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
shun-iwasawa 82a8f5
        component_sent[thisi] = TRUE;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Now verify that everything got sent. */
shun-iwasawa 82a8f5
  if (cinfo->progressive_mode) {
shun-iwasawa 82a8f5
#ifdef C_PROGRESSIVE_SUPPORTED
shun-iwasawa 82a8f5
    /* For progressive mode, we only check that at least some DC data
shun-iwasawa 82a8f5
     * got sent for each component; the spec does not require that all bits
shun-iwasawa 82a8f5
     * of all coefficients be transmitted.  Would it be wiser to enforce
shun-iwasawa 82a8f5
     * transmission of all coefficient bits??
shun-iwasawa 82a8f5
     */
shun-iwasawa 82a8f5
    for (ci = 0; ci < cinfo->num_components; ci++) {
shun-iwasawa 82a8f5
      if (last_bitpos[ci][0] < 0)
shun-iwasawa 82a8f5
        ERREXIT(cinfo, JERR_MISSING_DATA);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    for (ci = 0; ci < cinfo->num_components; ci++) {
shun-iwasawa 82a8f5
      if (!component_sent[ci])
shun-iwasawa 82a8f5
        ERREXIT(cinfo, JERR_MISSING_DATA);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#endif /* C_MULTISCAN_FILES_SUPPORTED */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
select_scan_parameters(j_compress_ptr cinfo)
shun-iwasawa 82a8f5
/* Set up the scan parameters for the current scan */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int ci;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifdef C_MULTISCAN_FILES_SUPPORTED
shun-iwasawa 82a8f5
  if (cinfo->scan_info != NULL) {
shun-iwasawa 82a8f5
    /* Prepare for current scan --- the script is already validated */
shun-iwasawa 82a8f5
    my_master_ptr master = (my_master_ptr)cinfo->master;
shun-iwasawa 82a8f5
    const jpeg_scan_info *scanptr = cinfo->scan_info + master->scan_number;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    cinfo->comps_in_scan = scanptr->comps_in_scan;
shun-iwasawa 82a8f5
    for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
shun-iwasawa 82a8f5
      cinfo->cur_comp_info[ci] =
shun-iwasawa 82a8f5
        &cinfo->comp_info[scanptr->component_index[ci]];
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    cinfo->Ss = scanptr->Ss;
shun-iwasawa 82a8f5
    cinfo->Se = scanptr->Se;
shun-iwasawa 82a8f5
    cinfo->Ah = scanptr->Ah;
shun-iwasawa 82a8f5
    cinfo->Al = scanptr->Al;
shun-iwasawa 82a8f5
  } else
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
  {
shun-iwasawa 82a8f5
    /* Prepare for single sequential-JPEG scan containing all components */
shun-iwasawa 82a8f5
    if (cinfo->num_components > MAX_COMPS_IN_SCAN)
shun-iwasawa 82a8f5
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
shun-iwasawa 82a8f5
               MAX_COMPS_IN_SCAN);
shun-iwasawa 82a8f5
    cinfo->comps_in_scan = cinfo->num_components;
shun-iwasawa 82a8f5
    for (ci = 0; ci < cinfo->num_components; ci++) {
shun-iwasawa 82a8f5
      cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    cinfo->Ss = 0;
shun-iwasawa 82a8f5
    cinfo->Se = DCTSIZE2 - 1;
shun-iwasawa 82a8f5
    cinfo->Ah = 0;
shun-iwasawa 82a8f5
    cinfo->Al = 0;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
per_scan_setup(j_compress_ptr cinfo)
shun-iwasawa 82a8f5
/* Do computations that are needed before processing a JPEG scan */
shun-iwasawa 82a8f5
/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int ci, mcublks, tmp;
shun-iwasawa 82a8f5
  jpeg_component_info *compptr;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (cinfo->comps_in_scan == 1) {
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* Noninterleaved (single-component) scan */
shun-iwasawa 82a8f5
    compptr = cinfo->cur_comp_info[0];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* Overall image size in MCUs */
shun-iwasawa 82a8f5
    cinfo->MCUs_per_row = compptr->width_in_blocks;
shun-iwasawa 82a8f5
    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* For noninterleaved scan, always one block per MCU */
shun-iwasawa 82a8f5
    compptr->MCU_width = 1;
shun-iwasawa 82a8f5
    compptr->MCU_height = 1;
shun-iwasawa 82a8f5
    compptr->MCU_blocks = 1;
shun-iwasawa 82a8f5
    compptr->MCU_sample_width = DCTSIZE;
shun-iwasawa 82a8f5
    compptr->last_col_width = 1;
shun-iwasawa 82a8f5
    /* For noninterleaved scans, it is convenient to define last_row_height
shun-iwasawa 82a8f5
     * as the number of block rows present in the last iMCU row.
shun-iwasawa 82a8f5
     */
shun-iwasawa 82a8f5
    tmp = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
shun-iwasawa 82a8f5
    if (tmp == 0) tmp = compptr->v_samp_factor;
shun-iwasawa 82a8f5
    compptr->last_row_height = tmp;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* Prepare array describing MCU composition */
shun-iwasawa 82a8f5
    cinfo->blocks_in_MCU = 1;
shun-iwasawa 82a8f5
    cinfo->MCU_membership[0] = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* Interleaved (multi-component) scan */
shun-iwasawa 82a8f5
    if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
shun-iwasawa 82a8f5
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
shun-iwasawa 82a8f5
               MAX_COMPS_IN_SCAN);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* Overall image size in MCUs */
shun-iwasawa 82a8f5
    cinfo->MCUs_per_row = (JDIMENSION)
shun-iwasawa 82a8f5
      jdiv_round_up((long)cinfo->_jpeg_width,
shun-iwasawa 82a8f5
                    (long)(cinfo->max_h_samp_factor * DCTSIZE));
shun-iwasawa 82a8f5
    cinfo->MCU_rows_in_scan = (JDIMENSION)
shun-iwasawa 82a8f5
      jdiv_round_up((long)cinfo->_jpeg_height,
shun-iwasawa 82a8f5
                    (long)(cinfo->max_v_samp_factor * DCTSIZE));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    cinfo->blocks_in_MCU = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
shun-iwasawa 82a8f5
      compptr = cinfo->cur_comp_info[ci];
shun-iwasawa 82a8f5
      /* Sampling factors give # of blocks of component in each MCU */
shun-iwasawa 82a8f5
      compptr->MCU_width = compptr->h_samp_factor;
shun-iwasawa 82a8f5
      compptr->MCU_height = compptr->v_samp_factor;
shun-iwasawa 82a8f5
      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
shun-iwasawa 82a8f5
      compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
shun-iwasawa 82a8f5
      /* Figure number of non-dummy blocks in last MCU column & row */
shun-iwasawa 82a8f5
      tmp = (int)(compptr->width_in_blocks % compptr->MCU_width);
shun-iwasawa 82a8f5
      if (tmp == 0) tmp = compptr->MCU_width;
shun-iwasawa 82a8f5
      compptr->last_col_width = tmp;
shun-iwasawa 82a8f5
      tmp = (int)(compptr->height_in_blocks % compptr->MCU_height);
shun-iwasawa 82a8f5
      if (tmp == 0) tmp = compptr->MCU_height;
shun-iwasawa 82a8f5
      compptr->last_row_height = tmp;
shun-iwasawa 82a8f5
      /* Prepare array describing MCU composition */
shun-iwasawa 82a8f5
      mcublks = compptr->MCU_blocks;
shun-iwasawa 82a8f5
      if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
shun-iwasawa 82a8f5
        ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
shun-iwasawa 82a8f5
      while (mcublks-- > 0) {
shun-iwasawa 82a8f5
        cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Convert restart specified in rows to actual MCU count. */
shun-iwasawa 82a8f5
  /* Note that count must fit in 16 bits, so we provide limiting. */
shun-iwasawa 82a8f5
  if (cinfo->restart_in_rows > 0) {
shun-iwasawa 82a8f5
    long nominal = (long)cinfo->restart_in_rows * (long)cinfo->MCUs_per_row;
shun-iwasawa 82a8f5
    cinfo->restart_interval = (unsigned int)MIN(nominal, 65535L);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Per-pass setup.
shun-iwasawa 82a8f5
 * This is called at the beginning of each pass.  We determine which modules
shun-iwasawa 82a8f5
 * will be active during this pass and give them appropriate start_pass calls.
shun-iwasawa 82a8f5
 * We also set is_last_pass to indicate whether any more passes will be
shun-iwasawa 82a8f5
 * required.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
prepare_for_pass(j_compress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  my_master_ptr master = (my_master_ptr)cinfo->master;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  switch (master->pass_type) {
shun-iwasawa 82a8f5
  case main_pass:
shun-iwasawa 82a8f5
    /* Initial pass: will collect input data, and do either Huffman
shun-iwasawa 82a8f5
     * optimization or data output for the first scan.
shun-iwasawa 82a8f5
     */
shun-iwasawa 82a8f5
    select_scan_parameters(cinfo);
shun-iwasawa 82a8f5
    per_scan_setup(cinfo);
shun-iwasawa 82a8f5
    if (!cinfo->raw_data_in) {
shun-iwasawa 82a8f5
      (*cinfo->cconvert->start_pass) (cinfo);
shun-iwasawa 82a8f5
      (*cinfo->downsample->start_pass) (cinfo);
shun-iwasawa 82a8f5
      (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    (*cinfo->fdct->start_pass) (cinfo);
shun-iwasawa 82a8f5
    (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
shun-iwasawa 82a8f5
    (*cinfo->coef->start_pass) (cinfo,
shun-iwasawa 82a8f5
                                (master->total_passes > 1 ?
shun-iwasawa 82a8f5
                                 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
shun-iwasawa 82a8f5
    (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
shun-iwasawa 82a8f5
    if (cinfo->optimize_coding) {
shun-iwasawa 82a8f5
      /* No immediate data output; postpone writing frame/scan headers */
shun-iwasawa 82a8f5
      master->pub.call_pass_startup = FALSE;
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      /* Will write frame/scan headers at first jpeg_write_scanlines call */
shun-iwasawa 82a8f5
      master->pub.call_pass_startup = TRUE;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
#ifdef ENTROPY_OPT_SUPPORTED
shun-iwasawa 82a8f5
  case huff_opt_pass:
shun-iwasawa 82a8f5
    /* Do Huffman optimization for a scan after the first one. */
shun-iwasawa 82a8f5
    select_scan_parameters(cinfo);
shun-iwasawa 82a8f5
    per_scan_setup(cinfo);
shun-iwasawa 82a8f5
    if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
shun-iwasawa 82a8f5
      (*cinfo->entropy->start_pass) (cinfo, TRUE);
shun-iwasawa 82a8f5
      (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
shun-iwasawa 82a8f5
      master->pub.call_pass_startup = FALSE;
shun-iwasawa 82a8f5
      break;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    /* Special case: Huffman DC refinement scans need no Huffman table
shun-iwasawa 82a8f5
     * and therefore we can skip the optimization pass for them.
shun-iwasawa 82a8f5
     */
shun-iwasawa 82a8f5
    master->pass_type = output_pass;
shun-iwasawa 82a8f5
    master->pass_number++;
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
    /*FALLTHROUGH*/
shun-iwasawa 82a8f5
  case output_pass:
shun-iwasawa 82a8f5
    /* Do a data-output pass. */
shun-iwasawa 82a8f5
    /* We need not repeat per-scan setup if prior optimization pass did it. */
shun-iwasawa 82a8f5
    if (!cinfo->optimize_coding) {
shun-iwasawa 82a8f5
      select_scan_parameters(cinfo);
shun-iwasawa 82a8f5
      per_scan_setup(cinfo);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    (*cinfo->entropy->start_pass) (cinfo, FALSE);
shun-iwasawa 82a8f5
    (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
shun-iwasawa 82a8f5
    /* We emit frame/scan headers now */
shun-iwasawa 82a8f5
    if (master->scan_number == 0)
shun-iwasawa 82a8f5
      (*cinfo->marker->write_frame_header) (cinfo);
shun-iwasawa 82a8f5
    (*cinfo->marker->write_scan_header) (cinfo);
shun-iwasawa 82a8f5
    master->pub.call_pass_startup = FALSE;
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  default:
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_NOT_COMPILED);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  master->pub.is_last_pass = (master->pass_number == master->total_passes - 1);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Set up progress monitor's pass info if present */
shun-iwasawa 82a8f5
  if (cinfo->progress != NULL) {
shun-iwasawa 82a8f5
    cinfo->progress->completed_passes = master->pass_number;
shun-iwasawa 82a8f5
    cinfo->progress->total_passes = master->total_passes;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Special start-of-pass hook.
shun-iwasawa 82a8f5
 * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
shun-iwasawa 82a8f5
 * In single-pass processing, we need this hook because we don't want to
shun-iwasawa 82a8f5
 * write frame/scan headers during jpeg_start_compress; we want to let the
shun-iwasawa 82a8f5
 * application write COM markers etc. between jpeg_start_compress and the
shun-iwasawa 82a8f5
 * jpeg_write_scanlines loop.
shun-iwasawa 82a8f5
 * In multi-pass processing, this routine is not used.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
pass_startup(j_compress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  (*cinfo->marker->write_frame_header) (cinfo);
shun-iwasawa 82a8f5
  (*cinfo->marker->write_scan_header) (cinfo);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Finish up at end of pass.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
finish_pass_master(j_compress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  my_master_ptr master = (my_master_ptr)cinfo->master;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* The entropy coder always needs an end-of-pass call,
shun-iwasawa 82a8f5
   * either to analyze statistics or to flush its output buffer.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  (*cinfo->entropy->finish_pass) (cinfo);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Update state for next pass */
shun-iwasawa 82a8f5
  switch (master->pass_type) {
shun-iwasawa 82a8f5
  case main_pass:
shun-iwasawa 82a8f5
    /* next pass is either output of scan 0 (after optimization)
shun-iwasawa 82a8f5
     * or output of scan 1 (if no optimization).
shun-iwasawa 82a8f5
     */
shun-iwasawa 82a8f5
    master->pass_type = output_pass;
shun-iwasawa 82a8f5
    if (!cinfo->optimize_coding)
shun-iwasawa 82a8f5
      master->scan_number++;
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case huff_opt_pass:
shun-iwasawa 82a8f5
    /* next pass is always output of current scan */
shun-iwasawa 82a8f5
    master->pass_type = output_pass;
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case output_pass:
shun-iwasawa 82a8f5
    /* next pass is either optimization or output of next scan */
shun-iwasawa 82a8f5
    if (cinfo->optimize_coding)
shun-iwasawa 82a8f5
      master->pass_type = huff_opt_pass;
shun-iwasawa 82a8f5
    master->scan_number++;
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  master->pass_number++;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Initialize master compression control.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
GLOBAL(void)
shun-iwasawa 82a8f5
jinit_c_master_control(j_compress_ptr cinfo, boolean transcode_only)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  my_master_ptr master;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  master = (my_master_ptr)
shun-iwasawa 82a8f5
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                sizeof(my_comp_master));
shun-iwasawa 82a8f5
  cinfo->master = (struct jpeg_comp_master *)master;
shun-iwasawa 82a8f5
  master->pub.prepare_for_pass = prepare_for_pass;
shun-iwasawa 82a8f5
  master->pub.pass_startup = pass_startup;
shun-iwasawa 82a8f5
  master->pub.finish_pass = finish_pass_master;
shun-iwasawa 82a8f5
  master->pub.is_last_pass = FALSE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Validate parameters, determine derived values */
shun-iwasawa 82a8f5
  initial_setup(cinfo, transcode_only);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (cinfo->scan_info != NULL) {
shun-iwasawa 82a8f5
#ifdef C_MULTISCAN_FILES_SUPPORTED
shun-iwasawa 82a8f5
    validate_script(cinfo);
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_NOT_COMPILED);
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    cinfo->progressive_mode = FALSE;
shun-iwasawa 82a8f5
    cinfo->num_scans = 1;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (cinfo->progressive_mode && !cinfo->arith_code)  /*  TEMPORARY HACK ??? */
shun-iwasawa 82a8f5
    cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Initialize my private state */
shun-iwasawa 82a8f5
  if (transcode_only) {
shun-iwasawa 82a8f5
    /* no main pass in transcoding */
shun-iwasawa 82a8f5
    if (cinfo->optimize_coding)
shun-iwasawa 82a8f5
      master->pass_type = huff_opt_pass;
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      master->pass_type = output_pass;
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    /* for normal compression, first pass is always this type: */
shun-iwasawa 82a8f5
    master->pass_type = main_pass;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  master->scan_number = 0;
shun-iwasawa 82a8f5
  master->pass_number = 0;
shun-iwasawa 82a8f5
  if (cinfo->optimize_coding)
shun-iwasawa 82a8f5
    master->total_passes = cinfo->num_scans * 2;
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    master->total_passes = cinfo->num_scans;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  master->jpeg_version = PACKAGE_NAME " version " VERSION " (build " BUILD ")";
shun-iwasawa 82a8f5
}