Coverage Report

Created: 2026-04-28 06:57

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
115M
{
78
115M
  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
115M
  if (cinfo->comps_in_scan > 1) {
85
27.7M
    coef->MCU_rows_per_iMCU_row = 1;
86
87.5M
  } else {
87
87.5M
    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
88
86.8M
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
89
775k
    else
90
775k
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
91
87.5M
  }
92
93
115M
  coef->mcu_ctr = 0;
94
115M
  coef->MCU_vert_offset = 0;
95
115M
}
jccoefct-8.c:start_iMCU_row
Line
Count
Source
77
83.7M
{
78
83.7M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
79
80
  /* In an interleaved scan, an MCU row is the same as an iMCU row.
81
   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
82
   * But at the bottom of the image, process only what's left.
83
   */
84
83.7M
  if (cinfo->comps_in_scan > 1) {
85
19.1M
    coef->MCU_rows_per_iMCU_row = 1;
86
64.6M
  } else {
87
64.6M
    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
88
64.1M
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
89
489k
    else
90
489k
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
91
64.6M
  }
92
93
83.7M
  coef->mcu_ctr = 0;
94
83.7M
  coef->MCU_vert_offset = 0;
95
83.7M
}
jccoefct-12.c:start_iMCU_row
Line
Count
Source
77
31.5M
{
78
31.5M
  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
31.5M
  if (cinfo->comps_in_scan > 1) {
85
8.67M
    coef->MCU_rows_per_iMCU_row = 1;
86
22.9M
  } else {
87
22.9M
    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
88
22.6M
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
89
286k
    else
90
286k
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
91
22.9M
  }
92
93
31.5M
  coef->mcu_ctr = 0;
94
31.5M
  coef->MCU_vert_offset = 0;
95
31.5M
}
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
514k
{
105
514k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
106
107
514k
  coef->iMCU_row_num = 0;
108
514k
  start_iMCU_row(cinfo);
109
110
514k
  switch (pass_mode) {
111
47.3k
  case JBUF_PASS_THRU:
112
47.3k
    if (coef->whole_image[0] != NULL)
113
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
114
47.3k
    coef->pub._compress_data = compress_data;
115
47.3k
    break;
116
0
#ifdef FULL_COEF_BUFFER_SUPPORTED
117
58.2k
  case JBUF_SAVE_AND_PASS:
118
58.2k
    if (coef->whole_image[0] == NULL)
119
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
120
58.2k
    coef->pub._compress_data = compress_first_pass;
121
58.2k
    break;
122
409k
  case JBUF_CRANK_DEST:
123
409k
    if (coef->whole_image[0] == NULL)
124
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
125
409k
    coef->pub._compress_data = compress_output;
126
409k
    break;
127
0
#endif
128
0
  default:
129
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
130
0
    break;
131
514k
  }
132
514k
}
jccoefct-8.c:start_pass_coef
Line
Count
Source
104
329k
{
105
329k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
106
107
329k
  coef->iMCU_row_num = 0;
108
329k
  start_iMCU_row(cinfo);
109
110
329k
  switch (pass_mode) {
111
42.7k
  case JBUF_PASS_THRU:
112
42.7k
    if (coef->whole_image[0] != NULL)
113
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
114
42.7k
    coef->pub._compress_data = compress_data;
115
42.7k
    break;
116
0
#ifdef FULL_COEF_BUFFER_SUPPORTED
117
29.3k
  case JBUF_SAVE_AND_PASS:
118
29.3k
    if (coef->whole_image[0] == NULL)
119
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
120
29.3k
    coef->pub._compress_data = compress_first_pass;
121
29.3k
    break;
122
257k
  case JBUF_CRANK_DEST:
123
257k
    if (coef->whole_image[0] == NULL)
124
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
125
257k
    coef->pub._compress_data = compress_output;
126
257k
    break;
127
0
#endif
128
0
  default:
129
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
130
0
    break;
131
329k
  }
132
329k
}
jccoefct-12.c:start_pass_coef
Line
Count
Source
104
185k
{
105
185k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
106
107
185k
  coef->iMCU_row_num = 0;
108
185k
  start_iMCU_row(cinfo);
109
110
185k
  switch (pass_mode) {
111
4.65k
  case JBUF_PASS_THRU:
112
4.65k
    if (coef->whole_image[0] != NULL)
113
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
114
4.65k
    coef->pub._compress_data = compress_data;
115
4.65k
    break;
116
0
#ifdef FULL_COEF_BUFFER_SUPPORTED
117
28.9k
  case JBUF_SAVE_AND_PASS:
118
28.9k
    if (coef->whole_image[0] == NULL)
119
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
120
28.9k
    coef->pub._compress_data = compress_first_pass;
121
28.9k
    break;
122
152k
  case JBUF_CRANK_DEST:
123
152k
    if (coef->whole_image[0] == NULL)
124
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
125
152k
    coef->pub._compress_data = compress_output;
126
152k
    break;
127
0
#endif
128
0
  default:
129
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
130
0
    break;
131
185k
  }
132
185k
}
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.46M
{
148
8.46M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
149
8.46M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
150
8.46M
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
151
8.46M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
152
8.46M
  int blkn, bi, ci, yindex, yoffset, blockcnt;
153
8.46M
  JDIMENSION ypos, xpos;
154
8.46M
  jpeg_component_info *compptr;
155
156
  /* Loop to write as much as one whole iMCU row */
157
16.9M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
158
8.46M
       yoffset++) {
159
26.9M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
160
18.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
18.4M
      blkn = 0;
171
57.6M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
172
39.1M
        compptr = cinfo->cur_comp_info[ci];
173
39.1M
        blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width :
174
39.1M
                                                  compptr->last_col_width;
175
39.1M
        xpos = MCU_col_num * compptr->MCU_sample_width;
176
39.1M
        ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
177
83.4M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
178
44.2M
          if (coef->iMCU_row_num < last_iMCU_row ||
179
42.4M
              yoffset + yindex < compptr->last_row_height) {
180
42.4M
            (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
181
42.4M
                                          input_buf[compptr->component_index],
182
42.4M
                                          coef->MCU_buffer[blkn],
183
42.4M
                                          ypos, xpos, (JDIMENSION)blockcnt);
184
42.4M
            if (blockcnt < compptr->MCU_width) {
185
              /* Create some dummy blocks at the right edge of the image. */
186
4.67M
              jzero_far((void *)coef->MCU_buffer[blkn + blockcnt],
187
4.67M
                        (compptr->MCU_width - blockcnt) * sizeof(JBLOCK));
188
9.73M
              for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
189
5.05M
                coef->MCU_buffer[blkn + bi][0][0] =
190
5.05M
                  coef->MCU_buffer[blkn + bi - 1][0][0];
191
5.05M
              }
192
4.67M
            }
193
42.4M
          } else {
194
            /* Create a row of dummy blocks at the bottom of the image. */
195
1.85M
            jzero_far((void *)coef->MCU_buffer[blkn],
196
1.85M
                      compptr->MCU_width * sizeof(JBLOCK));
197
5.49M
            for (bi = 0; bi < compptr->MCU_width; bi++) {
198
3.63M
              coef->MCU_buffer[blkn + bi][0][0] =
199
3.63M
                coef->MCU_buffer[blkn - 1][0][0];
200
3.63M
            }
201
1.85M
          }
202
44.2M
          blkn += compptr->MCU_width;
203
44.2M
          ypos += DCTSIZE;
204
44.2M
        }
205
39.1M
      }
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.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
18.4M
    }
229
    /* Completed an MCU row, but perhaps not an iMCU row */
230
8.46M
    coef->mcu_ctr = 0;
231
8.46M
  }
232
  /* Completed the iMCU row, advance counters for next one */
233
8.46M
  coef->iMCU_row_num++;
234
8.46M
  start_iMCU_row(cinfo);
235
8.46M
  return TRUE;
236
8.46M
}
jccoefct-8.c:compress_data
Line
Count
Source
147
7.88M
{
148
7.88M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
149
7.88M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
150
7.88M
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
151
7.88M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
152
7.88M
  int blkn, bi, ci, yindex, yoffset, blockcnt;
153
7.88M
  JDIMENSION ypos, xpos;
154
7.88M
  jpeg_component_info *compptr;
155
156
  /* Loop to write as much as one whole iMCU row */
157
15.7M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
158
7.88M
       yoffset++) {
159
25.2M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
160
17.3M
         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.3M
      blkn = 0;
171
52.9M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
172
35.6M
        compptr = cinfo->cur_comp_info[ci];
173
35.6M
        blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width :
174
35.6M
                                                  compptr->last_col_width;
175
35.6M
        xpos = MCU_col_num * compptr->MCU_sample_width;
176
35.6M
        ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
177
75.2M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
178
39.5M
          if (coef->iMCU_row_num < last_iMCU_row ||
179
38.2M
              yoffset + yindex < compptr->last_row_height) {
180
38.2M
            (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
181
38.2M
                                          input_buf[compptr->component_index],
182
38.2M
                                          coef->MCU_buffer[blkn],
183
38.2M
                                          ypos, xpos, (JDIMENSION)blockcnt);
184
38.2M
            if (blockcnt < compptr->MCU_width) {
185
              /* Create some dummy blocks at the right edge of the image. */
186
3.52M
              jzero_far((void *)coef->MCU_buffer[blkn + blockcnt],
187
3.52M
                        (compptr->MCU_width - blockcnt) * sizeof(JBLOCK));
188
7.43M
              for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
189
3.90M
                coef->MCU_buffer[blkn + bi][0][0] =
190
3.90M
                  coef->MCU_buffer[blkn + bi - 1][0][0];
191
3.90M
              }
192
3.52M
            }
193
38.2M
          } else {
194
            /* Create a row of dummy blocks at the bottom of the image. */
195
1.33M
            jzero_far((void *)coef->MCU_buffer[blkn],
196
1.33M
                      compptr->MCU_width * sizeof(JBLOCK));
197
3.93M
            for (bi = 0; bi < compptr->MCU_width; bi++) {
198
2.59M
              coef->MCU_buffer[blkn + bi][0][0] =
199
2.59M
                coef->MCU_buffer[blkn - 1][0][0];
200
2.59M
            }
201
1.33M
          }
202
39.5M
          blkn += compptr->MCU_width;
203
39.5M
          ypos += DCTSIZE;
204
39.5M
        }
205
35.6M
      }
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.3M
      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.3M
    }
229
    /* Completed an MCU row, but perhaps not an iMCU row */
230
7.88M
    coef->mcu_ctr = 0;
231
7.88M
  }
232
  /* Completed the iMCU row, advance counters for next one */
233
7.88M
  coef->iMCU_row_num++;
234
7.88M
  start_iMCU_row(cinfo);
235
7.88M
  return TRUE;
236
7.88M
}
jccoefct-12.c:compress_data
Line
Count
Source
147
576k
{
148
576k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
149
576k
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
150
576k
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
151
576k
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
152
576k
  int blkn, bi, ci, yindex, yoffset, blockcnt;
153
576k
  JDIMENSION ypos, xpos;
154
576k
  jpeg_component_info *compptr;
155
156
  /* Loop to write as much as one whole iMCU row */
157
1.15M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
158
576k
       yoffset++) {
159
1.75M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
160
1.17M
         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.17M
      blkn = 0;
171
4.70M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
172
3.52M
        compptr = cinfo->cur_comp_info[ci];
173
3.52M
        blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width :
174
3.52M
                                                  compptr->last_col_width;
175
3.52M
        xpos = MCU_col_num * compptr->MCU_sample_width;
176
3.52M
        ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
177
8.23M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
178
4.70M
          if (coef->iMCU_row_num < last_iMCU_row ||
179
4.18M
              yoffset + yindex < compptr->last_row_height) {
180
4.18M
            (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
181
4.18M
                                          input_buf[compptr->component_index],
182
4.18M
                                          coef->MCU_buffer[blkn],
183
4.18M
                                          ypos, xpos, (JDIMENSION)blockcnt);
184
4.18M
            if (blockcnt < compptr->MCU_width) {
185
              /* Create some dummy blocks at the right edge of the image. */
186
1.14M
              jzero_far((void *)coef->MCU_buffer[blkn + blockcnt],
187
1.14M
                        (compptr->MCU_width - blockcnt) * sizeof(JBLOCK));
188
2.29M
              for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
189
1.14M
                coef->MCU_buffer[blkn + bi][0][0] =
190
1.14M
                  coef->MCU_buffer[blkn + bi - 1][0][0];
191
1.14M
              }
192
1.14M
            }
193
4.18M
          } else {
194
            /* Create a row of dummy blocks at the bottom of the image. */
195
518k
            jzero_far((void *)coef->MCU_buffer[blkn],
196
518k
                      compptr->MCU_width * sizeof(JBLOCK));
197
1.55M
            for (bi = 0; bi < compptr->MCU_width; bi++) {
198
1.03M
              coef->MCU_buffer[blkn + bi][0][0] =
199
1.03M
                coef->MCU_buffer[blkn - 1][0][0];
200
1.03M
            }
201
518k
          }
202
4.70M
          blkn += compptr->MCU_width;
203
4.70M
          ypos += DCTSIZE;
204
4.70M
        }
205
3.52M
      }
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.17M
      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.17M
    }
229
    /* Completed an MCU row, but perhaps not an iMCU row */
230
576k
    coef->mcu_ctr = 0;
231
576k
  }
232
  /* Completed the iMCU row, advance counters for next one */
233
576k
  coef->iMCU_row_num++;
234
576k
  start_iMCU_row(cinfo);
235
576k
  return TRUE;
236
576k
}
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.5M
{
265
13.5M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
266
13.5M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
267
13.5M
  JDIMENSION blocks_across, MCUs_across, MCUindex;
268
13.5M
  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
269
13.5M
  JCOEF lastDC;
270
13.5M
  jpeg_component_info *compptr;
271
13.5M
  JBLOCKARRAY buffer;
272
13.5M
  JBLOCKROW thisblockrow, lastblockrow;
273
274
48.2M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
275
34.7M
       ci++, compptr++) {
276
    /* Align the virtual buffer for this component. */
277
34.7M
    buffer = (*cinfo->mem->access_virt_barray)
278
34.7M
      ((j_common_ptr)cinfo, coef->whole_image[ci],
279
34.7M
       coef->iMCU_row_num * compptr->v_samp_factor,
280
34.7M
       (JDIMENSION)compptr->v_samp_factor, TRUE);
281
    /* Count non-dummy DCT block rows in this iMCU row. */
282
34.7M
    if (coef->iMCU_row_num < last_iMCU_row)
283
34.5M
      block_rows = compptr->v_samp_factor;
284
155k
    else {
285
      /* NB: can't use last_row_height here, since may not be set! */
286
155k
      block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
287
155k
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
288
155k
    }
289
34.7M
    blocks_across = compptr->width_in_blocks;
290
34.7M
    h_samp_factor = compptr->h_samp_factor;
291
    /* Count number of dummy blocks to be added at the right margin. */
292
34.7M
    ndummy = (int)(blocks_across % h_samp_factor);
293
34.7M
    if (ndummy > 0)
294
7.52M
      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
71.8M
    for (block_row = 0; block_row < block_rows; block_row++) {
299
37.1M
      thisblockrow = buffer[block_row];
300
37.1M
      (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
301
37.1M
                                    input_buf[ci], thisblockrow,
302
37.1M
                                    (JDIMENSION)(block_row * DCTSIZE),
303
37.1M
                                    (JDIMENSION)0, blocks_across);
304
37.1M
      if (ndummy > 0) {
305
        /* Create dummy blocks at the right edge of the image. */
306
7.52M
        thisblockrow += blocks_across; /* => first dummy block */
307
7.52M
        jzero_far((void *)thisblockrow, ndummy * sizeof(JBLOCK));
308
7.52M
        lastDC = thisblockrow[-1][0];
309
22.5M
        for (bi = 0; bi < ndummy; bi++) {
310
15.0M
          thisblockrow[bi][0] = lastDC;
311
15.0M
        }
312
7.52M
      }
313
37.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
34.7M
    if (coef->iMCU_row_num == last_iMCU_row) {
320
155k
      blocks_across += ndummy;  /* include lower right corner */
321
155k
      MCUs_across = blocks_across / h_samp_factor;
322
172k
      for (block_row = block_rows; block_row < compptr->v_samp_factor;
323
155k
           block_row++) {
324
16.2k
        thisblockrow = buffer[block_row];
325
16.2k
        lastblockrow = buffer[block_row - 1];
326
16.2k
        jzero_far((void *)thisblockrow,
327
16.2k
                  (size_t)(blocks_across * sizeof(JBLOCK)));
328
4.52M
        for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
329
4.50M
          lastDC = lastblockrow[h_samp_factor - 1][0];
330
9.01M
          for (bi = 0; bi < h_samp_factor; bi++) {
331
4.50M
            thisblockrow[bi][0] = lastDC;
332
4.50M
          }
333
4.50M
          thisblockrow += h_samp_factor; /* advance to next MCU in row */
334
4.50M
          lastblockrow += h_samp_factor;
335
4.50M
        }
336
16.2k
      }
337
155k
    }
338
34.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
13.5M
  return compress_output(cinfo, input_buf);
345
13.5M
}
jccoefct-8.c:compress_first_pass
Line
Count
Source
264
7.31M
{
265
7.31M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
266
7.31M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
267
7.31M
  JDIMENSION blocks_across, MCUs_across, MCUindex;
268
7.31M
  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
269
7.31M
  JCOEF lastDC;
270
7.31M
  jpeg_component_info *compptr;
271
7.31M
  JBLOCKARRAY buffer;
272
7.31M
  JBLOCKROW thisblockrow, lastblockrow;
273
274
27.2M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
275
19.8M
       ci++, compptr++) {
276
    /* Align the virtual buffer for this component. */
277
19.8M
    buffer = (*cinfo->mem->access_virt_barray)
278
19.8M
      ((j_common_ptr)cinfo, coef->whole_image[ci],
279
19.8M
       coef->iMCU_row_num * compptr->v_samp_factor,
280
19.8M
       (JDIMENSION)compptr->v_samp_factor, TRUE);
281
    /* Count non-dummy DCT block rows in this iMCU row. */
282
19.8M
    if (coef->iMCU_row_num < last_iMCU_row)
283
19.8M
      block_rows = compptr->v_samp_factor;
284
84.4k
    else {
285
      /* NB: can't use last_row_height here, since may not be set! */
286
84.4k
      block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
287
84.4k
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
288
84.4k
    }
289
19.8M
    blocks_across = compptr->width_in_blocks;
290
19.8M
    h_samp_factor = compptr->h_samp_factor;
291
    /* Count number of dummy blocks to be added at the right margin. */
292
19.8M
    ndummy = (int)(blocks_across % h_samp_factor);
293
19.8M
    if (ndummy > 0)
294
5.22M
      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
41.0M
    for (block_row = 0; block_row < block_rows; block_row++) {
299
21.1M
      thisblockrow = buffer[block_row];
300
21.1M
      (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
301
21.1M
                                    input_buf[ci], thisblockrow,
302
21.1M
                                    (JDIMENSION)(block_row * DCTSIZE),
303
21.1M
                                    (JDIMENSION)0, blocks_across);
304
21.1M
      if (ndummy > 0) {
305
        /* Create dummy blocks at the right edge of the image. */
306
5.22M
        thisblockrow += blocks_across; /* => first dummy block */
307
5.22M
        jzero_far((void *)thisblockrow, ndummy * sizeof(JBLOCK));
308
5.22M
        lastDC = thisblockrow[-1][0];
309
15.6M
        for (bi = 0; bi < ndummy; bi++) {
310
10.4M
          thisblockrow[bi][0] = lastDC;
311
10.4M
        }
312
5.22M
      }
313
21.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
19.8M
    if (coef->iMCU_row_num == last_iMCU_row) {
320
84.4k
      blocks_across += ndummy;  /* include lower right corner */
321
84.4k
      MCUs_across = blocks_across / h_samp_factor;
322
93.2k
      for (block_row = block_rows; block_row < compptr->v_samp_factor;
323
84.4k
           block_row++) {
324
8.76k
        thisblockrow = buffer[block_row];
325
8.76k
        lastblockrow = buffer[block_row - 1];
326
8.76k
        jzero_far((void *)thisblockrow,
327
8.76k
                  (size_t)(blocks_across * sizeof(JBLOCK)));
328
2.44M
        for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
329
2.43M
          lastDC = lastblockrow[h_samp_factor - 1][0];
330
4.87M
          for (bi = 0; bi < h_samp_factor; bi++) {
331
2.43M
            thisblockrow[bi][0] = lastDC;
332
2.43M
          }
333
2.43M
          thisblockrow += h_samp_factor; /* advance to next MCU in row */
334
2.43M
          lastblockrow += h_samp_factor;
335
2.43M
        }
336
8.76k
      }
337
84.4k
    }
338
19.8M
  }
339
  /* NB: compress_output will increment iMCU_row_num if successful.
340
   * A suspension return will result in redoing all the work above next time.
341
   */
342
343
  /* Emit data to the entropy encoder, sharing code with subsequent passes */
344
7.31M
  return compress_output(cinfo, input_buf);
345
7.31M
}
jccoefct-12.c:compress_first_pass
Line
Count
Source
264
6.19M
{
265
6.19M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
266
6.19M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
267
6.19M
  JDIMENSION blocks_across, MCUs_across, MCUindex;
268
6.19M
  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
269
6.19M
  JCOEF lastDC;
270
6.19M
  jpeg_component_info *compptr;
271
6.19M
  JBLOCKARRAY buffer;
272
6.19M
  JBLOCKROW thisblockrow, lastblockrow;
273
274
21.0M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
275
14.8M
       ci++, compptr++) {
276
    /* Align the virtual buffer for this component. */
277
14.8M
    buffer = (*cinfo->mem->access_virt_barray)
278
14.8M
      ((j_common_ptr)cinfo, coef->whole_image[ci],
279
14.8M
       coef->iMCU_row_num * compptr->v_samp_factor,
280
14.8M
       (JDIMENSION)compptr->v_samp_factor, TRUE);
281
    /* Count non-dummy DCT block rows in this iMCU row. */
282
14.8M
    if (coef->iMCU_row_num < last_iMCU_row)
283
14.7M
      block_rows = compptr->v_samp_factor;
284
71.4k
    else {
285
      /* NB: can't use last_row_height here, since may not be set! */
286
71.4k
      block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
287
71.4k
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
288
71.4k
    }
289
14.8M
    blocks_across = compptr->width_in_blocks;
290
14.8M
    h_samp_factor = compptr->h_samp_factor;
291
    /* Count number of dummy blocks to be added at the right margin. */
292
14.8M
    ndummy = (int)(blocks_across % h_samp_factor);
293
14.8M
    if (ndummy > 0)
294
2.29M
      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
30.8M
    for (block_row = 0; block_row < block_rows; block_row++) {
299
15.9M
      thisblockrow = buffer[block_row];
300
15.9M
      (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
301
15.9M
                                    input_buf[ci], thisblockrow,
302
15.9M
                                    (JDIMENSION)(block_row * DCTSIZE),
303
15.9M
                                    (JDIMENSION)0, blocks_across);
304
15.9M
      if (ndummy > 0) {
305
        /* Create dummy blocks at the right edge of the image. */
306
2.29M
        thisblockrow += blocks_across; /* => first dummy block */
307
2.29M
        jzero_far((void *)thisblockrow, ndummy * sizeof(JBLOCK));
308
2.29M
        lastDC = thisblockrow[-1][0];
309
6.89M
        for (bi = 0; bi < ndummy; bi++) {
310
4.59M
          thisblockrow[bi][0] = lastDC;
311
4.59M
        }
312
2.29M
      }
313
15.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
14.8M
    if (coef->iMCU_row_num == last_iMCU_row) {
320
71.4k
      blocks_across += ndummy;  /* include lower right corner */
321
71.4k
      MCUs_across = blocks_across / h_samp_factor;
322
78.9k
      for (block_row = block_rows; block_row < compptr->v_samp_factor;
323
71.4k
           block_row++) {
324
7.47k
        thisblockrow = buffer[block_row];
325
7.47k
        lastblockrow = buffer[block_row - 1];
326
7.47k
        jzero_far((void *)thisblockrow,
327
7.47k
                  (size_t)(blocks_across * sizeof(JBLOCK)));
328
2.07M
        for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
329
2.06M
          lastDC = lastblockrow[h_samp_factor - 1][0];
330
4.13M
          for (bi = 0; bi < h_samp_factor; bi++) {
331
2.06M
            thisblockrow[bi][0] = lastDC;
332
2.06M
          }
333
2.06M
          thisblockrow += h_samp_factor; /* advance to next MCU in row */
334
2.06M
          lastblockrow += h_samp_factor;
335
2.06M
        }
336
7.47k
      }
337
71.4k
    }
338
14.8M
  }
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.19M
  return compress_output(cinfo, input_buf);
345
6.19M
}
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
106M
{
361
106M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
362
106M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
363
106M
  int blkn, ci, xindex, yindex, yoffset;
364
106M
  JDIMENSION start_col;
365
106M
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
366
106M
  JBLOCKROW buffer_ptr;
367
106M
  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
260M
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
374
154M
    compptr = cinfo->cur_comp_info[ci];
375
154M
    buffer[ci] = (*cinfo->mem->access_virt_barray)
376
154M
      ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
377
154M
       coef->iMCU_row_num * compptr->v_samp_factor,
378
154M
       (JDIMENSION)compptr->v_samp_factor, FALSE);
379
154M
  }
380
381
  /* Loop to process one whole iMCU row */
382
217M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
383
110M
       yoffset++) {
384
324M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
385
213M
         MCU_col_num++) {
386
      /* Construct list of pointers to DCT blocks belonging to this MCU */
387
213M
      blkn = 0;                 /* index of current DCT block within MCU */
388
513M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
389
299M
        compptr = cinfo->cur_comp_info[ci];
390
299M
        start_col = MCU_col_num * compptr->MCU_width;
391
614M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
392
314M
          buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
393
675M
          for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
394
360M
            coef->MCU_buffer[blkn++] = buffer_ptr++;
395
360M
          }
396
314M
        }
397
299M
      }
398
      /* Try to write the MCU. */
399
#ifdef WITH_PROFILE
400
      cinfo->master->start = getTime();
401
#endif
402
213M
      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
213M
    }
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
106M
  coef->iMCU_row_num++;
424
106M
  start_iMCU_row(cinfo);
425
106M
  return TRUE;
426
106M
}
jccoefct-8.c:compress_output
Line
Count
Source
360
75.5M
{
361
75.5M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
362
75.5M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
363
75.5M
  int blkn, ci, xindex, yindex, yoffset;
364
75.5M
  JDIMENSION start_col;
365
75.5M
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
366
75.5M
  JBLOCKROW buffer_ptr;
367
75.5M
  jpeg_component_info *compptr;
368
369
  /* Align the virtual buffers for the components used in this scan.
370
   * NB: during first pass, this is safe only because the buffers will
371
   * already be aligned properly, so jmemmgr.c won't need to do any I/O.
372
   */
373
181M
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
374
106M
    compptr = cinfo->cur_comp_info[ci];
375
106M
    buffer[ci] = (*cinfo->mem->access_virt_barray)
376
106M
      ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
377
106M
       coef->iMCU_row_num * compptr->v_samp_factor,
378
106M
       (JDIMENSION)compptr->v_samp_factor, FALSE);
379
106M
  }
380
381
  /* Loop to process one whole iMCU row */
382
151M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
383
75.5M
       yoffset++) {
384
209M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
385
134M
         MCU_col_num++) {
386
      /* Construct list of pointers to DCT blocks belonging to this MCU */
387
134M
      blkn = 0;                 /* index of current DCT block within MCU */
388
319M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
389
185M
        compptr = cinfo->cur_comp_info[ci];
390
185M
        start_col = MCU_col_num * compptr->MCU_width;
391
379M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
392
194M
          buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
393
420M
          for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
394
226M
            coef->MCU_buffer[blkn++] = buffer_ptr++;
395
226M
          }
396
194M
        }
397
185M
      }
398
      /* Try to write the MCU. */
399
#ifdef WITH_PROFILE
400
      cinfo->master->start = getTime();
401
#endif
402
134M
      if (!(*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
403
        /* Suspension forced; update state counters and exit */
404
0
        coef->MCU_vert_offset = yoffset;
405
0
        coef->mcu_ctr = MCU_col_num;
406
#ifdef WITH_PROFILE
407
        cinfo->master->entropy_elapsed += getTime() - cinfo->master->start;
408
        cinfo->master->entropy_mcoeffs +=
409
          (double)cinfo->blocks_in_MCU * DCTSIZE2 / 1000000.;
410
#endif
411
0
        return FALSE;
412
0
      }
413
#ifdef WITH_PROFILE
414
      cinfo->master->entropy_elapsed += getTime() - cinfo->master->start;
415
      cinfo->master->entropy_mcoeffs +=
416
        (double)cinfo->blocks_in_MCU * DCTSIZE2 / 1000000.;
417
#endif
418
134M
    }
419
    /* Completed an MCU row, but perhaps not an iMCU row */
420
75.5M
    coef->mcu_ctr = 0;
421
75.5M
  }
422
  /* Completed the iMCU row, advance counters for next one */
423
75.5M
  coef->iMCU_row_num++;
424
75.5M
  start_iMCU_row(cinfo);
425
75.5M
  return TRUE;
426
75.5M
}
jccoefct-12.c:compress_output
Line
Count
Source
360
30.8M
{
361
30.8M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
362
30.8M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
363
30.8M
  int blkn, ci, xindex, yindex, yoffset;
364
30.8M
  JDIMENSION start_col;
365
30.8M
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
366
30.8M
  JBLOCKROW buffer_ptr;
367
30.8M
  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
78.9M
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
374
48.0M
    compptr = cinfo->cur_comp_info[ci];
375
48.0M
    buffer[ci] = (*cinfo->mem->access_virt_barray)
376
48.0M
      ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
377
48.0M
       coef->iMCU_row_num * compptr->v_samp_factor,
378
48.0M
       (JDIMENSION)compptr->v_samp_factor, FALSE);
379
48.0M
  }
380
381
  /* Loop to process one whole iMCU row */
382
66.2M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
383
35.4M
       yoffset++) {
384
115M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
385
79.6M
         MCU_col_num++) {
386
      /* Construct list of pointers to DCT blocks belonging to this MCU */
387
79.6M
      blkn = 0;                 /* index of current DCT block within MCU */
388
193M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
389
113M
        compptr = cinfo->cur_comp_info[ci];
390
113M
        start_col = MCU_col_num * compptr->MCU_width;
391
234M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
392
120M
          buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
393
254M
          for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
394
133M
            coef->MCU_buffer[blkn++] = buffer_ptr++;
395
133M
          }
396
120M
        }
397
113M
      }
398
      /* Try to write the MCU. */
399
#ifdef WITH_PROFILE
400
      cinfo->master->start = getTime();
401
#endif
402
79.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
79.6M
    }
419
    /* Completed an MCU row, but perhaps not an iMCU row */
420
35.4M
    coef->mcu_ctr = 0;
421
35.4M
  }
422
  /* Completed the iMCU row, advance counters for next one */
423
30.8M
  coef->iMCU_row_num++;
424
30.8M
  start_iMCU_row(cinfo);
425
30.8M
  return TRUE;
426
30.8M
}
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
149k
{
438
149k
  my_coef_ptr coef;
439
440
149k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
441
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
442
443
149k
  coef = (my_coef_ptr)
444
149k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
445
149k
                                sizeof(my_coef_controller));
446
149k
  memset(coef, 0, sizeof(my_coef_controller));
447
149k
  cinfo->coef = (struct jpeg_c_coef_controller *)coef;
448
149k
  coef->pub.start_pass = start_pass_coef;
449
450
  /* Create the coefficient buffer. */
451
149k
  if (need_full_buffer) {
452
84.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
84.6k
    int ci;
456
84.6k
    jpeg_component_info *compptr;
457
458
311k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
459
226k
         ci++, compptr++) {
460
226k
      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
461
226k
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
462
226k
         (JDIMENSION)jround_up((long)compptr->width_in_blocks,
463
226k
                               (long)compptr->h_samp_factor),
464
226k
         (JDIMENSION)jround_up((long)compptr->height_in_blocks,
465
226k
                               (long)compptr->v_samp_factor),
466
226k
         (JDIMENSION)compptr->v_samp_factor);
467
226k
    }
468
#else
469
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
470
#endif
471
84.6k
  } else {
472
    /* We only need a single-MCU buffer. */
473
64.6k
    JBLOCKROW buffer;
474
64.6k
    int i;
475
476
64.6k
    buffer = (JBLOCKROW)
477
64.6k
      (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
478
64.6k
                                  C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
479
711k
    for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
480
646k
      coef->MCU_buffer[i] = buffer + i;
481
646k
    }
482
    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
483
64.6k
  }
484
149k
}
jinit_c_coef_controller
Line
Count
Source
437
101k
{
438
101k
  my_coef_ptr coef;
439
440
101k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
441
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
442
443
101k
  coef = (my_coef_ptr)
444
101k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
445
101k
                                sizeof(my_coef_controller));
446
101k
  memset(coef, 0, sizeof(my_coef_controller));
447
101k
  cinfo->coef = (struct jpeg_c_coef_controller *)coef;
448
101k
  coef->pub.start_pass = start_pass_coef;
449
450
  /* Create the coefficient buffer. */
451
101k
  if (need_full_buffer) {
452
43.8k
#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.8k
    int ci;
456
43.8k
    jpeg_component_info *compptr;
457
458
168k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
459
124k
         ci++, compptr++) {
460
124k
      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
461
124k
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
462
124k
         (JDIMENSION)jround_up((long)compptr->width_in_blocks,
463
124k
                               (long)compptr->h_samp_factor),
464
124k
         (JDIMENSION)jround_up((long)compptr->height_in_blocks,
465
124k
                               (long)compptr->v_samp_factor),
466
124k
         (JDIMENSION)compptr->v_samp_factor);
467
124k
    }
468
#else
469
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
470
#endif
471
57.9k
  } else {
472
    /* We only need a single-MCU buffer. */
473
57.9k
    JBLOCKROW buffer;
474
57.9k
    int i;
475
476
57.9k
    buffer = (JBLOCKROW)
477
57.9k
      (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
478
57.9k
                                  C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
479
637k
    for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
480
579k
      coef->MCU_buffer[i] = buffer + i;
481
579k
    }
482
    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
483
57.9k
  }
484
101k
}
j12init_c_coef_controller
Line
Count
Source
437
47.4k
{
438
47.4k
  my_coef_ptr coef;
439
440
47.4k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
441
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
442
443
47.4k
  coef = (my_coef_ptr)
444
47.4k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
445
47.4k
                                sizeof(my_coef_controller));
446
47.4k
  memset(coef, 0, sizeof(my_coef_controller));
447
47.4k
  cinfo->coef = (struct jpeg_c_coef_controller *)coef;
448
47.4k
  coef->pub.start_pass = start_pass_coef;
449
450
  /* Create the coefficient buffer. */
451
47.4k
  if (need_full_buffer) {
452
40.7k
#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
40.7k
    int ci;
456
40.7k
    jpeg_component_info *compptr;
457
458
142k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
459
101k
         ci++, compptr++) {
460
101k
      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
461
101k
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
462
101k
         (JDIMENSION)jround_up((long)compptr->width_in_blocks,
463
101k
                               (long)compptr->h_samp_factor),
464
101k
         (JDIMENSION)jround_up((long)compptr->height_in_blocks,
465
101k
                               (long)compptr->v_samp_factor),
466
101k
         (JDIMENSION)compptr->v_samp_factor);
467
101k
    }
468
#else
469
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
470
#endif
471
40.7k
  } else {
472
    /* We only need a single-MCU buffer. */
473
6.68k
    JBLOCKROW buffer;
474
6.68k
    int i;
475
476
6.68k
    buffer = (JBLOCKROW)
477
6.68k
      (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
478
6.68k
                                  C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
479
73.5k
    for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
480
66.8k
      coef->MCU_buffer[i] = buffer + i;
481
66.8k
    }
482
    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
483
6.68k
  }
484
47.4k
}