Coverage Report

Created: 2026-05-30 07:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.3.1.x/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, 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
21
22
/* We use a full-image coefficient buffer when doing Huffman optimization,
23
 * and also for writing multiple-scan JPEG files.  In all cases, the DCT
24
 * step is run during the first pass, and subsequent passes need only read
25
 * the buffered coefficients.
26
 */
27
#ifdef ENTROPY_OPT_SUPPORTED
28
#define FULL_COEF_BUFFER_SUPPORTED
29
#else
30
#ifdef C_MULTISCAN_FILES_SUPPORTED
31
#define FULL_COEF_BUFFER_SUPPORTED
32
#endif
33
#endif
34
35
36
/* Private buffer controller object */
37
38
typedef struct {
39
  struct jpeg_c_coef_controller pub; /* public fields */
40
41
  JDIMENSION iMCU_row_num;      /* iMCU row # within image */
42
  JDIMENSION mcu_ctr;           /* counts MCUs processed in current row */
43
  int MCU_vert_offset;          /* counts MCU rows within iMCU row */
44
  int MCU_rows_per_iMCU_row;    /* number of such rows needed */
45
46
  /* For single-pass compression, it's sufficient to buffer just one MCU
47
   * (although this may prove a bit slow in practice).  We allocate a
48
   * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each
49
   * MCU constructed and sent.  In multi-pass modes, this array points to the
50
   * current MCU's blocks within the virtual arrays.
51
   */
52
  JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
53
54
  /* In multi-pass modes, we need a virtual block array for each component. */
55
  jvirt_barray_ptr whole_image[MAX_COMPONENTS];
56
} my_coef_controller;
57
58
typedef my_coef_controller *my_coef_ptr;
59
60
61
/* Forward declarations */
62
METHODDEF(boolean) compress_data(j_compress_ptr cinfo, _JSAMPIMAGE input_buf);
63
#ifdef FULL_COEF_BUFFER_SUPPORTED
64
METHODDEF(boolean) compress_first_pass(j_compress_ptr cinfo,
65
                                       _JSAMPIMAGE input_buf);
66
METHODDEF(boolean) compress_output(j_compress_ptr cinfo,
67
                                   _JSAMPIMAGE input_buf);
68
#endif
69
70
71
LOCAL(void)
72
start_iMCU_row(j_compress_ptr cinfo)
73
/* Reset within-iMCU-row counters for a new row */
74
107M
{
75
107M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
76
77
  /* In an interleaved scan, an MCU row is the same as an iMCU row.
78
   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
79
   * But at the bottom of the image, process only what's left.
80
   */
81
107M
  if (cinfo->comps_in_scan > 1) {
82
26.0M
    coef->MCU_rows_per_iMCU_row = 1;
83
81.8M
  } else {
84
81.8M
    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
85
81.0M
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
86
788k
    else
87
788k
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
88
81.8M
  }
89
90
107M
  coef->mcu_ctr = 0;
91
107M
  coef->MCU_vert_offset = 0;
92
107M
}
jccoefct-8.c:start_iMCU_row
Line
Count
Source
74
76.0M
{
75
76.0M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
76
77
  /* In an interleaved scan, an MCU row is the same as an iMCU row.
78
   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
79
   * But at the bottom of the image, process only what's left.
80
   */
81
76.0M
  if (cinfo->comps_in_scan > 1) {
82
17.3M
    coef->MCU_rows_per_iMCU_row = 1;
83
58.6M
  } else {
84
58.6M
    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
85
58.1M
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
86
496k
    else
87
496k
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
88
58.6M
  }
89
90
76.0M
  coef->mcu_ctr = 0;
91
76.0M
  coef->MCU_vert_offset = 0;
92
76.0M
}
jccoefct-12.c:start_iMCU_row
Line
Count
Source
74
31.9M
{
75
31.9M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
76
77
  /* In an interleaved scan, an MCU row is the same as an iMCU row.
78
   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
79
   * But at the bottom of the image, process only what's left.
80
   */
81
31.9M
  if (cinfo->comps_in_scan > 1) {
82
8.70M
    coef->MCU_rows_per_iMCU_row = 1;
83
23.2M
  } else {
84
23.2M
    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
85
22.9M
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
86
291k
    else
87
291k
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
88
23.2M
  }
89
90
31.9M
  coef->mcu_ctr = 0;
91
31.9M
  coef->MCU_vert_offset = 0;
92
31.9M
}
93
94
95
/*
96
 * Initialize for a processing pass.
97
 */
98
99
METHODDEF(void)
100
start_pass_coef(j_compress_ptr cinfo, J_BUF_MODE pass_mode)
101
522k
{
102
522k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
103
104
522k
  coef->iMCU_row_num = 0;
105
522k
  start_iMCU_row(cinfo);
106
107
522k
  switch (pass_mode) {
108
47.8k
  case JBUF_PASS_THRU:
109
47.8k
    if (coef->whole_image[0] != NULL)
110
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
111
47.8k
    coef->pub._compress_data = compress_data;
112
47.8k
    break;
113
0
#ifdef FULL_COEF_BUFFER_SUPPORTED
114
59.1k
  case JBUF_SAVE_AND_PASS:
115
59.1k
    if (coef->whole_image[0] == NULL)
116
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
117
59.1k
    coef->pub._compress_data = compress_first_pass;
118
59.1k
    break;
119
415k
  case JBUF_CRANK_DEST:
120
415k
    if (coef->whole_image[0] == NULL)
121
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
122
415k
    coef->pub._compress_data = compress_output;
123
415k
    break;
124
0
#endif
125
0
  default:
126
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
127
0
    break;
128
522k
  }
129
522k
}
jccoefct-8.c:start_pass_coef
Line
Count
Source
101
333k
{
102
333k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
103
104
333k
  coef->iMCU_row_num = 0;
105
333k
  start_iMCU_row(cinfo);
106
107
333k
  switch (pass_mode) {
108
43.1k
  case JBUF_PASS_THRU:
109
43.1k
    if (coef->whole_image[0] != NULL)
110
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
111
43.1k
    coef->pub._compress_data = compress_data;
112
43.1k
    break;
113
0
#ifdef FULL_COEF_BUFFER_SUPPORTED
114
29.7k
  case JBUF_SAVE_AND_PASS:
115
29.7k
    if (coef->whole_image[0] == NULL)
116
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
117
29.7k
    coef->pub._compress_data = compress_first_pass;
118
29.7k
    break;
119
261k
  case JBUF_CRANK_DEST:
120
261k
    if (coef->whole_image[0] == NULL)
121
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
122
261k
    coef->pub._compress_data = compress_output;
123
261k
    break;
124
0
#endif
125
0
  default:
126
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
127
0
    break;
128
333k
  }
129
333k
}
jccoefct-12.c:start_pass_coef
Line
Count
Source
101
188k
{
102
188k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
103
104
188k
  coef->iMCU_row_num = 0;
105
188k
  start_iMCU_row(cinfo);
106
107
188k
  switch (pass_mode) {
108
4.73k
  case JBUF_PASS_THRU:
109
4.73k
    if (coef->whole_image[0] != NULL)
110
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
111
4.73k
    coef->pub._compress_data = compress_data;
112
4.73k
    break;
113
0
#ifdef FULL_COEF_BUFFER_SUPPORTED
114
29.4k
  case JBUF_SAVE_AND_PASS:
115
29.4k
    if (coef->whole_image[0] == NULL)
116
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
117
29.4k
    coef->pub._compress_data = compress_first_pass;
118
29.4k
    break;
119
154k
  case JBUF_CRANK_DEST:
120
154k
    if (coef->whole_image[0] == NULL)
121
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
122
154k
    coef->pub._compress_data = compress_output;
123
154k
    break;
124
0
#endif
125
0
  default:
126
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
127
0
    break;
128
188k
  }
129
188k
}
130
131
132
/*
133
 * Process some data in the single-pass case.
134
 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
135
 * per call, ie, v_samp_factor block rows for each component in the image.
136
 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
137
 *
138
 * NB: input_buf contains a plane for each component in image,
139
 * which we index according to the component's SOF position.
140
 */
141
142
METHODDEF(boolean)
143
compress_data(j_compress_ptr cinfo, _JSAMPIMAGE input_buf)
144
7.82M
{
145
7.82M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
146
7.82M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
147
7.82M
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
148
7.82M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
149
7.82M
  int blkn, bi, ci, yindex, yoffset, blockcnt;
150
7.82M
  JDIMENSION ypos, xpos;
151
7.82M
  jpeg_component_info *compptr;
152
153
  /* Loop to write as much as one whole iMCU row */
154
15.6M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
155
7.82M
       yoffset++) {
156
26.4M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
157
18.6M
         MCU_col_num++) {
158
      /* Determine where data comes from in input_buf and do the DCT thing.
159
       * Each call on forward_DCT processes a horizontal row of DCT blocks
160
       * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
161
       * sequentially.  Dummy blocks at the right or bottom edge are filled in
162
       * specially.  The data in them does not matter for image reconstruction,
163
       * so we fill them with values that will encode to the smallest amount of
164
       * data, viz: all zeroes in the AC entries, DC entries equal to previous
165
       * block's DC value.  (Thanks to Thomas Kinsman for this idea.)
166
       */
167
18.6M
      blkn = 0;
168
58.0M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
169
39.4M
        compptr = cinfo->cur_comp_info[ci];
170
39.4M
        blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width :
171
39.4M
                                                  compptr->last_col_width;
172
39.4M
        xpos = MCU_col_num * compptr->MCU_sample_width;
173
39.4M
        ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
174
83.8M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
175
44.4M
          if (coef->iMCU_row_num < last_iMCU_row ||
176
42.7M
              yoffset + yindex < compptr->last_row_height) {
177
42.7M
            (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
178
42.7M
                                          input_buf[compptr->component_index],
179
42.7M
                                          coef->MCU_buffer[blkn],
180
42.7M
                                          ypos, xpos, (JDIMENSION)blockcnt);
181
42.7M
            if (blockcnt < compptr->MCU_width) {
182
              /* Create some dummy blocks at the right edge of the image. */
183
4.49M
              jzero_far((void *)coef->MCU_buffer[blkn + blockcnt],
184
4.49M
                        (compptr->MCU_width - blockcnt) * sizeof(JBLOCK));
185
9.39M
              for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
186
4.89M
                coef->MCU_buffer[blkn + bi][0][0] =
187
4.89M
                  coef->MCU_buffer[blkn + bi - 1][0][0];
188
4.89M
              }
189
4.49M
            }
190
42.7M
          } else {
191
            /* Create a row of dummy blocks at the bottom of the image. */
192
1.70M
            jzero_far((void *)coef->MCU_buffer[blkn],
193
1.70M
                      compptr->MCU_width * sizeof(JBLOCK));
194
5.04M
            for (bi = 0; bi < compptr->MCU_width; bi++) {
195
3.33M
              coef->MCU_buffer[blkn + bi][0][0] =
196
3.33M
                coef->MCU_buffer[blkn - 1][0][0];
197
3.33M
            }
198
1.70M
          }
199
44.4M
          blkn += compptr->MCU_width;
200
44.4M
          ypos += DCTSIZE;
201
44.4M
        }
202
39.4M
      }
203
      /* Try to write the MCU.  In event of a suspension failure, we will
204
       * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
205
       */
206
18.6M
      if (!(*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
207
        /* Suspension forced; update state counters and exit */
208
0
        coef->MCU_vert_offset = yoffset;
209
0
        coef->mcu_ctr = MCU_col_num;
210
0
        return FALSE;
211
0
      }
212
18.6M
    }
213
    /* Completed an MCU row, but perhaps not an iMCU row */
214
7.82M
    coef->mcu_ctr = 0;
215
7.82M
  }
216
  /* Completed the iMCU row, advance counters for next one */
217
7.82M
  coef->iMCU_row_num++;
218
7.82M
  start_iMCU_row(cinfo);
219
7.82M
  return TRUE;
220
7.82M
}
jccoefct-8.c:compress_data
Line
Count
Source
144
7.24M
{
145
7.24M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
146
7.24M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
147
7.24M
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
148
7.24M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
149
7.24M
  int blkn, bi, ci, yindex, yoffset, blockcnt;
150
7.24M
  JDIMENSION ypos, xpos;
151
7.24M
  jpeg_component_info *compptr;
152
153
  /* Loop to write as much as one whole iMCU row */
154
14.4M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
155
7.24M
       yoffset++) {
156
24.7M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
157
17.4M
         MCU_col_num++) {
158
      /* Determine where data comes from in input_buf and do the DCT thing.
159
       * Each call on forward_DCT processes a horizontal row of DCT blocks
160
       * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
161
       * sequentially.  Dummy blocks at the right or bottom edge are filled in
162
       * specially.  The data in them does not matter for image reconstruction,
163
       * so we fill them with values that will encode to the smallest amount of
164
       * data, viz: all zeroes in the AC entries, DC entries equal to previous
165
       * block's DC value.  (Thanks to Thomas Kinsman for this idea.)
166
       */
167
17.4M
      blkn = 0;
168
53.3M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
169
35.8M
        compptr = cinfo->cur_comp_info[ci];
170
35.8M
        blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width :
171
35.8M
                                                  compptr->last_col_width;
172
35.8M
        xpos = MCU_col_num * compptr->MCU_sample_width;
173
35.8M
        ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
174
75.5M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
175
39.7M
          if (coef->iMCU_row_num < last_iMCU_row ||
176
38.5M
              yoffset + yindex < compptr->last_row_height) {
177
38.5M
            (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
178
38.5M
                                          input_buf[compptr->component_index],
179
38.5M
                                          coef->MCU_buffer[blkn],
180
38.5M
                                          ypos, xpos, (JDIMENSION)blockcnt);
181
38.5M
            if (blockcnt < compptr->MCU_width) {
182
              /* Create some dummy blocks at the right edge of the image. */
183
3.34M
              jzero_far((void *)coef->MCU_buffer[blkn + blockcnt],
184
3.34M
                        (compptr->MCU_width - blockcnt) * sizeof(JBLOCK));
185
7.09M
              for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
186
3.75M
                coef->MCU_buffer[blkn + bi][0][0] =
187
3.75M
                  coef->MCU_buffer[blkn + bi - 1][0][0];
188
3.75M
              }
189
3.34M
            }
190
38.5M
          } else {
191
            /* Create a row of dummy blocks at the bottom of the image. */
192
1.20M
            jzero_far((void *)coef->MCU_buffer[blkn],
193
1.20M
                      compptr->MCU_width * sizeof(JBLOCK));
194
3.54M
            for (bi = 0; bi < compptr->MCU_width; bi++) {
195
2.33M
              coef->MCU_buffer[blkn + bi][0][0] =
196
2.33M
                coef->MCU_buffer[blkn - 1][0][0];
197
2.33M
            }
198
1.20M
          }
199
39.7M
          blkn += compptr->MCU_width;
200
39.7M
          ypos += DCTSIZE;
201
39.7M
        }
202
35.8M
      }
203
      /* Try to write the MCU.  In event of a suspension failure, we will
204
       * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
205
       */
206
17.4M
      if (!(*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
207
        /* Suspension forced; update state counters and exit */
208
0
        coef->MCU_vert_offset = yoffset;
209
0
        coef->mcu_ctr = MCU_col_num;
210
0
        return FALSE;
211
0
      }
212
17.4M
    }
213
    /* Completed an MCU row, but perhaps not an iMCU row */
214
7.24M
    coef->mcu_ctr = 0;
215
7.24M
  }
216
  /* Completed the iMCU row, advance counters for next one */
217
7.24M
  coef->iMCU_row_num++;
218
7.24M
  start_iMCU_row(cinfo);
219
7.24M
  return TRUE;
220
7.24M
}
jccoefct-12.c:compress_data
Line
Count
Source
144
578k
{
145
578k
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
146
578k
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
147
578k
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
148
578k
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
149
578k
  int blkn, bi, ci, yindex, yoffset, blockcnt;
150
578k
  JDIMENSION ypos, xpos;
151
578k
  jpeg_component_info *compptr;
152
153
  /* Loop to write as much as one whole iMCU row */
154
1.15M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
155
578k
       yoffset++) {
156
1.76M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
157
1.18M
         MCU_col_num++) {
158
      /* Determine where data comes from in input_buf and do the DCT thing.
159
       * Each call on forward_DCT processes a horizontal row of DCT blocks
160
       * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
161
       * sequentially.  Dummy blocks at the right or bottom edge are filled in
162
       * specially.  The data in them does not matter for image reconstruction,
163
       * so we fill them with values that will encode to the smallest amount of
164
       * data, viz: all zeroes in the AC entries, DC entries equal to previous
165
       * block's DC value.  (Thanks to Thomas Kinsman for this idea.)
166
       */
167
1.18M
      blkn = 0;
168
4.74M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
169
3.55M
        compptr = cinfo->cur_comp_info[ci];
170
3.55M
        blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width :
171
3.55M
                                                  compptr->last_col_width;
172
3.55M
        xpos = MCU_col_num * compptr->MCU_sample_width;
173
3.55M
        ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
174
8.30M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
175
4.74M
          if (coef->iMCU_row_num < last_iMCU_row ||
176
4.24M
              yoffset + yindex < compptr->last_row_height) {
177
4.24M
            (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
178
4.24M
                                          input_buf[compptr->component_index],
179
4.24M
                                          coef->MCU_buffer[blkn],
180
4.24M
                                          ypos, xpos, (JDIMENSION)blockcnt);
181
4.24M
            if (blockcnt < compptr->MCU_width) {
182
              /* Create some dummy blocks at the right edge of the image. */
183
1.14M
              jzero_far((void *)coef->MCU_buffer[blkn + blockcnt],
184
1.14M
                        (compptr->MCU_width - blockcnt) * sizeof(JBLOCK));
185
2.29M
              for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
186
1.14M
                coef->MCU_buffer[blkn + bi][0][0] =
187
1.14M
                  coef->MCU_buffer[blkn + bi - 1][0][0];
188
1.14M
              }
189
1.14M
            }
190
4.24M
          } else {
191
            /* Create a row of dummy blocks at the bottom of the image. */
192
501k
            jzero_far((void *)coef->MCU_buffer[blkn],
193
501k
                      compptr->MCU_width * sizeof(JBLOCK));
194
1.50M
            for (bi = 0; bi < compptr->MCU_width; bi++) {
195
1.00M
              coef->MCU_buffer[blkn + bi][0][0] =
196
1.00M
                coef->MCU_buffer[blkn - 1][0][0];
197
1.00M
            }
198
501k
          }
199
4.74M
          blkn += compptr->MCU_width;
200
4.74M
          ypos += DCTSIZE;
201
4.74M
        }
202
3.55M
      }
203
      /* Try to write the MCU.  In event of a suspension failure, we will
204
       * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
205
       */
206
1.18M
      if (!(*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
207
        /* Suspension forced; update state counters and exit */
208
0
        coef->MCU_vert_offset = yoffset;
209
0
        coef->mcu_ctr = MCU_col_num;
210
0
        return FALSE;
211
0
      }
212
1.18M
    }
213
    /* Completed an MCU row, but perhaps not an iMCU row */
214
578k
    coef->mcu_ctr = 0;
215
578k
  }
216
  /* Completed the iMCU row, advance counters for next one */
217
578k
  coef->iMCU_row_num++;
218
578k
  start_iMCU_row(cinfo);
219
578k
  return TRUE;
220
578k
}
221
222
223
#ifdef FULL_COEF_BUFFER_SUPPORTED
224
225
/*
226
 * Process some data in the first pass of a multi-pass case.
227
 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
228
 * per call, ie, v_samp_factor block rows for each component in the image.
229
 * This amount of data is read from the source buffer, DCT'd and quantized,
230
 * and saved into the virtual arrays.  We also generate suitable dummy blocks
231
 * as needed at the right and lower edges.  (The dummy blocks are constructed
232
 * in the virtual arrays, which have been padded appropriately.)  This makes
233
 * it possible for subsequent passes not to worry about real vs. dummy blocks.
234
 *
235
 * We must also emit the data to the entropy encoder.  This is conveniently
236
 * done by calling compress_output() after we've loaded the current strip
237
 * of the virtual arrays.
238
 *
239
 * NB: input_buf contains a plane for each component in image.  All
240
 * components are DCT'd and loaded into the virtual arrays in this pass.
241
 * However, it may be that only a subset of the components are emitted to
242
 * the entropy encoder during this first pass; be careful about looking
243
 * at the scan-dependent variables (MCU dimensions, etc).
244
 */
245
246
METHODDEF(boolean)
247
compress_first_pass(j_compress_ptr cinfo, _JSAMPIMAGE input_buf)
248
12.8M
{
249
12.8M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
250
12.8M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
251
12.8M
  JDIMENSION blocks_across, MCUs_across, MCUindex;
252
12.8M
  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
253
12.8M
  JCOEF lastDC;
254
12.8M
  jpeg_component_info *compptr;
255
12.8M
  JBLOCKARRAY buffer;
256
12.8M
  JBLOCKROW thisblockrow, lastblockrow;
257
258
45.8M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
259
32.9M
       ci++, compptr++) {
260
    /* Align the virtual buffer for this component. */
261
32.9M
    buffer = (*cinfo->mem->access_virt_barray)
262
32.9M
      ((j_common_ptr)cinfo, coef->whole_image[ci],
263
32.9M
       coef->iMCU_row_num * compptr->v_samp_factor,
264
32.9M
       (JDIMENSION)compptr->v_samp_factor, TRUE);
265
    /* Count non-dummy DCT block rows in this iMCU row. */
266
32.9M
    if (coef->iMCU_row_num < last_iMCU_row)
267
32.7M
      block_rows = compptr->v_samp_factor;
268
158k
    else {
269
      /* NB: can't use last_row_height here, since may not be set! */
270
158k
      block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
271
158k
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
272
158k
    }
273
32.9M
    blocks_across = compptr->width_in_blocks;
274
32.9M
    h_samp_factor = compptr->h_samp_factor;
275
    /* Count number of dummy blocks to be added at the right margin. */
276
32.9M
    ndummy = (int)(blocks_across % h_samp_factor);
277
32.9M
    if (ndummy > 0)
278
7.01M
      ndummy = h_samp_factor - ndummy;
279
    /* Perform DCT for all non-dummy blocks in this iMCU row.  Each call
280
     * on forward_DCT processes a complete horizontal row of DCT blocks.
281
     */
282
68.1M
    for (block_row = 0; block_row < block_rows; block_row++) {
283
35.1M
      thisblockrow = buffer[block_row];
284
35.1M
      (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
285
35.1M
                                    input_buf[ci], thisblockrow,
286
35.1M
                                    (JDIMENSION)(block_row * DCTSIZE),
287
35.1M
                                    (JDIMENSION)0, blocks_across);
288
35.1M
      if (ndummy > 0) {
289
        /* Create dummy blocks at the right edge of the image. */
290
7.01M
        thisblockrow += blocks_across; /* => first dummy block */
291
7.01M
        jzero_far((void *)thisblockrow, ndummy * sizeof(JBLOCK));
292
7.01M
        lastDC = thisblockrow[-1][0];
293
21.0M
        for (bi = 0; bi < ndummy; bi++) {
294
14.0M
          thisblockrow[bi][0] = lastDC;
295
14.0M
        }
296
7.01M
      }
297
35.1M
    }
298
    /* If at end of image, create dummy block rows as needed.
299
     * The tricky part here is that within each MCU, we want the DC values
300
     * of the dummy blocks to match the last real block's DC value.
301
     * This squeezes a few more bytes out of the resulting file...
302
     */
303
32.9M
    if (coef->iMCU_row_num == last_iMCU_row) {
304
158k
      blocks_across += ndummy;  /* include lower right corner */
305
158k
      MCUs_across = blocks_across / h_samp_factor;
306
175k
      for (block_row = block_rows; block_row < compptr->v_samp_factor;
307
158k
           block_row++) {
308
16.8k
        thisblockrow = buffer[block_row];
309
16.8k
        lastblockrow = buffer[block_row - 1];
310
16.8k
        jzero_far((void *)thisblockrow,
311
16.8k
                  (size_t)(blocks_across * sizeof(JBLOCK)));
312
4.52M
        for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
313
4.50M
          lastDC = lastblockrow[h_samp_factor - 1][0];
314
9.01M
          for (bi = 0; bi < h_samp_factor; bi++) {
315
4.50M
            thisblockrow[bi][0] = lastDC;
316
4.50M
          }
317
4.50M
          thisblockrow += h_samp_factor; /* advance to next MCU in row */
318
4.50M
          lastblockrow += h_samp_factor;
319
4.50M
        }
320
16.8k
      }
321
158k
    }
322
32.9M
  }
323
  /* NB: compress_output will increment iMCU_row_num if successful.
324
   * A suspension return will result in redoing all the work above next time.
325
   */
326
327
  /* Emit data to the entropy encoder, sharing code with subsequent passes */
328
12.8M
  return compress_output(cinfo, input_buf);
329
12.8M
}
jccoefct-8.c:compress_first_pass
Line
Count
Source
248
6.65M
{
249
6.65M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
250
6.65M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
251
6.65M
  JDIMENSION blocks_across, MCUs_across, MCUindex;
252
6.65M
  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
253
6.65M
  JCOEF lastDC;
254
6.65M
  jpeg_component_info *compptr;
255
6.65M
  JBLOCKARRAY buffer;
256
6.65M
  JBLOCKROW thisblockrow, lastblockrow;
257
258
24.6M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
259
18.0M
       ci++, compptr++) {
260
    /* Align the virtual buffer for this component. */
261
18.0M
    buffer = (*cinfo->mem->access_virt_barray)
262
18.0M
      ((j_common_ptr)cinfo, coef->whole_image[ci],
263
18.0M
       coef->iMCU_row_num * compptr->v_samp_factor,
264
18.0M
       (JDIMENSION)compptr->v_samp_factor, TRUE);
265
    /* Count non-dummy DCT block rows in this iMCU row. */
266
18.0M
    if (coef->iMCU_row_num < last_iMCU_row)
267
17.9M
      block_rows = compptr->v_samp_factor;
268
86.0k
    else {
269
      /* NB: can't use last_row_height here, since may not be set! */
270
86.0k
      block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
271
86.0k
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
272
86.0k
    }
273
18.0M
    blocks_across = compptr->width_in_blocks;
274
18.0M
    h_samp_factor = compptr->h_samp_factor;
275
    /* Count number of dummy blocks to be added at the right margin. */
276
18.0M
    ndummy = (int)(blocks_across % h_samp_factor);
277
18.0M
    if (ndummy > 0)
278
4.71M
      ndummy = h_samp_factor - ndummy;
279
    /* Perform DCT for all non-dummy blocks in this iMCU row.  Each call
280
     * on forward_DCT processes a complete horizontal row of DCT blocks.
281
     */
282
37.1M
    for (block_row = 0; block_row < block_rows; block_row++) {
283
19.1M
      thisblockrow = buffer[block_row];
284
19.1M
      (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
285
19.1M
                                    input_buf[ci], thisblockrow,
286
19.1M
                                    (JDIMENSION)(block_row * DCTSIZE),
287
19.1M
                                    (JDIMENSION)0, blocks_across);
288
19.1M
      if (ndummy > 0) {
289
        /* Create dummy blocks at the right edge of the image. */
290
4.71M
        thisblockrow += blocks_across; /* => first dummy block */
291
4.71M
        jzero_far((void *)thisblockrow, ndummy * sizeof(JBLOCK));
292
4.71M
        lastDC = thisblockrow[-1][0];
293
14.1M
        for (bi = 0; bi < ndummy; bi++) {
294
9.42M
          thisblockrow[bi][0] = lastDC;
295
9.42M
        }
296
4.71M
      }
297
19.1M
    }
298
    /* If at end of image, create dummy block rows as needed.
299
     * The tricky part here is that within each MCU, we want the DC values
300
     * of the dummy blocks to match the last real block's DC value.
301
     * This squeezes a few more bytes out of the resulting file...
302
     */
303
18.0M
    if (coef->iMCU_row_num == last_iMCU_row) {
304
86.0k
      blocks_across += ndummy;  /* include lower right corner */
305
86.0k
      MCUs_across = blocks_across / h_samp_factor;
306
95.2k
      for (block_row = block_rows; block_row < compptr->v_samp_factor;
307
86.0k
           block_row++) {
308
9.21k
        thisblockrow = buffer[block_row];
309
9.21k
        lastblockrow = buffer[block_row - 1];
310
9.21k
        jzero_far((void *)thisblockrow,
311
9.21k
                  (size_t)(blocks_across * sizeof(JBLOCK)));
312
2.52M
        for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
313
2.51M
          lastDC = lastblockrow[h_samp_factor - 1][0];
314
5.02M
          for (bi = 0; bi < h_samp_factor; bi++) {
315
2.51M
            thisblockrow[bi][0] = lastDC;
316
2.51M
          }
317
2.51M
          thisblockrow += h_samp_factor; /* advance to next MCU in row */
318
2.51M
          lastblockrow += h_samp_factor;
319
2.51M
        }
320
9.21k
      }
321
86.0k
    }
322
18.0M
  }
323
  /* NB: compress_output will increment iMCU_row_num if successful.
324
   * A suspension return will result in redoing all the work above next time.
325
   */
326
327
  /* Emit data to the entropy encoder, sharing code with subsequent passes */
328
6.65M
  return compress_output(cinfo, input_buf);
329
6.65M
}
jccoefct-12.c:compress_first_pass
Line
Count
Source
248
6.24M
{
249
6.24M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
250
6.24M
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
251
6.24M
  JDIMENSION blocks_across, MCUs_across, MCUindex;
252
6.24M
  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
253
6.24M
  JCOEF lastDC;
254
6.24M
  jpeg_component_info *compptr;
255
6.24M
  JBLOCKARRAY buffer;
256
6.24M
  JBLOCKROW thisblockrow, lastblockrow;
257
258
21.1M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
259
14.8M
       ci++, compptr++) {
260
    /* Align the virtual buffer for this component. */
261
14.8M
    buffer = (*cinfo->mem->access_virt_barray)
262
14.8M
      ((j_common_ptr)cinfo, coef->whole_image[ci],
263
14.8M
       coef->iMCU_row_num * compptr->v_samp_factor,
264
14.8M
       (JDIMENSION)compptr->v_samp_factor, TRUE);
265
    /* Count non-dummy DCT block rows in this iMCU row. */
266
14.8M
    if (coef->iMCU_row_num < last_iMCU_row)
267
14.8M
      block_rows = compptr->v_samp_factor;
268
72.5k
    else {
269
      /* NB: can't use last_row_height here, since may not be set! */
270
72.5k
      block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
271
72.5k
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
272
72.5k
    }
273
14.8M
    blocks_across = compptr->width_in_blocks;
274
14.8M
    h_samp_factor = compptr->h_samp_factor;
275
    /* Count number of dummy blocks to be added at the right margin. */
276
14.8M
    ndummy = (int)(blocks_across % h_samp_factor);
277
14.8M
    if (ndummy > 0)
278
2.29M
      ndummy = h_samp_factor - ndummy;
279
    /* Perform DCT for all non-dummy blocks in this iMCU row.  Each call
280
     * on forward_DCT processes a complete horizontal row of DCT blocks.
281
     */
282
30.9M
    for (block_row = 0; block_row < block_rows; block_row++) {
283
16.0M
      thisblockrow = buffer[block_row];
284
16.0M
      (*cinfo->fdct->_forward_DCT) (cinfo, compptr,
285
16.0M
                                    input_buf[ci], thisblockrow,
286
16.0M
                                    (JDIMENSION)(block_row * DCTSIZE),
287
16.0M
                                    (JDIMENSION)0, blocks_across);
288
16.0M
      if (ndummy > 0) {
289
        /* Create dummy blocks at the right edge of the image. */
290
2.29M
        thisblockrow += blocks_across; /* => first dummy block */
291
2.29M
        jzero_far((void *)thisblockrow, ndummy * sizeof(JBLOCK));
292
2.29M
        lastDC = thisblockrow[-1][0];
293
6.88M
        for (bi = 0; bi < ndummy; bi++) {
294
4.58M
          thisblockrow[bi][0] = lastDC;
295
4.58M
        }
296
2.29M
      }
297
16.0M
    }
298
    /* If at end of image, create dummy block rows as needed.
299
     * The tricky part here is that within each MCU, we want the DC values
300
     * of the dummy blocks to match the last real block's DC value.
301
     * This squeezes a few more bytes out of the resulting file...
302
     */
303
14.8M
    if (coef->iMCU_row_num == last_iMCU_row) {
304
72.5k
      blocks_across += ndummy;  /* include lower right corner */
305
72.5k
      MCUs_across = blocks_across / h_samp_factor;
306
80.1k
      for (block_row = block_rows; block_row < compptr->v_samp_factor;
307
72.5k
           block_row++) {
308
7.60k
        thisblockrow = buffer[block_row];
309
7.60k
        lastblockrow = buffer[block_row - 1];
310
7.60k
        jzero_far((void *)thisblockrow,
311
7.60k
                  (size_t)(blocks_across * sizeof(JBLOCK)));
312
2.00M
        for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
313
1.99M
          lastDC = lastblockrow[h_samp_factor - 1][0];
314
3.99M
          for (bi = 0; bi < h_samp_factor; bi++) {
315
1.99M
            thisblockrow[bi][0] = lastDC;
316
1.99M
          }
317
1.99M
          thisblockrow += h_samp_factor; /* advance to next MCU in row */
318
1.99M
          lastblockrow += h_samp_factor;
319
1.99M
        }
320
7.60k
      }
321
72.5k
    }
322
14.8M
  }
323
  /* NB: compress_output will increment iMCU_row_num if successful.
324
   * A suspension return will result in redoing all the work above next time.
325
   */
326
327
  /* Emit data to the entropy encoder, sharing code with subsequent passes */
328
6.24M
  return compress_output(cinfo, input_buf);
329
6.24M
}
330
331
332
/*
333
 * Process some data in subsequent passes of a multi-pass case.
334
 * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
335
 * per call, ie, v_samp_factor block rows for each component in the scan.
336
 * The data is obtained from the virtual arrays and fed to the entropy coder.
337
 * Returns TRUE if the iMCU row is completed, FALSE if suspended.
338
 *
339
 * NB: input_buf is ignored; it is likely to be a NULL pointer.
340
 */
341
342
METHODDEF(boolean)
343
compress_output(j_compress_ptr cinfo, _JSAMPIMAGE input_buf)
344
99.5M
{
345
99.5M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
346
99.5M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
347
99.5M
  int blkn, ci, xindex, yindex, yoffset;
348
99.5M
  JDIMENSION start_col;
349
99.5M
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
350
99.5M
  JBLOCKROW buffer_ptr;
351
99.5M
  jpeg_component_info *compptr;
352
353
  /* Align the virtual buffers for the components used in this scan.
354
   * NB: during first pass, this is safe only because the buffers will
355
   * already be aligned properly, so jmemmgr.c won't need to do any I/O.
356
   */
357
244M
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
358
144M
    compptr = cinfo->cur_comp_info[ci];
359
144M
    buffer[ci] = (*cinfo->mem->access_virt_barray)
360
144M
      ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
361
144M
       coef->iMCU_row_num * compptr->v_samp_factor,
362
144M
       (JDIMENSION)compptr->v_samp_factor, FALSE);
363
144M
  }
364
365
  /* Loop to process one whole iMCU row */
366
203M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
367
104M
       yoffset++) {
368
316M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
369
212M
         MCU_col_num++) {
370
      /* Construct list of pointers to DCT blocks belonging to this MCU */
371
212M
      blkn = 0;                 /* index of current DCT block within MCU */
372
509M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
373
296M
        compptr = cinfo->cur_comp_info[ci];
374
296M
        start_col = MCU_col_num * compptr->MCU_width;
375
609M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
376
312M
          buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
377
669M
          for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
378
356M
            coef->MCU_buffer[blkn++] = buffer_ptr++;
379
356M
          }
380
312M
        }
381
296M
      }
382
      /* Try to write the MCU. */
383
212M
      if (!(*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
384
        /* Suspension forced; update state counters and exit */
385
0
        coef->MCU_vert_offset = yoffset;
386
0
        coef->mcu_ctr = MCU_col_num;
387
0
        return FALSE;
388
0
      }
389
212M
    }
390
    /* Completed an MCU row, but perhaps not an iMCU row */
391
104M
    coef->mcu_ctr = 0;
392
104M
  }
393
  /* Completed the iMCU row, advance counters for next one */
394
99.5M
  coef->iMCU_row_num++;
395
99.5M
  start_iMCU_row(cinfo);
396
99.5M
  return TRUE;
397
99.5M
}
jccoefct-8.c:compress_output
Line
Count
Source
344
68.4M
{
345
68.4M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
346
68.4M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
347
68.4M
  int blkn, ci, xindex, yindex, yoffset;
348
68.4M
  JDIMENSION start_col;
349
68.4M
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
350
68.4M
  JBLOCKROW buffer_ptr;
351
68.4M
  jpeg_component_info *compptr;
352
353
  /* Align the virtual buffers for the components used in this scan.
354
   * NB: during first pass, this is safe only because the buffers will
355
   * already be aligned properly, so jmemmgr.c won't need to do any I/O.
356
   */
357
164M
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
358
96.0M
    compptr = cinfo->cur_comp_info[ci];
359
96.0M
    buffer[ci] = (*cinfo->mem->access_virt_barray)
360
96.0M
      ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
361
96.0M
       coef->iMCU_row_num * compptr->v_samp_factor,
362
96.0M
       (JDIMENSION)compptr->v_samp_factor, FALSE);
363
96.0M
  }
364
365
  /* Loop to process one whole iMCU row */
366
136M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
367
68.4M
       yoffset++) {
368
200M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
369
132M
         MCU_col_num++) {
370
      /* Construct list of pointers to DCT blocks belonging to this MCU */
371
132M
      blkn = 0;                 /* index of current DCT block within MCU */
372
314M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
373
181M
        compptr = cinfo->cur_comp_info[ci];
374
181M
        start_col = MCU_col_num * compptr->MCU_width;
375
372M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
376
190M
          buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
377
412M
          for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
378
221M
            coef->MCU_buffer[blkn++] = buffer_ptr++;
379
221M
          }
380
190M
        }
381
181M
      }
382
      /* Try to write the MCU. */
383
132M
      if (!(*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
384
        /* Suspension forced; update state counters and exit */
385
0
        coef->MCU_vert_offset = yoffset;
386
0
        coef->mcu_ctr = MCU_col_num;
387
0
        return FALSE;
388
0
      }
389
132M
    }
390
    /* Completed an MCU row, but perhaps not an iMCU row */
391
68.4M
    coef->mcu_ctr = 0;
392
68.4M
  }
393
  /* Completed the iMCU row, advance counters for next one */
394
68.4M
  coef->iMCU_row_num++;
395
68.4M
  start_iMCU_row(cinfo);
396
68.4M
  return TRUE;
397
68.4M
}
jccoefct-12.c:compress_output
Line
Count
Source
344
31.1M
{
345
31.1M
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
346
31.1M
  JDIMENSION MCU_col_num;       /* index of current MCU within row */
347
31.1M
  int blkn, ci, xindex, yindex, yoffset;
348
31.1M
  JDIMENSION start_col;
349
31.1M
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
350
31.1M
  JBLOCKROW buffer_ptr;
351
31.1M
  jpeg_component_info *compptr;
352
353
  /* Align the virtual buffers for the components used in this scan.
354
   * NB: during first pass, this is safe only because the buffers will
355
   * already be aligned properly, so jmemmgr.c won't need to do any I/O.
356
   */
357
79.6M
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
358
48.4M
    compptr = cinfo->cur_comp_info[ci];
359
48.4M
    buffer[ci] = (*cinfo->mem->access_virt_barray)
360
48.4M
      ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
361
48.4M
       coef->iMCU_row_num * compptr->v_samp_factor,
362
48.4M
       (JDIMENSION)compptr->v_samp_factor, FALSE);
363
48.4M
  }
364
365
  /* Loop to process one whole iMCU row */
366
66.9M
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
367
35.7M
       yoffset++) {
368
115M
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
369
80.1M
         MCU_col_num++) {
370
      /* Construct list of pointers to DCT blocks belonging to this MCU */
371
80.1M
      blkn = 0;                 /* index of current DCT block within MCU */
372
195M
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
373
114M
        compptr = cinfo->cur_comp_info[ci];
374
114M
        start_col = MCU_col_num * compptr->MCU_width;
375
236M
        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
376
122M
          buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
377
256M
          for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
378
134M
            coef->MCU_buffer[blkn++] = buffer_ptr++;
379
134M
          }
380
122M
        }
381
114M
      }
382
      /* Try to write the MCU. */
383
80.1M
      if (!(*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
384
        /* Suspension forced; update state counters and exit */
385
0
        coef->MCU_vert_offset = yoffset;
386
0
        coef->mcu_ctr = MCU_col_num;
387
0
        return FALSE;
388
0
      }
389
80.1M
    }
390
    /* Completed an MCU row, but perhaps not an iMCU row */
391
35.7M
    coef->mcu_ctr = 0;
392
35.7M
  }
393
  /* Completed the iMCU row, advance counters for next one */
394
31.1M
  coef->iMCU_row_num++;
395
31.1M
  start_iMCU_row(cinfo);
396
31.1M
  return TRUE;
397
31.1M
}
398
399
#endif /* FULL_COEF_BUFFER_SUPPORTED */
400
401
402
/*
403
 * Initialize coefficient buffer controller.
404
 */
405
406
GLOBAL(void)
407
_jinit_c_coef_controller(j_compress_ptr cinfo, boolean need_full_buffer)
408
151k
{
409
151k
  my_coef_ptr coef;
410
411
151k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
412
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
413
414
151k
  coef = (my_coef_ptr)
415
151k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
416
151k
                                sizeof(my_coef_controller));
417
151k
  memset(coef, 0, sizeof(my_coef_controller));
418
151k
  cinfo->coef = (struct jpeg_c_coef_controller *)coef;
419
151k
  coef->pub.start_pass = start_pass_coef;
420
421
  /* Create the coefficient buffer. */
422
151k
  if (need_full_buffer) {
423
85.9k
#ifdef FULL_COEF_BUFFER_SUPPORTED
424
    /* Allocate a full-image virtual array for each component, */
425
    /* padded to a multiple of samp_factor DCT blocks in each direction. */
426
85.9k
    int ci;
427
85.9k
    jpeg_component_info *compptr;
428
429
316k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
430
230k
         ci++, compptr++) {
431
230k
      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
432
230k
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
433
230k
         (JDIMENSION)jround_up((long)compptr->width_in_blocks,
434
230k
                               (long)compptr->h_samp_factor),
435
230k
         (JDIMENSION)jround_up((long)compptr->height_in_blocks,
436
230k
                               (long)compptr->v_samp_factor),
437
230k
         (JDIMENSION)compptr->v_samp_factor);
438
230k
    }
439
#else
440
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
441
#endif
442
85.9k
  } else {
443
    /* We only need a single-MCU buffer. */
444
65.4k
    JBLOCKROW buffer;
445
65.4k
    int i;
446
447
65.4k
    buffer = (JBLOCKROW)
448
65.4k
      (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
449
65.4k
                                  C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
450
720k
    for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
451
654k
      coef->MCU_buffer[i] = buffer + i;
452
654k
    }
453
    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
454
65.4k
  }
455
151k
}
jinit_c_coef_controller
Line
Count
Source
408
103k
{
409
103k
  my_coef_ptr coef;
410
411
103k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
412
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
413
414
103k
  coef = (my_coef_ptr)
415
103k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
416
103k
                                sizeof(my_coef_controller));
417
103k
  memset(coef, 0, sizeof(my_coef_controller));
418
103k
  cinfo->coef = (struct jpeg_c_coef_controller *)coef;
419
103k
  coef->pub.start_pass = start_pass_coef;
420
421
  /* Create the coefficient buffer. */
422
103k
  if (need_full_buffer) {
423
44.4k
#ifdef FULL_COEF_BUFFER_SUPPORTED
424
    /* Allocate a full-image virtual array for each component, */
425
    /* padded to a multiple of samp_factor DCT blocks in each direction. */
426
44.4k
    int ci;
427
44.4k
    jpeg_component_info *compptr;
428
429
171k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
430
127k
         ci++, compptr++) {
431
127k
      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
432
127k
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
433
127k
         (JDIMENSION)jround_up((long)compptr->width_in_blocks,
434
127k
                               (long)compptr->h_samp_factor),
435
127k
         (JDIMENSION)jround_up((long)compptr->height_in_blocks,
436
127k
                               (long)compptr->v_samp_factor),
437
127k
         (JDIMENSION)compptr->v_samp_factor);
438
127k
    }
439
#else
440
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
441
#endif
442
58.6k
  } else {
443
    /* We only need a single-MCU buffer. */
444
58.6k
    JBLOCKROW buffer;
445
58.6k
    int i;
446
447
58.6k
    buffer = (JBLOCKROW)
448
58.6k
      (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
449
58.6k
                                  C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
450
645k
    for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
451
586k
      coef->MCU_buffer[i] = buffer + i;
452
586k
    }
453
    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
454
58.6k
  }
455
103k
}
j12init_c_coef_controller
Line
Count
Source
408
48.2k
{
409
48.2k
  my_coef_ptr coef;
410
411
48.2k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
412
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
413
414
48.2k
  coef = (my_coef_ptr)
415
48.2k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
416
48.2k
                                sizeof(my_coef_controller));
417
48.2k
  memset(coef, 0, sizeof(my_coef_controller));
418
48.2k
  cinfo->coef = (struct jpeg_c_coef_controller *)coef;
419
48.2k
  coef->pub.start_pass = start_pass_coef;
420
421
  /* Create the coefficient buffer. */
422
48.2k
  if (need_full_buffer) {
423
41.4k
#ifdef FULL_COEF_BUFFER_SUPPORTED
424
    /* Allocate a full-image virtual array for each component, */
425
    /* padded to a multiple of samp_factor DCT blocks in each direction. */
426
41.4k
    int ci;
427
41.4k
    jpeg_component_info *compptr;
428
429
144k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
430
103k
         ci++, compptr++) {
431
103k
      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
432
103k
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
433
103k
         (JDIMENSION)jround_up((long)compptr->width_in_blocks,
434
103k
                               (long)compptr->h_samp_factor),
435
103k
         (JDIMENSION)jround_up((long)compptr->height_in_blocks,
436
103k
                               (long)compptr->v_samp_factor),
437
103k
         (JDIMENSION)compptr->v_samp_factor);
438
103k
    }
439
#else
440
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
441
#endif
442
41.4k
  } else {
443
    /* We only need a single-MCU buffer. */
444
6.79k
    JBLOCKROW buffer;
445
6.79k
    int i;
446
447
6.79k
    buffer = (JBLOCKROW)
448
6.79k
      (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
449
6.79k
                                  C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
450
74.7k
    for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
451
67.9k
      coef->MCU_buffer[i] = buffer + i;
452
67.9k
    }
453
    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
454
6.79k
  }
455
48.2k
}