Coverage Report

Created: 2025-06-13 06:18

/src/gdal/build/frmts/jpeg/libjpeg12/jdsample12.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jdsample.c
3
 *
4
 * Copyright (C) 1991-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 upsampling routines.
9
 *
10
 * Upsampling input data is counted in "row groups".  A row group
11
 * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
12
 * sample rows of each component.  Upsampling will normally produce
13
 * max_v_samp_factor pixel rows from each row group (but this could vary
14
 * if the upsampler is applying a scale factor of its own).
15
 *
16
 * An excellent reference for image resampling is
17
 *   Digital Image Warping, George Wolberg, 1990.
18
 *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
19
 */
20
21
#define JPEG_INTERNALS
22
#include "jinclude.h"
23
#include "jpeglib.h"
24
25
#include "cpl_port.h"
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, JSAMPARRAY * 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
0
{
72
0
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
73
74
  /* Mark the conversion buffer empty */
75
0
  upsample->next_row_out = cinfo->max_v_samp_factor;
76
  /* Initialize total-height counter for detecting bottom of image */
77
0
  upsample->rows_to_go = cinfo->output_height;
78
0
}
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
        CPL_UNUSED JDIMENSION in_row_groups_avail,
93
        JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
94
        JDIMENSION out_rows_avail)
95
0
{
96
0
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
97
0
  int ci;
98
0
  jpeg_component_info * compptr;
99
0
  JDIMENSION num_rows;
100
101
  /* Fill the conversion buffer, if it's empty */
102
0
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
103
0
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
104
0
   ci++, compptr++) {
105
      /* Invoke per-component upsample method.  Notice we pass a POINTER
106
       * to color_buf[ci], so that fullsize_upsample can change it.
107
       */
108
0
      (*upsample->methods[ci]) (cinfo, compptr,
109
0
  input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
110
0
  upsample->color_buf + ci);
111
0
    }
112
0
    upsample->next_row_out = 0;
113
0
  }
114
115
  /* Color-convert and emit rows */
116
117
  /* How many we have in the buffer: */
118
0
  num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
119
  /* Not more than the distance to the end of the image.  Need this test
120
   * in case the image height is not a multiple of max_v_samp_factor:
121
   */
122
0
  if (num_rows > upsample->rows_to_go) 
123
0
    num_rows = upsample->rows_to_go;
124
  /* And not more than what the client can accept: */
125
0
  out_rows_avail -= *out_row_ctr;
126
0
  if (num_rows > out_rows_avail)
127
0
    num_rows = out_rows_avail;
128
129
0
  (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
130
0
             (JDIMENSION) upsample->next_row_out,
131
0
             output_buf + *out_row_ctr,
132
0
             (int) num_rows);
133
134
  /* Adjust counts */
135
0
  *out_row_ctr += num_rows;
136
0
  upsample->rows_to_go -= num_rows;
137
0
  upsample->next_row_out += num_rows;
138
  /* When the buffer is emptied, declare this input row group consumed */
139
0
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
140
0
    (*in_row_group_ctr)++;
141
0
}
142
143
144
/*
145
 * These are the routines invoked by sep_upsample to upsample pixel values
146
 * of a single component.  One row group is processed per call.
147
 */
148
149
150
/*
151
 * For full-size components, we just make color_buf[ci] point at the
152
 * input buffer, and thus avoid copying any data.  Note that this is
153
 * safe only because sep_upsample doesn't declare the input row group
154
 * "consumed" until we are done color converting and emitting it.
155
 */
156
157
METHODDEF(void)
158
fullsize_upsample (CPL_UNUSED j_decompress_ptr cinfo, CPL_UNUSED jpeg_component_info * compptr,
159
       JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
160
0
{
161
0
  *output_data_ptr = input_data;
162
0
}
163
164
165
/*
166
 * This is a no-op version used for "uninteresting" components.
167
 * These components will not be referenced by color conversion.
168
 */
169
170
METHODDEF(void)
171
noop_upsample (CPL_UNUSED j_decompress_ptr cinfo, CPL_UNUSED jpeg_component_info * compptr,
172
         CPL_UNUSED JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
173
0
{
174
0
  *output_data_ptr = NULL;  /* safety check */
175
0
}
176
177
178
/*
179
 * This version handles any integral sampling ratios.
180
 * This is not used for typical JPEG files, so it need not be fast.
181
 * Nor, for that matter, is it particularly accurate: the algorithm is
182
 * simple replication of the input pixel onto the corresponding output
183
 * pixels.  The highfalutin sampling literature refers to this as a
184
 * "box filter".  A box filter tends to introduce visible artifacts,
185
 * so if you are actually going to use 3:1 or 4:1 sampling ratios
186
 * you would be well advised to improve this code.
187
 */
188
189
METHODDEF(void)
190
int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
191
        JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
192
0
{
193
0
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
194
0
  JSAMPARRAY output_data = *output_data_ptr;
195
0
  register JSAMPROW inptr, outptr;
196
0
  register JSAMPLE invalue;
197
0
  register int h;
198
0
  JSAMPROW outend;
199
0
  int h_expand, v_expand;
200
0
  int inrow, outrow;
201
202
0
  h_expand = upsample->h_expand[compptr->component_index];
203
0
  v_expand = upsample->v_expand[compptr->component_index];
204
205
0
  inrow = outrow = 0;
206
0
  while (outrow < cinfo->max_v_samp_factor) {
207
    /* Generate one output row with proper horizontal expansion */
208
0
    inptr = input_data[inrow];
209
0
    outptr = output_data[outrow];
210
0
    outend = outptr + cinfo->output_width;
211
0
    while (outptr < outend) {
212
0
      invalue = *inptr++; /* don't need GETJSAMPLE() here */
213
0
      for (h = h_expand; h > 0; h--) {
214
0
  *outptr++ = invalue;
215
0
      }
216
0
    }
217
    /* Generate any additional output rows by duplicating the first one */
218
0
    if (v_expand > 1) {
219
0
      jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
220
0
      v_expand-1, cinfo->output_width);
221
0
    }
222
0
    inrow++;
223
0
    outrow += v_expand;
224
0
  }
225
0
}
226
227
228
/*
229
 * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
230
 * It's still a box filter.
231
 */
232
233
METHODDEF(void)
234
h2v1_upsample (j_decompress_ptr cinfo, CPL_UNUSED jpeg_component_info * compptr,
235
         JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
236
0
{
237
0
  JSAMPARRAY output_data = *output_data_ptr;
238
0
  register JSAMPROW inptr, outptr;
239
0
  register JSAMPLE invalue;
240
0
  JSAMPROW outend;
241
0
  int inrow;
242
243
0
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
244
0
    inptr = input_data[inrow];
245
0
    outptr = output_data[inrow];
246
0
    outend = outptr + cinfo->output_width;
247
0
    while (outptr < outend) {
248
0
      invalue = *inptr++; /* don't need GETJSAMPLE() here */
249
0
      *outptr++ = invalue;
250
0
      *outptr++ = invalue;
251
0
    }
252
0
  }
253
0
}
254
255
256
/*
257
 * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
258
 * It's still a box filter.
259
 */
260
261
METHODDEF(void)
262
h2v2_upsample (j_decompress_ptr cinfo, CPL_UNUSED jpeg_component_info * compptr,
263
         JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
264
0
{
265
0
  JSAMPARRAY output_data = *output_data_ptr;
266
0
  register JSAMPROW inptr, outptr;
267
0
  register JSAMPLE invalue;
268
0
  JSAMPROW outend;
269
0
  int inrow, outrow;
270
271
0
  inrow = outrow = 0;
272
0
  while (outrow < cinfo->max_v_samp_factor) {
273
0
    inptr = input_data[inrow];
274
0
    outptr = output_data[outrow];
275
0
    outend = outptr + cinfo->output_width;
276
0
    while (outptr < outend) {
277
0
      invalue = *inptr++; /* don't need GETJSAMPLE() here */
278
0
      *outptr++ = invalue;
279
0
      *outptr++ = invalue;
280
0
    }
281
0
    jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
282
0
          1, cinfo->output_width);
283
0
    inrow++;
284
0
    outrow += 2;
285
0
  }
286
0
}
287
288
289
/*
290
 * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
291
 *
292
 * The upsampling algorithm is linear interpolation between pixel centers,
293
 * also known as a "triangle filter".  This is a good compromise between
294
 * speed and visual quality.  The centers of the output pixels are 1/4 and 3/4
295
 * of the way between input pixel centers.
296
 *
297
 * A note about the "bias" calculations: when rounding fractional values to
298
 * integer, we do not want to always round 0.5 up to the next integer.
299
 * If we did that, we'd introduce a noticeable bias towards larger values.
300
 * Instead, this code is arranged so that 0.5 will be rounded up or down at
301
 * alternate pixel locations (a simple ordered dither pattern).
302
 */
303
304
METHODDEF(void)
305
h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
306
         JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
307
0
{
308
0
  JSAMPARRAY output_data = *output_data_ptr;
309
0
  register JSAMPROW inptr, outptr;
310
0
  register int invalue;
311
0
  register JDIMENSION colctr;
312
0
  int inrow;
313
314
0
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
315
0
    inptr = input_data[inrow];
316
0
    outptr = output_data[inrow];
317
    /* Special case for first column */
318
0
    invalue = GETJSAMPLE(*inptr++);
319
0
    *outptr++ = (JSAMPLE) invalue;
320
0
    *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
321
322
0
    for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
323
      /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
324
0
      invalue = GETJSAMPLE(*inptr++) * 3;
325
0
      *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
326
0
      *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
327
0
    }
328
329
    /* Special case for last column */
330
0
    invalue = GETJSAMPLE(*inptr);
331
0
    *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
332
0
    *outptr++ = (JSAMPLE) invalue;
333
0
  }
334
0
}
335
336
337
/*
338
 * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
339
 * Again a triangle filter; see comments for h2v1 case, above.
340
 *
341
 * It is OK for us to reference the adjacent input rows because we demanded
342
 * context from the main buffer controller (see initialization code).
343
 */
344
345
METHODDEF(void)
346
h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
347
         JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
348
0
{
349
0
  JSAMPARRAY output_data = *output_data_ptr;
350
0
  register JSAMPROW inptr0, inptr1, outptr;
351
#if BITS_IN_JSAMPLE == 8
352
  register int thiscolsum, lastcolsum, nextcolsum;
353
#else
354
0
  register INT32 thiscolsum, lastcolsum, nextcolsum;
355
0
#endif
356
0
  register JDIMENSION colctr;
357
0
  int inrow, outrow, v;
358
359
0
  inrow = outrow = 0;
360
0
  while (outrow < cinfo->max_v_samp_factor) {
361
0
    for (v = 0; v < 2; v++) {
362
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
363
0
      inptr0 = input_data[inrow];
364
0
      if (v == 0)   /* next nearest is row above */
365
0
  inptr1 = input_data[inrow-1];
366
0
      else      /* next nearest is row below */
367
0
  inptr1 = input_data[inrow+1];
368
0
      outptr = output_data[outrow++];
369
370
      /* Special case for first column */
371
0
      thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
372
0
      nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
373
0
      *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
374
0
      *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
375
0
      lastcolsum = thiscolsum; thiscolsum = nextcolsum;
376
377
0
      for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
378
  /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
379
  /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
380
0
  nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
381
0
  *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
382
0
  *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
383
0
  lastcolsum = thiscolsum; thiscolsum = nextcolsum;
384
0
      }
385
386
      /* Special case for last column */
387
0
      *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
388
0
      *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
389
0
    }
390
0
    inrow++;
391
0
  }
392
0
}
393
394
395
/*
396
 * Module initialization routine for upsampling.
397
 */
398
399
GLOBAL(void)
400
jinit_upsampler (j_decompress_ptr cinfo)
401
0
{
402
0
  my_upsample_ptr upsample;
403
0
  int ci;
404
0
  jpeg_component_info * compptr;
405
0
  boolean need_buffer, do_fancy;
406
0
  int h_in_group, v_in_group, h_out_group, v_out_group;
407
408
0
  upsample = (my_upsample_ptr)
409
0
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
410
0
        SIZEOF(my_upsampler));
411
0
  cinfo->upsample = (struct jpeg_upsampler *) upsample;
412
0
  upsample->pub.start_pass = start_pass_upsample;
413
0
  upsample->pub.upsample = sep_upsample;
414
0
  upsample->pub.need_context_rows = FALSE; /* until we find out differently */
415
416
0
  if (cinfo->CCIR601_sampling) /* this isn't supported */
417
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
418
419
  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
420
   * so don't ask for it.
421
   */
422
0
  do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1;
423
424
  /* Verify we can handle the sampling factors, select per-component methods,
425
   * and create storage as needed.
426
   */
427
0
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
428
0
       ci++, compptr++) {
429
    /* Compute size of an "input group" after IDCT scaling.  This many samples
430
     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
431
     */
432
0
    h_in_group = (compptr->h_samp_factor * compptr->DCT_scaled_size) /
433
0
     cinfo->min_DCT_scaled_size;
434
0
    v_in_group = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
435
0
     cinfo->min_DCT_scaled_size;
436
0
    h_out_group = cinfo->max_h_samp_factor;
437
0
    v_out_group = cinfo->max_v_samp_factor;
438
0
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
439
0
    need_buffer = TRUE;
440
0
    if (! compptr->component_needed) {
441
      /* Don't bother to upsample an uninteresting component. */
442
0
      upsample->methods[ci] = noop_upsample;
443
0
      need_buffer = FALSE;
444
0
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
445
      /* Fullsize components can be processed without any work. */
446
0
      upsample->methods[ci] = fullsize_upsample;
447
0
      need_buffer = FALSE;
448
0
    } else if (h_in_group * 2 == h_out_group &&
449
0
         v_in_group == v_out_group) {
450
      /* Special cases for 2h1v upsampling */
451
0
      if (do_fancy && compptr->downsampled_width > 2)
452
0
  upsample->methods[ci] = h2v1_fancy_upsample;
453
0
      else
454
0
  upsample->methods[ci] = h2v1_upsample;
455
0
    } else if (h_in_group * 2 == h_out_group &&
456
0
         v_in_group * 2 == v_out_group) {
457
      /* Special cases for 2h2v upsampling */
458
0
      if (do_fancy && compptr->downsampled_width > 2) {
459
0
  upsample->methods[ci] = h2v2_fancy_upsample;
460
0
  upsample->pub.need_context_rows = TRUE;
461
0
      } else
462
0
  upsample->methods[ci] = h2v2_upsample;
463
0
    } else if ((h_out_group % h_in_group) == 0 &&
464
0
         (v_out_group % v_in_group) == 0) {
465
      /* Generic integral-factors upsampling method */
466
0
      upsample->methods[ci] = int_upsample;
467
0
      upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
468
0
      upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
469
0
    } else
470
0
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
471
0
    if (need_buffer) {
472
0
      upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
473
0
  ((j_common_ptr) cinfo, JPOOL_IMAGE,
474
0
   (JDIMENSION) jround_up((long) cinfo->output_width,
475
0
        (long) cinfo->max_h_samp_factor),
476
0
   (JDIMENSION) cinfo->max_v_samp_factor);
477
0
    }
478
0
  }
479
0
}