Coverage Report

Created: 2024-09-08 06:06

/src/libjpeg-turbo/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
12.0k
{
44
12.0k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
12.0k
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
12.0k
  upsample->rows_to_go = cinfo->output_height;
50
12.0k
}
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
18.5M
{
67
18.5M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
68
18.5M
  int ci;
69
18.5M
  jpeg_component_info *compptr;
70
18.5M
  JDIMENSION num_rows;
71
72
  /* Fill the conversion buffer, if it's empty */
73
18.5M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
74
23.8M
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
75
13.1M
         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
13.1M
      (*upsample->methods[ci]) (cinfo, compptr,
80
13.1M
        input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
81
13.1M
        upsample->color_buf + ci);
82
13.1M
    }
83
10.6M
    upsample->next_row_out = 0;
84
10.6M
  }
85
86
  /* Color-convert and emit rows */
87
88
  /* How many we have in the buffer: */
89
18.5M
  num_rows = (JDIMENSION)(cinfo->max_v_samp_factor - upsample->next_row_out);
90
  /* Not more than the distance to the end of the image.  Need this test
91
   * in case the image height is not a multiple of max_v_samp_factor:
92
   */
93
18.5M
  if (num_rows > upsample->rows_to_go)
94
1.23k
    num_rows = upsample->rows_to_go;
95
  /* And not more than what the client can accept: */
96
18.5M
  out_rows_avail -= *out_row_ctr;
97
18.5M
  if (num_rows > out_rows_avail)
98
7.91M
    num_rows = out_rows_avail;
99
100
18.5M
  (*cinfo->cconvert->_color_convert) (cinfo, upsample->color_buf,
101
18.5M
                                      (JDIMENSION)upsample->next_row_out,
102
18.5M
                                      output_buf + *out_row_ctr,
103
18.5M
                                      (int)num_rows);
104
105
  /* Adjust counts */
106
18.5M
  *out_row_ctr += num_rows;
107
18.5M
  upsample->rows_to_go -= num_rows;
108
18.5M
  upsample->next_row_out += num_rows;
109
  /* When the buffer is emptied, declare this input row group consumed */
110
18.5M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
111
10.6M
    (*in_row_group_ctr)++;
112
18.5M
}
113
114
115
/*
116
 * These are the routines invoked by sep_upsample to upsample pixel values
117
 * of a single component.  One row group is processed per call.
118
 */
119
120
121
/*
122
 * For full-size components, we just make color_buf[ci] point at the
123
 * input buffer, and thus avoid copying any data.  Note that this is
124
 * safe only because sep_upsample doesn't declare the input row group
125
 * "consumed" until we are done color converting and emitting it.
126
 */
127
128
METHODDEF(void)
129
fullsize_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
130
                  _JSAMPARRAY input_data, _JSAMPARRAY *output_data_ptr)
131
11.0M
{
132
11.0M
  *output_data_ptr = input_data;
133
11.0M
}
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
0
{
145
0
  *output_data_ptr = NULL;      /* safety check */
146
0
}
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
517k
{
164
517k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
165
517k
  _JSAMPARRAY output_data = *output_data_ptr;
166
517k
  register _JSAMPROW inptr, outptr;
167
517k
  register _JSAMPLE invalue;
168
517k
  register int h;
169
517k
  _JSAMPROW outend;
170
517k
  int h_expand, v_expand;
171
517k
  int inrow, outrow;
172
173
517k
  h_expand = upsample->h_expand[compptr->component_index];
174
517k
  v_expand = upsample->v_expand[compptr->component_index];
175
176
517k
  inrow = outrow = 0;
177
1.16M
  while (outrow < cinfo->max_v_samp_factor) {
178
    /* Generate one output row with proper horizontal expansion */
179
645k
    inptr = input_data[inrow];
180
645k
    outptr = output_data[outrow];
181
645k
    outend = outptr + cinfo->output_width;
182
39.7M
    while (outptr < outend) {
183
39.1M
      invalue = *inptr++;
184
121M
      for (h = h_expand; h > 0; h--) {
185
82.3M
        *outptr++ = invalue;
186
82.3M
      }
187
39.1M
    }
188
    /* Generate any additional output rows by duplicating the first one */
189
645k
    if (v_expand > 1) {
190
398k
      _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1,
191
398k
                         v_expand - 1, cinfo->output_width);
192
398k
    }
193
645k
    inrow++;
194
645k
    outrow += v_expand;
195
645k
  }
196
517k
}
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
58.0k
{
208
58.0k
  _JSAMPARRAY output_data = *output_data_ptr;
209
58.0k
  register _JSAMPROW inptr, outptr;
210
58.0k
  register _JSAMPLE invalue;
211
58.0k
  _JSAMPROW outend;
212
58.0k
  int inrow;
213
214
225k
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
215
167k
    inptr = input_data[inrow];
216
167k
    outptr = output_data[inrow];
217
167k
    outend = outptr + cinfo->output_width;
218
468k
    while (outptr < outend) {
219
301k
      invalue = *inptr++;
220
301k
      *outptr++ = invalue;
221
301k
      *outptr++ = invalue;
222
301k
    }
223
167k
  }
224
58.0k
}
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
139k
{
236
139k
  _JSAMPARRAY output_data = *output_data_ptr;
237
139k
  register _JSAMPROW inptr, outptr;
238
139k
  register _JSAMPLE invalue;
239
139k
  _JSAMPROW outend;
240
139k
  int inrow, outrow;
241
242
139k
  inrow = outrow = 0;
243
294k
  while (outrow < cinfo->max_v_samp_factor) {
244
155k
    inptr = input_data[inrow];
245
155k
    outptr = output_data[outrow];
246
155k
    outend = outptr + cinfo->output_width;
247
407k
    while (outptr < outend) {
248
252k
      invalue = *inptr++;
249
252k
      *outptr++ = invalue;
250
252k
      *outptr++ = invalue;
251
252k
    }
252
155k
    _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1, 1,
253
155k
                       cinfo->output_width);
254
155k
    inrow++;
255
155k
    outrow += 2;
256
155k
  }
257
139k
}
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
518k
{
279
518k
  _JSAMPARRAY output_data = *output_data_ptr;
280
518k
  register _JSAMPROW inptr, outptr;
281
518k
  register int invalue;
282
518k
  register JDIMENSION colctr;
283
518k
  int inrow;
284
285
1.61M
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
286
1.09M
    inptr = input_data[inrow];
287
1.09M
    outptr = output_data[inrow];
288
    /* Special case for first column */
289
1.09M
    invalue = *inptr++;
290
1.09M
    *outptr++ = (_JSAMPLE)invalue;
291
1.09M
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[0] + 2) >> 2);
292
293
62.1M
    for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
294
      /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
295
61.0M
      invalue = (*inptr++) * 3;
296
61.0M
      *outptr++ = (_JSAMPLE)((invalue + inptr[-2] + 1) >> 2);
297
61.0M
      *outptr++ = (_JSAMPLE)((invalue + inptr[0] + 2) >> 2);
298
61.0M
    }
299
300
    /* Special case for last column */
301
1.09M
    invalue = *inptr;
302
1.09M
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[-1] + 1) >> 2);
303
1.09M
    *outptr++ = (_JSAMPLE)invalue;
304
1.09M
  }
305
518k
}
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
249k
{
319
249k
  _JSAMPARRAY output_data = *output_data_ptr;
320
249k
  _JSAMPROW inptr0, inptr1, outptr;
321
249k
#if BITS_IN_JSAMPLE == 8
322
249k
  int thiscolsum, bias;
323
#else
324
  JLONG thiscolsum, bias;
325
#endif
326
249k
  JDIMENSION colctr;
327
249k
  int inrow, outrow, v;
328
329
249k
  inrow = outrow = 0;
330
581k
  while (outrow < cinfo->max_v_samp_factor) {
331
997k
    for (v = 0; v < 2; v++) {
332
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
333
665k
      inptr0 = input_data[inrow];
334
665k
      if (v == 0) {             /* next nearest is row above */
335
332k
        inptr1 = input_data[inrow - 1];
336
332k
        bias = 1;
337
332k
      } else {                  /* next nearest is row below */
338
332k
        inptr1 = input_data[inrow + 1];
339
332k
        bias = 2;
340
332k
      }
341
665k
      outptr = output_data[outrow++];
342
343
76.6M
      for (colctr = 0; colctr < compptr->downsampled_width; colctr++) {
344
76.0M
        thiscolsum = (*inptr0++) * 3 + (*inptr1++);
345
76.0M
        *outptr++ = (_JSAMPLE)((thiscolsum + bias) >> 2);
346
76.0M
      }
347
665k
    }
348
332k
    inrow++;
349
332k
  }
350
249k
}
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
606k
{
365
606k
  _JSAMPARRAY output_data = *output_data_ptr;
366
606k
  register _JSAMPROW inptr0, inptr1, outptr;
367
606k
#if BITS_IN_JSAMPLE == 8
368
606k
  register int thiscolsum, lastcolsum, nextcolsum;
369
#else
370
  register JLONG thiscolsum, lastcolsum, nextcolsum;
371
#endif
372
606k
  register JDIMENSION colctr;
373
606k
  int inrow, outrow, v;
374
375
606k
  inrow = outrow = 0;
376
1.27M
  while (outrow < cinfo->max_v_samp_factor) {
377
2.00M
    for (v = 0; v < 2; v++) {
378
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
379
1.33M
      inptr0 = input_data[inrow];
380
1.33M
      if (v == 0)               /* next nearest is row above */
381
667k
        inptr1 = input_data[inrow - 1];
382
667k
      else                      /* next nearest is row below */
383
667k
        inptr1 = input_data[inrow + 1];
384
1.33M
      outptr = output_data[outrow++];
385
386
      /* Special case for first column */
387
1.33M
      thiscolsum = (*inptr0++) * 3 + (*inptr1++);
388
1.33M
      nextcolsum = (*inptr0++) * 3 + (*inptr1++);
389
1.33M
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 8) >> 4);
390
1.33M
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
391
1.33M
      lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
392
393
304M
      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
303M
        nextcolsum = (*inptr0++) * 3 + (*inptr1++);
397
303M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
398
303M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
399
303M
        lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
400
303M
      }
401
402
      /* Special case for last column */
403
1.33M
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
404
1.33M
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 7) >> 4);
405
1.33M
    }
406
667k
    inrow++;
407
667k
  }
408
606k
}
409
410
411
/*
412
 * Module initialization routine for upsampling.
413
 */
414
415
GLOBAL(void)
416
_jinit_upsampler(j_decompress_ptr cinfo)
417
19.3k
{
418
19.3k
  my_upsample_ptr upsample;
419
19.3k
  int ci;
420
19.3k
  jpeg_component_info *compptr;
421
19.3k
  boolean need_buffer, do_fancy;
422
19.3k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
19.3k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
19.3k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
19.3k
    upsample = (my_upsample_ptr)
429
19.3k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
19.3k
                                  sizeof(my_upsampler));
431
19.3k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
19.3k
    upsample->pub.start_pass = start_pass_upsample;
433
19.3k
    upsample->pub._upsample = sep_upsample;
434
19.3k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
19.3k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
19.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
19.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
67.7k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
48.3k
       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
48.3k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
48.3k
                 cinfo->_min_DCT_scaled_size;
456
48.3k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
48.3k
                 cinfo->_min_DCT_scaled_size;
458
48.3k
    h_out_group = cinfo->max_h_samp_factor;
459
48.3k
    v_out_group = cinfo->max_v_samp_factor;
460
48.3k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
48.3k
    need_buffer = TRUE;
462
48.3k
    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
48.3k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
28.0k
      upsample->methods[ci] = fullsize_upsample;
469
28.0k
      need_buffer = FALSE;
470
28.0k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
2.31k
      if (do_fancy && compptr->downsampled_width > 2) {
473
#ifdef WITH_SIMD
474
553
        if (jsimd_can_h2v1_fancy_upsample())
475
0
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
476
553
        else
477
553
#endif
478
553
          upsample->methods[ci] = h2v1_fancy_upsample;
479
1.56k
      } else {
480
#ifdef WITH_SIMD
481
515
        if (jsimd_can_h2v1_upsample())
482
0
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
515
        else
484
515
#endif
485
515
          upsample->methods[ci] = h2v1_upsample;
486
1.56k
      }
487
17.9k
    } else if (h_in_group == h_out_group &&
488
17.9k
               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.36k
        upsample->methods[ci] = h1v2_fancy_upsample;
497
1.36k
      upsample->pub.need_context_rows = TRUE;
498
16.5k
    } else if (h_in_group * 2 == h_out_group &&
499
16.5k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
8.67k
      if (do_fancy && compptr->downsampled_width > 2) {
502
#ifdef WITH_SIMD
503
6.91k
        if (jsimd_can_h2v2_fancy_upsample())
504
0
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
505
6.91k
        else
506
6.91k
#endif
507
6.91k
          upsample->methods[ci] = h2v2_fancy_upsample;
508
7.15k
        upsample->pub.need_context_rows = TRUE;
509
7.15k
      } else {
510
#ifdef WITH_SIMD
511
466
        if (jsimd_can_h2v2_upsample())
512
0
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
466
        else
514
466
#endif
515
466
          upsample->methods[ci] = h2v2_upsample;
516
1.51k
      }
517
8.67k
    } else if ((h_out_group % h_in_group) == 0 &&
518
7.92k
               (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
7.84k
        upsample->methods[ci] = int_upsample;
526
7.84k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
7.84k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
7.84k
    } else
529
73
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
48.3k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
20.2k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
20.2k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
20.2k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
20.2k
                               (long)cinfo->max_h_samp_factor),
535
20.2k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
20.2k
    }
537
48.3k
  }
538
19.3k
}
jinit_upsampler
Line
Count
Source
417
15.7k
{
418
15.7k
  my_upsample_ptr upsample;
419
15.7k
  int ci;
420
15.7k
  jpeg_component_info *compptr;
421
15.7k
  boolean need_buffer, do_fancy;
422
15.7k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
15.7k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
15.7k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
15.7k
    upsample = (my_upsample_ptr)
429
15.7k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
15.7k
                                  sizeof(my_upsampler));
431
15.7k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
15.7k
    upsample->pub.start_pass = start_pass_upsample;
433
15.7k
    upsample->pub._upsample = sep_upsample;
434
15.7k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
15.7k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
15.7k
  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
15.7k
  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
54.0k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
38.2k
       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
38.2k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
38.2k
                 cinfo->_min_DCT_scaled_size;
456
38.2k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
38.2k
                 cinfo->_min_DCT_scaled_size;
458
38.2k
    h_out_group = cinfo->max_h_samp_factor;
459
38.2k
    v_out_group = cinfo->max_v_samp_factor;
460
38.2k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
38.2k
    need_buffer = TRUE;
462
38.2k
    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
38.2k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
26.0k
      upsample->methods[ci] = fullsize_upsample;
469
26.0k
      need_buffer = FALSE;
470
26.0k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
1.06k
      if (do_fancy && compptr->downsampled_width > 2) {
473
553
#ifdef WITH_SIMD
474
553
        if (jsimd_can_h2v1_fancy_upsample())
475
0
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
476
553
        else
477
553
#endif
478
553
          upsample->methods[ci] = h2v1_fancy_upsample;
479
553
      } else {
480
515
#ifdef WITH_SIMD
481
515
        if (jsimd_can_h2v1_upsample())
482
0
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
515
        else
484
515
#endif
485
515
          upsample->methods[ci] = h2v1_upsample;
486
515
      }
487
11.1k
    } else if (h_in_group == h_out_group &&
488
11.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
1.06k
        upsample->methods[ci] = h1v2_fancy_upsample;
497
1.06k
      upsample->pub.need_context_rows = TRUE;
498
10.1k
    } else if (h_in_group * 2 == h_out_group &&
499
10.1k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
7.38k
      if (do_fancy && compptr->downsampled_width > 2) {
502
6.91k
#ifdef WITH_SIMD
503
6.91k
        if (jsimd_can_h2v2_fancy_upsample())
504
0
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
505
6.91k
        else
506
6.91k
#endif
507
6.91k
          upsample->methods[ci] = h2v2_fancy_upsample;
508
6.91k
        upsample->pub.need_context_rows = TRUE;
509
6.91k
      } else {
510
466
#ifdef WITH_SIMD
511
466
        if (jsimd_can_h2v2_upsample())
512
0
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
466
        else
514
466
#endif
515
466
          upsample->methods[ci] = h2v2_upsample;
516
466
      }
517
7.38k
    } else if ((h_out_group % h_in_group) == 0 &&
518
2.74k
               (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
2.72k
        upsample->methods[ci] = int_upsample;
526
2.72k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
2.72k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
2.72k
    } else
529
26
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
38.2k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
12.2k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
12.2k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
12.2k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
12.2k
                               (long)cinfo->max_h_samp_factor),
535
12.2k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
12.2k
    }
537
38.2k
  }
538
15.7k
}
j12init_upsampler
Line
Count
Source
417
2.09k
{
418
2.09k
  my_upsample_ptr upsample;
419
2.09k
  int ci;
420
2.09k
  jpeg_component_info *compptr;
421
2.09k
  boolean need_buffer, do_fancy;
422
2.09k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
2.09k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
2.09k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
2.09k
    upsample = (my_upsample_ptr)
429
2.09k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
2.09k
                                  sizeof(my_upsampler));
431
2.09k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
2.09k
    upsample->pub.start_pass = start_pass_upsample;
433
2.09k
    upsample->pub._upsample = sep_upsample;
434
2.09k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
2.09k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
2.09k
  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.09k
  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
7.85k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
5.76k
       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.76k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
5.76k
                 cinfo->_min_DCT_scaled_size;
456
5.76k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
5.76k
                 cinfo->_min_DCT_scaled_size;
458
5.76k
    h_out_group = cinfo->max_h_samp_factor;
459
5.76k
    v_out_group = cinfo->max_v_samp_factor;
460
5.76k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
5.76k
    need_buffer = TRUE;
462
5.76k
    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
5.76k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
1.29k
      upsample->methods[ci] = fullsize_upsample;
469
1.29k
      need_buffer = FALSE;
470
4.46k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
724
      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
152
          upsample->methods[ci] = h2v1_fancy_upsample;
479
572
      } else {
480
#ifdef WITH_SIMD
481
        if (jsimd_can_h2v1_upsample())
482
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
        else
484
#endif
485
572
          upsample->methods[ci] = h2v1_upsample;
486
572
      }
487
3.74k
    } else if (h_in_group == h_out_group &&
488
3.74k
               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
225
        upsample->methods[ci] = h1v2_fancy_upsample;
497
225
      upsample->pub.need_context_rows = TRUE;
498
3.51k
    } else if (h_in_group * 2 == h_out_group &&
499
3.51k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
882
      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
174
          upsample->methods[ci] = h2v2_fancy_upsample;
508
174
        upsample->pub.need_context_rows = TRUE;
509
708
      } else {
510
#ifdef WITH_SIMD
511
        if (jsimd_can_h2v2_upsample())
512
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
        else
514
#endif
515
708
          upsample->methods[ci] = h2v2_upsample;
516
708
      }
517
2.63k
    } else if ((h_out_group % h_in_group) == 0 &&
518
2.63k
               (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
2.61k
        upsample->methods[ci] = int_upsample;
526
2.61k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
2.61k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
2.61k
    } else
529
19
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
5.76k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
4.44k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
4.44k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
4.44k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
4.44k
                               (long)cinfo->max_h_samp_factor),
535
4.44k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
4.44k
    }
537
5.76k
  }
538
2.09k
}
j16init_upsampler
Line
Count
Source
417
1.46k
{
418
1.46k
  my_upsample_ptr upsample;
419
1.46k
  int ci;
420
1.46k
  jpeg_component_info *compptr;
421
1.46k
  boolean need_buffer, do_fancy;
422
1.46k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
1.46k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
1.46k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
1.46k
    upsample = (my_upsample_ptr)
429
1.46k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
1.46k
                                  sizeof(my_upsampler));
431
1.46k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
1.46k
    upsample->pub.start_pass = start_pass_upsample;
433
1.46k
    upsample->pub._upsample = sep_upsample;
434
1.46k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
1.46k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
1.46k
  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
1.46k
  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
5.77k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
4.30k
       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
4.30k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
4.30k
                 cinfo->_min_DCT_scaled_size;
456
4.30k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
4.30k
                 cinfo->_min_DCT_scaled_size;
458
4.30k
    h_out_group = cinfo->max_h_samp_factor;
459
4.30k
    v_out_group = cinfo->max_v_samp_factor;
460
4.30k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
4.30k
    need_buffer = TRUE;
462
4.30k
    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
4.30k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
754
      upsample->methods[ci] = fullsize_upsample;
469
754
      need_buffer = FALSE;
470
3.54k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
521
      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
42
          upsample->methods[ci] = h2v1_fancy_upsample;
479
479
      } else {
480
#ifdef WITH_SIMD
481
        if (jsimd_can_h2v1_upsample())
482
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
        else
484
#endif
485
479
          upsample->methods[ci] = h2v1_upsample;
486
479
      }
487
3.02k
    } else if (h_in_group == h_out_group &&
488
3.02k
               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
75
        upsample->methods[ci] = h1v2_fancy_upsample;
497
75
      upsample->pub.need_context_rows = TRUE;
498
2.95k
    } else if (h_in_group * 2 == h_out_group &&
499
2.95k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
414
      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
70
          upsample->methods[ci] = h2v2_fancy_upsample;
508
70
        upsample->pub.need_context_rows = TRUE;
509
344
      } else {
510
#ifdef WITH_SIMD
511
        if (jsimd_can_h2v2_upsample())
512
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
        else
514
#endif
515
344
          upsample->methods[ci] = h2v2_upsample;
516
344
      }
517
2.53k
    } else if ((h_out_group % h_in_group) == 0 &&
518
2.53k
               (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
2.51k
        upsample->methods[ci] = int_upsample;
526
2.51k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
2.51k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
2.51k
    } else
529
28
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
4.30k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
3.52k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
3.52k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
3.52k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
3.52k
                               (long)cinfo->max_h_samp_factor),
535
3.52k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
3.52k
    }
537
4.30k
  }
538
1.46k
}
539
540
#endif /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */