Coverage Report

Created: 2026-03-12 06:59

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-2026, 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 is defined to
18
 * be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size) sample rows of
19
 * each component.  Upsampling will normally produce max_v_samp_factor rows of
20
 * each component from each row group (but this could vary if the upsampler is
21
 * applying a scale factor of its own).
22
 *
23
 * An excellent reference for image resampling is
24
 *   Digital Image Warping, George Wolberg, 1990.
25
 *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
26
 */
27
28
#include "jinclude.h"
29
#include "jdsample.h"
30
#include "jsimd.h"
31
#include "jpegapicomp.h"
32
33
34
35
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
36
37
/*
38
 * Initialize for an upsampling pass.
39
 */
40
41
METHODDEF(void)
42
start_pass_upsample(j_decompress_ptr cinfo)
43
2.42k
{
44
2.42k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
2.42k
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
2.42k
  upsample->rows_to_go = cinfo->output_height;
50
2.42k
}
jdsample-8.c:start_pass_upsample
Line
Count
Source
43
2.32k
{
44
2.32k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
2.32k
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
2.32k
  upsample->rows_to_go = cinfo->output_height;
50
2.32k
}
jdsample-12.c:start_pass_upsample
Line
Count
Source
43
93
{
44
93
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
93
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
93
  upsample->rows_to_go = cinfo->output_height;
50
93
}
jdsample-16.c:start_pass_upsample
Line
Count
Source
43
5
{
44
5
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
5
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
5
  upsample->rows_to_go = cinfo->output_height;
50
5
}
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
12.6M
{
67
12.6M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
68
12.6M
  int ci;
69
12.6M
  jpeg_component_info *compptr;
70
12.6M
  JDIMENSION num_rows;
71
72
  /* Fill the conversion buffer, if it's empty */
73
12.6M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
74
36.6M
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
75
25.3M
         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
25.3M
      (*upsample->methods[ci]) (cinfo, compptr,
80
25.3M
        input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
81
25.3M
        upsample->color_buf + ci);
82
25.3M
    }
83
11.2M
    upsample->next_row_out = 0;
84
11.2M
  }
85
86
  /* Color-convert and emit rows */
87
88
  /* How many we have in the buffer: */
89
12.6M
  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
12.6M
  if (num_rows > upsample->rows_to_go)
94
523
    num_rows = upsample->rows_to_go;
95
  /* And not more than what the client can accept: */
96
12.6M
  out_rows_avail -= *out_row_ctr;
97
12.6M
  if (num_rows > out_rows_avail)
98
1.39M
    num_rows = out_rows_avail;
99
100
12.6M
  (*cinfo->cconvert->_color_convert) (cinfo, upsample->color_buf,
101
12.6M
                                      (JDIMENSION)upsample->next_row_out,
102
12.6M
                                      output_buf + *out_row_ctr,
103
12.6M
                                      (int)num_rows);
104
105
  /* Adjust counts */
106
12.6M
  *out_row_ctr += num_rows;
107
12.6M
  upsample->rows_to_go -= num_rows;
108
12.6M
  upsample->next_row_out += num_rows;
109
  /* When the buffer is emptied, declare this input row group consumed */
110
12.6M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
111
11.2M
    (*in_row_group_ctr)++;
112
12.6M
}
jdsample-8.c:sep_upsample
Line
Count
Source
66
12.6M
{
67
12.6M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
68
12.6M
  int ci;
69
12.6M
  jpeg_component_info *compptr;
70
12.6M
  JDIMENSION num_rows;
71
72
  /* Fill the conversion buffer, if it's empty */
73
12.6M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
74
36.6M
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
75
25.3M
         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
25.3M
      (*upsample->methods[ci]) (cinfo, compptr,
80
25.3M
        input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
81
25.3M
        upsample->color_buf + ci);
82
25.3M
    }
83
11.2M
    upsample->next_row_out = 0;
84
11.2M
  }
85
86
  /* Color-convert and emit rows */
87
88
  /* How many we have in the buffer: */
89
12.6M
  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
12.6M
  if (num_rows > upsample->rows_to_go)
94
523
    num_rows = upsample->rows_to_go;
95
  /* And not more than what the client can accept: */
96
12.6M
  out_rows_avail -= *out_row_ctr;
97
12.6M
  if (num_rows > out_rows_avail)
98
1.39M
    num_rows = out_rows_avail;
99
100
12.6M
  (*cinfo->cconvert->_color_convert) (cinfo, upsample->color_buf,
101
12.6M
                                      (JDIMENSION)upsample->next_row_out,
102
12.6M
                                      output_buf + *out_row_ctr,
103
12.6M
                                      (int)num_rows);
104
105
  /* Adjust counts */
106
12.6M
  *out_row_ctr += num_rows;
107
12.6M
  upsample->rows_to_go -= num_rows;
108
12.6M
  upsample->next_row_out += num_rows;
109
  /* When the buffer is emptied, declare this input row group consumed */
110
12.6M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
111
11.2M
    (*in_row_group_ctr)++;
112
12.6M
}
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 values of a
117
 * single component.  One row group is processed per call.
118
 */
119
120
121
/*
122
 * For full-size components, we just make color_buf[ci] point at the
123
 * input buffer, and thus avoid copying any data.  Note that this is
124
 * safe only because sep_upsample doesn't declare the input row group
125
 * "consumed" until we are done color converting and emitting it.
126
 */
127
128
METHODDEF(void)
129
fullsize_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
130
                  _JSAMPARRAY input_data, _JSAMPARRAY *output_data_ptr)
131
11.1M
{
132
11.1M
  *output_data_ptr = input_data;
133
11.1M
}
jdsample-8.c:fullsize_upsample
Line
Count
Source
131
11.1M
{
132
11.1M
  *output_data_ptr = input_data;
133
11.1M
}
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.  Nor, for
152
 * that matter, is it particularly accurate: the algorithm is simple
153
 * replication of the input sample onto the corresponding output components.
154
 * The hi-falutin sampling literature refers to this as a "box filter".  A box
155
 * filter tends to introduce visible artifacts, so if you are actually going to
156
 * use 3:1 or 4:1 sampling ratios you would be well advised to improve this
157
 * 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
708k
{
164
708k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
165
708k
  _JSAMPARRAY output_data = *output_data_ptr;
166
708k
  register _JSAMPROW inptr, outptr;
167
708k
  register _JSAMPLE invalue;
168
708k
  register int h;
169
708k
  _JSAMPROW outend;
170
708k
  int h_expand, v_expand;
171
708k
  int inrow, outrow;
172
173
708k
  h_expand = upsample->h_expand[compptr->component_index];
174
708k
  v_expand = upsample->v_expand[compptr->component_index];
175
176
708k
  inrow = outrow = 0;
177
1.57M
  while (outrow < cinfo->max_v_samp_factor) {
178
    /* Generate one output row with proper horizontal expansion */
179
870k
    inptr = input_data[inrow];
180
870k
    outptr = output_data[outrow];
181
870k
    outend = outptr + cinfo->output_width;
182
14.0M
    while (outptr < outend) {
183
13.2M
      invalue = *inptr++;
184
47.8M
      for (h = h_expand; h > 0; h--) {
185
34.6M
        *outptr++ = invalue;
186
34.6M
      }
187
13.2M
    }
188
    /* Generate any additional output rows by duplicating the first one */
189
870k
    if (v_expand > 1) {
190
397k
      _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1,
191
397k
                         v_expand - 1, cinfo->output_width);
192
397k
    }
193
870k
    inrow++;
194
870k
    outrow += v_expand;
195
870k
  }
196
708k
}
jdsample-8.c:int_upsample
Line
Count
Source
163
708k
{
164
708k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
165
708k
  _JSAMPARRAY output_data = *output_data_ptr;
166
708k
  register _JSAMPROW inptr, outptr;
167
708k
  register _JSAMPLE invalue;
168
708k
  register int h;
169
708k
  _JSAMPROW outend;
170
708k
  int h_expand, v_expand;
171
708k
  int inrow, outrow;
172
173
708k
  h_expand = upsample->h_expand[compptr->component_index];
174
708k
  v_expand = upsample->v_expand[compptr->component_index];
175
176
708k
  inrow = outrow = 0;
177
1.57M
  while (outrow < cinfo->max_v_samp_factor) {
178
    /* Generate one output row with proper horizontal expansion */
179
870k
    inptr = input_data[inrow];
180
870k
    outptr = output_data[outrow];
181
870k
    outend = outptr + cinfo->output_width;
182
14.0M
    while (outptr < outend) {
183
13.2M
      invalue = *inptr++;
184
47.8M
      for (h = h_expand; h > 0; h--) {
185
34.6M
        *outptr++ = invalue;
186
34.6M
      }
187
13.2M
    }
188
    /* Generate any additional output rows by duplicating the first one */
189
870k
    if (v_expand > 1) {
190
397k
      _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1,
191
397k
                         v_expand - 1, cinfo->output_width);
192
397k
    }
193
870k
    inrow++;
194
870k
    outrow += v_expand;
195
870k
  }
196
708k
}
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
8.07k
{
208
8.07k
  _JSAMPARRAY output_data = *output_data_ptr;
209
8.07k
  register _JSAMPROW inptr, outptr;
210
8.07k
  register _JSAMPLE invalue;
211
8.07k
  _JSAMPROW outend;
212
8.07k
  int inrow;
213
214
24.8k
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
215
16.7k
    inptr = input_data[inrow];
216
16.7k
    outptr = output_data[inrow];
217
16.7k
    outend = outptr + cinfo->output_width;
218
88.1k
    while (outptr < outend) {
219
71.4k
      invalue = *inptr++;
220
71.4k
      *outptr++ = invalue;
221
71.4k
      *outptr++ = invalue;
222
71.4k
    }
223
16.7k
  }
224
8.07k
}
jdsample-8.c:h2v1_upsample
Line
Count
Source
207
8.07k
{
208
8.07k
  _JSAMPARRAY output_data = *output_data_ptr;
209
8.07k
  register _JSAMPROW inptr, outptr;
210
8.07k
  register _JSAMPLE invalue;
211
8.07k
  _JSAMPROW outend;
212
8.07k
  int inrow;
213
214
24.8k
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
215
16.7k
    inptr = input_data[inrow];
216
16.7k
    outptr = output_data[inrow];
217
16.7k
    outend = outptr + cinfo->output_width;
218
88.1k
    while (outptr < outend) {
219
71.4k
      invalue = *inptr++;
220
71.4k
      *outptr++ = invalue;
221
71.4k
      *outptr++ = invalue;
222
71.4k
    }
223
16.7k
  }
224
8.07k
}
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
11.8k
{
236
11.8k
  _JSAMPARRAY output_data = *output_data_ptr;
237
11.8k
  register _JSAMPROW inptr, outptr;
238
11.8k
  register _JSAMPLE invalue;
239
11.8k
  _JSAMPROW outend;
240
11.8k
  int inrow, outrow;
241
242
11.8k
  inrow = outrow = 0;
243
23.8k
  while (outrow < cinfo->max_v_samp_factor) {
244
12.0k
    inptr = input_data[inrow];
245
12.0k
    outptr = output_data[outrow];
246
12.0k
    outend = outptr + cinfo->output_width;
247
56.8k
    while (outptr < outend) {
248
44.8k
      invalue = *inptr++;
249
44.8k
      *outptr++ = invalue;
250
44.8k
      *outptr++ = invalue;
251
44.8k
    }
252
12.0k
    _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1, 1,
253
12.0k
                       cinfo->output_width);
254
12.0k
    inrow++;
255
12.0k
    outrow += 2;
256
12.0k
  }
257
11.8k
}
jdsample-8.c:h2v2_upsample
Line
Count
Source
235
11.8k
{
236
11.8k
  _JSAMPARRAY output_data = *output_data_ptr;
237
11.8k
  register _JSAMPROW inptr, outptr;
238
11.8k
  register _JSAMPLE invalue;
239
11.8k
  _JSAMPROW outend;
240
11.8k
  int inrow, outrow;
241
242
11.8k
  inrow = outrow = 0;
243
23.8k
  while (outrow < cinfo->max_v_samp_factor) {
244
12.0k
    inptr = input_data[inrow];
245
12.0k
    outptr = output_data[outrow];
246
12.0k
    outend = outptr + cinfo->output_width;
247
56.8k
    while (outptr < outend) {
248
44.8k
      invalue = *inptr++;
249
44.8k
      *outptr++ = invalue;
250
44.8k
      *outptr++ = invalue;
251
44.8k
    }
252
12.0k
    _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1, 1,
253
12.0k
                       cinfo->output_width);
254
12.0k
    inrow++;
255
12.0k
    outrow += 2;
256
12.0k
  }
257
11.8k
}
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 component centers,
264
 * also known as a "triangle filter".  This is a good compromise between speed
265
 * and visual quality.  The centers of the output components are 1/4 and 3/4 of
266
 * the way between input component 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
11.1M
{
279
11.1M
  _JSAMPARRAY output_data = *output_data_ptr;
280
11.1M
  register _JSAMPROW inptr, outptr;
281
11.1M
  register int invalue;
282
11.1M
  register JDIMENSION colctr;
283
11.1M
  int inrow;
284
285
22.4M
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
286
11.2M
    inptr = input_data[inrow];
287
11.2M
    outptr = output_data[inrow];
288
    /* Special case for first column */
289
11.2M
    invalue = *inptr++;
290
11.2M
    *outptr++ = (_JSAMPLE)invalue;
291
11.2M
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[0] + 2) >> 2);
292
293
108M
    for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
294
      /* General case: 3/4 * nearer component + 1/4 * further component */
295
97.6M
      invalue = (*inptr++) * 3;
296
97.6M
      *outptr++ = (_JSAMPLE)((invalue + inptr[-2] + 1) >> 2);
297
97.6M
      *outptr++ = (_JSAMPLE)((invalue + inptr[0] + 2) >> 2);
298
97.6M
    }
299
300
    /* Special case for last column */
301
11.2M
    invalue = *inptr;
302
11.2M
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[-1] + 1) >> 2);
303
11.2M
    *outptr++ = (_JSAMPLE)invalue;
304
11.2M
  }
305
11.1M
}
jdsample-8.c:h2v1_fancy_upsample
Line
Count
Source
278
11.1M
{
279
11.1M
  _JSAMPARRAY output_data = *output_data_ptr;
280
11.1M
  register _JSAMPROW inptr, outptr;
281
11.1M
  register int invalue;
282
11.1M
  register JDIMENSION colctr;
283
11.1M
  int inrow;
284
285
22.4M
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
286
11.2M
    inptr = input_data[inrow];
287
11.2M
    outptr = output_data[inrow];
288
    /* Special case for first column */
289
11.2M
    invalue = *inptr++;
290
11.2M
    *outptr++ = (_JSAMPLE)invalue;
291
11.2M
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[0] + 2) >> 2);
292
293
108M
    for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
294
      /* General case: 3/4 * nearer component + 1/4 * further component */
295
97.6M
      invalue = (*inptr++) * 3;
296
97.6M
      *outptr++ = (_JSAMPLE)((invalue + inptr[-2] + 1) >> 2);
297
97.6M
      *outptr++ = (_JSAMPLE)((invalue + inptr[0] + 2) >> 2);
298
97.6M
    }
299
300
    /* Special case for last column */
301
11.2M
    invalue = *inptr;
302
11.2M
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[-1] + 1) >> 2);
303
11.2M
    *outptr++ = (_JSAMPLE)invalue;
304
11.2M
  }
305
11.1M
}
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
150k
{
319
150k
  _JSAMPARRAY output_data = *output_data_ptr;
320
150k
  _JSAMPROW inptr0, inptr1, outptr;
321
#if BITS_IN_JSAMPLE == 8
322
  int thiscolsum, bias;
323
#else
324
  JLONG thiscolsum, bias;
325
#endif
326
150k
  JDIMENSION colctr;
327
150k
  int inrow, outrow, v;
328
329
150k
  inrow = outrow = 0;
330
332k
  while (outrow < cinfo->max_v_samp_factor) {
331
545k
    for (v = 0; v < 2; v++) {
332
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
333
363k
      inptr0 = input_data[inrow];
334
363k
      if (v == 0) {             /* next nearest is row above */
335
181k
        inptr1 = input_data[inrow - 1];
336
181k
        bias = 1;
337
181k
      } else {                  /* next nearest is row below */
338
181k
        inptr1 = input_data[inrow + 1];
339
181k
        bias = 2;
340
181k
      }
341
363k
      outptr = output_data[outrow++];
342
343
45.9M
      for (colctr = 0; colctr < compptr->downsampled_width; colctr++) {
344
45.5M
        thiscolsum = (*inptr0++) * 3 + (*inptr1++);
345
45.5M
        *outptr++ = (_JSAMPLE)((thiscolsum + bias) >> 2);
346
45.5M
      }
347
363k
    }
348
181k
    inrow++;
349
181k
  }
350
150k
}
jdsample-8.c:h1v2_fancy_upsample
Line
Count
Source
318
150k
{
319
150k
  _JSAMPARRAY output_data = *output_data_ptr;
320
150k
  _JSAMPROW inptr0, inptr1, outptr;
321
150k
#if BITS_IN_JSAMPLE == 8
322
150k
  int thiscolsum, bias;
323
#else
324
  JLONG thiscolsum, bias;
325
#endif
326
150k
  JDIMENSION colctr;
327
150k
  int inrow, outrow, v;
328
329
150k
  inrow = outrow = 0;
330
332k
  while (outrow < cinfo->max_v_samp_factor) {
331
545k
    for (v = 0; v < 2; v++) {
332
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
333
363k
      inptr0 = input_data[inrow];
334
363k
      if (v == 0) {             /* next nearest is row above */
335
181k
        inptr1 = input_data[inrow - 1];
336
181k
        bias = 1;
337
181k
      } else {                  /* next nearest is row below */
338
181k
        inptr1 = input_data[inrow + 1];
339
181k
        bias = 2;
340
181k
      }
341
363k
      outptr = output_data[outrow++];
342
343
45.9M
      for (colctr = 0; colctr < compptr->downsampled_width; colctr++) {
344
45.5M
        thiscolsum = (*inptr0++) * 3 + (*inptr1++);
345
45.5M
        *outptr++ = (_JSAMPLE)((thiscolsum + bias) >> 2);
346
45.5M
      }
347
363k
    }
348
181k
    inrow++;
349
181k
  }
350
150k
}
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
2.24M
{
365
2.24M
  _JSAMPARRAY output_data = *output_data_ptr;
366
2.24M
  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
2.24M
  register JDIMENSION colctr;
373
2.24M
  int inrow, outrow, v;
374
375
2.24M
  inrow = outrow = 0;
376
4.50M
  while (outrow < cinfo->max_v_samp_factor) {
377
6.79M
    for (v = 0; v < 2; v++) {
378
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
379
4.53M
      inptr0 = input_data[inrow];
380
4.53M
      if (v == 0)               /* next nearest is row above */
381
2.26M
        inptr1 = input_data[inrow - 1];
382
2.26M
      else                      /* next nearest is row below */
383
2.26M
        inptr1 = input_data[inrow + 1];
384
4.53M
      outptr = output_data[outrow++];
385
386
      /* Special case for first column */
387
4.53M
      thiscolsum = (*inptr0++) * 3 + (*inptr1++);
388
4.53M
      nextcolsum = (*inptr0++) * 3 + (*inptr1++);
389
4.53M
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 8) >> 4);
390
4.53M
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
391
4.53M
      lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
392
393
192M
      for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
394
        /* General case: 3/4 * nearer component + 1/4 * further component in
395
         * each dimension, thus 9/16, 3/16, 3/16, 1/16 overall
396
         */
397
188M
        nextcolsum = (*inptr0++) * 3 + (*inptr1++);
398
188M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
399
188M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
400
188M
        lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
401
188M
      }
402
403
      /* Special case for last column */
404
4.53M
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
405
4.53M
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 7) >> 4);
406
4.53M
    }
407
2.26M
    inrow++;
408
2.26M
  }
409
2.24M
}
jdsample-8.c:h2v2_fancy_upsample
Line
Count
Source
364
2.24M
{
365
2.24M
  _JSAMPARRAY output_data = *output_data_ptr;
366
2.24M
  register _JSAMPROW inptr0, inptr1, outptr;
367
2.24M
#if BITS_IN_JSAMPLE == 8
368
2.24M
  register int thiscolsum, lastcolsum, nextcolsum;
369
#else
370
  register JLONG thiscolsum, lastcolsum, nextcolsum;
371
#endif
372
2.24M
  register JDIMENSION colctr;
373
2.24M
  int inrow, outrow, v;
374
375
2.24M
  inrow = outrow = 0;
376
4.50M
  while (outrow < cinfo->max_v_samp_factor) {
377
6.79M
    for (v = 0; v < 2; v++) {
378
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
379
4.53M
      inptr0 = input_data[inrow];
380
4.53M
      if (v == 0)               /* next nearest is row above */
381
2.26M
        inptr1 = input_data[inrow - 1];
382
2.26M
      else                      /* next nearest is row below */
383
2.26M
        inptr1 = input_data[inrow + 1];
384
4.53M
      outptr = output_data[outrow++];
385
386
      /* Special case for first column */
387
4.53M
      thiscolsum = (*inptr0++) * 3 + (*inptr1++);
388
4.53M
      nextcolsum = (*inptr0++) * 3 + (*inptr1++);
389
4.53M
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 8) >> 4);
390
4.53M
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
391
4.53M
      lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
392
393
192M
      for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
394
        /* General case: 3/4 * nearer component + 1/4 * further component in
395
         * each dimension, thus 9/16, 3/16, 3/16, 1/16 overall
396
         */
397
188M
        nextcolsum = (*inptr0++) * 3 + (*inptr1++);
398
188M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
399
188M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
400
188M
        lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
401
188M
      }
402
403
      /* Special case for last column */
404
4.53M
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
405
4.53M
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 7) >> 4);
406
4.53M
    }
407
2.26M
    inrow++;
408
2.26M
  }
409
2.24M
}
Unexecuted instantiation: jdsample-12.c:h2v2_fancy_upsample
Unexecuted instantiation: jdsample-16.c:h2v2_fancy_upsample
410
411
412
/*
413
 * Module initialization routine for upsampling.
414
 */
415
416
GLOBAL(void)
417
_jinit_upsampler(j_decompress_ptr cinfo)
418
4.78k
{
419
4.78k
  my_upsample_ptr upsample;
420
4.78k
  int ci;
421
4.78k
  jpeg_component_info *compptr;
422
4.78k
  boolean need_buffer, do_fancy;
423
4.78k
  int h_in_group, v_in_group, h_out_group, v_out_group;
424
425
4.78k
#ifdef D_LOSSLESS_SUPPORTED
426
4.78k
  if (cinfo->master->lossless) {
427
#if BITS_IN_JSAMPLE == 8
428
480
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
429
#else
430
848
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
431
848
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
432
0
#endif
433
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
434
1.32k
  } else
435
3.46k
#endif
436
3.46k
  {
437
3.46k
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
438
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
439
3.46k
  }
440
441
4.78k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
442
4.78k
    upsample = (my_upsample_ptr)
443
4.78k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
444
4.78k
                                  sizeof(my_upsampler));
445
4.78k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
446
4.78k
    upsample->pub.start_pass = start_pass_upsample;
447
4.78k
    upsample->pub._upsample = sep_upsample;
448
4.78k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
449
4.78k
  } else
450
0
    upsample = (my_upsample_ptr)cinfo->upsample;
451
452
4.78k
  if (cinfo->CCIR601_sampling)  /* this isn't supported */
453
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
454
455
  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
456
   * so don't ask for it.
457
   */
458
4.78k
  do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
459
460
  /* Verify we can handle the sampling factors, select per-component methods,
461
   * and create storage as needed.
462
   */
463
17.3k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
464
12.5k
       ci++, compptr++) {
465
    /* Compute size of an "input group" after IDCT scaling.  This many samples
466
     * are to be converted to max_h_samp_factor * max_v_samp_factor components.
467
     */
468
12.5k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
469
12.5k
                 cinfo->_min_DCT_scaled_size;
470
12.5k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
471
12.5k
                 cinfo->_min_DCT_scaled_size;
472
12.5k
    h_out_group = cinfo->max_h_samp_factor;
473
12.5k
    v_out_group = cinfo->max_v_samp_factor;
474
12.5k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
475
12.5k
    need_buffer = TRUE;
476
12.5k
    if (!compptr->component_needed) {
477
      /* Don't bother to upsample an uninteresting component. */
478
0
      upsample->methods[ci] = noop_upsample;
479
0
      need_buffer = FALSE;
480
12.5k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
481
      /* Fullsize components can be processed without any work. */
482
5.06k
      upsample->methods[ci] = fullsize_upsample;
483
5.06k
      need_buffer = FALSE;
484
7.46k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
485
      /* Special cases for 2h1v upsampling */
486
923
      if (do_fancy && compptr->downsampled_width > 2) {
487
#ifdef WITH_SIMD
488
445
        if (jsimd_can_h2v1_fancy_upsample())
489
0
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
490
445
        else
491
445
#endif
492
445
          upsample->methods[ci] = h2v1_fancy_upsample;
493
487
      } else {
494
#ifdef WITH_SIMD
495
335
        if (jsimd_can_h2v1_upsample())
496
0
          upsample->methods[ci] = jsimd_h2v1_upsample;
497
335
        else
498
335
#endif
499
335
          upsample->methods[ci] = h2v1_upsample;
500
436
      }
501
6.54k
    } else if (h_in_group == h_out_group &&
502
1.77k
               v_in_group * 2 == v_out_group && do_fancy) {
503
      /* Non-fancy upsampling is handled by the generic method */
504
#if defined(WITH_SIMD) && (defined(__arm__) || defined(__aarch64__) || \
505
                           defined(_M_ARM) || defined(_M_ARM64) || \
506
                           defined(_M_ARM64EC))
507
      if (jsimd_can_h1v2_fancy_upsample())
508
        upsample->methods[ci] = jsimd_h1v2_fancy_upsample;
509
      else
510
#endif
511
670
        upsample->methods[ci] = h1v2_fancy_upsample;
512
670
      upsample->pub.need_context_rows = TRUE;
513
5.87k
    } else if (h_in_group * 2 == h_out_group &&
514
4.00k
               v_in_group * 2 == v_out_group) {
515
      /* Special cases for 2h2v upsampling */
516
3.29k
      if (do_fancy && compptr->downsampled_width > 2) {
517
#ifdef WITH_SIMD
518
2.40k
        if (jsimd_can_h2v2_fancy_upsample())
519
0
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
520
2.40k
        else
521
2.40k
#endif
522
2.40k
          upsample->methods[ci] = h2v2_fancy_upsample;
523
2.79k
        upsample->pub.need_context_rows = TRUE;
524
2.79k
      } else {
525
#ifdef WITH_SIMD
526
425
        if (jsimd_can_h2v2_upsample())
527
0
          upsample->methods[ci] = jsimd_h2v2_upsample;
528
425
        else
529
425
#endif
530
425
          upsample->methods[ci] = h2v2_upsample;
531
501
      }
532
3.29k
    } else if ((h_out_group % h_in_group) == 0 &&
533
2.56k
               (v_out_group % v_in_group) == 0) {
534
      /* Generic integral-factors upsampling method */
535
#if defined(WITH_SIMD) && defined(__mips__)
536
      if (jsimd_can_int_upsample())
537
        upsample->methods[ci] = jsimd_int_upsample;
538
      else
539
#endif
540
2.53k
        upsample->methods[ci] = int_upsample;
541
2.53k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
542
2.53k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
543
2.53k
    } else
544
40
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
545
12.5k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
546
7.42k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
547
7.42k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
548
7.42k
         (JDIMENSION)jround_up((long)cinfo->output_width,
549
7.42k
                               (long)cinfo->max_h_samp_factor),
550
7.42k
         (JDIMENSION)cinfo->max_v_samp_factor);
551
7.42k
    }
552
12.5k
  }
553
4.78k
}
jinit_upsampler
Line
Count
Source
418
3.58k
{
419
3.58k
  my_upsample_ptr upsample;
420
3.58k
  int ci;
421
3.58k
  jpeg_component_info *compptr;
422
3.58k
  boolean need_buffer, do_fancy;
423
3.58k
  int h_in_group, v_in_group, h_out_group, v_out_group;
424
425
3.58k
#ifdef D_LOSSLESS_SUPPORTED
426
3.58k
  if (cinfo->master->lossless) {
427
480
#if BITS_IN_JSAMPLE == 8
428
480
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
429
#else
430
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
431
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
432
#endif
433
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
434
480
  } else
435
3.10k
#endif
436
3.10k
  {
437
3.10k
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
438
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
439
3.10k
  }
440
441
3.58k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
442
3.58k
    upsample = (my_upsample_ptr)
443
3.58k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
444
3.58k
                                  sizeof(my_upsampler));
445
3.58k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
446
3.58k
    upsample->pub.start_pass = start_pass_upsample;
447
3.58k
    upsample->pub._upsample = sep_upsample;
448
3.58k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
449
3.58k
  } else
450
0
    upsample = (my_upsample_ptr)cinfo->upsample;
451
452
3.58k
  if (cinfo->CCIR601_sampling)  /* this isn't supported */
453
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
454
455
  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
456
   * so don't ask for it.
457
   */
458
3.58k
  do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
459
460
  /* Verify we can handle the sampling factors, select per-component methods,
461
   * and create storage as needed.
462
   */
463
12.5k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
464
8.99k
       ci++, compptr++) {
465
    /* Compute size of an "input group" after IDCT scaling.  This many samples
466
     * are to be converted to max_h_samp_factor * max_v_samp_factor components.
467
     */
468
8.99k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
469
8.99k
                 cinfo->_min_DCT_scaled_size;
470
8.99k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
471
8.99k
                 cinfo->_min_DCT_scaled_size;
472
8.99k
    h_out_group = cinfo->max_h_samp_factor;
473
8.99k
    v_out_group = cinfo->max_v_samp_factor;
474
8.99k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
475
8.99k
    need_buffer = TRUE;
476
8.99k
    if (!compptr->component_needed) {
477
      /* Don't bother to upsample an uninteresting component. */
478
0
      upsample->methods[ci] = noop_upsample;
479
0
      need_buffer = FALSE;
480
8.99k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
481
      /* Fullsize components can be processed without any work. */
482
3.50k
      upsample->methods[ci] = fullsize_upsample;
483
3.50k
      need_buffer = FALSE;
484
5.48k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
485
      /* Special cases for 2h1v upsampling */
486
780
      if (do_fancy && compptr->downsampled_width > 2) {
487
445
#ifdef WITH_SIMD
488
445
        if (jsimd_can_h2v1_fancy_upsample())
489
0
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
490
445
        else
491
445
#endif
492
445
          upsample->methods[ci] = h2v1_fancy_upsample;
493
445
      } else {
494
335
#ifdef WITH_SIMD
495
335
        if (jsimd_can_h2v1_upsample())
496
0
          upsample->methods[ci] = jsimd_h2v1_upsample;
497
335
        else
498
335
#endif
499
335
          upsample->methods[ci] = h2v1_upsample;
500
335
      }
501
4.70k
    } else if (h_in_group == h_out_group &&
502
1.06k
               v_in_group * 2 == v_out_group && do_fancy) {
503
      /* Non-fancy upsampling is handled by the generic method */
504
#if defined(WITH_SIMD) && (defined(__arm__) || defined(__aarch64__) || \
505
                           defined(_M_ARM) || defined(_M_ARM64) || \
506
                           defined(_M_ARM64EC))
507
      if (jsimd_can_h1v2_fancy_upsample())
508
        upsample->methods[ci] = jsimd_h1v2_fancy_upsample;
509
      else
510
#endif
511
627
        upsample->methods[ci] = h1v2_fancy_upsample;
512
627
      upsample->pub.need_context_rows = TRUE;
513
4.07k
    } else if (h_in_group * 2 == h_out_group &&
514
3.09k
               v_in_group * 2 == v_out_group) {
515
      /* Special cases for 2h2v upsampling */
516
2.82k
      if (do_fancy && compptr->downsampled_width > 2) {
517
2.40k
#ifdef WITH_SIMD
518
2.40k
        if (jsimd_can_h2v2_fancy_upsample())
519
0
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
520
2.40k
        else
521
2.40k
#endif
522
2.40k
          upsample->methods[ci] = h2v2_fancy_upsample;
523
2.40k
        upsample->pub.need_context_rows = TRUE;
524
2.40k
      } else {
525
425
#ifdef WITH_SIMD
526
425
        if (jsimd_can_h2v2_upsample())
527
0
          upsample->methods[ci] = jsimd_h2v2_upsample;
528
425
        else
529
425
#endif
530
425
          upsample->methods[ci] = h2v2_upsample;
531
425
      }
532
2.82k
    } else if ((h_out_group % h_in_group) == 0 &&
533
1.24k
               (v_out_group % v_in_group) == 0) {
534
      /* Generic integral-factors upsampling method */
535
#if defined(WITH_SIMD) && defined(__mips__)
536
      if (jsimd_can_int_upsample())
537
        upsample->methods[ci] = jsimd_int_upsample;
538
      else
539
#endif
540
1.23k
        upsample->methods[ci] = int_upsample;
541
1.23k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
542
1.23k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
543
1.23k
    } else
544
22
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
545
8.99k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
546
5.46k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
547
5.46k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
548
5.46k
         (JDIMENSION)jround_up((long)cinfo->output_width,
549
5.46k
                               (long)cinfo->max_h_samp_factor),
550
5.46k
         (JDIMENSION)cinfo->max_v_samp_factor);
551
5.46k
    }
552
8.99k
  }
553
3.58k
}
j12init_upsampler
Line
Count
Source
418
755
{
419
755
  my_upsample_ptr upsample;
420
755
  int ci;
421
755
  jpeg_component_info *compptr;
422
755
  boolean need_buffer, do_fancy;
423
755
  int h_in_group, v_in_group, h_out_group, v_out_group;
424
425
755
#ifdef D_LOSSLESS_SUPPORTED
426
755
  if (cinfo->master->lossless) {
427
#if BITS_IN_JSAMPLE == 8
428
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
429
#else
430
402
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
431
402
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
432
0
#endif
433
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
434
402
  } else
435
353
#endif
436
353
  {
437
353
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
438
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
439
353
  }
440
441
755
  if (!cinfo->master->jinit_upsampler_no_alloc) {
442
755
    upsample = (my_upsample_ptr)
443
755
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
444
755
                                  sizeof(my_upsampler));
445
755
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
446
755
    upsample->pub.start_pass = start_pass_upsample;
447
755
    upsample->pub._upsample = sep_upsample;
448
755
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
449
755
  } else
450
0
    upsample = (my_upsample_ptr)cinfo->upsample;
451
452
755
  if (cinfo->CCIR601_sampling)  /* this isn't supported */
453
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
454
455
  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
456
   * so don't ask for it.
457
   */
458
755
  do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
459
460
  /* Verify we can handle the sampling factors, select per-component methods,
461
   * and create storage as needed.
462
   */
463
2.93k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
464
2.18k
       ci++, compptr++) {
465
    /* Compute size of an "input group" after IDCT scaling.  This many samples
466
     * are to be converted to max_h_samp_factor * max_v_samp_factor components.
467
     */
468
2.18k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
469
2.18k
                 cinfo->_min_DCT_scaled_size;
470
2.18k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
471
2.18k
                 cinfo->_min_DCT_scaled_size;
472
2.18k
    h_out_group = cinfo->max_h_samp_factor;
473
2.18k
    v_out_group = cinfo->max_v_samp_factor;
474
2.18k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
475
2.18k
    need_buffer = TRUE;
476
2.18k
    if (!compptr->component_needed) {
477
      /* Don't bother to upsample an uninteresting component. */
478
0
      upsample->methods[ci] = noop_upsample;
479
0
      need_buffer = FALSE;
480
2.18k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
481
      /* Fullsize components can be processed without any work. */
482
810
      upsample->methods[ci] = fullsize_upsample;
483
810
      need_buffer = FALSE;
484
1.37k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
485
      /* Special cases for 2h1v upsampling */
486
80
      if (do_fancy && compptr->downsampled_width > 2) {
487
#ifdef WITH_SIMD
488
        if (jsimd_can_h2v1_fancy_upsample())
489
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
490
        else
491
#endif
492
42
          upsample->methods[ci] = h2v1_fancy_upsample;
493
42
      } else {
494
#ifdef WITH_SIMD
495
        if (jsimd_can_h2v1_upsample())
496
          upsample->methods[ci] = jsimd_h2v1_upsample;
497
        else
498
#endif
499
38
          upsample->methods[ci] = h2v1_upsample;
500
38
      }
501
1.29k
    } else if (h_in_group == h_out_group &&
502
422
               v_in_group * 2 == v_out_group && do_fancy) {
503
      /* Non-fancy upsampling is handled by the generic method */
504
#if defined(WITH_SIMD) && (defined(__arm__) || defined(__aarch64__) || \
505
                           defined(_M_ARM) || defined(_M_ARM64) || \
506
                           defined(_M_ARM64EC))
507
      if (jsimd_can_h1v2_fancy_upsample())
508
        upsample->methods[ci] = jsimd_h1v2_fancy_upsample;
509
      else
510
#endif
511
43
        upsample->methods[ci] = h1v2_fancy_upsample;
512
43
      upsample->pub.need_context_rows = TRUE;
513
1.25k
    } else if (h_in_group * 2 == h_out_group &&
514
750
               v_in_group * 2 == v_out_group) {
515
      /* Special cases for 2h2v upsampling */
516
439
      if (do_fancy && compptr->downsampled_width > 2) {
517
#ifdef WITH_SIMD
518
        if (jsimd_can_h2v2_fancy_upsample())
519
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
520
        else
521
#endif
522
393
          upsample->methods[ci] = h2v2_fancy_upsample;
523
393
        upsample->pub.need_context_rows = TRUE;
524
393
      } else {
525
#ifdef WITH_SIMD
526
        if (jsimd_can_h2v2_upsample())
527
          upsample->methods[ci] = jsimd_h2v2_upsample;
528
        else
529
#endif
530
46
          upsample->methods[ci] = h2v2_upsample;
531
46
      }
532
812
    } else if ((h_out_group % h_in_group) == 0 &&
533
809
               (v_out_group % v_in_group) == 0) {
534
      /* Generic integral-factors upsampling method */
535
#if defined(WITH_SIMD) && defined(__mips__)
536
      if (jsimd_can_int_upsample())
537
        upsample->methods[ci] = jsimd_int_upsample;
538
      else
539
#endif
540
803
        upsample->methods[ci] = int_upsample;
541
803
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
542
803
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
543
803
    } else
544
9
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
545
2.18k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
546
1.36k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
547
1.36k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
548
1.36k
         (JDIMENSION)jround_up((long)cinfo->output_width,
549
1.36k
                               (long)cinfo->max_h_samp_factor),
550
1.36k
         (JDIMENSION)cinfo->max_v_samp_factor);
551
1.36k
    }
552
2.18k
  }
553
755
}
j16init_upsampler
Line
Count
Source
418
446
{
419
446
  my_upsample_ptr upsample;
420
446
  int ci;
421
446
  jpeg_component_info *compptr;
422
446
  boolean need_buffer, do_fancy;
423
446
  int h_in_group, v_in_group, h_out_group, v_out_group;
424
425
446
#ifdef D_LOSSLESS_SUPPORTED
426
446
  if (cinfo->master->lossless) {
427
#if BITS_IN_JSAMPLE == 8
428
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
429
#else
430
446
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
431
446
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
432
0
#endif
433
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
434
446
  } else
435
0
#endif
436
0
  {
437
0
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
438
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
439
0
  }
440
441
446
  if (!cinfo->master->jinit_upsampler_no_alloc) {
442
446
    upsample = (my_upsample_ptr)
443
446
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
444
446
                                  sizeof(my_upsampler));
445
446
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
446
446
    upsample->pub.start_pass = start_pass_upsample;
447
446
    upsample->pub._upsample = sep_upsample;
448
446
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
449
446
  } else
450
0
    upsample = (my_upsample_ptr)cinfo->upsample;
451
452
446
  if (cinfo->CCIR601_sampling)  /* this isn't supported */
453
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
454
455
  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
456
   * so don't ask for it.
457
   */
458
446
  do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
459
460
  /* Verify we can handle the sampling factors, select per-component methods,
461
   * and create storage as needed.
462
   */
463
1.79k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
464
1.35k
       ci++, compptr++) {
465
    /* Compute size of an "input group" after IDCT scaling.  This many samples
466
     * are to be converted to max_h_samp_factor * max_v_samp_factor components.
467
     */
468
1.35k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
469
1.35k
                 cinfo->_min_DCT_scaled_size;
470
1.35k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
471
1.35k
                 cinfo->_min_DCT_scaled_size;
472
1.35k
    h_out_group = cinfo->max_h_samp_factor;
473
1.35k
    v_out_group = cinfo->max_v_samp_factor;
474
1.35k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
475
1.35k
    need_buffer = TRUE;
476
1.35k
    if (!compptr->component_needed) {
477
      /* Don't bother to upsample an uninteresting component. */
478
0
      upsample->methods[ci] = noop_upsample;
479
0
      need_buffer = FALSE;
480
1.35k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
481
      /* Fullsize components can be processed without any work. */
482
745
      upsample->methods[ci] = fullsize_upsample;
483
745
      need_buffer = FALSE;
484
745
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
485
      /* Special cases for 2h1v upsampling */
486
63
      if (do_fancy && compptr->downsampled_width > 2) {
487
#ifdef WITH_SIMD
488
        if (jsimd_can_h2v1_fancy_upsample())
489
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
490
        else
491
#endif
492
0
          upsample->methods[ci] = h2v1_fancy_upsample;
493
63
      } else {
494
#ifdef WITH_SIMD
495
        if (jsimd_can_h2v1_upsample())
496
          upsample->methods[ci] = jsimd_h2v1_upsample;
497
        else
498
#endif
499
63
          upsample->methods[ci] = h2v1_upsample;
500
63
      }
501
545
    } else if (h_in_group == h_out_group &&
502
284
               v_in_group * 2 == v_out_group && do_fancy) {
503
      /* Non-fancy upsampling is handled by the generic method */
504
#if defined(WITH_SIMD) && (defined(__arm__) || defined(__aarch64__) || \
505
                           defined(_M_ARM) || defined(_M_ARM64) || \
506
                           defined(_M_ARM64EC))
507
      if (jsimd_can_h1v2_fancy_upsample())
508
        upsample->methods[ci] = jsimd_h1v2_fancy_upsample;
509
      else
510
#endif
511
0
        upsample->methods[ci] = h1v2_fancy_upsample;
512
0
      upsample->pub.need_context_rows = TRUE;
513
545
    } else if (h_in_group * 2 == h_out_group &&
514
159
               v_in_group * 2 == v_out_group) {
515
      /* Special cases for 2h2v upsampling */
516
30
      if (do_fancy && compptr->downsampled_width > 2) {
517
#ifdef WITH_SIMD
518
        if (jsimd_can_h2v2_fancy_upsample())
519
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
520
        else
521
#endif
522
0
          upsample->methods[ci] = h2v2_fancy_upsample;
523
0
        upsample->pub.need_context_rows = TRUE;
524
30
      } else {
525
#ifdef WITH_SIMD
526
        if (jsimd_can_h2v2_upsample())
527
          upsample->methods[ci] = jsimd_h2v2_upsample;
528
        else
529
#endif
530
30
          upsample->methods[ci] = h2v2_upsample;
531
30
      }
532
515
    } else if ((h_out_group % h_in_group) == 0 &&
533
509
               (v_out_group % v_in_group) == 0) {
534
      /* Generic integral-factors upsampling method */
535
#if defined(WITH_SIMD) && defined(__mips__)
536
      if (jsimd_can_int_upsample())
537
        upsample->methods[ci] = jsimd_int_upsample;
538
      else
539
#endif
540
506
        upsample->methods[ci] = int_upsample;
541
506
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
542
506
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
543
506
    } else
544
9
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
545
1.35k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
546
599
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
547
599
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
548
599
         (JDIMENSION)jround_up((long)cinfo->output_width,
549
599
                               (long)cinfo->max_h_samp_factor),
550
599
         (JDIMENSION)cinfo->max_v_samp_factor);
551
599
    }
552
1.35k
  }
553
446
}
554
555
#endif /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */