src/java.desktop/share/native/libjavajpeg/jctrans.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
 * jctrans.c
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * Copyright (C) 1995-1998, 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 library routines for transcoding compression,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * that is, writing raw DCT coefficient arrays to an output JPEG file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * The routines in jcapimin.c will also be needed by a transcoder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
#define JPEG_INTERNALS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
#include "jinclude.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
#include "jpeglib.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
/* Forward declarations */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
LOCAL(void) transencode_master_selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
        JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
LOCAL(void) transencode_coef_controller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
        JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Compression initialization for writing raw-coefficient data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Before calling this, all parameters and a data destination must be set up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Call jpeg_finish_compress() to actually write the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * The number of passed virtual arrays must match cinfo->num_components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Note that the virtual arrays need not be filled or even realized at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * the time write_coefficients is called; indeed, if the virtual arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * were requested from this compression object's memory manager, they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * typically will be realized during this routine and filled afterwards.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  if (cinfo->global_state != CSTATE_START)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
  /* Mark all tables to be written */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
  jpeg_suppress_tables(cinfo, FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
  /* (Re)initialize error mgr and destination modules */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
  (*cinfo->dest->init_destination) (cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  /* Perform master selection of active modules */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  transencode_master_selection(cinfo, coef_arrays);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  /* Wait for jpeg_finish_compress() call */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  cinfo->next_scanline = 0;     /* so jpeg_write_marker works */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  cinfo->global_state = CSTATE_WRCOEFS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Initialize the compression object with default parameters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * then copy from the source object all parameters needed for lossless
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * transcoding.  Parameters that can be varied without loss (such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * scan script and Huffman optimization) are left in their default states.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                               j_compress_ptr dstinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
  JQUANT_TBL ** qtblptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
  jpeg_component_info *incomp, *outcomp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
  JQUANT_TBL *c_quant, *slot_quant;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
  int tblno, ci, coefi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
  /* Safety check to ensure start_compress not called yet. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
  if (dstinfo->global_state != CSTATE_START)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
  /* Copy fundamental image dimensions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
  dstinfo->image_width = srcinfo->image_width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
  dstinfo->image_height = srcinfo->image_height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
  dstinfo->input_components = srcinfo->num_components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
  dstinfo->in_color_space = srcinfo->jpeg_color_space;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
  /* Initialize all parameters to default values */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
  jpeg_set_defaults(dstinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
  /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
   * Fix it to get the right header markers for the image colorspace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
  jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
  dstinfo->data_precision = srcinfo->data_precision;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
  dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
  /* Copy the source's quantization tables. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
  for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
      qtblptr = & dstinfo->quant_tbl_ptrs[tblno];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
      if (*qtblptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
      MEMCOPY((*qtblptr)->quantval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
              srcinfo->quant_tbl_ptrs[tblno]->quantval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
              SIZEOF((*qtblptr)->quantval));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
      (*qtblptr)->sent_table = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
  /* Copy the source's per-component info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
   * Note we assume jpeg_set_defaults has allocated the dest comp_info array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
  dstinfo->num_components = srcinfo->num_components;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
  if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
             MAX_COMPONENTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
  for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
       ci < dstinfo->num_components; ci++, incomp++, outcomp++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    outcomp->component_id = incomp->component_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    outcomp->h_samp_factor = incomp->h_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    outcomp->v_samp_factor = incomp->v_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    outcomp->quant_tbl_no = incomp->quant_tbl_no;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /* Make sure saved quantization table for component matches the qtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * slot.  If not, the input file re-used this qtable slot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * IJG encoder currently cannot duplicate this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    tblno = outcomp->quant_tbl_no;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        srcinfo->quant_tbl_ptrs[tblno] == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
      ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    slot_quant = srcinfo->quant_tbl_ptrs[tblno];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    c_quant = incomp->quant_table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    if (c_quant != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
      for (coefi = 0; coefi < DCTSIZE2; coefi++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
          ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /* Note: we do not copy the source's Huffman table assignments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * instead we rely on jpeg_set_colorspace to have made a suitable choice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
  /* Also copy JFIF version and resolution information, if available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
   * Strictly speaking this isn't "critical" info, but it's nearly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
   * always appropriate to copy it if available.  In particular,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
   * if the application chooses to copy JFIF 1.02 extension markers from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
   * the source file, we need to copy the version to make sure we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
   * emit a file that has 1.02 extensions but a claimed version of 1.01.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
   * We will *not*, however, copy version info from mislabeled "2.01" files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
  if (srcinfo->saw_JFIF_marker) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    if (srcinfo->JFIF_major_version == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
      dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
      dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    dstinfo->density_unit = srcinfo->density_unit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    dstinfo->X_density = srcinfo->X_density;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    dstinfo->Y_density = srcinfo->Y_density;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * Master selection of compression modules for transcoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * This substitutes for jcinit.c's initialization of the full compressor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
transencode_master_selection (j_compress_ptr cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                              jvirt_barray_ptr * coef_arrays)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
  /* Although we don't actually use input_components for transcoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
   * jcmaster.c's initial_setup will complain if input_components is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
  cinfo->input_components = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
  /* Initialize master control (includes parameter checking/processing) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
  jinit_c_master_control(cinfo, TRUE /* transcode only */);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
  /* Entropy encoding: either Huffman or arithmetic coding. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
  if (cinfo->arith_code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    if (cinfo->progressive_mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
#ifdef C_PROGRESSIVE_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
      jinit_phuff_encoder(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
      ERREXIT(cinfo, JERR_NOT_COMPILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
      jinit_huff_encoder(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
  /* We need a special coefficient buffer controller. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
  transencode_coef_controller(cinfo, coef_arrays);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
  jinit_marker_writer(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
  /* We can now tell the memory manager to allocate virtual arrays. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
  /* Write the datastream header (SOI, JFIF) immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
   * Frame and scan headers are postponed till later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
   * This lets application insert special markers after the SOI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
  (*cinfo->marker->write_file_header) (cinfo);
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
 * The rest of this file is a special implementation of the coefficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * buffer controller.  This is similar to jccoefct.c, but it handles only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * output from presupplied virtual arrays.  Furthermore, we generate any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * dummy padding blocks on-the-fly rather than expecting them to be present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * in the arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
/* Private buffer controller object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
  struct jpeg_c_coef_controller pub; /* public fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
  JDIMENSION iMCU_row_num;      /* iMCU row # within image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
  JDIMENSION mcu_ctr;           /* counts MCUs processed in current row */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
  int MCU_vert_offset;          /* counts MCU rows within iMCU row */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
  int MCU_rows_per_iMCU_row;    /* number of such rows needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
  /* Virtual block array for each component. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
  jvirt_barray_ptr * whole_image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
  /* Workspace for constructing dummy blocks at right/bottom edges. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
  JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
} my_coef_controller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
typedef my_coef_controller * my_coef_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
start_iMCU_row (j_compress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
/* Reset within-iMCU-row counters for a new row */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
  /* In an interleaved scan, an MCU row is the same as an iMCU row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
   * But at the bottom of the image, process only what's left.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
  if (cinfo->comps_in_scan > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    coef->MCU_rows_per_iMCU_row = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
  coef->mcu_ctr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
  coef->MCU_vert_offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 * Initialize for a processing pass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
  if (pass_mode != JBUF_CRANK_DEST)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
  coef->iMCU_row_num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
  start_iMCU_row(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 * Process some data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 * per call, ie, v_samp_factor block rows for each component in the scan.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
 * The data is obtained from the virtual arrays and fed to the entropy coder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 * NB: input_buf is ignored; it is likely to be a NULL pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
METHODDEF(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
  int blkn, ci, xindex, yindex, yoffset, blockcnt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
  JDIMENSION start_col;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
  JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
  JBLOCKROW buffer_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
  jpeg_component_info *compptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
  /* Align the virtual buffers for the components used in this scan. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    compptr = cinfo->cur_comp_info[ci];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    buffer[ci] = (*cinfo->mem->access_virt_barray)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
      ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
       coef->iMCU_row_num * compptr->v_samp_factor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
       (JDIMENSION) compptr->v_samp_factor, FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
  /* Loop to process one whole iMCU row */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
       yoffset++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
         MCU_col_num++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
      /* Construct list of pointers to DCT blocks belonging to this MCU */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
      blkn = 0;                 /* index of current DCT block within MCU */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        compptr = cinfo->cur_comp_info[ci];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        start_col = MCU_col_num * compptr->MCU_width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                                : compptr->last_col_width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
          if (coef->iMCU_row_num < last_iMCU_row ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
              yindex+yoffset < compptr->last_row_height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            /* Fill in pointers to real blocks in this row */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            for (xindex = 0; xindex < blockcnt; xindex++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
              MCU_buffer[blkn++] = buffer_ptr++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
          } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            /* At bottom of image, need a whole row of dummy blocks */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            xindex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
          /* Fill in any dummy blocks needed in this row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
           * Dummy blocks are filled in the same way as in jccoefct.c:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
           * all zeroes in the AC entries, DC entries equal to previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
           * block's DC value.  The init routine has already zeroed the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
           * AC entries, so we need only set the DC entries correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
          for (; xindex < compptr->MCU_width; xindex++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            MCU_buffer[blkn] = coef->dummy_buffer[blkn];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            blkn++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
      /* Try to write the MCU. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
      if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        /* Suspension forced; update state counters and exit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        coef->MCU_vert_offset = yoffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        coef->mcu_ctr = MCU_col_num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /* Completed an MCU row, but perhaps not an iMCU row */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    coef->mcu_ctr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
  /* Completed the iMCU row, advance counters for next one */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
  coef->iMCU_row_num++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
  start_iMCU_row(cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
  return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
}
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
 * Initialize coefficient buffer controller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
 * Each passed coefficient array must be the right size for that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
 * coefficient: width_in_blocks wide and height_in_blocks high,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
 * with unitheight at least v_samp_factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
transencode_coef_controller (j_compress_ptr cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                             jvirt_barray_ptr * coef_arrays)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
  my_coef_ptr coef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
  JBLOCKROW buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
  int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
  coef = (my_coef_ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                                SIZEOF(my_coef_controller));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
  cinfo->coef = (struct jpeg_c_coef_controller *) coef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
  coef->pub.start_pass = start_pass_coef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
  coef->pub.compress_data = compress_output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
  /* Save pointer to virtual arrays */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
  coef->whole_image = coef_arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
  /* Allocate and pre-zero space for dummy DCT blocks. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
  buffer = (JBLOCKROW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                                C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
  jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
  for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    coef->dummy_buffer[i] = buffer + i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
}