Coverage Report

Created: 2025-06-24 07:01

/src/ghostpdl/obj/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-2022 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).
45
   * We append a workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks,
46
   * and reuse it 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
43.3k
{
77
43.3k
  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
43.3k
  if (cinfo->comps_in_scan > 1) {
84
21.4k
    coef->MCU_rows_per_iMCU_row = 1;
85
21.9k
  } else {
86
21.9k
    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
87
16.1k
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
88
5.75k
    else
89
5.75k
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
90
21.9k
  }
91
92
43.3k
  coef->MCU_ctr = 0;
93
43.3k
  coef->MCU_vert_offset = 0;
94
43.3k
}
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
6.12k
{
104
6.12k
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
105
106
6.12k
  coef->iMCU_row_num = 0;
107
6.12k
  start_iMCU_row(cinfo);
108
109
6.12k
  switch (pass_mode) {
110
6.12k
  case JBUF_PASS_THRU:
111
6.12k
    if (coef->whole_image[0] != NULL)
112
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
113
6.12k
    coef->pub.compress_data = compress_data;
114
6.12k
    break;
115
#ifdef FULL_COEF_BUFFER_SUPPORTED
116
  case JBUF_SAVE_AND_PASS:
117
    if (coef->whole_image[0] == NULL)
118
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
119
    coef->pub.compress_data = compress_first_pass;
120
    break;
121
  case JBUF_CRANK_DEST:
122
    if (coef->whole_image[0] == NULL)
123
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
124
    coef->pub.compress_data = compress_output;
125
    break;
126
#endif
127
0
  default:
128
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
129
6.12k
  }
130
6.12k
}
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
42.6k
{
146
42.6k
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
147
42.6k
  JDIMENSION MCU_col_num; /* index of current MCU within row */
148
42.6k
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
149
42.6k
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
150
42.6k
  int ci, xindex, yindex, yoffset, blockcnt;
151
42.6k
  JBLOCKROW blkp;
152
42.6k
  JSAMPARRAY input_ptr;
153
42.6k
  JDIMENSION xpos;
154
42.6k
  jpeg_component_info *compptr;
155
42.6k
  forward_DCT_ptr forward_DCT;
156
157
  /* Loop to write as much as one whole iMCU row */
158
98.2k
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
159
61.0k
       yoffset++) {
160
648k
    for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
161
592k
   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
592k
      blkp = coef->blk_buffer;  /* pointer to current DCT block within MCU */
171
1.45M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
172
864k
  compptr = cinfo->cur_comp_info[ci];
173
864k
  forward_DCT = cinfo->fdct->forward_DCT[compptr->component_index];
174
864k
  input_ptr = input_buf[compptr->component_index] +
175
864k
    yoffset * compptr->DCT_v_scaled_size;
176
  /* ypos == (yoffset + yindex) * compptr->DCT_v_scaled_size */
177
864k
  blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
178
864k
            : compptr->last_col_width;
179
864k
  xpos = MCU_col_num * compptr->MCU_sample_width;
180
1.86M
  for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
181
995k
    if (coef->iMCU_row_num < last_iMCU_row ||
182
995k
        yoffset + yindex < compptr->last_row_height) {
183
991k
      (*forward_DCT) (cinfo, compptr, input_ptr, blkp,
184
991k
          xpos, (JDIMENSION) blockcnt);
185
991k
      input_ptr += compptr->DCT_v_scaled_size;
186
991k
      blkp += blockcnt;
187
      /* Dummy blocks at right edge */
188
991k
      if ((xindex = compptr->MCU_width - blockcnt) == 0)
189
986k
        continue;
190
991k
    } else {
191
      /* At bottom of image, need a whole row of dummy blocks */
192
3.53k
      xindex = compptr->MCU_width;
193
3.53k
    }
194
    /* Fill in any dummy blocks needed in this row */
195
9.17k
    MEMZERO(blkp, xindex * SIZEOF(JBLOCK));
196
12.7k
    do {
197
12.7k
      blkp[0][0] = blkp[-1][0];
198
12.7k
      blkp++;
199
12.7k
    } while (--xindex);
200
9.17k
  }
201
864k
      }
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
592k
      if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
206
  /* Suspension forced; update state counters and exit */
207
5.47k
  coef->MCU_vert_offset = yoffset;
208
5.47k
  coef->MCU_ctr = MCU_col_num;
209
5.47k
  return FALSE;
210
5.47k
      }
211
592k
    }
212
    /* Completed an MCU row, but perhaps not an iMCU row */
213
55.6k
    coef->MCU_ctr = 0;
214
55.6k
  }
215
  /* Completed the iMCU row, advance counters for next one */
216
37.2k
  coef->iMCU_row_num++;
217
37.2k
  start_iMCU_row(cinfo);
218
37.2k
  return TRUE;
219
42.6k
}
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
{
248
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
249
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
250
  JDIMENSION blocks_across, MCUs_across, MCUindex;
251
  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
252
  JCOEF lastDC;
253
  jpeg_component_info *compptr;
254
  JBLOCKARRAY buffer;
255
  JBLOCKROW thisblockrow, lastblockrow;
256
  JSAMPARRAY input_ptr;
257
  forward_DCT_ptr forward_DCT;
258
259
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
260
       ci++, compptr++) {
261
    /* Align the virtual buffer for this component. */
262
    buffer = (*cinfo->mem->access_virt_barray)
263
      ((j_common_ptr) cinfo, coef->whole_image[ci],
264
       coef->iMCU_row_num * compptr->v_samp_factor,
265
       (JDIMENSION) compptr->v_samp_factor, TRUE);
266
    /* Count non-dummy DCT block rows in this iMCU row. */
267
    if (coef->iMCU_row_num < last_iMCU_row)
268
      block_rows = compptr->v_samp_factor;
269
    else {
270
      /* NB: can't use last_row_height here, since may not be set! */
271
      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
272
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
273
    }
274
    blocks_across = compptr->width_in_blocks;
275
    h_samp_factor = compptr->h_samp_factor;
276
    /* Count number of dummy blocks to be added at the right margin. */
277
    ndummy = (int) (blocks_across % h_samp_factor);
278
    if (ndummy > 0)
279
      ndummy = h_samp_factor - ndummy;
280
    forward_DCT = cinfo->fdct->forward_DCT[ci];
281
    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
    for (block_row = 0; block_row < block_rows; block_row++) {
286
      thisblockrow = buffer[block_row];
287
      (*forward_DCT) (cinfo, compptr, input_ptr, thisblockrow,
288
          (JDIMENSION) 0, blocks_across);
289
      input_ptr += compptr->DCT_v_scaled_size;
290
      if (ndummy > 0) {
291
  /* Create dummy blocks at the right edge of the image. */
292
  thisblockrow += blocks_across; /* => first dummy block */
293
  FMEMZERO((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
294
  lastDC = thisblockrow[-1][0];
295
  for (bi = 0; bi < ndummy; bi++) {
296
    thisblockrow[bi][0] = lastDC;
297
  }
298
      }
299
    }
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
    if (block_row < compptr->v_samp_factor) {
306
      blocks_across += ndummy;  /* include lower right corner */
307
      MCUs_across = blocks_across / h_samp_factor;
308
      do {
309
  thisblockrow = buffer[block_row];
310
  lastblockrow = buffer[block_row-1];
311
  FMEMZERO((void FAR *) thisblockrow,
312
     (size_t) blocks_across * SIZEOF(JBLOCK));
313
  for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
314
    lastDC = lastblockrow[h_samp_factor-1][0];
315
    for (bi = 0; bi < h_samp_factor; bi++) {
316
      thisblockrow[bi][0] = lastDC;
317
    }
318
    thisblockrow += h_samp_factor; /* advance to next MCU in row */
319
    lastblockrow += h_samp_factor;
320
  }
321
      } while (++block_row < compptr->v_samp_factor);
322
    }
323
  }
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
  return compress_output(cinfo, input_buf);
330
}
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
{
346
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
347
  JDIMENSION MCU_col_num; /* index of current MCU within row */
348
  int ci, xindex, yindex, yoffset;
349
  JDIMENSION start_col;
350
  JBLOCKARRAY blkp;
351
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
352
  JBLOCKROW buffer_ptr;
353
  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
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
360
    compptr = cinfo->cur_comp_info[ci];
361
    buffer[ci] = (*cinfo->mem->access_virt_barray)
362
      ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
363
       coef->iMCU_row_num * compptr->v_samp_factor,
364
       (JDIMENSION) compptr->v_samp_factor, FALSE);
365
  }
366
367
  /* Loop to process one whole iMCU row */
368
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
369
       yoffset++) {
370
    for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
371
   MCU_col_num++) {
372
      /* Construct list of pointers to DCT blocks belonging to this MCU */
373
      blkp = coef->MCU_buffer;  /* pointer to current DCT block within MCU */
374
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
375
  compptr = cinfo->cur_comp_info[ci];
376
  start_col = MCU_col_num * compptr->MCU_width;
377
  for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
378
    buffer_ptr = buffer[ci][yoffset + yindex] + start_col;
379
    xindex = compptr->MCU_width;
380
    do {
381
      *blkp++ = buffer_ptr++;
382
    } while (--xindex);
383
  }
384
      }
385
      /* Try to write the MCU. */
386
      if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
387
  /* Suspension forced; update state counters and exit */
388
  coef->MCU_vert_offset = yoffset;
389
  coef->MCU_ctr = MCU_col_num;
390
  return FALSE;
391
      }
392
    }
393
    /* Completed an MCU row, but perhaps not an iMCU row */
394
    coef->MCU_ctr = 0;
395
  }
396
  /* Completed the iMCU row, advance counters for next one */
397
  coef->iMCU_row_num++;
398
  start_iMCU_row(cinfo);
399
  return TRUE;
400
}
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
6.12k
{
412
6.12k
  my_coef_ptr coef;
413
414
6.12k
  if (need_full_buffer) {
415
#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
    int ci;
419
    jpeg_component_info *compptr;
420
421
    coef = (my_coef_ptr) (*cinfo->mem->alloc_small)
422
      ((j_common_ptr) cinfo, JPOOL_IMAGE,
423
       SIZEOF(my_coef_controller) - SIZEOF(coef->blk_buffer));
424
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
425
   ci++, compptr++) {
426
      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
427
  ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
428
   (JDIMENSION) jround_up((long) compptr->width_in_blocks,
429
        (long) compptr->h_samp_factor),
430
   (JDIMENSION) jround_up((long) compptr->height_in_blocks,
431
        (long) compptr->v_samp_factor),
432
   (JDIMENSION) compptr->v_samp_factor);
433
    }
434
#else
435
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
436
0
#endif
437
6.12k
  } else {
438
    /* We only need a single-MCU buffer. */
439
6.12k
    JBLOCKARRAY blkp;
440
6.12k
    JBLOCKROW buffer_ptr;
441
6.12k
    int bi;
442
443
6.12k
    coef = (my_coef_ptr) (*cinfo->mem->alloc_small)
444
6.12k
      ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_coef_controller));
445
6.12k
    blkp = coef->MCU_buffer;
446
6.12k
    buffer_ptr = coef->blk_buffer;
447
6.12k
    bi = C_MAX_BLOCKS_IN_MCU;
448
61.2k
    do {
449
61.2k
      *blkp++ = buffer_ptr++;
450
61.2k
    } while (--bi);
451
6.12k
    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
452
6.12k
  }
453
454
6.12k
  coef->pub.start_pass = start_pass_coef;
455
6.12k
  cinfo->coef = &coef->pub;
456
6.12k
}