Coverage Report

Created: 2023-06-07 06:20

/src/mupdf/thirdparty/libjpeg/jccoefct.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jccoefct.c
3
 *
4
 * Copyright (C) 1994-1997, Thomas G. Lane.
5
 * Modified 2003-2020 by Guido Vollbeding.
6
 * This file is part of the Independent JPEG Group's software.
7
 * For conditions of distribution and use, see the accompanying README file.
8
 *
9
 * This file contains the coefficient buffer controller for compression.
10
 * This controller is the top level of the JPEG compressor proper.
11
 * The coefficient buffer lies between forward-DCT and entropy encoding steps.
12
 */
13
14
#define JPEG_INTERNALS
15
#include "jinclude.h"
16
#include "jpeglib.h"
17
18
19
/* We use a full-image coefficient buffer when doing Huffman optimization,
20
 * and also for writing multiple-scan JPEG files.  In all cases, the DCT
21
 * step is run during the first pass, and subsequent passes need only read
22
 * the buffered coefficients.
23
 */
24
#ifdef ENTROPY_OPT_SUPPORTED
25
#define FULL_COEF_BUFFER_SUPPORTED
26
#else
27
#ifdef C_MULTISCAN_FILES_SUPPORTED
28
#define FULL_COEF_BUFFER_SUPPORTED
29
#endif
30
#endif
31
32
33
/* Private buffer controller object */
34
35
typedef struct {
36
  struct jpeg_c_coef_controller pub; /* public fields */
37
38
  JDIMENSION iMCU_row_num;  /* iMCU row # within image */
39
  JDIMENSION MCU_ctr;   /* counts MCUs processed in current row */
40
  int MCU_vert_offset;    /* counts MCU rows within iMCU row */
41
  int MCU_rows_per_iMCU_row;  /* number of such rows needed */
42
43
  /* For single-pass compression, it's sufficient to buffer just one MCU
44
   * (although this may prove a bit slow in practice).  We append a
45
   * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it
46
   * for each MCU constructed and sent.
47
   * In multi-pass modes, this array points to the current MCU's blocks
48
   * within the virtual arrays.
49
   */
50
  JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
51
52
  /* In multi-pass modes, we need a virtual block array for each component. */
53
  jvirt_barray_ptr whole_image[MAX_COMPONENTS];
54
55
  /* Workspace for single-pass compression (omitted otherwise). */
56
  JBLOCK blk_buffer[C_MAX_BLOCKS_IN_MCU];
57
} my_coef_controller;
58
59
typedef my_coef_controller * my_coef_ptr;
60
61
62
/* Forward declarations */
63
METHODDEF(boolean) compress_data
64
    JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
65
#ifdef FULL_COEF_BUFFER_SUPPORTED
66
METHODDEF(boolean) compress_first_pass
67
    JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
68
METHODDEF(boolean) compress_output
69
    JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
70
#endif
71
72
73
LOCAL(void)
74
start_iMCU_row (j_compress_ptr cinfo)
75
/* Reset within-iMCU-row counters for a new row */
76
0
{
77
0
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
78
79
  /* In an interleaved scan, an MCU row is the same as an iMCU row.
80
   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
81
   * But at the bottom of the image, process only what's left.
82
   */
83
0
  if (cinfo->comps_in_scan > 1) {
84
0
    coef->MCU_rows_per_iMCU_row = 1;
85
0
  } else {
86
0
    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
87
0
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
88
0
    else
89
0
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
90
0
  }
91
92
0
  coef->MCU_ctr = 0;
93
0
  coef->MCU_vert_offset = 0;
94
0
}
95
96
97
/*
98
 * Initialize for a processing pass.
99
 */
100
101
METHODDEF(void)
102
start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
103
0
{
104
0
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
105
106
0
  coef->iMCU_row_num = 0;
107
0
  start_iMCU_row(cinfo);
108
109
0
  switch (pass_mode) {
110
0
  case JBUF_PASS_THRU:
111
0
    if (coef->whole_image[0] != NULL)
112
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
113
0
    coef->pub.compress_data = compress_data;
114
0
    break;
115
0
#ifdef FULL_COEF_BUFFER_SUPPORTED
116
0
  case JBUF_SAVE_AND_PASS:
117
0
    if (coef->whole_image[0] == NULL)
118
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
119
0
    coef->pub.compress_data = compress_first_pass;
120
0
    break;
121
0
  case JBUF_CRANK_DEST:
122
0
    if (coef->whole_image[0] == NULL)
123
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
124
0
    coef->pub.compress_data = compress_output;
125
0
    break;
126
0
#endif
127
0
  default:
128
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
129
0
  }
130
0
}
131
132
133
/*
134
 * Process some data in the single-pass case.
135
 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
136
 * per call, ie, v_samp_factor block rows for each component in the image.
137
 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
138
 *
139
 * NB: input_buf contains a plane for each component in image,
140
 * which we index according to the component's SOF position.
141
 */
142
143
METHODDEF(boolean)
144
compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
145
0
{
146
0
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
147
0
  JDIMENSION MCU_col_num; /* index of current MCU within row */
148
0
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
149
0
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
150
0
  int ci, xindex, yindex, yoffset, blockcnt;
151
0
  JBLOCKROW blkp;
152
0
  JSAMPARRAY input_ptr;
153
0
  JDIMENSION xpos;
154
0
  jpeg_component_info *compptr;
155
0
  forward_DCT_ptr forward_DCT;
156
157
  /* Loop to write as much as one whole iMCU row */
158
0
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
159
0
       yoffset++) {
160
0
    for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
161
0
   MCU_col_num++) {
162
      /* Determine where data comes from in input_buf and do the DCT thing.
163
       * Each call on forward_DCT processes a horizontal row of DCT blocks as
164
       * wide as an MCU.  Dummy blocks at the right or bottom edge are filled in
165
       * specially.  The data in them does not matter for image reconstruction,
166
       * so we fill them with values that will encode to the smallest amount of
167
       * data, viz: all zeroes in the AC entries, DC entries equal to previous
168
       * block's DC value.  (Thanks to Thomas Kinsman for this idea.)
169
       */
170
0
      blkp = coef->blk_buffer;  /* pointer to current DCT block within MCU */
171
0
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
172
0
  compptr = cinfo->cur_comp_info[ci];
173
0
  forward_DCT = cinfo->fdct->forward_DCT[compptr->component_index];
174
0
  input_ptr = input_buf[compptr->component_index] +
175
0
    yoffset * compptr->DCT_v_scaled_size;
176
  /* ypos == (yoffset + yindex) * compptr->DCT_v_scaled_size */
177
0
  blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
178
0
            : compptr->last_col_width;
179
0
  xpos = MCU_col_num * compptr->MCU_sample_width;
180
0
  for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
181
0
    if (coef->iMCU_row_num < last_iMCU_row ||
182
0
        yoffset + yindex < compptr->last_row_height) {
183
0
      (*forward_DCT) (cinfo, compptr, input_ptr, blkp,
184
0
          xpos, (JDIMENSION) blockcnt);
185
0
      input_ptr += compptr->DCT_v_scaled_size;
186
0
      blkp += blockcnt;
187
      /* Dummy blocks at right edge */
188
0
      if ((xindex = compptr->MCU_width - blockcnt) == 0)
189
0
        continue;
190
0
    } else {
191
      /* At bottom of image, need a whole row of dummy blocks */
192
0
      xindex = compptr->MCU_width;
193
0
    }
194
    /* Fill in any dummy blocks needed in this row */
195
0
    MEMZERO(blkp, xindex * SIZEOF(JBLOCK));
196
0
    do {
197
0
      blkp[0][0] = blkp[-1][0];
198
0
      blkp++;
199
0
    } while (--xindex);
200
0
  }
201
0
      }
202
      /* Try to write the MCU.  In event of a suspension failure, we will
203
       * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
204
       */
205
0
      if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
206
  /* Suspension forced; update state counters and exit */
207
0
  coef->MCU_vert_offset = yoffset;
208
0
  coef->MCU_ctr = MCU_col_num;
209
0
  return FALSE;
210
0
      }
211
0
    }
212
    /* Completed an MCU row, but perhaps not an iMCU row */
213
0
    coef->MCU_ctr = 0;
214
0
  }
215
  /* Completed the iMCU row, advance counters for next one */
216
0
  coef->iMCU_row_num++;
217
0
  start_iMCU_row(cinfo);
218
0
  return TRUE;
219
0
}
220
221
222
#ifdef FULL_COEF_BUFFER_SUPPORTED
223
224
/*
225
 * Process some data in the first pass of a multi-pass case.
226
 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
227
 * per call, ie, v_samp_factor block rows for each component in the image.
228
 * This amount of data is read from the source buffer, DCT'd and quantized,
229
 * and saved into the virtual arrays.  We also generate suitable dummy blocks
230
 * as needed at the right and lower edges.  (The dummy blocks are constructed
231
 * in the virtual arrays, which have been padded appropriately.)  This makes
232
 * it possible for subsequent passes not to worry about real vs. dummy blocks.
233
 *
234
 * We must also emit the data to the entropy encoder.  This is conveniently
235
 * done by calling compress_output() after we've loaded the current strip
236
 * of the virtual arrays.
237
 *
238
 * NB: input_buf contains a plane for each component in image.  All
239
 * components are DCT'd and loaded into the virtual arrays in this pass.
240
 * However, it may be that only a subset of the components are emitted to
241
 * the entropy encoder during this first pass; be careful about looking
242
 * at the scan-dependent variables (MCU dimensions, etc).
243
 */
244
245
METHODDEF(boolean)
246
compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
247
0
{
248
0
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
249
0
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
250
0
  JDIMENSION blocks_across, MCUs_across, MCUindex;
251
0
  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
252
0
  JCOEF lastDC;
253
0
  jpeg_component_info *compptr;
254
0
  JBLOCKARRAY buffer;
255
0
  JBLOCKROW thisblockrow, lastblockrow;
256
0
  JSAMPARRAY input_ptr;
257
0
  forward_DCT_ptr forward_DCT;
258
259
0
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
260
0
       ci++, compptr++) {
261
    /* Align the virtual buffer for this component. */
262
0
    buffer = (*cinfo->mem->access_virt_barray)
263
0
      ((j_common_ptr) cinfo, coef->whole_image[ci],
264
0
       coef->iMCU_row_num * compptr->v_samp_factor,
265
0
       (JDIMENSION) compptr->v_samp_factor, TRUE);
266
    /* Count non-dummy DCT block rows in this iMCU row. */
267
0
    if (coef->iMCU_row_num < last_iMCU_row)
268
0
      block_rows = compptr->v_samp_factor;
269
0
    else {
270
      /* NB: can't use last_row_height here, since may not be set! */
271
0
      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
272
0
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
273
0
    }
274
0
    blocks_across = compptr->width_in_blocks;
275
0
    h_samp_factor = compptr->h_samp_factor;
276
    /* Count number of dummy blocks to be added at the right margin. */
277
0
    ndummy = (int) (blocks_across % h_samp_factor);
278
0
    if (ndummy > 0)
279
0
      ndummy = h_samp_factor - ndummy;
280
0
    forward_DCT = cinfo->fdct->forward_DCT[ci];
281
0
    input_ptr = input_buf[ci];
282
    /* Perform DCT for all non-dummy blocks in this iMCU row.  Each call
283
     * on forward_DCT processes a complete horizontal row of DCT blocks.
284
     */
285
0
    for (block_row = 0; block_row < block_rows; block_row++) {
286
0
      thisblockrow = buffer[block_row];
287
0
      (*forward_DCT) (cinfo, compptr, input_ptr, thisblockrow,
288
0
          (JDIMENSION) 0, blocks_across);
289
0
      input_ptr += compptr->DCT_v_scaled_size;
290
0
      if (ndummy > 0) {
291
  /* Create dummy blocks at the right edge of the image. */
292
0
  thisblockrow += blocks_across; /* => first dummy block */
293
0
  FMEMZERO((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
294
0
  lastDC = thisblockrow[-1][0];
295
0
  for (bi = 0; bi < ndummy; bi++) {
296
0
    thisblockrow[bi][0] = lastDC;
297
0
  }
298
0
      }
299
0
    }
300
    /* If at end of image, create dummy block rows as needed.
301
     * The tricky part here is that within each MCU, we want the DC values
302
     * of the dummy blocks to match the last real block's DC value.
303
     * This squeezes a few more bytes out of the resulting file...
304
     */
305
0
    if (block_row < compptr->v_samp_factor) {
306
0
      blocks_across += ndummy;  /* include lower right corner */
307
0
      MCUs_across = blocks_across / h_samp_factor;
308
0
      do {
309
0
  thisblockrow = buffer[block_row];
310
0
  lastblockrow = buffer[block_row-1];
311
0
  FMEMZERO((void FAR *) thisblockrow,
312
0
     (size_t) blocks_across * SIZEOF(JBLOCK));
313
0
  for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
314
0
    lastDC = lastblockrow[h_samp_factor-1][0];
315
0
    for (bi = 0; bi < h_samp_factor; bi++) {
316
0
      thisblockrow[bi][0] = lastDC;
317
0
    }
318
0
    thisblockrow += h_samp_factor; /* advance to next MCU in row */
319
0
    lastblockrow += h_samp_factor;
320
0
  }
321
0
      } while (++block_row < compptr->v_samp_factor);
322
0
    }
323
0
  }
324
  /* NB: compress_output will increment iMCU_row_num if successful.
325
   * A suspension return will result in redoing all the work above next time.
326
   */
327
328
  /* Emit data to the entropy encoder, sharing code with subsequent passes */
329
0
  return compress_output(cinfo, input_buf);
330
0
}
331
332
333
/*
334
 * Process some data in subsequent passes of a multi-pass case.
335
 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
336
 * per call, ie, v_samp_factor block rows for each component in the scan.
337
 * The data is obtained from the virtual arrays and fed to the entropy coder.
338
 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
339
 *
340
 * NB: input_buf is ignored; it is likely to be a NULL pointer.
341
 */
342
343
METHODDEF(boolean)
344
compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
345
0
{
346
0
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
347
0
  JDIMENSION MCU_col_num; /* index of current MCU within row */
348
0
  int ci, xindex, yindex, yoffset;
349
0
  JDIMENSION start_col;
350
0
  JBLOCKARRAY blkp;
351
0
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
352
0
  JBLOCKROW buffer_ptr;
353
0
  jpeg_component_info *compptr;
354
355
  /* Align the virtual buffers for the components used in this scan.
356
   * NB: during first pass, this is safe only because the buffers will
357
   * already be aligned properly, so jmemmgr.c won't need to do any I/O.
358
   */
359
0
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
360
0
    compptr = cinfo->cur_comp_info[ci];
361
0
    buffer[ci] = (*cinfo->mem->access_virt_barray)
362
0
      ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
363
0
       coef->iMCU_row_num * compptr->v_samp_factor,
364
0
       (JDIMENSION) compptr->v_samp_factor, FALSE);
365
0
  }
366
367
  /* Loop to process one whole iMCU row */
368
0
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
369
0
       yoffset++) {
370
0
    for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
371
0
   MCU_col_num++) {
372
      /* Construct list of pointers to DCT blocks belonging to this MCU */
373
0
      blkp = coef->MCU_buffer;  /* pointer to current DCT block within MCU */
374
0
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
375
0
  compptr = cinfo->cur_comp_info[ci];
376
0
  start_col = MCU_col_num * compptr->MCU_width;
377
0
  for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
378
0
    buffer_ptr = buffer[ci][yoffset + yindex] + start_col;
379
0
    xindex = compptr->MCU_width;
380
0
    do {
381
0
      *blkp++ = buffer_ptr++;
382
0
    } while (--xindex);
383
0
  }
384
0
      }
385
      /* Try to write the MCU. */
386
0
      if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
387
  /* Suspension forced; update state counters and exit */
388
0
  coef->MCU_vert_offset = yoffset;
389
0
  coef->MCU_ctr = MCU_col_num;
390
0
  return FALSE;
391
0
      }
392
0
    }
393
    /* Completed an MCU row, but perhaps not an iMCU row */
394
0
    coef->MCU_ctr = 0;
395
0
  }
396
  /* Completed the iMCU row, advance counters for next one */
397
0
  coef->iMCU_row_num++;
398
0
  start_iMCU_row(cinfo);
399
0
  return TRUE;
400
0
}
401
402
#endif /* FULL_COEF_BUFFER_SUPPORTED */
403
404
405
/*
406
 * Initialize coefficient buffer controller.
407
 */
408
409
GLOBAL(void)
410
jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
411
0
{
412
0
  my_coef_ptr coef;
413
414
0
  if (need_full_buffer) {
415
0
#ifdef FULL_COEF_BUFFER_SUPPORTED
416
    /* Allocate a full-image virtual array for each component, */
417
    /* padded to a multiple of samp_factor DCT blocks in each direction. */
418
0
    int ci;
419
0
    jpeg_component_info *compptr;
420
421
0
    coef = (my_coef_ptr) (*cinfo->mem->alloc_small)
422
0
      ((j_common_ptr) cinfo, JPOOL_IMAGE,
423
0
       SIZEOF(my_coef_controller) - SIZEOF(coef->blk_buffer));
424
0
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
425
0
   ci++, compptr++) {
426
0
      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
427
0
  ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
428
0
   (JDIMENSION) jround_up((long) compptr->width_in_blocks,
429
0
        (long) compptr->h_samp_factor),
430
0
   (JDIMENSION) jround_up((long) compptr->height_in_blocks,
431
0
        (long) compptr->v_samp_factor),
432
0
   (JDIMENSION) compptr->v_samp_factor);
433
0
    }
434
#else
435
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
436
#endif
437
0
  } else {
438
    /* We only need a single-MCU buffer. */
439
0
    JBLOCKARRAY blkp;
440
0
    JBLOCKROW buffer_ptr;
441
0
    int bi;
442
443
0
    coef = (my_coef_ptr) (*cinfo->mem->alloc_small)
444
0
      ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_coef_controller));
445
0
    blkp = coef->MCU_buffer;
446
0
    buffer_ptr = coef->blk_buffer;
447
0
    bi = C_MAX_BLOCKS_IN_MCU;
448
0
    do {
449
0
      *blkp++ = buffer_ptr++;
450
0
    } while (--bi);
451
0
    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
452
0
  }
453
454
0
  coef->pub.start_pass = start_pass_coef;
455
0
  cinfo->coef = &coef->pub;
456
0
}