Coverage Report

Created: 2026-10-28 06:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.main/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
113M
{
78
113M
  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
113M
  if (cinfo->comps_in_scan > 1) {
85
27.8M
    coef->MCU_rows_per_iMCU_row = 1;
86
86.0M
  } else {
87
86.0M
    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
88
85.2M
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
89
783k
    else
90
783k
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
91
86.0M
  }
92
93
113M
  coef->mcu_ctr = 0;
94
113M
  coef->MCU_vert_offset = 0;
95
113M
}
jccoefct-8.c:start_iMCU_row
Line
Count
Source
77
75.7M
{
78
75.7M
  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
75.7M
  if (cinfo->comps_in_scan > 1) {
85
17.3M
    coef->MCU_rows_per_iMCU_row = 1;
86
58.3M
  } else {
87
58.3M
    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
88
57.8M
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
89
481k
    else
90
481k
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
91
58.3M
  }
92
93
75.7M
  coef->mcu_ctr = 0;
94
75.7M
  coef->MCU_vert_offset = 0;
95
75.7M
}
jccoefct-12.c:start_iMCU_row
Line
Count
Source
77
38.2M
{
78
38.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
38.2M
  if (cinfo->comps_in_scan > 1) {
85
10.5M
    coef->MCU_rows_per_iMCU_row = 1;
86
27.6M
  } else {
87
27.6M
    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
88
27.3M
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
89
302k
    else
90
302k
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
91
27.6M
  }
92
93
38.2M
  coef->mcu_ctr = 0;
94
38.2M
  coef->MCU_vert_offset = 0;
95
38.2M
}
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
520k
{
105
520k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
106
107
520k
  coef->iMCU_row_num = 0;
108
520k
  start_iMCU_row(cinfo);
109
110
520k
  switch (pass_mode) {
111
47.5k
  case JBUF_PASS_THRU:
112
47.5k
    if (coef->whole_image[0] != NULL)
113
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
114
47.5k
    coef->pub._compress_data = compress_data;
115
47.5k
    break;
116
0
#ifdef FULL_COEF_BUFFER_SUPPORTED
117
59.6k
  case JBUF_SAVE_AND_PASS:
118
59.6k
    if (coef->whole_image[0] == NULL)
119
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
120
59.6k
    coef->pub._compress_data = compress_first_pass;
121
59.6k
    break;
122
413k
  case JBUF_CRANK_DEST:
123
413k
    if (coef->whole_image[0] == NULL)
124
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
125
413k
    coef->pub._compress_data = compress_output;
126
413k
    break;
127
0
#endif
128
0
  default:
129
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
130
0
    break;
131
520k
  }
132
520k
}
jccoefct-8.c:start_pass_coef
Line
Count
Source
104
324k
{
105
324k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
106
107
324k
  coef->iMCU_row_num = 0;
108
324k
  start_iMCU_row(cinfo);
109
110
324k
  switch (pass_mode) {
111
42.5k
  case JBUF_PASS_THRU:
112
42.5k
    if (coef->whole_image[0] != NULL)
113
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
114
42.5k
    coef->pub._compress_data = compress_data;
115
42.5k
    break;
116
0
#ifdef FULL_COEF_BUFFER_SUPPORTED
117
28.8k
  case JBUF_SAVE_AND_PASS:
118
28.8k
    if (coef->whole_image[0] == NULL)
119
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
120
28.8k
    coef->pub._compress_data = compress_first_pass;
121
28.8k
    break;
122
252k
  case JBUF_CRANK_DEST:
123
252k
    if (coef->whole_image[0] == NULL)
124
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
125
252k
    coef->pub._compress_data = compress_output;
126
252k
    break;
127
0
#endif
128
0
  default:
129
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
130
0
    break;
131
324k
  }
132
324k
}
jccoefct-12.c:start_pass_coef
Line
Count
Source
104
196k
{
105
196k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
106
107
196k
  coef->iMCU_row_num = 0;
108
196k
  start_iMCU_row(cinfo);
109
110
196k
  switch (pass_mode) {
111
4.99k
  case JBUF_PASS_THRU:
112
4.99k
    if (coef->whole_image[0] != NULL)
113
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
114
4.99k
    coef->pub._compress_data = compress_data;
115
4.99k
    break;
116
0
#ifdef FULL_COEF_BUFFER_SUPPORTED
117
30.7k
  case JBUF_SAVE_AND_PASS:
118
30.7k
    if (coef->whole_image[0] == NULL)
119
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
120
30.7k
    coef->pub._compress_data = compress_first_pass;
121
30.7k
    break;
122
160k
  case JBUF_CRANK_DEST:
123
160k
    if (coef->whole_image[0] == NULL)
124
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
125
160k
    coef->pub._compress_data = compress_output;
126
160k
    break;
127
0
#endif
128
0
  default:
129
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
130
0
    break;
131
196k
  }
132
196k
}
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
7.85M
{
148
7.85M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
149
7.85M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
150
7.85M
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
151
7.85M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
152
7.85M
  int blkn, bi, ci, yindex, yoffset, blockcnt;
153
7.85M
  JDIMENSION ypos, xpos;
154
7.85M
  jpeg_component_info *compptr;
155
156
  /* Loop to write as much as one whole iMCU row */
157
15.7M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
158
7.85M
       yoffset++) {
159
27.0M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
160
19.1M
         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
19.1M
      blkn = 0;
171
59.5M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
172
40.3M
        compptr = cinfo->cur_comp_info[ci];
173
40.3M
        blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width :
174
40.3M
                                                  compptr->last_col_width;
175
40.3M
        xpos = MCU_col_num * compptr->MCU_sample_width;
176
40.3M
        ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
177
85.8M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
178
45.4M
          if (coef->iMCU_row_num < last_iMCU_row ||
179
43.9M
              yoffset + yindex < compptr->last_row_height) {
180
43.9M
            (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
181
43.9M
                                          input_buf[compptr->component_index],
182
43.9M
                                          coef->MCU_buffer[blkn],
183
43.9M
                                          ypos, xpos, (JDIMENSION)blockcnt);
184
43.9M
            if (blockcnt < compptr->MCU_width) {
185
              /* Create some dummy blocks at the right edge of the image. */
186
4.70M
              jzero_far((void *)coef->MCU_buffer[blkn + blockcnt],
187
4.70M
                        (compptr->MCU_width - blockcnt) * sizeof(JBLOCK));
188
9.82M
              for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
189
5.12M
                coef->MCU_buffer[blkn + bi][0][0] =
190
5.12M
                  coef->MCU_buffer[blkn + bi - 1][0][0];
191
5.12M
              }
192
4.70M
            }
193
43.9M
          } else {
194
            /* Create a row of dummy blocks at the bottom of the image. */
195
1.55M
            jzero_far((void *)coef->MCU_buffer[blkn],
196
1.55M
                      compptr->MCU_width * sizeof(JBLOCK));
197
4.59M
            for (bi = 0; bi < compptr->MCU_width; bi++) {
198
3.04M
              coef->MCU_buffer[blkn + bi][0][0] =
199
3.04M
                coef->MCU_buffer[blkn - 1][0][0];
200
3.04M
            }
201
1.55M
          }
202
45.4M
          blkn += compptr->MCU_width;
203
45.4M
          ypos += DCTSIZE;
204
45.4M
        }
205
40.3M
      }
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
19.1M
      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
19.1M
    }
229
    /* Completed an MCU row, but perhaps not an iMCU row */
230
7.85M
    coef->mcu_ctr = 0;
231
7.85M
  }
232
  /* Completed the iMCU row, advance counters for next one */
233
7.85M
  coef->iMCU_row_num++;
234
7.85M
  start_iMCU_row(cinfo);
235
7.85M
  return TRUE;
236
7.85M
}
jccoefct-8.c:compress_data
Line
Count
Source
147
7.15M
{
148
7.15M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
149
7.15M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
150
7.15M
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
151
7.15M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
152
7.15M
  int blkn, bi, ci, yindex, yoffset, blockcnt;
153
7.15M
  JDIMENSION ypos, xpos;
154
7.15M
  jpeg_component_info *compptr;
155
156
  /* Loop to write as much as one whole iMCU row */
157
14.3M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
158
7.15M
       yoffset++) {
159
24.9M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
160
17.7M
         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
17.7M
      blkn = 0;
171
53.9M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
172
36.2M
        compptr = cinfo->cur_comp_info[ci];
173
36.2M
        blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width :
174
36.2M
                                                  compptr->last_col_width;
175
36.2M
        xpos = MCU_col_num * compptr->MCU_sample_width;
176
36.2M
        ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
177
76.1M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
178
39.9M
          if (coef->iMCU_row_num < last_iMCU_row ||
179
38.8M
              yoffset + yindex < compptr->last_row_height) {
180
38.8M
            (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
181
38.8M
                                          input_buf[compptr->component_index],
182
38.8M
                                          coef->MCU_buffer[blkn],
183
38.8M
                                          ypos, xpos, (JDIMENSION)blockcnt);
184
38.8M
            if (blockcnt < compptr->MCU_width) {
185
              /* Create some dummy blocks at the right edge of the image. */
186
3.31M
              jzero_far((void *)coef->MCU_buffer[blkn + blockcnt],
187
3.31M
                        (compptr->MCU_width - blockcnt) * sizeof(JBLOCK));
188
7.05M
              for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
189
3.73M
                coef->MCU_buffer[blkn + bi][0][0] =
190
3.73M
                  coef->MCU_buffer[blkn + bi - 1][0][0];
191
3.73M
              }
192
3.31M
            }
193
38.8M
          } else {
194
            /* Create a row of dummy blocks at the bottom of the image. */
195
1.04M
            jzero_far((void *)coef->MCU_buffer[blkn],
196
1.04M
                      compptr->MCU_width * sizeof(JBLOCK));
197
3.06M
            for (bi = 0; bi < compptr->MCU_width; bi++) {
198
2.02M
              coef->MCU_buffer[blkn + bi][0][0] =
199
2.02M
                coef->MCU_buffer[blkn - 1][0][0];
200
2.02M
            }
201
1.04M
          }
202
39.9M
          blkn += compptr->MCU_width;
203
39.9M
          ypos += DCTSIZE;
204
39.9M
        }
205
36.2M
      }
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
17.7M
      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
17.7M
    }
229
    /* Completed an MCU row, but perhaps not an iMCU row */
230
7.15M
    coef->mcu_ctr = 0;
231
7.15M
  }
232
  /* Completed the iMCU row, advance counters for next one */
233
7.15M
  coef->iMCU_row_num++;
234
7.15M
  start_iMCU_row(cinfo);
235
7.15M
  return TRUE;
236
7.15M
}
jccoefct-12.c:compress_data
Line
Count
Source
147
701k
{
148
701k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
149
701k
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
150
701k
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
151
701k
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
152
701k
  int blkn, bi, ci, yindex, yoffset, blockcnt;
153
701k
  JDIMENSION ypos, xpos;
154
701k
  jpeg_component_info *compptr;
155
156
  /* Loop to write as much as one whole iMCU row */
157
1.40M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
158
701k
       yoffset++) {
159
2.08M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
160
1.38M
         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
1.38M
      blkn = 0;
171
5.54M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
172
4.15M
        compptr = cinfo->cur_comp_info[ci];
173
4.15M
        blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width :
174
4.15M
                                                  compptr->last_col_width;
175
4.15M
        xpos = MCU_col_num * compptr->MCU_sample_width;
176
4.15M
        ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
177
9.69M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
178
5.54M
          if (coef->iMCU_row_num < last_iMCU_row ||
179
5.03M
              yoffset + yindex < compptr->last_row_height) {
180
5.03M
            (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
181
5.03M
                                          input_buf[compptr->component_index],
182
5.03M
                                          coef->MCU_buffer[blkn],
183
5.03M
                                          ypos, xpos, (JDIMENSION)blockcnt);
184
5.03M
            if (blockcnt < compptr->MCU_width) {
185
              /* Create some dummy blocks at the right edge of the image. */
186
1.38M
              jzero_far((void *)coef->MCU_buffer[blkn + blockcnt],
187
1.38M
                        (compptr->MCU_width - blockcnt) * sizeof(JBLOCK));
188
2.77M
              for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
189
1.38M
                coef->MCU_buffer[blkn + bi][0][0] =
190
1.38M
                  coef->MCU_buffer[blkn + bi - 1][0][0];
191
1.38M
              }
192
1.38M
            }
193
5.03M
          } else {
194
            /* Create a row of dummy blocks at the bottom of the image. */
195
510k
            jzero_far((void *)coef->MCU_buffer[blkn],
196
510k
                      compptr->MCU_width * sizeof(JBLOCK));
197
1.53M
            for (bi = 0; bi < compptr->MCU_width; bi++) {
198
1.02M
              coef->MCU_buffer[blkn + bi][0][0] =
199
1.02M
                coef->MCU_buffer[blkn - 1][0][0];
200
1.02M
            }
201
510k
          }
202
5.54M
          blkn += compptr->MCU_width;
203
5.54M
          ypos += DCTSIZE;
204
5.54M
        }
205
4.15M
      }
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
1.38M
      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
1.38M
    }
229
    /* Completed an MCU row, but perhaps not an iMCU row */
230
701k
    coef->mcu_ctr = 0;
231
701k
  }
232
  /* Completed the iMCU row, advance counters for next one */
233
701k
  coef->iMCU_row_num++;
234
701k
  start_iMCU_row(cinfo);
235
701k
  return TRUE;
236
701k
}
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
14.1M
{
265
14.1M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
266
14.1M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
267
14.1M
  JDIMENSION blocks_across, MCUs_across, MCUindex;
268
14.1M
  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
269
14.1M
  JCOEF lastDC;
270
14.1M
  jpeg_component_info *compptr;
271
14.1M
  JBLOCKARRAY buffer;
272
14.1M
  JBLOCKROW thisblockrow, lastblockrow;
273
274
50.1M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
275
35.9M
       ci++, compptr++) {
276
    /* Align the virtual buffer for this component. */
277
35.9M
    buffer = (*cinfo->mem->access_virt_barray)
278
35.9M
      ((j_common_ptr)cinfo, coef->whole_image[ci],
279
35.9M
       coef->iMCU_row_num * compptr->v_samp_factor,
280
35.9M
       (JDIMENSION)compptr->v_samp_factor, TRUE);
281
    /* Count non-dummy DCT block rows in this iMCU row. */
282
35.9M
    if (coef->iMCU_row_num < last_iMCU_row)
283
35.8M
      block_rows = compptr->v_samp_factor;
284
159k
    else {
285
      /* NB: can't use last_row_height here, since may not be set! */
286
159k
      block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
287
159k
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
288
159k
    }
289
35.9M
    blocks_across = compptr->width_in_blocks;
290
35.9M
    h_samp_factor = compptr->h_samp_factor;
291
    /* Count number of dummy blocks to be added at the right margin. */
292
35.9M
    ndummy = (int)(blocks_across % h_samp_factor);
293
35.9M
    if (ndummy > 0)
294
7.43M
      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
74.5M
    for (block_row = 0; block_row < block_rows; block_row++) {
299
38.5M
      thisblockrow = buffer[block_row];
300
38.5M
      (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
301
38.5M
                                    input_buf[ci], thisblockrow,
302
38.5M
                                    (JDIMENSION)(block_row * DCTSIZE),
303
38.5M
                                    (JDIMENSION)0, blocks_across);
304
38.5M
      if (ndummy > 0) {
305
        /* Create dummy blocks at the right edge of the image. */
306
7.43M
        thisblockrow += blocks_across; /* => first dummy block */
307
7.43M
        jzero_far((void *)thisblockrow, ndummy * sizeof(JBLOCK));
308
7.43M
        lastDC = thisblockrow[-1][0];
309
22.2M
        for (bi = 0; bi < ndummy; bi++) {
310
14.8M
          thisblockrow[bi][0] = lastDC;
311
14.8M
        }
312
7.43M
      }
313
38.5M
    }
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
35.9M
    if (coef->iMCU_row_num == last_iMCU_row) {
320
159k
      blocks_across += ndummy;  /* include lower right corner */
321
159k
      MCUs_across = blocks_across / h_samp_factor;
322
176k
      for (block_row = block_rows; block_row < compptr->v_samp_factor;
323
159k
           block_row++) {
324
16.7k
        thisblockrow = buffer[block_row];
325
16.7k
        lastblockrow = buffer[block_row - 1];
326
16.7k
        jzero_far((void *)thisblockrow,
327
16.7k
                  (size_t)(blocks_across * sizeof(JBLOCK)));
328
4.24M
        for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
329
4.23M
          lastDC = lastblockrow[h_samp_factor - 1][0];
330
8.46M
          for (bi = 0; bi < h_samp_factor; bi++) {
331
4.23M
            thisblockrow[bi][0] = lastDC;
332
4.23M
          }
333
4.23M
          thisblockrow += h_samp_factor; /* advance to next MCU in row */
334
4.23M
          lastblockrow += h_samp_factor;
335
4.23M
        }
336
16.7k
      }
337
159k
    }
338
35.9M
  }
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
14.1M
  return compress_output(cinfo, input_buf);
345
14.1M
}
jccoefct-8.c:compress_first_pass
Line
Count
Source
264
6.60M
{
265
6.60M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
266
6.60M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
267
6.60M
  JDIMENSION blocks_across, MCUs_across, MCUindex;
268
6.60M
  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
269
6.60M
  JCOEF lastDC;
270
6.60M
  jpeg_component_info *compptr;
271
6.60M
  JBLOCKARRAY buffer;
272
6.60M
  JBLOCKROW thisblockrow, lastblockrow;
273
274
24.5M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
275
17.9M
       ci++, compptr++) {
276
    /* Align the virtual buffer for this component. */
277
17.9M
    buffer = (*cinfo->mem->access_virt_barray)
278
17.9M
      ((j_common_ptr)cinfo, coef->whole_image[ci],
279
17.9M
       coef->iMCU_row_num * compptr->v_samp_factor,
280
17.9M
       (JDIMENSION)compptr->v_samp_factor, TRUE);
281
    /* Count non-dummy DCT block rows in this iMCU row. */
282
17.9M
    if (coef->iMCU_row_num < last_iMCU_row)
283
17.8M
      block_rows = compptr->v_samp_factor;
284
83.2k
    else {
285
      /* NB: can't use last_row_height here, since may not be set! */
286
83.2k
      block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
287
83.2k
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
288
83.2k
    }
289
17.9M
    blocks_across = compptr->width_in_blocks;
290
17.9M
    h_samp_factor = compptr->h_samp_factor;
291
    /* Count number of dummy blocks to be added at the right margin. */
292
17.9M
    ndummy = (int)(blocks_across % h_samp_factor);
293
17.9M
    if (ndummy > 0)
294
4.65M
      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
37.1M
    for (block_row = 0; block_row < block_rows; block_row++) {
299
19.1M
      thisblockrow = buffer[block_row];
300
19.1M
      (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
301
19.1M
                                    input_buf[ci], thisblockrow,
302
19.1M
                                    (JDIMENSION)(block_row * DCTSIZE),
303
19.1M
                                    (JDIMENSION)0, blocks_across);
304
19.1M
      if (ndummy > 0) {
305
        /* Create dummy blocks at the right edge of the image. */
306
4.65M
        thisblockrow += blocks_across; /* => first dummy block */
307
4.65M
        jzero_far((void *)thisblockrow, ndummy * sizeof(JBLOCK));
308
4.65M
        lastDC = thisblockrow[-1][0];
309
13.9M
        for (bi = 0; bi < ndummy; bi++) {
310
9.29M
          thisblockrow[bi][0] = lastDC;
311
9.29M
        }
312
4.65M
      }
313
19.1M
    }
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
17.9M
    if (coef->iMCU_row_num == last_iMCU_row) {
320
83.2k
      blocks_across += ndummy;  /* include lower right corner */
321
83.2k
      MCUs_across = blocks_across / h_samp_factor;
322
91.9k
      for (block_row = block_rows; block_row < compptr->v_samp_factor;
323
83.2k
           block_row++) {
324
8.71k
        thisblockrow = buffer[block_row];
325
8.71k
        lastblockrow = buffer[block_row - 1];
326
8.71k
        jzero_far((void *)thisblockrow,
327
8.71k
                  (size_t)(blocks_across * sizeof(JBLOCK)));
328
2.20M
        for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
329
2.19M
          lastDC = lastblockrow[h_samp_factor - 1][0];
330
4.39M
          for (bi = 0; bi < h_samp_factor; bi++) {
331
2.19M
            thisblockrow[bi][0] = lastDC;
332
2.19M
          }
333
2.19M
          thisblockrow += h_samp_factor; /* advance to next MCU in row */
334
2.19M
          lastblockrow += h_samp_factor;
335
2.19M
        }
336
8.71k
      }
337
83.2k
    }
338
17.9M
  }
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
6.60M
  return compress_output(cinfo, input_buf);
345
6.60M
}
jccoefct-12.c:compress_first_pass
Line
Count
Source
264
7.52M
{
265
7.52M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
266
7.52M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
267
7.52M
  JDIMENSION blocks_across, MCUs_across, MCUindex;
268
7.52M
  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
269
7.52M
  JCOEF lastDC;
270
7.52M
  jpeg_component_info *compptr;
271
7.52M
  JBLOCKARRAY buffer;
272
7.52M
  JBLOCKROW thisblockrow, lastblockrow;
273
274
25.5M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
275
18.0M
       ci++, compptr++) {
276
    /* Align the virtual buffer for this component. */
277
18.0M
    buffer = (*cinfo->mem->access_virt_barray)
278
18.0M
      ((j_common_ptr)cinfo, coef->whole_image[ci],
279
18.0M
       coef->iMCU_row_num * compptr->v_samp_factor,
280
18.0M
       (JDIMENSION)compptr->v_samp_factor, TRUE);
281
    /* Count non-dummy DCT block rows in this iMCU row. */
282
18.0M
    if (coef->iMCU_row_num < last_iMCU_row)
283
17.9M
      block_rows = compptr->v_samp_factor;
284
76.1k
    else {
285
      /* NB: can't use last_row_height here, since may not be set! */
286
76.1k
      block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
287
76.1k
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
288
76.1k
    }
289
18.0M
    blocks_across = compptr->width_in_blocks;
290
18.0M
    h_samp_factor = compptr->h_samp_factor;
291
    /* Count number of dummy blocks to be added at the right margin. */
292
18.0M
    ndummy = (int)(blocks_across % h_samp_factor);
293
18.0M
    if (ndummy > 0)
294
2.77M
      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
37.4M
    for (block_row = 0; block_row < block_rows; block_row++) {
299
19.4M
      thisblockrow = buffer[block_row];
300
19.4M
      (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
301
19.4M
                                    input_buf[ci], thisblockrow,
302
19.4M
                                    (JDIMENSION)(block_row * DCTSIZE),
303
19.4M
                                    (JDIMENSION)0, blocks_across);
304
19.4M
      if (ndummy > 0) {
305
        /* Create dummy blocks at the right edge of the image. */
306
2.77M
        thisblockrow += blocks_across; /* => first dummy block */
307
2.77M
        jzero_far((void *)thisblockrow, ndummy * sizeof(JBLOCK));
308
2.77M
        lastDC = thisblockrow[-1][0];
309
8.33M
        for (bi = 0; bi < ndummy; bi++) {
310
5.55M
          thisblockrow[bi][0] = lastDC;
311
5.55M
        }
312
2.77M
      }
313
19.4M
    }
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
18.0M
    if (coef->iMCU_row_num == last_iMCU_row) {
320
76.1k
      blocks_across += ndummy;  /* include lower right corner */
321
76.1k
      MCUs_across = blocks_across / h_samp_factor;
322
84.1k
      for (block_row = block_rows; block_row < compptr->v_samp_factor;
323
76.1k
           block_row++) {
324
8.02k
        thisblockrow = buffer[block_row];
325
8.02k
        lastblockrow = buffer[block_row - 1];
326
8.02k
        jzero_far((void *)thisblockrow,
327
8.02k
                  (size_t)(blocks_across * sizeof(JBLOCK)));
328
2.04M
        for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
329
2.03M
          lastDC = lastblockrow[h_samp_factor - 1][0];
330
4.06M
          for (bi = 0; bi < h_samp_factor; bi++) {
331
2.03M
            thisblockrow[bi][0] = lastDC;
332
2.03M
          }
333
2.03M
          thisblockrow += h_samp_factor; /* advance to next MCU in row */
334
2.03M
          lastblockrow += h_samp_factor;
335
2.03M
        }
336
8.02k
      }
337
76.1k
    }
338
18.0M
  }
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
7.52M
  return compress_output(cinfo, input_buf);
345
7.52M
}
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
105M
{
361
105M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
362
105M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
363
105M
  int blkn, ci, xindex, yindex, yoffset;
364
105M
  JDIMENSION start_col;
365
105M
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
366
105M
  JBLOCKROW buffer_ptr;
367
105M
  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
259M
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
374
154M
    compptr = cinfo->cur_comp_info[ci];
375
154M
    buffer[ci] = (*cinfo->mem->access_virt_barray)
376
154M
      ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
377
154M
       coef->iMCU_row_num * compptr->v_samp_factor,
378
154M
       (JDIMENSION)compptr->v_samp_factor, FALSE);
379
154M
  }
380
381
  /* Loop to process one whole iMCU row */
382
216M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
383
111M
       yoffset++) {
384
340M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
385
229M
         MCU_col_num++) {
386
      /* Construct list of pointers to DCT blocks belonging to this MCU */
387
229M
      blkn = 0;                 /* index of current DCT block within MCU */
388
549M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
389
320M
        compptr = cinfo->cur_comp_info[ci];
390
320M
        start_col = MCU_col_num * compptr->MCU_width;
391
658M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
392
337M
          buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
393
722M
          for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
394
384M
            coef->MCU_buffer[blkn++] = buffer_ptr++;
395
384M
          }
396
337M
        }
397
320M
      }
398
      /* Try to write the MCU. */
399
#ifdef WITH_PROFILE
400
      cinfo->master->start = getTime();
401
#endif
402
229M
      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
229M
    }
419
    /* Completed an MCU row, but perhaps not an iMCU row */
420
111M
    coef->mcu_ctr = 0;
421
111M
  }
422
  /* Completed the iMCU row, advance counters for next one */
423
105M
  coef->iMCU_row_num++;
424
105M
  start_iMCU_row(cinfo);
425
105M
  return TRUE;
426
105M
}
jccoefct-8.c:compress_output
Line
Count
Source
360
68.2M
{
361
68.2M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
362
68.2M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
363
68.2M
  int blkn, ci, xindex, yindex, yoffset;
364
68.2M
  JDIMENSION start_col;
365
68.2M
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
366
68.2M
  JBLOCKROW buffer_ptr;
367
68.2M
  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
164M
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
374
95.7M
    compptr = cinfo->cur_comp_info[ci];
375
95.7M
    buffer[ci] = (*cinfo->mem->access_virt_barray)
376
95.7M
      ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
377
95.7M
       coef->iMCU_row_num * compptr->v_samp_factor,
378
95.7M
       (JDIMENSION)compptr->v_samp_factor, FALSE);
379
95.7M
  }
380
381
  /* Loop to process one whole iMCU row */
382
136M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
383
68.2M
       yoffset++) {
384
202M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
385
134M
         MCU_col_num++) {
386
      /* Construct list of pointers to DCT blocks belonging to this MCU */
387
134M
      blkn = 0;                 /* index of current DCT block within MCU */
388
318M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
389
184M
        compptr = cinfo->cur_comp_info[ci];
390
184M
        start_col = MCU_col_num * compptr->MCU_width;
391
377M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
392
193M
          buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
393
417M
          for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
394
224M
            coef->MCU_buffer[blkn++] = buffer_ptr++;
395
224M
          }
396
193M
        }
397
184M
      }
398
      /* Try to write the MCU. */
399
#ifdef WITH_PROFILE
400
      cinfo->master->start = getTime();
401
#endif
402
134M
      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
134M
    }
419
    /* Completed an MCU row, but perhaps not an iMCU row */
420
68.2M
    coef->mcu_ctr = 0;
421
68.2M
  }
422
  /* Completed the iMCU row, advance counters for next one */
423
68.2M
  coef->iMCU_row_num++;
424
68.2M
  start_iMCU_row(cinfo);
425
68.2M
  return TRUE;
426
68.2M
}
jccoefct-12.c:compress_output
Line
Count
Source
360
37.3M
{
361
37.3M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
362
37.3M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
363
37.3M
  int blkn, ci, xindex, yindex, yoffset;
364
37.3M
  JDIMENSION start_col;
365
37.3M
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
366
37.3M
  JBLOCKROW buffer_ptr;
367
37.3M
  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
95.6M
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
374
58.2M
    compptr = cinfo->cur_comp_info[ci];
375
58.2M
    buffer[ci] = (*cinfo->mem->access_virt_barray)
376
58.2M
      ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
377
58.2M
       coef->iMCU_row_num * compptr->v_samp_factor,
378
58.2M
       (JDIMENSION)compptr->v_samp_factor, FALSE);
379
58.2M
  }
380
381
  /* Loop to process one whole iMCU row */
382
80.1M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
383
42.8M
       yoffset++) {
384
137M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
385
94.9M
         MCU_col_num++) {
386
      /* Construct list of pointers to DCT blocks belonging to this MCU */
387
94.9M
      blkn = 0;                 /* index of current DCT block within MCU */
388
231M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
389
136M
        compptr = cinfo->cur_comp_info[ci];
390
136M
        start_col = MCU_col_num * compptr->MCU_width;
391
280M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
392
144M
          buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
393
304M
          for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
394
160M
            coef->MCU_buffer[blkn++] = buffer_ptr++;
395
160M
          }
396
144M
        }
397
136M
      }
398
      /* Try to write the MCU. */
399
#ifdef WITH_PROFILE
400
      cinfo->master->start = getTime();
401
#endif
402
94.9M
      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
94.9M
    }
419
    /* Completed an MCU row, but perhaps not an iMCU row */
420
42.8M
    coef->mcu_ctr = 0;
421
42.8M
  }
422
  /* Completed the iMCU row, advance counters for next one */
423
37.3M
  coef->iMCU_row_num++;
424
37.3M
  start_iMCU_row(cinfo);
425
37.3M
  return TRUE;
426
37.3M
}
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
151k
{
438
151k
  my_coef_ptr coef;
439
440
151k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
441
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
442
443
151k
  coef = (my_coef_ptr)
444
151k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
445
151k
                                sizeof(my_coef_controller));
446
151k
  memset(coef, 0, sizeof(my_coef_controller));
447
151k
  cinfo->coef = (struct jpeg_c_coef_controller *)coef;
448
151k
  coef->pub.start_pass = start_pass_coef;
449
450
  /* Create the coefficient buffer. */
451
151k
  if (need_full_buffer) {
452
86.1k
#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
86.1k
    int ci;
456
86.1k
    jpeg_component_info *compptr;
457
458
316k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
459
230k
         ci++, compptr++) {
460
230k
      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
461
230k
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
462
230k
         (JDIMENSION)jround_up((long)compptr->width_in_blocks,
463
230k
                               (long)compptr->h_samp_factor),
464
230k
         (JDIMENSION)jround_up((long)compptr->height_in_blocks,
465
230k
                               (long)compptr->v_samp_factor),
466
230k
         (JDIMENSION)compptr->v_samp_factor);
467
230k
    }
468
#else
469
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
470
#endif
471
86.1k
  } else {
472
    /* We only need a single-MCU buffer. */
473
64.9k
    JBLOCKROW buffer;
474
64.9k
    int i;
475
476
64.9k
    buffer = (JBLOCKROW)
477
64.9k
      (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
478
64.9k
                                  C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
479
713k
    for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
480
649k
      coef->MCU_buffer[i] = buffer + i;
481
649k
    }
482
    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
483
64.9k
  }
484
151k
}
jinit_c_coef_controller
Line
Count
Source
437
101k
{
438
101k
  my_coef_ptr coef;
439
440
101k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
441
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
442
443
101k
  coef = (my_coef_ptr)
444
101k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
445
101k
                                sizeof(my_coef_controller));
446
101k
  memset(coef, 0, sizeof(my_coef_controller));
447
101k
  cinfo->coef = (struct jpeg_c_coef_controller *)coef;
448
101k
  coef->pub.start_pass = start_pass_coef;
449
450
  /* Create the coefficient buffer. */
451
101k
  if (need_full_buffer) {
452
43.4k
#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
43.4k
    int ci;
456
43.4k
    jpeg_component_info *compptr;
457
458
167k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
459
123k
         ci++, compptr++) {
460
123k
      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
461
123k
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
462
123k
         (JDIMENSION)jround_up((long)compptr->width_in_blocks,
463
123k
                               (long)compptr->h_samp_factor),
464
123k
         (JDIMENSION)jround_up((long)compptr->height_in_blocks,
465
123k
                               (long)compptr->v_samp_factor),
466
123k
         (JDIMENSION)compptr->v_samp_factor);
467
123k
    }
468
#else
469
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
470
#endif
471
57.8k
  } else {
472
    /* We only need a single-MCU buffer. */
473
57.8k
    JBLOCKROW buffer;
474
57.8k
    int i;
475
476
57.8k
    buffer = (JBLOCKROW)
477
57.8k
      (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
478
57.8k
                                  C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
479
636k
    for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
480
578k
      coef->MCU_buffer[i] = buffer + i;
481
578k
    }
482
    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
483
57.8k
  }
484
101k
}
j12init_c_coef_controller
Line
Count
Source
437
49.7k
{
438
49.7k
  my_coef_ptr coef;
439
440
49.7k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
441
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
442
443
49.7k
  coef = (my_coef_ptr)
444
49.7k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
445
49.7k
                                sizeof(my_coef_controller));
446
49.7k
  memset(coef, 0, sizeof(my_coef_controller));
447
49.7k
  cinfo->coef = (struct jpeg_c_coef_controller *)coef;
448
49.7k
  coef->pub.start_pass = start_pass_coef;
449
450
  /* Create the coefficient buffer. */
451
49.7k
  if (need_full_buffer) {
452
42.6k
#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
42.6k
    int ci;
456
42.6k
    jpeg_component_info *compptr;
457
458
149k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
459
106k
         ci++, compptr++) {
460
106k
      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
461
106k
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
462
106k
         (JDIMENSION)jround_up((long)compptr->width_in_blocks,
463
106k
                               (long)compptr->h_samp_factor),
464
106k
         (JDIMENSION)jround_up((long)compptr->height_in_blocks,
465
106k
                               (long)compptr->v_samp_factor),
466
106k
         (JDIMENSION)compptr->v_samp_factor);
467
106k
    }
468
#else
469
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
470
#endif
471
42.6k
  } else {
472
    /* We only need a single-MCU buffer. */
473
7.03k
    JBLOCKROW buffer;
474
7.03k
    int i;
475
476
7.03k
    buffer = (JBLOCKROW)
477
7.03k
      (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
478
7.03k
                                  C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
479
77.4k
    for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
480
70.3k
      coef->MCU_buffer[i] = buffer + i;
481
70.3k
    }
482
    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
483
7.03k
  }
484
49.7k
}