Coverage Report

Created: 2025-06-10 07:17

/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
510
{
72
510
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
73
74
  /* Mark the conversion buffer empty */
75
510
  upsample->next_row_out = cinfo->max_v_samp_factor;
76
  /* Initialize total-height counter for detecting bottom of image */
77
510
  upsample->rows_to_go = cinfo->output_height;
78
510
}
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
183k
{
96
183k
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
97
183k
  int ci;
98
183k
  jpeg_component_info * compptr;
99
183k
  JDIMENSION num_rows;
100
101
  /* Fill the conversion buffer, if it's empty */
102
183k
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
103
453k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
104
337k
   ci++, compptr++) {
105
      /* Don't bother to upsample an uninteresting component. */
106
337k
      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
337k
      (*upsample->methods[ci]) (cinfo, compptr,
112
337k
  input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
113
337k
  upsample->color_buf + ci);
114
337k
    }
115
115k
    upsample->next_row_out = 0;
116
115k
  }
117
118
  /* Color-convert and emit rows */
119
120
  /* How many we have in the buffer: */
121
183k
  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
183k
  if (num_rows > upsample->rows_to_go) 
126
47
    num_rows = upsample->rows_to_go;
127
  /* And not more than what the client can accept: */
128
183k
  out_rows_avail -= *out_row_ctr;
129
183k
  if (num_rows > out_rows_avail)
130
67.5k
    num_rows = out_rows_avail;
131
132
183k
  (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
133
183k
             (JDIMENSION) upsample->next_row_out,
134
183k
             output_buf + *out_row_ctr,
135
183k
             (int) num_rows);
136
137
  /* Adjust counts */
138
183k
  *out_row_ctr += num_rows;
139
183k
  upsample->rows_to_go -= num_rows;
140
183k
  upsample->next_row_out += num_rows;
141
  /* When the buffer is emptied, declare this input row group consumed */
142
183k
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
143
115k
    (*in_row_group_ctr)++;
144
183k
}
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
202k
{
164
202k
  *output_data_ptr = input_data;
165
202k
}
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
2
{
183
2
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
184
2
  JSAMPARRAY output_data, output_end;
185
2
  register JSAMPROW inptr, outptr;
186
2
  register JSAMPLE invalue;
187
2
  register int h;
188
2
  JSAMPROW outend;
189
2
  int h_expand, v_expand;
190
191
2
  h_expand = upsample->h_expand[compptr->component_index];
192
2
  v_expand = upsample->v_expand[compptr->component_index];
193
194
2
  output_data = *output_data_ptr;
195
2
  output_end = output_data + cinfo->max_v_samp_factor;
196
4
  for (; output_data < output_end; output_data += v_expand) {
197
    /* Generate one output row with proper horizontal expansion */
198
2
    inptr = *input_data++;
199
2
    outptr = *output_data;
200
2
    outend = outptr + cinfo->output_width;
201
48.8k
    while (outptr < outend) {
202
48.8k
      invalue = *inptr++; /* don't need GETJSAMPLE() here */
203
97.7k
      for (h = h_expand; h > 0; h--) {
204
48.8k
  *outptr++ = invalue;
205
48.8k
      }
206
48.8k
    }
207
    /* Generate any additional output rows by duplicating the first one */
208
2
    if (v_expand > 1) {
209
2
      jcopy_sample_rows(output_data, output_data + 1,
210
2
      v_expand - 1, cinfo->output_width);
211
2
    }
212
2
  }
213
2
}
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
0
{
225
0
  JSAMPARRAY output_data = *output_data_ptr;
226
0
  register JSAMPROW inptr, outptr;
227
0
  register JSAMPLE invalue;
228
0
  JSAMPROW outend;
229
0
  int outrow;
230
231
0
  for (outrow = 0; outrow < cinfo->max_v_samp_factor; outrow++) {
232
0
    inptr = input_data[outrow];
233
0
    outptr = output_data[outrow];
234
0
    outend = outptr + cinfo->output_width;
235
0
    while (outptr < outend) {
236
0
      invalue = *inptr++; /* don't need GETJSAMPLE() here */
237
0
      *outptr++ = invalue;
238
0
      *outptr++ = invalue;
239
0
    }
240
0
  }
241
0
}
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
135k
{
253
135k
  JSAMPARRAY output_data, output_end;
254
135k
  register JSAMPROW inptr, outptr;
255
135k
  register JSAMPLE invalue;
256
135k
  JSAMPROW outend;
257
258
135k
  output_data = *output_data_ptr;
259
135k
  output_end = output_data + cinfo->max_v_samp_factor;
260
270k
  for (; output_data < output_end; output_data += 2) {
261
135k
    inptr = *input_data++;
262
135k
    outptr = *output_data;
263
135k
    outend = outptr + cinfo->output_width;
264
125M
    while (outptr < outend) {
265
125M
      invalue = *inptr++; /* don't need GETJSAMPLE() here */
266
125M
      *outptr++ = invalue;
267
125M
      *outptr++ = invalue;
268
125M
    }
269
135k
    jcopy_sample_rows(output_data, output_data + 1,
270
135k
          1, cinfo->output_width);
271
135k
  }
272
135k
}
273
274
275
/*
276
 * Module initialization routine for upsampling.
277
 */
278
279
GLOBAL(void)
280
jinit_upsampler (j_decompress_ptr cinfo)
281
510
{
282
510
  my_upsample_ptr upsample;
283
510
  int ci;
284
510
  jpeg_component_info * compptr;
285
510
  int h_in_group, v_in_group, h_out_group, v_out_group;
286
287
510
  upsample = (my_upsample_ptr) (*cinfo->mem->alloc_small)
288
510
    ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_upsampler));
289
510
  cinfo->upsample = &upsample->pub;
290
510
  upsample->pub.start_pass = start_pass_upsample;
291
510
  upsample->pub.upsample = sep_upsample;
292
510
  upsample->pub.need_context_rows = FALSE; /* until we find out differently */
293
294
510
  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
1.96k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
301
1.45k
       ci++, compptr++) {
302
    /* Don't bother to upsample an uninteresting component. */
303
1.45k
    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
1.45k
    h_in_group = (compptr->h_samp_factor * compptr->DCT_h_scaled_size) /
309
1.45k
     cinfo->min_DCT_h_scaled_size;
310
1.45k
    v_in_group = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
311
1.45k
     cinfo->min_DCT_v_scaled_size;
312
1.45k
    h_out_group = cinfo->max_h_samp_factor;
313
1.45k
    v_out_group = cinfo->max_v_samp_factor;
314
1.45k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
315
1.45k
    if (h_in_group == h_out_group && v_in_group == v_out_group) {
316
      /* Fullsize components can be processed without any work. */
317
844
      upsample->methods[ci] = fullsize_upsample;
318
844
      continue;   /* don't need to allocate buffer */
319
844
    }
320
610
    if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
321
      /* Special case for 2h1v upsampling */
322
0
      upsample->methods[ci] = h2v1_upsample;
323
610
    } else if (h_in_group * 2 == h_out_group &&
324
610
         v_in_group * 2 == v_out_group) {
325
      /* Special case for 2h2v upsampling */
326
608
      upsample->methods[ci] = h2v2_upsample;
327
608
    } else if ((h_out_group % h_in_group) == 0 &&
328
2
         (v_out_group % v_in_group) == 0) {
329
      /* Generic integral-factors upsampling method */
330
2
      upsample->methods[ci] = int_upsample;
331
2
      upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
332
2
      upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
333
2
    } else
334
0
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
335
610
    upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
336
610
      ((j_common_ptr) cinfo, JPOOL_IMAGE,
337
610
       (JDIMENSION) jround_up((long) cinfo->output_width,
338
610
            (long) cinfo->max_h_samp_factor),
339
610
       (JDIMENSION) cinfo->max_v_samp_factor);
340
610
  }
341
510
}