Coverage Report

Created: 2025-06-22 06:59

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