Coverage Report

Created: 2026-11-06 11:29

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