Coverage Report

Created: 2026-07-16 07:45

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
114M
{
78
114M
  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
114M
  if (cinfo->comps_in_scan > 1) {
85
27.9M
    coef->MCU_rows_per_iMCU_row = 1;
86
86.5M
  } else {
87
86.5M
    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
88
85.7M
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
89
818k
    else
90
818k
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
91
86.5M
  }
92
93
114M
  coef->mcu_ctr = 0;
94
114M
  coef->MCU_vert_offset = 0;
95
114M
}
jccoefct-8.c:start_iMCU_row
Line
Count
Source
77
79.2M
{
78
79.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
79.2M
  if (cinfo->comps_in_scan > 1) {
85
18.1M
    coef->MCU_rows_per_iMCU_row = 1;
86
61.0M
  } else {
87
61.0M
    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
88
60.5M
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
89
508k
    else
90
508k
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
91
61.0M
  }
92
93
79.2M
  coef->mcu_ctr = 0;
94
79.2M
  coef->MCU_vert_offset = 0;
95
79.2M
}
jccoefct-12.c:start_iMCU_row
Line
Count
Source
77
35.2M
{
78
35.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
35.2M
  if (cinfo->comps_in_scan > 1) {
85
9.80M
    coef->MCU_rows_per_iMCU_row = 1;
86
25.4M
  } else {
87
25.4M
    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
88
25.1M
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
89
310k
    else
90
310k
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
91
25.4M
  }
92
93
35.2M
  coef->mcu_ctr = 0;
94
35.2M
  coef->MCU_vert_offset = 0;
95
35.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
543k
{
105
543k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
106
107
543k
  coef->iMCU_row_num = 0;
108
543k
  start_iMCU_row(cinfo);
109
110
543k
  switch (pass_mode) {
111
49.4k
  case JBUF_PASS_THRU:
112
49.4k
    if (coef->whole_image[0] != NULL)
113
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
114
49.4k
    coef->pub._compress_data = compress_data;
115
49.4k
    break;
116
0
#ifdef FULL_COEF_BUFFER_SUPPORTED
117
61.8k
  case JBUF_SAVE_AND_PASS:
118
61.8k
    if (coef->whole_image[0] == NULL)
119
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
120
61.8k
    coef->pub._compress_data = compress_first_pass;
121
61.8k
    break;
122
432k
  case JBUF_CRANK_DEST:
123
432k
    if (coef->whole_image[0] == NULL)
124
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
125
432k
    coef->pub._compress_data = compress_output;
126
432k
    break;
127
0
#endif
128
0
  default:
129
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
130
0
    break;
131
543k
  }
132
543k
}
jccoefct-8.c:start_pass_coef
Line
Count
Source
104
342k
{
105
342k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
106
107
342k
  coef->iMCU_row_num = 0;
108
342k
  start_iMCU_row(cinfo);
109
110
342k
  switch (pass_mode) {
111
44.3k
  case JBUF_PASS_THRU:
112
44.3k
    if (coef->whole_image[0] != NULL)
113
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
114
44.3k
    coef->pub._compress_data = compress_data;
115
44.3k
    break;
116
0
#ifdef FULL_COEF_BUFFER_SUPPORTED
117
30.4k
  case JBUF_SAVE_AND_PASS:
118
30.4k
    if (coef->whole_image[0] == NULL)
119
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
120
30.4k
    coef->pub._compress_data = compress_first_pass;
121
30.4k
    break;
122
267k
  case JBUF_CRANK_DEST:
123
267k
    if (coef->whole_image[0] == NULL)
124
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
125
267k
    coef->pub._compress_data = compress_output;
126
267k
    break;
127
0
#endif
128
0
  default:
129
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
130
0
    break;
131
342k
  }
132
342k
}
jccoefct-12.c:start_pass_coef
Line
Count
Source
104
201k
{
105
201k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
106
107
201k
  coef->iMCU_row_num = 0;
108
201k
  start_iMCU_row(cinfo);
109
110
201k
  switch (pass_mode) {
111
5.08k
  case JBUF_PASS_THRU:
112
5.08k
    if (coef->whole_image[0] != NULL)
113
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
114
5.08k
    coef->pub._compress_data = compress_data;
115
5.08k
    break;
116
0
#ifdef FULL_COEF_BUFFER_SUPPORTED
117
31.4k
  case JBUF_SAVE_AND_PASS:
118
31.4k
    if (coef->whole_image[0] == NULL)
119
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
120
31.4k
    coef->pub._compress_data = compress_first_pass;
121
31.4k
    break;
122
164k
  case JBUF_CRANK_DEST:
123
164k
    if (coef->whole_image[0] == NULL)
124
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
125
164k
    coef->pub._compress_data = compress_output;
126
164k
    break;
127
0
#endif
128
0
  default:
129
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
130
0
    break;
131
201k
  }
132
201k
}
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
8.22M
{
148
8.22M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
149
8.22M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
150
8.22M
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
151
8.22M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
152
8.22M
  int blkn, bi, ci, yindex, yoffset, blockcnt;
153
8.22M
  JDIMENSION ypos, xpos;
154
8.22M
  jpeg_component_info *compptr;
155
156
  /* Loop to write as much as one whole iMCU row */
157
16.4M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
158
8.22M
       yoffset++) {
159
27.0M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
160
18.8M
         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.8M
      blkn = 0;
171
58.8M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
172
39.9M
        compptr = cinfo->cur_comp_info[ci];
173
39.9M
        blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width :
174
39.9M
                                                  compptr->last_col_width;
175
39.9M
        xpos = MCU_col_num * compptr->MCU_sample_width;
176
39.9M
        ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
177
85.1M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
178
45.1M
          if (coef->iMCU_row_num < last_iMCU_row ||
179
43.4M
              yoffset + yindex < compptr->last_row_height) {
180
43.4M
            (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
181
43.4M
                                          input_buf[compptr->component_index],
182
43.4M
                                          coef->MCU_buffer[blkn],
183
43.4M
                                          ypos, xpos, (JDIMENSION)blockcnt);
184
43.4M
            if (blockcnt < compptr->MCU_width) {
185
              /* Create some dummy blocks at the right edge of the image. */
186
4.78M
              jzero_far((void *)coef->MCU_buffer[blkn + blockcnt],
187
4.78M
                        (compptr->MCU_width - blockcnt) * sizeof(JBLOCK));
188
10.0M
              for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
189
5.22M
                coef->MCU_buffer[blkn + bi][0][0] =
190
5.22M
                  coef->MCU_buffer[blkn + bi - 1][0][0];
191
5.22M
              }
192
4.78M
            }
193
43.4M
          } else {
194
            /* Create a row of dummy blocks at the bottom of the image. */
195
1.72M
            jzero_far((void *)coef->MCU_buffer[blkn],
196
1.72M
                      compptr->MCU_width * sizeof(JBLOCK));
197
5.11M
            for (bi = 0; bi < compptr->MCU_width; bi++) {
198
3.39M
              coef->MCU_buffer[blkn + bi][0][0] =
199
3.39M
                coef->MCU_buffer[blkn - 1][0][0];
200
3.39M
            }
201
1.72M
          }
202
45.1M
          blkn += compptr->MCU_width;
203
45.1M
          ypos += DCTSIZE;
204
45.1M
        }
205
39.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
18.8M
      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.8M
    }
229
    /* Completed an MCU row, but perhaps not an iMCU row */
230
8.22M
    coef->mcu_ctr = 0;
231
8.22M
  }
232
  /* Completed the iMCU row, advance counters for next one */
233
8.22M
  coef->iMCU_row_num++;
234
8.22M
  start_iMCU_row(cinfo);
235
8.22M
  return TRUE;
236
8.22M
}
jccoefct-8.c:compress_data
Line
Count
Source
147
7.57M
{
148
7.57M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
149
7.57M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
150
7.57M
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
151
7.57M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
152
7.57M
  int blkn, bi, ci, yindex, yoffset, blockcnt;
153
7.57M
  JDIMENSION ypos, xpos;
154
7.57M
  jpeg_component_info *compptr;
155
156
  /* Loop to write as much as one whole iMCU row */
157
15.1M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
158
7.57M
       yoffset++) {
159
25.0M
    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.2M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
172
35.7M
        compptr = cinfo->cur_comp_info[ci];
173
35.7M
        blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width :
174
35.7M
                                                  compptr->last_col_width;
175
35.7M
        xpos = MCU_col_num * compptr->MCU_sample_width;
176
35.7M
        ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
177
75.3M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
178
39.5M
          if (coef->iMCU_row_num < last_iMCU_row ||
179
38.4M
              yoffset + yindex < compptr->last_row_height) {
180
38.4M
            (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
181
38.4M
                                          input_buf[compptr->component_index],
182
38.4M
                                          coef->MCU_buffer[blkn],
183
38.4M
                                          ypos, xpos, (JDIMENSION)blockcnt);
184
38.4M
            if (blockcnt < compptr->MCU_width) {
185
              /* Create some dummy blocks at the right edge of the image. */
186
3.49M
              jzero_far((void *)coef->MCU_buffer[blkn + blockcnt],
187
3.49M
                        (compptr->MCU_width - blockcnt) * sizeof(JBLOCK));
188
7.43M
              for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
189
3.93M
                coef->MCU_buffer[blkn + bi][0][0] =
190
3.93M
                  coef->MCU_buffer[blkn + bi - 1][0][0];
191
3.93M
              }
192
3.49M
            }
193
38.4M
          } else {
194
            /* Create a row of dummy blocks at the bottom of the image. */
195
1.15M
            jzero_far((void *)coef->MCU_buffer[blkn],
196
1.15M
                      compptr->MCU_width * sizeof(JBLOCK));
197
3.40M
            for (bi = 0; bi < compptr->MCU_width; bi++) {
198
2.24M
              coef->MCU_buffer[blkn + bi][0][0] =
199
2.24M
                coef->MCU_buffer[blkn - 1][0][0];
200
2.24M
            }
201
1.15M
          }
202
39.5M
          blkn += compptr->MCU_width;
203
39.5M
          ypos += DCTSIZE;
204
39.5M
        }
205
35.7M
      }
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.57M
    coef->mcu_ctr = 0;
231
7.57M
  }
232
  /* Completed the iMCU row, advance counters for next one */
233
7.57M
  coef->iMCU_row_num++;
234
7.57M
  start_iMCU_row(cinfo);
235
7.57M
  return TRUE;
236
7.57M
}
jccoefct-12.c:compress_data
Line
Count
Source
147
652k
{
148
652k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
149
652k
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
150
652k
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
151
652k
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
152
652k
  int blkn, bi, ci, yindex, yoffset, blockcnt;
153
652k
  JDIMENSION ypos, xpos;
154
652k
  jpeg_component_info *compptr;
155
156
  /* Loop to write as much as one whole iMCU row */
157
1.30M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
158
652k
       yoffset++) {
159
2.05M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
160
1.39M
         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.39M
      blkn = 0;
171
5.59M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
172
4.19M
        compptr = cinfo->cur_comp_info[ci];
173
4.19M
        blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width :
174
4.19M
                                                  compptr->last_col_width;
175
4.19M
        xpos = MCU_col_num * compptr->MCU_sample_width;
176
4.19M
        ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
177
9.78M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
178
5.59M
          if (coef->iMCU_row_num < last_iMCU_row ||
179
5.02M
              yoffset + yindex < compptr->last_row_height) {
180
5.02M
            (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
181
5.02M
                                          input_buf[compptr->component_index],
182
5.02M
                                          coef->MCU_buffer[blkn],
183
5.02M
                                          ypos, xpos, (JDIMENSION)blockcnt);
184
5.02M
            if (blockcnt < compptr->MCU_width) {
185
              /* Create some dummy blocks at the right edge of the image. */
186
1.29M
              jzero_far((void *)coef->MCU_buffer[blkn + blockcnt],
187
1.29M
                        (compptr->MCU_width - blockcnt) * sizeof(JBLOCK));
188
2.58M
              for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
189
1.29M
                coef->MCU_buffer[blkn + bi][0][0] =
190
1.29M
                  coef->MCU_buffer[blkn + bi - 1][0][0];
191
1.29M
              }
192
1.29M
            }
193
5.02M
          } else {
194
            /* Create a row of dummy blocks at the bottom of the image. */
195
571k
            jzero_far((void *)coef->MCU_buffer[blkn],
196
571k
                      compptr->MCU_width * sizeof(JBLOCK));
197
1.71M
            for (bi = 0; bi < compptr->MCU_width; bi++) {
198
1.14M
              coef->MCU_buffer[blkn + bi][0][0] =
199
1.14M
                coef->MCU_buffer[blkn - 1][0][0];
200
1.14M
            }
201
571k
          }
202
5.59M
          blkn += compptr->MCU_width;
203
5.59M
          ypos += DCTSIZE;
204
5.59M
        }
205
4.19M
      }
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.39M
      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.39M
    }
229
    /* Completed an MCU row, but perhaps not an iMCU row */
230
652k
    coef->mcu_ctr = 0;
231
652k
  }
232
  /* Completed the iMCU row, advance counters for next one */
233
652k
  coef->iMCU_row_num++;
234
652k
  start_iMCU_row(cinfo);
235
652k
  return TRUE;
236
652k
}
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
13.9M
{
265
13.9M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
266
13.9M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
267
13.9M
  JDIMENSION blocks_across, MCUs_across, MCUindex;
268
13.9M
  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
269
13.9M
  JCOEF lastDC;
270
13.9M
  jpeg_component_info *compptr;
271
13.9M
  JBLOCKARRAY buffer;
272
13.9M
  JBLOCKROW thisblockrow, lastblockrow;
273
274
49.4M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
275
35.5M
       ci++, compptr++) {
276
    /* Align the virtual buffer for this component. */
277
35.5M
    buffer = (*cinfo->mem->access_virt_barray)
278
35.5M
      ((j_common_ptr)cinfo, coef->whole_image[ci],
279
35.5M
       coef->iMCU_row_num * compptr->v_samp_factor,
280
35.5M
       (JDIMENSION)compptr->v_samp_factor, TRUE);
281
    /* Count non-dummy DCT block rows in this iMCU row. */
282
35.5M
    if (coef->iMCU_row_num < last_iMCU_row)
283
35.3M
      block_rows = compptr->v_samp_factor;
284
165k
    else {
285
      /* NB: can't use last_row_height here, since may not be set! */
286
165k
      block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
287
165k
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
288
165k
    }
289
35.5M
    blocks_across = compptr->width_in_blocks;
290
35.5M
    h_samp_factor = compptr->h_samp_factor;
291
    /* Count number of dummy blocks to be added at the right margin. */
292
35.5M
    ndummy = (int)(blocks_across % h_samp_factor);
293
35.5M
    if (ndummy > 0)
294
7.49M
      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
73.4M
    for (block_row = 0; block_row < block_rows; block_row++) {
299
37.9M
      thisblockrow = buffer[block_row];
300
37.9M
      (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
301
37.9M
                                    input_buf[ci], thisblockrow,
302
37.9M
                                    (JDIMENSION)(block_row * DCTSIZE),
303
37.9M
                                    (JDIMENSION)0, blocks_across);
304
37.9M
      if (ndummy > 0) {
305
        /* Create dummy blocks at the right edge of the image. */
306
7.49M
        thisblockrow += blocks_across; /* => first dummy block */
307
7.49M
        jzero_far((void *)thisblockrow, ndummy * sizeof(JBLOCK));
308
7.49M
        lastDC = thisblockrow[-1][0];
309
22.4M
        for (bi = 0; bi < ndummy; bi++) {
310
14.9M
          thisblockrow[bi][0] = lastDC;
311
14.9M
        }
312
7.49M
      }
313
37.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
35.5M
    if (coef->iMCU_row_num == last_iMCU_row) {
320
165k
      blocks_across += ndummy;  /* include lower right corner */
321
165k
      MCUs_across = blocks_across / h_samp_factor;
322
183k
      for (block_row = block_rows; block_row < compptr->v_samp_factor;
323
165k
           block_row++) {
324
17.7k
        thisblockrow = buffer[block_row];
325
17.7k
        lastblockrow = buffer[block_row - 1];
326
17.7k
        jzero_far((void *)thisblockrow,
327
17.7k
                  (size_t)(blocks_across * sizeof(JBLOCK)));
328
4.92M
        for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
329
4.90M
          lastDC = lastblockrow[h_samp_factor - 1][0];
330
9.81M
          for (bi = 0; bi < h_samp_factor; bi++) {
331
4.90M
            thisblockrow[bi][0] = lastDC;
332
4.90M
          }
333
4.90M
          thisblockrow += h_samp_factor; /* advance to next MCU in row */
334
4.90M
          lastblockrow += h_samp_factor;
335
4.90M
        }
336
17.7k
      }
337
165k
    }
338
35.5M
  }
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
13.9M
  return compress_output(cinfo, input_buf);
345
13.9M
}
jccoefct-8.c:compress_first_pass
Line
Count
Source
264
6.97M
{
265
6.97M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
266
6.97M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
267
6.97M
  JDIMENSION blocks_across, MCUs_across, MCUindex;
268
6.97M
  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
269
6.97M
  JCOEF lastDC;
270
6.97M
  jpeg_component_info *compptr;
271
6.97M
  JBLOCKARRAY buffer;
272
6.97M
  JBLOCKROW thisblockrow, lastblockrow;
273
274
25.7M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
275
18.7M
       ci++, compptr++) {
276
    /* Align the virtual buffer for this component. */
277
18.7M
    buffer = (*cinfo->mem->access_virt_barray)
278
18.7M
      ((j_common_ptr)cinfo, coef->whole_image[ci],
279
18.7M
       coef->iMCU_row_num * compptr->v_samp_factor,
280
18.7M
       (JDIMENSION)compptr->v_samp_factor, TRUE);
281
    /* Count non-dummy DCT block rows in this iMCU row. */
282
18.7M
    if (coef->iMCU_row_num < last_iMCU_row)
283
18.6M
      block_rows = compptr->v_samp_factor;
284
88.1k
    else {
285
      /* NB: can't use last_row_height here, since may not be set! */
286
88.1k
      block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
287
88.1k
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
288
88.1k
    }
289
18.7M
    blocks_across = compptr->width_in_blocks;
290
18.7M
    h_samp_factor = compptr->h_samp_factor;
291
    /* Count number of dummy blocks to be added at the right margin. */
292
18.7M
    ndummy = (int)(blocks_across % h_samp_factor);
293
18.7M
    if (ndummy > 0)
294
4.90M
      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
38.6M
    for (block_row = 0; block_row < block_rows; block_row++) {
299
19.8M
      thisblockrow = buffer[block_row];
300
19.8M
      (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
301
19.8M
                                    input_buf[ci], thisblockrow,
302
19.8M
                                    (JDIMENSION)(block_row * DCTSIZE),
303
19.8M
                                    (JDIMENSION)0, blocks_across);
304
19.8M
      if (ndummy > 0) {
305
        /* Create dummy blocks at the right edge of the image. */
306
4.90M
        thisblockrow += blocks_across; /* => first dummy block */
307
4.90M
        jzero_far((void *)thisblockrow, ndummy * sizeof(JBLOCK));
308
4.90M
        lastDC = thisblockrow[-1][0];
309
14.7M
        for (bi = 0; bi < ndummy; bi++) {
310
9.79M
          thisblockrow[bi][0] = lastDC;
311
9.79M
        }
312
4.90M
      }
313
19.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
18.7M
    if (coef->iMCU_row_num == last_iMCU_row) {
320
88.1k
      blocks_across += ndummy;  /* include lower right corner */
321
88.1k
      MCUs_across = blocks_across / h_samp_factor;
322
97.7k
      for (block_row = block_rows; block_row < compptr->v_samp_factor;
323
88.1k
           block_row++) {
324
9.55k
        thisblockrow = buffer[block_row];
325
9.55k
        lastblockrow = buffer[block_row - 1];
326
9.55k
        jzero_far((void *)thisblockrow,
327
9.55k
                  (size_t)(blocks_across * sizeof(JBLOCK)));
328
2.63M
        for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
329
2.62M
          lastDC = lastblockrow[h_samp_factor - 1][0];
330
5.25M
          for (bi = 0; bi < h_samp_factor; bi++) {
331
2.62M
            thisblockrow[bi][0] = lastDC;
332
2.62M
          }
333
2.62M
          thisblockrow += h_samp_factor; /* advance to next MCU in row */
334
2.62M
          lastblockrow += h_samp_factor;
335
2.62M
        }
336
9.55k
      }
337
88.1k
    }
338
18.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.97M
  return compress_output(cinfo, input_buf);
345
6.97M
}
jccoefct-12.c:compress_first_pass
Line
Count
Source
264
6.97M
{
265
6.97M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
266
6.97M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
267
6.97M
  JDIMENSION blocks_across, MCUs_across, MCUindex;
268
6.97M
  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
269
6.97M
  JCOEF lastDC;
270
6.97M
  jpeg_component_info *compptr;
271
6.97M
  JBLOCKARRAY buffer;
272
6.97M
  JBLOCKROW thisblockrow, lastblockrow;
273
274
23.7M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
275
16.7M
       ci++, compptr++) {
276
    /* Align the virtual buffer for this component. */
277
16.7M
    buffer = (*cinfo->mem->access_virt_barray)
278
16.7M
      ((j_common_ptr)cinfo, coef->whole_image[ci],
279
16.7M
       coef->iMCU_row_num * compptr->v_samp_factor,
280
16.7M
       (JDIMENSION)compptr->v_samp_factor, TRUE);
281
    /* Count non-dummy DCT block rows in this iMCU row. */
282
16.7M
    if (coef->iMCU_row_num < last_iMCU_row)
283
16.6M
      block_rows = compptr->v_samp_factor;
284
77.5k
    else {
285
      /* NB: can't use last_row_height here, since may not be set! */
286
77.5k
      block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
287
77.5k
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
288
77.5k
    }
289
16.7M
    blocks_across = compptr->width_in_blocks;
290
16.7M
    h_samp_factor = compptr->h_samp_factor;
291
    /* Count number of dummy blocks to be added at the right margin. */
292
16.7M
    ndummy = (int)(blocks_across % h_samp_factor);
293
16.7M
    if (ndummy > 0)
294
2.58M
      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
34.7M
    for (block_row = 0; block_row < block_rows; block_row++) {
299
18.0M
      thisblockrow = buffer[block_row];
300
18.0M
      (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
301
18.0M
                                    input_buf[ci], thisblockrow,
302
18.0M
                                    (JDIMENSION)(block_row * DCTSIZE),
303
18.0M
                                    (JDIMENSION)0, blocks_across);
304
18.0M
      if (ndummy > 0) {
305
        /* Create dummy blocks at the right edge of the image. */
306
2.58M
        thisblockrow += blocks_across; /* => first dummy block */
307
2.58M
        jzero_far((void *)thisblockrow, ndummy * sizeof(JBLOCK));
308
2.58M
        lastDC = thisblockrow[-1][0];
309
7.75M
        for (bi = 0; bi < ndummy; bi++) {
310
5.16M
          thisblockrow[bi][0] = lastDC;
311
5.16M
        }
312
2.58M
      }
313
18.0M
    }
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
16.7M
    if (coef->iMCU_row_num == last_iMCU_row) {
320
77.5k
      blocks_across += ndummy;  /* include lower right corner */
321
77.5k
      MCUs_across = blocks_across / h_samp_factor;
322
85.7k
      for (block_row = block_rows; block_row < compptr->v_samp_factor;
323
77.5k
           block_row++) {
324
8.17k
        thisblockrow = buffer[block_row];
325
8.17k
        lastblockrow = buffer[block_row - 1];
326
8.17k
        jzero_far((void *)thisblockrow,
327
8.17k
                  (size_t)(blocks_across * sizeof(JBLOCK)));
328
2.28M
        for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
329
2.27M
          lastDC = lastblockrow[h_samp_factor - 1][0];
330
4.55M
          for (bi = 0; bi < h_samp_factor; bi++) {
331
2.27M
            thisblockrow[bi][0] = lastDC;
332
2.27M
          }
333
2.27M
          thisblockrow += h_samp_factor; /* advance to next MCU in row */
334
2.27M
          lastblockrow += h_samp_factor;
335
2.27M
        }
336
8.17k
      }
337
77.5k
    }
338
16.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.97M
  return compress_output(cinfo, input_buf);
345
6.97M
}
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
153M
    compptr = cinfo->cur_comp_info[ci];
375
153M
    buffer[ci] = (*cinfo->mem->access_virt_barray)
376
153M
      ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
377
153M
       coef->iMCU_row_num * compptr->v_samp_factor,
378
153M
       (JDIMENSION)compptr->v_samp_factor, FALSE);
379
153M
  }
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
110M
       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
551M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
389
322M
        compptr = cinfo->cur_comp_info[ci];
390
322M
        start_col = MCU_col_num * compptr->MCU_width;
391
661M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
392
339M
          buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
393
726M
          for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
394
386M
            coef->MCU_buffer[blkn++] = buffer_ptr++;
395
386M
          }
396
339M
        }
397
322M
      }
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
110M
    coef->mcu_ctr = 0;
421
110M
  }
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
71.3M
{
361
71.3M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
362
71.3M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
363
71.3M
  int blkn, ci, xindex, yindex, yoffset;
364
71.3M
  JDIMENSION start_col;
365
71.3M
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
366
71.3M
  JBLOCKROW buffer_ptr;
367
71.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
171M
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
374
99.9M
    compptr = cinfo->cur_comp_info[ci];
375
99.9M
    buffer[ci] = (*cinfo->mem->access_virt_barray)
376
99.9M
      ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
377
99.9M
       coef->iMCU_row_num * compptr->v_samp_factor,
378
99.9M
       (JDIMENSION)compptr->v_samp_factor, FALSE);
379
99.9M
  }
380
381
  /* Loop to process one whole iMCU row */
382
142M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
383
71.3M
       yoffset++) {
384
204M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
385
133M
         MCU_col_num++) {
386
      /* Construct list of pointers to DCT blocks belonging to this MCU */
387
133M
      blkn = 0;                 /* index of current DCT block within MCU */
388
317M
      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
376M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
392
192M
          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
192M
        }
397
184M
      }
398
      /* Try to write the MCU. */
399
#ifdef WITH_PROFILE
400
      cinfo->master->start = getTime();
401
#endif
402
133M
      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
133M
    }
419
    /* Completed an MCU row, but perhaps not an iMCU row */
420
71.3M
    coef->mcu_ctr = 0;
421
71.3M
  }
422
  /* Completed the iMCU row, advance counters for next one */
423
71.3M
  coef->iMCU_row_num++;
424
71.3M
  start_iMCU_row(cinfo);
425
71.3M
  return TRUE;
426
71.3M
}
jccoefct-12.c:compress_output
Line
Count
Source
360
34.4M
{
361
34.4M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
362
34.4M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
363
34.4M
  int blkn, ci, xindex, yindex, yoffset;
364
34.4M
  JDIMENSION start_col;
365
34.4M
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
366
34.4M
  JBLOCKROW buffer_ptr;
367
34.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
88.3M
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
374
53.9M
    compptr = cinfo->cur_comp_info[ci];
375
53.9M
    buffer[ci] = (*cinfo->mem->access_virt_barray)
376
53.9M
      ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
377
53.9M
       coef->iMCU_row_num * compptr->v_samp_factor,
378
53.9M
       (JDIMENSION)compptr->v_samp_factor, FALSE);
379
53.9M
  }
380
381
  /* Loop to process one whole iMCU row */
382
74.0M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
383
39.6M
       yoffset++) {
384
136M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
385
96.5M
         MCU_col_num++) {
386
      /* Construct list of pointers to DCT blocks belonging to this MCU */
387
96.5M
      blkn = 0;                 /* index of current DCT block within MCU */
388
234M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
389
138M
        compptr = cinfo->cur_comp_info[ci];
390
138M
        start_col = MCU_col_num * compptr->MCU_width;
391
284M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
392
146M
          buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
393
308M
          for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
394
161M
            coef->MCU_buffer[blkn++] = buffer_ptr++;
395
161M
          }
396
146M
        }
397
138M
      }
398
      /* Try to write the MCU. */
399
#ifdef WITH_PROFILE
400
      cinfo->master->start = getTime();
401
#endif
402
96.5M
      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
96.5M
    }
419
    /* Completed an MCU row, but perhaps not an iMCU row */
420
39.6M
    coef->mcu_ctr = 0;
421
39.6M
  }
422
  /* Completed the iMCU row, advance counters for next one */
423
34.4M
  coef->iMCU_row_num++;
424
34.4M
  start_iMCU_row(cinfo);
425
34.4M
  return TRUE;
426
34.4M
}
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
156k
{
438
156k
  my_coef_ptr coef;
439
440
156k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
441
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
442
443
156k
  coef = (my_coef_ptr)
444
156k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
445
156k
                                sizeof(my_coef_controller));
446
156k
  memset(coef, 0, sizeof(my_coef_controller));
447
156k
  cinfo->coef = (struct jpeg_c_coef_controller *)coef;
448
156k
  coef->pub.start_pass = start_pass_coef;
449
450
  /* Create the coefficient buffer. */
451
156k
  if (need_full_buffer) {
452
88.9k
#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
88.9k
    int ci;
456
88.9k
    jpeg_component_info *compptr;
457
458
327k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
459
238k
         ci++, compptr++) {
460
238k
      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
461
238k
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
462
238k
         (JDIMENSION)jround_up((long)compptr->width_in_blocks,
463
238k
                               (long)compptr->h_samp_factor),
464
238k
         (JDIMENSION)jround_up((long)compptr->height_in_blocks,
465
238k
                               (long)compptr->v_samp_factor),
466
238k
         (JDIMENSION)compptr->v_samp_factor);
467
238k
    }
468
#else
469
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
470
#endif
471
88.9k
  } else {
472
    /* We only need a single-MCU buffer. */
473
67.1k
    JBLOCKROW buffer;
474
67.1k
    int i;
475
476
67.1k
    buffer = (JBLOCKROW)
477
67.1k
      (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
478
67.1k
                                  C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
479
738k
    for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
480
671k
      coef->MCU_buffer[i] = buffer + i;
481
671k
    }
482
    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
483
67.1k
  }
484
156k
}
jinit_c_coef_controller
Line
Count
Source
437
105k
{
438
105k
  my_coef_ptr coef;
439
440
105k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
441
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
442
443
105k
  coef = (my_coef_ptr)
444
105k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
445
105k
                                sizeof(my_coef_controller));
446
105k
  memset(coef, 0, sizeof(my_coef_controller));
447
105k
  cinfo->coef = (struct jpeg_c_coef_controller *)coef;
448
105k
  coef->pub.start_pass = start_pass_coef;
449
450
  /* Create the coefficient buffer. */
451
105k
  if (need_full_buffer) {
452
45.3k
#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.3k
    int ci;
456
45.3k
    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.9k
  } else {
472
    /* We only need a single-MCU buffer. */
473
59.9k
    JBLOCKROW buffer;
474
59.9k
    int i;
475
476
59.9k
    buffer = (JBLOCKROW)
477
59.9k
      (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
478
59.9k
                                  C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
479
659k
    for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
480
599k
      coef->MCU_buffer[i] = buffer + i;
481
599k
    }
482
    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
483
59.9k
  }
484
105k
}
j12init_c_coef_controller
Line
Count
Source
437
50.8k
{
438
50.8k
  my_coef_ptr coef;
439
440
50.8k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
441
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
442
443
50.8k
  coef = (my_coef_ptr)
444
50.8k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
445
50.8k
                                sizeof(my_coef_controller));
446
50.8k
  memset(coef, 0, sizeof(my_coef_controller));
447
50.8k
  cinfo->coef = (struct jpeg_c_coef_controller *)coef;
448
50.8k
  coef->pub.start_pass = start_pass_coef;
449
450
  /* Create the coefficient buffer. */
451
50.8k
  if (need_full_buffer) {
452
43.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
43.6k
    int ci;
456
43.6k
    jpeg_component_info *compptr;
457
458
152k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
459
108k
         ci++, compptr++) {
460
108k
      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
461
108k
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
462
108k
         (JDIMENSION)jround_up((long)compptr->width_in_blocks,
463
108k
                               (long)compptr->h_samp_factor),
464
108k
         (JDIMENSION)jround_up((long)compptr->height_in_blocks,
465
108k
                               (long)compptr->v_samp_factor),
466
108k
         (JDIMENSION)compptr->v_samp_factor);
467
108k
    }
468
#else
469
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
470
#endif
471
43.6k
  } else {
472
    /* We only need a single-MCU buffer. */
473
7.17k
    JBLOCKROW buffer;
474
7.17k
    int i;
475
476
7.17k
    buffer = (JBLOCKROW)
477
7.17k
      (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
478
7.17k
                                  C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
479
78.9k
    for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
480
71.7k
      coef->MCU_buffer[i] = buffer + i;
481
71.7k
    }
482
    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
483
7.17k
  }
484
50.8k
}