src/java.desktop/share/native/libjavajpeg/jdinput.c
author jiefu
Fri, 15 Nov 2019 20:39:26 +0800
changeset 59110 8c4c358272a9
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234232: [TESTBUG] gc/shenandoah/jvmti/TestHeapDump.java fails with -Xcomp Reviewed-by: zgu
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
 * jdinput.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 input control logic for the JPEG decompressor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * These routines are concerned with controlling the decompressor's input
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * processing (marker reading and coefficient decoding).  The actual input
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
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 struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
  struct jpeg_input_controller pub; /* public fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
  boolean inheaders;            /* TRUE until first SOS is reached */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
} my_input_controller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
typedef my_input_controller * my_inputctl_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/* Forward declarations */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Routines to calculate various quantities related to the size of the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
initial_setup (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/* Called once, when first SOS marker is reached */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
  int ci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
  jpeg_component_info *compptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
  /* Make sure image isn't bigger than I can handle */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
  if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
      (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  /* For now, precision must match compiled-in value... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
  /* Check that number of components won't exceed internal array sizes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
  if (cinfo->num_components > MAX_COMPONENTS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
             MAX_COMPONENTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
  /* Compute maximum sampling factors; check factor validity */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
  cinfo->max_h_samp_factor = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
  cinfo->max_v_samp_factor = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
       ci++, compptr++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
      ERREXIT(cinfo, JERR_BAD_SAMPLING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                   compptr->h_samp_factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                   compptr->v_samp_factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
  /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
   * In the full decompressor, this will be overridden by jdmaster.c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
   * but in the transcoder, jdmaster.c is not used, so we must do it here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
  cinfo->min_DCT_scaled_size = DCTSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
  /* Compute dimensions of components */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
       ci++, compptr++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    compptr->DCT_scaled_size = DCTSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /* Size in DCT blocks */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    compptr->width_in_blocks = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    (long) (cinfo->max_h_samp_factor * DCTSIZE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    compptr->height_in_blocks = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    (long) (cinfo->max_v_samp_factor * DCTSIZE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /* downsampled_width and downsampled_height will also be overridden by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * jdmaster.c if we are doing full decompression.  The transcoder library
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * doesn't use these values, but the calling application might.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /* Size in samples */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    compptr->downsampled_width = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    (long) cinfo->max_h_samp_factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    compptr->downsampled_height = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    (long) cinfo->max_v_samp_factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /* Mark component needed, until color conversion says otherwise */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    compptr->component_needed = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /* Mark no quantization table yet saved for component */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    compptr->quant_table = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
  /* Compute number of fully interleaved MCU rows. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
  cinfo->total_iMCU_rows = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    jdiv_round_up((long) cinfo->image_height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                  (long) (cinfo->max_v_samp_factor*DCTSIZE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
  /* Decide whether file contains multiple scans */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
  if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    cinfo->inputctl->has_multiple_scans = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
  else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    cinfo->inputctl->has_multiple_scans = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
per_scan_setup (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
/* Do computations that are needed before processing a JPEG scan */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
/* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
  int ci, mcublks, tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
  jpeg_component_info *compptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
  if (cinfo->comps_in_scan == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /* Noninterleaved (single-component) scan */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    compptr = cinfo->cur_comp_info[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /* Overall image size in MCUs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    cinfo->MCUs_per_row = compptr->width_in_blocks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /* For noninterleaved scan, always one block per MCU */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    compptr->MCU_width = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    compptr->MCU_height = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    compptr->MCU_blocks = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    compptr->MCU_sample_width = compptr->DCT_scaled_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    compptr->last_col_width = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /* For noninterleaved scans, it is convenient to define last_row_height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * as the number of block rows present in the last iMCU row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    if (tmp == 0) tmp = compptr->v_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    compptr->last_row_height = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /* Prepare array describing MCU composition */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    cinfo->blocks_in_MCU = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    cinfo->MCU_membership[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /* Interleaved (multi-component) scan */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
               MAX_COMPS_IN_SCAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /* Overall image size in MCUs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    cinfo->MCUs_per_row = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
      jdiv_round_up((long) cinfo->image_width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    (long) (cinfo->max_h_samp_factor*DCTSIZE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    cinfo->MCU_rows_in_scan = (JDIMENSION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
      jdiv_round_up((long) cinfo->image_height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    (long) (cinfo->max_v_samp_factor*DCTSIZE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    cinfo->blocks_in_MCU = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
      compptr = cinfo->cur_comp_info[ci];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
      /* Sampling factors give # of blocks of component in each MCU */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
      compptr->MCU_width = compptr->h_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
      compptr->MCU_height = compptr->v_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
      compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
      /* Figure number of non-dummy blocks in last MCU column & row */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
      tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
      if (tmp == 0) tmp = compptr->MCU_width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
      compptr->last_col_width = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
      tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
      if (tmp == 0) tmp = compptr->MCU_height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
      compptr->last_row_height = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
      /* Prepare array describing MCU composition */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
      mcublks = compptr->MCU_blocks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
      if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
      while (mcublks-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * Save away a copy of the Q-table referenced by each component present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * in the current scan, unless already saved during a prior scan.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * In a multiple-scan JPEG file, the encoder could assign different components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * the same Q-table slot number, but change table definitions between scans
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 * so that each component uses a different Q-table.  (The IJG encoder is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 * currently capable of doing this, but other encoders might.)  Since we want
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * to be able to dequantize all the components at the end of the file, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * means that we have to save away the table actually used for each component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * We do this by copying the table at the start of the first scan containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * The JPEG spec prohibits the encoder from changing the contents of a Q-table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * slot between scans of a component using that slot.  If the encoder does so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * anyway, this decoder will simply use the Q-table values that were current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * at the start of the first scan for the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 * The decompressor output side looks only at the saved quant tables,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 * not at the current Q-table slots.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
latch_quant_tables (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
  int ci, qtblno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
  jpeg_component_info *compptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
  JQUANT_TBL * qtbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    compptr = cinfo->cur_comp_info[ci];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /* No work if we already saved Q-table for this component */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    if (compptr->quant_table != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
      continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /* Make sure specified quantization table is present */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    qtblno = compptr->quant_tbl_no;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        cinfo->quant_tbl_ptrs[qtblno] == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
      ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /* OK, save away the quantization table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    qtbl = (JQUANT_TBL *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                  SIZEOF(JQUANT_TBL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    compptr->quant_table = qtbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 * Initialize the input modules to read a scan of compressed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 * The first call to this is done by jdmaster.c after initializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 * the entire decompressor (during jpeg_start_decompress).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 * Subsequent calls come from consume_markers, below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
start_input_pass (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
  per_scan_setup(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
  latch_quant_tables(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
  (*cinfo->entropy->start_pass) (cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
  (*cinfo->coef->start_input_pass) (cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
  cinfo->inputctl->consume_input = cinfo->coef->consume_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 * Finish up after inputting a compressed-data scan.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
 * This is called by the coefficient controller after it's read all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 * the expected data of the scan.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
finish_input_pass (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
  cinfo->inputctl->consume_input = consume_markers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 * Read JPEG markers before, between, or after compressed-data scans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
 * Change state as necessary when a new scan is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
 * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
 * The consume_input method pointer points either here or to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
 * coefficient controller's consume_data routine, depending on whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 * we are reading a compressed data segment or inter-segment markers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
METHODDEF(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
consume_markers (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
  my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
  int val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
  if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    return JPEG_REACHED_EOI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
  val = (*cinfo->marker->read_markers) (cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
  switch (val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
  case JPEG_REACHED_SOS:        /* Found SOS */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    if (inputctl->inheaders) {  /* 1st SOS */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
      initial_setup(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
      inputctl->inheaders = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
      /* Note: start_input_pass must be called by jdmaster.c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
       * before any more input can be consumed.  jdapimin.c is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
       * responsible for enforcing this sequencing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    } else {                    /* 2nd or later SOS marker */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
      if (! inputctl->pub.has_multiple_scans)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
      start_input_pass(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
  case JPEG_REACHED_EOI:        /* Found EOI */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    inputctl->pub.eoi_reached = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    if (inputctl->inheaders) {  /* Tables-only datastream, apparently */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
      if (cinfo->marker->saw_SOF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        ERREXIT(cinfo, JERR_SOF_NO_SOS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
      /* Prevent infinite loop in coef ctlr's decompress_data routine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
       * if user set output_scan_number larger than number of scans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
      if (cinfo->output_scan_number > cinfo->input_scan_number)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        cinfo->output_scan_number = cinfo->input_scan_number;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
  case JPEG_SUSPENDED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
  return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
 * Reset state to begin a fresh datastream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
reset_input_controller (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
  my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
  inputctl->pub.consume_input = consume_markers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
  inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
  inputctl->pub.eoi_reached = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
  inputctl->inheaders = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
  /* Reset other modules */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
  (*cinfo->marker->reset_marker_reader) (cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
  /* Reset progression state -- would be cleaner if entropy decoder did this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
  cinfo->coef_bits = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
 * Initialize the input controller module.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
 * This is called only once, when the decompression object is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
jinit_input_controller (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
  my_inputctl_ptr inputctl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
  /* Create subobject in permanent pool */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
  inputctl = (my_inputctl_ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                                SIZEOF(my_input_controller));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
  cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
  /* Initialize method pointers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
  inputctl->pub.consume_input = consume_markers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
  inputctl->pub.reset_input_controller = reset_input_controller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
  inputctl->pub.start_input_pass = start_input_pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
  inputctl->pub.finish_input_pass = finish_input_pass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
  /* Initialize state: can't use reset_input_controller since we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
   * want to try to reset other modules yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
  inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
  inputctl->pub.eoi_reached = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
  inputctl->inheaders = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
}