Coverage Report

Created: 2025-08-28 06:38

/src/libjpeg-turbo/src/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, 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
5.06k
{
44
5.06k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
5.06k
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
5.06k
  upsample->rows_to_go = cinfo->output_height;
50
5.06k
}
jdsample-8.c:start_pass_upsample
Line
Count
Source
43
4.90k
{
44
4.90k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
4.90k
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
4.90k
  upsample->rows_to_go = cinfo->output_height;
50
4.90k
}
jdsample-12.c:start_pass_upsample
Line
Count
Source
43
150
{
44
150
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
150
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
150
  upsample->rows_to_go = cinfo->output_height;
50
150
}
jdsample-16.c:start_pass_upsample
Line
Count
Source
43
9
{
44
9
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
9
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
9
  upsample->rows_to_go = cinfo->output_height;
50
9
}
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
10.2M
{
67
10.2M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
68
10.2M
  int ci;
69
10.2M
  jpeg_component_info *compptr;
70
10.2M
  JDIMENSION num_rows;
71
72
  /* Fill the conversion buffer, if it's empty */
73
10.2M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
74
34.5M
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
75
26.6M
         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
26.6M
      (*upsample->methods[ci]) (cinfo, compptr,
80
26.6M
        input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
81
26.6M
        upsample->color_buf + ci);
82
26.6M
    }
83
7.97M
    upsample->next_row_out = 0;
84
7.97M
  }
85
86
  /* Color-convert and emit rows */
87
88
  /* How many we have in the buffer: */
89
10.2M
  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
10.2M
  if (num_rows > upsample->rows_to_go)
94
957
    num_rows = upsample->rows_to_go;
95
  /* And not more than what the client can accept: */
96
10.2M
  out_rows_avail -= *out_row_ctr;
97
10.2M
  if (num_rows > out_rows_avail)
98
2.27M
    num_rows = out_rows_avail;
99
100
10.2M
  (*cinfo->cconvert->_color_convert) (cinfo, upsample->color_buf,
101
10.2M
                                      (JDIMENSION)upsample->next_row_out,
102
10.2M
                                      output_buf + *out_row_ctr,
103
10.2M
                                      (int)num_rows);
104
105
  /* Adjust counts */
106
10.2M
  *out_row_ctr += num_rows;
107
10.2M
  upsample->rows_to_go -= num_rows;
108
10.2M
  upsample->next_row_out += num_rows;
109
  /* When the buffer is emptied, declare this input row group consumed */
110
10.2M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
111
7.97M
    (*in_row_group_ctr)++;
112
10.2M
}
jdsample-8.c:sep_upsample
Line
Count
Source
66
10.2M
{
67
10.2M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
68
10.2M
  int ci;
69
10.2M
  jpeg_component_info *compptr;
70
10.2M
  JDIMENSION num_rows;
71
72
  /* Fill the conversion buffer, if it's empty */
73
10.2M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
74
34.5M
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
75
26.6M
         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
26.6M
      (*upsample->methods[ci]) (cinfo, compptr,
80
26.6M
        input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
81
26.6M
        upsample->color_buf + ci);
82
26.6M
    }
83
7.97M
    upsample->next_row_out = 0;
84
7.97M
  }
85
86
  /* Color-convert and emit rows */
87
88
  /* How many we have in the buffer: */
89
10.2M
  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
10.2M
  if (num_rows > upsample->rows_to_go)
94
957
    num_rows = upsample->rows_to_go;
95
  /* And not more than what the client can accept: */
96
10.2M
  out_rows_avail -= *out_row_ctr;
97
10.2M
  if (num_rows > out_rows_avail)
98
2.27M
    num_rows = out_rows_avail;
99
100
10.2M
  (*cinfo->cconvert->_color_convert) (cinfo, upsample->color_buf,
101
10.2M
                                      (JDIMENSION)upsample->next_row_out,
102
10.2M
                                      output_buf + *out_row_ctr,
103
10.2M
                                      (int)num_rows);
104
105
  /* Adjust counts */
106
10.2M
  *out_row_ctr += num_rows;
107
10.2M
  upsample->rows_to_go -= num_rows;
108
10.2M
  upsample->next_row_out += num_rows;
109
  /* When the buffer is emptied, declare this input row group consumed */
110
10.2M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
111
7.97M
    (*in_row_group_ctr)++;
112
10.2M
}
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
22.4M
{
132
22.4M
  *output_data_ptr = input_data;
133
22.4M
}
jdsample-8.c:fullsize_upsample
Line
Count
Source
131
22.4M
{
132
22.4M
  *output_data_ptr = input_data;
133
22.4M
}
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
2.38M
{
164
2.38M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
165
2.38M
  _JSAMPARRAY output_data = *output_data_ptr;
166
2.38M
  register _JSAMPROW inptr, outptr;
167
2.38M
  register _JSAMPLE invalue;
168
2.38M
  register int h;
169
2.38M
  _JSAMPROW outend;
170
2.38M
  int h_expand, v_expand;
171
2.38M
  int inrow, outrow;
172
173
2.38M
  h_expand = upsample->h_expand[compptr->component_index];
174
2.38M
  v_expand = upsample->v_expand[compptr->component_index];
175
176
2.38M
  inrow = outrow = 0;
177
4.81M
  while (outrow < cinfo->max_v_samp_factor) {
178
    /* Generate one output row with proper horizontal expansion */
179
2.42M
    inptr = input_data[inrow];
180
2.42M
    outptr = output_data[outrow];
181
2.42M
    outend = outptr + cinfo->output_width;
182
122M
    while (outptr < outend) {
183
120M
      invalue = *inptr++;
184
276M
      for (h = h_expand; h > 0; h--) {
185
156M
        *outptr++ = invalue;
186
156M
      }
187
120M
    }
188
    /* Generate any additional output rows by duplicating the first one */
189
2.42M
    if (v_expand > 1) {
190
1.97M
      _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1,
191
1.97M
                         v_expand - 1, cinfo->output_width);
192
1.97M
    }
193
2.42M
    inrow++;
194
2.42M
    outrow += v_expand;
195
2.42M
  }
196
2.38M
}
jdsample-8.c:int_upsample
Line
Count
Source
163
2.38M
{
164
2.38M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
165
2.38M
  _JSAMPARRAY output_data = *output_data_ptr;
166
2.38M
  register _JSAMPROW inptr, outptr;
167
2.38M
  register _JSAMPLE invalue;
168
2.38M
  register int h;
169
2.38M
  _JSAMPROW outend;
170
2.38M
  int h_expand, v_expand;
171
2.38M
  int inrow, outrow;
172
173
2.38M
  h_expand = upsample->h_expand[compptr->component_index];
174
2.38M
  v_expand = upsample->v_expand[compptr->component_index];
175
176
2.38M
  inrow = outrow = 0;
177
4.81M
  while (outrow < cinfo->max_v_samp_factor) {
178
    /* Generate one output row with proper horizontal expansion */
179
2.42M
    inptr = input_data[inrow];
180
2.42M
    outptr = output_data[outrow];
181
2.42M
    outend = outptr + cinfo->output_width;
182
122M
    while (outptr < outend) {
183
120M
      invalue = *inptr++;
184
276M
      for (h = h_expand; h > 0; h--) {
185
156M
        *outptr++ = invalue;
186
156M
      }
187
120M
    }
188
    /* Generate any additional output rows by duplicating the first one */
189
2.42M
    if (v_expand > 1) {
190
1.97M
      _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1,
191
1.97M
                         v_expand - 1, cinfo->output_width);
192
1.97M
    }
193
2.42M
    inrow++;
194
2.42M
    outrow += v_expand;
195
2.42M
  }
196
2.38M
}
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
204k
{
208
204k
  _JSAMPARRAY output_data = *output_data_ptr;
209
204k
  register _JSAMPROW inptr, outptr;
210
204k
  register _JSAMPLE invalue;
211
204k
  _JSAMPROW outend;
212
204k
  int inrow;
213
214
415k
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
215
211k
    inptr = input_data[inrow];
216
211k
    outptr = output_data[inrow];
217
211k
    outend = outptr + cinfo->output_width;
218
479k
    while (outptr < outend) {
219
268k
      invalue = *inptr++;
220
268k
      *outptr++ = invalue;
221
268k
      *outptr++ = invalue;
222
268k
    }
223
211k
  }
224
204k
}
jdsample-8.c:h2v1_upsample
Line
Count
Source
207
204k
{
208
204k
  _JSAMPARRAY output_data = *output_data_ptr;
209
204k
  register _JSAMPROW inptr, outptr;
210
204k
  register _JSAMPLE invalue;
211
204k
  _JSAMPROW outend;
212
204k
  int inrow;
213
214
415k
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
215
211k
    inptr = input_data[inrow];
216
211k
    outptr = output_data[inrow];
217
211k
    outend = outptr + cinfo->output_width;
218
479k
    while (outptr < outend) {
219
268k
      invalue = *inptr++;
220
268k
      *outptr++ = invalue;
221
268k
      *outptr++ = invalue;
222
268k
    }
223
211k
  }
224
204k
}
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
597k
{
236
597k
  _JSAMPARRAY output_data = *output_data_ptr;
237
597k
  register _JSAMPROW inptr, outptr;
238
597k
  register _JSAMPLE invalue;
239
597k
  _JSAMPROW outend;
240
597k
  int inrow, outrow;
241
242
597k
  inrow = outrow = 0;
243
1.19M
  while (outrow < cinfo->max_v_samp_factor) {
244
597k
    inptr = input_data[inrow];
245
597k
    outptr = output_data[outrow];
246
597k
    outend = outptr + cinfo->output_width;
247
1.60M
    while (outptr < outend) {
248
1.01M
      invalue = *inptr++;
249
1.01M
      *outptr++ = invalue;
250
1.01M
      *outptr++ = invalue;
251
1.01M
    }
252
597k
    _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1, 1,
253
597k
                       cinfo->output_width);
254
597k
    inrow++;
255
597k
    outrow += 2;
256
597k
  }
257
597k
}
jdsample-8.c:h2v2_upsample
Line
Count
Source
235
597k
{
236
597k
  _JSAMPARRAY output_data = *output_data_ptr;
237
597k
  register _JSAMPROW inptr, outptr;
238
597k
  register _JSAMPLE invalue;
239
597k
  _JSAMPROW outend;
240
597k
  int inrow, outrow;
241
242
597k
  inrow = outrow = 0;
243
1.19M
  while (outrow < cinfo->max_v_samp_factor) {
244
597k
    inptr = input_data[inrow];
245
597k
    outptr = output_data[outrow];
246
597k
    outend = outptr + cinfo->output_width;
247
1.60M
    while (outptr < outend) {
248
1.01M
      invalue = *inptr++;
249
1.01M
      *outptr++ = invalue;
250
1.01M
      *outptr++ = invalue;
251
1.01M
    }
252
597k
    _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1, 1,
253
597k
                       cinfo->output_width);
254
597k
    inrow++;
255
597k
    outrow += 2;
256
597k
  }
257
597k
}
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
302k
{
279
302k
  _JSAMPARRAY output_data = *output_data_ptr;
280
302k
  register _JSAMPROW inptr, outptr;
281
302k
  register int invalue;
282
302k
  register JDIMENSION colctr;
283
302k
  int inrow;
284
285
624k
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
286
321k
    inptr = input_data[inrow];
287
321k
    outptr = output_data[inrow];
288
    /* Special case for first column */
289
321k
    invalue = *inptr++;
290
321k
    *outptr++ = (_JSAMPLE)invalue;
291
321k
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[0] + 2) >> 2);
292
293
7.06M
    for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
294
      /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
295
6.74M
      invalue = (*inptr++) * 3;
296
6.74M
      *outptr++ = (_JSAMPLE)((invalue + inptr[-2] + 1) >> 2);
297
6.74M
      *outptr++ = (_JSAMPLE)((invalue + inptr[0] + 2) >> 2);
298
6.74M
    }
299
300
    /* Special case for last column */
301
321k
    invalue = *inptr;
302
321k
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[-1] + 1) >> 2);
303
321k
    *outptr++ = (_JSAMPLE)invalue;
304
321k
  }
305
302k
}
jdsample-8.c:h2v1_fancy_upsample
Line
Count
Source
278
302k
{
279
302k
  _JSAMPARRAY output_data = *output_data_ptr;
280
302k
  register _JSAMPROW inptr, outptr;
281
302k
  register int invalue;
282
302k
  register JDIMENSION colctr;
283
302k
  int inrow;
284
285
624k
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
286
321k
    inptr = input_data[inrow];
287
321k
    outptr = output_data[inrow];
288
    /* Special case for first column */
289
321k
    invalue = *inptr++;
290
321k
    *outptr++ = (_JSAMPLE)invalue;
291
321k
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[0] + 2) >> 2);
292
293
7.06M
    for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
294
      /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
295
6.74M
      invalue = (*inptr++) * 3;
296
6.74M
      *outptr++ = (_JSAMPLE)((invalue + inptr[-2] + 1) >> 2);
297
6.74M
      *outptr++ = (_JSAMPLE)((invalue + inptr[0] + 2) >> 2);
298
6.74M
    }
299
300
    /* Special case for last column */
301
321k
    invalue = *inptr;
302
321k
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[-1] + 1) >> 2);
303
321k
    *outptr++ = (_JSAMPLE)invalue;
304
321k
  }
305
302k
}
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
447k
{
319
447k
  _JSAMPARRAY output_data = *output_data_ptr;
320
447k
  _JSAMPROW inptr0, inptr1, outptr;
321
#if BITS_IN_JSAMPLE == 8
322
  int thiscolsum, bias;
323
#else
324
  JLONG thiscolsum, bias;
325
#endif
326
447k
  JDIMENSION colctr;
327
447k
  int inrow, outrow, v;
328
329
447k
  inrow = outrow = 0;
330
949k
  while (outrow < cinfo->max_v_samp_factor) {
331
1.50M
    for (v = 0; v < 2; v++) {
332
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
333
1.00M
      inptr0 = input_data[inrow];
334
1.00M
      if (v == 0) {             /* next nearest is row above */
335
502k
        inptr1 = input_data[inrow - 1];
336
502k
        bias = 1;
337
502k
      } else {                  /* next nearest is row below */
338
502k
        inptr1 = input_data[inrow + 1];
339
502k
        bias = 2;
340
502k
      }
341
1.00M
      outptr = output_data[outrow++];
342
343
337M
      for (colctr = 0; colctr < compptr->downsampled_width; colctr++) {
344
336M
        thiscolsum = (*inptr0++) * 3 + (*inptr1++);
345
336M
        *outptr++ = (_JSAMPLE)((thiscolsum + bias) >> 2);
346
336M
      }
347
1.00M
    }
348
502k
    inrow++;
349
502k
  }
350
447k
}
jdsample-8.c:h1v2_fancy_upsample
Line
Count
Source
318
447k
{
319
447k
  _JSAMPARRAY output_data = *output_data_ptr;
320
447k
  _JSAMPROW inptr0, inptr1, outptr;
321
447k
#if BITS_IN_JSAMPLE == 8
322
447k
  int thiscolsum, bias;
323
#else
324
  JLONG thiscolsum, bias;
325
#endif
326
447k
  JDIMENSION colctr;
327
447k
  int inrow, outrow, v;
328
329
447k
  inrow = outrow = 0;
330
949k
  while (outrow < cinfo->max_v_samp_factor) {
331
1.50M
    for (v = 0; v < 2; v++) {
332
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
333
1.00M
      inptr0 = input_data[inrow];
334
1.00M
      if (v == 0) {             /* next nearest is row above */
335
502k
        inptr1 = input_data[inrow - 1];
336
502k
        bias = 1;
337
502k
      } else {                  /* next nearest is row below */
338
502k
        inptr1 = input_data[inrow + 1];
339
502k
        bias = 2;
340
502k
      }
341
1.00M
      outptr = output_data[outrow++];
342
343
337M
      for (colctr = 0; colctr < compptr->downsampled_width; colctr++) {
344
336M
        thiscolsum = (*inptr0++) * 3 + (*inptr1++);
345
336M
        *outptr++ = (_JSAMPLE)((thiscolsum + bias) >> 2);
346
336M
      }
347
1.00M
    }
348
502k
    inrow++;
349
502k
  }
350
447k
}
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
210k
{
365
210k
  _JSAMPARRAY output_data = *output_data_ptr;
366
210k
  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
210k
  register JDIMENSION colctr;
373
210k
  int inrow, outrow, v;
374
375
210k
  inrow = outrow = 0;
376
420k
  while (outrow < cinfo->max_v_samp_factor) {
377
630k
    for (v = 0; v < 2; v++) {
378
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
379
420k
      inptr0 = input_data[inrow];
380
420k
      if (v == 0)               /* next nearest is row above */
381
210k
        inptr1 = input_data[inrow - 1];
382
210k
      else                      /* next nearest is row below */
383
210k
        inptr1 = input_data[inrow + 1];
384
420k
      outptr = output_data[outrow++];
385
386
      /* Special case for first column */
387
420k
      thiscolsum = (*inptr0++) * 3 + (*inptr1++);
388
420k
      nextcolsum = (*inptr0++) * 3 + (*inptr1++);
389
420k
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 8) >> 4);
390
420k
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
391
420k
      lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
392
393
85.4M
      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
85.0M
        nextcolsum = (*inptr0++) * 3 + (*inptr1++);
397
85.0M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
398
85.0M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
399
85.0M
        lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
400
85.0M
      }
401
402
      /* Special case for last column */
403
420k
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
404
420k
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 7) >> 4);
405
420k
    }
406
210k
    inrow++;
407
210k
  }
408
210k
}
jdsample-8.c:h2v2_fancy_upsample
Line
Count
Source
364
210k
{
365
210k
  _JSAMPARRAY output_data = *output_data_ptr;
366
210k
  register _JSAMPROW inptr0, inptr1, outptr;
367
210k
#if BITS_IN_JSAMPLE == 8
368
210k
  register int thiscolsum, lastcolsum, nextcolsum;
369
#else
370
  register JLONG thiscolsum, lastcolsum, nextcolsum;
371
#endif
372
210k
  register JDIMENSION colctr;
373
210k
  int inrow, outrow, v;
374
375
210k
  inrow = outrow = 0;
376
420k
  while (outrow < cinfo->max_v_samp_factor) {
377
630k
    for (v = 0; v < 2; v++) {
378
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
379
420k
      inptr0 = input_data[inrow];
380
420k
      if (v == 0)               /* next nearest is row above */
381
210k
        inptr1 = input_data[inrow - 1];
382
210k
      else                      /* next nearest is row below */
383
210k
        inptr1 = input_data[inrow + 1];
384
420k
      outptr = output_data[outrow++];
385
386
      /* Special case for first column */
387
420k
      thiscolsum = (*inptr0++) * 3 + (*inptr1++);
388
420k
      nextcolsum = (*inptr0++) * 3 + (*inptr1++);
389
420k
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 8) >> 4);
390
420k
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
391
420k
      lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
392
393
85.4M
      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
85.0M
        nextcolsum = (*inptr0++) * 3 + (*inptr1++);
397
85.0M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
398
85.0M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
399
85.0M
        lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
400
85.0M
      }
401
402
      /* Special case for last column */
403
420k
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
404
420k
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 7) >> 4);
405
420k
    }
406
210k
    inrow++;
407
210k
  }
408
210k
}
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
7.12k
{
418
7.12k
  my_upsample_ptr upsample;
419
7.12k
  int ci;
420
7.12k
  jpeg_component_info *compptr;
421
7.12k
  boolean need_buffer, do_fancy;
422
7.12k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
7.12k
#ifdef D_LOSSLESS_SUPPORTED
425
7.12k
  if (cinfo->master->lossless) {
426
#if BITS_IN_JSAMPLE == 8
427
539
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
428
#else
429
861
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
430
861
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
431
0
#endif
432
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
433
1.40k
  } else
434
5.72k
#endif
435
5.72k
  {
436
5.72k
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
437
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
438
5.72k
  }
439
440
7.12k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
441
7.12k
    upsample = (my_upsample_ptr)
442
7.12k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
443
7.12k
                                  sizeof(my_upsampler));
444
7.12k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
445
7.12k
    upsample->pub.start_pass = start_pass_upsample;
446
7.12k
    upsample->pub._upsample = sep_upsample;
447
7.12k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
448
7.12k
  } else
449
0
    upsample = (my_upsample_ptr)cinfo->upsample;
450
451
7.12k
  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
7.12k
  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
26.9k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
463
19.8k
       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
19.8k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
468
19.8k
                 cinfo->_min_DCT_scaled_size;
469
19.8k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
470
19.8k
                 cinfo->_min_DCT_scaled_size;
471
19.8k
    h_out_group = cinfo->max_h_samp_factor;
472
19.8k
    v_out_group = cinfo->max_v_samp_factor;
473
19.8k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
474
19.8k
    need_buffer = TRUE;
475
19.8k
    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
19.8k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
480
      /* Fullsize components can be processed without any work. */
481
12.4k
      upsample->methods[ci] = fullsize_upsample;
482
12.4k
      need_buffer = FALSE;
483
12.4k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
484
      /* Special cases for 2h1v upsampling */
485
590
      if (do_fancy && compptr->downsampled_width > 2) {
486
#ifdef WITH_SIMD
487
160
        if (jsimd_can_h2v1_fancy_upsample())
488
0
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
489
160
        else
490
160
#endif
491
160
          upsample->methods[ci] = h2v1_fancy_upsample;
492
408
      } else {
493
#ifdef WITH_SIMD
494
171
        if (jsimd_can_h2v1_upsample())
495
0
          upsample->methods[ci] = jsimd_h2v1_upsample;
496
171
        else
497
171
#endif
498
171
          upsample->methods[ci] = h2v1_upsample;
499
408
      }
500
6.79k
    } else if (h_in_group == h_out_group &&
501
6.79k
               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
723
        upsample->methods[ci] = h1v2_fancy_upsample;
511
723
      upsample->pub.need_context_rows = TRUE;
512
6.07k
    } else if (h_in_group * 2 == h_out_group &&
513
6.07k
               v_in_group * 2 == v_out_group) {
514
      /* Special cases for 2h2v upsampling */
515
2.72k
      if (do_fancy && compptr->downsampled_width > 2) {
516
#ifdef WITH_SIMD
517
2.29k
        if (jsimd_can_h2v2_fancy_upsample())
518
0
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
519
2.29k
        else
520
2.29k
#endif
521
2.29k
          upsample->methods[ci] = h2v2_fancy_upsample;
522
2.41k
        upsample->pub.need_context_rows = TRUE;
523
2.41k
      } else {
524
#ifdef WITH_SIMD
525
135
        if (jsimd_can_h2v2_upsample())
526
0
          upsample->methods[ci] = jsimd_h2v2_upsample;
527
135
        else
528
135
#endif
529
135
          upsample->methods[ci] = h2v2_upsample;
530
306
      }
531
3.35k
    } else if ((h_out_group % h_in_group) == 0 &&
532
3.35k
               (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
3.31k
        upsample->methods[ci] = int_upsample;
540
3.31k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
541
3.31k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
542
3.31k
    } else
543
35
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
544
19.8k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
545
7.34k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
546
7.34k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
547
7.34k
         (JDIMENSION)jround_up((long)cinfo->output_width,
548
7.34k
                               (long)cinfo->max_h_samp_factor),
549
7.34k
         (JDIMENSION)cinfo->max_v_samp_factor);
550
7.34k
    }
551
19.8k
  }
552
7.12k
}
jinit_upsampler
Line
Count
Source
417
5.85k
{
418
5.85k
  my_upsample_ptr upsample;
419
5.85k
  int ci;
420
5.85k
  jpeg_component_info *compptr;
421
5.85k
  boolean need_buffer, do_fancy;
422
5.85k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
5.85k
#ifdef D_LOSSLESS_SUPPORTED
425
5.85k
  if (cinfo->master->lossless) {
426
539
#if BITS_IN_JSAMPLE == 8
427
539
    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
539
  } else
434
5.31k
#endif
435
5.31k
  {
436
5.31k
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
437
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
438
5.31k
  }
439
440
5.85k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
441
5.85k
    upsample = (my_upsample_ptr)
442
5.85k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
443
5.85k
                                  sizeof(my_upsampler));
444
5.85k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
445
5.85k
    upsample->pub.start_pass = start_pass_upsample;
446
5.85k
    upsample->pub._upsample = sep_upsample;
447
5.85k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
448
5.85k
  } else
449
0
    upsample = (my_upsample_ptr)cinfo->upsample;
450
451
5.85k
  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.85k
  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
21.9k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
463
16.0k
       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
16.0k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
468
16.0k
                 cinfo->_min_DCT_scaled_size;
469
16.0k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
470
16.0k
                 cinfo->_min_DCT_scaled_size;
471
16.0k
    h_out_group = cinfo->max_h_samp_factor;
472
16.0k
    v_out_group = cinfo->max_v_samp_factor;
473
16.0k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
474
16.0k
    need_buffer = TRUE;
475
16.0k
    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
16.0k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
480
      /* Fullsize components can be processed without any work. */
481
11.1k
      upsample->methods[ci] = fullsize_upsample;
482
11.1k
      need_buffer = FALSE;
483
11.1k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
484
      /* Special cases for 2h1v upsampling */
485
331
      if (do_fancy && compptr->downsampled_width > 2) {
486
160
#ifdef WITH_SIMD
487
160
        if (jsimd_can_h2v1_fancy_upsample())
488
0
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
489
160
        else
490
160
#endif
491
160
          upsample->methods[ci] = h2v1_fancy_upsample;
492
171
      } else {
493
171
#ifdef WITH_SIMD
494
171
        if (jsimd_can_h2v1_upsample())
495
0
          upsample->methods[ci] = jsimd_h2v1_upsample;
496
171
        else
497
171
#endif
498
171
          upsample->methods[ci] = h2v1_upsample;
499
171
      }
500
4.53k
    } else if (h_in_group == h_out_group &&
501
4.53k
               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
443
        upsample->methods[ci] = h1v2_fancy_upsample;
511
443
      upsample->pub.need_context_rows = TRUE;
512
4.08k
    } else if (h_in_group * 2 == h_out_group &&
513
4.08k
               v_in_group * 2 == v_out_group) {
514
      /* Special cases for 2h2v upsampling */
515
2.42k
      if (do_fancy && compptr->downsampled_width > 2) {
516
2.29k
#ifdef WITH_SIMD
517
2.29k
        if (jsimd_can_h2v2_fancy_upsample())
518
0
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
519
2.29k
        else
520
2.29k
#endif
521
2.29k
          upsample->methods[ci] = h2v2_fancy_upsample;
522
2.29k
        upsample->pub.need_context_rows = TRUE;
523
2.29k
      } else {
524
135
#ifdef WITH_SIMD
525
135
        if (jsimd_can_h2v2_upsample())
526
0
          upsample->methods[ci] = jsimd_h2v2_upsample;
527
135
        else
528
135
#endif
529
135
          upsample->methods[ci] = h2v2_upsample;
530
135
      }
531
2.42k
    } else if ((h_out_group % h_in_group) == 0 &&
532
1.66k
               (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
1.65k
        upsample->methods[ci] = int_upsample;
540
1.65k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
541
1.65k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
542
1.65k
    } else
543
8
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
544
16.0k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
545
4.85k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
546
4.85k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
547
4.85k
         (JDIMENSION)jround_up((long)cinfo->output_width,
548
4.85k
                               (long)cinfo->max_h_samp_factor),
549
4.85k
         (JDIMENSION)cinfo->max_v_samp_factor);
550
4.85k
    }
551
16.0k
  }
552
5.85k
}
j12init_upsampler
Line
Count
Source
417
781
{
418
781
  my_upsample_ptr upsample;
419
781
  int ci;
420
781
  jpeg_component_info *compptr;
421
781
  boolean need_buffer, do_fancy;
422
781
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
781
#ifdef D_LOSSLESS_SUPPORTED
425
781
  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
376
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
430
376
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
431
0
#endif
432
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
433
376
  } else
434
405
#endif
435
405
  {
436
405
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
437
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
438
405
  }
439
440
781
  if (!cinfo->master->jinit_upsampler_no_alloc) {
441
781
    upsample = (my_upsample_ptr)
442
781
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
443
781
                                  sizeof(my_upsampler));
444
781
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
445
781
    upsample->pub.start_pass = start_pass_upsample;
446
781
    upsample->pub._upsample = sep_upsample;
447
781
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
448
781
  } else
449
0
    upsample = (my_upsample_ptr)cinfo->upsample;
450
451
781
  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
781
  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
3.10k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
463
2.32k
       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
2.32k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
468
2.32k
                 cinfo->_min_DCT_scaled_size;
469
2.32k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
470
2.32k
                 cinfo->_min_DCT_scaled_size;
471
2.32k
    h_out_group = cinfo->max_h_samp_factor;
472
2.32k
    v_out_group = cinfo->max_v_samp_factor;
473
2.32k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
474
2.32k
    need_buffer = TRUE;
475
2.32k
    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
2.32k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
480
      /* Fullsize components can be processed without any work. */
481
847
      upsample->methods[ci] = fullsize_upsample;
482
847
      need_buffer = FALSE;
483
1.47k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
484
      /* Special cases for 2h1v upsampling */
485
132
      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
22
          upsample->methods[ci] = h2v1_fancy_upsample;
492
110
      } else {
493
#ifdef WITH_SIMD
494
        if (jsimd_can_h2v1_upsample())
495
          upsample->methods[ci] = jsimd_h2v1_upsample;
496
        else
497
#endif
498
110
          upsample->methods[ci] = h2v1_upsample;
499
110
      }
500
1.34k
    } else if (h_in_group == h_out_group &&
501
1.34k
               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
280
        upsample->methods[ci] = h1v2_fancy_upsample;
511
280
      upsample->pub.need_context_rows = TRUE;
512
1.06k
    } else if (h_in_group * 2 == h_out_group &&
513
1.06k
               v_in_group * 2 == v_out_group) {
514
      /* Special cases for 2h2v upsampling */
515
212
      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
125
          upsample->methods[ci] = h2v2_fancy_upsample;
522
125
        upsample->pub.need_context_rows = TRUE;
523
125
      } else {
524
#ifdef WITH_SIMD
525
        if (jsimd_can_h2v2_upsample())
526
          upsample->methods[ci] = jsimd_h2v2_upsample;
527
        else
528
#endif
529
87
          upsample->methods[ci] = h2v2_upsample;
530
87
      }
531
852
    } else if ((h_out_group % h_in_group) == 0 &&
532
852
               (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
836
        upsample->methods[ci] = int_upsample;
540
836
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
541
836
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
542
836
    } else
543
16
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
544
2.32k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
545
1.46k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
546
1.46k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
547
1.46k
         (JDIMENSION)jround_up((long)cinfo->output_width,
548
1.46k
                               (long)cinfo->max_h_samp_factor),
549
1.46k
         (JDIMENSION)cinfo->max_v_samp_factor);
550
1.46k
    }
551
2.32k
  }
552
781
}
j16init_upsampler
Line
Count
Source
417
485
{
418
485
  my_upsample_ptr upsample;
419
485
  int ci;
420
485
  jpeg_component_info *compptr;
421
485
  boolean need_buffer, do_fancy;
422
485
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
485
#ifdef D_LOSSLESS_SUPPORTED
425
485
  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
485
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
430
485
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
431
0
#endif
432
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
433
485
  } 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
485
  if (!cinfo->master->jinit_upsampler_no_alloc) {
441
485
    upsample = (my_upsample_ptr)
442
485
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
443
485
                                  sizeof(my_upsampler));
444
485
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
445
485
    upsample->pub.start_pass = start_pass_upsample;
446
485
    upsample->pub._upsample = sep_upsample;
447
485
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
448
485
  } else
449
0
    upsample = (my_upsample_ptr)cinfo->upsample;
450
451
485
  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
485
  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
1.92k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
463
1.44k
       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
1.44k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
468
1.44k
                 cinfo->_min_DCT_scaled_size;
469
1.44k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
470
1.44k
                 cinfo->_min_DCT_scaled_size;
471
1.44k
    h_out_group = cinfo->max_h_samp_factor;
472
1.44k
    v_out_group = cinfo->max_v_samp_factor;
473
1.44k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
474
1.44k
    need_buffer = TRUE;
475
1.44k
    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
1.44k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
480
      /* Fullsize components can be processed without any work. */
481
395
      upsample->methods[ci] = fullsize_upsample;
482
395
      need_buffer = FALSE;
483
1.04k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
484
      /* Special cases for 2h1v upsampling */
485
127
      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
127
      } else {
493
#ifdef WITH_SIMD
494
        if (jsimd_can_h2v1_upsample())
495
          upsample->methods[ci] = jsimd_h2v1_upsample;
496
        else
497
#endif
498
127
          upsample->methods[ci] = h2v1_upsample;
499
127
      }
500
918
    } else if (h_in_group == h_out_group &&
501
918
               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
918
    } else if (h_in_group * 2 == h_out_group &&
513
918
               v_in_group * 2 == v_out_group) {
514
      /* Special cases for 2h2v upsampling */
515
84
      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
84
      } else {
524
#ifdef WITH_SIMD
525
        if (jsimd_can_h2v2_upsample())
526
          upsample->methods[ci] = jsimd_h2v2_upsample;
527
        else
528
#endif
529
84
          upsample->methods[ci] = h2v2_upsample;
530
84
      }
531
834
    } else if ((h_out_group % h_in_group) == 0 &&
532
834
               (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
823
        upsample->methods[ci] = int_upsample;
540
823
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
541
823
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
542
823
    } else
543
11
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
544
1.44k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
545
1.03k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
546
1.03k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
547
1.03k
         (JDIMENSION)jround_up((long)cinfo->output_width,
548
1.03k
                               (long)cinfo->max_h_samp_factor),
549
1.03k
         (JDIMENSION)cinfo->max_v_samp_factor);
550
1.03k
    }
551
1.44k
  }
552
485
}
553
554
#endif /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */