Coverage Report

Created: 2026-03-12 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.main/src/jdpostct.c
Line
Count
Source
1
/*
2
 * jdpostct.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1994-1996, 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 decompression postprocessing controller.
12
 * This controller manages the upsampling, color conversion, and color
13
 * quantization/reduction steps; specifically, it controls the buffering
14
 * between upsample/color conversion and color quantization/reduction.
15
 *
16
 * If no color quantization/reduction is required, then this module has no
17
 * work to do, and it just hands off to the upsample/color conversion code.
18
 * An integrated upsample/convert/quantize process would replace this module
19
 * entirely.
20
 */
21
22
#define JPEG_INTERNALS
23
#include "jinclude.h"
24
#include "jpeglib.h"
25
#include "jsamplecomp.h"
26
27
28
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
29
30
/* Private buffer controller object */
31
32
typedef struct {
33
  struct jpeg_d_post_controller pub; /* public fields */
34
35
  /* Color quantization source buffer: this holds output data from
36
   * the upsample/color conversion step to be passed to the quantizer.
37
   * For two-pass color quantization, we need a full-image buffer;
38
   * for one-pass operation, a strip buffer is sufficient.
39
   */
40
  jvirt_sarray_ptr whole_image; /* virtual array, or NULL if one-pass */
41
  _JSAMPARRAY buffer;           /* strip buffer, or current strip of virtual */
42
  JDIMENSION strip_height;      /* buffer size in rows */
43
  /* for two-pass mode only: */
44
  JDIMENSION starting_row;      /* row # of first row in current strip */
45
  JDIMENSION next_row;          /* index of next row to fill/empty in strip */
46
} my_post_controller;
47
48
typedef my_post_controller *my_post_ptr;
49
50
51
/* Forward declarations */
52
#if BITS_IN_JSAMPLE != 16
53
METHODDEF(void) post_process_1pass(j_decompress_ptr cinfo,
54
                                   _JSAMPIMAGE input_buf,
55
                                   JDIMENSION *in_row_group_ctr,
56
                                   JDIMENSION in_row_groups_avail,
57
                                   _JSAMPARRAY output_buf,
58
                                   JDIMENSION *out_row_ctr,
59
                                   JDIMENSION out_rows_avail);
60
#endif
61
#if defined(QUANT_2PASS_SUPPORTED) && BITS_IN_JSAMPLE != 16
62
METHODDEF(void) post_process_prepass(j_decompress_ptr cinfo,
63
                                     _JSAMPIMAGE input_buf,
64
                                     JDIMENSION *in_row_group_ctr,
65
                                     JDIMENSION in_row_groups_avail,
66
                                     _JSAMPARRAY output_buf,
67
                                     JDIMENSION *out_row_ctr,
68
                                     JDIMENSION out_rows_avail);
69
METHODDEF(void) post_process_2pass(j_decompress_ptr cinfo,
70
                                   _JSAMPIMAGE input_buf,
71
                                   JDIMENSION *in_row_group_ctr,
72
                                   JDIMENSION in_row_groups_avail,
73
                                   _JSAMPARRAY output_buf,
74
                                   JDIMENSION *out_row_ctr,
75
                                   JDIMENSION out_rows_avail);
76
#endif
77
78
79
/*
80
 * Initialize for a processing pass.
81
 */
82
83
METHODDEF(void)
84
start_pass_dpost(j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
85
34.6k
{
86
34.6k
  my_post_ptr post = (my_post_ptr)cinfo->post;
87
88
34.6k
  switch (pass_mode) {
89
30.6k
  case JBUF_PASS_THRU:
90
#if BITS_IN_JSAMPLE != 16
91
30.5k
    if (cinfo->quantize_colors) {
92
      /* Single-pass processing with color quantization. */
93
17.6k
      post->pub._post_process_data = post_process_1pass;
94
      /* We could be doing buffered-image output before starting a 2-pass
95
       * color quantization; in that case, jinit_d_post_controller did not
96
       * allocate a strip buffer.  Use the virtual-array buffer as workspace.
97
       */
98
17.6k
      if (post->buffer == NULL) {
99
0
        post->buffer = (_JSAMPARRAY)(*cinfo->mem->access_virt_sarray)
100
0
          ((j_common_ptr)cinfo, post->whole_image,
101
0
           (JDIMENSION)0, post->strip_height, TRUE);
102
0
      }
103
17.6k
    } else
104
12.9k
#endif
105
12.9k
    {
106
      /* For single-pass processing without color quantization,
107
       * I have no work to do; just call the upsampler directly.
108
       */
109
12.9k
      post->pub._post_process_data = cinfo->upsample->_upsample;
110
12.9k
    }
111
30.6k
    break;
112
#if defined(QUANT_2PASS_SUPPORTED) && BITS_IN_JSAMPLE != 16
113
2.02k
  case JBUF_SAVE_AND_PASS:
114
    /* First pass of 2-pass quantization */
115
2.02k
    if (post->whole_image == NULL)
116
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
117
2.02k
    post->pub._post_process_data = post_process_prepass;
118
2.02k
    break;
119
2.00k
  case JBUF_CRANK_DEST:
120
    /* Second pass of 2-pass quantization */
121
2.00k
    if (post->whole_image == NULL)
122
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
123
2.00k
    post->pub._post_process_data = post_process_2pass;
124
2.00k
    break;
125
0
#endif /* defined(QUANT_2PASS_SUPPORTED) && BITS_IN_JSAMPLE != 16 */
126
0
  default:
127
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
128
0
    break;
129
34.6k
  }
130
34.6k
  post->starting_row = post->next_row = 0;
131
34.6k
}
jdpostct-8.c:start_pass_dpost
Line
Count
Source
85
34.5k
{
86
34.5k
  my_post_ptr post = (my_post_ptr)cinfo->post;
87
88
34.5k
  switch (pass_mode) {
89
30.5k
  case JBUF_PASS_THRU:
90
30.5k
#if BITS_IN_JSAMPLE != 16
91
30.5k
    if (cinfo->quantize_colors) {
92
      /* Single-pass processing with color quantization. */
93
17.6k
      post->pub._post_process_data = post_process_1pass;
94
      /* We could be doing buffered-image output before starting a 2-pass
95
       * color quantization; in that case, jinit_d_post_controller did not
96
       * allocate a strip buffer.  Use the virtual-array buffer as workspace.
97
       */
98
17.6k
      if (post->buffer == NULL) {
99
0
        post->buffer = (_JSAMPARRAY)(*cinfo->mem->access_virt_sarray)
100
0
          ((j_common_ptr)cinfo, post->whole_image,
101
0
           (JDIMENSION)0, post->strip_height, TRUE);
102
0
      }
103
17.6k
    } else
104
12.8k
#endif
105
12.8k
    {
106
      /* For single-pass processing without color quantization,
107
       * I have no work to do; just call the upsampler directly.
108
       */
109
12.8k
      post->pub._post_process_data = cinfo->upsample->_upsample;
110
12.8k
    }
111
30.5k
    break;
112
0
#if defined(QUANT_2PASS_SUPPORTED) && BITS_IN_JSAMPLE != 16
113
2.02k
  case JBUF_SAVE_AND_PASS:
114
    /* First pass of 2-pass quantization */
115
2.02k
    if (post->whole_image == NULL)
116
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
117
2.02k
    post->pub._post_process_data = post_process_prepass;
118
2.02k
    break;
119
2.00k
  case JBUF_CRANK_DEST:
120
    /* Second pass of 2-pass quantization */
121
2.00k
    if (post->whole_image == NULL)
122
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
123
2.00k
    post->pub._post_process_data = post_process_2pass;
124
2.00k
    break;
125
0
#endif /* defined(QUANT_2PASS_SUPPORTED) && BITS_IN_JSAMPLE != 16 */
126
0
  default:
127
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
128
0
    break;
129
34.5k
  }
130
34.5k
  post->starting_row = post->next_row = 0;
131
34.5k
}
jdpostct-12.c:start_pass_dpost
Line
Count
Source
85
94
{
86
94
  my_post_ptr post = (my_post_ptr)cinfo->post;
87
88
94
  switch (pass_mode) {
89
94
  case JBUF_PASS_THRU:
90
94
#if BITS_IN_JSAMPLE != 16
91
94
    if (cinfo->quantize_colors) {
92
      /* Single-pass processing with color quantization. */
93
0
      post->pub._post_process_data = post_process_1pass;
94
      /* We could be doing buffered-image output before starting a 2-pass
95
       * color quantization; in that case, jinit_d_post_controller did not
96
       * allocate a strip buffer.  Use the virtual-array buffer as workspace.
97
       */
98
0
      if (post->buffer == NULL) {
99
0
        post->buffer = (_JSAMPARRAY)(*cinfo->mem->access_virt_sarray)
100
0
          ((j_common_ptr)cinfo, post->whole_image,
101
0
           (JDIMENSION)0, post->strip_height, TRUE);
102
0
      }
103
0
    } else
104
94
#endif
105
94
    {
106
      /* For single-pass processing without color quantization,
107
       * I have no work to do; just call the upsampler directly.
108
       */
109
94
      post->pub._post_process_data = cinfo->upsample->_upsample;
110
94
    }
111
94
    break;
112
0
#if defined(QUANT_2PASS_SUPPORTED) && BITS_IN_JSAMPLE != 16
113
0
  case JBUF_SAVE_AND_PASS:
114
    /* First pass of 2-pass quantization */
115
0
    if (post->whole_image == NULL)
116
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
117
0
    post->pub._post_process_data = post_process_prepass;
118
0
    break;
119
0
  case JBUF_CRANK_DEST:
120
    /* Second pass of 2-pass quantization */
121
0
    if (post->whole_image == NULL)
122
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
123
0
    post->pub._post_process_data = post_process_2pass;
124
0
    break;
125
0
#endif /* defined(QUANT_2PASS_SUPPORTED) && BITS_IN_JSAMPLE != 16 */
126
0
  default:
127
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
128
0
    break;
129
94
  }
130
94
  post->starting_row = post->next_row = 0;
131
94
}
jdpostct-16.c:start_pass_dpost
Line
Count
Source
85
6
{
86
6
  my_post_ptr post = (my_post_ptr)cinfo->post;
87
88
6
  switch (pass_mode) {
89
6
  case JBUF_PASS_THRU:
90
#if BITS_IN_JSAMPLE != 16
91
    if (cinfo->quantize_colors) {
92
      /* Single-pass processing with color quantization. */
93
      post->pub._post_process_data = post_process_1pass;
94
      /* We could be doing buffered-image output before starting a 2-pass
95
       * color quantization; in that case, jinit_d_post_controller did not
96
       * allocate a strip buffer.  Use the virtual-array buffer as workspace.
97
       */
98
      if (post->buffer == NULL) {
99
        post->buffer = (_JSAMPARRAY)(*cinfo->mem->access_virt_sarray)
100
          ((j_common_ptr)cinfo, post->whole_image,
101
           (JDIMENSION)0, post->strip_height, TRUE);
102
      }
103
    } else
104
#endif
105
6
    {
106
      /* For single-pass processing without color quantization,
107
       * I have no work to do; just call the upsampler directly.
108
       */
109
6
      post->pub._post_process_data = cinfo->upsample->_upsample;
110
6
    }
111
6
    break;
112
#if defined(QUANT_2PASS_SUPPORTED) && BITS_IN_JSAMPLE != 16
113
  case JBUF_SAVE_AND_PASS:
114
    /* First pass of 2-pass quantization */
115
    if (post->whole_image == NULL)
116
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
117
    post->pub._post_process_data = post_process_prepass;
118
    break;
119
  case JBUF_CRANK_DEST:
120
    /* Second pass of 2-pass quantization */
121
    if (post->whole_image == NULL)
122
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
123
    post->pub._post_process_data = post_process_2pass;
124
    break;
125
#endif /* defined(QUANT_2PASS_SUPPORTED) && BITS_IN_JSAMPLE != 16 */
126
0
  default:
127
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
128
0
    break;
129
6
  }
130
6
  post->starting_row = post->next_row = 0;
131
6
}
132
133
134
/*
135
 * Process some data in the one-pass (strip buffer) case.
136
 * This is used for color precision reduction as well as one-pass quantization.
137
 */
138
139
#if BITS_IN_JSAMPLE != 16
140
141
METHODDEF(void)
142
post_process_1pass(j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
143
                   JDIMENSION *in_row_group_ctr,
144
                   JDIMENSION in_row_groups_avail, _JSAMPARRAY output_buf,
145
                   JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
146
81.8M
{
147
81.8M
  my_post_ptr post = (my_post_ptr)cinfo->post;
148
81.8M
  JDIMENSION num_rows, max_rows;
149
150
  /* Fill the buffer, but not more than what we can dump out in one go. */
151
  /* Note we rely on the upsampler to detect bottom of image. */
152
81.8M
  max_rows = out_rows_avail - *out_row_ctr;
153
81.8M
  if (max_rows > post->strip_height)
154
0
    max_rows = post->strip_height;
155
81.8M
  num_rows = 0;
156
81.8M
  (*cinfo->upsample->_upsample) (cinfo, input_buf, in_row_group_ctr,
157
81.8M
                                 in_row_groups_avail, post->buffer, &num_rows,
158
81.8M
                                 max_rows);
159
  /* Quantize and emit data. */
160
81.8M
  (*cinfo->cquantize->_color_quantize) (cinfo, post->buffer,
161
81.8M
                                        output_buf + *out_row_ctr,
162
81.8M
                                        (int)num_rows);
163
81.8M
  *out_row_ctr += num_rows;
164
81.8M
}
jdpostct-8.c:post_process_1pass
Line
Count
Source
146
81.8M
{
147
81.8M
  my_post_ptr post = (my_post_ptr)cinfo->post;
148
81.8M
  JDIMENSION num_rows, max_rows;
149
150
  /* Fill the buffer, but not more than what we can dump out in one go. */
151
  /* Note we rely on the upsampler to detect bottom of image. */
152
81.8M
  max_rows = out_rows_avail - *out_row_ctr;
153
81.8M
  if (max_rows > post->strip_height)
154
0
    max_rows = post->strip_height;
155
81.8M
  num_rows = 0;
156
81.8M
  (*cinfo->upsample->_upsample) (cinfo, input_buf, in_row_group_ctr,
157
81.8M
                                 in_row_groups_avail, post->buffer, &num_rows,
158
81.8M
                                 max_rows);
159
  /* Quantize and emit data. */
160
81.8M
  (*cinfo->cquantize->_color_quantize) (cinfo, post->buffer,
161
81.8M
                                        output_buf + *out_row_ctr,
162
81.8M
                                        (int)num_rows);
163
81.8M
  *out_row_ctr += num_rows;
164
81.8M
}
Unexecuted instantiation: jdpostct-12.c:post_process_1pass
165
166
#endif
167
168
169
#if defined(QUANT_2PASS_SUPPORTED) && BITS_IN_JSAMPLE != 16
170
171
/*
172
 * Process some data in the first pass of 2-pass quantization.
173
 */
174
175
METHODDEF(void)
176
post_process_prepass(j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
177
                     JDIMENSION *in_row_group_ctr,
178
                     JDIMENSION in_row_groups_avail, _JSAMPARRAY output_buf,
179
                     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
180
28.3M
{
181
28.3M
  my_post_ptr post = (my_post_ptr)cinfo->post;
182
28.3M
  JDIMENSION old_next_row, num_rows;
183
184
  /* Reposition virtual buffer if at start of strip. */
185
28.3M
  if (post->next_row == 0) {
186
28.3M
    post->buffer = (_JSAMPARRAY)(*cinfo->mem->access_virt_sarray)
187
28.3M
        ((j_common_ptr)cinfo, post->whole_image,
188
28.3M
         post->starting_row, post->strip_height, TRUE);
189
28.3M
  }
190
191
  /* Upsample some data (up to a strip height's worth). */
192
28.3M
  old_next_row = post->next_row;
193
28.3M
  (*cinfo->upsample->_upsample) (cinfo, input_buf, in_row_group_ctr,
194
28.3M
                                 in_row_groups_avail, post->buffer,
195
28.3M
                                 &post->next_row, post->strip_height);
196
197
  /* Allow quantizer to scan new data.  No data is emitted, */
198
  /* but we advance out_row_ctr so outer loop can tell when we're done. */
199
28.3M
  if (post->next_row > old_next_row) {
200
28.3M
    num_rows = post->next_row - old_next_row;
201
28.3M
    (*cinfo->cquantize->_color_quantize) (cinfo, post->buffer + old_next_row,
202
28.3M
                                          (_JSAMPARRAY)NULL, (int)num_rows);
203
28.3M
    *out_row_ctr += num_rows;
204
28.3M
  }
205
206
  /* Advance if we filled the strip. */
207
28.3M
  if (post->next_row >= post->strip_height) {
208
28.3M
    post->starting_row += post->strip_height;
209
28.3M
    post->next_row = 0;
210
28.3M
  }
211
28.3M
}
jdpostct-8.c:post_process_prepass
Line
Count
Source
180
28.3M
{
181
28.3M
  my_post_ptr post = (my_post_ptr)cinfo->post;
182
28.3M
  JDIMENSION old_next_row, num_rows;
183
184
  /* Reposition virtual buffer if at start of strip. */
185
28.3M
  if (post->next_row == 0) {
186
28.3M
    post->buffer = (_JSAMPARRAY)(*cinfo->mem->access_virt_sarray)
187
28.3M
        ((j_common_ptr)cinfo, post->whole_image,
188
28.3M
         post->starting_row, post->strip_height, TRUE);
189
28.3M
  }
190
191
  /* Upsample some data (up to a strip height's worth). */
192
28.3M
  old_next_row = post->next_row;
193
28.3M
  (*cinfo->upsample->_upsample) (cinfo, input_buf, in_row_group_ctr,
194
28.3M
                                 in_row_groups_avail, post->buffer,
195
28.3M
                                 &post->next_row, post->strip_height);
196
197
  /* Allow quantizer to scan new data.  No data is emitted, */
198
  /* but we advance out_row_ctr so outer loop can tell when we're done. */
199
28.3M
  if (post->next_row > old_next_row) {
200
28.3M
    num_rows = post->next_row - old_next_row;
201
28.3M
    (*cinfo->cquantize->_color_quantize) (cinfo, post->buffer + old_next_row,
202
28.3M
                                          (_JSAMPARRAY)NULL, (int)num_rows);
203
28.3M
    *out_row_ctr += num_rows;
204
28.3M
  }
205
206
  /* Advance if we filled the strip. */
207
28.3M
  if (post->next_row >= post->strip_height) {
208
28.3M
    post->starting_row += post->strip_height;
209
28.3M
    post->next_row = 0;
210
28.3M
  }
211
28.3M
}
Unexecuted instantiation: jdpostct-12.c:post_process_prepass
212
213
214
/*
215
 * Process some data in the second pass of 2-pass quantization.
216
 */
217
218
METHODDEF(void)
219
post_process_2pass(j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
220
                   JDIMENSION *in_row_group_ctr,
221
                   JDIMENSION in_row_groups_avail, _JSAMPARRAY output_buf,
222
                   JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
223
34.7M
{
224
34.7M
  my_post_ptr post = (my_post_ptr)cinfo->post;
225
34.7M
  JDIMENSION num_rows, max_rows;
226
227
  /* Reposition virtual buffer if at start of strip. */
228
34.7M
  if (post->next_row == 0) {
229
28.3M
    post->buffer = (_JSAMPARRAY)(*cinfo->mem->access_virt_sarray)
230
28.3M
        ((j_common_ptr)cinfo, post->whole_image,
231
28.3M
         post->starting_row, post->strip_height, FALSE);
232
28.3M
  }
233
234
  /* Determine number of rows to emit. */
235
34.7M
  num_rows = post->strip_height - post->next_row; /* available in strip */
236
34.7M
  max_rows = out_rows_avail - *out_row_ctr; /* available in output area */
237
34.7M
  if (num_rows > max_rows)
238
6.39M
    num_rows = max_rows;
239
  /* We have to check bottom of image here, can't depend on upsampler. */
240
34.7M
  max_rows = cinfo->output_height - post->starting_row;
241
34.7M
  if (num_rows > max_rows)
242
0
    num_rows = max_rows;
243
244
  /* Quantize and emit data. */
245
34.7M
  (*cinfo->cquantize->_color_quantize) (cinfo, post->buffer + post->next_row,
246
34.7M
                                        output_buf + *out_row_ctr,
247
34.7M
                                        (int)num_rows);
248
34.7M
  *out_row_ctr += num_rows;
249
250
  /* Advance if we filled the strip. */
251
34.7M
  post->next_row += num_rows;
252
34.7M
  if (post->next_row >= post->strip_height) {
253
28.3M
    post->starting_row += post->strip_height;
254
28.3M
    post->next_row = 0;
255
28.3M
  }
256
34.7M
}
jdpostct-8.c:post_process_2pass
Line
Count
Source
223
34.7M
{
224
34.7M
  my_post_ptr post = (my_post_ptr)cinfo->post;
225
34.7M
  JDIMENSION num_rows, max_rows;
226
227
  /* Reposition virtual buffer if at start of strip. */
228
34.7M
  if (post->next_row == 0) {
229
28.3M
    post->buffer = (_JSAMPARRAY)(*cinfo->mem->access_virt_sarray)
230
28.3M
        ((j_common_ptr)cinfo, post->whole_image,
231
28.3M
         post->starting_row, post->strip_height, FALSE);
232
28.3M
  }
233
234
  /* Determine number of rows to emit. */
235
34.7M
  num_rows = post->strip_height - post->next_row; /* available in strip */
236
34.7M
  max_rows = out_rows_avail - *out_row_ctr; /* available in output area */
237
34.7M
  if (num_rows > max_rows)
238
6.39M
    num_rows = max_rows;
239
  /* We have to check bottom of image here, can't depend on upsampler. */
240
34.7M
  max_rows = cinfo->output_height - post->starting_row;
241
34.7M
  if (num_rows > max_rows)
242
0
    num_rows = max_rows;
243
244
  /* Quantize and emit data. */
245
34.7M
  (*cinfo->cquantize->_color_quantize) (cinfo, post->buffer + post->next_row,
246
34.7M
                                        output_buf + *out_row_ctr,
247
34.7M
                                        (int)num_rows);
248
34.7M
  *out_row_ctr += num_rows;
249
250
  /* Advance if we filled the strip. */
251
34.7M
  post->next_row += num_rows;
252
34.7M
  if (post->next_row >= post->strip_height) {
253
28.3M
    post->starting_row += post->strip_height;
254
28.3M
    post->next_row = 0;
255
28.3M
  }
256
34.7M
}
Unexecuted instantiation: jdpostct-12.c:post_process_2pass
257
258
#endif /* defined(QUANT_2PASS_SUPPORTED) && BITS_IN_JSAMPLE != 16 */
259
260
261
/*
262
 * Initialize postprocessing controller.
263
 */
264
265
GLOBAL(void)
266
_jinit_d_post_controller(j_decompress_ptr cinfo, boolean need_full_buffer)
267
17.0k
{
268
17.0k
  my_post_ptr post;
269
270
17.0k
#ifdef D_LOSSLESS_SUPPORTED
271
17.0k
  if (cinfo->master->lossless) {
272
#if BITS_IN_JSAMPLE == 8
273
380
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
274
#else
275
590
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
276
590
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
277
0
#endif
278
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
279
970
  } else
280
16.1k
#endif
281
16.1k
  {
282
16.1k
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
283
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
284
16.1k
  }
285
286
17.0k
  post = (my_post_ptr)
287
17.0k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
288
17.0k
                                sizeof(my_post_controller));
289
17.0k
  cinfo->post = (struct jpeg_d_post_controller *)post;
290
17.0k
  post->pub.start_pass = start_pass_dpost;
291
17.0k
  post->whole_image = NULL;     /* flag for no virtual arrays */
292
17.0k
  post->buffer = NULL;          /* flag for no strip buffer */
293
294
  /* Create the quantization buffer, if needed */
295
17.0k
  if (cinfo->quantize_colors) {
296
#if BITS_IN_JSAMPLE != 16
297
    /* The buffer strip height is max_v_samp_factor, which is typically
298
     * an efficient number of rows for upsampling to return.
299
     * (In the presence of output rescaling, we might want to be smarter?)
300
     */
301
    post->strip_height = (JDIMENSION)cinfo->max_v_samp_factor;
302
9.87k
    if (need_full_buffer) {
303
      /* Two-pass color quantization: need full-image storage. */
304
      /* We round up the number of rows to a multiple of the strip height. */
305
2.75k
#ifdef QUANT_2PASS_SUPPORTED
306
2.75k
      post->whole_image = (*cinfo->mem->request_virt_sarray)
307
2.75k
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
308
2.75k
         cinfo->output_width * cinfo->out_color_components,
309
2.75k
         (JDIMENSION)jround_up((long)cinfo->output_height,
310
2.75k
                               (long)post->strip_height),
311
2.75k
         post->strip_height);
312
#else
313
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
314
#endif /* QUANT_2PASS_SUPPORTED */
315
7.11k
    } else {
316
      /* One-pass color quantization: just make a strip buffer. */
317
7.11k
      post->buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
318
7.11k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
319
7.11k
         cinfo->output_width * cinfo->out_color_components,
320
7.11k
         post->strip_height);
321
7.11k
    }
322
#else
323
0
    ERREXIT(cinfo, JERR_NOTIMPL);
324
#endif
325
9.87k
  }
326
17.0k
}
jinit_d_post_controller
Line
Count
Source
267
15.4k
{
268
15.4k
  my_post_ptr post;
269
270
15.4k
#ifdef D_LOSSLESS_SUPPORTED
271
15.4k
  if (cinfo->master->lossless) {
272
380
#if BITS_IN_JSAMPLE == 8
273
380
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
274
#else
275
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
276
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
277
#endif
278
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
279
380
  } else
280
15.1k
#endif
281
15.1k
  {
282
15.1k
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
283
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
284
15.1k
  }
285
286
15.4k
  post = (my_post_ptr)
287
15.4k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
288
15.4k
                                sizeof(my_post_controller));
289
15.4k
  cinfo->post = (struct jpeg_d_post_controller *)post;
290
15.4k
  post->pub.start_pass = start_pass_dpost;
291
15.4k
  post->whole_image = NULL;     /* flag for no virtual arrays */
292
15.4k
  post->buffer = NULL;          /* flag for no strip buffer */
293
294
  /* Create the quantization buffer, if needed */
295
15.4k
  if (cinfo->quantize_colors) {
296
9.33k
#if BITS_IN_JSAMPLE != 16
297
    /* The buffer strip height is max_v_samp_factor, which is typically
298
     * an efficient number of rows for upsampling to return.
299
     * (In the presence of output rescaling, we might want to be smarter?)
300
     */
301
9.33k
    post->strip_height = (JDIMENSION)cinfo->max_v_samp_factor;
302
9.33k
    if (need_full_buffer) {
303
      /* Two-pass color quantization: need full-image storage. */
304
      /* We round up the number of rows to a multiple of the strip height. */
305
2.63k
#ifdef QUANT_2PASS_SUPPORTED
306
2.63k
      post->whole_image = (*cinfo->mem->request_virt_sarray)
307
2.63k
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
308
2.63k
         cinfo->output_width * cinfo->out_color_components,
309
2.63k
         (JDIMENSION)jround_up((long)cinfo->output_height,
310
2.63k
                               (long)post->strip_height),
311
2.63k
         post->strip_height);
312
#else
313
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
314
#endif /* QUANT_2PASS_SUPPORTED */
315
6.70k
    } else {
316
      /* One-pass color quantization: just make a strip buffer. */
317
6.70k
      post->buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
318
6.70k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
319
6.70k
         cinfo->output_width * cinfo->out_color_components,
320
6.70k
         post->strip_height);
321
6.70k
    }
322
#else
323
    ERREXIT(cinfo, JERR_NOTIMPL);
324
#endif
325
9.33k
  }
326
15.4k
}
j12init_d_post_controller
Line
Count
Source
267
1.30k
{
268
1.30k
  my_post_ptr post;
269
270
1.30k
#ifdef D_LOSSLESS_SUPPORTED
271
1.30k
  if (cinfo->master->lossless) {
272
#if BITS_IN_JSAMPLE == 8
273
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
274
#else
275
297
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
276
297
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
277
0
#endif
278
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
279
297
  } else
280
1.00k
#endif
281
1.00k
  {
282
1.00k
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
283
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
284
1.00k
  }
285
286
1.30k
  post = (my_post_ptr)
287
1.30k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
288
1.30k
                                sizeof(my_post_controller));
289
1.30k
  cinfo->post = (struct jpeg_d_post_controller *)post;
290
1.30k
  post->pub.start_pass = start_pass_dpost;
291
1.30k
  post->whole_image = NULL;     /* flag for no virtual arrays */
292
1.30k
  post->buffer = NULL;          /* flag for no strip buffer */
293
294
  /* Create the quantization buffer, if needed */
295
1.30k
  if (cinfo->quantize_colors) {
296
537
#if BITS_IN_JSAMPLE != 16
297
    /* The buffer strip height is max_v_samp_factor, which is typically
298
     * an efficient number of rows for upsampling to return.
299
     * (In the presence of output rescaling, we might want to be smarter?)
300
     */
301
537
    post->strip_height = (JDIMENSION)cinfo->max_v_samp_factor;
302
537
    if (need_full_buffer) {
303
      /* Two-pass color quantization: need full-image storage. */
304
      /* We round up the number of rows to a multiple of the strip height. */
305
120
#ifdef QUANT_2PASS_SUPPORTED
306
120
      post->whole_image = (*cinfo->mem->request_virt_sarray)
307
120
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
308
120
         cinfo->output_width * cinfo->out_color_components,
309
120
         (JDIMENSION)jround_up((long)cinfo->output_height,
310
120
                               (long)post->strip_height),
311
120
         post->strip_height);
312
#else
313
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
314
#endif /* QUANT_2PASS_SUPPORTED */
315
417
    } else {
316
      /* One-pass color quantization: just make a strip buffer. */
317
417
      post->buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
318
417
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
319
417
         cinfo->output_width * cinfo->out_color_components,
320
417
         post->strip_height);
321
417
    }
322
#else
323
    ERREXIT(cinfo, JERR_NOTIMPL);
324
#endif
325
537
  }
326
1.30k
}
j16init_d_post_controller
Line
Count
Source
267
293
{
268
293
  my_post_ptr post;
269
270
293
#ifdef D_LOSSLESS_SUPPORTED
271
293
  if (cinfo->master->lossless) {
272
#if BITS_IN_JSAMPLE == 8
273
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
274
#else
275
293
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
276
293
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
277
0
#endif
278
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
279
293
  } else
280
0
#endif
281
0
  {
282
0
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
283
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
284
0
  }
285
286
293
  post = (my_post_ptr)
287
293
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
288
293
                                sizeof(my_post_controller));
289
293
  cinfo->post = (struct jpeg_d_post_controller *)post;
290
293
  post->pub.start_pass = start_pass_dpost;
291
293
  post->whole_image = NULL;     /* flag for no virtual arrays */
292
293
  post->buffer = NULL;          /* flag for no strip buffer */
293
294
  /* Create the quantization buffer, if needed */
295
293
  if (cinfo->quantize_colors) {
296
#if BITS_IN_JSAMPLE != 16
297
    /* The buffer strip height is max_v_samp_factor, which is typically
298
     * an efficient number of rows for upsampling to return.
299
     * (In the presence of output rescaling, we might want to be smarter?)
300
     */
301
    post->strip_height = (JDIMENSION)cinfo->max_v_samp_factor;
302
    if (need_full_buffer) {
303
      /* Two-pass color quantization: need full-image storage. */
304
      /* We round up the number of rows to a multiple of the strip height. */
305
#ifdef QUANT_2PASS_SUPPORTED
306
      post->whole_image = (*cinfo->mem->request_virt_sarray)
307
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
308
         cinfo->output_width * cinfo->out_color_components,
309
         (JDIMENSION)jround_up((long)cinfo->output_height,
310
                               (long)post->strip_height),
311
         post->strip_height);
312
#else
313
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
314
#endif /* QUANT_2PASS_SUPPORTED */
315
    } else {
316
      /* One-pass color quantization: just make a strip buffer. */
317
      post->buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
318
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
319
         cinfo->output_width * cinfo->out_color_components,
320
         post->strip_height);
321
    }
322
#else
323
0
    ERREXIT(cinfo, JERR_NOTIMPL);
324
0
#endif
325
0
  }
326
293
}
327
328
#endif /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */