Coverage Report

Created: 2025-06-24 07:01

/src/ghostpdl/obj/jdsample.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jdsample.c
3
 *
4
 * Copyright (C) 1991-1996, Thomas G. Lane.
5
 * Modified 2002-2020 by Guido Vollbeding.
6
 * This file is part of the Independent JPEG Group's software.
7
 * For conditions of distribution and use, see the accompanying README file.
8
 *
9
 * This file contains upsampling routines.
10
 *
11
 * Upsampling input data is counted in "row groups".  A row group
12
 * is defined to be (v_samp_factor * DCT_v_scaled_size / min_DCT_v_scaled_size)
13
 * sample rows of each component.  Upsampling will normally produce
14
 * max_v_samp_factor pixel rows from each row group (but this could vary
15
 * if the upsampler is applying a scale factor of its own).
16
 *
17
 * An excellent reference for image resampling is
18
 *   Digital Image Warping, George Wolberg, 1990.
19
 *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
20
 */
21
22
#define JPEG_INTERNALS
23
#include "jinclude.h"
24
#include "jpeglib.h"
25
26
27
/* Pointer to routine to upsample a single component */
28
typedef JMETHOD(void, upsample1_ptr,
29
    (j_decompress_ptr cinfo, jpeg_component_info * compptr,
30
     JSAMPARRAY input_data, JSAMPIMAGE output_data_ptr));
31
32
/* Private subobject */
33
34
typedef struct {
35
  struct jpeg_upsampler pub;  /* public fields */
36
37
  /* Color conversion buffer.  When using separate upsampling and color
38
   * conversion steps, this buffer holds one upsampled row group until it
39
   * has been color converted and output.
40
   * Note: we do not allocate any storage for component(s) which are full-size,
41
   * ie do not need rescaling.  The corresponding entry of color_buf[] is
42
   * simply set to point to the input data array, thereby avoiding copying.
43
   */
44
  JSAMPARRAY color_buf[MAX_COMPONENTS];
45
46
  /* Per-component upsampling method pointers */
47
  upsample1_ptr methods[MAX_COMPONENTS];
48
49
  int next_row_out;   /* counts rows emitted from color_buf */
50
  JDIMENSION rows_to_go;  /* counts rows remaining in image */
51
52
  /* Height of an input row group for each component. */
53
  int rowgroup_height[MAX_COMPONENTS];
54
55
  /* These arrays save pixel expansion factors so that int_expand need not
56
   * recompute them each time.  They are unused for other upsampling methods.
57
   */
58
  UINT8 h_expand[MAX_COMPONENTS];
59
  UINT8 v_expand[MAX_COMPONENTS];
60
} my_upsampler;
61
62
typedef my_upsampler * my_upsample_ptr;
63
64
65
/*
66
 * Initialize for an upsampling pass.
67
 */
68
69
METHODDEF(void)
70
start_pass_upsample (j_decompress_ptr cinfo)
71
9.94k
{
72
9.94k
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
73
74
  /* Mark the conversion buffer empty */
75
9.94k
  upsample->next_row_out = cinfo->max_v_samp_factor;
76
  /* Initialize total-height counter for detecting bottom of image */
77
9.94k
  upsample->rows_to_go = cinfo->output_height;
78
9.94k
}
79
80
81
/*
82
 * Control routine to do upsampling (and color conversion).
83
 *
84
 * In this version we upsample each component independently.
85
 * We upsample one row group into the conversion buffer, then apply
86
 * color conversion a row at a time.
87
 */
88
89
METHODDEF(void)
90
sep_upsample (j_decompress_ptr cinfo,
91
        JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
92
        JDIMENSION in_row_groups_avail,
93
        JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
94
        JDIMENSION out_rows_avail)
95
2.17M
{
96
2.17M
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
97
2.17M
  int ci;
98
2.17M
  jpeg_component_info * compptr;
99
2.17M
  JDIMENSION num_rows;
100
101
  /* Fill the conversion buffer, if it's empty */
102
2.17M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
103
5.79M
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
104
4.27M
   ci++, compptr++) {
105
      /* Don't bother to upsample an uninteresting component. */
106
4.27M
      if (! compptr->component_needed)
107
0
  continue;
108
      /* Invoke per-component upsample method.  Notice we pass a POINTER
109
       * to color_buf[ci], so that fullsize_upsample can change it.
110
       */
111
4.27M
      (*upsample->methods[ci]) (cinfo, compptr,
112
4.27M
  input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
113
4.27M
  upsample->color_buf + ci);
114
4.27M
    }
115
1.51M
    upsample->next_row_out = 0;
116
1.51M
  }
117
118
  /* Color-convert and emit rows */
119
120
  /* How many we have in the buffer: */
121
2.17M
  num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
122
  /* Not more than the distance to the end of the image.  Need this test
123
   * in case the image height is not a multiple of max_v_samp_factor:
124
   */
125
2.17M
  if (num_rows > upsample->rows_to_go) 
126
2.54k
    num_rows = upsample->rows_to_go;
127
  /* And not more than what the client can accept: */
128
2.17M
  out_rows_avail -= *out_row_ctr;
129
2.17M
  if (num_rows > out_rows_avail)
130
661k
    num_rows = out_rows_avail;
131
132
2.17M
  (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
133
2.17M
             (JDIMENSION) upsample->next_row_out,
134
2.17M
             output_buf + *out_row_ctr,
135
2.17M
             (int) num_rows);
136
137
  /* Adjust counts */
138
2.17M
  *out_row_ctr += num_rows;
139
2.17M
  upsample->rows_to_go -= num_rows;
140
2.17M
  upsample->next_row_out += num_rows;
141
  /* When the buffer is emptied, declare this input row group consumed */
142
2.17M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
143
1.51M
    (*in_row_group_ctr)++;
144
2.17M
}
145
146
147
/*
148
 * These are the routines invoked by sep_upsample to upsample pixel values
149
 * of a single component.  One row group is processed per call.
150
 */
151
152
153
/*
154
 * For full-size components, we just make color_buf[ci] point at the
155
 * input buffer, and thus avoid copying any data.  Note that this is
156
 * safe only because sep_upsample doesn't declare the input row group
157
 * "consumed" until we are done color converting and emitting it.
158
 */
159
160
METHODDEF(void)
161
fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
162
       JSAMPARRAY input_data, JSAMPIMAGE output_data_ptr)
163
2.94M
{
164
2.94M
  *output_data_ptr = input_data;
165
2.94M
}
166
167
168
/*
169
 * This version handles any integral sampling ratios.
170
 * This is not used for typical JPEG files, so it need not be fast.
171
 * Nor, for that matter, is it particularly accurate: the algorithm is
172
 * simple replication of the input pixel onto the corresponding output
173
 * pixels.  The hi-falutin sampling literature refers to this as a
174
 * "box filter".  A box filter tends to introduce visible artifacts,
175
 * so if you are actually going to use 3:1 or 4:1 sampling ratios
176
 * you would be well advised to improve this code.
177
 */
178
179
METHODDEF(void)
180
int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
181
        JSAMPARRAY input_data, JSAMPIMAGE output_data_ptr)
182
1.57k
{
183
1.57k
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
184
1.57k
  JSAMPARRAY output_data, output_end;
185
1.57k
  register JSAMPROW inptr, outptr;
186
1.57k
  register JSAMPLE invalue;
187
1.57k
  register int h;
188
1.57k
  JSAMPROW outend;
189
1.57k
  int h_expand, v_expand;
190
191
1.57k
  h_expand = upsample->h_expand[compptr->component_index];
192
1.57k
  v_expand = upsample->v_expand[compptr->component_index];
193
194
1.57k
  output_data = *output_data_ptr;
195
1.57k
  output_end = output_data + cinfo->max_v_samp_factor;
196
3.14k
  for (; output_data < output_end; output_data += v_expand) {
197
    /* Generate one output row with proper horizontal expansion */
198
1.57k
    inptr = *input_data++;
199
1.57k
    outptr = *output_data;
200
1.57k
    outend = outptr + cinfo->output_width;
201
12.4M
    while (outptr < outend) {
202
12.4M
      invalue = *inptr++; /* don't need GETJSAMPLE() here */
203
24.8M
      for (h = h_expand; h > 0; h--) {
204
12.4M
  *outptr++ = invalue;
205
12.4M
      }
206
12.4M
    }
207
    /* Generate any additional output rows by duplicating the first one */
208
1.57k
    if (v_expand > 1) {
209
1.57k
      jcopy_sample_rows(output_data, output_data + 1,
210
1.57k
      v_expand - 1, cinfo->output_width);
211
1.57k
    }
212
1.57k
  }
213
1.57k
}
214
215
216
/*
217
 * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
218
 * It's still a box filter.
219
 */
220
221
METHODDEF(void)
222
h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
223
         JSAMPARRAY input_data, JSAMPIMAGE output_data_ptr)
224
1.04k
{
225
1.04k
  JSAMPARRAY output_data = *output_data_ptr;
226
1.04k
  register JSAMPROW inptr, outptr;
227
1.04k
  register JSAMPLE invalue;
228
1.04k
  JSAMPROW outend;
229
1.04k
  int outrow;
230
231
2.30k
  for (outrow = 0; outrow < cinfo->max_v_samp_factor; outrow++) {
232
1.26k
    inptr = input_data[outrow];
233
1.26k
    outptr = output_data[outrow];
234
1.26k
    outend = outptr + cinfo->output_width;
235
302k
    while (outptr < outend) {
236
301k
      invalue = *inptr++; /* don't need GETJSAMPLE() here */
237
301k
      *outptr++ = invalue;
238
301k
      *outptr++ = invalue;
239
301k
    }
240
1.26k
  }
241
1.04k
}
242
243
244
/*
245
 * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
246
 * It's still a box filter.
247
 */
248
249
METHODDEF(void)
250
h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
251
         JSAMPARRAY input_data, JSAMPIMAGE output_data_ptr)
252
1.32M
{
253
1.32M
  JSAMPARRAY output_data, output_end;
254
1.32M
  register JSAMPROW inptr, outptr;
255
1.32M
  register JSAMPLE invalue;
256
1.32M
  JSAMPROW outend;
257
258
1.32M
  output_data = *output_data_ptr;
259
1.32M
  output_end = output_data + cinfo->max_v_samp_factor;
260
2.65M
  for (; output_data < output_end; output_data += 2) {
261
1.32M
    inptr = *input_data++;
262
1.32M
    outptr = *output_data;
263
1.32M
    outend = outptr + cinfo->output_width;
264
1.02G
    while (outptr < outend) {
265
1.02G
      invalue = *inptr++; /* don't need GETJSAMPLE() here */
266
1.02G
      *outptr++ = invalue;
267
1.02G
      *outptr++ = invalue;
268
1.02G
    }
269
1.32M
    jcopy_sample_rows(output_data, output_data + 1,
270
1.32M
          1, cinfo->output_width);
271
1.32M
  }
272
1.32M
}
273
274
275
/*
276
 * Module initialization routine for upsampling.
277
 */
278
279
GLOBAL(void)
280
jinit_upsampler (j_decompress_ptr cinfo)
281
10.0k
{
282
10.0k
  my_upsample_ptr upsample;
283
10.0k
  int ci;
284
10.0k
  jpeg_component_info * compptr;
285
10.0k
  int h_in_group, v_in_group, h_out_group, v_out_group;
286
287
10.0k
  upsample = (my_upsample_ptr) (*cinfo->mem->alloc_small)
288
10.0k
    ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_upsampler));
289
10.0k
  cinfo->upsample = &upsample->pub;
290
10.0k
  upsample->pub.start_pass = start_pass_upsample;
291
10.0k
  upsample->pub.upsample = sep_upsample;
292
10.0k
  upsample->pub.need_context_rows = FALSE; /* until we find out differently */
293
294
10.0k
  if (cinfo->CCIR601_sampling)  /* this isn't supported */
295
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
296
297
  /* Verify we can handle the sampling factors, select per-component methods,
298
   * and create storage as needed.
299
   */
300
38.5k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
301
28.5k
       ci++, compptr++) {
302
    /* Don't bother to upsample an uninteresting component. */
303
28.5k
    if (! compptr->component_needed)
304
0
      continue;
305
    /* Compute size of an "input group" after IDCT scaling.  This many samples
306
     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
307
     */
308
28.5k
    h_in_group = (compptr->h_samp_factor * compptr->DCT_h_scaled_size) /
309
28.5k
     cinfo->min_DCT_h_scaled_size;
310
28.5k
    v_in_group = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
311
28.5k
     cinfo->min_DCT_v_scaled_size;
312
28.5k
    h_out_group = cinfo->max_h_samp_factor;
313
28.5k
    v_out_group = cinfo->max_v_samp_factor;
314
28.5k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
315
28.5k
    if (h_in_group == h_out_group && v_in_group == v_out_group) {
316
      /* Fullsize components can be processed without any work. */
317
16.7k
      upsample->methods[ci] = fullsize_upsample;
318
16.7k
      continue;   /* don't need to allocate buffer */
319
16.7k
    }
320
11.7k
    if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
321
      /* Special case for 2h1v upsampling */
322
6
      upsample->methods[ci] = h2v1_upsample;
323
11.7k
    } else if (h_in_group * 2 == h_out_group &&
324
11.7k
         v_in_group * 2 == v_out_group) {
325
      /* Special case for 2h2v upsampling */
326
11.4k
      upsample->methods[ci] = h2v2_upsample;
327
11.4k
    } else if ((h_out_group % h_in_group) == 0 &&
328
289
         (v_out_group % v_in_group) == 0) {
329
      /* Generic integral-factors upsampling method */
330
289
      upsample->methods[ci] = int_upsample;
331
289
      upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
332
289
      upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
333
289
    } else
334
0
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
335
11.7k
    upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
336
11.7k
      ((j_common_ptr) cinfo, JPOOL_IMAGE,
337
11.7k
       (JDIMENSION) jround_up((long) cinfo->output_width,
338
11.7k
            (long) cinfo->max_h_samp_factor),
339
11.7k
       (JDIMENSION) cinfo->max_v_samp_factor);
340
11.7k
  }
341
10.0k
}