Coverage Report

Created: 2023-10-07 13:33

/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
101k
{
44
101k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
101k
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
101k
  upsample->rows_to_go = cinfo->output_height;
50
101k
}
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
70.4M
{
67
70.4M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
68
70.4M
  int ci;
69
70.4M
  jpeg_component_info *compptr;
70
70.4M
  JDIMENSION num_rows;
71
72
  /* Fill the conversion buffer, if it's empty */
73
70.4M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
74
273M
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
75
202M
         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
202M
      (*upsample->methods[ci]) (cinfo, compptr,
80
202M
        input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
81
202M
        upsample->color_buf + ci);
82
202M
    }
83
70.4M
    upsample->next_row_out = 0;
84
70.4M
  }
85
86
  /* Color-convert and emit rows */
87
88
  /* How many we have in the buffer: */
89
70.4M
  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
70.4M
  if (num_rows > upsample->rows_to_go)
94
27.2k
    num_rows = upsample->rows_to_go;
95
  /* And not more than what the client can accept: */
96
70.4M
  out_rows_avail -= *out_row_ctr;
97
70.4M
  if (num_rows > out_rows_avail)
98
0
    num_rows = out_rows_avail;
99
100
70.4M
  (*cinfo->cconvert->_color_convert) (cinfo, upsample->color_buf,
101
70.4M
                                      (JDIMENSION)upsample->next_row_out,
102
70.4M
                                      output_buf + *out_row_ctr,
103
70.4M
                                      (int)num_rows);
104
105
  /* Adjust counts */
106
70.4M
  *out_row_ctr += num_rows;
107
70.4M
  upsample->rows_to_go -= num_rows;
108
70.4M
  upsample->next_row_out += num_rows;
109
  /* When the buffer is emptied, declare this input row group consumed */
110
70.4M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
111
70.4M
    (*in_row_group_ctr)++;
112
70.4M
}
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
83.5M
{
132
83.5M
  *output_data_ptr = input_data;
133
83.5M
}
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
20.2M
{
145
20.2M
  *output_data_ptr = NULL;      /* safety check */
146
20.2M
}
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
52.2M
{
164
52.2M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
165
52.2M
  _JSAMPARRAY output_data = *output_data_ptr;
166
52.2M
  register _JSAMPROW inptr, outptr;
167
52.2M
  register _JSAMPLE invalue;
168
52.2M
  register int h;
169
52.2M
  _JSAMPROW outend;
170
52.2M
  int h_expand, v_expand;
171
52.2M
  int inrow, outrow;
172
173
52.2M
  h_expand = upsample->h_expand[compptr->component_index];
174
52.2M
  v_expand = upsample->v_expand[compptr->component_index];
175
176
52.2M
  inrow = outrow = 0;
177
114M
  while (outrow < cinfo->max_v_samp_factor) {
178
    /* Generate one output row with proper horizontal expansion */
179
62.3M
    inptr = input_data[inrow];
180
62.3M
    outptr = output_data[outrow];
181
62.3M
    outend = outptr + cinfo->output_width;
182
1.55G
    while (outptr < outend) {
183
1.49G
      invalue = *inptr++;
184
4.09G
      for (h = h_expand; h > 0; h--) {
185
2.60G
        *outptr++ = invalue;
186
2.60G
      }
187
1.49G
    }
188
    /* Generate any additional output rows by duplicating the first one */
189
62.3M
    if (v_expand > 1) {
190
42.0M
      _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1,
191
42.0M
                         v_expand - 1, cinfo->output_width);
192
42.0M
    }
193
62.3M
    inrow++;
194
62.3M
    outrow += v_expand;
195
62.3M
  }
196
52.2M
}
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
17.2M
{
208
17.2M
  _JSAMPARRAY output_data = *output_data_ptr;
209
17.2M
  register _JSAMPROW inptr, outptr;
210
17.2M
  register _JSAMPLE invalue;
211
17.2M
  _JSAMPROW outend;
212
17.2M
  int inrow;
213
214
47.6M
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
215
30.4M
    inptr = input_data[inrow];
216
30.4M
    outptr = output_data[inrow];
217
30.4M
    outend = outptr + cinfo->output_width;
218
519M
    while (outptr < outend) {
219
488M
      invalue = *inptr++;
220
488M
      *outptr++ = invalue;
221
488M
      *outptr++ = invalue;
222
488M
    }
223
30.4M
  }
224
17.2M
}
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
10.2M
{
236
10.2M
  _JSAMPARRAY output_data = *output_data_ptr;
237
10.2M
  register _JSAMPROW inptr, outptr;
238
10.2M
  register _JSAMPLE invalue;
239
10.2M
  _JSAMPROW outend;
240
10.2M
  int inrow, outrow;
241
242
10.2M
  inrow = outrow = 0;
243
20.9M
  while (outrow < cinfo->max_v_samp_factor) {
244
10.6M
    inptr = input_data[inrow];
245
10.6M
    outptr = output_data[outrow];
246
10.6M
    outend = outptr + cinfo->output_width;
247
203M
    while (outptr < outend) {
248
193M
      invalue = *inptr++;
249
193M
      *outptr++ = invalue;
250
193M
      *outptr++ = invalue;
251
193M
    }
252
10.6M
    _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1, 1,
253
10.6M
                       cinfo->output_width);
254
10.6M
    inrow++;
255
10.6M
    outrow += 2;
256
10.6M
  }
257
10.2M
}
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
5.46M
{
279
5.46M
  _JSAMPARRAY output_data = *output_data_ptr;
280
5.46M
  register _JSAMPROW inptr, outptr;
281
5.46M
  register int invalue;
282
5.46M
  register JDIMENSION colctr;
283
5.46M
  int inrow;
284
285
16.4M
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
286
10.9M
    inptr = input_data[inrow];
287
10.9M
    outptr = output_data[inrow];
288
    /* Special case for first column */
289
10.9M
    invalue = *inptr++;
290
10.9M
    *outptr++ = (_JSAMPLE)invalue;
291
10.9M
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[0] + 2) >> 2);
292
293
113M
    for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
294
      /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
295
102M
      invalue = (*inptr++) * 3;
296
102M
      *outptr++ = (_JSAMPLE)((invalue + inptr[-2] + 1) >> 2);
297
102M
      *outptr++ = (_JSAMPLE)((invalue + inptr[0] + 2) >> 2);
298
102M
    }
299
300
    /* Special case for last column */
301
10.9M
    invalue = *inptr;
302
10.9M
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[-1] + 1) >> 2);
303
10.9M
    *outptr++ = (_JSAMPLE)invalue;
304
10.9M
  }
305
5.46M
}
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
4.78M
{
319
4.78M
  _JSAMPARRAY output_data = *output_data_ptr;
320
4.78M
  _JSAMPROW inptr0, inptr1, outptr;
321
#if BITS_IN_JSAMPLE == 8
322
  int thiscolsum, bias;
323
#else
324
4.78M
  JLONG thiscolsum, bias;
325
4.78M
#endif
326
4.78M
  JDIMENSION colctr;
327
4.78M
  int inrow, outrow, v;
328
329
4.78M
  inrow = outrow = 0;
330
10.5M
  while (outrow < cinfo->max_v_samp_factor) {
331
17.3M
    for (v = 0; v < 2; v++) {
332
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
333
11.5M
      inptr0 = input_data[inrow];
334
11.5M
      if (v == 0) {             /* next nearest is row above */
335
5.79M
        inptr1 = input_data[inrow - 1];
336
5.79M
        bias = 1;
337
5.79M
      } else {                  /* next nearest is row below */
338
5.79M
        inptr1 = input_data[inrow + 1];
339
5.79M
        bias = 2;
340
5.79M
      }
341
11.5M
      outptr = output_data[outrow++];
342
343
388M
      for (colctr = 0; colctr < compptr->downsampled_width; colctr++) {
344
377M
        thiscolsum = (*inptr0++) * 3 + (*inptr1++);
345
377M
        *outptr++ = (_JSAMPLE)((thiscolsum + bias) >> 2);
346
377M
      }
347
11.5M
    }
348
5.79M
    inrow++;
349
5.79M
  }
350
4.78M
}
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
343k
{
365
343k
  _JSAMPARRAY output_data = *output_data_ptr;
366
343k
  register _JSAMPROW inptr0, inptr1, outptr;
367
#if BITS_IN_JSAMPLE == 8
368
  register int thiscolsum, lastcolsum, nextcolsum;
369
#else
370
343k
  register JLONG thiscolsum, lastcolsum, nextcolsum;
371
343k
#endif
372
343k
  register JDIMENSION colctr;
373
343k
  int inrow, outrow, v;
374
375
343k
  inrow = outrow = 0;
376
741k
  while (outrow < cinfo->max_v_samp_factor) {
377
1.19M
    for (v = 0; v < 2; v++) {
378
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
379
796k
      inptr0 = input_data[inrow];
380
796k
      if (v == 0)               /* next nearest is row above */
381
398k
        inptr1 = input_data[inrow - 1];
382
398k
      else                      /* next nearest is row below */
383
398k
        inptr1 = input_data[inrow + 1];
384
796k
      outptr = output_data[outrow++];
385
386
      /* Special case for first column */
387
796k
      thiscolsum = (*inptr0++) * 3 + (*inptr1++);
388
796k
      nextcolsum = (*inptr0++) * 3 + (*inptr1++);
389
796k
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 8) >> 4);
390
796k
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
391
796k
      lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
392
393
28.4M
      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
27.6M
        nextcolsum = (*inptr0++) * 3 + (*inptr1++);
397
27.6M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
398
27.6M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
399
27.6M
        lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
400
27.6M
      }
401
402
      /* Special case for last column */
403
796k
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
404
796k
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 7) >> 4);
405
796k
    }
406
398k
    inrow++;
407
398k
  }
408
343k
}
409
410
411
/*
412
 * Module initialization routine for upsampling.
413
 */
414
415
GLOBAL(void)
416
_jinit_upsampler(j_decompress_ptr cinfo)
417
115k
{
418
115k
  my_upsample_ptr upsample;
419
115k
  int ci;
420
115k
  jpeg_component_info *compptr;
421
115k
  boolean need_buffer, do_fancy;
422
115k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
115k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
115k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
115k
    upsample = (my_upsample_ptr)
429
115k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
115k
                                  sizeof(my_upsampler));
431
115k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
115k
    upsample->pub.start_pass = start_pass_upsample;
433
115k
    upsample->pub._upsample = sep_upsample;
434
115k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
115k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
115k
  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
115k
  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
450k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
335k
       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
335k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
335k
                 cinfo->_min_DCT_scaled_size;
456
335k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
335k
                 cinfo->_min_DCT_scaled_size;
458
335k
    h_out_group = cinfo->max_h_samp_factor;
459
335k
    v_out_group = cinfo->max_v_samp_factor;
460
335k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
335k
    need_buffer = TRUE;
462
335k
    if (!compptr->component_needed) {
463
      /* Don't bother to upsample an uninteresting component. */
464
16.4k
      upsample->methods[ci] = noop_upsample;
465
16.4k
      need_buffer = FALSE;
466
318k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
142k
      upsample->methods[ci] = fullsize_upsample;
469
142k
      need_buffer = FALSE;
470
176k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
38.6k
      if (do_fancy && compptr->downsampled_width > 2) {
473
#ifdef WITH_SIMD
474
1.58k
        if (jsimd_can_h2v1_fancy_upsample())
475
1.58k
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
476
0
        else
477
0
#endif
478
0
          upsample->methods[ci] = h2v1_fancy_upsample;
479
33.4k
      } else {
480
#ifdef WITH_SIMD
481
15.2k
        if (jsimd_can_h2v1_upsample())
482
15.2k
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
0
        else
484
0
#endif
485
0
          upsample->methods[ci] = h2v1_upsample;
486
33.4k
      }
487
137k
    } else if (h_in_group == h_out_group &&
488
137k
               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
6.96k
        upsample->methods[ci] = h1v2_fancy_upsample;
497
6.96k
      upsample->pub.need_context_rows = TRUE;
498
130k
    } else if (h_in_group * 2 == h_out_group &&
499
130k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
22.0k
      if (do_fancy && compptr->downsampled_width > 2) {
502
#ifdef WITH_SIMD
503
431
        if (jsimd_can_h2v2_fancy_upsample())
504
431
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
505
0
        else
506
0
#endif
507
0
          upsample->methods[ci] = h2v2_fancy_upsample;
508
2.55k
        upsample->pub.need_context_rows = TRUE;
509
19.4k
      } else {
510
#ifdef WITH_SIMD
511
3.55k
        if (jsimd_can_h2v2_upsample())
512
3.55k
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
0
        else
514
0
#endif
515
0
          upsample->methods[ci] = h2v2_upsample;
516
19.4k
      }
517
108k
    } else if ((h_out_group % h_in_group) == 0 &&
518
108k
               (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
108k
        upsample->methods[ci] = int_upsample;
526
108k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
108k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
108k
    } else
529
335
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
335k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
175k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
175k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
175k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
175k
                               (long)cinfo->max_h_samp_factor),
535
175k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
175k
    }
537
335k
  }
538
115k
}
j12init_upsampler
Line
Count
Source
417
49.8k
{
418
49.8k
  my_upsample_ptr upsample;
419
49.8k
  int ci;
420
49.8k
  jpeg_component_info *compptr;
421
49.8k
  boolean need_buffer, do_fancy;
422
49.8k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
49.8k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
49.8k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
49.8k
    upsample = (my_upsample_ptr)
429
49.8k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
49.8k
                                  sizeof(my_upsampler));
431
49.8k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
49.8k
    upsample->pub.start_pass = start_pass_upsample;
433
49.8k
    upsample->pub._upsample = sep_upsample;
434
49.8k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
49.8k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
49.8k
  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
49.8k
  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
193k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
144k
       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
144k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
144k
                 cinfo->_min_DCT_scaled_size;
456
144k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
144k
                 cinfo->_min_DCT_scaled_size;
458
144k
    h_out_group = cinfo->max_h_samp_factor;
459
144k
    v_out_group = cinfo->max_v_samp_factor;
460
144k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
144k
    need_buffer = TRUE;
462
144k
    if (!compptr->component_needed) {
463
      /* Don't bother to upsample an uninteresting component. */
464
11.7k
      upsample->methods[ci] = noop_upsample;
465
11.7k
      need_buffer = FALSE;
466
132k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
59.5k
      upsample->methods[ci] = fullsize_upsample;
469
59.5k
      need_buffer = FALSE;
470
72.7k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
16.2k
      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
3.65k
          upsample->methods[ci] = h2v1_fancy_upsample;
479
12.5k
      } else {
480
#ifdef WITH_SIMD
481
        if (jsimd_can_h2v1_upsample())
482
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
        else
484
#endif
485
12.5k
          upsample->methods[ci] = h2v1_upsample;
486
12.5k
      }
487
56.5k
    } else if (h_in_group == h_out_group &&
488
56.5k
               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
5.00k
        upsample->methods[ci] = h1v2_fancy_upsample;
497
5.00k
      upsample->pub.need_context_rows = TRUE;
498
51.5k
    } else if (h_in_group * 2 == h_out_group &&
499
51.5k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
13.9k
      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
2.12k
          upsample->methods[ci] = h2v2_fancy_upsample;
508
2.12k
        upsample->pub.need_context_rows = TRUE;
509
11.8k
      } else {
510
#ifdef WITH_SIMD
511
        if (jsimd_can_h2v2_upsample())
512
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
        else
514
#endif
515
11.8k
          upsample->methods[ci] = h2v2_upsample;
516
11.8k
      }
517
37.5k
    } else if ((h_out_group % h_in_group) == 0 &&
518
37.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
37.4k
        upsample->methods[ci] = int_upsample;
526
37.4k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
37.4k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
37.4k
    } else
529
117
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
144k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
72.6k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
72.6k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
72.6k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
72.6k
                               (long)cinfo->max_h_samp_factor),
535
72.6k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
72.6k
    }
537
144k
  }
538
49.8k
}
jinit_upsampler
Line
Count
Source
417
46.3k
{
418
46.3k
  my_upsample_ptr upsample;
419
46.3k
  int ci;
420
46.3k
  jpeg_component_info *compptr;
421
46.3k
  boolean need_buffer, do_fancy;
422
46.3k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
46.3k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
46.3k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
46.3k
    upsample = (my_upsample_ptr)
429
46.3k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
46.3k
                                  sizeof(my_upsampler));
431
46.3k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
46.3k
    upsample->pub.start_pass = start_pass_upsample;
433
46.3k
    upsample->pub._upsample = sep_upsample;
434
46.3k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
46.3k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
46.3k
  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.3k
  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
178k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
132k
       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
132k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
132k
                 cinfo->_min_DCT_scaled_size;
456
132k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
132k
                 cinfo->_min_DCT_scaled_size;
458
132k
    h_out_group = cinfo->max_h_samp_factor;
459
132k
    v_out_group = cinfo->max_v_samp_factor;
460
132k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
132k
    need_buffer = TRUE;
462
132k
    if (!compptr->component_needed) {
463
      /* Don't bother to upsample an uninteresting component. */
464
4.69k
      upsample->methods[ci] = noop_upsample;
465
4.69k
      need_buffer = FALSE;
466
127k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
50.0k
      upsample->methods[ci] = fullsize_upsample;
469
50.0k
      need_buffer = FALSE;
470
77.7k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
16.8k
      if (do_fancy && compptr->downsampled_width > 2) {
473
1.58k
#ifdef WITH_SIMD
474
1.58k
        if (jsimd_can_h2v1_fancy_upsample())
475
1.58k
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
476
0
        else
477
0
#endif
478
0
          upsample->methods[ci] = h2v1_fancy_upsample;
479
15.2k
      } else {
480
15.2k
#ifdef WITH_SIMD
481
15.2k
        if (jsimd_can_h2v1_upsample())
482
15.2k
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
0
        else
484
0
#endif
485
0
          upsample->methods[ci] = h2v1_upsample;
486
15.2k
      }
487
60.8k
    } else if (h_in_group == h_out_group &&
488
60.8k
               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.95k
        upsample->methods[ci] = h1v2_fancy_upsample;
497
1.95k
      upsample->pub.need_context_rows = TRUE;
498
58.9k
    } else if (h_in_group * 2 == h_out_group &&
499
58.9k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
3.98k
      if (do_fancy && compptr->downsampled_width > 2) {
502
431
#ifdef WITH_SIMD
503
431
        if (jsimd_can_h2v2_fancy_upsample())
504
431
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
505
0
        else
506
0
#endif
507
0
          upsample->methods[ci] = h2v2_fancy_upsample;
508
431
        upsample->pub.need_context_rows = TRUE;
509
3.55k
      } else {
510
3.55k
#ifdef WITH_SIMD
511
3.55k
        if (jsimd_can_h2v2_upsample())
512
3.55k
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
0
        else
514
0
#endif
515
0
          upsample->methods[ci] = h2v2_upsample;
516
3.55k
      }
517
54.9k
    } else if ((h_out_group % h_in_group) == 0 &&
518
54.9k
               (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
54.8k
        upsample->methods[ci] = int_upsample;
526
54.8k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
54.8k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
54.8k
    } else
529
97
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
132k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
77.6k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
77.6k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
77.6k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
77.6k
                               (long)cinfo->max_h_samp_factor),
535
77.6k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
77.6k
    }
537
132k
  }
538
46.3k
}
j16init_upsampler
Line
Count
Source
417
19.5k
{
418
19.5k
  my_upsample_ptr upsample;
419
19.5k
  int ci;
420
19.5k
  jpeg_component_info *compptr;
421
19.5k
  boolean need_buffer, do_fancy;
422
19.5k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
19.5k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
19.5k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
19.5k
    upsample = (my_upsample_ptr)
429
19.5k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
19.5k
                                  sizeof(my_upsampler));
431
19.5k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
19.5k
    upsample->pub.start_pass = start_pass_upsample;
433
19.5k
    upsample->pub._upsample = sep_upsample;
434
19.5k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
19.5k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
19.5k
  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
19.5k
  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
78.1k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
58.5k
       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
58.5k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
58.5k
                 cinfo->_min_DCT_scaled_size;
456
58.5k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
58.5k
                 cinfo->_min_DCT_scaled_size;
458
58.5k
    h_out_group = cinfo->max_h_samp_factor;
459
58.5k
    v_out_group = cinfo->max_v_samp_factor;
460
58.5k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
58.5k
    need_buffer = TRUE;
462
58.5k
    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
58.5k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
32.8k
      upsample->methods[ci] = fullsize_upsample;
469
32.8k
      need_buffer = FALSE;
470
32.8k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
5.60k
      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
5.60k
      } else {
480
#ifdef WITH_SIMD
481
        if (jsimd_can_h2v1_upsample())
482
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
        else
484
#endif
485
5.60k
          upsample->methods[ci] = h2v1_upsample;
486
5.60k
      }
487
20.1k
    } else if (h_in_group == h_out_group &&
488
20.1k
               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
20.1k
    } else if (h_in_group * 2 == h_out_group &&
499
20.1k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
4.09k
      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
4.09k
      } else {
510
#ifdef WITH_SIMD
511
        if (jsimd_can_h2v2_upsample())
512
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
        else
514
#endif
515
4.09k
          upsample->methods[ci] = h2v2_upsample;
516
4.09k
      }
517
16.0k
    } else if ((h_out_group % h_in_group) == 0 &&
518
16.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
15.9k
        upsample->methods[ci] = int_upsample;
526
15.9k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
15.9k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
15.9k
    } else
529
121
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
58.5k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
25.6k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
25.6k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
25.6k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
25.6k
                               (long)cinfo->max_h_samp_factor),
535
25.6k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
25.6k
    }
537
58.5k
  }
538
19.5k
}
539
540
#endif /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */