Coverage Report

Created: 2024-09-08 06:05

/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
2.15k
{
44
2.15k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
2.15k
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
2.15k
  upsample->rows_to_go = cinfo->output_height;
50
2.15k
}
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
7.07M
{
67
7.07M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
68
7.07M
  int ci;
69
7.07M
  jpeg_component_info *compptr;
70
7.07M
  JDIMENSION num_rows;
71
72
  /* Fill the conversion buffer, if it's empty */
73
7.07M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
74
8.48M
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
75
4.60M
         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
4.60M
      (*upsample->methods[ci]) (cinfo, compptr,
80
4.60M
        input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
81
4.60M
        upsample->color_buf + ci);
82
4.60M
    }
83
3.87M
    upsample->next_row_out = 0;
84
3.87M
  }
85
86
  /* Color-convert and emit rows */
87
88
  /* How many we have in the buffer: */
89
7.07M
  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
7.07M
  if (num_rows > upsample->rows_to_go)
94
522
    num_rows = upsample->rows_to_go;
95
  /* And not more than what the client can accept: */
96
7.07M
  out_rows_avail -= *out_row_ctr;
97
7.07M
  if (num_rows > out_rows_avail)
98
3.20M
    num_rows = out_rows_avail;
99
100
7.07M
  (*cinfo->cconvert->_color_convert) (cinfo, upsample->color_buf,
101
7.07M
                                      (JDIMENSION)upsample->next_row_out,
102
7.07M
                                      output_buf + *out_row_ctr,
103
7.07M
                                      (int)num_rows);
104
105
  /* Adjust counts */
106
7.07M
  *out_row_ctr += num_rows;
107
7.07M
  upsample->rows_to_go -= num_rows;
108
7.07M
  upsample->next_row_out += num_rows;
109
  /* When the buffer is emptied, declare this input row group consumed */
110
7.07M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
111
3.87M
    (*in_row_group_ctr)++;
112
7.07M
}
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
3.61M
{
132
3.61M
  *output_data_ptr = input_data;
133
3.61M
}
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
333k
{
164
333k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
165
333k
  _JSAMPARRAY output_data = *output_data_ptr;
166
333k
  register _JSAMPROW inptr, outptr;
167
333k
  register _JSAMPLE invalue;
168
333k
  register int h;
169
333k
  _JSAMPROW outend;
170
333k
  int h_expand, v_expand;
171
333k
  int inrow, outrow;
172
173
333k
  h_expand = upsample->h_expand[compptr->component_index];
174
333k
  v_expand = upsample->v_expand[compptr->component_index];
175
176
333k
  inrow = outrow = 0;
177
742k
  while (outrow < cinfo->max_v_samp_factor) {
178
    /* Generate one output row with proper horizontal expansion */
179
409k
    inptr = input_data[inrow];
180
409k
    outptr = output_data[outrow];
181
409k
    outend = outptr + cinfo->output_width;
182
16.0M
    while (outptr < outend) {
183
15.6M
      invalue = *inptr++;
184
46.3M
      for (h = h_expand; h > 0; h--) {
185
30.7M
        *outptr++ = invalue;
186
30.7M
      }
187
15.6M
    }
188
    /* Generate any additional output rows by duplicating the first one */
189
409k
    if (v_expand > 1) {
190
269k
      _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1,
191
269k
                         v_expand - 1, cinfo->output_width);
192
269k
    }
193
409k
    inrow++;
194
409k
    outrow += v_expand;
195
409k
  }
196
333k
}
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
47.3k
{
208
47.3k
  _JSAMPARRAY output_data = *output_data_ptr;
209
47.3k
  register _JSAMPROW inptr, outptr;
210
47.3k
  register _JSAMPLE invalue;
211
47.3k
  _JSAMPROW outend;
212
47.3k
  int inrow;
213
214
175k
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
215
128k
    inptr = input_data[inrow];
216
128k
    outptr = output_data[inrow];
217
128k
    outend = outptr + cinfo->output_width;
218
339k
    while (outptr < outend) {
219
211k
      invalue = *inptr++;
220
211k
      *outptr++ = invalue;
221
211k
      *outptr++ = invalue;
222
211k
    }
223
128k
  }
224
47.3k
}
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
49.3k
{
236
49.3k
  _JSAMPARRAY output_data = *output_data_ptr;
237
49.3k
  register _JSAMPROW inptr, outptr;
238
49.3k
  register _JSAMPLE invalue;
239
49.3k
  _JSAMPROW outend;
240
49.3k
  int inrow, outrow;
241
242
49.3k
  inrow = outrow = 0;
243
113k
  while (outrow < cinfo->max_v_samp_factor) {
244
63.9k
    inptr = input_data[inrow];
245
63.9k
    outptr = output_data[outrow];
246
63.9k
    outend = outptr + cinfo->output_width;
247
179k
    while (outptr < outend) {
248
115k
      invalue = *inptr++;
249
115k
      *outptr++ = invalue;
250
115k
      *outptr++ = invalue;
251
115k
    }
252
63.9k
    _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1, 1,
253
63.9k
                       cinfo->output_width);
254
63.9k
    inrow++;
255
63.9k
    outrow += 2;
256
63.9k
  }
257
49.3k
}
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
291k
{
279
291k
  _JSAMPARRAY output_data = *output_data_ptr;
280
291k
  register _JSAMPROW inptr, outptr;
281
291k
  register int invalue;
282
291k
  register JDIMENSION colctr;
283
291k
  int inrow;
284
285
1.06M
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
286
775k
    inptr = input_data[inrow];
287
775k
    outptr = output_data[inrow];
288
    /* Special case for first column */
289
775k
    invalue = *inptr++;
290
775k
    *outptr++ = (_JSAMPLE)invalue;
291
775k
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[0] + 2) >> 2);
292
293
31.5M
    for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
294
      /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
295
30.7M
      invalue = (*inptr++) * 3;
296
30.7M
      *outptr++ = (_JSAMPLE)((invalue + inptr[-2] + 1) >> 2);
297
30.7M
      *outptr++ = (_JSAMPLE)((invalue + inptr[0] + 2) >> 2);
298
30.7M
    }
299
300
    /* Special case for last column */
301
775k
    invalue = *inptr;
302
775k
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[-1] + 1) >> 2);
303
775k
    *outptr++ = (_JSAMPLE)invalue;
304
775k
  }
305
291k
}
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
186k
{
319
186k
  _JSAMPARRAY output_data = *output_data_ptr;
320
186k
  _JSAMPROW inptr0, inptr1, outptr;
321
186k
#if BITS_IN_JSAMPLE == 8
322
186k
  int thiscolsum, bias;
323
#else
324
  JLONG thiscolsum, bias;
325
#endif
326
186k
  JDIMENSION colctr;
327
186k
  int inrow, outrow, v;
328
329
186k
  inrow = outrow = 0;
330
448k
  while (outrow < cinfo->max_v_samp_factor) {
331
783k
    for (v = 0; v < 2; v++) {
332
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
333
522k
      inptr0 = input_data[inrow];
334
522k
      if (v == 0) {             /* next nearest is row above */
335
261k
        inptr1 = input_data[inrow - 1];
336
261k
        bias = 1;
337
261k
      } else {                  /* next nearest is row below */
338
261k
        inptr1 = input_data[inrow + 1];
339
261k
        bias = 2;
340
261k
      }
341
522k
      outptr = output_data[outrow++];
342
343
37.3M
      for (colctr = 0; colctr < compptr->downsampled_width; colctr++) {
344
36.8M
        thiscolsum = (*inptr0++) * 3 + (*inptr1++);
345
36.8M
        *outptr++ = (_JSAMPLE)((thiscolsum + bias) >> 2);
346
36.8M
      }
347
522k
    }
348
261k
    inrow++;
349
261k
  }
350
186k
}
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
84.3k
{
365
84.3k
  _JSAMPARRAY output_data = *output_data_ptr;
366
84.3k
  register _JSAMPROW inptr0, inptr1, outptr;
367
84.3k
#if BITS_IN_JSAMPLE == 8
368
84.3k
  register int thiscolsum, lastcolsum, nextcolsum;
369
#else
370
  register JLONG thiscolsum, lastcolsum, nextcolsum;
371
#endif
372
84.3k
  register JDIMENSION colctr;
373
84.3k
  int inrow, outrow, v;
374
375
84.3k
  inrow = outrow = 0;
376
212k
  while (outrow < cinfo->max_v_samp_factor) {
377
384k
    for (v = 0; v < 2; v++) {
378
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
379
256k
      inptr0 = input_data[inrow];
380
256k
      if (v == 0)               /* next nearest is row above */
381
128k
        inptr1 = input_data[inrow - 1];
382
128k
      else                      /* next nearest is row below */
383
128k
        inptr1 = input_data[inrow + 1];
384
256k
      outptr = output_data[outrow++];
385
386
      /* Special case for first column */
387
256k
      thiscolsum = (*inptr0++) * 3 + (*inptr1++);
388
256k
      nextcolsum = (*inptr0++) * 3 + (*inptr1++);
389
256k
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 8) >> 4);
390
256k
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
391
256k
      lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
392
393
22.7M
      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
22.4M
        nextcolsum = (*inptr0++) * 3 + (*inptr1++);
397
22.4M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
398
22.4M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
399
22.4M
        lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
400
22.4M
      }
401
402
      /* Special case for last column */
403
256k
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
404
256k
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 7) >> 4);
405
256k
    }
406
128k
    inrow++;
407
128k
  }
408
84.3k
}
409
410
411
/*
412
 * Module initialization routine for upsampling.
413
 */
414
415
GLOBAL(void)
416
_jinit_upsampler(j_decompress_ptr cinfo)
417
5.43k
{
418
5.43k
  my_upsample_ptr upsample;
419
5.43k
  int ci;
420
5.43k
  jpeg_component_info *compptr;
421
5.43k
  boolean need_buffer, do_fancy;
422
5.43k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
5.43k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
5.43k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
5.43k
    upsample = (my_upsample_ptr)
429
5.43k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
5.43k
                                  sizeof(my_upsampler));
431
5.43k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
5.43k
    upsample->pub.start_pass = start_pass_upsample;
433
5.43k
    upsample->pub._upsample = sep_upsample;
434
5.43k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
5.43k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
5.43k
  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
5.43k
  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
16.3k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
10.8k
       ci++, compptr++) {
451
    /* Compute size of an "input group" after IDCT scaling.  This many samples
452
     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
453
     */
454
10.8k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
10.8k
                 cinfo->_min_DCT_scaled_size;
456
10.8k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
10.8k
                 cinfo->_min_DCT_scaled_size;
458
10.8k
    h_out_group = cinfo->max_h_samp_factor;
459
10.8k
    v_out_group = cinfo->max_v_samp_factor;
460
10.8k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
10.8k
    need_buffer = TRUE;
462
10.8k
    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
10.8k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
3.98k
      upsample->methods[ci] = fullsize_upsample;
469
3.98k
      need_buffer = FALSE;
470
6.88k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
1.36k
      if (do_fancy && compptr->downsampled_width > 2) {
473
#ifdef WITH_SIMD
474
313
        if (jsimd_can_h2v1_fancy_upsample())
475
0
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
476
313
        else
477
313
#endif
478
313
          upsample->methods[ci] = h2v1_fancy_upsample;
479
936
      } else {
480
#ifdef WITH_SIMD
481
292
        if (jsimd_can_h2v1_upsample())
482
0
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
292
        else
484
292
#endif
485
292
          upsample->methods[ci] = h2v1_upsample;
486
936
      }
487
5.51k
    } else if (h_in_group == h_out_group &&
488
5.51k
               v_in_group * 2 == v_out_group && do_fancy) {
489
      /* Non-fancy upsampling is handled by the generic method */
490
#if defined(WITH_SIMD) && (defined(__arm__) || defined(__aarch64__) || \
491
                           defined(_M_ARM) || defined(_M_ARM64))
492
      if (jsimd_can_h1v2_fancy_upsample())
493
        upsample->methods[ci] = jsimd_h1v2_fancy_upsample;
494
      else
495
#endif
496
782
        upsample->methods[ci] = h1v2_fancy_upsample;
497
782
      upsample->pub.need_context_rows = TRUE;
498
4.73k
    } else if (h_in_group * 2 == h_out_group &&
499
4.73k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
925
      if (do_fancy && compptr->downsampled_width > 2) {
502
#ifdef WITH_SIMD
503
160
        if (jsimd_can_h2v2_fancy_upsample())
504
0
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
505
160
        else
506
160
#endif
507
160
          upsample->methods[ci] = h2v2_fancy_upsample;
508
265
        upsample->pub.need_context_rows = TRUE;
509
660
      } else {
510
#ifdef WITH_SIMD
511
202
        if (jsimd_can_h2v2_upsample())
512
0
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
202
        else
514
202
#endif
515
202
          upsample->methods[ci] = h2v2_upsample;
516
660
      }
517
3.80k
    } else if ((h_out_group % h_in_group) == 0 &&
518
3.80k
               (v_out_group % v_in_group) == 0) {
519
      /* Generic integral-factors upsampling method */
520
#if defined(WITH_SIMD) && defined(__mips__)
521
      if (jsimd_can_int_upsample())
522
        upsample->methods[ci] = jsimd_int_upsample;
523
      else
524
#endif
525
3.77k
        upsample->methods[ci] = int_upsample;
526
3.77k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
3.77k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
3.77k
    } else
529
29
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
10.8k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
6.85k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
6.85k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
6.85k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
6.85k
                               (long)cinfo->max_h_samp_factor),
535
6.85k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
6.85k
    }
537
10.8k
  }
538
5.43k
}
jinit_upsampler
Line
Count
Source
417
3.63k
{
418
3.63k
  my_upsample_ptr upsample;
419
3.63k
  int ci;
420
3.63k
  jpeg_component_info *compptr;
421
3.63k
  boolean need_buffer, do_fancy;
422
3.63k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
3.63k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
3.63k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
3.63k
    upsample = (my_upsample_ptr)
429
3.63k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
3.63k
                                  sizeof(my_upsampler));
431
3.63k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
3.63k
    upsample->pub.start_pass = start_pass_upsample;
433
3.63k
    upsample->pub._upsample = sep_upsample;
434
3.63k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
3.63k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
3.63k
  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
3.63k
  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
9.88k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
6.24k
       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
6.24k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
6.24k
                 cinfo->_min_DCT_scaled_size;
456
6.24k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
6.24k
                 cinfo->_min_DCT_scaled_size;
458
6.24k
    h_out_group = cinfo->max_h_samp_factor;
459
6.24k
    v_out_group = cinfo->max_v_samp_factor;
460
6.24k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
6.24k
    need_buffer = TRUE;
462
6.24k
    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
6.24k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
3.19k
      upsample->methods[ci] = fullsize_upsample;
469
3.19k
      need_buffer = FALSE;
470
3.19k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
605
      if (do_fancy && compptr->downsampled_width > 2) {
473
313
#ifdef WITH_SIMD
474
313
        if (jsimd_can_h2v1_fancy_upsample())
475
0
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
476
313
        else
477
313
#endif
478
313
          upsample->methods[ci] = h2v1_fancy_upsample;
479
313
      } else {
480
292
#ifdef WITH_SIMD
481
292
        if (jsimd_can_h2v1_upsample())
482
0
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
292
        else
484
292
#endif
485
292
          upsample->methods[ci] = h2v1_upsample;
486
292
      }
487
2.44k
    } else if (h_in_group == h_out_group &&
488
2.44k
               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
649
        upsample->methods[ci] = h1v2_fancy_upsample;
497
649
      upsample->pub.need_context_rows = TRUE;
498
1.80k
    } else if (h_in_group * 2 == h_out_group &&
499
1.80k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
362
      if (do_fancy && compptr->downsampled_width > 2) {
502
160
#ifdef WITH_SIMD
503
160
        if (jsimd_can_h2v2_fancy_upsample())
504
0
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
505
160
        else
506
160
#endif
507
160
          upsample->methods[ci] = h2v2_fancy_upsample;
508
160
        upsample->pub.need_context_rows = TRUE;
509
202
      } else {
510
202
#ifdef WITH_SIMD
511
202
        if (jsimd_can_h2v2_upsample())
512
0
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
202
        else
514
202
#endif
515
202
          upsample->methods[ci] = h2v2_upsample;
516
202
      }
517
1.43k
    } else if ((h_out_group % h_in_group) == 0 &&
518
1.43k
               (v_out_group % v_in_group) == 0) {
519
      /* Generic integral-factors upsampling method */
520
#if defined(WITH_SIMD) && defined(__mips__)
521
      if (jsimd_can_int_upsample())
522
        upsample->methods[ci] = jsimd_int_upsample;
523
      else
524
#endif
525
1.43k
        upsample->methods[ci] = int_upsample;
526
1.43k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
1.43k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
1.43k
    } else
529
8
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
6.24k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
3.04k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
3.04k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
3.04k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
3.04k
                               (long)cinfo->max_h_samp_factor),
535
3.04k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
3.04k
    }
537
6.24k
  }
538
3.63k
}
j12init_upsampler
Line
Count
Source
417
1.01k
{
418
1.01k
  my_upsample_ptr upsample;
419
1.01k
  int ci;
420
1.01k
  jpeg_component_info *compptr;
421
1.01k
  boolean need_buffer, do_fancy;
422
1.01k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
1.01k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
1.01k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
1.01k
    upsample = (my_upsample_ptr)
429
1.01k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
1.01k
                                  sizeof(my_upsampler));
431
1.01k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
1.01k
    upsample->pub.start_pass = start_pass_upsample;
433
1.01k
    upsample->pub._upsample = sep_upsample;
434
1.01k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
1.01k
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
1.01k
  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.01k
  do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
445
446
  /* Verify we can handle the sampling factors, select per-component methods,
447
   * and create storage as needed.
448
   */
449
3.57k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
2.56k
       ci++, compptr++) {
451
    /* Compute size of an "input group" after IDCT scaling.  This many samples
452
     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
453
     */
454
2.56k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
2.56k
                 cinfo->_min_DCT_scaled_size;
456
2.56k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
2.56k
                 cinfo->_min_DCT_scaled_size;
458
2.56k
    h_out_group = cinfo->max_h_samp_factor;
459
2.56k
    v_out_group = cinfo->max_v_samp_factor;
460
2.56k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
2.56k
    need_buffer = TRUE;
462
2.56k
    if (!compptr->component_needed) {
463
      /* Don't bother to upsample an uninteresting component. */
464
0
      upsample->methods[ci] = noop_upsample;
465
0
      need_buffer = FALSE;
466
2.56k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
432
      upsample->methods[ci] = fullsize_upsample;
469
432
      need_buffer = FALSE;
470
2.13k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
470
      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
94
          upsample->methods[ci] = h2v1_fancy_upsample;
479
376
      } else {
480
#ifdef WITH_SIMD
481
        if (jsimd_can_h2v1_upsample())
482
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
        else
484
#endif
485
376
          upsample->methods[ci] = h2v1_upsample;
486
376
      }
487
1.66k
    } else if (h_in_group == h_out_group &&
488
1.66k
               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
106
        upsample->methods[ci] = h1v2_fancy_upsample;
497
106
      upsample->pub.need_context_rows = TRUE;
498
1.55k
    } else if (h_in_group * 2 == h_out_group &&
499
1.55k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
378
      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
74
          upsample->methods[ci] = h2v2_fancy_upsample;
508
74
        upsample->pub.need_context_rows = TRUE;
509
304
      } else {
510
#ifdef WITH_SIMD
511
        if (jsimd_can_h2v2_upsample())
512
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
        else
514
#endif
515
304
          upsample->methods[ci] = h2v2_upsample;
516
304
      }
517
1.17k
    } else if ((h_out_group % h_in_group) == 0 &&
518
1.17k
               (v_out_group % v_in_group) == 0) {
519
      /* Generic integral-factors upsampling method */
520
#if defined(WITH_SIMD) && defined(__mips__)
521
      if (jsimd_can_int_upsample())
522
        upsample->methods[ci] = jsimd_int_upsample;
523
      else
524
#endif
525
1.16k
        upsample->methods[ci] = int_upsample;
526
1.16k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
1.16k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
1.16k
    } else
529
10
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
2.56k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
2.12k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
2.12k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
2.12k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
2.12k
                               (long)cinfo->max_h_samp_factor),
535
2.12k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
2.12k
    }
537
2.56k
  }
538
1.01k
}
j16init_upsampler
Line
Count
Source
417
785
{
418
785
  my_upsample_ptr upsample;
419
785
  int ci;
420
785
  jpeg_component_info *compptr;
421
785
  boolean need_buffer, do_fancy;
422
785
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
785
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
425
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
426
427
785
  if (!cinfo->master->jinit_upsampler_no_alloc) {
428
785
    upsample = (my_upsample_ptr)
429
785
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
430
785
                                  sizeof(my_upsampler));
431
785
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
432
785
    upsample->pub.start_pass = start_pass_upsample;
433
785
    upsample->pub._upsample = sep_upsample;
434
785
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
435
785
  } else
436
0
    upsample = (my_upsample_ptr)cinfo->upsample;
437
438
785
  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
785
  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
2.84k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450
2.05k
       ci++, compptr++) {
451
    /* Compute size of an "input group" after IDCT scaling.  This many samples
452
     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
453
     */
454
2.05k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
455
2.05k
                 cinfo->_min_DCT_scaled_size;
456
2.05k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
457
2.05k
                 cinfo->_min_DCT_scaled_size;
458
2.05k
    h_out_group = cinfo->max_h_samp_factor;
459
2.05k
    v_out_group = cinfo->max_v_samp_factor;
460
2.05k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
461
2.05k
    need_buffer = TRUE;
462
2.05k
    if (!compptr->component_needed) {
463
      /* Don't bother to upsample an uninteresting component. */
464
0
      upsample->methods[ci] = noop_upsample;
465
0
      need_buffer = FALSE;
466
2.05k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
467
      /* Fullsize components can be processed without any work. */
468
360
      upsample->methods[ci] = fullsize_upsample;
469
360
      need_buffer = FALSE;
470
1.69k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
471
      /* Special cases for 2h1v upsampling */
472
290
      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
22
          upsample->methods[ci] = h2v1_fancy_upsample;
479
268
      } else {
480
#ifdef WITH_SIMD
481
        if (jsimd_can_h2v1_upsample())
482
          upsample->methods[ci] = jsimd_h2v1_upsample;
483
        else
484
#endif
485
268
          upsample->methods[ci] = h2v1_upsample;
486
268
      }
487
1.40k
    } else if (h_in_group == h_out_group &&
488
1.40k
               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
27
        upsample->methods[ci] = h1v2_fancy_upsample;
497
27
      upsample->pub.need_context_rows = TRUE;
498
1.37k
    } else if (h_in_group * 2 == h_out_group &&
499
1.37k
               v_in_group * 2 == v_out_group) {
500
      /* Special cases for 2h2v upsampling */
501
185
      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
31
          upsample->methods[ci] = h2v2_fancy_upsample;
508
31
        upsample->pub.need_context_rows = TRUE;
509
154
      } else {
510
#ifdef WITH_SIMD
511
        if (jsimd_can_h2v2_upsample())
512
          upsample->methods[ci] = jsimd_h2v2_upsample;
513
        else
514
#endif
515
154
          upsample->methods[ci] = h2v2_upsample;
516
154
      }
517
1.19k
    } else if ((h_out_group % h_in_group) == 0 &&
518
1.19k
               (v_out_group % v_in_group) == 0) {
519
      /* Generic integral-factors upsampling method */
520
#if defined(WITH_SIMD) && defined(__mips__)
521
      if (jsimd_can_int_upsample())
522
        upsample->methods[ci] = jsimd_int_upsample;
523
      else
524
#endif
525
1.18k
        upsample->methods[ci] = int_upsample;
526
1.18k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
527
1.18k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
528
1.18k
    } else
529
11
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
530
2.05k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
531
1.68k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
532
1.68k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
533
1.68k
         (JDIMENSION)jround_up((long)cinfo->output_width,
534
1.68k
                               (long)cinfo->max_h_samp_factor),
535
1.68k
         (JDIMENSION)cinfo->max_v_samp_factor);
536
1.68k
    }
537
2.05k
  }
538
785
}
539
540
#endif /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */