Coverage Report

Created: 2024-06-09 08:56

/src/libjpeg-turbo.main/jdsample.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jdsample.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1991-1996, Thomas G. Lane.
6
 * libjpeg-turbo Modifications:
7
 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
 * Copyright (C) 2010, 2015-2016, 2022, D. R. Commander.
9
 * Copyright (C) 2014, MIPS Technologies, Inc., California.
10
 * Copyright (C) 2015, Google, Inc.
11
 * Copyright (C) 2019-2020, Arm Limited.
12
 * For conditions of distribution and use, see the accompanying README.ijg
13
 * file.
14
 *
15
 * This file contains upsampling routines.
16
 *
17
 * Upsampling input data is counted in "row groups".  A row group
18
 * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
19
 * sample rows of each component.  Upsampling will normally produce
20
 * max_v_samp_factor pixel rows from each row group (but this could vary
21
 * if the upsampler is applying a scale factor of its own).
22
 *
23
 * An excellent reference for image resampling is
24
 *   Digital Image Warping, George Wolberg, 1990.
25
 *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
26
 */
27
28
#include "jinclude.h"
29
#include "jdsample.h"
30
#include "jsimd.h"
31
#include "jpegapicomp.h"
32
33
34
35
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
36
37
/*
38
 * Initialize for an upsampling pass.
39
 */
40
41
METHODDEF(void)
42
start_pass_upsample(j_decompress_ptr cinfo)
43
112k
{
44
112k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
112k
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
112k
  upsample->rows_to_go = cinfo->output_height;
50
112k
}
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
84.1M
{
67
84.1M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
68
84.1M
  int ci;
69
84.1M
  jpeg_component_info *compptr;
70
84.1M
  JDIMENSION num_rows;
71
72
  /* Fill the conversion buffer, if it's empty */
73
84.1M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
74
327M
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
75
242M
         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
242M
      (*upsample->methods[ci]) (cinfo, compptr,
80
242M
        input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
81
242M
        upsample->color_buf + ci);
82
242M
    }
83
84.1M
    upsample->next_row_out = 0;
84
84.1M
  }
85
86
  /* Color-convert and emit rows */
87
88
  /* How many we have in the buffer: */
89
84.1M
  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
84.1M
  if (num_rows > upsample->rows_to_go)
94
33.0k
    num_rows = upsample->rows_to_go;
95
  /* And not more than what the client can accept: */
96
84.1M
  out_rows_avail -= *out_row_ctr;
97
84.1M
  if (num_rows > out_rows_avail)
98
0
    num_rows = out_rows_avail;
99
100
84.1M
  (*cinfo->cconvert->_color_convert) (cinfo, upsample->color_buf,
101
84.1M
                                      (JDIMENSION)upsample->next_row_out,
102
84.1M
                                      output_buf + *out_row_ctr,
103
84.1M
                                      (int)num_rows);
104
105
  /* Adjust counts */
106
84.1M
  *out_row_ctr += num_rows;
107
84.1M
  upsample->rows_to_go -= num_rows;
108
84.1M
  upsample->next_row_out += num_rows;
109
  /* When the buffer is emptied, declare this input row group consumed */
110
84.1M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
111
84.1M
    (*in_row_group_ctr)++;
112
84.1M
}
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
100M
{
132
100M
  *output_data_ptr = input_data;
133
100M
}
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
19.9M
{
145
19.9M
  *output_data_ptr = NULL;      /* safety check */
146
19.9M
}
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
72.0M
{
164
72.0M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
165
72.0M
  _JSAMPARRAY output_data = *output_data_ptr;
166
72.0M
  register _JSAMPROW inptr, outptr;
167
72.0M
  register _JSAMPLE invalue;
168
72.0M
  register int h;
169
72.0M
  _JSAMPROW outend;
170
72.0M
  int h_expand, v_expand;
171
72.0M
  int inrow, outrow;
172
173
72.0M
  h_expand = upsample->h_expand[compptr->component_index];
174
72.0M
  v_expand = upsample->v_expand[compptr->component_index];
175
176
72.0M
  inrow = outrow = 0;
177
154M
  while (outrow < cinfo->max_v_samp_factor) {
178
    /* Generate one output row with proper horizontal expansion */
179
82.2M
    inptr = input_data[inrow];
180
82.2M
    outptr = output_data[outrow];
181
82.2M
    outend = outptr + cinfo->output_width;
182
1.47G
    while (outptr < outend) {
183
1.39G
      invalue = *inptr++;
184
3.83G
      for (h = h_expand; h > 0; h--) {
185
2.43G
        *outptr++ = invalue;
186
2.43G
      }
187
1.39G
    }
188
    /* Generate any additional output rows by duplicating the first one */
189
82.2M
    if (v_expand > 1) {
190
56.5M
      _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1,
191
56.5M
                         v_expand - 1, cinfo->output_width);
192
56.5M
    }
193
82.2M
    inrow++;
194
82.2M
    outrow += v_expand;
195
82.2M
  }
196
72.0M
}
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
18.9M
{
208
18.9M
  _JSAMPARRAY output_data = *output_data_ptr;
209
18.9M
  register _JSAMPROW inptr, outptr;
210
18.9M
  register _JSAMPLE invalue;
211
18.9M
  _JSAMPROW outend;
212
18.9M
  int inrow;
213
214
56.6M
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
215
37.7M
    inptr = input_data[inrow];
216
37.7M
    outptr = output_data[inrow];
217
37.7M
    outend = outptr + cinfo->output_width;
218
427M
    while (outptr < outend) {
219
390M
      invalue = *inptr++;
220
390M
      *outptr++ = invalue;
221
390M
      *outptr++ = invalue;
222
390M
    }
223
37.7M
  }
224
18.9M
}
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
9.96M
{
236
9.96M
  _JSAMPARRAY output_data = *output_data_ptr;
237
9.96M
  register _JSAMPROW inptr, outptr;
238
9.96M
  register _JSAMPLE invalue;
239
9.96M
  _JSAMPROW outend;
240
9.96M
  int inrow, outrow;
241
242
9.96M
  inrow = outrow = 0;
243
20.4M
  while (outrow < cinfo->max_v_samp_factor) {
244
10.4M
    inptr = input_data[inrow];
245
10.4M
    outptr = output_data[outrow];
246
10.4M
    outend = outptr + cinfo->output_width;
247
158M
    while (outptr < outend) {
248
147M
      invalue = *inptr++;
249
147M
      *outptr++ = invalue;
250
147M
      *outptr++ = invalue;
251
147M
    }
252
10.4M
    _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1, 1,
253
10.4M
                       cinfo->output_width);
254
10.4M
    inrow++;
255
10.4M
    outrow += 2;
256
10.4M
  }
257
9.96M
}
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
3.75M
{
279
3.75M
  _JSAMPARRAY output_data = *output_data_ptr;
280
3.75M
  register _JSAMPROW inptr, outptr;
281
3.75M
  register int invalue;
282
3.75M
  register JDIMENSION colctr;
283
3.75M
  int inrow;
284
285
11.1M
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
286
7.41M
    inptr = input_data[inrow];
287
7.41M
    outptr = output_data[inrow];
288
    /* Special case for first column */
289
7.41M
    invalue = *inptr++;
290
7.41M
    *outptr++ = (_JSAMPLE)invalue;
291
7.41M
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[0] + 2) >> 2);
292
293
100M
    for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
294
      /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
295
93.3M
      invalue = (*inptr++) * 3;
296
93.3M
      *outptr++ = (_JSAMPLE)((invalue + inptr[-2] + 1) >> 2);
297
93.3M
      *outptr++ = (_JSAMPLE)((invalue + inptr[0] + 2) >> 2);
298
93.3M
    }
299
300
    /* Special case for last column */
301
7.41M
    invalue = *inptr;
302
7.41M
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[-1] + 1) >> 2);
303
7.41M
    *outptr++ = (_JSAMPLE)invalue;
304
7.41M
  }
305
3.75M
}
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
5.65M
{
319
5.65M
  _JSAMPARRAY output_data = *output_data_ptr;
320
5.65M
  _JSAMPROW inptr0, inptr1, outptr;
321
#if BITS_IN_JSAMPLE == 8
322
  int thiscolsum, bias;
323
#else
324
5.65M
  JLONG thiscolsum, bias;
325
5.65M
#endif
326
5.65M
  JDIMENSION colctr;
327
5.65M
  int inrow, outrow, v;
328
329
5.65M
  inrow = outrow = 0;
330
12.7M
  while (outrow < cinfo->max_v_samp_factor) {
331
21.3M
    for (v = 0; v < 2; v++) {
332
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
333
14.2M
      inptr0 = input_data[inrow];
334
14.2M
      if (v == 0) {             /* next nearest is row above */
335
7.11M
        inptr1 = input_data[inrow - 1];
336
7.11M
        bias = 1;
337
7.11M
      } else {                  /* next nearest is row below */
338
7.11M
        inptr1 = input_data[inrow + 1];
339
7.11M
        bias = 2;
340
7.11M
      }
341
14.2M
      outptr = output_data[outrow++];
342
343
361M
      for (colctr = 0; colctr < compptr->downsampled_width; colctr++) {
344
347M
        thiscolsum = (*inptr0++) * 3 + (*inptr1++);
345
347M
        *outptr++ = (_JSAMPLE)((thiscolsum + bias) >> 2);
346
347M
      }
347
14.2M
    }
348
7.11M
    inrow++;
349
7.11M
  }
350
5.65M
}
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
637k
{
365
637k
  _JSAMPARRAY output_data = *output_data_ptr;
366
637k
  register _JSAMPROW inptr0, inptr1, outptr;
367
#if BITS_IN_JSAMPLE == 8
368
  register int thiscolsum, lastcolsum, nextcolsum;
369
#else
370
637k
  register JLONG thiscolsum, lastcolsum, nextcolsum;
371
637k
#endif
372
637k
  register JDIMENSION colctr;
373
637k
  int inrow, outrow, v;
374
375
637k
  inrow = outrow = 0;
376
1.36M
  while (outrow < cinfo->max_v_samp_factor) {
377
2.17M
    for (v = 0; v < 2; v++) {
378
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
379
1.45M
      inptr0 = input_data[inrow];
380
1.45M
      if (v == 0)               /* next nearest is row above */
381
726k
        inptr1 = input_data[inrow - 1];
382
726k
      else                      /* next nearest is row below */
383
726k
        inptr1 = input_data[inrow + 1];
384
1.45M
      outptr = output_data[outrow++];
385
386
      /* Special case for first column */
387
1.45M
      thiscolsum = (*inptr0++) * 3 + (*inptr1++);
388
1.45M
      nextcolsum = (*inptr0++) * 3 + (*inptr1++);
389
1.45M
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 8) >> 4);
390
1.45M
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
391
1.45M
      lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
392
393
26.9M
      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
25.4M
        nextcolsum = (*inptr0++) * 3 + (*inptr1++);
397
25.4M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
398
25.4M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
399
25.4M
        lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
400
25.4M
      }
401
402
      /* Special case for last column */
403
1.45M
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
404
1.45M
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 7) >> 4);
405
1.45M
    }
406
726k
    inrow++;
407
726k
  }
408
637k
}
409
410
411
/*
412
 * Module initialization routine for upsampling.
413
 */
414
415
GLOBAL(void)
416
_jinit_upsampler(j_decompress_ptr cinfo)
417
141k
{
418
141k
  my_upsample_ptr upsample;
419
141k
  int ci;
420
141k
  jpeg_component_info *compptr;
421
141k
  boolean need_buffer, do_fancy;
422
141k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
141k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
141k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
141k
    upsample = (my_upsample_ptr)
429
141k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
141k
                                  sizeof(my_upsampler));
431
141k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
141k
    upsample->pub.start_pass = start_pass_upsample;
433
141k
    upsample->pub._upsample = sep_upsample;
434
141k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
141k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
141k
  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
141k
  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
556k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
415k
       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
415k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
415k
                 cinfo->_min_DCT_scaled_size;
456
415k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
415k
                 cinfo->_min_DCT_scaled_size;
458
415k
    h_out_group = cinfo->max_h_samp_factor;
459
415k
    v_out_group = cinfo->max_v_samp_factor;
460
415k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
415k
    need_buffer = TRUE;
462
415k
    if (!compptr->component_needed) {
463
      /* Don't bother to upsample an uninteresting component. */
464
18.7k
      upsample->methods[ci] = noop_upsample;
465
18.7k
      need_buffer = FALSE;
466
396k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
154k
      upsample->methods[ci] = fullsize_upsample;
469
154k
      need_buffer = FALSE;
470
242k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
48.6k
      if (do_fancy && compptr->downsampled_width > 2) {
473
#ifdef WITH_SIMD
474
2.19k
        if (jsimd_can_h2v1_fancy_upsample())
475
2.19k
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
476
0
        else
477
0
#endif
478
0
          upsample->methods[ci] = h2v1_fancy_upsample;
479
41.5k
      } else {
480
#ifdef WITH_SIMD
481
25.7k
        if (jsimd_can_h2v1_upsample())
482
25.7k
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
0
        else
484
0
#endif
485
0
          upsample->methods[ci] = h2v1_upsample;
486
41.5k
      }
487
193k
    } else if (h_in_group == h_out_group &&
488
193k
               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
8.22k
        upsample->methods[ci] = h1v2_fancy_upsample;
497
8.22k
      upsample->pub.need_context_rows = TRUE;
498
185k
    } else if (h_in_group * 2 == h_out_group &&
499
185k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
19.5k
      if (do_fancy && compptr->downsampled_width > 2) {
502
#ifdef WITH_SIMD
503
386
        if (jsimd_can_h2v2_fancy_upsample())
504
386
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
505
0
        else
506
0
#endif
507
0
          upsample->methods[ci] = h2v2_fancy_upsample;
508
2.40k
        upsample->pub.need_context_rows = TRUE;
509
17.1k
      } else {
510
#ifdef WITH_SIMD
511
3.33k
        if (jsimd_can_h2v2_upsample())
512
3.33k
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
0
        else
514
0
#endif
515
0
          upsample->methods[ci] = h2v2_upsample;
516
17.1k
      }
517
165k
    } else if ((h_out_group % h_in_group) == 0 &&
518
165k
               (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
165k
        upsample->methods[ci] = int_upsample;
526
165k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
165k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
165k
    } else
529
370
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
415k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
242k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
242k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
242k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
242k
                               (long)cinfo->max_h_samp_factor),
535
242k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
242k
    }
537
415k
  }
538
141k
}
j12init_upsampler
Line
Count
Source
417
50.8k
{
418
50.8k
  my_upsample_ptr upsample;
419
50.8k
  int ci;
420
50.8k
  jpeg_component_info *compptr;
421
50.8k
  boolean need_buffer, do_fancy;
422
50.8k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
50.8k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
50.8k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
50.8k
    upsample = (my_upsample_ptr)
429
50.8k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
50.8k
                                  sizeof(my_upsampler));
431
50.8k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
50.8k
    upsample->pub.start_pass = start_pass_upsample;
433
50.8k
    upsample->pub._upsample = sep_upsample;
434
50.8k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
50.8k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
50.8k
  if (cinfo->CCIR601_sampling)  /* this isn't supported */
439
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
440
441
  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
442
   * so don't ask for it.
443
   */
444
50.8k
  do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
445
446
  /* Verify we can handle the sampling factors, select per-component methods,
447
   * and create storage as needed.
448
   */
449
198k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
147k
       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
147k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
147k
                 cinfo->_min_DCT_scaled_size;
456
147k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
147k
                 cinfo->_min_DCT_scaled_size;
458
147k
    h_out_group = cinfo->max_h_samp_factor;
459
147k
    v_out_group = cinfo->max_v_samp_factor;
460
147k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
147k
    need_buffer = TRUE;
462
147k
    if (!compptr->component_needed) {
463
      /* Don't bother to upsample an uninteresting component. */
464
13.0k
      upsample->methods[ci] = noop_upsample;
465
13.0k
      need_buffer = FALSE;
466
134k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
61.5k
      upsample->methods[ci] = fullsize_upsample;
469
61.5k
      need_buffer = FALSE;
470
73.2k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
15.4k
      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
4.86k
          upsample->methods[ci] = h2v1_fancy_upsample;
479
10.5k
      } else {
480
#ifdef WITH_SIMD
481
        if (jsimd_can_h2v1_upsample())
482
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
        else
484
#endif
485
10.5k
          upsample->methods[ci] = h2v1_upsample;
486
10.5k
      }
487
57.7k
    } else if (h_in_group == h_out_group &&
488
57.7k
               v_in_group * 2 == v_out_group && do_fancy) {
489
      /* Non-fancy upsampling is handled by the generic method */
490
#if defined(WITH_SIMD) && (defined(__arm__) || defined(__aarch64__) || \
491
                           defined(_M_ARM) || defined(_M_ARM64))
492
      if (jsimd_can_h1v2_fancy_upsample())
493
        upsample->methods[ci] = jsimd_h1v2_fancy_upsample;
494
      else
495
#endif
496
5.54k
        upsample->methods[ci] = h1v2_fancy_upsample;
497
5.54k
      upsample->pub.need_context_rows = TRUE;
498
52.2k
    } else if (h_in_group * 2 == h_out_group &&
499
52.2k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
11.7k
      if (do_fancy && compptr->downsampled_width > 2) {
502
#ifdef WITH_SIMD
503
        if (jsimd_can_h2v2_fancy_upsample())
504
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
505
        else
506
#endif
507
2.02k
          upsample->methods[ci] = h2v2_fancy_upsample;
508
2.02k
        upsample->pub.need_context_rows = TRUE;
509
9.73k
      } else {
510
#ifdef WITH_SIMD
511
        if (jsimd_can_h2v2_upsample())
512
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
        else
514
#endif
515
9.73k
          upsample->methods[ci] = h2v2_upsample;
516
9.73k
      }
517
40.4k
    } else if ((h_out_group % h_in_group) == 0 &&
518
40.4k
               (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
40.3k
        upsample->methods[ci] = int_upsample;
526
40.3k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
40.3k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
40.3k
    } else
529
114
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
147k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
73.0k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
73.0k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
73.0k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
73.0k
                               (long)cinfo->max_h_samp_factor),
535
73.0k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
73.0k
    }
537
147k
  }
538
50.8k
}
jinit_upsampler
Line
Count
Source
417
69.8k
{
418
69.8k
  my_upsample_ptr upsample;
419
69.8k
  int ci;
420
69.8k
  jpeg_component_info *compptr;
421
69.8k
  boolean need_buffer, do_fancy;
422
69.8k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
69.8k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
69.8k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
69.8k
    upsample = (my_upsample_ptr)
429
69.8k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
69.8k
                                  sizeof(my_upsampler));
431
69.8k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
69.8k
    upsample->pub.start_pass = start_pass_upsample;
433
69.8k
    upsample->pub._upsample = sep_upsample;
434
69.8k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
69.8k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
69.8k
  if (cinfo->CCIR601_sampling)  /* this isn't supported */
439
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
440
441
  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
442
   * so don't ask for it.
443
   */
444
69.8k
  do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
445
446
  /* Verify we can handle the sampling factors, select per-component methods,
447
   * and create storage as needed.
448
   */
449
276k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
206k
       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
206k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
206k
                 cinfo->_min_DCT_scaled_size;
456
206k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
206k
                 cinfo->_min_DCT_scaled_size;
458
206k
    h_out_group = cinfo->max_h_samp_factor;
459
206k
    v_out_group = cinfo->max_v_samp_factor;
460
206k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
206k
    need_buffer = TRUE;
462
206k
    if (!compptr->component_needed) {
463
      /* Don't bother to upsample an uninteresting component. */
464
5.67k
      upsample->methods[ci] = noop_upsample;
465
5.67k
      need_buffer = FALSE;
466
200k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
59.3k
      upsample->methods[ci] = fullsize_upsample;
469
59.3k
      need_buffer = FALSE;
470
141k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
27.9k
      if (do_fancy && compptr->downsampled_width > 2) {
473
2.19k
#ifdef WITH_SIMD
474
2.19k
        if (jsimd_can_h2v1_fancy_upsample())
475
2.19k
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
476
0
        else
477
0
#endif
478
0
          upsample->methods[ci] = h2v1_fancy_upsample;
479
25.7k
      } else {
480
25.7k
#ifdef WITH_SIMD
481
25.7k
        if (jsimd_can_h2v1_upsample())
482
25.7k
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
0
        else
484
0
#endif
485
0
          upsample->methods[ci] = h2v1_upsample;
486
25.7k
      }
487
113k
    } else if (h_in_group == h_out_group &&
488
113k
               v_in_group * 2 == v_out_group && do_fancy) {
489
      /* Non-fancy upsampling is handled by the generic method */
490
#if defined(WITH_SIMD) && (defined(__arm__) || defined(__aarch64__) || \
491
                           defined(_M_ARM) || defined(_M_ARM64))
492
      if (jsimd_can_h1v2_fancy_upsample())
493
        upsample->methods[ci] = jsimd_h1v2_fancy_upsample;
494
      else
495
#endif
496
2.68k
        upsample->methods[ci] = h1v2_fancy_upsample;
497
2.68k
      upsample->pub.need_context_rows = TRUE;
498
110k
    } else if (h_in_group * 2 == h_out_group &&
499
110k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
3.71k
      if (do_fancy && compptr->downsampled_width > 2) {
502
386
#ifdef WITH_SIMD
503
386
        if (jsimd_can_h2v2_fancy_upsample())
504
386
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
505
0
        else
506
0
#endif
507
0
          upsample->methods[ci] = h2v2_fancy_upsample;
508
386
        upsample->pub.need_context_rows = TRUE;
509
3.33k
      } else {
510
3.33k
#ifdef WITH_SIMD
511
3.33k
        if (jsimd_can_h2v2_upsample())
512
3.33k
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
0
        else
514
0
#endif
515
0
          upsample->methods[ci] = h2v2_upsample;
516
3.33k
      }
517
107k
    } else if ((h_out_group % h_in_group) == 0 &&
518
107k
               (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
106k
        upsample->methods[ci] = int_upsample;
526
106k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
106k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
106k
    } else
529
130
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
206k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
141k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
141k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
141k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
141k
                               (long)cinfo->max_h_samp_factor),
535
141k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
141k
    }
537
206k
  }
538
69.8k
}
j16init_upsampler
Line
Count
Source
417
20.4k
{
418
20.4k
  my_upsample_ptr upsample;
419
20.4k
  int ci;
420
20.4k
  jpeg_component_info *compptr;
421
20.4k
  boolean need_buffer, do_fancy;
422
20.4k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
20.4k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
20.4k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
20.4k
    upsample = (my_upsample_ptr)
429
20.4k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
20.4k
                                  sizeof(my_upsampler));
431
20.4k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
20.4k
    upsample->pub.start_pass = start_pass_upsample;
433
20.4k
    upsample->pub._upsample = sep_upsample;
434
20.4k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
20.4k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
20.4k
  if (cinfo->CCIR601_sampling)  /* this isn't supported */
439
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
440
441
  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
442
   * so don't ask for it.
443
   */
444
20.4k
  do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
445
446
  /* Verify we can handle the sampling factors, select per-component methods,
447
   * and create storage as needed.
448
   */
449
81.8k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
61.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
61.3k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
61.3k
                 cinfo->_min_DCT_scaled_size;
456
61.3k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
61.3k
                 cinfo->_min_DCT_scaled_size;
458
61.3k
    h_out_group = cinfo->max_h_samp_factor;
459
61.3k
    v_out_group = cinfo->max_v_samp_factor;
460
61.3k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
61.3k
    need_buffer = TRUE;
462
61.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
61.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
33.6k
      upsample->methods[ci] = fullsize_upsample;
469
33.6k
      need_buffer = FALSE;
470
33.6k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
5.31k
      if (do_fancy && compptr->downsampled_width > 2) {
473
#ifdef WITH_SIMD
474
        if (jsimd_can_h2v1_fancy_upsample())
475
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
476
        else
477
#endif
478
0
          upsample->methods[ci] = h2v1_fancy_upsample;
479
5.31k
      } else {
480
#ifdef WITH_SIMD
481
        if (jsimd_can_h2v1_upsample())
482
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
        else
484
#endif
485
5.31k
          upsample->methods[ci] = h2v1_upsample;
486
5.31k
      }
487
22.4k
    } else if (h_in_group == h_out_group &&
488
22.4k
               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
22.4k
    } else if (h_in_group * 2 == h_out_group &&
499
22.4k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
4.10k
      if (do_fancy && compptr->downsampled_width > 2) {
502
#ifdef WITH_SIMD
503
        if (jsimd_can_h2v2_fancy_upsample())
504
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
505
        else
506
#endif
507
0
          upsample->methods[ci] = h2v2_fancy_upsample;
508
0
        upsample->pub.need_context_rows = TRUE;
509
4.10k
      } else {
510
#ifdef WITH_SIMD
511
        if (jsimd_can_h2v2_upsample())
512
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
        else
514
#endif
515
4.10k
          upsample->methods[ci] = h2v2_upsample;
516
4.10k
      }
517
18.3k
    } else if ((h_out_group % h_in_group) == 0 &&
518
18.3k
               (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
18.1k
        upsample->methods[ci] = int_upsample;
526
18.1k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
18.1k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
18.1k
    } else
529
126
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
61.3k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
27.6k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
27.6k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
27.6k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
27.6k
                               (long)cinfo->max_h_samp_factor),
535
27.6k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
27.6k
    }
537
61.3k
  }
538
20.4k
}
539
540
#endif /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */