Coverage Report

Created: 2024-01-20 12:28

/src/libjpeg-turbo.main/jdpostct.c
Line
Count
Source (jump to first uncovered line)
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, 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
METHODDEF(void) post_process_1pass(j_decompress_ptr cinfo,
53
                                   _JSAMPIMAGE input_buf,
54
                                   JDIMENSION *in_row_group_ctr,
55
                                   JDIMENSION in_row_groups_avail,
56
                                   _JSAMPARRAY output_buf,
57
                                   JDIMENSION *out_row_ctr,
58
                                   JDIMENSION out_rows_avail);
59
#ifdef QUANT_2PASS_SUPPORTED
60
METHODDEF(void) post_process_prepass(j_decompress_ptr cinfo,
61
                                     _JSAMPIMAGE input_buf,
62
                                     JDIMENSION *in_row_group_ctr,
63
                                     JDIMENSION in_row_groups_avail,
64
                                     _JSAMPARRAY output_buf,
65
                                     JDIMENSION *out_row_ctr,
66
                                     JDIMENSION out_rows_avail);
67
METHODDEF(void) post_process_2pass(j_decompress_ptr cinfo,
68
                                   _JSAMPIMAGE input_buf,
69
                                   JDIMENSION *in_row_group_ctr,
70
                                   JDIMENSION in_row_groups_avail,
71
                                   _JSAMPARRAY output_buf,
72
                                   JDIMENSION *out_row_ctr,
73
                                   JDIMENSION out_rows_avail);
74
#endif
75
76
77
/*
78
 * Initialize for a processing pass.
79
 */
80
81
METHODDEF(void)
82
start_pass_dpost(j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
83
117k
{
84
117k
  my_post_ptr post = (my_post_ptr)cinfo->post;
85
86
117k
  switch (pass_mode) {
87
117k
  case JBUF_PASS_THRU:
88
117k
    if (cinfo->quantize_colors) {
89
      /* Single-pass processing with color quantization. */
90
0
      post->pub._post_process_data = post_process_1pass;
91
      /* We could be doing buffered-image output before starting a 2-pass
92
       * color quantization; in that case, jinit_d_post_controller did not
93
       * allocate a strip buffer.  Use the virtual-array buffer as workspace.
94
       */
95
0
      if (post->buffer == NULL) {
96
0
        post->buffer = (_JSAMPARRAY)(*cinfo->mem->access_virt_sarray)
97
0
          ((j_common_ptr)cinfo, post->whole_image,
98
0
           (JDIMENSION)0, post->strip_height, TRUE);
99
0
      }
100
117k
    } else {
101
      /* For single-pass processing without color quantization,
102
       * I have no work to do; just call the upsampler directly.
103
       */
104
117k
      post->pub._post_process_data = cinfo->upsample->_upsample;
105
117k
    }
106
117k
    break;
107
0
#ifdef QUANT_2PASS_SUPPORTED
108
0
  case JBUF_SAVE_AND_PASS:
109
    /* First pass of 2-pass quantization */
110
0
    if (post->whole_image == NULL)
111
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
112
0
    post->pub._post_process_data = post_process_prepass;
113
0
    break;
114
0
  case JBUF_CRANK_DEST:
115
    /* Second pass of 2-pass quantization */
116
0
    if (post->whole_image == NULL)
117
0
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
118
0
    post->pub._post_process_data = post_process_2pass;
119
0
    break;
120
0
#endif /* QUANT_2PASS_SUPPORTED */
121
0
  default:
122
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
123
0
    break;
124
117k
  }
125
117k
  post->starting_row = post->next_row = 0;
126
117k
}
127
128
129
/*
130
 * Process some data in the one-pass (strip buffer) case.
131
 * This is used for color precision reduction as well as one-pass quantization.
132
 */
133
134
METHODDEF(void)
135
post_process_1pass(j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
136
                   JDIMENSION *in_row_group_ctr,
137
                   JDIMENSION in_row_groups_avail, _JSAMPARRAY output_buf,
138
                   JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
139
0
{
140
0
  my_post_ptr post = (my_post_ptr)cinfo->post;
141
0
  JDIMENSION num_rows, max_rows;
142
143
  /* Fill the buffer, but not more than what we can dump out in one go. */
144
  /* Note we rely on the upsampler to detect bottom of image. */
145
0
  max_rows = out_rows_avail - *out_row_ctr;
146
0
  if (max_rows > post->strip_height)
147
0
    max_rows = post->strip_height;
148
0
  num_rows = 0;
149
0
  (*cinfo->upsample->_upsample) (cinfo, input_buf, in_row_group_ctr,
150
0
                                 in_row_groups_avail, post->buffer, &num_rows,
151
0
                                 max_rows);
152
  /* Quantize and emit data. */
153
0
  (*cinfo->cquantize->_color_quantize) (cinfo, post->buffer,
154
0
                                        output_buf + *out_row_ctr,
155
0
                                        (int)num_rows);
156
0
  *out_row_ctr += num_rows;
157
0
}
158
159
160
#ifdef QUANT_2PASS_SUPPORTED
161
162
/*
163
 * Process some data in the first pass of 2-pass quantization.
164
 */
165
166
METHODDEF(void)
167
post_process_prepass(j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
168
                     JDIMENSION *in_row_group_ctr,
169
                     JDIMENSION in_row_groups_avail, _JSAMPARRAY output_buf,
170
                     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
171
0
{
172
0
  my_post_ptr post = (my_post_ptr)cinfo->post;
173
0
  JDIMENSION old_next_row, num_rows;
174
175
  /* Reposition virtual buffer if at start of strip. */
176
0
  if (post->next_row == 0) {
177
0
    post->buffer = (_JSAMPARRAY)(*cinfo->mem->access_virt_sarray)
178
0
        ((j_common_ptr)cinfo, post->whole_image,
179
0
         post->starting_row, post->strip_height, TRUE);
180
0
  }
181
182
  /* Upsample some data (up to a strip height's worth). */
183
0
  old_next_row = post->next_row;
184
0
  (*cinfo->upsample->_upsample) (cinfo, input_buf, in_row_group_ctr,
185
0
                                 in_row_groups_avail, post->buffer,
186
0
                                 &post->next_row, post->strip_height);
187
188
  /* Allow quantizer to scan new data.  No data is emitted, */
189
  /* but we advance out_row_ctr so outer loop can tell when we're done. */
190
0
  if (post->next_row > old_next_row) {
191
0
    num_rows = post->next_row - old_next_row;
192
0
    (*cinfo->cquantize->_color_quantize) (cinfo, post->buffer + old_next_row,
193
0
                                          (_JSAMPARRAY)NULL, (int)num_rows);
194
0
    *out_row_ctr += num_rows;
195
0
  }
196
197
  /* Advance if we filled the strip. */
198
0
  if (post->next_row >= post->strip_height) {
199
0
    post->starting_row += post->strip_height;
200
0
    post->next_row = 0;
201
0
  }
202
0
}
203
204
205
/*
206
 * Process some data in the second pass of 2-pass quantization.
207
 */
208
209
METHODDEF(void)
210
post_process_2pass(j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
211
                   JDIMENSION *in_row_group_ctr,
212
                   JDIMENSION in_row_groups_avail, _JSAMPARRAY output_buf,
213
                   JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
214
0
{
215
0
  my_post_ptr post = (my_post_ptr)cinfo->post;
216
0
  JDIMENSION num_rows, max_rows;
217
218
  /* Reposition virtual buffer if at start of strip. */
219
0
  if (post->next_row == 0) {
220
0
    post->buffer = (_JSAMPARRAY)(*cinfo->mem->access_virt_sarray)
221
0
        ((j_common_ptr)cinfo, post->whole_image,
222
0
         post->starting_row, post->strip_height, FALSE);
223
0
  }
224
225
  /* Determine number of rows to emit. */
226
0
  num_rows = post->strip_height - post->next_row; /* available in strip */
227
0
  max_rows = out_rows_avail - *out_row_ctr; /* available in output area */
228
0
  if (num_rows > max_rows)
229
0
    num_rows = max_rows;
230
  /* We have to check bottom of image here, can't depend on upsampler. */
231
0
  max_rows = cinfo->output_height - post->starting_row;
232
0
  if (num_rows > max_rows)
233
0
    num_rows = max_rows;
234
235
  /* Quantize and emit data. */
236
0
  (*cinfo->cquantize->_color_quantize) (cinfo, post->buffer + post->next_row,
237
0
                                        output_buf + *out_row_ctr,
238
0
                                        (int)num_rows);
239
0
  *out_row_ctr += num_rows;
240
241
  /* Advance if we filled the strip. */
242
0
  post->next_row += num_rows;
243
0
  if (post->next_row >= post->strip_height) {
244
0
    post->starting_row += post->strip_height;
245
0
    post->next_row = 0;
246
0
  }
247
0
}
248
249
#endif /* QUANT_2PASS_SUPPORTED */
250
251
252
/*
253
 * Initialize postprocessing controller.
254
 */
255
256
GLOBAL(void)
257
_jinit_d_post_controller(j_decompress_ptr cinfo, boolean need_full_buffer)
258
142k
{
259
142k
  my_post_ptr post;
260
261
142k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
262
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
263
264
142k
  post = (my_post_ptr)
265
142k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
266
142k
                                sizeof(my_post_controller));
267
142k
  cinfo->post = (struct jpeg_d_post_controller *)post;
268
142k
  post->pub.start_pass = start_pass_dpost;
269
142k
  post->whole_image = NULL;     /* flag for no virtual arrays */
270
142k
  post->buffer = NULL;          /* flag for no strip buffer */
271
272
  /* Create the quantization buffer, if needed */
273
142k
  if (cinfo->quantize_colors) {
274
    /* The buffer strip height is max_v_samp_factor, which is typically
275
     * an efficient number of rows for upsampling to return.
276
     * (In the presence of output rescaling, we might want to be smarter?)
277
     */
278
0
    post->strip_height = (JDIMENSION)cinfo->max_v_samp_factor;
279
0
    if (need_full_buffer) {
280
      /* Two-pass color quantization: need full-image storage. */
281
      /* We round up the number of rows to a multiple of the strip height. */
282
0
#ifdef QUANT_2PASS_SUPPORTED
283
0
      post->whole_image = (*cinfo->mem->request_virt_sarray)
284
0
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
285
0
         cinfo->output_width * cinfo->out_color_components,
286
0
         (JDIMENSION)jround_up((long)cinfo->output_height,
287
0
                               (long)post->strip_height),
288
0
         post->strip_height);
289
#else
290
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
291
#endif /* QUANT_2PASS_SUPPORTED */
292
0
    } else {
293
      /* One-pass color quantization: just make a strip buffer. */
294
0
      post->buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
295
0
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
296
0
         cinfo->output_width * cinfo->out_color_components,
297
0
         post->strip_height);
298
0
    }
299
0
  }
300
142k
}
j12init_d_post_controller
Line
Count
Source
258
60.1k
{
259
60.1k
  my_post_ptr post;
260
261
60.1k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
262
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
263
264
60.1k
  post = (my_post_ptr)
265
60.1k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
266
60.1k
                                sizeof(my_post_controller));
267
60.1k
  cinfo->post = (struct jpeg_d_post_controller *)post;
268
60.1k
  post->pub.start_pass = start_pass_dpost;
269
60.1k
  post->whole_image = NULL;     /* flag for no virtual arrays */
270
60.1k
  post->buffer = NULL;          /* flag for no strip buffer */
271
272
  /* Create the quantization buffer, if needed */
273
60.1k
  if (cinfo->quantize_colors) {
274
    /* The buffer strip height is max_v_samp_factor, which is typically
275
     * an efficient number of rows for upsampling to return.
276
     * (In the presence of output rescaling, we might want to be smarter?)
277
     */
278
0
    post->strip_height = (JDIMENSION)cinfo->max_v_samp_factor;
279
0
    if (need_full_buffer) {
280
      /* Two-pass color quantization: need full-image storage. */
281
      /* We round up the number of rows to a multiple of the strip height. */
282
0
#ifdef QUANT_2PASS_SUPPORTED
283
0
      post->whole_image = (*cinfo->mem->request_virt_sarray)
284
0
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
285
0
         cinfo->output_width * cinfo->out_color_components,
286
0
         (JDIMENSION)jround_up((long)cinfo->output_height,
287
0
                               (long)post->strip_height),
288
0
         post->strip_height);
289
#else
290
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
291
#endif /* QUANT_2PASS_SUPPORTED */
292
0
    } else {
293
      /* One-pass color quantization: just make a strip buffer. */
294
0
      post->buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
295
0
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
296
0
         cinfo->output_width * cinfo->out_color_components,
297
0
         post->strip_height);
298
0
    }
299
0
  }
300
60.1k
}
j16init_d_post_controller
Line
Count
Source
258
17.8k
{
259
17.8k
  my_post_ptr post;
260
261
17.8k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
262
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
263
264
17.8k
  post = (my_post_ptr)
265
17.8k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
266
17.8k
                                sizeof(my_post_controller));
267
17.8k
  cinfo->post = (struct jpeg_d_post_controller *)post;
268
17.8k
  post->pub.start_pass = start_pass_dpost;
269
17.8k
  post->whole_image = NULL;     /* flag for no virtual arrays */
270
17.8k
  post->buffer = NULL;          /* flag for no strip buffer */
271
272
  /* Create the quantization buffer, if needed */
273
17.8k
  if (cinfo->quantize_colors) {
274
    /* The buffer strip height is max_v_samp_factor, which is typically
275
     * an efficient number of rows for upsampling to return.
276
     * (In the presence of output rescaling, we might want to be smarter?)
277
     */
278
0
    post->strip_height = (JDIMENSION)cinfo->max_v_samp_factor;
279
0
    if (need_full_buffer) {
280
      /* Two-pass color quantization: need full-image storage. */
281
      /* We round up the number of rows to a multiple of the strip height. */
282
0
#ifdef QUANT_2PASS_SUPPORTED
283
0
      post->whole_image = (*cinfo->mem->request_virt_sarray)
284
0
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
285
0
         cinfo->output_width * cinfo->out_color_components,
286
0
         (JDIMENSION)jround_up((long)cinfo->output_height,
287
0
                               (long)post->strip_height),
288
0
         post->strip_height);
289
#else
290
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
291
#endif /* QUANT_2PASS_SUPPORTED */
292
0
    } else {
293
      /* One-pass color quantization: just make a strip buffer. */
294
0
      post->buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
295
0
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
296
0
         cinfo->output_width * cinfo->out_color_components,
297
0
         post->strip_height);
298
0
    }
299
0
  }
300
17.8k
}
jinit_d_post_controller
Line
Count
Source
258
64.6k
{
259
64.6k
  my_post_ptr post;
260
261
64.6k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
262
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
263
264
64.6k
  post = (my_post_ptr)
265
64.6k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
266
64.6k
                                sizeof(my_post_controller));
267
64.6k
  cinfo->post = (struct jpeg_d_post_controller *)post;
268
64.6k
  post->pub.start_pass = start_pass_dpost;
269
64.6k
  post->whole_image = NULL;     /* flag for no virtual arrays */
270
64.6k
  post->buffer = NULL;          /* flag for no strip buffer */
271
272
  /* Create the quantization buffer, if needed */
273
64.6k
  if (cinfo->quantize_colors) {
274
    /* The buffer strip height is max_v_samp_factor, which is typically
275
     * an efficient number of rows for upsampling to return.
276
     * (In the presence of output rescaling, we might want to be smarter?)
277
     */
278
0
    post->strip_height = (JDIMENSION)cinfo->max_v_samp_factor;
279
0
    if (need_full_buffer) {
280
      /* Two-pass color quantization: need full-image storage. */
281
      /* We round up the number of rows to a multiple of the strip height. */
282
0
#ifdef QUANT_2PASS_SUPPORTED
283
0
      post->whole_image = (*cinfo->mem->request_virt_sarray)
284
0
        ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
285
0
         cinfo->output_width * cinfo->out_color_components,
286
0
         (JDIMENSION)jround_up((long)cinfo->output_height,
287
0
                               (long)post->strip_height),
288
0
         post->strip_height);
289
#else
290
      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
291
#endif /* QUANT_2PASS_SUPPORTED */
292
0
    } else {
293
      /* One-pass color quantization: just make a strip buffer. */
294
0
      post->buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
295
0
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
296
0
         cinfo->output_width * cinfo->out_color_components,
297
0
         post->strip_height);
298
0
    }
299
0
  }
300
64.6k
}
301
302
#endif /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */