Coverage Report

Created: 2025-11-24 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.3.0.x/jdsample.c
Line
Count
Source
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
7.24k
{
44
7.24k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
7.24k
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
7.24k
  upsample->rows_to_go = cinfo->output_height;
50
7.24k
}
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
16.7M
{
67
16.7M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
68
16.7M
  int ci;
69
16.7M
  jpeg_component_info *compptr;
70
16.7M
  JDIMENSION num_rows;
71
72
  /* Fill the conversion buffer, if it's empty */
73
16.7M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
74
44.5M
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
75
27.8M
         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
27.8M
      (*upsample->methods[ci]) (cinfo, compptr,
80
27.8M
        input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
81
27.8M
        upsample->color_buf + ci);
82
27.8M
    }
83
16.7M
    upsample->next_row_out = 0;
84
16.7M
  }
85
86
  /* Color-convert and emit rows */
87
88
  /* How many we have in the buffer: */
89
16.7M
  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
16.7M
  if (num_rows > upsample->rows_to_go)
94
3.22k
    num_rows = upsample->rows_to_go;
95
  /* And not more than what the client can accept: */
96
16.7M
  out_rows_avail -= *out_row_ctr;
97
16.7M
  if (num_rows > out_rows_avail)
98
0
    num_rows = out_rows_avail;
99
100
16.7M
  (*cinfo->cconvert->_color_convert) (cinfo, upsample->color_buf,
101
16.7M
                                      (JDIMENSION)upsample->next_row_out,
102
16.7M
                                      output_buf + *out_row_ctr,
103
16.7M
                                      (int)num_rows);
104
105
  /* Adjust counts */
106
16.7M
  *out_row_ctr += num_rows;
107
16.7M
  upsample->rows_to_go -= num_rows;
108
16.7M
  upsample->next_row_out += num_rows;
109
  /* When the buffer is emptied, declare this input row group consumed */
110
16.7M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
111
16.7M
    (*in_row_group_ctr)++;
112
16.7M
}
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
18.4M
{
132
18.4M
  *output_data_ptr = input_data;
133
18.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.57M
{
145
2.57M
  *output_data_ptr = NULL;      /* safety check */
146
2.57M
}
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
4.29M
{
164
4.29M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
165
4.29M
  _JSAMPARRAY output_data = *output_data_ptr;
166
4.29M
  register _JSAMPROW inptr, outptr;
167
4.29M
  register _JSAMPLE invalue;
168
4.29M
  register int h;
169
4.29M
  _JSAMPROW outend;
170
4.29M
  int h_expand, v_expand;
171
4.29M
  int inrow, outrow;
172
173
4.29M
  h_expand = upsample->h_expand[compptr->component_index];
174
4.29M
  v_expand = upsample->v_expand[compptr->component_index];
175
176
4.29M
  inrow = outrow = 0;
177
10.3M
  while (outrow < cinfo->max_v_samp_factor) {
178
    /* Generate one output row with proper horizontal expansion */
179
6.06M
    inptr = input_data[inrow];
180
6.06M
    outptr = output_data[outrow];
181
6.06M
    outend = outptr + cinfo->output_width;
182
155M
    while (outptr < outend) {
183
149M
      invalue = *inptr++;
184
404M
      for (h = h_expand; h > 0; h--) {
185
254M
        *outptr++ = invalue;
186
254M
      }
187
149M
    }
188
    /* Generate any additional output rows by duplicating the first one */
189
6.06M
    if (v_expand > 1) {
190
2.83M
      _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1,
191
2.83M
                         v_expand - 1, cinfo->output_width);
192
2.83M
    }
193
6.06M
    inrow++;
194
6.06M
    outrow += v_expand;
195
6.06M
  }
196
4.29M
}
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
527k
{
208
527k
  _JSAMPARRAY output_data = *output_data_ptr;
209
527k
  register _JSAMPROW inptr, outptr;
210
527k
  register _JSAMPLE invalue;
211
527k
  _JSAMPROW outend;
212
527k
  int inrow;
213
214
2.01M
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
215
1.48M
    inptr = input_data[inrow];
216
1.48M
    outptr = output_data[inrow];
217
1.48M
    outend = outptr + cinfo->output_width;
218
28.6M
    while (outptr < outend) {
219
27.1M
      invalue = *inptr++;
220
27.1M
      *outptr++ = invalue;
221
27.1M
      *outptr++ = invalue;
222
27.1M
    }
223
1.48M
  }
224
527k
}
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
256k
{
236
256k
  _JSAMPARRAY output_data = *output_data_ptr;
237
256k
  register _JSAMPROW inptr, outptr;
238
256k
  register _JSAMPLE invalue;
239
256k
  _JSAMPROW outend;
240
256k
  int inrow, outrow;
241
242
256k
  inrow = outrow = 0;
243
673k
  while (outrow < cinfo->max_v_samp_factor) {
244
416k
    inptr = input_data[inrow];
245
416k
    outptr = output_data[outrow];
246
416k
    outend = outptr + cinfo->output_width;
247
9.73M
    while (outptr < outend) {
248
9.32M
      invalue = *inptr++;
249
9.32M
      *outptr++ = invalue;
250
9.32M
      *outptr++ = invalue;
251
9.32M
    }
252
416k
    _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1, 1,
253
416k
                       cinfo->output_width);
254
416k
    inrow++;
255
416k
    outrow += 2;
256
416k
  }
257
256k
}
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
113k
{
279
113k
  _JSAMPARRAY output_data = *output_data_ptr;
280
113k
  register _JSAMPROW inptr, outptr;
281
113k
  register int invalue;
282
113k
  register JDIMENSION colctr;
283
113k
  int inrow;
284
285
244k
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
286
131k
    inptr = input_data[inrow];
287
131k
    outptr = output_data[inrow];
288
    /* Special case for first column */
289
131k
    invalue = *inptr++;
290
131k
    *outptr++ = (_JSAMPLE)invalue;
291
131k
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[0] + 2) >> 2);
292
293
8.14M
    for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
294
      /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
295
8.01M
      invalue = (*inptr++) * 3;
296
8.01M
      *outptr++ = (_JSAMPLE)((invalue + inptr[-2] + 1) >> 2);
297
8.01M
      *outptr++ = (_JSAMPLE)((invalue + inptr[0] + 2) >> 2);
298
8.01M
    }
299
300
    /* Special case for last column */
301
131k
    invalue = *inptr;
302
131k
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[-1] + 1) >> 2);
303
131k
    *outptr++ = (_JSAMPLE)invalue;
304
131k
  }
305
113k
}
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
219k
{
319
219k
  _JSAMPARRAY output_data = *output_data_ptr;
320
219k
  _JSAMPROW inptr0, inptr1, outptr;
321
#if BITS_IN_JSAMPLE == 8
322
  int thiscolsum, bias;
323
#else
324
219k
  JLONG thiscolsum, bias;
325
219k
#endif
326
219k
  JDIMENSION colctr;
327
219k
  int inrow, outrow, v;
328
329
219k
  inrow = outrow = 0;
330
465k
  while (outrow < cinfo->max_v_samp_factor) {
331
738k
    for (v = 0; v < 2; v++) {
332
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
333
492k
      inptr0 = input_data[inrow];
334
492k
      if (v == 0) {             /* next nearest is row above */
335
246k
        inptr1 = input_data[inrow - 1];
336
246k
        bias = 1;
337
246k
      } else {                  /* next nearest is row below */
338
246k
        inptr1 = input_data[inrow + 1];
339
246k
        bias = 2;
340
246k
      }
341
492k
      outptr = output_data[outrow++];
342
343
41.0M
      for (colctr = 0; colctr < compptr->downsampled_width; colctr++) {
344
40.5M
        thiscolsum = (*inptr0++) * 3 + (*inptr1++);
345
40.5M
        *outptr++ = (_JSAMPLE)((thiscolsum + bias) >> 2);
346
40.5M
      }
347
492k
    }
348
246k
    inrow++;
349
246k
  }
350
219k
}
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
13.2k
{
365
13.2k
  _JSAMPARRAY output_data = *output_data_ptr;
366
13.2k
  register _JSAMPROW inptr0, inptr1, outptr;
367
#if BITS_IN_JSAMPLE == 8
368
  register int thiscolsum, lastcolsum, nextcolsum;
369
#else
370
13.2k
  register JLONG thiscolsum, lastcolsum, nextcolsum;
371
13.2k
#endif
372
13.2k
  register JDIMENSION colctr;
373
13.2k
  int inrow, outrow, v;
374
375
13.2k
  inrow = outrow = 0;
376
28.6k
  while (outrow < cinfo->max_v_samp_factor) {
377
46.2k
    for (v = 0; v < 2; v++) {
378
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
379
30.8k
      inptr0 = input_data[inrow];
380
30.8k
      if (v == 0)               /* next nearest is row above */
381
15.4k
        inptr1 = input_data[inrow - 1];
382
15.4k
      else                      /* next nearest is row below */
383
15.4k
        inptr1 = input_data[inrow + 1];
384
30.8k
      outptr = output_data[outrow++];
385
386
      /* Special case for first column */
387
30.8k
      thiscolsum = (*inptr0++) * 3 + (*inptr1++);
388
30.8k
      nextcolsum = (*inptr0++) * 3 + (*inptr1++);
389
30.8k
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 8) >> 4);
390
30.8k
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
391
30.8k
      lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
392
393
3.40M
      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
3.36M
        nextcolsum = (*inptr0++) * 3 + (*inptr1++);
397
3.36M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
398
3.36M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
399
3.36M
        lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
400
3.36M
      }
401
402
      /* Special case for last column */
403
30.8k
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
404
30.8k
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 7) >> 4);
405
30.8k
    }
406
15.4k
    inrow++;
407
15.4k
  }
408
13.2k
}
409
410
411
/*
412
 * Module initialization routine for upsampling.
413
 */
414
415
GLOBAL(void)
416
_jinit_upsampler(j_decompress_ptr cinfo)
417
8.51k
{
418
8.51k
  my_upsample_ptr upsample;
419
8.51k
  int ci;
420
8.51k
  jpeg_component_info *compptr;
421
8.51k
  boolean need_buffer, do_fancy;
422
8.51k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
8.51k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
8.51k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
8.51k
    upsample = (my_upsample_ptr)
429
8.51k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
8.51k
                                  sizeof(my_upsampler));
431
8.51k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
8.51k
    upsample->pub.start_pass = start_pass_upsample;
433
8.51k
    upsample->pub._upsample = sep_upsample;
434
8.51k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
8.51k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
8.51k
  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
8.51k
  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
27.3k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
18.8k
       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
18.8k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
18.8k
                 cinfo->_min_DCT_scaled_size;
456
18.8k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
18.8k
                 cinfo->_min_DCT_scaled_size;
458
18.8k
    h_out_group = cinfo->max_h_samp_factor;
459
18.8k
    v_out_group = cinfo->max_v_samp_factor;
460
18.8k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
18.8k
    need_buffer = TRUE;
462
18.8k
    if (!compptr->component_needed) {
463
      /* Don't bother to upsample an uninteresting component. */
464
1.21k
      upsample->methods[ci] = noop_upsample;
465
1.21k
      need_buffer = FALSE;
466
17.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.85k
      upsample->methods[ci] = fullsize_upsample;
469
7.85k
      need_buffer = FALSE;
470
9.79k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
947
      if (do_fancy && compptr->downsampled_width > 2) {
473
#ifdef WITH_SIMD
474
49
        if (jsimd_can_h2v1_fancy_upsample())
475
49
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
476
0
        else
477
0
#endif
478
0
          upsample->methods[ci] = h2v1_fancy_upsample;
479
814
      } else {
480
#ifdef WITH_SIMD
481
430
        if (jsimd_can_h2v1_upsample())
482
430
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
0
        else
484
0
#endif
485
0
          upsample->methods[ci] = h2v1_upsample;
486
814
      }
487
8.85k
    } else if (h_in_group == h_out_group &&
488
3.51k
               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
243
        upsample->methods[ci] = h1v2_fancy_upsample;
497
243
      upsample->pub.need_context_rows = TRUE;
498
8.60k
    } else if (h_in_group * 2 == h_out_group &&
499
2.85k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
2.51k
      if (do_fancy && compptr->downsampled_width > 2) {
502
#ifdef WITH_SIMD
503
31
        if (jsimd_can_h2v2_fancy_upsample())
504
31
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
505
0
        else
506
0
#endif
507
0
          upsample->methods[ci] = h2v2_fancy_upsample;
508
58
        upsample->pub.need_context_rows = TRUE;
509
2.45k
      } else {
510
#ifdef WITH_SIMD
511
850
        if (jsimd_can_h2v2_upsample())
512
850
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
0
        else
514
0
#endif
515
0
          upsample->methods[ci] = h2v2_upsample;
516
2.45k
      }
517
6.09k
    } else if ((h_out_group % h_in_group) == 0 &&
518
6.08k
               (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
6.06k
        upsample->methods[ci] = int_upsample;
526
6.06k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
6.06k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
6.06k
    } else
529
34
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
18.8k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
9.76k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
9.76k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
9.76k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
9.76k
                               (long)cinfo->max_h_samp_factor),
535
9.76k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
9.76k
    }
537
18.8k
  }
538
8.51k
}
j12init_upsampler
Line
Count
Source
417
2.89k
{
418
2.89k
  my_upsample_ptr upsample;
419
2.89k
  int ci;
420
2.89k
  jpeg_component_info *compptr;
421
2.89k
  boolean need_buffer, do_fancy;
422
2.89k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
2.89k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
2.89k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
2.89k
    upsample = (my_upsample_ptr)
429
2.89k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
2.89k
                                  sizeof(my_upsampler));
431
2.89k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
2.89k
    upsample->pub.start_pass = start_pass_upsample;
433
2.89k
    upsample->pub._upsample = sep_upsample;
434
2.89k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
2.89k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
2.89k
  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
2.89k
  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
8.85k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
5.96k
       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
5.96k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
5.96k
                 cinfo->_min_DCT_scaled_size;
456
5.96k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
5.96k
                 cinfo->_min_DCT_scaled_size;
458
5.96k
    h_out_group = cinfo->max_h_samp_factor;
459
5.96k
    v_out_group = cinfo->max_v_samp_factor;
460
5.96k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
5.96k
    need_buffer = TRUE;
462
5.96k
    if (!compptr->component_needed) {
463
      /* Don't bother to upsample an uninteresting component. */
464
224
      upsample->methods[ci] = noop_upsample;
465
224
      need_buffer = FALSE;
466
5.73k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
2.49k
      upsample->methods[ci] = fullsize_upsample;
469
2.49k
      need_buffer = FALSE;
470
3.24k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
341
      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
84
          upsample->methods[ci] = h2v1_fancy_upsample;
479
257
      } else {
480
#ifdef WITH_SIMD
481
        if (jsimd_can_h2v1_upsample())
482
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
        else
484
#endif
485
257
          upsample->methods[ci] = h2v1_upsample;
486
257
      }
487
2.90k
    } else if (h_in_group == h_out_group &&
488
945
               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
102
        upsample->methods[ci] = h1v2_fancy_upsample;
497
102
      upsample->pub.need_context_rows = TRUE;
498
2.80k
    } else if (h_in_group * 2 == h_out_group &&
499
962
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
861
      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
27
          upsample->methods[ci] = h2v2_fancy_upsample;
508
27
        upsample->pub.need_context_rows = TRUE;
509
834
      } else {
510
#ifdef WITH_SIMD
511
        if (jsimd_can_h2v2_upsample())
512
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
        else
514
#endif
515
834
          upsample->methods[ci] = h2v2_upsample;
516
834
      }
517
1.93k
    } else if ((h_out_group % h_in_group) == 0 &&
518
1.93k
               (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
1.92k
        upsample->methods[ci] = int_upsample;
526
1.92k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
1.92k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
1.92k
    } else
529
17
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
5.96k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
3.22k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
3.22k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
3.22k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
3.22k
                               (long)cinfo->max_h_samp_factor),
535
3.22k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
3.22k
    }
537
5.96k
  }
538
2.89k
}
jinit_upsampler
Line
Count
Source
417
4.69k
{
418
4.69k
  my_upsample_ptr upsample;
419
4.69k
  int ci;
420
4.69k
  jpeg_component_info *compptr;
421
4.69k
  boolean need_buffer, do_fancy;
422
4.69k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
4.69k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
4.69k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
4.69k
    upsample = (my_upsample_ptr)
429
4.69k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
4.69k
                                  sizeof(my_upsampler));
431
4.69k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
4.69k
    upsample->pub.start_pass = start_pass_upsample;
433
4.69k
    upsample->pub._upsample = sep_upsample;
434
4.69k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
4.69k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
4.69k
  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.69k
  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
14.8k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
10.1k
       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
10.1k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
10.1k
                 cinfo->_min_DCT_scaled_size;
456
10.1k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
10.1k
                 cinfo->_min_DCT_scaled_size;
458
10.1k
    h_out_group = cinfo->max_h_samp_factor;
459
10.1k
    v_out_group = cinfo->max_v_samp_factor;
460
10.1k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
10.1k
    need_buffer = TRUE;
462
10.1k
    if (!compptr->component_needed) {
463
      /* Don't bother to upsample an uninteresting component. */
464
990
      upsample->methods[ci] = noop_upsample;
465
990
      need_buffer = FALSE;
466
9.18k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
4.50k
      upsample->methods[ci] = fullsize_upsample;
469
4.50k
      need_buffer = FALSE;
470
4.67k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
479
      if (do_fancy && compptr->downsampled_width > 2) {
473
49
#ifdef WITH_SIMD
474
49
        if (jsimd_can_h2v1_fancy_upsample())
475
49
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
476
0
        else
477
0
#endif
478
0
          upsample->methods[ci] = h2v1_fancy_upsample;
479
430
      } else {
480
430
#ifdef WITH_SIMD
481
430
        if (jsimd_can_h2v1_upsample())
482
430
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
0
        else
484
0
#endif
485
0
          upsample->methods[ci] = h2v1_upsample;
486
430
      }
487
4.19k
    } else if (h_in_group == h_out_group &&
488
2.13k
               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
141
        upsample->methods[ci] = h1v2_fancy_upsample;
497
141
      upsample->pub.need_context_rows = TRUE;
498
4.05k
    } else if (h_in_group * 2 == h_out_group &&
499
1.05k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
881
      if (do_fancy && compptr->downsampled_width > 2) {
502
31
#ifdef WITH_SIMD
503
31
        if (jsimd_can_h2v2_fancy_upsample())
504
31
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
505
0
        else
506
0
#endif
507
0
          upsample->methods[ci] = h2v2_fancy_upsample;
508
31
        upsample->pub.need_context_rows = TRUE;
509
850
      } else {
510
850
#ifdef WITH_SIMD
511
850
        if (jsimd_can_h2v2_upsample())
512
850
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
0
        else
514
0
#endif
515
0
          upsample->methods[ci] = h2v2_upsample;
516
850
      }
517
3.17k
    } else if ((h_out_group % h_in_group) == 0 &&
518
3.17k
               (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
3.16k
        upsample->methods[ci] = int_upsample;
526
3.16k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
3.16k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
3.16k
    } else
529
11
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
10.1k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
4.66k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
4.66k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
4.66k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
4.66k
                               (long)cinfo->max_h_samp_factor),
535
4.66k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
4.66k
    }
537
10.1k
  }
538
4.69k
}
j16init_upsampler
Line
Count
Source
417
919
{
418
919
  my_upsample_ptr upsample;
419
919
  int ci;
420
919
  jpeg_component_info *compptr;
421
919
  boolean need_buffer, do_fancy;
422
919
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
919
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
919
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
919
    upsample = (my_upsample_ptr)
429
919
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
919
                                  sizeof(my_upsampler));
431
919
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
919
    upsample->pub.start_pass = start_pass_upsample;
433
919
    upsample->pub._upsample = sep_upsample;
434
919
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
919
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
919
  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
919
  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
3.65k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
2.73k
       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
2.73k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
2.73k
                 cinfo->_min_DCT_scaled_size;
456
2.73k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
2.73k
                 cinfo->_min_DCT_scaled_size;
458
2.73k
    h_out_group = cinfo->max_h_samp_factor;
459
2.73k
    v_out_group = cinfo->max_v_samp_factor;
460
2.73k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
2.73k
    need_buffer = TRUE;
462
2.73k
    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
2.73k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
856
      upsample->methods[ci] = fullsize_upsample;
469
856
      need_buffer = FALSE;
470
1.87k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
127
      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
127
      } else {
480
#ifdef WITH_SIMD
481
        if (jsimd_can_h2v1_upsample())
482
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
        else
484
#endif
485
127
          upsample->methods[ci] = h2v1_upsample;
486
127
      }
487
1.75k
    } else if (h_in_group == h_out_group &&
488
434
               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
1.75k
    } else if (h_in_group * 2 == h_out_group &&
499
835
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
770
      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
770
      } else {
510
#ifdef WITH_SIMD
511
        if (jsimd_can_h2v2_upsample())
512
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
        else
514
#endif
515
770
          upsample->methods[ci] = h2v2_upsample;
516
770
      }
517
980
    } else if ((h_out_group % h_in_group) == 0 &&
518
978
               (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
974
        upsample->methods[ci] = int_upsample;
526
974
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
974
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
974
    } else
529
6
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
2.73k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
1.87k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
1.87k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
1.87k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
1.87k
                               (long)cinfo->max_h_samp_factor),
535
1.87k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
1.87k
    }
537
2.73k
  }
538
919
}
539
540
#endif /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */