Coverage Report

Created: 2025-10-10 07:05

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