Coverage Report

Created: 2026-06-10 06:18

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
107M
{
78
107M
  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
107M
  if (cinfo->comps_in_scan > 1) {
85
25.9M
    coef->MCU_rows_per_iMCU_row = 1;
86
81.1M
  } else {
87
81.1M
    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
88
80.3M
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
89
798k
    else
90
798k
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
91
81.1M
  }
92
93
107M
  coef->mcu_ctr = 0;
94
107M
  coef->MCU_vert_offset = 0;
95
107M
}
jccoefct-8.c:start_iMCU_row
Line
Count
Source
77
75.0M
{
78
75.0M
  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.0M
  if (cinfo->comps_in_scan > 1) {
85
17.2M
    coef->MCU_rows_per_iMCU_row = 1;
86
57.8M
  } else {
87
57.8M
    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
88
57.3M
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
89
503k
    else
90
503k
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
91
57.8M
  }
92
93
75.0M
  coef->mcu_ctr = 0;
94
75.0M
  coef->MCU_vert_offset = 0;
95
75.0M
}
jccoefct-12.c:start_iMCU_row
Line
Count
Source
77
32.0M
{
78
32.0M
  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
32.0M
  if (cinfo->comps_in_scan > 1) {
85
8.73M
    coef->MCU_rows_per_iMCU_row = 1;
86
23.2M
  } else {
87
23.2M
    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
88
22.9M
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
89
294k
    else
90
294k
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
91
23.2M
  }
92
93
32.0M
  coef->mcu_ctr = 0;
94
32.0M
  coef->MCU_vert_offset = 0;
95
32.0M
}
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
529k
{
105
529k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
106
107
529k
  coef->iMCU_row_num = 0;
108
529k
  start_iMCU_row(cinfo);
109
110
529k
  switch (pass_mode) {
111
48.7k
  case JBUF_PASS_THRU:
112
48.7k
    if (coef->whole_image[0] != NULL)
113
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
114
48.7k
    coef->pub._compress_data = compress_data;
115
48.7k
    break;
116
0
#ifdef FULL_COEF_BUFFER_SUPPORTED
117
59.8k
  case JBUF_SAVE_AND_PASS:
118
59.8k
    if (coef->whole_image[0] == NULL)
119
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
120
59.8k
    coef->pub._compress_data = compress_first_pass;
121
59.8k
    break;
122
421k
  case JBUF_CRANK_DEST:
123
421k
    if (coef->whole_image[0] == NULL)
124
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
125
421k
    coef->pub._compress_data = compress_output;
126
421k
    break;
127
0
#endif
128
0
  default:
129
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
130
0
    break;
131
529k
  }
132
529k
}
jccoefct-8.c:start_pass_coef
Line
Count
Source
104
339k
{
105
339k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
106
107
339k
  coef->iMCU_row_num = 0;
108
339k
  start_iMCU_row(cinfo);
109
110
339k
  switch (pass_mode) {
111
43.9k
  case JBUF_PASS_THRU:
112
43.9k
    if (coef->whole_image[0] != NULL)
113
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
114
43.9k
    coef->pub._compress_data = compress_data;
115
43.9k
    break;
116
0
#ifdef FULL_COEF_BUFFER_SUPPORTED
117
30.1k
  case JBUF_SAVE_AND_PASS:
118
30.1k
    if (coef->whole_image[0] == NULL)
119
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
120
30.1k
    coef->pub._compress_data = compress_first_pass;
121
30.1k
    break;
122
264k
  case JBUF_CRANK_DEST:
123
264k
    if (coef->whole_image[0] == NULL)
124
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
125
264k
    coef->pub._compress_data = compress_output;
126
264k
    break;
127
0
#endif
128
0
  default:
129
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
130
0
    break;
131
339k
  }
132
339k
}
jccoefct-12.c:start_pass_coef
Line
Count
Source
104
190k
{
105
190k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
106
107
190k
  coef->iMCU_row_num = 0;
108
190k
  start_iMCU_row(cinfo);
109
110
190k
  switch (pass_mode) {
111
4.77k
  case JBUF_PASS_THRU:
112
4.77k
    if (coef->whole_image[0] != NULL)
113
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
114
4.77k
    coef->pub._compress_data = compress_data;
115
4.77k
    break;
116
0
#ifdef FULL_COEF_BUFFER_SUPPORTED
117
29.6k
  case JBUF_SAVE_AND_PASS:
118
29.6k
    if (coef->whole_image[0] == NULL)
119
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
120
29.6k
    coef->pub._compress_data = compress_first_pass;
121
29.6k
    break;
122
156k
  case JBUF_CRANK_DEST:
123
156k
    if (coef->whole_image[0] == NULL)
124
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
125
156k
    coef->pub._compress_data = compress_output;
126
156k
    break;
127
0
#endif
128
0
  default:
129
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
130
0
    break;
131
190k
  }
132
190k
}
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.80M
{
148
7.80M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
149
7.80M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
150
7.80M
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
151
7.80M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
152
7.80M
  int blkn, bi, ci, yindex, yoffset, blockcnt;
153
7.80M
  JDIMENSION ypos, xpos;
154
7.80M
  jpeg_component_info *compptr;
155
156
  /* Loop to write as much as one whole iMCU row */
157
15.6M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
158
7.80M
       yoffset++) {
159
26.4M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
160
18.6M
         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
18.6M
      blkn = 0;
171
58.1M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
172
39.5M
        compptr = cinfo->cur_comp_info[ci];
173
39.5M
        blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width :
174
39.5M
                                                  compptr->last_col_width;
175
39.5M
        xpos = MCU_col_num * compptr->MCU_sample_width;
176
39.5M
        ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
177
84.1M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
178
44.6M
          if (coef->iMCU_row_num < last_iMCU_row ||
179
42.8M
              yoffset + yindex < compptr->last_row_height) {
180
42.8M
            (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
181
42.8M
                                          input_buf[compptr->component_index],
182
42.8M
                                          coef->MCU_buffer[blkn],
183
42.8M
                                          ypos, xpos, (JDIMENSION)blockcnt);
184
42.8M
            if (blockcnt < compptr->MCU_width) {
185
              /* Create some dummy blocks at the right edge of the image. */
186
4.55M
              jzero_far((void *)coef->MCU_buffer[blkn + blockcnt],
187
4.55M
                        (compptr->MCU_width - blockcnt) * sizeof(JBLOCK));
188
9.56M
              for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
189
5.00M
                coef->MCU_buffer[blkn + bi][0][0] =
190
5.00M
                  coef->MCU_buffer[blkn + bi - 1][0][0];
191
5.00M
              }
192
4.55M
            }
193
42.8M
          } else {
194
            /* Create a row of dummy blocks at the bottom of the image. */
195
1.74M
            jzero_far((void *)coef->MCU_buffer[blkn],
196
1.74M
                      compptr->MCU_width * sizeof(JBLOCK));
197
5.16M
            for (bi = 0; bi < compptr->MCU_width; bi++) {
198
3.41M
              coef->MCU_buffer[blkn + bi][0][0] =
199
3.41M
                coef->MCU_buffer[blkn - 1][0][0];
200
3.41M
            }
201
1.74M
          }
202
44.6M
          blkn += compptr->MCU_width;
203
44.6M
          ypos += DCTSIZE;
204
44.6M
        }
205
39.5M
      }
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
18.6M
      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
18.6M
    }
229
    /* Completed an MCU row, but perhaps not an iMCU row */
230
7.80M
    coef->mcu_ctr = 0;
231
7.80M
  }
232
  /* Completed the iMCU row, advance counters for next one */
233
7.80M
  coef->iMCU_row_num++;
234
7.80M
  start_iMCU_row(cinfo);
235
7.80M
  return TRUE;
236
7.80M
}
jccoefct-8.c:compress_data
Line
Count
Source
147
7.22M
{
148
7.22M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
149
7.22M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
150
7.22M
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
151
7.22M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
152
7.22M
  int blkn, bi, ci, yindex, yoffset, blockcnt;
153
7.22M
  JDIMENSION ypos, xpos;
154
7.22M
  jpeg_component_info *compptr;
155
156
  /* Loop to write as much as one whole iMCU row */
157
14.4M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
158
7.22M
       yoffset++) {
159
24.6M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
160
17.4M
         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.4M
      blkn = 0;
171
53.3M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
172
35.9M
        compptr = cinfo->cur_comp_info[ci];
173
35.9M
        blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width :
174
35.9M
                                                  compptr->last_col_width;
175
35.9M
        xpos = MCU_col_num * compptr->MCU_sample_width;
176
35.9M
        ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
177
75.7M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
178
39.8M
          if (coef->iMCU_row_num < last_iMCU_row ||
179
38.5M
              yoffset + yindex < compptr->last_row_height) {
180
38.5M
            (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
181
38.5M
                                          input_buf[compptr->component_index],
182
38.5M
                                          coef->MCU_buffer[blkn],
183
38.5M
                                          ypos, xpos, (JDIMENSION)blockcnt);
184
38.5M
            if (blockcnt < compptr->MCU_width) {
185
              /* Create some dummy blocks at the right edge of the image. */
186
3.40M
              jzero_far((void *)coef->MCU_buffer[blkn + blockcnt],
187
3.40M
                        (compptr->MCU_width - blockcnt) * sizeof(JBLOCK));
188
7.25M
              for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
189
3.85M
                coef->MCU_buffer[blkn + bi][0][0] =
190
3.85M
                  coef->MCU_buffer[blkn + bi - 1][0][0];
191
3.85M
              }
192
3.40M
            }
193
38.5M
          } else {
194
            /* Create a row of dummy blocks at the bottom of the image. */
195
1.24M
            jzero_far((void *)coef->MCU_buffer[blkn],
196
1.24M
                      compptr->MCU_width * sizeof(JBLOCK));
197
3.64M
            for (bi = 0; bi < compptr->MCU_width; bi++) {
198
2.40M
              coef->MCU_buffer[blkn + bi][0][0] =
199
2.40M
                coef->MCU_buffer[blkn - 1][0][0];
200
2.40M
            }
201
1.24M
          }
202
39.8M
          blkn += compptr->MCU_width;
203
39.8M
          ypos += DCTSIZE;
204
39.8M
        }
205
35.9M
      }
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.4M
      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.4M
    }
229
    /* Completed an MCU row, but perhaps not an iMCU row */
230
7.22M
    coef->mcu_ctr = 0;
231
7.22M
  }
232
  /* Completed the iMCU row, advance counters for next one */
233
7.22M
  coef->iMCU_row_num++;
234
7.22M
  start_iMCU_row(cinfo);
235
7.22M
  return TRUE;
236
7.22M
}
jccoefct-12.c:compress_data
Line
Count
Source
147
581k
{
148
581k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
149
581k
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
150
581k
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
151
581k
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
152
581k
  int blkn, bi, ci, yindex, yoffset, blockcnt;
153
581k
  JDIMENSION ypos, xpos;
154
581k
  jpeg_component_info *compptr;
155
156
  /* Loop to write as much as one whole iMCU row */
157
1.16M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
158
581k
       yoffset++) {
159
1.77M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
160
1.19M
         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.19M
      blkn = 0;
171
4.79M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
172
3.59M
        compptr = cinfo->cur_comp_info[ci];
173
3.59M
        blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width :
174
3.59M
                                                  compptr->last_col_width;
175
3.59M
        xpos = MCU_col_num * compptr->MCU_sample_width;
176
3.59M
        ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
177
8.38M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
178
4.79M
          if (coef->iMCU_row_num < last_iMCU_row ||
179
4.28M
              yoffset + yindex < compptr->last_row_height) {
180
4.28M
            (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
181
4.28M
                                          input_buf[compptr->component_index],
182
4.28M
                                          coef->MCU_buffer[blkn],
183
4.28M
                                          ypos, xpos, (JDIMENSION)blockcnt);
184
4.28M
            if (blockcnt < compptr->MCU_width) {
185
              /* Create some dummy blocks at the right edge of the image. */
186
1.15M
              jzero_far((void *)coef->MCU_buffer[blkn + blockcnt],
187
1.15M
                        (compptr->MCU_width - blockcnt) * sizeof(JBLOCK));
188
2.30M
              for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
189
1.15M
                coef->MCU_buffer[blkn + bi][0][0] =
190
1.15M
                  coef->MCU_buffer[blkn + bi - 1][0][0];
191
1.15M
              }
192
1.15M
            }
193
4.28M
          } else {
194
            /* Create a row of dummy blocks at the bottom of the image. */
195
506k
            jzero_far((void *)coef->MCU_buffer[blkn],
196
506k
                      compptr->MCU_width * sizeof(JBLOCK));
197
1.51M
            for (bi = 0; bi < compptr->MCU_width; bi++) {
198
1.01M
              coef->MCU_buffer[blkn + bi][0][0] =
199
1.01M
                coef->MCU_buffer[blkn - 1][0][0];
200
1.01M
            }
201
506k
          }
202
4.79M
          blkn += compptr->MCU_width;
203
4.79M
          ypos += DCTSIZE;
204
4.79M
        }
205
3.59M
      }
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.19M
      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.19M
    }
229
    /* Completed an MCU row, but perhaps not an iMCU row */
230
581k
    coef->mcu_ctr = 0;
231
581k
  }
232
  /* Completed the iMCU row, advance counters for next one */
233
581k
  coef->iMCU_row_num++;
234
581k
  start_iMCU_row(cinfo);
235
581k
  return TRUE;
236
581k
}
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
12.8M
{
265
12.8M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
266
12.8M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
267
12.8M
  JDIMENSION blocks_across, MCUs_across, MCUindex;
268
12.8M
  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
269
12.8M
  JCOEF lastDC;
270
12.8M
  jpeg_component_info *compptr;
271
12.8M
  JBLOCKARRAY buffer;
272
12.8M
  JBLOCKROW thisblockrow, lastblockrow;
273
274
45.5M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
275
32.7M
       ci++, compptr++) {
276
    /* Align the virtual buffer for this component. */
277
32.7M
    buffer = (*cinfo->mem->access_virt_barray)
278
32.7M
      ((j_common_ptr)cinfo, coef->whole_image[ci],
279
32.7M
       coef->iMCU_row_num * compptr->v_samp_factor,
280
32.7M
       (JDIMENSION)compptr->v_samp_factor, TRUE);
281
    /* Count non-dummy DCT block rows in this iMCU row. */
282
32.7M
    if (coef->iMCU_row_num < last_iMCU_row)
283
32.5M
      block_rows = compptr->v_samp_factor;
284
160k
    else {
285
      /* NB: can't use last_row_height here, since may not be set! */
286
160k
      block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
287
160k
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
288
160k
    }
289
32.7M
    blocks_across = compptr->width_in_blocks;
290
32.7M
    h_samp_factor = compptr->h_samp_factor;
291
    /* Count number of dummy blocks to be added at the right margin. */
292
32.7M
    ndummy = (int)(blocks_across % h_samp_factor);
293
32.7M
    if (ndummy > 0)
294
6.95M
      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
67.7M
    for (block_row = 0; block_row < block_rows; block_row++) {
299
34.9M
      thisblockrow = buffer[block_row];
300
34.9M
      (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
301
34.9M
                                    input_buf[ci], thisblockrow,
302
34.9M
                                    (JDIMENSION)(block_row * DCTSIZE),
303
34.9M
                                    (JDIMENSION)0, blocks_across);
304
34.9M
      if (ndummy > 0) {
305
        /* Create dummy blocks at the right edge of the image. */
306
6.95M
        thisblockrow += blocks_across; /* => first dummy block */
307
6.95M
        jzero_far((void *)thisblockrow, ndummy * sizeof(JBLOCK));
308
6.95M
        lastDC = thisblockrow[-1][0];
309
20.8M
        for (bi = 0; bi < ndummy; bi++) {
310
13.8M
          thisblockrow[bi][0] = lastDC;
311
13.8M
        }
312
6.95M
      }
313
34.9M
    }
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
32.7M
    if (coef->iMCU_row_num == last_iMCU_row) {
320
160k
      blocks_across += ndummy;  /* include lower right corner */
321
160k
      MCUs_across = blocks_across / h_samp_factor;
322
177k
      for (block_row = block_rows; block_row < compptr->v_samp_factor;
323
160k
           block_row++) {
324
17.1k
        thisblockrow = buffer[block_row];
325
17.1k
        lastblockrow = buffer[block_row - 1];
326
17.1k
        jzero_far((void *)thisblockrow,
327
17.1k
                  (size_t)(blocks_across * sizeof(JBLOCK)));
328
4.67M
        for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
329
4.65M
          lastDC = lastblockrow[h_samp_factor - 1][0];
330
9.30M
          for (bi = 0; bi < h_samp_factor; bi++) {
331
4.65M
            thisblockrow[bi][0] = lastDC;
332
4.65M
          }
333
4.65M
          thisblockrow += h_samp_factor; /* advance to next MCU in row */
334
4.65M
          lastblockrow += h_samp_factor;
335
4.65M
        }
336
17.1k
      }
337
160k
    }
338
32.7M
  }
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
12.8M
  return compress_output(cinfo, input_buf);
345
12.8M
}
jccoefct-8.c:compress_first_pass
Line
Count
Source
264
6.57M
{
265
6.57M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
266
6.57M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
267
6.57M
  JDIMENSION blocks_across, MCUs_across, MCUindex;
268
6.57M
  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
269
6.57M
  JCOEF lastDC;
270
6.57M
  jpeg_component_info *compptr;
271
6.57M
  JBLOCKARRAY buffer;
272
6.57M
  JBLOCKROW thisblockrow, lastblockrow;
273
274
24.3M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
275
17.7M
       ci++, compptr++) {
276
    /* Align the virtual buffer for this component. */
277
17.7M
    buffer = (*cinfo->mem->access_virt_barray)
278
17.7M
      ((j_common_ptr)cinfo, coef->whole_image[ci],
279
17.7M
       coef->iMCU_row_num * compptr->v_samp_factor,
280
17.7M
       (JDIMENSION)compptr->v_samp_factor, TRUE);
281
    /* Count non-dummy DCT block rows in this iMCU row. */
282
17.7M
    if (coef->iMCU_row_num < last_iMCU_row)
283
17.6M
      block_rows = compptr->v_samp_factor;
284
87.4k
    else {
285
      /* NB: can't use last_row_height here, since may not be set! */
286
87.4k
      block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
287
87.4k
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
288
87.4k
    }
289
17.7M
    blocks_across = compptr->width_in_blocks;
290
17.7M
    h_samp_factor = compptr->h_samp_factor;
291
    /* Count number of dummy blocks to be added at the right margin. */
292
17.7M
    ndummy = (int)(blocks_across % h_samp_factor);
293
17.7M
    if (ndummy > 0)
294
4.64M
      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
36.6M
    for (block_row = 0; block_row < block_rows; block_row++) {
299
18.8M
      thisblockrow = buffer[block_row];
300
18.8M
      (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
301
18.8M
                                    input_buf[ci], thisblockrow,
302
18.8M
                                    (JDIMENSION)(block_row * DCTSIZE),
303
18.8M
                                    (JDIMENSION)0, blocks_across);
304
18.8M
      if (ndummy > 0) {
305
        /* Create dummy blocks at the right edge of the image. */
306
4.64M
        thisblockrow += blocks_across; /* => first dummy block */
307
4.64M
        jzero_far((void *)thisblockrow, ndummy * sizeof(JBLOCK));
308
4.64M
        lastDC = thisblockrow[-1][0];
309
13.9M
        for (bi = 0; bi < ndummy; bi++) {
310
9.28M
          thisblockrow[bi][0] = lastDC;
311
9.28M
        }
312
4.64M
      }
313
18.8M
    }
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.7M
    if (coef->iMCU_row_num == last_iMCU_row) {
320
87.4k
      blocks_across += ndummy;  /* include lower right corner */
321
87.4k
      MCUs_across = blocks_across / h_samp_factor;
322
96.8k
      for (block_row = block_rows; block_row < compptr->v_samp_factor;
323
87.4k
           block_row++) {
324
9.45k
        thisblockrow = buffer[block_row];
325
9.45k
        lastblockrow = buffer[block_row - 1];
326
9.45k
        jzero_far((void *)thisblockrow,
327
9.45k
                  (size_t)(blocks_across * sizeof(JBLOCK)));
328
2.64M
        for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
329
2.63M
          lastDC = lastblockrow[h_samp_factor - 1][0];
330
5.27M
          for (bi = 0; bi < h_samp_factor; bi++) {
331
2.63M
            thisblockrow[bi][0] = lastDC;
332
2.63M
          }
333
2.63M
          thisblockrow += h_samp_factor; /* advance to next MCU in row */
334
2.63M
          lastblockrow += h_samp_factor;
335
2.63M
        }
336
9.45k
      }
337
87.4k
    }
338
17.7M
  }
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.57M
  return compress_output(cinfo, input_buf);
345
6.57M
}
jccoefct-12.c:compress_first_pass
Line
Count
Source
264
6.26M
{
265
6.26M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
266
6.26M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
267
6.26M
  JDIMENSION blocks_across, MCUs_across, MCUindex;
268
6.26M
  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
269
6.26M
  JCOEF lastDC;
270
6.26M
  jpeg_component_info *compptr;
271
6.26M
  JBLOCKARRAY buffer;
272
6.26M
  JBLOCKROW thisblockrow, lastblockrow;
273
274
21.2M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
275
14.9M
       ci++, compptr++) {
276
    /* Align the virtual buffer for this component. */
277
14.9M
    buffer = (*cinfo->mem->access_virt_barray)
278
14.9M
      ((j_common_ptr)cinfo, coef->whole_image[ci],
279
14.9M
       coef->iMCU_row_num * compptr->v_samp_factor,
280
14.9M
       (JDIMENSION)compptr->v_samp_factor, TRUE);
281
    /* Count non-dummy DCT block rows in this iMCU row. */
282
14.9M
    if (coef->iMCU_row_num < last_iMCU_row)
283
14.8M
      block_rows = compptr->v_samp_factor;
284
73.1k
    else {
285
      /* NB: can't use last_row_height here, since may not be set! */
286
73.1k
      block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
287
73.1k
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
288
73.1k
    }
289
14.9M
    blocks_across = compptr->width_in_blocks;
290
14.9M
    h_samp_factor = compptr->h_samp_factor;
291
    /* Count number of dummy blocks to be added at the right margin. */
292
14.9M
    ndummy = (int)(blocks_across % h_samp_factor);
293
14.9M
    if (ndummy > 0)
294
2.30M
      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
31.0M
    for (block_row = 0; block_row < block_rows; block_row++) {
299
16.1M
      thisblockrow = buffer[block_row];
300
16.1M
      (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
301
16.1M
                                    input_buf[ci], thisblockrow,
302
16.1M
                                    (JDIMENSION)(block_row * DCTSIZE),
303
16.1M
                                    (JDIMENSION)0, blocks_across);
304
16.1M
      if (ndummy > 0) {
305
        /* Create dummy blocks at the right edge of the image. */
306
2.30M
        thisblockrow += blocks_across; /* => first dummy block */
307
2.30M
        jzero_far((void *)thisblockrow, ndummy * sizeof(JBLOCK));
308
2.30M
        lastDC = thisblockrow[-1][0];
309
6.90M
        for (bi = 0; bi < ndummy; bi++) {
310
4.60M
          thisblockrow[bi][0] = lastDC;
311
4.60M
        }
312
2.30M
      }
313
16.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
14.9M
    if (coef->iMCU_row_num == last_iMCU_row) {
320
73.1k
      blocks_across += ndummy;  /* include lower right corner */
321
73.1k
      MCUs_across = blocks_across / h_samp_factor;
322
80.7k
      for (block_row = block_rows; block_row < compptr->v_samp_factor;
323
73.1k
           block_row++) {
324
7.65k
        thisblockrow = buffer[block_row];
325
7.65k
        lastblockrow = buffer[block_row - 1];
326
7.65k
        jzero_far((void *)thisblockrow,
327
7.65k
                  (size_t)(blocks_across * sizeof(JBLOCK)));
328
2.02M
        for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
329
2.01M
          lastDC = lastblockrow[h_samp_factor - 1][0];
330
4.03M
          for (bi = 0; bi < h_samp_factor; bi++) {
331
2.01M
            thisblockrow[bi][0] = lastDC;
332
2.01M
          }
333
2.01M
          thisblockrow += h_samp_factor; /* advance to next MCU in row */
334
2.01M
          lastblockrow += h_samp_factor;
335
2.01M
        }
336
7.65k
      }
337
73.1k
    }
338
14.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.26M
  return compress_output(cinfo, input_buf);
345
6.26M
}
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
98.7M
{
361
98.7M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
362
98.7M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
363
98.7M
  int blkn, ci, xindex, yindex, yoffset;
364
98.7M
  JDIMENSION start_col;
365
98.7M
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
366
98.7M
  JBLOCKROW buffer_ptr;
367
98.7M
  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
242M
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
374
143M
    compptr = cinfo->cur_comp_info[ci];
375
143M
    buffer[ci] = (*cinfo->mem->access_virt_barray)
376
143M
      ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
377
143M
       coef->iMCU_row_num * compptr->v_samp_factor,
378
143M
       (JDIMENSION)compptr->v_samp_factor, FALSE);
379
143M
  }
380
381
  /* Loop to process one whole iMCU row */
382
202M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
383
103M
       yoffset++) {
384
315M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
385
212M
         MCU_col_num++) {
386
      /* Construct list of pointers to DCT blocks belonging to this MCU */
387
212M
      blkn = 0;                 /* index of current DCT block within MCU */
388
508M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
389
296M
        compptr = cinfo->cur_comp_info[ci];
390
296M
        start_col = MCU_col_num * compptr->MCU_width;
391
609M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
392
313M
          buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
393
670M
          for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
394
357M
            coef->MCU_buffer[blkn++] = buffer_ptr++;
395
357M
          }
396
313M
        }
397
296M
      }
398
      /* Try to write the MCU. */
399
#ifdef WITH_PROFILE
400
      cinfo->master->start = getTime();
401
#endif
402
212M
      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
212M
    }
419
    /* Completed an MCU row, but perhaps not an iMCU row */
420
103M
    coef->mcu_ctr = 0;
421
103M
  }
422
  /* Completed the iMCU row, advance counters for next one */
423
98.7M
  coef->iMCU_row_num++;
424
98.7M
  start_iMCU_row(cinfo);
425
98.7M
  return TRUE;
426
98.7M
}
jccoefct-8.c:compress_output
Line
Count
Source
360
67.4M
{
361
67.4M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
362
67.4M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
363
67.4M
  int blkn, ci, xindex, yindex, yoffset;
364
67.4M
  JDIMENSION start_col;
365
67.4M
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
366
67.4M
  JBLOCKROW buffer_ptr;
367
67.4M
  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
162M
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
374
94.6M
    compptr = cinfo->cur_comp_info[ci];
375
94.6M
    buffer[ci] = (*cinfo->mem->access_virt_barray)
376
94.6M
      ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
377
94.6M
       coef->iMCU_row_num * compptr->v_samp_factor,
378
94.6M
       (JDIMENSION)compptr->v_samp_factor, FALSE);
379
94.6M
  }
380
381
  /* Loop to process one whole iMCU row */
382
134M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
383
67.4M
       yoffset++) {
384
198M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
385
131M
         MCU_col_num++) {
386
      /* Construct list of pointers to DCT blocks belonging to this MCU */
387
131M
      blkn = 0;                 /* index of current DCT block within MCU */
388
312M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
389
181M
        compptr = cinfo->cur_comp_info[ci];
390
181M
        start_col = MCU_col_num * compptr->MCU_width;
391
371M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
392
189M
          buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
393
411M
          for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
394
221M
            coef->MCU_buffer[blkn++] = buffer_ptr++;
395
221M
          }
396
189M
        }
397
181M
      }
398
      /* Try to write the MCU. */
399
#ifdef WITH_PROFILE
400
      cinfo->master->start = getTime();
401
#endif
402
131M
      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
131M
    }
419
    /* Completed an MCU row, but perhaps not an iMCU row */
420
67.4M
    coef->mcu_ctr = 0;
421
67.4M
  }
422
  /* Completed the iMCU row, advance counters for next one */
423
67.4M
  coef->iMCU_row_num++;
424
67.4M
  start_iMCU_row(cinfo);
425
67.4M
  return TRUE;
426
67.4M
}
jccoefct-12.c:compress_output
Line
Count
Source
360
31.2M
{
361
31.2M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
362
31.2M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
363
31.2M
  int blkn, ci, xindex, yindex, yoffset;
364
31.2M
  JDIMENSION start_col;
365
31.2M
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
366
31.2M
  JBLOCKROW buffer_ptr;
367
31.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
79.8M
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
374
48.6M
    compptr = cinfo->cur_comp_info[ci];
375
48.6M
    buffer[ci] = (*cinfo->mem->access_virt_barray)
376
48.6M
      ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
377
48.6M
       coef->iMCU_row_num * compptr->v_samp_factor,
378
48.6M
       (JDIMENSION)compptr->v_samp_factor, FALSE);
379
48.6M
  }
380
381
  /* Loop to process one whole iMCU row */
382
67.1M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
383
35.8M
       yoffset++) {
384
116M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
385
80.6M
         MCU_col_num++) {
386
      /* Construct list of pointers to DCT blocks belonging to this MCU */
387
80.6M
      blkn = 0;                 /* index of current DCT block within MCU */
388
196M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
389
115M
        compptr = cinfo->cur_comp_info[ci];
390
115M
        start_col = MCU_col_num * compptr->MCU_width;
391
238M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
392
123M
          buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
393
258M
          for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
394
135M
            coef->MCU_buffer[blkn++] = buffer_ptr++;
395
135M
          }
396
123M
        }
397
115M
      }
398
      /* Try to write the MCU. */
399
#ifdef WITH_PROFILE
400
      cinfo->master->start = getTime();
401
#endif
402
80.6M
      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
80.6M
    }
419
    /* Completed an MCU row, but perhaps not an iMCU row */
420
35.8M
    coef->mcu_ctr = 0;
421
35.8M
  }
422
  /* Completed the iMCU row, advance counters for next one */
423
31.2M
  coef->iMCU_row_num++;
424
31.2M
  start_iMCU_row(cinfo);
425
31.2M
  return TRUE;
426
31.2M
}
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
153k
{
438
153k
  my_coef_ptr coef;
439
440
153k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
441
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
442
443
153k
  coef = (my_coef_ptr)
444
153k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
445
153k
                                sizeof(my_coef_controller));
446
153k
  memset(coef, 0, sizeof(my_coef_controller));
447
153k
  cinfo->coef = (struct jpeg_c_coef_controller *)coef;
448
153k
  coef->pub.start_pass = start_pass_coef;
449
450
  /* Create the coefficient buffer. */
451
153k
  if (need_full_buffer) {
452
87.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
87.1k
    int ci;
456
87.1k
    jpeg_component_info *compptr;
457
458
320k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
459
233k
         ci++, compptr++) {
460
233k
      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
461
233k
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
462
233k
         (JDIMENSION)jround_up((long)compptr->width_in_blocks,
463
233k
                               (long)compptr->h_samp_factor),
464
233k
         (JDIMENSION)jround_up((long)compptr->height_in_blocks,
465
233k
                               (long)compptr->v_samp_factor),
466
233k
         (JDIMENSION)compptr->v_samp_factor);
467
233k
    }
468
#else
469
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
470
#endif
471
87.1k
  } else {
472
    /* We only need a single-MCU buffer. */
473
66.6k
    JBLOCKROW buffer;
474
66.6k
    int i;
475
476
66.6k
    buffer = (JBLOCKROW)
477
66.6k
      (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
478
66.6k
                                  C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
479
732k
    for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
480
666k
      coef->MCU_buffer[i] = buffer + i;
481
666k
    }
482
    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
483
66.6k
  }
484
153k
}
jinit_c_coef_controller
Line
Count
Source
437
104k
{
438
104k
  my_coef_ptr coef;
439
440
104k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
441
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
442
443
104k
  coef = (my_coef_ptr)
444
104k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
445
104k
                                sizeof(my_coef_controller));
446
104k
  memset(coef, 0, sizeof(my_coef_controller));
447
104k
  cinfo->coef = (struct jpeg_c_coef_controller *)coef;
448
104k
  coef->pub.start_pass = start_pass_coef;
449
450
  /* Create the coefficient buffer. */
451
104k
  if (need_full_buffer) {
452
45.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
45.1k
    int ci;
456
45.1k
    jpeg_component_info *compptr;
457
458
174k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
459
129k
         ci++, compptr++) {
460
129k
      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
461
129k
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
462
129k
         (JDIMENSION)jround_up((long)compptr->width_in_blocks,
463
129k
                               (long)compptr->h_samp_factor),
464
129k
         (JDIMENSION)jround_up((long)compptr->height_in_blocks,
465
129k
                               (long)compptr->v_samp_factor),
466
129k
         (JDIMENSION)compptr->v_samp_factor);
467
129k
    }
468
#else
469
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
470
#endif
471
59.7k
  } else {
472
    /* We only need a single-MCU buffer. */
473
59.7k
    JBLOCKROW buffer;
474
59.7k
    int i;
475
476
59.7k
    buffer = (JBLOCKROW)
477
59.7k
      (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
478
59.7k
                                  C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
479
657k
    for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
480
597k
      coef->MCU_buffer[i] = buffer + i;
481
597k
    }
482
    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
483
59.7k
  }
484
104k
}
j12init_c_coef_controller
Line
Count
Source
437
48.9k
{
438
48.9k
  my_coef_ptr coef;
439
440
48.9k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
441
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
442
443
48.9k
  coef = (my_coef_ptr)
444
48.9k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
445
48.9k
                                sizeof(my_coef_controller));
446
48.9k
  memset(coef, 0, sizeof(my_coef_controller));
447
48.9k
  cinfo->coef = (struct jpeg_c_coef_controller *)coef;
448
48.9k
  coef->pub.start_pass = start_pass_coef;
449
450
  /* Create the coefficient buffer. */
451
48.9k
  if (need_full_buffer) {
452
42.0k
#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.0k
    int ci;
456
42.0k
    jpeg_component_info *compptr;
457
458
146k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
459
104k
         ci++, compptr++) {
460
104k
      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
461
104k
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
462
104k
         (JDIMENSION)jround_up((long)compptr->width_in_blocks,
463
104k
                               (long)compptr->h_samp_factor),
464
104k
         (JDIMENSION)jround_up((long)compptr->height_in_blocks,
465
104k
                               (long)compptr->v_samp_factor),
466
104k
         (JDIMENSION)compptr->v_samp_factor);
467
104k
    }
468
#else
469
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
470
#endif
471
42.0k
  } else {
472
    /* We only need a single-MCU buffer. */
473
6.89k
    JBLOCKROW buffer;
474
6.89k
    int i;
475
476
6.89k
    buffer = (JBLOCKROW)
477
6.89k
      (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
478
6.89k
                                  C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
479
75.8k
    for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
480
68.9k
      coef->MCU_buffer[i] = buffer + i;
481
68.9k
    }
482
    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
483
6.89k
  }
484
48.9k
}