Coverage Report

Created: 2025-10-10 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo/src/jdsample.c
Line
Count
Source
1
/*
2
 * jdsample.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1991-1996, Thomas G. Lane.
6
 * libjpeg-turbo Modifications:
7
 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
 * Copyright (C) 2010, 2015-2016, 2022, 2024-2025, 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
35.6k
{
44
35.6k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
35.6k
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
35.6k
  upsample->rows_to_go = cinfo->output_height;
50
35.6k
}
jdsample-8.c:start_pass_upsample
Line
Count
Source
43
34.3k
{
44
34.3k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
34.3k
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
34.3k
  upsample->rows_to_go = cinfo->output_height;
50
34.3k
}
jdsample-12.c:start_pass_upsample
Line
Count
Source
43
1.18k
{
44
1.18k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
1.18k
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
1.18k
  upsample->rows_to_go = cinfo->output_height;
50
1.18k
}
jdsample-16.c:start_pass_upsample
Line
Count
Source
43
118
{
44
118
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
118
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
118
  upsample->rows_to_go = cinfo->output_height;
50
118
}
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
99.5M
{
67
99.5M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
68
99.5M
  int ci;
69
99.5M
  jpeg_component_info *compptr;
70
99.5M
  JDIMENSION num_rows;
71
72
  /* Fill the conversion buffer, if it's empty */
73
99.5M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
74
175M
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
75
122M
         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
122M
      (*upsample->methods[ci]) (cinfo, compptr,
80
122M
        input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
81
122M
        upsample->color_buf + ci);
82
122M
    }
83
53.1M
    upsample->next_row_out = 0;
84
53.1M
  }
85
86
  /* Color-convert and emit rows */
87
88
  /* How many we have in the buffer: */
89
99.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
99.5M
  if (num_rows > upsample->rows_to_go)
94
11.8k
    num_rows = upsample->rows_to_go;
95
  /* And not more than what the client can accept: */
96
99.5M
  out_rows_avail -= *out_row_ctr;
97
99.5M
  if (num_rows > out_rows_avail)
98
46.4M
    num_rows = out_rows_avail;
99
100
99.5M
  (*cinfo->cconvert->_color_convert) (cinfo, upsample->color_buf,
101
99.5M
                                      (JDIMENSION)upsample->next_row_out,
102
99.5M
                                      output_buf + *out_row_ctr,
103
99.5M
                                      (int)num_rows);
104
105
  /* Adjust counts */
106
99.5M
  *out_row_ctr += num_rows;
107
99.5M
  upsample->rows_to_go -= num_rows;
108
99.5M
  upsample->next_row_out += num_rows;
109
  /* When the buffer is emptied, declare this input row group consumed */
110
99.5M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
111
53.1M
    (*in_row_group_ctr)++;
112
99.5M
}
jdsample-8.c:sep_upsample
Line
Count
Source
66
99.5M
{
67
99.5M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
68
99.5M
  int ci;
69
99.5M
  jpeg_component_info *compptr;
70
99.5M
  JDIMENSION num_rows;
71
72
  /* Fill the conversion buffer, if it's empty */
73
99.5M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
74
175M
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
75
122M
         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
122M
      (*upsample->methods[ci]) (cinfo, compptr,
80
122M
        input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
81
122M
        upsample->color_buf + ci);
82
122M
    }
83
53.1M
    upsample->next_row_out = 0;
84
53.1M
  }
85
86
  /* Color-convert and emit rows */
87
88
  /* How many we have in the buffer: */
89
99.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
99.5M
  if (num_rows > upsample->rows_to_go)
94
11.8k
    num_rows = upsample->rows_to_go;
95
  /* And not more than what the client can accept: */
96
99.5M
  out_rows_avail -= *out_row_ctr;
97
99.5M
  if (num_rows > out_rows_avail)
98
46.4M
    num_rows = out_rows_avail;
99
100
99.5M
  (*cinfo->cconvert->_color_convert) (cinfo, upsample->color_buf,
101
99.5M
                                      (JDIMENSION)upsample->next_row_out,
102
99.5M
                                      output_buf + *out_row_ctr,
103
99.5M
                                      (int)num_rows);
104
105
  /* Adjust counts */
106
99.5M
  *out_row_ctr += num_rows;
107
99.5M
  upsample->rows_to_go -= num_rows;
108
99.5M
  upsample->next_row_out += num_rows;
109
  /* When the buffer is emptied, declare this input row group consumed */
110
99.5M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
111
53.1M
    (*in_row_group_ctr)++;
112
99.5M
}
Unexecuted instantiation: jdsample-12.c:sep_upsample
Unexecuted instantiation: jdsample-16.c:sep_upsample
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
72.9M
{
132
72.9M
  *output_data_ptr = input_data;
133
72.9M
}
jdsample-8.c:fullsize_upsample
Line
Count
Source
131
72.9M
{
132
72.9M
  *output_data_ptr = input_data;
133
72.9M
}
Unexecuted instantiation: jdsample-12.c:fullsize_upsample
Unexecuted instantiation: jdsample-16.c:fullsize_upsample
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
}
Unexecuted instantiation: jdsample-8.c:noop_upsample
Unexecuted instantiation: jdsample-12.c:noop_upsample
Unexecuted instantiation: jdsample-16.c:noop_upsample
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
15.7M
{
164
15.7M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
165
15.7M
  _JSAMPARRAY output_data = *output_data_ptr;
166
15.7M
  register _JSAMPROW inptr, outptr;
167
15.7M
  register _JSAMPLE invalue;
168
15.7M
  register int h;
169
15.7M
  _JSAMPROW outend;
170
15.7M
  int h_expand, v_expand;
171
15.7M
  int inrow, outrow;
172
173
15.7M
  h_expand = upsample->h_expand[compptr->component_index];
174
15.7M
  v_expand = upsample->v_expand[compptr->component_index];
175
176
15.7M
  inrow = outrow = 0;
177
32.9M
  while (outrow < cinfo->max_v_samp_factor) {
178
    /* Generate one output row with proper horizontal expansion */
179
17.1M
    inptr = input_data[inrow];
180
17.1M
    outptr = output_data[outrow];
181
17.1M
    outend = outptr + cinfo->output_width;
182
1.21G
    while (outptr < outend) {
183
1.19G
      invalue = *inptr++;
184
3.20G
      for (h = h_expand; h > 0; h--) {
185
2.01G
        *outptr++ = invalue;
186
2.01G
      }
187
1.19G
    }
188
    /* Generate any additional output rows by duplicating the first one */
189
17.1M
    if (v_expand > 1) {
190
13.1M
      _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1,
191
13.1M
                         v_expand - 1, cinfo->output_width);
192
13.1M
    }
193
17.1M
    inrow++;
194
17.1M
    outrow += v_expand;
195
17.1M
  }
196
15.7M
}
jdsample-8.c:int_upsample
Line
Count
Source
163
15.7M
{
164
15.7M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
165
15.7M
  _JSAMPARRAY output_data = *output_data_ptr;
166
15.7M
  register _JSAMPROW inptr, outptr;
167
15.7M
  register _JSAMPLE invalue;
168
15.7M
  register int h;
169
15.7M
  _JSAMPROW outend;
170
15.7M
  int h_expand, v_expand;
171
15.7M
  int inrow, outrow;
172
173
15.7M
  h_expand = upsample->h_expand[compptr->component_index];
174
15.7M
  v_expand = upsample->v_expand[compptr->component_index];
175
176
15.7M
  inrow = outrow = 0;
177
32.9M
  while (outrow < cinfo->max_v_samp_factor) {
178
    /* Generate one output row with proper horizontal expansion */
179
17.1M
    inptr = input_data[inrow];
180
17.1M
    outptr = output_data[outrow];
181
17.1M
    outend = outptr + cinfo->output_width;
182
1.21G
    while (outptr < outend) {
183
1.19G
      invalue = *inptr++;
184
3.20G
      for (h = h_expand; h > 0; h--) {
185
2.01G
        *outptr++ = invalue;
186
2.01G
      }
187
1.19G
    }
188
    /* Generate any additional output rows by duplicating the first one */
189
17.1M
    if (v_expand > 1) {
190
13.1M
      _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1,
191
13.1M
                         v_expand - 1, cinfo->output_width);
192
13.1M
    }
193
17.1M
    inrow++;
194
17.1M
    outrow += v_expand;
195
17.1M
  }
196
15.7M
}
Unexecuted instantiation: jdsample-12.c:int_upsample
Unexecuted instantiation: jdsample-16.c:int_upsample
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
1.60M
{
208
1.60M
  _JSAMPARRAY output_data = *output_data_ptr;
209
1.60M
  register _JSAMPROW inptr, outptr;
210
1.60M
  register _JSAMPLE invalue;
211
1.60M
  _JSAMPROW outend;
212
1.60M
  int inrow;
213
214
5.43M
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
215
3.83M
    inptr = input_data[inrow];
216
3.83M
    outptr = output_data[inrow];
217
3.83M
    outend = outptr + cinfo->output_width;
218
11.0M
    while (outptr < outend) {
219
7.19M
      invalue = *inptr++;
220
7.19M
      *outptr++ = invalue;
221
7.19M
      *outptr++ = invalue;
222
7.19M
    }
223
3.83M
  }
224
1.60M
}
jdsample-8.c:h2v1_upsample
Line
Count
Source
207
1.60M
{
208
1.60M
  _JSAMPARRAY output_data = *output_data_ptr;
209
1.60M
  register _JSAMPROW inptr, outptr;
210
1.60M
  register _JSAMPLE invalue;
211
1.60M
  _JSAMPROW outend;
212
1.60M
  int inrow;
213
214
5.43M
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
215
3.83M
    inptr = input_data[inrow];
216
3.83M
    outptr = output_data[inrow];
217
3.83M
    outend = outptr + cinfo->output_width;
218
11.0M
    while (outptr < outend) {
219
7.19M
      invalue = *inptr++;
220
7.19M
      *outptr++ = invalue;
221
7.19M
      *outptr++ = invalue;
222
7.19M
    }
223
3.83M
  }
224
1.60M
}
Unexecuted instantiation: jdsample-12.c:h2v1_upsample
Unexecuted instantiation: jdsample-16.c:h2v1_upsample
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
1.85M
{
236
1.85M
  _JSAMPARRAY output_data = *output_data_ptr;
237
1.85M
  register _JSAMPROW inptr, outptr;
238
1.85M
  register _JSAMPLE invalue;
239
1.85M
  _JSAMPROW outend;
240
1.85M
  int inrow, outrow;
241
242
1.85M
  inrow = outrow = 0;
243
4.31M
  while (outrow < cinfo->max_v_samp_factor) {
244
2.45M
    inptr = input_data[inrow];
245
2.45M
    outptr = output_data[outrow];
246
2.45M
    outend = outptr + cinfo->output_width;
247
6.95M
    while (outptr < outend) {
248
4.49M
      invalue = *inptr++;
249
4.49M
      *outptr++ = invalue;
250
4.49M
      *outptr++ = invalue;
251
4.49M
    }
252
2.45M
    _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1, 1,
253
2.45M
                       cinfo->output_width);
254
2.45M
    inrow++;
255
2.45M
    outrow += 2;
256
2.45M
  }
257
1.85M
}
jdsample-8.c:h2v2_upsample
Line
Count
Source
235
1.85M
{
236
1.85M
  _JSAMPARRAY output_data = *output_data_ptr;
237
1.85M
  register _JSAMPROW inptr, outptr;
238
1.85M
  register _JSAMPLE invalue;
239
1.85M
  _JSAMPROW outend;
240
1.85M
  int inrow, outrow;
241
242
1.85M
  inrow = outrow = 0;
243
4.31M
  while (outrow < cinfo->max_v_samp_factor) {
244
2.45M
    inptr = input_data[inrow];
245
2.45M
    outptr = output_data[outrow];
246
2.45M
    outend = outptr + cinfo->output_width;
247
6.95M
    while (outptr < outend) {
248
4.49M
      invalue = *inptr++;
249
4.49M
      *outptr++ = invalue;
250
4.49M
      *outptr++ = invalue;
251
4.49M
    }
252
2.45M
    _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1, 1,
253
2.45M
                       cinfo->output_width);
254
2.45M
    inrow++;
255
2.45M
    outrow += 2;
256
2.45M
  }
257
1.85M
}
Unexecuted instantiation: jdsample-12.c:h2v2_upsample
Unexecuted instantiation: jdsample-16.c:h2v2_upsample
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
10.6M
{
279
10.6M
  _JSAMPARRAY output_data = *output_data_ptr;
280
10.6M
  register _JSAMPROW inptr, outptr;
281
10.6M
  register int invalue;
282
10.6M
  register JDIMENSION colctr;
283
10.6M
  int inrow;
284
285
32.6M
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
286
22.0M
    inptr = input_data[inrow];
287
22.0M
    outptr = output_data[inrow];
288
    /* Special case for first column */
289
22.0M
    invalue = *inptr++;
290
22.0M
    *outptr++ = (_JSAMPLE)invalue;
291
22.0M
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[0] + 2) >> 2);
292
293
1.05G
    for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
294
      /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
295
1.03G
      invalue = (*inptr++) * 3;
296
1.03G
      *outptr++ = (_JSAMPLE)((invalue + inptr[-2] + 1) >> 2);
297
1.03G
      *outptr++ = (_JSAMPLE)((invalue + inptr[0] + 2) >> 2);
298
1.03G
    }
299
300
    /* Special case for last column */
301
22.0M
    invalue = *inptr;
302
22.0M
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[-1] + 1) >> 2);
303
22.0M
    *outptr++ = (_JSAMPLE)invalue;
304
22.0M
  }
305
10.6M
}
jdsample-8.c:h2v1_fancy_upsample
Line
Count
Source
278
10.6M
{
279
10.6M
  _JSAMPARRAY output_data = *output_data_ptr;
280
10.6M
  register _JSAMPROW inptr, outptr;
281
10.6M
  register int invalue;
282
10.6M
  register JDIMENSION colctr;
283
10.6M
  int inrow;
284
285
32.6M
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
286
22.0M
    inptr = input_data[inrow];
287
22.0M
    outptr = output_data[inrow];
288
    /* Special case for first column */
289
22.0M
    invalue = *inptr++;
290
22.0M
    *outptr++ = (_JSAMPLE)invalue;
291
22.0M
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[0] + 2) >> 2);
292
293
1.05G
    for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
294
      /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
295
1.03G
      invalue = (*inptr++) * 3;
296
1.03G
      *outptr++ = (_JSAMPLE)((invalue + inptr[-2] + 1) >> 2);
297
1.03G
      *outptr++ = (_JSAMPLE)((invalue + inptr[0] + 2) >> 2);
298
1.03G
    }
299
300
    /* Special case for last column */
301
22.0M
    invalue = *inptr;
302
22.0M
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[-1] + 1) >> 2);
303
22.0M
    *outptr++ = (_JSAMPLE)invalue;
304
22.0M
  }
305
10.6M
}
Unexecuted instantiation: jdsample-12.c:h2v1_fancy_upsample
Unexecuted instantiation: jdsample-16.c:h2v1_fancy_upsample
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
11.7M
{
319
11.7M
  _JSAMPARRAY output_data = *output_data_ptr;
320
11.7M
  _JSAMPROW inptr0, inptr1, outptr;
321
#if BITS_IN_JSAMPLE == 8
322
  int thiscolsum, bias;
323
#else
324
  JLONG thiscolsum, bias;
325
#endif
326
11.7M
  JDIMENSION colctr;
327
11.7M
  int inrow, outrow, v;
328
329
11.7M
  inrow = outrow = 0;
330
25.1M
  while (outrow < cinfo->max_v_samp_factor) {
331
40.1M
    for (v = 0; v < 2; v++) {
332
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
333
26.7M
      inptr0 = input_data[inrow];
334
26.7M
      if (v == 0) {             /* next nearest is row above */
335
13.3M
        inptr1 = input_data[inrow - 1];
336
13.3M
        bias = 1;
337
13.3M
      } else {                  /* next nearest is row below */
338
13.3M
        inptr1 = input_data[inrow + 1];
339
13.3M
        bias = 2;
340
13.3M
      }
341
26.7M
      outptr = output_data[outrow++];
342
343
3.11G
      for (colctr = 0; colctr < compptr->downsampled_width; colctr++) {
344
3.08G
        thiscolsum = (*inptr0++) * 3 + (*inptr1++);
345
3.08G
        *outptr++ = (_JSAMPLE)((thiscolsum + bias) >> 2);
346
3.08G
      }
347
26.7M
    }
348
13.3M
    inrow++;
349
13.3M
  }
350
11.7M
}
jdsample-8.c:h1v2_fancy_upsample
Line
Count
Source
318
11.7M
{
319
11.7M
  _JSAMPARRAY output_data = *output_data_ptr;
320
11.7M
  _JSAMPROW inptr0, inptr1, outptr;
321
11.7M
#if BITS_IN_JSAMPLE == 8
322
11.7M
  int thiscolsum, bias;
323
#else
324
  JLONG thiscolsum, bias;
325
#endif
326
11.7M
  JDIMENSION colctr;
327
11.7M
  int inrow, outrow, v;
328
329
11.7M
  inrow = outrow = 0;
330
25.1M
  while (outrow < cinfo->max_v_samp_factor) {
331
40.1M
    for (v = 0; v < 2; v++) {
332
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
333
26.7M
      inptr0 = input_data[inrow];
334
26.7M
      if (v == 0) {             /* next nearest is row above */
335
13.3M
        inptr1 = input_data[inrow - 1];
336
13.3M
        bias = 1;
337
13.3M
      } else {                  /* next nearest is row below */
338
13.3M
        inptr1 = input_data[inrow + 1];
339
13.3M
        bias = 2;
340
13.3M
      }
341
26.7M
      outptr = output_data[outrow++];
342
343
3.11G
      for (colctr = 0; colctr < compptr->downsampled_width; colctr++) {
344
3.08G
        thiscolsum = (*inptr0++) * 3 + (*inptr1++);
345
3.08G
        *outptr++ = (_JSAMPLE)((thiscolsum + bias) >> 2);
346
3.08G
      }
347
26.7M
    }
348
13.3M
    inrow++;
349
13.3M
  }
350
11.7M
}
Unexecuted instantiation: jdsample-12.c:h1v2_fancy_upsample
Unexecuted instantiation: jdsample-16.c:h1v2_fancy_upsample
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
8.13M
{
365
8.13M
  _JSAMPARRAY output_data = *output_data_ptr;
366
8.13M
  register _JSAMPROW inptr0, inptr1, outptr;
367
#if BITS_IN_JSAMPLE == 8
368
  register int thiscolsum, lastcolsum, nextcolsum;
369
#else
370
  register JLONG thiscolsum, lastcolsum, nextcolsum;
371
#endif
372
8.13M
  register JDIMENSION colctr;
373
8.13M
  int inrow, outrow, v;
374
375
8.13M
  inrow = outrow = 0;
376
18.8M
  while (outrow < cinfo->max_v_samp_factor) {
377
32.1M
    for (v = 0; v < 2; v++) {
378
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
379
21.4M
      inptr0 = input_data[inrow];
380
21.4M
      if (v == 0)               /* next nearest is row above */
381
10.7M
        inptr1 = input_data[inrow - 1];
382
10.7M
      else                      /* next nearest is row below */
383
10.7M
        inptr1 = input_data[inrow + 1];
384
21.4M
      outptr = output_data[outrow++];
385
386
      /* Special case for first column */
387
21.4M
      thiscolsum = (*inptr0++) * 3 + (*inptr1++);
388
21.4M
      nextcolsum = (*inptr0++) * 3 + (*inptr1++);
389
21.4M
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 8) >> 4);
390
21.4M
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
391
21.4M
      lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
392
393
2.14G
      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
2.12G
        nextcolsum = (*inptr0++) * 3 + (*inptr1++);
397
2.12G
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
398
2.12G
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
399
2.12G
        lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
400
2.12G
      }
401
402
      /* Special case for last column */
403
21.4M
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
404
21.4M
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 7) >> 4);
405
21.4M
    }
406
10.7M
    inrow++;
407
10.7M
  }
408
8.13M
}
jdsample-8.c:h2v2_fancy_upsample
Line
Count
Source
364
8.13M
{
365
8.13M
  _JSAMPARRAY output_data = *output_data_ptr;
366
8.13M
  register _JSAMPROW inptr0, inptr1, outptr;
367
8.13M
#if BITS_IN_JSAMPLE == 8
368
8.13M
  register int thiscolsum, lastcolsum, nextcolsum;
369
#else
370
  register JLONG thiscolsum, lastcolsum, nextcolsum;
371
#endif
372
8.13M
  register JDIMENSION colctr;
373
8.13M
  int inrow, outrow, v;
374
375
8.13M
  inrow = outrow = 0;
376
18.8M
  while (outrow < cinfo->max_v_samp_factor) {
377
32.1M
    for (v = 0; v < 2; v++) {
378
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
379
21.4M
      inptr0 = input_data[inrow];
380
21.4M
      if (v == 0)               /* next nearest is row above */
381
10.7M
        inptr1 = input_data[inrow - 1];
382
10.7M
      else                      /* next nearest is row below */
383
10.7M
        inptr1 = input_data[inrow + 1];
384
21.4M
      outptr = output_data[outrow++];
385
386
      /* Special case for first column */
387
21.4M
      thiscolsum = (*inptr0++) * 3 + (*inptr1++);
388
21.4M
      nextcolsum = (*inptr0++) * 3 + (*inptr1++);
389
21.4M
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 8) >> 4);
390
21.4M
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
391
21.4M
      lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
392
393
2.14G
      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
2.12G
        nextcolsum = (*inptr0++) * 3 + (*inptr1++);
397
2.12G
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
398
2.12G
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
399
2.12G
        lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
400
2.12G
      }
401
402
      /* Special case for last column */
403
21.4M
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
404
21.4M
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 7) >> 4);
405
21.4M
    }
406
10.7M
    inrow++;
407
10.7M
  }
408
8.13M
}
Unexecuted instantiation: jdsample-12.c:h2v2_fancy_upsample
Unexecuted instantiation: jdsample-16.c:h2v2_fancy_upsample
409
410
411
/*
412
 * Module initialization routine for upsampling.
413
 */
414
415
GLOBAL(void)
416
_jinit_upsampler(j_decompress_ptr cinfo)
417
61.0k
{
418
61.0k
  my_upsample_ptr upsample;
419
61.0k
  int ci;
420
61.0k
  jpeg_component_info *compptr;
421
61.0k
  boolean need_buffer, do_fancy;
422
61.0k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
61.0k
#ifdef D_LOSSLESS_SUPPORTED
425
61.0k
  if (cinfo->master->lossless) {
426
#if BITS_IN_JSAMPLE == 8
427
7.20k
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
428
#else
429
10.1k
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
430
10.1k
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
431
0
#endif
432
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
433
17.3k
  } else
434
43.7k
#endif
435
43.7k
  {
436
43.7k
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
437
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
438
43.7k
  }
439
440
61.0k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
441
61.0k
    upsample = (my_upsample_ptr)
442
61.0k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
443
61.0k
                                  sizeof(my_upsampler));
444
61.0k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
445
61.0k
    upsample->pub.start_pass = start_pass_upsample;
446
61.0k
    upsample->pub._upsample = sep_upsample;
447
61.0k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
448
61.0k
  } else
449
0
    upsample = (my_upsample_ptr)cinfo->upsample;
450
451
61.0k
  if (cinfo->CCIR601_sampling)  /* this isn't supported */
452
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
453
454
  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
455
   * so don't ask for it.
456
   */
457
61.0k
  do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
458
459
  /* Verify we can handle the sampling factors, select per-component methods,
460
   * and create storage as needed.
461
   */
462
219k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
463
158k
       ci++, compptr++) {
464
    /* Compute size of an "input group" after IDCT scaling.  This many samples
465
     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
466
     */
467
158k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
468
158k
                 cinfo->_min_DCT_scaled_size;
469
158k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
470
158k
                 cinfo->_min_DCT_scaled_size;
471
158k
    h_out_group = cinfo->max_h_samp_factor;
472
158k
    v_out_group = cinfo->max_v_samp_factor;
473
158k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
474
158k
    need_buffer = TRUE;
475
158k
    if (!compptr->component_needed) {
476
      /* Don't bother to upsample an uninteresting component. */
477
0
      upsample->methods[ci] = noop_upsample;
478
0
      need_buffer = FALSE;
479
158k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
480
      /* Fullsize components can be processed without any work. */
481
67.5k
      upsample->methods[ci] = fullsize_upsample;
482
67.5k
      need_buffer = FALSE;
483
90.5k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
484
      /* Special cases for 2h1v upsampling */
485
10.8k
      if (do_fancy && compptr->downsampled_width > 2) {
486
#ifdef WITH_SIMD
487
4.65k
        if (jsimd_can_h2v1_fancy_upsample())
488
0
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
489
4.65k
        else
490
4.65k
#endif
491
4.65k
          upsample->methods[ci] = h2v1_fancy_upsample;
492
5.55k
      } else {
493
#ifdef WITH_SIMD
494
3.02k
        if (jsimd_can_h2v1_upsample())
495
0
          upsample->methods[ci] = jsimd_h2v1_upsample;
496
3.02k
        else
497
3.02k
#endif
498
3.02k
          upsample->methods[ci] = h2v1_upsample;
499
5.31k
      }
500
79.6k
    } else if (h_in_group == h_out_group &&
501
25.9k
               v_in_group * 2 == v_out_group && do_fancy) {
502
      /* Non-fancy upsampling is handled by the generic method */
503
#if defined(WITH_SIMD) && (defined(__arm__) || defined(__aarch64__) || \
504
                           defined(_M_ARM) || defined(_M_ARM64) || \
505
                           defined(_M_ARM64EC))
506
      if (jsimd_can_h1v2_fancy_upsample())
507
        upsample->methods[ci] = jsimd_h1v2_fancy_upsample;
508
      else
509
#endif
510
10.2k
        upsample->methods[ci] = h1v2_fancy_upsample;
511
10.2k
      upsample->pub.need_context_rows = TRUE;
512
69.3k
    } else if (h_in_group * 2 == h_out_group &&
513
37.0k
               v_in_group * 2 == v_out_group) {
514
      /* Special cases for 2h2v upsampling */
515
30.2k
      if (do_fancy && compptr->downsampled_width > 2) {
516
#ifdef WITH_SIMD
517
22.6k
        if (jsimd_can_h2v2_fancy_upsample())
518
0
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
519
22.6k
        else
520
22.6k
#endif
521
22.6k
          upsample->methods[ci] = h2v2_fancy_upsample;
522
24.7k
        upsample->pub.need_context_rows = TRUE;
523
24.7k
      } else {
524
#ifdef WITH_SIMD
525
3.21k
        if (jsimd_can_h2v2_upsample())
526
0
          upsample->methods[ci] = jsimd_h2v2_upsample;
527
3.21k
        else
528
3.21k
#endif
529
3.21k
          upsample->methods[ci] = h2v2_upsample;
530
5.47k
      }
531
39.1k
    } else if ((h_out_group % h_in_group) == 0 &&
532
39.0k
               (v_out_group % v_in_group) == 0) {
533
      /* Generic integral-factors upsampling method */
534
#if defined(WITH_SIMD) && defined(__mips__)
535
      if (jsimd_can_int_upsample())
536
        upsample->methods[ci] = jsimd_int_upsample;
537
      else
538
#endif
539
38.8k
        upsample->methods[ci] = int_upsample;
540
38.8k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
541
38.8k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
542
38.8k
    } else
543
362
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
544
158k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
545
90.1k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
546
90.1k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
547
90.1k
         (JDIMENSION)jround_up((long)cinfo->output_width,
548
90.1k
                               (long)cinfo->max_h_samp_factor),
549
90.1k
         (JDIMENSION)cinfo->max_v_samp_factor);
550
90.1k
    }
551
158k
  }
552
61.0k
}
jinit_upsampler
Line
Count
Source
417
46.6k
{
418
46.6k
  my_upsample_ptr upsample;
419
46.6k
  int ci;
420
46.6k
  jpeg_component_info *compptr;
421
46.6k
  boolean need_buffer, do_fancy;
422
46.6k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
46.6k
#ifdef D_LOSSLESS_SUPPORTED
425
46.6k
  if (cinfo->master->lossless) {
426
7.20k
#if BITS_IN_JSAMPLE == 8
427
7.20k
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
428
#else
429
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
430
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
431
#endif
432
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
433
7.20k
  } else
434
39.4k
#endif
435
39.4k
  {
436
39.4k
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
437
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
438
39.4k
  }
439
440
46.6k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
441
46.6k
    upsample = (my_upsample_ptr)
442
46.6k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
443
46.6k
                                  sizeof(my_upsampler));
444
46.6k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
445
46.6k
    upsample->pub.start_pass = start_pass_upsample;
446
46.6k
    upsample->pub._upsample = sep_upsample;
447
46.6k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
448
46.6k
  } else
449
0
    upsample = (my_upsample_ptr)cinfo->upsample;
450
451
46.6k
  if (cinfo->CCIR601_sampling)  /* this isn't supported */
452
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
453
454
  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
455
   * so don't ask for it.
456
   */
457
46.6k
  do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
458
459
  /* Verify we can handle the sampling factors, select per-component methods,
460
   * and create storage as needed.
461
   */
462
164k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
463
117k
       ci++, compptr++) {
464
    /* Compute size of an "input group" after IDCT scaling.  This many samples
465
     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
466
     */
467
117k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
468
117k
                 cinfo->_min_DCT_scaled_size;
469
117k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
470
117k
                 cinfo->_min_DCT_scaled_size;
471
117k
    h_out_group = cinfo->max_h_samp_factor;
472
117k
    v_out_group = cinfo->max_v_samp_factor;
473
117k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
474
117k
    need_buffer = TRUE;
475
117k
    if (!compptr->component_needed) {
476
      /* Don't bother to upsample an uninteresting component. */
477
0
      upsample->methods[ci] = noop_upsample;
478
0
      need_buffer = FALSE;
479
117k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
480
      /* Fullsize components can be processed without any work. */
481
54.8k
      upsample->methods[ci] = fullsize_upsample;
482
54.8k
      need_buffer = FALSE;
483
62.5k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
484
      /* Special cases for 2h1v upsampling */
485
7.68k
      if (do_fancy && compptr->downsampled_width > 2) {
486
4.65k
#ifdef WITH_SIMD
487
4.65k
        if (jsimd_can_h2v1_fancy_upsample())
488
0
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
489
4.65k
        else
490
4.65k
#endif
491
4.65k
          upsample->methods[ci] = h2v1_fancy_upsample;
492
4.65k
      } else {
493
3.02k
#ifdef WITH_SIMD
494
3.02k
        if (jsimd_can_h2v1_upsample())
495
0
          upsample->methods[ci] = jsimd_h2v1_upsample;
496
3.02k
        else
497
3.02k
#endif
498
3.02k
          upsample->methods[ci] = h2v1_upsample;
499
3.02k
      }
500
54.8k
    } else if (h_in_group == h_out_group &&
501
17.8k
               v_in_group * 2 == v_out_group && do_fancy) {
502
      /* Non-fancy upsampling is handled by the generic method */
503
#if defined(WITH_SIMD) && (defined(__arm__) || defined(__aarch64__) || \
504
                           defined(_M_ARM) || defined(_M_ARM64) || \
505
                           defined(_M_ARM64EC))
506
      if (jsimd_can_h1v2_fancy_upsample())
507
        upsample->methods[ci] = jsimd_h1v2_fancy_upsample;
508
      else
509
#endif
510
8.68k
        upsample->methods[ci] = h1v2_fancy_upsample;
511
8.68k
      upsample->pub.need_context_rows = TRUE;
512
46.1k
    } else if (h_in_group * 2 == h_out_group &&
513
30.0k
               v_in_group * 2 == v_out_group) {
514
      /* Special cases for 2h2v upsampling */
515
25.9k
      if (do_fancy && compptr->downsampled_width > 2) {
516
22.6k
#ifdef WITH_SIMD
517
22.6k
        if (jsimd_can_h2v2_fancy_upsample())
518
0
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
519
22.6k
        else
520
22.6k
#endif
521
22.6k
          upsample->methods[ci] = h2v2_fancy_upsample;
522
22.6k
        upsample->pub.need_context_rows = TRUE;
523
22.6k
      } else {
524
3.21k
#ifdef WITH_SIMD
525
3.21k
        if (jsimd_can_h2v2_upsample())
526
0
          upsample->methods[ci] = jsimd_h2v2_upsample;
527
3.21k
        else
528
3.21k
#endif
529
3.21k
          upsample->methods[ci] = h2v2_upsample;
530
3.21k
      }
531
25.9k
    } else if ((h_out_group % h_in_group) == 0 &&
532
20.1k
               (v_out_group % v_in_group) == 0) {
533
      /* Generic integral-factors upsampling method */
534
#if defined(WITH_SIMD) && defined(__mips__)
535
      if (jsimd_can_int_upsample())
536
        upsample->methods[ci] = jsimd_int_upsample;
537
      else
538
#endif
539
20.1k
        upsample->methods[ci] = int_upsample;
540
20.1k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
541
20.1k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
542
20.1k
    } else
543
127
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
544
117k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
545
62.4k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
546
62.4k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
547
62.4k
         (JDIMENSION)jround_up((long)cinfo->output_width,
548
62.4k
                               (long)cinfo->max_h_samp_factor),
549
62.4k
         (JDIMENSION)cinfo->max_v_samp_factor);
550
62.4k
    }
551
117k
  }
552
46.6k
}
j12init_upsampler
Line
Count
Source
417
9.29k
{
418
9.29k
  my_upsample_ptr upsample;
419
9.29k
  int ci;
420
9.29k
  jpeg_component_info *compptr;
421
9.29k
  boolean need_buffer, do_fancy;
422
9.29k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
9.29k
#ifdef D_LOSSLESS_SUPPORTED
425
9.29k
  if (cinfo->master->lossless) {
426
#if BITS_IN_JSAMPLE == 8
427
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
428
#else
429
5.03k
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
430
5.03k
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
431
0
#endif
432
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
433
5.03k
  } else
434
4.26k
#endif
435
4.26k
  {
436
4.26k
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
437
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
438
4.26k
  }
439
440
9.29k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
441
9.29k
    upsample = (my_upsample_ptr)
442
9.29k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
443
9.29k
                                  sizeof(my_upsampler));
444
9.29k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
445
9.29k
    upsample->pub.start_pass = start_pass_upsample;
446
9.29k
    upsample->pub._upsample = sep_upsample;
447
9.29k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
448
9.29k
  } else
449
0
    upsample = (my_upsample_ptr)cinfo->upsample;
450
451
9.29k
  if (cinfo->CCIR601_sampling)  /* this isn't supported */
452
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
453
454
  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
455
   * so don't ask for it.
456
   */
457
9.29k
  do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
458
459
  /* Verify we can handle the sampling factors, select per-component methods,
460
   * and create storage as needed.
461
   */
462
35.4k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
463
26.1k
       ci++, compptr++) {
464
    /* Compute size of an "input group" after IDCT scaling.  This many samples
465
     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
466
     */
467
26.1k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
468
26.1k
                 cinfo->_min_DCT_scaled_size;
469
26.1k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
470
26.1k
                 cinfo->_min_DCT_scaled_size;
471
26.1k
    h_out_group = cinfo->max_h_samp_factor;
472
26.1k
    v_out_group = cinfo->max_v_samp_factor;
473
26.1k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
474
26.1k
    need_buffer = TRUE;
475
26.1k
    if (!compptr->component_needed) {
476
      /* Don't bother to upsample an uninteresting component. */
477
0
      upsample->methods[ci] = noop_upsample;
478
0
      need_buffer = FALSE;
479
26.1k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
480
      /* Fullsize components can be processed without any work. */
481
8.43k
      upsample->methods[ci] = fullsize_upsample;
482
8.43k
      need_buffer = FALSE;
483
17.7k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
484
      /* Special cases for 2h1v upsampling */
485
2.13k
      if (do_fancy && compptr->downsampled_width > 2) {
486
#ifdef WITH_SIMD
487
        if (jsimd_can_h2v1_fancy_upsample())
488
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
489
        else
490
#endif
491
895
          upsample->methods[ci] = h2v1_fancy_upsample;
492
1.23k
      } else {
493
#ifdef WITH_SIMD
494
        if (jsimd_can_h2v1_upsample())
495
          upsample->methods[ci] = jsimd_h2v1_upsample;
496
        else
497
#endif
498
1.23k
          upsample->methods[ci] = h2v1_upsample;
499
1.23k
      }
500
15.6k
    } else if (h_in_group == h_out_group &&
501
5.05k
               v_in_group * 2 == v_out_group && do_fancy) {
502
      /* Non-fancy upsampling is handled by the generic method */
503
#if defined(WITH_SIMD) && (defined(__arm__) || defined(__aarch64__) || \
504
                           defined(_M_ARM) || defined(_M_ARM64) || \
505
                           defined(_M_ARM64EC))
506
      if (jsimd_can_h1v2_fancy_upsample())
507
        upsample->methods[ci] = jsimd_h1v2_fancy_upsample;
508
      else
509
#endif
510
1.59k
        upsample->methods[ci] = h1v2_fancy_upsample;
511
1.59k
      upsample->pub.need_context_rows = TRUE;
512
14.0k
    } else if (h_in_group * 2 == h_out_group &&
513
4.97k
               v_in_group * 2 == v_out_group) {
514
      /* Special cases for 2h2v upsampling */
515
3.14k
      if (do_fancy && compptr->downsampled_width > 2) {
516
#ifdef WITH_SIMD
517
        if (jsimd_can_h2v2_fancy_upsample())
518
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
519
        else
520
#endif
521
2.02k
          upsample->methods[ci] = h2v2_fancy_upsample;
522
2.02k
        upsample->pub.need_context_rows = TRUE;
523
2.02k
      } else {
524
#ifdef WITH_SIMD
525
        if (jsimd_can_h2v2_upsample())
526
          upsample->methods[ci] = jsimd_h2v2_upsample;
527
        else
528
#endif
529
1.12k
          upsample->methods[ci] = h2v2_upsample;
530
1.12k
      }
531
10.8k
    } else if ((h_out_group % h_in_group) == 0 &&
532
10.8k
               (v_out_group % v_in_group) == 0) {
533
      /* Generic integral-factors upsampling method */
534
#if defined(WITH_SIMD) && defined(__mips__)
535
      if (jsimd_can_int_upsample())
536
        upsample->methods[ci] = jsimd_int_upsample;
537
      else
538
#endif
539
10.7k
        upsample->methods[ci] = int_upsample;
540
10.7k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
541
10.7k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
542
10.7k
    } else
543
119
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
544
26.1k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
545
17.6k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
546
17.6k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
547
17.6k
         (JDIMENSION)jround_up((long)cinfo->output_width,
548
17.6k
                               (long)cinfo->max_h_samp_factor),
549
17.6k
         (JDIMENSION)cinfo->max_v_samp_factor);
550
17.6k
    }
551
26.1k
  }
552
9.29k
}
j16init_upsampler
Line
Count
Source
417
5.14k
{
418
5.14k
  my_upsample_ptr upsample;
419
5.14k
  int ci;
420
5.14k
  jpeg_component_info *compptr;
421
5.14k
  boolean need_buffer, do_fancy;
422
5.14k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
5.14k
#ifdef D_LOSSLESS_SUPPORTED
425
5.14k
  if (cinfo->master->lossless) {
426
#if BITS_IN_JSAMPLE == 8
427
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
428
#else
429
5.14k
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
430
5.14k
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
431
0
#endif
432
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
433
5.14k
  } else
434
0
#endif
435
0
  {
436
0
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
437
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
438
0
  }
439
440
5.14k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
441
5.14k
    upsample = (my_upsample_ptr)
442
5.14k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
443
5.14k
                                  sizeof(my_upsampler));
444
5.14k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
445
5.14k
    upsample->pub.start_pass = start_pass_upsample;
446
5.14k
    upsample->pub._upsample = sep_upsample;
447
5.14k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
448
5.14k
  } else
449
0
    upsample = (my_upsample_ptr)cinfo->upsample;
450
451
5.14k
  if (cinfo->CCIR601_sampling)  /* this isn't supported */
452
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
453
454
  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
455
   * so don't ask for it.
456
   */
457
5.14k
  do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
458
459
  /* Verify we can handle the sampling factors, select per-component methods,
460
   * and create storage as needed.
461
   */
462
19.6k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
463
14.4k
       ci++, compptr++) {
464
    /* Compute size of an "input group" after IDCT scaling.  This many samples
465
     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
466
     */
467
14.4k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
468
14.4k
                 cinfo->_min_DCT_scaled_size;
469
14.4k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
470
14.4k
                 cinfo->_min_DCT_scaled_size;
471
14.4k
    h_out_group = cinfo->max_h_samp_factor;
472
14.4k
    v_out_group = cinfo->max_v_samp_factor;
473
14.4k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
474
14.4k
    need_buffer = TRUE;
475
14.4k
    if (!compptr->component_needed) {
476
      /* Don't bother to upsample an uninteresting component. */
477
0
      upsample->methods[ci] = noop_upsample;
478
0
      need_buffer = FALSE;
479
14.4k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
480
      /* Fullsize components can be processed without any work. */
481
4.19k
      upsample->methods[ci] = fullsize_upsample;
482
4.19k
      need_buffer = FALSE;
483
10.2k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
484
      /* Special cases for 2h1v upsampling */
485
1.04k
      if (do_fancy && compptr->downsampled_width > 2) {
486
#ifdef WITH_SIMD
487
        if (jsimd_can_h2v1_fancy_upsample())
488
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
489
        else
490
#endif
491
0
          upsample->methods[ci] = h2v1_fancy_upsample;
492
1.04k
      } else {
493
#ifdef WITH_SIMD
494
        if (jsimd_can_h2v1_upsample())
495
          upsample->methods[ci] = jsimd_h2v1_upsample;
496
        else
497
#endif
498
1.04k
          upsample->methods[ci] = h2v1_upsample;
499
1.04k
      }
500
9.22k
    } else if (h_in_group == h_out_group &&
501
3.00k
               v_in_group * 2 == v_out_group && do_fancy) {
502
      /* Non-fancy upsampling is handled by the generic method */
503
#if defined(WITH_SIMD) && (defined(__arm__) || defined(__aarch64__) || \
504
                           defined(_M_ARM) || defined(_M_ARM64) || \
505
                           defined(_M_ARM64EC))
506
      if (jsimd_can_h1v2_fancy_upsample())
507
        upsample->methods[ci] = jsimd_h1v2_fancy_upsample;
508
      else
509
#endif
510
0
        upsample->methods[ci] = h1v2_fancy_upsample;
511
0
      upsample->pub.need_context_rows = TRUE;
512
9.22k
    } else if (h_in_group * 2 == h_out_group &&
513
2.06k
               v_in_group * 2 == v_out_group) {
514
      /* Special cases for 2h2v upsampling */
515
1.14k
      if (do_fancy && compptr->downsampled_width > 2) {
516
#ifdef WITH_SIMD
517
        if (jsimd_can_h2v2_fancy_upsample())
518
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
519
        else
520
#endif
521
0
          upsample->methods[ci] = h2v2_fancy_upsample;
522
0
        upsample->pub.need_context_rows = TRUE;
523
1.14k
      } else {
524
#ifdef WITH_SIMD
525
        if (jsimd_can_h2v2_upsample())
526
          upsample->methods[ci] = jsimd_h2v2_upsample;
527
        else
528
#endif
529
1.14k
          upsample->methods[ci] = h2v2_upsample;
530
1.14k
      }
531
8.08k
    } else if ((h_out_group % h_in_group) == 0 &&
532
8.03k
               (v_out_group % v_in_group) == 0) {
533
      /* Generic integral-factors upsampling method */
534
#if defined(WITH_SIMD) && defined(__mips__)
535
      if (jsimd_can_int_upsample())
536
        upsample->methods[ci] = jsimd_int_upsample;
537
      else
538
#endif
539
7.96k
        upsample->methods[ci] = int_upsample;
540
7.96k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
541
7.96k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
542
7.96k
    } else
543
116
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
544
14.4k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
545
10.1k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
546
10.1k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
547
10.1k
         (JDIMENSION)jround_up((long)cinfo->output_width,
548
10.1k
                               (long)cinfo->max_h_samp_factor),
549
10.1k
         (JDIMENSION)cinfo->max_v_samp_factor);
550
10.1k
    }
551
14.4k
  }
552
5.14k
}
553
554
#endif /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */