Coverage Report

Created: 2026-03-07 06:26

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
4.23k
{
44
4.23k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
4.23k
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
4.23k
  upsample->rows_to_go = cinfo->output_height;
50
4.23k
}
jdsample-8.c:start_pass_upsample
Line
Count
Source
43
3.95k
{
44
3.95k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
3.95k
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
3.95k
  upsample->rows_to_go = cinfo->output_height;
50
3.95k
}
jdsample-12.c:start_pass_upsample
Line
Count
Source
43
246
{
44
246
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
246
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
246
  upsample->rows_to_go = cinfo->output_height;
50
246
}
jdsample-16.c:start_pass_upsample
Line
Count
Source
43
27
{
44
27
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
27
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
27
  upsample->rows_to_go = cinfo->output_height;
50
27
}
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
19.2M
{
67
19.2M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
68
19.2M
  int ci;
69
19.2M
  jpeg_component_info *compptr;
70
19.2M
  JDIMENSION num_rows;
71
72
  /* Fill the conversion buffer, if it's empty */
73
19.2M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
74
22.5M
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
75
14.0M
         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
14.0M
      (*upsample->methods[ci]) (cinfo, compptr,
80
14.0M
        input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
81
14.0M
        upsample->color_buf + ci);
82
14.0M
    }
83
8.52M
    upsample->next_row_out = 0;
84
8.52M
  }
85
86
  /* Color-convert and emit rows */
87
88
  /* How many we have in the buffer: */
89
19.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
19.2M
  if (num_rows > upsample->rows_to_go)
94
1.15k
    num_rows = upsample->rows_to_go;
95
  /* And not more than what the client can accept: */
96
19.2M
  out_rows_avail -= *out_row_ctr;
97
19.2M
  if (num_rows > out_rows_avail)
98
10.7M
    num_rows = out_rows_avail;
99
100
19.2M
  (*cinfo->cconvert->_color_convert) (cinfo, upsample->color_buf,
101
19.2M
                                      (JDIMENSION)upsample->next_row_out,
102
19.2M
                                      output_buf + *out_row_ctr,
103
19.2M
                                      (int)num_rows);
104
105
  /* Adjust counts */
106
19.2M
  *out_row_ctr += num_rows;
107
19.2M
  upsample->rows_to_go -= num_rows;
108
19.2M
  upsample->next_row_out += num_rows;
109
  /* When the buffer is emptied, declare this input row group consumed */
110
19.2M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
111
8.52M
    (*in_row_group_ctr)++;
112
19.2M
}
jdsample-8.c:sep_upsample
Line
Count
Source
66
19.2M
{
67
19.2M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
68
19.2M
  int ci;
69
19.2M
  jpeg_component_info *compptr;
70
19.2M
  JDIMENSION num_rows;
71
72
  /* Fill the conversion buffer, if it's empty */
73
19.2M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
74
22.5M
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
75
14.0M
         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
14.0M
      (*upsample->methods[ci]) (cinfo, compptr,
80
14.0M
        input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
81
14.0M
        upsample->color_buf + ci);
82
14.0M
    }
83
8.52M
    upsample->next_row_out = 0;
84
8.52M
  }
85
86
  /* Color-convert and emit rows */
87
88
  /* How many we have in the buffer: */
89
19.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
19.2M
  if (num_rows > upsample->rows_to_go)
94
1.15k
    num_rows = upsample->rows_to_go;
95
  /* And not more than what the client can accept: */
96
19.2M
  out_rows_avail -= *out_row_ctr;
97
19.2M
  if (num_rows > out_rows_avail)
98
10.7M
    num_rows = out_rows_avail;
99
100
19.2M
  (*cinfo->cconvert->_color_convert) (cinfo, upsample->color_buf,
101
19.2M
                                      (JDIMENSION)upsample->next_row_out,
102
19.2M
                                      output_buf + *out_row_ctr,
103
19.2M
                                      (int)num_rows);
104
105
  /* Adjust counts */
106
19.2M
  *out_row_ctr += num_rows;
107
19.2M
  upsample->rows_to_go -= num_rows;
108
19.2M
  upsample->next_row_out += num_rows;
109
  /* When the buffer is emptied, declare this input row group consumed */
110
19.2M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
111
8.52M
    (*in_row_group_ctr)++;
112
19.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 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
7.02M
{
132
7.02M
  *output_data_ptr = input_data;
133
7.02M
}
jdsample-8.c:fullsize_upsample
Line
Count
Source
131
7.02M
{
132
7.02M
  *output_data_ptr = input_data;
133
7.02M
}
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
2.20M
{
164
2.20M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
165
2.20M
  _JSAMPARRAY output_data = *output_data_ptr;
166
2.20M
  register _JSAMPROW inptr, outptr;
167
2.20M
  register _JSAMPLE invalue;
168
2.20M
  register int h;
169
2.20M
  _JSAMPROW outend;
170
2.20M
  int h_expand, v_expand;
171
2.20M
  int inrow, outrow;
172
173
2.20M
  h_expand = upsample->h_expand[compptr->component_index];
174
2.20M
  v_expand = upsample->v_expand[compptr->component_index];
175
176
2.20M
  inrow = outrow = 0;
177
4.40M
  while (outrow < cinfo->max_v_samp_factor) {
178
    /* Generate one output row with proper horizontal expansion */
179
2.20M
    inptr = input_data[inrow];
180
2.20M
    outptr = output_data[outrow];
181
2.20M
    outend = outptr + cinfo->output_width;
182
122M
    while (outptr < outend) {
183
120M
      invalue = *inptr++;
184
318M
      for (h = h_expand; h > 0; h--) {
185
198M
        *outptr++ = invalue;
186
198M
      }
187
120M
    }
188
    /* Generate any additional output rows by duplicating the first one */
189
2.20M
    if (v_expand > 1) {
190
2.19M
      _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1,
191
2.19M
                         v_expand - 1, cinfo->output_width);
192
2.19M
    }
193
2.20M
    inrow++;
194
2.20M
    outrow += v_expand;
195
2.20M
  }
196
2.20M
}
jdsample-8.c:int_upsample
Line
Count
Source
163
2.20M
{
164
2.20M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
165
2.20M
  _JSAMPARRAY output_data = *output_data_ptr;
166
2.20M
  register _JSAMPROW inptr, outptr;
167
2.20M
  register _JSAMPLE invalue;
168
2.20M
  register int h;
169
2.20M
  _JSAMPROW outend;
170
2.20M
  int h_expand, v_expand;
171
2.20M
  int inrow, outrow;
172
173
2.20M
  h_expand = upsample->h_expand[compptr->component_index];
174
2.20M
  v_expand = upsample->v_expand[compptr->component_index];
175
176
2.20M
  inrow = outrow = 0;
177
4.40M
  while (outrow < cinfo->max_v_samp_factor) {
178
    /* Generate one output row with proper horizontal expansion */
179
2.20M
    inptr = input_data[inrow];
180
2.20M
    outptr = output_data[outrow];
181
2.20M
    outend = outptr + cinfo->output_width;
182
122M
    while (outptr < outend) {
183
120M
      invalue = *inptr++;
184
318M
      for (h = h_expand; h > 0; h--) {
185
198M
        *outptr++ = invalue;
186
198M
      }
187
120M
    }
188
    /* Generate any additional output rows by duplicating the first one */
189
2.20M
    if (v_expand > 1) {
190
2.19M
      _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1,
191
2.19M
                         v_expand - 1, cinfo->output_width);
192
2.19M
    }
193
2.20M
    inrow++;
194
2.20M
    outrow += v_expand;
195
2.20M
  }
196
2.20M
}
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
58.5k
{
208
58.5k
  _JSAMPARRAY output_data = *output_data_ptr;
209
58.5k
  register _JSAMPROW inptr, outptr;
210
58.5k
  register _JSAMPLE invalue;
211
58.5k
  _JSAMPROW outend;
212
58.5k
  int inrow;
213
214
290k
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
215
231k
    inptr = input_data[inrow];
216
231k
    outptr = output_data[inrow];
217
231k
    outend = outptr + cinfo->output_width;
218
731k
    while (outptr < outend) {
219
499k
      invalue = *inptr++;
220
499k
      *outptr++ = invalue;
221
499k
      *outptr++ = invalue;
222
499k
    }
223
231k
  }
224
58.5k
}
jdsample-8.c:h2v1_upsample
Line
Count
Source
207
58.5k
{
208
58.5k
  _JSAMPARRAY output_data = *output_data_ptr;
209
58.5k
  register _JSAMPROW inptr, outptr;
210
58.5k
  register _JSAMPLE invalue;
211
58.5k
  _JSAMPROW outend;
212
58.5k
  int inrow;
213
214
290k
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
215
231k
    inptr = input_data[inrow];
216
231k
    outptr = output_data[inrow];
217
231k
    outend = outptr + cinfo->output_width;
218
731k
    while (outptr < outend) {
219
499k
      invalue = *inptr++;
220
499k
      *outptr++ = invalue;
221
499k
      *outptr++ = invalue;
222
499k
    }
223
231k
  }
224
58.5k
}
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
69.4k
{
236
69.4k
  _JSAMPARRAY output_data = *output_data_ptr;
237
69.4k
  register _JSAMPROW inptr, outptr;
238
69.4k
  register _JSAMPLE invalue;
239
69.4k
  _JSAMPROW outend;
240
69.4k
  int inrow, outrow;
241
242
69.4k
  inrow = outrow = 0;
243
196k
  while (outrow < cinfo->max_v_samp_factor) {
244
126k
    inptr = input_data[inrow];
245
126k
    outptr = output_data[outrow];
246
126k
    outend = outptr + cinfo->output_width;
247
394k
    while (outptr < outend) {
248
268k
      invalue = *inptr++;
249
268k
      *outptr++ = invalue;
250
268k
      *outptr++ = invalue;
251
268k
    }
252
126k
    _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1, 1,
253
126k
                       cinfo->output_width);
254
126k
    inrow++;
255
126k
    outrow += 2;
256
126k
  }
257
69.4k
}
jdsample-8.c:h2v2_upsample
Line
Count
Source
235
69.4k
{
236
69.4k
  _JSAMPARRAY output_data = *output_data_ptr;
237
69.4k
  register _JSAMPROW inptr, outptr;
238
69.4k
  register _JSAMPLE invalue;
239
69.4k
  _JSAMPROW outend;
240
69.4k
  int inrow, outrow;
241
242
69.4k
  inrow = outrow = 0;
243
196k
  while (outrow < cinfo->max_v_samp_factor) {
244
126k
    inptr = input_data[inrow];
245
126k
    outptr = output_data[outrow];
246
126k
    outend = outptr + cinfo->output_width;
247
394k
    while (outptr < outend) {
248
268k
      invalue = *inptr++;
249
268k
      *outptr++ = invalue;
250
268k
      *outptr++ = invalue;
251
268k
    }
252
126k
    _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1, 1,
253
126k
                       cinfo->output_width);
254
126k
    inrow++;
255
126k
    outrow += 2;
256
126k
  }
257
69.4k
}
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
1.66M
{
279
1.66M
  _JSAMPARRAY output_data = *output_data_ptr;
280
1.66M
  register _JSAMPROW inptr, outptr;
281
1.66M
  register int invalue;
282
1.66M
  register JDIMENSION colctr;
283
1.66M
  int inrow;
284
285
6.93M
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
286
5.27M
    inptr = input_data[inrow];
287
5.27M
    outptr = output_data[inrow];
288
    /* Special case for first column */
289
5.27M
    invalue = *inptr++;
290
5.27M
    *outptr++ = (_JSAMPLE)invalue;
291
5.27M
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[0] + 2) >> 2);
292
293
32.5M
    for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
294
      /* General case: 3/4 * nearer component + 1/4 * further component */
295
27.2M
      invalue = (*inptr++) * 3;
296
27.2M
      *outptr++ = (_JSAMPLE)((invalue + inptr[-2] + 1) >> 2);
297
27.2M
      *outptr++ = (_JSAMPLE)((invalue + inptr[0] + 2) >> 2);
298
27.2M
    }
299
300
    /* Special case for last column */
301
5.27M
    invalue = *inptr;
302
5.27M
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[-1] + 1) >> 2);
303
5.27M
    *outptr++ = (_JSAMPLE)invalue;
304
5.27M
  }
305
1.66M
}
jdsample-8.c:h2v1_fancy_upsample
Line
Count
Source
278
1.66M
{
279
1.66M
  _JSAMPARRAY output_data = *output_data_ptr;
280
1.66M
  register _JSAMPROW inptr, outptr;
281
1.66M
  register int invalue;
282
1.66M
  register JDIMENSION colctr;
283
1.66M
  int inrow;
284
285
6.93M
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
286
5.27M
    inptr = input_data[inrow];
287
5.27M
    outptr = output_data[inrow];
288
    /* Special case for first column */
289
5.27M
    invalue = *inptr++;
290
5.27M
    *outptr++ = (_JSAMPLE)invalue;
291
5.27M
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[0] + 2) >> 2);
292
293
32.5M
    for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
294
      /* General case: 3/4 * nearer component + 1/4 * further component */
295
27.2M
      invalue = (*inptr++) * 3;
296
27.2M
      *outptr++ = (_JSAMPLE)((invalue + inptr[-2] + 1) >> 2);
297
27.2M
      *outptr++ = (_JSAMPLE)((invalue + inptr[0] + 2) >> 2);
298
27.2M
    }
299
300
    /* Special case for last column */
301
5.27M
    invalue = *inptr;
302
5.27M
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[-1] + 1) >> 2);
303
5.27M
    *outptr++ = (_JSAMPLE)invalue;
304
5.27M
  }
305
1.66M
}
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
579k
{
319
579k
  _JSAMPARRAY output_data = *output_data_ptr;
320
579k
  _JSAMPROW inptr0, inptr1, outptr;
321
#if BITS_IN_JSAMPLE == 8
322
  int thiscolsum, bias;
323
#else
324
  JLONG thiscolsum, bias;
325
#endif
326
579k
  JDIMENSION colctr;
327
579k
  int inrow, outrow, v;
328
329
579k
  inrow = outrow = 0;
330
1.17M
  while (outrow < cinfo->max_v_samp_factor) {
331
1.79M
    for (v = 0; v < 2; v++) {
332
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
333
1.19M
      inptr0 = input_data[inrow];
334
1.19M
      if (v == 0) {             /* next nearest is row above */
335
597k
        inptr1 = input_data[inrow - 1];
336
597k
        bias = 1;
337
597k
      } else {                  /* next nearest is row below */
338
597k
        inptr1 = input_data[inrow + 1];
339
597k
        bias = 2;
340
597k
      }
341
1.19M
      outptr = output_data[outrow++];
342
343
38.6M
      for (colctr = 0; colctr < compptr->downsampled_width; colctr++) {
344
37.4M
        thiscolsum = (*inptr0++) * 3 + (*inptr1++);
345
37.4M
        *outptr++ = (_JSAMPLE)((thiscolsum + bias) >> 2);
346
37.4M
      }
347
1.19M
    }
348
597k
    inrow++;
349
597k
  }
350
579k
}
jdsample-8.c:h1v2_fancy_upsample
Line
Count
Source
318
579k
{
319
579k
  _JSAMPARRAY output_data = *output_data_ptr;
320
579k
  _JSAMPROW inptr0, inptr1, outptr;
321
579k
#if BITS_IN_JSAMPLE == 8
322
579k
  int thiscolsum, bias;
323
#else
324
  JLONG thiscolsum, bias;
325
#endif
326
579k
  JDIMENSION colctr;
327
579k
  int inrow, outrow, v;
328
329
579k
  inrow = outrow = 0;
330
1.17M
  while (outrow < cinfo->max_v_samp_factor) {
331
1.79M
    for (v = 0; v < 2; v++) {
332
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
333
1.19M
      inptr0 = input_data[inrow];
334
1.19M
      if (v == 0) {             /* next nearest is row above */
335
597k
        inptr1 = input_data[inrow - 1];
336
597k
        bias = 1;
337
597k
      } else {                  /* next nearest is row below */
338
597k
        inptr1 = input_data[inrow + 1];
339
597k
        bias = 2;
340
597k
      }
341
1.19M
      outptr = output_data[outrow++];
342
343
38.6M
      for (colctr = 0; colctr < compptr->downsampled_width; colctr++) {
344
37.4M
        thiscolsum = (*inptr0++) * 3 + (*inptr1++);
345
37.4M
        *outptr++ = (_JSAMPLE)((thiscolsum + bias) >> 2);
346
37.4M
      }
347
1.19M
    }
348
597k
    inrow++;
349
597k
  }
350
579k
}
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.41M
{
365
2.41M
  _JSAMPARRAY output_data = *output_data_ptr;
366
2.41M
  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.41M
  register JDIMENSION colctr;
373
2.41M
  int inrow, outrow, v;
374
375
2.41M
  inrow = outrow = 0;
376
5.84M
  while (outrow < cinfo->max_v_samp_factor) {
377
10.2M
    for (v = 0; v < 2; v++) {
378
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
379
6.86M
      inptr0 = input_data[inrow];
380
6.86M
      if (v == 0)               /* next nearest is row above */
381
3.43M
        inptr1 = input_data[inrow - 1];
382
3.43M
      else                      /* next nearest is row below */
383
3.43M
        inptr1 = input_data[inrow + 1];
384
6.86M
      outptr = output_data[outrow++];
385
386
      /* Special case for first column */
387
6.86M
      thiscolsum = (*inptr0++) * 3 + (*inptr1++);
388
6.86M
      nextcolsum = (*inptr0++) * 3 + (*inptr1++);
389
6.86M
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 8) >> 4);
390
6.86M
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
391
6.86M
      lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
392
393
163M
      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
156M
        nextcolsum = (*inptr0++) * 3 + (*inptr1++);
398
156M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
399
156M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
400
156M
        lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
401
156M
      }
402
403
      /* Special case for last column */
404
6.86M
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
405
6.86M
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 7) >> 4);
406
6.86M
    }
407
3.43M
    inrow++;
408
3.43M
  }
409
2.41M
}
jdsample-8.c:h2v2_fancy_upsample
Line
Count
Source
364
2.41M
{
365
2.41M
  _JSAMPARRAY output_data = *output_data_ptr;
366
2.41M
  register _JSAMPROW inptr0, inptr1, outptr;
367
2.41M
#if BITS_IN_JSAMPLE == 8
368
2.41M
  register int thiscolsum, lastcolsum, nextcolsum;
369
#else
370
  register JLONG thiscolsum, lastcolsum, nextcolsum;
371
#endif
372
2.41M
  register JDIMENSION colctr;
373
2.41M
  int inrow, outrow, v;
374
375
2.41M
  inrow = outrow = 0;
376
5.84M
  while (outrow < cinfo->max_v_samp_factor) {
377
10.2M
    for (v = 0; v < 2; v++) {
378
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
379
6.86M
      inptr0 = input_data[inrow];
380
6.86M
      if (v == 0)               /* next nearest is row above */
381
3.43M
        inptr1 = input_data[inrow - 1];
382
3.43M
      else                      /* next nearest is row below */
383
3.43M
        inptr1 = input_data[inrow + 1];
384
6.86M
      outptr = output_data[outrow++];
385
386
      /* Special case for first column */
387
6.86M
      thiscolsum = (*inptr0++) * 3 + (*inptr1++);
388
6.86M
      nextcolsum = (*inptr0++) * 3 + (*inptr1++);
389
6.86M
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 8) >> 4);
390
6.86M
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
391
6.86M
      lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
392
393
163M
      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
156M
        nextcolsum = (*inptr0++) * 3 + (*inptr1++);
398
156M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
399
156M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
400
156M
        lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
401
156M
      }
402
403
      /* Special case for last column */
404
6.86M
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
405
6.86M
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 7) >> 4);
406
6.86M
    }
407
3.43M
    inrow++;
408
3.43M
  }
409
2.41M
}
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
6.76k
{
419
6.76k
  my_upsample_ptr upsample;
420
6.76k
  int ci;
421
6.76k
  jpeg_component_info *compptr;
422
6.76k
  boolean need_buffer, do_fancy;
423
6.76k
  int h_in_group, v_in_group, h_out_group, v_out_group;
424
425
6.76k
#ifdef D_LOSSLESS_SUPPORTED
426
6.76k
  if (cinfo->master->lossless) {
427
#if BITS_IN_JSAMPLE == 8
428
624
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
429
#else
430
1.13k
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
431
1.13k
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
432
0
#endif
433
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
434
1.75k
  } else
435
5.01k
#endif
436
5.01k
  {
437
5.01k
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
438
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
439
5.01k
  }
440
441
6.76k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
442
6.76k
    upsample = (my_upsample_ptr)
443
6.76k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
444
6.76k
                                  sizeof(my_upsampler));
445
6.76k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
446
6.76k
    upsample->pub.start_pass = start_pass_upsample;
447
6.76k
    upsample->pub._upsample = sep_upsample;
448
6.76k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
449
6.76k
  } else
450
0
    upsample = (my_upsample_ptr)cinfo->upsample;
451
452
6.76k
  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
6.76k
  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
24.1k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
464
17.3k
       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
17.3k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
469
17.3k
                 cinfo->_min_DCT_scaled_size;
470
17.3k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
471
17.3k
                 cinfo->_min_DCT_scaled_size;
472
17.3k
    h_out_group = cinfo->max_h_samp_factor;
473
17.3k
    v_out_group = cinfo->max_v_samp_factor;
474
17.3k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
475
17.3k
    need_buffer = TRUE;
476
17.3k
    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
17.3k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
481
      /* Fullsize components can be processed without any work. */
482
7.90k
      upsample->methods[ci] = fullsize_upsample;
483
7.90k
      need_buffer = FALSE;
484
9.47k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
485
      /* Special cases for 2h1v upsampling */
486
1.09k
      if (do_fancy && compptr->downsampled_width > 2) {
487
#ifdef WITH_SIMD
488
274
        if (jsimd_can_h2v1_fancy_upsample())
489
0
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
490
274
        else
491
274
#endif
492
274
          upsample->methods[ci] = h2v1_fancy_upsample;
493
797
      } else {
494
#ifdef WITH_SIMD
495
318
        if (jsimd_can_h2v1_upsample())
496
0
          upsample->methods[ci] = jsimd_h2v1_upsample;
497
318
        else
498
318
#endif
499
318
          upsample->methods[ci] = h2v1_upsample;
500
797
      }
501
8.37k
    } else if (h_in_group == h_out_group &&
502
1.91k
               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
398
        upsample->methods[ci] = h1v2_fancy_upsample;
512
398
      upsample->pub.need_context_rows = TRUE;
513
7.97k
    } else if (h_in_group * 2 == h_out_group &&
514
5.61k
               v_in_group * 2 == v_out_group) {
515
      /* Special cases for 2h2v upsampling */
516
4.58k
      if (do_fancy && compptr->downsampled_width > 2) {
517
#ifdef WITH_SIMD
518
3.09k
        if (jsimd_can_h2v2_fancy_upsample())
519
0
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
520
3.09k
        else
521
3.09k
#endif
522
3.09k
          upsample->methods[ci] = h2v2_fancy_upsample;
523
3.22k
        upsample->pub.need_context_rows = TRUE;
524
3.22k
      } else {
525
#ifdef WITH_SIMD
526
496
        if (jsimd_can_h2v2_upsample())
527
0
          upsample->methods[ci] = jsimd_h2v2_upsample;
528
496
        else
529
496
#endif
530
496
          upsample->methods[ci] = h2v2_upsample;
531
1.36k
      }
532
4.58k
    } else if ((h_out_group % h_in_group) == 0 &&
533
3.35k
               (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
3.32k
        upsample->methods[ci] = int_upsample;
541
3.32k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
542
3.32k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
543
3.32k
    } else
544
59
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
545
17.3k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
546
9.41k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
547
9.41k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
548
9.41k
         (JDIMENSION)jround_up((long)cinfo->output_width,
549
9.41k
                               (long)cinfo->max_h_samp_factor),
550
9.41k
         (JDIMENSION)cinfo->max_v_samp_factor);
551
9.41k
    }
552
17.3k
  }
553
6.76k
}
jinit_upsampler
Line
Count
Source
418
4.99k
{
419
4.99k
  my_upsample_ptr upsample;
420
4.99k
  int ci;
421
4.99k
  jpeg_component_info *compptr;
422
4.99k
  boolean need_buffer, do_fancy;
423
4.99k
  int h_in_group, v_in_group, h_out_group, v_out_group;
424
425
4.99k
#ifdef D_LOSSLESS_SUPPORTED
426
4.99k
  if (cinfo->master->lossless) {
427
624
#if BITS_IN_JSAMPLE == 8
428
624
    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
624
  } else
435
4.36k
#endif
436
4.36k
  {
437
4.36k
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
438
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
439
4.36k
  }
440
441
4.99k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
442
4.99k
    upsample = (my_upsample_ptr)
443
4.99k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
444
4.99k
                                  sizeof(my_upsampler));
445
4.99k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
446
4.99k
    upsample->pub.start_pass = start_pass_upsample;
447
4.99k
    upsample->pub._upsample = sep_upsample;
448
4.99k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
449
4.99k
  } else
450
0
    upsample = (my_upsample_ptr)cinfo->upsample;
451
452
4.99k
  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.99k
  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.0k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
464
12.0k
       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.0k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
469
12.0k
                 cinfo->_min_DCT_scaled_size;
470
12.0k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
471
12.0k
                 cinfo->_min_DCT_scaled_size;
472
12.0k
    h_out_group = cinfo->max_h_samp_factor;
473
12.0k
    v_out_group = cinfo->max_v_samp_factor;
474
12.0k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
475
12.0k
    need_buffer = TRUE;
476
12.0k
    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.0k
    } 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.68k
      upsample->methods[ci] = fullsize_upsample;
483
5.68k
      need_buffer = FALSE;
484
6.41k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
485
      /* Special cases for 2h1v upsampling */
486
592
      if (do_fancy && compptr->downsampled_width > 2) {
487
274
#ifdef WITH_SIMD
488
274
        if (jsimd_can_h2v1_fancy_upsample())
489
0
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
490
274
        else
491
274
#endif
492
274
          upsample->methods[ci] = h2v1_fancy_upsample;
493
318
      } else {
494
318
#ifdef WITH_SIMD
495
318
        if (jsimd_can_h2v1_upsample())
496
0
          upsample->methods[ci] = jsimd_h2v1_upsample;
497
318
        else
498
318
#endif
499
318
          upsample->methods[ci] = h2v1_upsample;
500
318
      }
501
5.81k
    } else if (h_in_group == h_out_group &&
502
1.26k
               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
179
        upsample->methods[ci] = h1v2_fancy_upsample;
512
179
      upsample->pub.need_context_rows = TRUE;
513
5.64k
    } else if (h_in_group * 2 == h_out_group &&
514
4.01k
               v_in_group * 2 == v_out_group) {
515
      /* Special cases for 2h2v upsampling */
516
3.58k
      if (do_fancy && compptr->downsampled_width > 2) {
517
3.09k
#ifdef WITH_SIMD
518
3.09k
        if (jsimd_can_h2v2_fancy_upsample())
519
0
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
520
3.09k
        else
521
3.09k
#endif
522
3.09k
          upsample->methods[ci] = h2v2_fancy_upsample;
523
3.09k
        upsample->pub.need_context_rows = TRUE;
524
3.09k
      } else {
525
496
#ifdef WITH_SIMD
526
496
        if (jsimd_can_h2v2_upsample())
527
0
          upsample->methods[ci] = jsimd_h2v2_upsample;
528
496
        else
529
496
#endif
530
496
          upsample->methods[ci] = h2v2_upsample;
531
496
      }
532
3.58k
    } else if ((h_out_group % h_in_group) == 0 &&
533
2.04k
               (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.03k
        upsample->methods[ci] = int_upsample;
541
2.03k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
542
2.03k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
543
2.03k
    } else
544
16
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
545
12.0k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
546
6.39k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
547
6.39k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
548
6.39k
         (JDIMENSION)jround_up((long)cinfo->output_width,
549
6.39k
                               (long)cinfo->max_h_samp_factor),
550
6.39k
         (JDIMENSION)cinfo->max_v_samp_factor);
551
6.39k
    }
552
12.0k
  }
553
4.99k
}
j12init_upsampler
Line
Count
Source
418
1.17k
{
419
1.17k
  my_upsample_ptr upsample;
420
1.17k
  int ci;
421
1.17k
  jpeg_component_info *compptr;
422
1.17k
  boolean need_buffer, do_fancy;
423
1.17k
  int h_in_group, v_in_group, h_out_group, v_out_group;
424
425
1.17k
#ifdef D_LOSSLESS_SUPPORTED
426
1.17k
  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
533
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
431
533
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
432
0
#endif
433
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
434
533
  } else
435
641
#endif
436
641
  {
437
641
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
438
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
439
641
  }
440
441
1.17k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
442
1.17k
    upsample = (my_upsample_ptr)
443
1.17k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
444
1.17k
                                  sizeof(my_upsampler));
445
1.17k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
446
1.17k
    upsample->pub.start_pass = start_pass_upsample;
447
1.17k
    upsample->pub._upsample = sep_upsample;
448
1.17k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
449
1.17k
  } else
450
0
    upsample = (my_upsample_ptr)cinfo->upsample;
451
452
1.17k
  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
1.17k
  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
4.61k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
464
3.43k
       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
3.43k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
469
3.43k
                 cinfo->_min_DCT_scaled_size;
470
3.43k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
471
3.43k
                 cinfo->_min_DCT_scaled_size;
472
3.43k
    h_out_group = cinfo->max_h_samp_factor;
473
3.43k
    v_out_group = cinfo->max_v_samp_factor;
474
3.43k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
475
3.43k
    need_buffer = TRUE;
476
3.43k
    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
3.43k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
481
      /* Fullsize components can be processed without any work. */
482
1.60k
      upsample->methods[ci] = fullsize_upsample;
483
1.60k
      need_buffer = FALSE;
484
1.82k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
485
      /* Special cases for 2h1v upsampling */
486
254
      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
26
          upsample->methods[ci] = h2v1_fancy_upsample;
493
228
      } else {
494
#ifdef WITH_SIMD
495
        if (jsimd_can_h2v1_upsample())
496
          upsample->methods[ci] = jsimd_h2v1_upsample;
497
        else
498
#endif
499
228
          upsample->methods[ci] = h2v1_upsample;
500
228
      }
501
1.57k
    } else if (h_in_group == h_out_group &&
502
419
               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
219
        upsample->methods[ci] = h1v2_fancy_upsample;
512
219
      upsample->pub.need_context_rows = TRUE;
513
1.35k
    } else if (h_in_group * 2 == h_out_group &&
514
908
               v_in_group * 2 == v_out_group) {
515
      /* Special cases for 2h2v upsampling */
516
516
      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
127
          upsample->methods[ci] = h2v2_fancy_upsample;
523
127
        upsample->pub.need_context_rows = TRUE;
524
389
      } else {
525
#ifdef WITH_SIMD
526
        if (jsimd_can_h2v2_upsample())
527
          upsample->methods[ci] = jsimd_h2v2_upsample;
528
        else
529
#endif
530
389
          upsample->methods[ci] = h2v2_upsample;
531
389
      }
532
840
    } else if ((h_out_group % h_in_group) == 0 &&
533
821
               (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
811
        upsample->methods[ci] = int_upsample;
541
811
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
542
811
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
543
811
    } else
544
29
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
545
3.43k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
546
1.80k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
547
1.80k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
548
1.80k
         (JDIMENSION)jround_up((long)cinfo->output_width,
549
1.80k
                               (long)cinfo->max_h_samp_factor),
550
1.80k
         (JDIMENSION)cinfo->max_v_samp_factor);
551
1.80k
    }
552
3.43k
  }
553
1.17k
}
j16init_upsampler
Line
Count
Source
418
597
{
419
597
  my_upsample_ptr upsample;
420
597
  int ci;
421
597
  jpeg_component_info *compptr;
422
597
  boolean need_buffer, do_fancy;
423
597
  int h_in_group, v_in_group, h_out_group, v_out_group;
424
425
597
#ifdef D_LOSSLESS_SUPPORTED
426
597
  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
597
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
431
597
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
432
0
#endif
433
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
434
597
  } 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
597
  if (!cinfo->master->jinit_upsampler_no_alloc) {
442
597
    upsample = (my_upsample_ptr)
443
597
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
444
597
                                  sizeof(my_upsampler));
445
597
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
446
597
    upsample->pub.start_pass = start_pass_upsample;
447
597
    upsample->pub._upsample = sep_upsample;
448
597
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
449
597
  } else
450
0
    upsample = (my_upsample_ptr)cinfo->upsample;
451
452
597
  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
597
  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.43k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
464
1.83k
       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.83k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
469
1.83k
                 cinfo->_min_DCT_scaled_size;
470
1.83k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
471
1.83k
                 cinfo->_min_DCT_scaled_size;
472
1.83k
    h_out_group = cinfo->max_h_samp_factor;
473
1.83k
    v_out_group = cinfo->max_v_samp_factor;
474
1.83k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
475
1.83k
    need_buffer = TRUE;
476
1.83k
    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.83k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
481
      /* Fullsize components can be processed without any work. */
482
607
      upsample->methods[ci] = fullsize_upsample;
483
607
      need_buffer = FALSE;
484
1.23k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
485
      /* Special cases for 2h1v upsampling */
486
251
      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
251
      } else {
494
#ifdef WITH_SIMD
495
        if (jsimd_can_h2v1_upsample())
496
          upsample->methods[ci] = jsimd_h2v1_upsample;
497
        else
498
#endif
499
251
          upsample->methods[ci] = h2v1_upsample;
500
251
      }
501
979
    } else if (h_in_group == h_out_group &&
502
236
               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
979
    } else if (h_in_group * 2 == h_out_group &&
514
692
               v_in_group * 2 == v_out_group) {
515
      /* Special cases for 2h2v upsampling */
516
482
      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
482
      } else {
525
#ifdef WITH_SIMD
526
        if (jsimd_can_h2v2_upsample())
527
          upsample->methods[ci] = jsimd_h2v2_upsample;
528
        else
529
#endif
530
482
          upsample->methods[ci] = h2v2_upsample;
531
482
      }
532
497
    } else if ((h_out_group % h_in_group) == 0 &&
533
489
               (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
483
        upsample->methods[ci] = int_upsample;
541
483
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
542
483
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
543
483
    } else
544
14
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
545
1.83k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
546
1.21k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
547
1.21k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
548
1.21k
         (JDIMENSION)jround_up((long)cinfo->output_width,
549
1.21k
                               (long)cinfo->max_h_samp_factor),
550
1.21k
         (JDIMENSION)cinfo->max_v_samp_factor);
551
1.21k
    }
552
1.83k
  }
553
597
}
554
555
#endif /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */