src/java.desktop/share/native/libjavajpeg/jcmaster.c
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 25859 jdk/src/java.desktop/share/native/libjavajpeg/jcmaster.c@3317bb8137f4
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * jcmaster.c
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * Copyright (C) 1991-1997, Thomas G. Lane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This file is part of the Independent JPEG Group's software.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * For conditions of distribution and use, see the accompanying README file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * This file contains master control logic for the JPEG compressor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * These routines are concerned with parameter validation, initial setup,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * and inter-pass control (determining the number of passes and the work
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * to be done in each pass).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
#define JPEG_INTERNALS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
#include "jinclude.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
#include "jpeglib.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
/* Private state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
typedef enum {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
        main_pass,              /* input data, also do first output step */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
        huff_opt_pass,          /* Huffman code optimization pass */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
        output_pass             /* data output pass */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
} c_pass_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
  struct jpeg_comp_master pub;  /* public fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
  c_pass_type pass_type;        /* the type of the current pass */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
  int pass_number;              /* # of passes completed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
  int total_passes;             /* total # of passes needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
  int scan_number;              /* current index in scan_info[] */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
} my_comp_master;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
typedef my_comp_master * my_master_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Support routines that do various essential calculations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
initial_setup (j_compress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/* Do computations that are needed before master selection phase */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  int ci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  jpeg_component_info *compptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  long samplesperrow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
  JDIMENSION jd_samplesperrow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
  /* Sanity check on image dimensions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
  if (cinfo->image_height <= 0 || cinfo->image_width <= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
      || cinfo->num_components <= 0 || cinfo->input_components <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    ERREXIT(cinfo, JERR_EMPTY_IMAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
  /* Make sure image isn't bigger than I can handle */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
  if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
      (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
  /* Width of an input scanline must be representable as JDIMENSION. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
  samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
  jd_samplesperrow = (JDIMENSION) samplesperrow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
  if ((long) jd_samplesperrow != samplesperrow)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
  /* For now, precision must match compiled-in value... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
  /* Check that number of components won't exceed internal array sizes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
  if (cinfo->num_components > MAX_COMPONENTS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
             MAX_COMPONENTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
  /* Compute maximum sampling factors; check factor validity */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
  cinfo->max_h_samp_factor = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
  cinfo->max_v_samp_factor = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
       ci++, compptr++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
      ERREXIT(cinfo, JERR_BAD_SAMPLING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                   compptr->h_samp_factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                   compptr->v_samp_factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
  /* Compute dimensions of components */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
       ci++, compptr++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /* Fill in the correct component_index value; don't rely on application */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    compptr->component_index = ci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /* For compression, we never do DCT scaling. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    compptr->DCT_scaled_size = DCTSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /* Size in DCT blocks */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    compptr->width_in_blocks = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    (long) (cinfo->max_h_samp_factor * DCTSIZE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    compptr->height_in_blocks = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    (long) (cinfo->max_v_samp_factor * DCTSIZE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /* Size in samples */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    compptr->downsampled_width = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    (long) cinfo->max_h_samp_factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    compptr->downsampled_height = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    (long) cinfo->max_v_samp_factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /* Mark component needed (this flag isn't actually used for compression) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    compptr->component_needed = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
  /* Compute number of fully interleaved MCU rows (number of times that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
   * main controller will call coefficient controller).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
  cinfo->total_iMCU_rows = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    jdiv_round_up((long) cinfo->image_height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                  (long) (cinfo->max_v_samp_factor*DCTSIZE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
#ifdef C_MULTISCAN_FILES_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
validate_script (j_compress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
/* Verify that the scan script in cinfo->scan_info[] is valid; also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
  const jpeg_scan_info * scanptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
  int scanno, ncomps, ci, coefi, thisi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
  int Ss, Se, Ah, Al;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
  boolean component_sent[MAX_COMPONENTS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
#ifdef C_PROGRESSIVE_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
  int * last_bitpos_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
  int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
  /* -1 until that coefficient has been seen; then last Al for it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
  if (cinfo->num_scans <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
  /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
   * for progressive JPEG, no scan can have this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
  scanptr = cinfo->scan_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
  if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
#ifdef C_PROGRESSIVE_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    cinfo->progressive_mode = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    last_bitpos_ptr = & last_bitpos[0][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    for (ci = 0; ci < cinfo->num_components; ci++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
      for (coefi = 0; coefi < DCTSIZE2; coefi++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        *last_bitpos_ptr++ = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    ERREXIT(cinfo, JERR_NOT_COMPILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    cinfo->progressive_mode = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    for (ci = 0; ci < cinfo->num_components; ci++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
      component_sent[ci] = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
  for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /* Validate component indexes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    ncomps = scanptr->comps_in_scan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    for (ci = 0; ci < ncomps; ci++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
      thisi = scanptr->component_index[ci];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
      if (thisi < 0 || thisi >= cinfo->num_components)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
      /* Components must appear in SOF order within each scan */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
      if (ci > 0 && thisi <= scanptr->component_index[ci-1])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /* Validate progression parameters */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    Ss = scanptr->Ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    Se = scanptr->Se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    Ah = scanptr->Ah;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    Al = scanptr->Al;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    if (cinfo->progressive_mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
#ifdef C_PROGRESSIVE_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
      /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
       * seems wrong: the upper bound ought to depend on data precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
       * Perhaps they really meant 0..N+1 for N-bit precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
       * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
       * out-of-range reconstructed DC values during the first DC scan,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
       * which might cause problems for some decoders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
#if BITS_IN_JSAMPLE == 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
#define MAX_AH_AL 10
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
#define MAX_AH_AL 13
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
      if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
          Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
      if (Ss == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (Se != 0)            /* DC and AC together not OK */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
      } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (ncomps != 1)        /* AC scans must be for only one component */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
      for (ci = 0; ci < ncomps; ci++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
          ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        for (coefi = Ss; coefi <= Se; coefi++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
          if (last_bitpos_ptr[coefi] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            /* first scan of this coefficient */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if (Ah != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
              ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
          } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            /* not first scan */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
              ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
          last_bitpos_ptr[coefi] = Al;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
      /* For sequential JPEG, all progression parameters must be these: */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
      if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
      /* Make sure components are not sent twice */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
      for (ci = 0; ci < ncomps; ci++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        thisi = scanptr->component_index[ci];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (component_sent[thisi])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
          ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        component_sent[thisi] = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
  /* Now verify that everything got sent. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
  if (cinfo->progressive_mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
#ifdef C_PROGRESSIVE_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /* For progressive mode, we only check that at least some DC data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * got sent for each component; the spec does not require that all bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * of all coefficients be transmitted.  Would it be wiser to enforce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * transmission of all coefficient bits??
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    for (ci = 0; ci < cinfo->num_components; ci++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
      if (last_bitpos[ci][0] < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        ERREXIT(cinfo, JERR_MISSING_DATA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    for (ci = 0; ci < cinfo->num_components; ci++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
      if (! component_sent[ci])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        ERREXIT(cinfo, JERR_MISSING_DATA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
#endif /* C_MULTISCAN_FILES_SUPPORTED */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
select_scan_parameters (j_compress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
/* Set up the scan parameters for the current scan */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
  int ci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
#ifdef C_MULTISCAN_FILES_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
  if (cinfo->scan_info != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /* Prepare for current scan --- the script is already validated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    my_master_ptr master = (my_master_ptr) cinfo->master;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    cinfo->comps_in_scan = scanptr->comps_in_scan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
      cinfo->cur_comp_info[ci] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        &cinfo->comp_info[scanptr->component_index[ci]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    cinfo->Ss = scanptr->Ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    cinfo->Se = scanptr->Se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    cinfo->Ah = scanptr->Ah;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    cinfo->Al = scanptr->Al;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
  else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /* Prepare for single sequential-JPEG scan containing all components */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    if (cinfo->num_components > MAX_COMPS_IN_SCAN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
               MAX_COMPS_IN_SCAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    cinfo->comps_in_scan = cinfo->num_components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    for (ci = 0; ci < cinfo->num_components; ci++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
      cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    cinfo->Ss = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    cinfo->Se = DCTSIZE2-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    cinfo->Ah = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    cinfo->Al = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
per_scan_setup (j_compress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
/* Do computations that are needed before processing a JPEG scan */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
  int ci, mcublks, tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
  jpeg_component_info *compptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
  if (cinfo->comps_in_scan == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /* Noninterleaved (single-component) scan */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    compptr = cinfo->cur_comp_info[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /* Overall image size in MCUs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    cinfo->MCUs_per_row = compptr->width_in_blocks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /* For noninterleaved scan, always one block per MCU */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    compptr->MCU_width = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    compptr->MCU_height = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    compptr->MCU_blocks = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    compptr->MCU_sample_width = DCTSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    compptr->last_col_width = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /* For noninterleaved scans, it is convenient to define last_row_height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * as the number of block rows present in the last iMCU row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    if (tmp == 0) tmp = compptr->v_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    compptr->last_row_height = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /* Prepare array describing MCU composition */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    cinfo->blocks_in_MCU = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    cinfo->MCU_membership[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /* Interleaved (multi-component) scan */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
               MAX_COMPS_IN_SCAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    /* Overall image size in MCUs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    cinfo->MCUs_per_row = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
      jdiv_round_up((long) cinfo->image_width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    (long) (cinfo->max_h_samp_factor*DCTSIZE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    cinfo->MCU_rows_in_scan = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
      jdiv_round_up((long) cinfo->image_height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    (long) (cinfo->max_v_samp_factor*DCTSIZE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    cinfo->blocks_in_MCU = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
      compptr = cinfo->cur_comp_info[ci];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
      /* Sampling factors give # of blocks of component in each MCU */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
      compptr->MCU_width = compptr->h_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
      compptr->MCU_height = compptr->v_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
      compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
      /* Figure number of non-dummy blocks in last MCU column & row */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
      tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
      if (tmp == 0) tmp = compptr->MCU_width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
      compptr->last_col_width = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
      tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
      if (tmp == 0) tmp = compptr->MCU_height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
      compptr->last_row_height = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
      /* Prepare array describing MCU composition */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
      mcublks = compptr->MCU_blocks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
      if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
      while (mcublks-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
  /* Convert restart specified in rows to actual MCU count. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
  /* Note that count must fit in 16 bits, so we provide limiting. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
  if (cinfo->restart_in_rows > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
 * Per-pass setup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
 * This is called at the beginning of each pass.  We determine which modules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
 * will be active during this pass and give them appropriate start_pass calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
 * We also set is_last_pass to indicate whether any more passes will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
 * required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
prepare_for_pass (j_compress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
  my_master_ptr master = (my_master_ptr) cinfo->master;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
  switch (master->pass_type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
  case main_pass:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /* Initial pass: will collect input data, and do either Huffman
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * optimization or data output for the first scan.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    select_scan_parameters(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    per_scan_setup(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    if (! cinfo->raw_data_in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
      (*cinfo->cconvert->start_pass) (cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
      (*cinfo->downsample->start_pass) (cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
      (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    (*cinfo->fdct->start_pass) (cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    (*cinfo->coef->start_pass) (cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                                (master->total_passes > 1 ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                                 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    if (cinfo->optimize_coding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
      /* No immediate data output; postpone writing frame/scan headers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
      master->pub.call_pass_startup = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
      /* Will write frame/scan headers at first jpeg_write_scanlines call */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
      master->pub.call_pass_startup = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
#ifdef ENTROPY_OPT_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
  case huff_opt_pass:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /* Do Huffman optimization for a scan after the first one. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    select_scan_parameters(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    per_scan_setup(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
      (*cinfo->entropy->start_pass) (cinfo, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
      (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
      master->pub.call_pass_startup = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /* Special case: Huffman DC refinement scans need no Huffman table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * and therefore we can skip the optimization pass for them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    master->pass_type = output_pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    master->pass_number++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    /*FALLTHROUGH*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
  case output_pass:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /* Do a data-output pass. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /* We need not repeat per-scan setup if prior optimization pass did it. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    if (! cinfo->optimize_coding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
      select_scan_parameters(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
      per_scan_setup(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    (*cinfo->entropy->start_pass) (cinfo, FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    /* We emit frame/scan headers now */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    if (master->scan_number == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
      (*cinfo->marker->write_frame_header) (cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    (*cinfo->marker->write_scan_header) (cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    master->pub.call_pass_startup = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
  default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    ERREXIT(cinfo, JERR_NOT_COMPILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
  master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
  /* Set up progress monitor's pass info if present */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
  if (cinfo->progress != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    cinfo->progress->completed_passes = master->pass_number;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    cinfo->progress->total_passes = master->total_passes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
 * Special start-of-pass hook.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
 * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
 * In single-pass processing, we need this hook because we don't want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
 * write frame/scan headers during jpeg_start_compress; we want to let the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
 * application write COM markers etc. between jpeg_start_compress and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
 * jpeg_write_scanlines loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
 * In multi-pass processing, this routine is not used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
pass_startup (j_compress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
  cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
  (*cinfo->marker->write_frame_header) (cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
  (*cinfo->marker->write_scan_header) (cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
 * Finish up at end of pass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
finish_pass_master (j_compress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
  my_master_ptr master = (my_master_ptr) cinfo->master;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
  /* The entropy coder always needs an end-of-pass call,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
   * either to analyze statistics or to flush its output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
  (*cinfo->entropy->finish_pass) (cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
  /* Update state for next pass */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
  switch (master->pass_type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
  case main_pass:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    /* next pass is either output of scan 0 (after optimization)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * or output of scan 1 (if no optimization).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    master->pass_type = output_pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    if (! cinfo->optimize_coding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
      master->scan_number++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
  case huff_opt_pass:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /* next pass is always output of current scan */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    master->pass_type = output_pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
  case output_pass:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    /* next pass is either optimization or output of next scan */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    if (cinfo->optimize_coding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
      master->pass_type = huff_opt_pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    master->scan_number++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
  master->pass_number++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
 * Initialize master compression control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
  my_master_ptr master;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
  master = (my_master_ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                                  SIZEOF(my_comp_master));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
  cinfo->master = (struct jpeg_comp_master *) master;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
  master->pub.prepare_for_pass = prepare_for_pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
  master->pub.pass_startup = pass_startup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
  master->pub.finish_pass = finish_pass_master;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
  master->pub.is_last_pass = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
  /* Validate parameters, determine derived values */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
  initial_setup(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
  if (cinfo->scan_info != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
#ifdef C_MULTISCAN_FILES_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    validate_script(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    ERREXIT(cinfo, JERR_NOT_COMPILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    cinfo->progressive_mode = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    cinfo->num_scans = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
  if (cinfo->progressive_mode)  /*  TEMPORARY HACK ??? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
  /* Initialize my private state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
  if (transcode_only) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    /* no main pass in transcoding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    if (cinfo->optimize_coding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
      master->pass_type = huff_opt_pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
      master->pass_type = output_pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    /* for normal compression, first pass is always this type: */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    master->pass_type = main_pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
  master->scan_number = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
  master->pass_number = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
  if (cinfo->optimize_coding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    master->total_passes = cinfo->num_scans * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
  else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    master->total_passes = cinfo->num_scans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
}