Coverage Report

Created: 2023-09-28 22:18

/src/libjpeg-turbo.main/jdsample.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jdsample.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1991-1996, Thomas G. Lane.
6
 * libjpeg-turbo Modifications:
7
 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
 * Copyright (C) 2010, 2015-2016, 2022, D. R. Commander.
9
 * Copyright (C) 2014, MIPS Technologies, Inc., California.
10
 * Copyright (C) 2015, Google, Inc.
11
 * Copyright (C) 2019-2020, Arm Limited.
12
 * For conditions of distribution and use, see the accompanying README.ijg
13
 * file.
14
 *
15
 * This file contains upsampling routines.
16
 *
17
 * Upsampling input data is counted in "row groups".  A row group
18
 * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
19
 * sample rows of each component.  Upsampling will normally produce
20
 * max_v_samp_factor pixel rows from each row group (but this could vary
21
 * if the upsampler is applying a scale factor of its own).
22
 *
23
 * An excellent reference for image resampling is
24
 *   Digital Image Warping, George Wolberg, 1990.
25
 *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
26
 */
27
28
#include "jinclude.h"
29
#include "jdsample.h"
30
#include "jsimd.h"
31
#include "jpegapicomp.h"
32
33
34
35
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
36
37
/*
38
 * Initialize for an upsampling pass.
39
 */
40
41
METHODDEF(void)
42
start_pass_upsample(j_decompress_ptr cinfo)
43
39.1k
{
44
39.1k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
39.1k
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
39.1k
  upsample->rows_to_go = cinfo->output_height;
50
39.1k
}
51
52
53
/*
54
 * Control routine to do upsampling (and color conversion).
55
 *
56
 * In this version we upsample each component independently.
57
 * We upsample one row group into the conversion buffer, then apply
58
 * color conversion a row at a time.
59
 */
60
61
METHODDEF(void)
62
sep_upsample(j_decompress_ptr cinfo, _JSAMPIMAGE input_buf,
63
             JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail,
64
             _JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
65
             JDIMENSION out_rows_avail)
66
14.5M
{
67
14.5M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
68
14.5M
  int ci;
69
14.5M
  jpeg_component_info *compptr;
70
14.5M
  JDIMENSION num_rows;
71
72
  /* Fill the conversion buffer, if it's empty */
73
14.5M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
74
54.5M
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
75
40.0M
         ci++, compptr++) {
76
      /* Invoke per-component upsample method.  Notice we pass a POINTER
77
       * to color_buf[ci], so that fullsize_upsample can change it.
78
       */
79
40.0M
      (*upsample->methods[ci]) (cinfo, compptr,
80
40.0M
        input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
81
40.0M
        upsample->color_buf + ci);
82
40.0M
    }
83
14.5M
    upsample->next_row_out = 0;
84
14.5M
  }
85
86
  /* Color-convert and emit rows */
87
88
  /* How many we have in the buffer: */
89
14.5M
  num_rows = (JDIMENSION)(cinfo->max_v_samp_factor - upsample->next_row_out);
90
  /* Not more than the distance to the end of the image.  Need this test
91
   * in case the image height is not a multiple of max_v_samp_factor:
92
   */
93
14.5M
  if (num_rows > upsample->rows_to_go)
94
8.54k
    num_rows = upsample->rows_to_go;
95
  /* And not more than what the client can accept: */
96
14.5M
  out_rows_avail -= *out_row_ctr;
97
14.5M
  if (num_rows > out_rows_avail)
98
0
    num_rows = out_rows_avail;
99
100
14.5M
  (*cinfo->cconvert->_color_convert) (cinfo, upsample->color_buf,
101
14.5M
                                      (JDIMENSION)upsample->next_row_out,
102
14.5M
                                      output_buf + *out_row_ctr,
103
14.5M
                                      (int)num_rows);
104
105
  /* Adjust counts */
106
14.5M
  *out_row_ctr += num_rows;
107
14.5M
  upsample->rows_to_go -= num_rows;
108
14.5M
  upsample->next_row_out += num_rows;
109
  /* When the buffer is emptied, declare this input row group consumed */
110
14.5M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
111
14.5M
    (*in_row_group_ctr)++;
112
14.5M
}
113
114
115
/*
116
 * These are the routines invoked by sep_upsample to upsample pixel values
117
 * of a single component.  One row group is processed per call.
118
 */
119
120
121
/*
122
 * For full-size components, we just make color_buf[ci] point at the
123
 * input buffer, and thus avoid copying any data.  Note that this is
124
 * safe only because sep_upsample doesn't declare the input row group
125
 * "consumed" until we are done color converting and emitting it.
126
 */
127
128
METHODDEF(void)
129
fullsize_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
130
                  _JSAMPARRAY input_data, _JSAMPARRAY *output_data_ptr)
131
15.4M
{
132
15.4M
  *output_data_ptr = input_data;
133
15.4M
}
134
135
136
/*
137
 * This is a no-op version used for "uninteresting" components.
138
 * These components will not be referenced by color conversion.
139
 */
140
141
METHODDEF(void)
142
noop_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
143
              _JSAMPARRAY input_data, _JSAMPARRAY *output_data_ptr)
144
2.88M
{
145
2.88M
  *output_data_ptr = NULL;      /* safety check */
146
2.88M
}
147
148
149
/*
150
 * This version handles any integral sampling ratios.
151
 * This is not used for typical JPEG files, so it need not be fast.
152
 * Nor, for that matter, is it particularly accurate: the algorithm is
153
 * simple replication of the input pixel onto the corresponding output
154
 * pixels.  The hi-falutin sampling literature refers to this as a
155
 * "box filter".  A box filter tends to introduce visible artifacts,
156
 * so if you are actually going to use 3:1 or 4:1 sampling ratios
157
 * you would be well advised to improve this code.
158
 */
159
160
METHODDEF(void)
161
int_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
162
             _JSAMPARRAY input_data, _JSAMPARRAY *output_data_ptr)
163
14.3M
{
164
14.3M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
165
14.3M
  _JSAMPARRAY output_data = *output_data_ptr;
166
14.3M
  register _JSAMPROW inptr, outptr;
167
14.3M
  register _JSAMPLE invalue;
168
14.3M
  register int h;
169
14.3M
  _JSAMPROW outend;
170
14.3M
  int h_expand, v_expand;
171
14.3M
  int inrow, outrow;
172
173
14.3M
  h_expand = upsample->h_expand[compptr->component_index];
174
14.3M
  v_expand = upsample->v_expand[compptr->component_index];
175
176
14.3M
  inrow = outrow = 0;
177
32.2M
  while (outrow < cinfo->max_v_samp_factor) {
178
    /* Generate one output row with proper horizontal expansion */
179
17.8M
    inptr = input_data[inrow];
180
17.8M
    outptr = output_data[outrow];
181
17.8M
    outend = outptr + cinfo->output_width;
182
508M
    while (outptr < outend) {
183
490M
      invalue = *inptr++;
184
1.39G
      for (h = h_expand; h > 0; h--) {
185
902M
        *outptr++ = invalue;
186
902M
      }
187
490M
    }
188
    /* Generate any additional output rows by duplicating the first one */
189
17.8M
    if (v_expand > 1) {
190
13.1M
      _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1,
191
13.1M
                         v_expand - 1, cinfo->output_width);
192
13.1M
    }
193
17.8M
    inrow++;
194
17.8M
    outrow += v_expand;
195
17.8M
  }
196
14.3M
}
197
198
199
/*
200
 * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
201
 * It's still a box filter.
202
 */
203
204
METHODDEF(void)
205
h2v1_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
206
              _JSAMPARRAY input_data, _JSAMPARRAY *output_data_ptr)
207
2.02M
{
208
2.02M
  _JSAMPARRAY output_data = *output_data_ptr;
209
2.02M
  register _JSAMPROW inptr, outptr;
210
2.02M
  register _JSAMPLE invalue;
211
2.02M
  _JSAMPROW outend;
212
2.02M
  int inrow;
213
214
7.47M
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
215
5.45M
    inptr = input_data[inrow];
216
5.45M
    outptr = output_data[inrow];
217
5.45M
    outend = outptr + cinfo->output_width;
218
95.3M
    while (outptr < outend) {
219
89.8M
      invalue = *inptr++;
220
89.8M
      *outptr++ = invalue;
221
89.8M
      *outptr++ = invalue;
222
89.8M
    }
223
5.45M
  }
224
2.02M
}
225
226
227
/*
228
 * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
229
 * It's still a box filter.
230
 */
231
232
METHODDEF(void)
233
h2v2_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
234
              _JSAMPARRAY input_data, _JSAMPARRAY *output_data_ptr)
235
1.45M
{
236
1.45M
  _JSAMPARRAY output_data = *output_data_ptr;
237
1.45M
  register _JSAMPROW inptr, outptr;
238
1.45M
  register _JSAMPLE invalue;
239
1.45M
  _JSAMPROW outend;
240
1.45M
  int inrow, outrow;
241
242
1.45M
  inrow = outrow = 0;
243
3.05M
  while (outrow < cinfo->max_v_samp_factor) {
244
1.59M
    inptr = input_data[inrow];
245
1.59M
    outptr = output_data[outrow];
246
1.59M
    outend = outptr + cinfo->output_width;
247
39.2M
    while (outptr < outend) {
248
37.6M
      invalue = *inptr++;
249
37.6M
      *outptr++ = invalue;
250
37.6M
      *outptr++ = invalue;
251
37.6M
    }
252
1.59M
    _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1, 1,
253
1.59M
                       cinfo->output_width);
254
1.59M
    inrow++;
255
1.59M
    outrow += 2;
256
1.59M
  }
257
1.45M
}
258
259
260
/*
261
 * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
262
 *
263
 * The upsampling algorithm is linear interpolation between pixel centers,
264
 * also known as a "triangle filter".  This is a good compromise between
265
 * speed and visual quality.  The centers of the output pixels are 1/4 and 3/4
266
 * of the way between input pixel centers.
267
 *
268
 * A note about the "bias" calculations: when rounding fractional values to
269
 * integer, we do not want to always round 0.5 up to the next integer.
270
 * If we did that, we'd introduce a noticeable bias towards larger values.
271
 * Instead, this code is arranged so that 0.5 will be rounded up or down at
272
 * alternate pixel locations (a simple ordered dither pattern).
273
 */
274
275
METHODDEF(void)
276
h2v1_fancy_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
277
                    _JSAMPARRAY input_data, _JSAMPARRAY *output_data_ptr)
278
409k
{
279
409k
  _JSAMPARRAY output_data = *output_data_ptr;
280
409k
  register _JSAMPROW inptr, outptr;
281
409k
  register int invalue;
282
409k
  register JDIMENSION colctr;
283
409k
  int inrow;
284
285
1.54M
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
286
1.13M
    inptr = input_data[inrow];
287
1.13M
    outptr = output_data[inrow];
288
    /* Special case for first column */
289
1.13M
    invalue = *inptr++;
290
1.13M
    *outptr++ = (_JSAMPLE)invalue;
291
1.13M
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[0] + 2) >> 2);
292
293
9.59M
    for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
294
      /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
295
8.46M
      invalue = (*inptr++) * 3;
296
8.46M
      *outptr++ = (_JSAMPLE)((invalue + inptr[-2] + 1) >> 2);
297
8.46M
      *outptr++ = (_JSAMPLE)((invalue + inptr[0] + 2) >> 2);
298
8.46M
    }
299
300
    /* Special case for last column */
301
1.13M
    invalue = *inptr;
302
1.13M
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[-1] + 1) >> 2);
303
1.13M
    *outptr++ = (_JSAMPLE)invalue;
304
1.13M
  }
305
409k
}
306
307
308
/*
309
 * Fancy processing for 1:1 horizontal and 2:1 vertical (4:4:0 subsampling).
310
 *
311
 * This is a less common case, but it can be encountered when losslessly
312
 * rotating/transposing a JPEG file that uses 4:2:2 chroma subsampling.
313
 */
314
315
METHODDEF(void)
316
h1v2_fancy_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
317
                    _JSAMPARRAY input_data, _JSAMPARRAY *output_data_ptr)
318
907k
{
319
907k
  _JSAMPARRAY output_data = *output_data_ptr;
320
907k
  _JSAMPROW inptr0, inptr1, outptr;
321
#if BITS_IN_JSAMPLE == 8
322
  int thiscolsum, bias;
323
#else
324
907k
  JLONG thiscolsum, bias;
325
907k
#endif
326
907k
  JDIMENSION colctr;
327
907k
  int inrow, outrow, v;
328
329
907k
  inrow = outrow = 0;
330
2.34M
  while (outrow < cinfo->max_v_samp_factor) {
331
4.31M
    for (v = 0; v < 2; v++) {
332
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
333
2.87M
      inptr0 = input_data[inrow];
334
2.87M
      if (v == 0) {             /* next nearest is row above */
335
1.43M
        inptr1 = input_data[inrow - 1];
336
1.43M
        bias = 1;
337
1.43M
      } else {                  /* next nearest is row below */
338
1.43M
        inptr1 = input_data[inrow + 1];
339
1.43M
        bias = 2;
340
1.43M
      }
341
2.87M
      outptr = output_data[outrow++];
342
343
62.5M
      for (colctr = 0; colctr < compptr->downsampled_width; colctr++) {
344
59.6M
        thiscolsum = (*inptr0++) * 3 + (*inptr1++);
345
59.6M
        *outptr++ = (_JSAMPLE)((thiscolsum + bias) >> 2);
346
59.6M
      }
347
2.87M
    }
348
1.43M
    inrow++;
349
1.43M
  }
350
907k
}
351
352
353
/*
354
 * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
355
 * Again a triangle filter; see comments for h2v1 case, above.
356
 *
357
 * It is OK for us to reference the adjacent input rows because we demanded
358
 * context from the main buffer controller (see initialization code).
359
 */
360
361
METHODDEF(void)
362
h2v2_fancy_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
363
                    _JSAMPARRAY input_data, _JSAMPARRAY *output_data_ptr)
364
148k
{
365
148k
  _JSAMPARRAY output_data = *output_data_ptr;
366
148k
  register _JSAMPROW inptr0, inptr1, outptr;
367
#if BITS_IN_JSAMPLE == 8
368
  register int thiscolsum, lastcolsum, nextcolsum;
369
#else
370
148k
  register JLONG thiscolsum, lastcolsum, nextcolsum;
371
148k
#endif
372
148k
  register JDIMENSION colctr;
373
148k
  int inrow, outrow, v;
374
375
148k
  inrow = outrow = 0;
376
325k
  while (outrow < cinfo->max_v_samp_factor) {
377
533k
    for (v = 0; v < 2; v++) {
378
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
379
355k
      inptr0 = input_data[inrow];
380
355k
      if (v == 0)               /* next nearest is row above */
381
177k
        inptr1 = input_data[inrow - 1];
382
177k
      else                      /* next nearest is row below */
383
177k
        inptr1 = input_data[inrow + 1];
384
355k
      outptr = output_data[outrow++];
385
386
      /* Special case for first column */
387
355k
      thiscolsum = (*inptr0++) * 3 + (*inptr1++);
388
355k
      nextcolsum = (*inptr0++) * 3 + (*inptr1++);
389
355k
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 8) >> 4);
390
355k
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
391
355k
      lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
392
393
9.27M
      for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
394
        /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
395
        /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
396
8.92M
        nextcolsum = (*inptr0++) * 3 + (*inptr1++);
397
8.92M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
398
8.92M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
399
8.92M
        lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
400
8.92M
      }
401
402
      /* Special case for last column */
403
355k
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
404
355k
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 7) >> 4);
405
355k
    }
406
177k
    inrow++;
407
177k
  }
408
148k
}
409
410
411
/*
412
 * Module initialization routine for upsampling.
413
 */
414
415
GLOBAL(void)
416
_jinit_upsampler(j_decompress_ptr cinfo)
417
46.2k
{
418
46.2k
  my_upsample_ptr upsample;
419
46.2k
  int ci;
420
46.2k
  jpeg_component_info *compptr;
421
46.2k
  boolean need_buffer, do_fancy;
422
46.2k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
46.2k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
46.2k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
46.2k
    upsample = (my_upsample_ptr)
429
46.2k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
46.2k
                                  sizeof(my_upsampler));
431
46.2k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
46.2k
    upsample->pub.start_pass = start_pass_upsample;
433
46.2k
    upsample->pub._upsample = sep_upsample;
434
46.2k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
46.2k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
46.2k
  if (cinfo->CCIR601_sampling)  /* this isn't supported */
439
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
440
441
  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
442
   * so don't ask for it.
443
   */
444
46.2k
  do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
445
446
  /* Verify we can handle the sampling factors, select per-component methods,
447
   * and create storage as needed.
448
   */
449
182k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
135k
       ci++, compptr++) {
451
    /* Compute size of an "input group" after IDCT scaling.  This many samples
452
     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
453
     */
454
135k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
135k
                 cinfo->_min_DCT_scaled_size;
456
135k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
135k
                 cinfo->_min_DCT_scaled_size;
458
135k
    h_out_group = cinfo->max_h_samp_factor;
459
135k
    v_out_group = cinfo->max_v_samp_factor;
460
135k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
135k
    need_buffer = TRUE;
462
135k
    if (!compptr->component_needed) {
463
      /* Don't bother to upsample an uninteresting component. */
464
10.3k
      upsample->methods[ci] = noop_upsample;
465
10.3k
      need_buffer = FALSE;
466
125k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
51.2k
      upsample->methods[ci] = fullsize_upsample;
469
51.2k
      need_buffer = FALSE;
470
74.3k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
15.9k
      if (do_fancy && compptr->downsampled_width > 2) {
473
#ifdef WITH_SIMD
474
964
        if (jsimd_can_h2v1_fancy_upsample())
475
964
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
476
0
        else
477
0
#endif
478
0
          upsample->methods[ci] = h2v1_fancy_upsample;
479
13.6k
      } else {
480
#ifdef WITH_SIMD
481
8.68k
        if (jsimd_can_h2v1_upsample())
482
8.68k
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
0
        else
484
0
#endif
485
0
          upsample->methods[ci] = h2v1_upsample;
486
13.6k
      }
487
58.3k
    } else if (h_in_group == h_out_group &&
488
58.3k
               v_in_group * 2 == v_out_group && do_fancy) {
489
      /* Non-fancy upsampling is handled by the generic method */
490
#if defined(WITH_SIMD) && (defined(__arm__) || defined(__aarch64__) || \
491
                           defined(_M_ARM) || defined(_M_ARM64))
492
      if (jsimd_can_h1v2_fancy_upsample())
493
        upsample->methods[ci] = jsimd_h1v2_fancy_upsample;
494
      else
495
#endif
496
2.92k
        upsample->methods[ci] = h1v2_fancy_upsample;
497
2.92k
      upsample->pub.need_context_rows = TRUE;
498
55.4k
    } else if (h_in_group * 2 == h_out_group &&
499
55.4k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
8.95k
      if (do_fancy && compptr->downsampled_width > 2) {
502
#ifdef WITH_SIMD
503
160
        if (jsimd_can_h2v2_fancy_upsample())
504
160
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
505
0
        else
506
0
#endif
507
0
          upsample->methods[ci] = h2v2_fancy_upsample;
508
1.79k
        upsample->pub.need_context_rows = TRUE;
509
7.15k
      } else {
510
#ifdef WITH_SIMD
511
1.46k
        if (jsimd_can_h2v2_upsample())
512
1.46k
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
0
        else
514
0
#endif
515
0
          upsample->methods[ci] = h2v2_upsample;
516
7.15k
      }
517
46.5k
    } else if ((h_out_group % h_in_group) == 0 &&
518
46.5k
               (v_out_group % v_in_group) == 0) {
519
      /* Generic integral-factors upsampling method */
520
#if defined(WITH_SIMD) && defined(__mips__)
521
      if (jsimd_can_int_upsample())
522
        upsample->methods[ci] = jsimd_int_upsample;
523
      else
524
#endif
525
46.2k
        upsample->methods[ci] = int_upsample;
526
46.2k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
46.2k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
46.2k
    } else
529
229
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
135k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
74.1k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
74.1k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
74.1k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
74.1k
                               (long)cinfo->max_h_samp_factor),
535
74.1k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
74.1k
    }
537
135k
  }
538
46.2k
}
j12init_upsampler
Line
Count
Source
417
18.4k
{
418
18.4k
  my_upsample_ptr upsample;
419
18.4k
  int ci;
420
18.4k
  jpeg_component_info *compptr;
421
18.4k
  boolean need_buffer, do_fancy;
422
18.4k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
18.4k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
18.4k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
18.4k
    upsample = (my_upsample_ptr)
429
18.4k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
18.4k
                                  sizeof(my_upsampler));
431
18.4k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
18.4k
    upsample->pub.start_pass = start_pass_upsample;
433
18.4k
    upsample->pub._upsample = sep_upsample;
434
18.4k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
18.4k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
18.4k
  if (cinfo->CCIR601_sampling)  /* this isn't supported */
439
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
440
441
  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
442
   * so don't ask for it.
443
   */
444
18.4k
  do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
445
446
  /* Verify we can handle the sampling factors, select per-component methods,
447
   * and create storage as needed.
448
   */
449
72.0k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
53.6k
       ci++, compptr++) {
451
    /* Compute size of an "input group" after IDCT scaling.  This many samples
452
     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
453
     */
454
53.6k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
53.6k
                 cinfo->_min_DCT_scaled_size;
456
53.6k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
53.6k
                 cinfo->_min_DCT_scaled_size;
458
53.6k
    h_out_group = cinfo->max_h_samp_factor;
459
53.6k
    v_out_group = cinfo->max_v_samp_factor;
460
53.6k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
53.6k
    need_buffer = TRUE;
462
53.6k
    if (!compptr->component_needed) {
463
      /* Don't bother to upsample an uninteresting component. */
464
5.65k
      upsample->methods[ci] = noop_upsample;
465
5.65k
      need_buffer = FALSE;
466
47.9k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
23.1k
      upsample->methods[ci] = fullsize_upsample;
469
23.1k
      need_buffer = FALSE;
470
24.8k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
5.24k
      if (do_fancy && compptr->downsampled_width > 2) {
473
#ifdef WITH_SIMD
474
        if (jsimd_can_h2v1_fancy_upsample())
475
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
476
        else
477
#endif
478
1.28k
          upsample->methods[ci] = h2v1_fancy_upsample;
479
3.95k
      } else {
480
#ifdef WITH_SIMD
481
        if (jsimd_can_h2v1_upsample())
482
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
        else
484
#endif
485
3.95k
          upsample->methods[ci] = h2v1_upsample;
486
3.95k
      }
487
19.6k
    } else if (h_in_group == h_out_group &&
488
19.6k
               v_in_group * 2 == v_out_group && do_fancy) {
489
      /* Non-fancy upsampling is handled by the generic method */
490
#if defined(WITH_SIMD) && (defined(__arm__) || defined(__aarch64__) || \
491
                           defined(_M_ARM) || defined(_M_ARM64))
492
      if (jsimd_can_h1v2_fancy_upsample())
493
        upsample->methods[ci] = jsimd_h1v2_fancy_upsample;
494
      else
495
#endif
496
1.89k
        upsample->methods[ci] = h1v2_fancy_upsample;
497
1.89k
      upsample->pub.need_context_rows = TRUE;
498
17.7k
    } else if (h_in_group * 2 == h_out_group &&
499
17.7k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
6.65k
      if (do_fancy && compptr->downsampled_width > 2) {
502
#ifdef WITH_SIMD
503
        if (jsimd_can_h2v2_fancy_upsample())
504
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
505
        else
506
#endif
507
1.63k
          upsample->methods[ci] = h2v2_fancy_upsample;
508
1.63k
        upsample->pub.need_context_rows = TRUE;
509
5.01k
      } else {
510
#ifdef WITH_SIMD
511
        if (jsimd_can_h2v2_upsample())
512
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
        else
514
#endif
515
5.01k
          upsample->methods[ci] = h2v2_upsample;
516
5.01k
      }
517
11.0k
    } else if ((h_out_group % h_in_group) == 0 &&
518
11.0k
               (v_out_group % v_in_group) == 0) {
519
      /* Generic integral-factors upsampling method */
520
#if defined(WITH_SIMD) && defined(__mips__)
521
      if (jsimd_can_int_upsample())
522
        upsample->methods[ci] = jsimd_int_upsample;
523
      else
524
#endif
525
10.9k
        upsample->methods[ci] = int_upsample;
526
10.9k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
10.9k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
10.9k
    } else
529
62
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
53.6k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
24.7k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
24.7k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
24.7k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
24.7k
                               (long)cinfo->max_h_samp_factor),
535
24.7k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
24.7k
    }
537
53.6k
  }
538
18.4k
}
jinit_upsampler
Line
Count
Source
417
23.2k
{
418
23.2k
  my_upsample_ptr upsample;
419
23.2k
  int ci;
420
23.2k
  jpeg_component_info *compptr;
421
23.2k
  boolean need_buffer, do_fancy;
422
23.2k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
23.2k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
23.2k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
23.2k
    upsample = (my_upsample_ptr)
429
23.2k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
23.2k
                                  sizeof(my_upsampler));
431
23.2k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
23.2k
    upsample->pub.start_pass = start_pass_upsample;
433
23.2k
    upsample->pub._upsample = sep_upsample;
434
23.2k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
23.2k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
23.2k
  if (cinfo->CCIR601_sampling)  /* this isn't supported */
439
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
440
441
  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
442
   * so don't ask for it.
443
   */
444
23.2k
  do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
445
446
  /* Verify we can handle the sampling factors, select per-component methods,
447
   * and create storage as needed.
448
   */
449
91.9k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
68.6k
       ci++, compptr++) {
451
    /* Compute size of an "input group" after IDCT scaling.  This many samples
452
     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
453
     */
454
68.6k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
68.6k
                 cinfo->_min_DCT_scaled_size;
456
68.6k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
68.6k
                 cinfo->_min_DCT_scaled_size;
458
68.6k
    h_out_group = cinfo->max_h_samp_factor;
459
68.6k
    v_out_group = cinfo->max_v_samp_factor;
460
68.6k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
68.6k
    need_buffer = TRUE;
462
68.6k
    if (!compptr->component_needed) {
463
      /* Don't bother to upsample an uninteresting component. */
464
4.66k
      upsample->methods[ci] = noop_upsample;
465
4.66k
      need_buffer = FALSE;
466
63.9k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
20.5k
      upsample->methods[ci] = fullsize_upsample;
469
20.5k
      need_buffer = FALSE;
470
43.4k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
9.64k
      if (do_fancy && compptr->downsampled_width > 2) {
473
964
#ifdef WITH_SIMD
474
964
        if (jsimd_can_h2v1_fancy_upsample())
475
964
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
476
0
        else
477
0
#endif
478
0
          upsample->methods[ci] = h2v1_fancy_upsample;
479
8.68k
      } else {
480
8.68k
#ifdef WITH_SIMD
481
8.68k
        if (jsimd_can_h2v1_upsample())
482
8.68k
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
0
        else
484
0
#endif
485
0
          upsample->methods[ci] = h2v1_upsample;
486
8.68k
      }
487
33.7k
    } else if (h_in_group == h_out_group &&
488
33.7k
               v_in_group * 2 == v_out_group && do_fancy) {
489
      /* Non-fancy upsampling is handled by the generic method */
490
#if defined(WITH_SIMD) && (defined(__arm__) || defined(__aarch64__) || \
491
                           defined(_M_ARM) || defined(_M_ARM64))
492
      if (jsimd_can_h1v2_fancy_upsample())
493
        upsample->methods[ci] = jsimd_h1v2_fancy_upsample;
494
      else
495
#endif
496
1.02k
        upsample->methods[ci] = h1v2_fancy_upsample;
497
1.02k
      upsample->pub.need_context_rows = TRUE;
498
32.7k
    } else if (h_in_group * 2 == h_out_group &&
499
32.7k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
1.62k
      if (do_fancy && compptr->downsampled_width > 2) {
502
160
#ifdef WITH_SIMD
503
160
        if (jsimd_can_h2v2_fancy_upsample())
504
160
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
505
0
        else
506
0
#endif
507
0
          upsample->methods[ci] = h2v2_fancy_upsample;
508
160
        upsample->pub.need_context_rows = TRUE;
509
1.46k
      } else {
510
1.46k
#ifdef WITH_SIMD
511
1.46k
        if (jsimd_can_h2v2_upsample())
512
1.46k
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
0
        else
514
0
#endif
515
0
          upsample->methods[ci] = h2v2_upsample;
516
1.46k
      }
517
31.1k
    } else if ((h_out_group % h_in_group) == 0 &&
518
31.1k
               (v_out_group % v_in_group) == 0) {
519
      /* Generic integral-factors upsampling method */
520
#if defined(WITH_SIMD) && defined(__mips__)
521
      if (jsimd_can_int_upsample())
522
        upsample->methods[ci] = jsimd_int_upsample;
523
      else
524
#endif
525
30.9k
        upsample->methods[ci] = int_upsample;
526
30.9k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
30.9k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
30.9k
    } else
529
123
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
68.6k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
43.2k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
43.2k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
43.2k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
43.2k
                               (long)cinfo->max_h_samp_factor),
535
43.2k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
43.2k
    }
537
68.6k
  }
538
23.2k
}
j16init_upsampler
Line
Count
Source
417
4.56k
{
418
4.56k
  my_upsample_ptr upsample;
419
4.56k
  int ci;
420
4.56k
  jpeg_component_info *compptr;
421
4.56k
  boolean need_buffer, do_fancy;
422
4.56k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
4.56k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
4.56k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
4.56k
    upsample = (my_upsample_ptr)
429
4.56k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
4.56k
                                  sizeof(my_upsampler));
431
4.56k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
4.56k
    upsample->pub.start_pass = start_pass_upsample;
433
4.56k
    upsample->pub._upsample = sep_upsample;
434
4.56k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
4.56k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
4.56k
  if (cinfo->CCIR601_sampling)  /* this isn't supported */
439
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
440
441
  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
442
   * so don't ask for it.
443
   */
444
4.56k
  do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
445
446
  /* Verify we can handle the sampling factors, select per-component methods,
447
   * and create storage as needed.
448
   */
449
18.1k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
13.6k
       ci++, compptr++) {
451
    /* Compute size of an "input group" after IDCT scaling.  This many samples
452
     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
453
     */
454
13.6k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
13.6k
                 cinfo->_min_DCT_scaled_size;
456
13.6k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
13.6k
                 cinfo->_min_DCT_scaled_size;
458
13.6k
    h_out_group = cinfo->max_h_samp_factor;
459
13.6k
    v_out_group = cinfo->max_v_samp_factor;
460
13.6k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
13.6k
    need_buffer = TRUE;
462
13.6k
    if (!compptr->component_needed) {
463
      /* Don't bother to upsample an uninteresting component. */
464
0
      upsample->methods[ci] = noop_upsample;
465
0
      need_buffer = FALSE;
466
13.6k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
7.53k
      upsample->methods[ci] = fullsize_upsample;
469
7.53k
      need_buffer = FALSE;
470
7.53k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
1.04k
      if (do_fancy && compptr->downsampled_width > 2) {
473
#ifdef WITH_SIMD
474
        if (jsimd_can_h2v1_fancy_upsample())
475
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
476
        else
477
#endif
478
0
          upsample->methods[ci] = h2v1_fancy_upsample;
479
1.04k
      } else {
480
#ifdef WITH_SIMD
481
        if (jsimd_can_h2v1_upsample())
482
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
        else
484
#endif
485
1.04k
          upsample->methods[ci] = h2v1_upsample;
486
1.04k
      }
487
5.03k
    } else if (h_in_group == h_out_group &&
488
5.03k
               v_in_group * 2 == v_out_group && do_fancy) {
489
      /* Non-fancy upsampling is handled by the generic method */
490
#if defined(WITH_SIMD) && (defined(__arm__) || defined(__aarch64__) || \
491
                           defined(_M_ARM) || defined(_M_ARM64))
492
      if (jsimd_can_h1v2_fancy_upsample())
493
        upsample->methods[ci] = jsimd_h1v2_fancy_upsample;
494
      else
495
#endif
496
0
        upsample->methods[ci] = h1v2_fancy_upsample;
497
0
      upsample->pub.need_context_rows = TRUE;
498
5.03k
    } else if (h_in_group * 2 == h_out_group &&
499
5.03k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
670
      if (do_fancy && compptr->downsampled_width > 2) {
502
#ifdef WITH_SIMD
503
        if (jsimd_can_h2v2_fancy_upsample())
504
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
505
        else
506
#endif
507
0
          upsample->methods[ci] = h2v2_fancy_upsample;
508
0
        upsample->pub.need_context_rows = TRUE;
509
670
      } else {
510
#ifdef WITH_SIMD
511
        if (jsimd_can_h2v2_upsample())
512
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
        else
514
#endif
515
670
          upsample->methods[ci] = h2v2_upsample;
516
670
      }
517
4.36k
    } else if ((h_out_group % h_in_group) == 0 &&
518
4.36k
               (v_out_group % v_in_group) == 0) {
519
      /* Generic integral-factors upsampling method */
520
#if defined(WITH_SIMD) && defined(__mips__)
521
      if (jsimd_can_int_upsample())
522
        upsample->methods[ci] = jsimd_int_upsample;
523
      else
524
#endif
525
4.32k
        upsample->methods[ci] = int_upsample;
526
4.32k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
4.32k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
4.32k
    } else
529
44
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
13.6k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
6.04k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
6.04k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
6.04k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
6.04k
                               (long)cinfo->max_h_samp_factor),
535
6.04k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
6.04k
    }
537
13.6k
  }
538
4.56k
}
539
540
#endif /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */