Coverage Report

Created: 2018-09-25 14:53

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