Coverage Report

Created: 2025-08-28 06:31

/src/libjpeg-turbo/src/jdsample.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jdsample.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1991-1996, Thomas G. Lane.
6
 * libjpeg-turbo Modifications:
7
 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
 * Copyright (C) 2010, 2015-2016, 2022, 2024-2025, D. R. Commander.
9
 * Copyright (C) 2014, MIPS Technologies, Inc., California.
10
 * Copyright (C) 2015, Google, Inc.
11
 * Copyright (C) 2019-2020, Arm Limited.
12
 * For conditions of distribution and use, see the accompanying README.ijg
13
 * file.
14
 *
15
 * This file contains upsampling routines.
16
 *
17
 * Upsampling input data is counted in "row groups".  A row group
18
 * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
19
 * sample rows of each component.  Upsampling will normally produce
20
 * max_v_samp_factor pixel rows from each row group (but this could vary
21
 * if the upsampler is applying a scale factor of its own).
22
 *
23
 * An excellent reference for image resampling is
24
 *   Digital Image Warping, George Wolberg, 1990.
25
 *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
26
 */
27
28
#include "jinclude.h"
29
#include "jdsample.h"
30
#include "jsimd.h"
31
#include "jpegapicomp.h"
32
33
34
35
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
36
37
/*
38
 * Initialize for an upsampling pass.
39
 */
40
41
METHODDEF(void)
42
start_pass_upsample(j_decompress_ptr cinfo)
43
2.51k
{
44
2.51k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
2.51k
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
2.51k
  upsample->rows_to_go = cinfo->output_height;
50
2.51k
}
jdsample-8.c:start_pass_upsample
Line
Count
Source
43
2.37k
{
44
2.37k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
2.37k
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
2.37k
  upsample->rows_to_go = cinfo->output_height;
50
2.37k
}
jdsample-12.c:start_pass_upsample
Line
Count
Source
43
118
{
44
118
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
118
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
118
  upsample->rows_to_go = cinfo->output_height;
50
118
}
jdsample-16.c:start_pass_upsample
Line
Count
Source
43
25
{
44
25
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
45
46
  /* Mark the conversion buffer empty */
47
25
  upsample->next_row_out = cinfo->max_v_samp_factor;
48
  /* Initialize total-height counter for detecting bottom of image */
49
25
  upsample->rows_to_go = cinfo->output_height;
50
25
}
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
5.81M
{
67
5.81M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
68
5.81M
  int ci;
69
5.81M
  jpeg_component_info *compptr;
70
5.81M
  JDIMENSION num_rows;
71
72
  /* Fill the conversion buffer, if it's empty */
73
5.81M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
74
7.64M
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
75
4.30M
         ci++, compptr++) {
76
      /* Invoke per-component upsample method.  Notice we pass a POINTER
77
       * to color_buf[ci], so that fullsize_upsample can change it.
78
       */
79
4.30M
      (*upsample->methods[ci]) (cinfo, compptr,
80
4.30M
        input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
81
4.30M
        upsample->color_buf + ci);
82
4.30M
    }
83
3.34M
    upsample->next_row_out = 0;
84
3.34M
  }
85
86
  /* Color-convert and emit rows */
87
88
  /* How many we have in the buffer: */
89
5.81M
  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
5.81M
  if (num_rows > upsample->rows_to_go)
94
597
    num_rows = upsample->rows_to_go;
95
  /* And not more than what the client can accept: */
96
5.81M
  out_rows_avail -= *out_row_ctr;
97
5.81M
  if (num_rows > out_rows_avail)
98
2.47M
    num_rows = out_rows_avail;
99
100
5.81M
  (*cinfo->cconvert->_color_convert) (cinfo, upsample->color_buf,
101
5.81M
                                      (JDIMENSION)upsample->next_row_out,
102
5.81M
                                      output_buf + *out_row_ctr,
103
5.81M
                                      (int)num_rows);
104
105
  /* Adjust counts */
106
5.81M
  *out_row_ctr += num_rows;
107
5.81M
  upsample->rows_to_go -= num_rows;
108
5.81M
  upsample->next_row_out += num_rows;
109
  /* When the buffer is emptied, declare this input row group consumed */
110
5.81M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
111
3.34M
    (*in_row_group_ctr)++;
112
5.81M
}
jdsample-8.c:sep_upsample
Line
Count
Source
66
5.81M
{
67
5.81M
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
68
5.81M
  int ci;
69
5.81M
  jpeg_component_info *compptr;
70
5.81M
  JDIMENSION num_rows;
71
72
  /* Fill the conversion buffer, if it's empty */
73
5.81M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
74
7.64M
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
75
4.30M
         ci++, compptr++) {
76
      /* Invoke per-component upsample method.  Notice we pass a POINTER
77
       * to color_buf[ci], so that fullsize_upsample can change it.
78
       */
79
4.30M
      (*upsample->methods[ci]) (cinfo, compptr,
80
4.30M
        input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
81
4.30M
        upsample->color_buf + ci);
82
4.30M
    }
83
3.34M
    upsample->next_row_out = 0;
84
3.34M
  }
85
86
  /* Color-convert and emit rows */
87
88
  /* How many we have in the buffer: */
89
5.81M
  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
5.81M
  if (num_rows > upsample->rows_to_go)
94
597
    num_rows = upsample->rows_to_go;
95
  /* And not more than what the client can accept: */
96
5.81M
  out_rows_avail -= *out_row_ctr;
97
5.81M
  if (num_rows > out_rows_avail)
98
2.47M
    num_rows = out_rows_avail;
99
100
5.81M
  (*cinfo->cconvert->_color_convert) (cinfo, upsample->color_buf,
101
5.81M
                                      (JDIMENSION)upsample->next_row_out,
102
5.81M
                                      output_buf + *out_row_ctr,
103
5.81M
                                      (int)num_rows);
104
105
  /* Adjust counts */
106
5.81M
  *out_row_ctr += num_rows;
107
5.81M
  upsample->rows_to_go -= num_rows;
108
5.81M
  upsample->next_row_out += num_rows;
109
  /* When the buffer is emptied, declare this input row group consumed */
110
5.81M
  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
111
3.34M
    (*in_row_group_ctr)++;
112
5.81M
}
Unexecuted instantiation: jdsample-12.c:sep_upsample
Unexecuted instantiation: jdsample-16.c:sep_upsample
113
114
115
/*
116
 * These are the routines invoked by sep_upsample to upsample pixel values
117
 * of a single component.  One row group is processed per call.
118
 */
119
120
121
/*
122
 * For full-size components, we just make color_buf[ci] point at the
123
 * input buffer, and thus avoid copying any data.  Note that this is
124
 * safe only because sep_upsample doesn't declare the input row group
125
 * "consumed" until we are done color converting and emitting it.
126
 */
127
128
METHODDEF(void)
129
fullsize_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
130
                  _JSAMPARRAY input_data, _JSAMPARRAY *output_data_ptr)
131
3.01M
{
132
3.01M
  *output_data_ptr = input_data;
133
3.01M
}
jdsample-8.c:fullsize_upsample
Line
Count
Source
131
3.01M
{
132
3.01M
  *output_data_ptr = input_data;
133
3.01M
}
Unexecuted instantiation: jdsample-12.c:fullsize_upsample
Unexecuted instantiation: jdsample-16.c:fullsize_upsample
134
135
136
/*
137
 * This is a no-op version used for "uninteresting" components.
138
 * These components will not be referenced by color conversion.
139
 */
140
141
METHODDEF(void)
142
noop_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
143
              _JSAMPARRAY input_data, _JSAMPARRAY *output_data_ptr)
144
0
{
145
0
  *output_data_ptr = NULL;      /* safety check */
146
0
}
Unexecuted instantiation: jdsample-8.c:noop_upsample
Unexecuted instantiation: jdsample-12.c:noop_upsample
Unexecuted instantiation: jdsample-16.c:noop_upsample
147
148
149
/*
150
 * This version handles any integral sampling ratios.
151
 * This is not used for typical JPEG files, so it need not be fast.
152
 * Nor, for that matter, is it particularly accurate: the algorithm is
153
 * simple replication of the input pixel onto the corresponding output
154
 * pixels.  The hi-falutin sampling literature refers to this as a
155
 * "box filter".  A box filter tends to introduce visible artifacts,
156
 * so if you are actually going to use 3:1 or 4:1 sampling ratios
157
 * you would be well advised to improve this code.
158
 */
159
160
METHODDEF(void)
161
int_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
162
             _JSAMPARRAY input_data, _JSAMPARRAY *output_data_ptr)
163
266k
{
164
266k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
165
266k
  _JSAMPARRAY output_data = *output_data_ptr;
166
266k
  register _JSAMPROW inptr, outptr;
167
266k
  register _JSAMPLE invalue;
168
266k
  register int h;
169
266k
  _JSAMPROW outend;
170
266k
  int h_expand, v_expand;
171
266k
  int inrow, outrow;
172
173
266k
  h_expand = upsample->h_expand[compptr->component_index];
174
266k
  v_expand = upsample->v_expand[compptr->component_index];
175
176
266k
  inrow = outrow = 0;
177
671k
  while (outrow < cinfo->max_v_samp_factor) {
178
    /* Generate one output row with proper horizontal expansion */
179
405k
    inptr = input_data[inrow];
180
405k
    outptr = output_data[outrow];
181
405k
    outend = outptr + cinfo->output_width;
182
17.5M
    while (outptr < outend) {
183
17.1M
      invalue = *inptr++;
184
47.2M
      for (h = h_expand; h > 0; h--) {
185
30.1M
        *outptr++ = invalue;
186
30.1M
      }
187
17.1M
    }
188
    /* Generate any additional output rows by duplicating the first one */
189
405k
    if (v_expand > 1) {
190
180k
      _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1,
191
180k
                         v_expand - 1, cinfo->output_width);
192
180k
    }
193
405k
    inrow++;
194
405k
    outrow += v_expand;
195
405k
  }
196
266k
}
jdsample-8.c:int_upsample
Line
Count
Source
163
266k
{
164
266k
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
165
266k
  _JSAMPARRAY output_data = *output_data_ptr;
166
266k
  register _JSAMPROW inptr, outptr;
167
266k
  register _JSAMPLE invalue;
168
266k
  register int h;
169
266k
  _JSAMPROW outend;
170
266k
  int h_expand, v_expand;
171
266k
  int inrow, outrow;
172
173
266k
  h_expand = upsample->h_expand[compptr->component_index];
174
266k
  v_expand = upsample->v_expand[compptr->component_index];
175
176
266k
  inrow = outrow = 0;
177
671k
  while (outrow < cinfo->max_v_samp_factor) {
178
    /* Generate one output row with proper horizontal expansion */
179
405k
    inptr = input_data[inrow];
180
405k
    outptr = output_data[outrow];
181
405k
    outend = outptr + cinfo->output_width;
182
17.5M
    while (outptr < outend) {
183
17.1M
      invalue = *inptr++;
184
47.2M
      for (h = h_expand; h > 0; h--) {
185
30.1M
        *outptr++ = invalue;
186
30.1M
      }
187
17.1M
    }
188
    /* Generate any additional output rows by duplicating the first one */
189
405k
    if (v_expand > 1) {
190
180k
      _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1,
191
180k
                         v_expand - 1, cinfo->output_width);
192
180k
    }
193
405k
    inrow++;
194
405k
    outrow += v_expand;
195
405k
  }
196
266k
}
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
82.7k
{
208
82.7k
  _JSAMPARRAY output_data = *output_data_ptr;
209
82.7k
  register _JSAMPROW inptr, outptr;
210
82.7k
  register _JSAMPLE invalue;
211
82.7k
  _JSAMPROW outend;
212
82.7k
  int inrow;
213
214
308k
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
215
225k
    inptr = input_data[inrow];
216
225k
    outptr = output_data[inrow];
217
225k
    outend = outptr + cinfo->output_width;
218
585k
    while (outptr < outend) {
219
359k
      invalue = *inptr++;
220
359k
      *outptr++ = invalue;
221
359k
      *outptr++ = invalue;
222
359k
    }
223
225k
  }
224
82.7k
}
jdsample-8.c:h2v1_upsample
Line
Count
Source
207
82.7k
{
208
82.7k
  _JSAMPARRAY output_data = *output_data_ptr;
209
82.7k
  register _JSAMPROW inptr, outptr;
210
82.7k
  register _JSAMPLE invalue;
211
82.7k
  _JSAMPROW outend;
212
82.7k
  int inrow;
213
214
308k
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
215
225k
    inptr = input_data[inrow];
216
225k
    outptr = output_data[inrow];
217
225k
    outend = outptr + cinfo->output_width;
218
585k
    while (outptr < outend) {
219
359k
      invalue = *inptr++;
220
359k
      *outptr++ = invalue;
221
359k
      *outptr++ = invalue;
222
359k
    }
223
225k
  }
224
82.7k
}
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
162k
{
236
162k
  _JSAMPARRAY output_data = *output_data_ptr;
237
162k
  register _JSAMPROW inptr, outptr;
238
162k
  register _JSAMPLE invalue;
239
162k
  _JSAMPROW outend;
240
162k
  int inrow, outrow;
241
242
162k
  inrow = outrow = 0;
243
376k
  while (outrow < cinfo->max_v_samp_factor) {
244
214k
    inptr = input_data[inrow];
245
214k
    outptr = output_data[outrow];
246
214k
    outend = outptr + cinfo->output_width;
247
499k
    while (outptr < outend) {
248
285k
      invalue = *inptr++;
249
285k
      *outptr++ = invalue;
250
285k
      *outptr++ = invalue;
251
285k
    }
252
214k
    _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1, 1,
253
214k
                       cinfo->output_width);
254
214k
    inrow++;
255
214k
    outrow += 2;
256
214k
  }
257
162k
}
jdsample-8.c:h2v2_upsample
Line
Count
Source
235
162k
{
236
162k
  _JSAMPARRAY output_data = *output_data_ptr;
237
162k
  register _JSAMPROW inptr, outptr;
238
162k
  register _JSAMPLE invalue;
239
162k
  _JSAMPROW outend;
240
162k
  int inrow, outrow;
241
242
162k
  inrow = outrow = 0;
243
376k
  while (outrow < cinfo->max_v_samp_factor) {
244
214k
    inptr = input_data[inrow];
245
214k
    outptr = output_data[outrow];
246
214k
    outend = outptr + cinfo->output_width;
247
499k
    while (outptr < outend) {
248
285k
      invalue = *inptr++;
249
285k
      *outptr++ = invalue;
250
285k
      *outptr++ = invalue;
251
285k
    }
252
214k
    _jcopy_sample_rows(output_data, outrow, output_data, outrow + 1, 1,
253
214k
                       cinfo->output_width);
254
214k
    inrow++;
255
214k
    outrow += 2;
256
214k
  }
257
162k
}
Unexecuted instantiation: jdsample-12.c:h2v2_upsample
Unexecuted instantiation: jdsample-16.c:h2v2_upsample
258
259
260
/*
261
 * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
262
 *
263
 * The upsampling algorithm is linear interpolation between pixel centers,
264
 * also known as a "triangle filter".  This is a good compromise between
265
 * speed and visual quality.  The centers of the output pixels are 1/4 and 3/4
266
 * of the way between input pixel centers.
267
 *
268
 * A note about the "bias" calculations: when rounding fractional values to
269
 * integer, we do not want to always round 0.5 up to the next integer.
270
 * If we did that, we'd introduce a noticeable bias towards larger values.
271
 * Instead, this code is arranged so that 0.5 will be rounded up or down at
272
 * alternate pixel locations (a simple ordered dither pattern).
273
 */
274
275
METHODDEF(void)
276
h2v1_fancy_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
277
                    _JSAMPARRAY input_data, _JSAMPARRAY *output_data_ptr)
278
227k
{
279
227k
  _JSAMPARRAY output_data = *output_data_ptr;
280
227k
  register _JSAMPROW inptr, outptr;
281
227k
  register int invalue;
282
227k
  register JDIMENSION colctr;
283
227k
  int inrow;
284
285
806k
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
286
579k
    inptr = input_data[inrow];
287
579k
    outptr = output_data[inrow];
288
    /* Special case for first column */
289
579k
    invalue = *inptr++;
290
579k
    *outptr++ = (_JSAMPLE)invalue;
291
579k
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[0] + 2) >> 2);
292
293
36.1M
    for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
294
      /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
295
35.5M
      invalue = (*inptr++) * 3;
296
35.5M
      *outptr++ = (_JSAMPLE)((invalue + inptr[-2] + 1) >> 2);
297
35.5M
      *outptr++ = (_JSAMPLE)((invalue + inptr[0] + 2) >> 2);
298
35.5M
    }
299
300
    /* Special case for last column */
301
579k
    invalue = *inptr;
302
579k
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[-1] + 1) >> 2);
303
579k
    *outptr++ = (_JSAMPLE)invalue;
304
579k
  }
305
227k
}
jdsample-8.c:h2v1_fancy_upsample
Line
Count
Source
278
227k
{
279
227k
  _JSAMPARRAY output_data = *output_data_ptr;
280
227k
  register _JSAMPROW inptr, outptr;
281
227k
  register int invalue;
282
227k
  register JDIMENSION colctr;
283
227k
  int inrow;
284
285
806k
  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
286
579k
    inptr = input_data[inrow];
287
579k
    outptr = output_data[inrow];
288
    /* Special case for first column */
289
579k
    invalue = *inptr++;
290
579k
    *outptr++ = (_JSAMPLE)invalue;
291
579k
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[0] + 2) >> 2);
292
293
36.1M
    for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
294
      /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
295
35.5M
      invalue = (*inptr++) * 3;
296
35.5M
      *outptr++ = (_JSAMPLE)((invalue + inptr[-2] + 1) >> 2);
297
35.5M
      *outptr++ = (_JSAMPLE)((invalue + inptr[0] + 2) >> 2);
298
35.5M
    }
299
300
    /* Special case for last column */
301
579k
    invalue = *inptr;
302
579k
    *outptr++ = (_JSAMPLE)((invalue * 3 + inptr[-1] + 1) >> 2);
303
579k
    *outptr++ = (_JSAMPLE)invalue;
304
579k
  }
305
227k
}
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
488k
{
319
488k
  _JSAMPARRAY output_data = *output_data_ptr;
320
488k
  _JSAMPROW inptr0, inptr1, outptr;
321
#if BITS_IN_JSAMPLE == 8
322
  int thiscolsum, bias;
323
#else
324
  JLONG thiscolsum, bias;
325
#endif
326
488k
  JDIMENSION colctr;
327
488k
  int inrow, outrow, v;
328
329
488k
  inrow = outrow = 0;
330
1.02M
  while (outrow < cinfo->max_v_samp_factor) {
331
1.60M
    for (v = 0; v < 2; v++) {
332
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
333
1.06M
      inptr0 = input_data[inrow];
334
1.06M
      if (v == 0) {             /* next nearest is row above */
335
534k
        inptr1 = input_data[inrow - 1];
336
534k
        bias = 1;
337
534k
      } else {                  /* next nearest is row below */
338
534k
        inptr1 = input_data[inrow + 1];
339
534k
        bias = 2;
340
534k
      }
341
1.06M
      outptr = output_data[outrow++];
342
343
64.9M
      for (colctr = 0; colctr < compptr->downsampled_width; colctr++) {
344
63.8M
        thiscolsum = (*inptr0++) * 3 + (*inptr1++);
345
63.8M
        *outptr++ = (_JSAMPLE)((thiscolsum + bias) >> 2);
346
63.8M
      }
347
1.06M
    }
348
534k
    inrow++;
349
534k
  }
350
488k
}
jdsample-8.c:h1v2_fancy_upsample
Line
Count
Source
318
488k
{
319
488k
  _JSAMPARRAY output_data = *output_data_ptr;
320
488k
  _JSAMPROW inptr0, inptr1, outptr;
321
488k
#if BITS_IN_JSAMPLE == 8
322
488k
  int thiscolsum, bias;
323
#else
324
  JLONG thiscolsum, bias;
325
#endif
326
488k
  JDIMENSION colctr;
327
488k
  int inrow, outrow, v;
328
329
488k
  inrow = outrow = 0;
330
1.02M
  while (outrow < cinfo->max_v_samp_factor) {
331
1.60M
    for (v = 0; v < 2; v++) {
332
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
333
1.06M
      inptr0 = input_data[inrow];
334
1.06M
      if (v == 0) {             /* next nearest is row above */
335
534k
        inptr1 = input_data[inrow - 1];
336
534k
        bias = 1;
337
534k
      } else {                  /* next nearest is row below */
338
534k
        inptr1 = input_data[inrow + 1];
339
534k
        bias = 2;
340
534k
      }
341
1.06M
      outptr = output_data[outrow++];
342
343
64.9M
      for (colctr = 0; colctr < compptr->downsampled_width; colctr++) {
344
63.8M
        thiscolsum = (*inptr0++) * 3 + (*inptr1++);
345
63.8M
        *outptr++ = (_JSAMPLE)((thiscolsum + bias) >> 2);
346
63.8M
      }
347
1.06M
    }
348
534k
    inrow++;
349
534k
  }
350
488k
}
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
67.8k
{
365
67.8k
  _JSAMPARRAY output_data = *output_data_ptr;
366
67.8k
  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
67.8k
  register JDIMENSION colctr;
373
67.8k
  int inrow, outrow, v;
374
375
67.8k
  inrow = outrow = 0;
376
180k
  while (outrow < cinfo->max_v_samp_factor) {
377
336k
    for (v = 0; v < 2; v++) {
378
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
379
224k
      inptr0 = input_data[inrow];
380
224k
      if (v == 0)               /* next nearest is row above */
381
112k
        inptr1 = input_data[inrow - 1];
382
112k
      else                      /* next nearest is row below */
383
112k
        inptr1 = input_data[inrow + 1];
384
224k
      outptr = output_data[outrow++];
385
386
      /* Special case for first column */
387
224k
      thiscolsum = (*inptr0++) * 3 + (*inptr1++);
388
224k
      nextcolsum = (*inptr0++) * 3 + (*inptr1++);
389
224k
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 8) >> 4);
390
224k
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
391
224k
      lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
392
393
26.4M
      for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
394
        /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
395
        /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
396
26.2M
        nextcolsum = (*inptr0++) * 3 + (*inptr1++);
397
26.2M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
398
26.2M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
399
26.2M
        lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
400
26.2M
      }
401
402
      /* Special case for last column */
403
224k
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
404
224k
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 7) >> 4);
405
224k
    }
406
112k
    inrow++;
407
112k
  }
408
67.8k
}
jdsample-8.c:h2v2_fancy_upsample
Line
Count
Source
364
67.8k
{
365
67.8k
  _JSAMPARRAY output_data = *output_data_ptr;
366
67.8k
  register _JSAMPROW inptr0, inptr1, outptr;
367
67.8k
#if BITS_IN_JSAMPLE == 8
368
67.8k
  register int thiscolsum, lastcolsum, nextcolsum;
369
#else
370
  register JLONG thiscolsum, lastcolsum, nextcolsum;
371
#endif
372
67.8k
  register JDIMENSION colctr;
373
67.8k
  int inrow, outrow, v;
374
375
67.8k
  inrow = outrow = 0;
376
180k
  while (outrow < cinfo->max_v_samp_factor) {
377
336k
    for (v = 0; v < 2; v++) {
378
      /* inptr0 points to nearest input row, inptr1 points to next nearest */
379
224k
      inptr0 = input_data[inrow];
380
224k
      if (v == 0)               /* next nearest is row above */
381
112k
        inptr1 = input_data[inrow - 1];
382
112k
      else                      /* next nearest is row below */
383
112k
        inptr1 = input_data[inrow + 1];
384
224k
      outptr = output_data[outrow++];
385
386
      /* Special case for first column */
387
224k
      thiscolsum = (*inptr0++) * 3 + (*inptr1++);
388
224k
      nextcolsum = (*inptr0++) * 3 + (*inptr1++);
389
224k
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 8) >> 4);
390
224k
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
391
224k
      lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
392
393
26.4M
      for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
394
        /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
395
        /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
396
26.2M
        nextcolsum = (*inptr0++) * 3 + (*inptr1++);
397
26.2M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
398
26.2M
        *outptr++ = (_JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);
399
26.2M
        lastcolsum = thiscolsum;  thiscolsum = nextcolsum;
400
26.2M
      }
401
402
      /* Special case for last column */
403
224k
      *outptr++ = (_JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);
404
224k
      *outptr++ = (_JSAMPLE)((thiscolsum * 4 + 7) >> 4);
405
224k
    }
406
112k
    inrow++;
407
112k
  }
408
67.8k
}
Unexecuted instantiation: jdsample-12.c:h2v2_fancy_upsample
Unexecuted instantiation: jdsample-16.c:h2v2_fancy_upsample
409
410
411
/*
412
 * Module initialization routine for upsampling.
413
 */
414
415
GLOBAL(void)
416
_jinit_upsampler(j_decompress_ptr cinfo)
417
6.10k
{
418
6.10k
  my_upsample_ptr upsample;
419
6.10k
  int ci;
420
6.10k
  jpeg_component_info *compptr;
421
6.10k
  boolean need_buffer, do_fancy;
422
6.10k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
6.10k
#ifdef D_LOSSLESS_SUPPORTED
425
6.10k
  if (cinfo->master->lossless) {
426
#if BITS_IN_JSAMPLE == 8
427
1.24k
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
428
#else
429
1.68k
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
430
1.68k
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
431
0
#endif
432
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
433
2.93k
  } else
434
3.17k
#endif
435
3.17k
  {
436
3.17k
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
437
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
438
3.17k
  }
439
440
6.10k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
441
6.10k
    upsample = (my_upsample_ptr)
442
6.10k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
443
6.10k
                                  sizeof(my_upsampler));
444
6.10k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
445
6.10k
    upsample->pub.start_pass = start_pass_upsample;
446
6.10k
    upsample->pub._upsample = sep_upsample;
447
6.10k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
448
6.10k
  } else
449
0
    upsample = (my_upsample_ptr)cinfo->upsample;
450
451
6.10k
  if (cinfo->CCIR601_sampling)  /* this isn't supported */
452
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
453
454
  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
455
   * so don't ask for it.
456
   */
457
6.10k
  do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
458
459
  /* Verify we can handle the sampling factors, select per-component methods,
460
   * and create storage as needed.
461
   */
462
17.5k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
463
11.4k
       ci++, compptr++) {
464
    /* Compute size of an "input group" after IDCT scaling.  This many samples
465
     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
466
     */
467
11.4k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
468
11.4k
                 cinfo->_min_DCT_scaled_size;
469
11.4k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
470
11.4k
                 cinfo->_min_DCT_scaled_size;
471
11.4k
    h_out_group = cinfo->max_h_samp_factor;
472
11.4k
    v_out_group = cinfo->max_v_samp_factor;
473
11.4k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
474
11.4k
    need_buffer = TRUE;
475
11.4k
    if (!compptr->component_needed) {
476
      /* Don't bother to upsample an uninteresting component. */
477
0
      upsample->methods[ci] = noop_upsample;
478
0
      need_buffer = FALSE;
479
11.4k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
480
      /* Fullsize components can be processed without any work. */
481
4.44k
      upsample->methods[ci] = fullsize_upsample;
482
4.44k
      need_buffer = FALSE;
483
6.97k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
484
      /* Special cases for 2h1v upsampling */
485
904
      if (do_fancy && compptr->downsampled_width > 2) {
486
#ifdef WITH_SIMD
487
204
        if (jsimd_can_h2v1_fancy_upsample())
488
0
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
489
204
        else
490
204
#endif
491
204
          upsample->methods[ci] = h2v1_fancy_upsample;
492
586
      } else {
493
#ifdef WITH_SIMD
494
279
        if (jsimd_can_h2v1_upsample())
495
0
          upsample->methods[ci] = jsimd_h2v1_upsample;
496
279
        else
497
279
#endif
498
279
          upsample->methods[ci] = h2v1_upsample;
499
586
      }
500
6.07k
    } else if (h_in_group == h_out_group &&
501
6.07k
               v_in_group * 2 == v_out_group && do_fancy) {
502
      /* Non-fancy upsampling is handled by the generic method */
503
#if defined(WITH_SIMD) && (defined(__arm__) || defined(__aarch64__) || \
504
                           defined(_M_ARM) || defined(_M_ARM64) || \
505
                           defined(_M_ARM64EC))
506
      if (jsimd_can_h1v2_fancy_upsample())
507
        upsample->methods[ci] = jsimd_h1v2_fancy_upsample;
508
      else
509
#endif
510
696
        upsample->methods[ci] = h1v2_fancy_upsample;
511
696
      upsample->pub.need_context_rows = TRUE;
512
5.37k
    } else if (h_in_group * 2 == h_out_group &&
513
5.37k
               v_in_group * 2 == v_out_group) {
514
      /* Special cases for 2h2v upsampling */
515
648
      if (do_fancy && compptr->downsampled_width > 2) {
516
#ifdef WITH_SIMD
517
154
        if (jsimd_can_h2v2_fancy_upsample())
518
0
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
519
154
        else
520
154
#endif
521
154
          upsample->methods[ci] = h2v2_fancy_upsample;
522
210
        upsample->pub.need_context_rows = TRUE;
523
438
      } else {
524
#ifdef WITH_SIMD
525
240
        if (jsimd_can_h2v2_upsample())
526
0
          upsample->methods[ci] = jsimd_h2v2_upsample;
527
240
        else
528
240
#endif
529
240
          upsample->methods[ci] = h2v2_upsample;
530
438
      }
531
4.73k
    } else if ((h_out_group % h_in_group) == 0 &&
532
4.73k
               (v_out_group % v_in_group) == 0) {
533
      /* Generic integral-factors upsampling method */
534
#if defined(WITH_SIMD) && defined(__mips__)
535
      if (jsimd_can_int_upsample())
536
        upsample->methods[ci] = jsimd_int_upsample;
537
      else
538
#endif
539
4.70k
        upsample->methods[ci] = int_upsample;
540
4.70k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
541
4.70k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
542
4.70k
    } else
543
29
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
544
11.4k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
545
6.94k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
546
6.94k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
547
6.94k
         (JDIMENSION)jround_up((long)cinfo->output_width,
548
6.94k
                               (long)cinfo->max_h_samp_factor),
549
6.94k
         (JDIMENSION)cinfo->max_v_samp_factor);
550
6.94k
    }
551
11.4k
  }
552
6.10k
}
jinit_upsampler
Line
Count
Source
417
3.95k
{
418
3.95k
  my_upsample_ptr upsample;
419
3.95k
  int ci;
420
3.95k
  jpeg_component_info *compptr;
421
3.95k
  boolean need_buffer, do_fancy;
422
3.95k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
3.95k
#ifdef D_LOSSLESS_SUPPORTED
425
3.95k
  if (cinfo->master->lossless) {
426
1.24k
#if BITS_IN_JSAMPLE == 8
427
1.24k
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
428
#else
429
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
430
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
431
#endif
432
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
433
1.24k
  } else
434
2.71k
#endif
435
2.71k
  {
436
2.71k
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
437
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
438
2.71k
  }
439
440
3.95k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
441
3.95k
    upsample = (my_upsample_ptr)
442
3.95k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
443
3.95k
                                  sizeof(my_upsampler));
444
3.95k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
445
3.95k
    upsample->pub.start_pass = start_pass_upsample;
446
3.95k
    upsample->pub._upsample = sep_upsample;
447
3.95k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
448
3.95k
  } else
449
0
    upsample = (my_upsample_ptr)cinfo->upsample;
450
451
3.95k
  if (cinfo->CCIR601_sampling)  /* this isn't supported */
452
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
453
454
  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
455
   * so don't ask for it.
456
   */
457
3.95k
  do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
458
459
  /* Verify we can handle the sampling factors, select per-component methods,
460
   * and create storage as needed.
461
   */
462
10.5k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
463
6.56k
       ci++, compptr++) {
464
    /* Compute size of an "input group" after IDCT scaling.  This many samples
465
     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
466
     */
467
6.56k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
468
6.56k
                 cinfo->_min_DCT_scaled_size;
469
6.56k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
470
6.56k
                 cinfo->_min_DCT_scaled_size;
471
6.56k
    h_out_group = cinfo->max_h_samp_factor;
472
6.56k
    v_out_group = cinfo->max_v_samp_factor;
473
6.56k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
474
6.56k
    need_buffer = TRUE;
475
6.56k
    if (!compptr->component_needed) {
476
      /* Don't bother to upsample an uninteresting component. */
477
0
      upsample->methods[ci] = noop_upsample;
478
0
      need_buffer = FALSE;
479
6.56k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
480
      /* Fullsize components can be processed without any work. */
481
3.42k
      upsample->methods[ci] = fullsize_upsample;
482
3.42k
      need_buffer = FALSE;
483
3.42k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
484
      /* Special cases for 2h1v upsampling */
485
483
      if (do_fancy && compptr->downsampled_width > 2) {
486
204
#ifdef WITH_SIMD
487
204
        if (jsimd_can_h2v1_fancy_upsample())
488
0
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
489
204
        else
490
204
#endif
491
204
          upsample->methods[ci] = h2v1_fancy_upsample;
492
279
      } else {
493
279
#ifdef WITH_SIMD
494
279
        if (jsimd_can_h2v1_upsample())
495
0
          upsample->methods[ci] = jsimd_h2v1_upsample;
496
279
        else
497
279
#endif
498
279
          upsample->methods[ci] = h2v1_upsample;
499
279
      }
500
2.65k
    } else if (h_in_group == h_out_group &&
501
2.65k
               v_in_group * 2 == v_out_group && do_fancy) {
502
      /* Non-fancy upsampling is handled by the generic method */
503
#if defined(WITH_SIMD) && (defined(__arm__) || defined(__aarch64__) || \
504
                           defined(_M_ARM) || defined(_M_ARM64) || \
505
                           defined(_M_ARM64EC))
506
      if (jsimd_can_h1v2_fancy_upsample())
507
        upsample->methods[ci] = jsimd_h1v2_fancy_upsample;
508
      else
509
#endif
510
547
        upsample->methods[ci] = h1v2_fancy_upsample;
511
547
      upsample->pub.need_context_rows = TRUE;
512
2.10k
    } else if (h_in_group * 2 == h_out_group &&
513
2.10k
               v_in_group * 2 == v_out_group) {
514
      /* Special cases for 2h2v upsampling */
515
394
      if (do_fancy && compptr->downsampled_width > 2) {
516
154
#ifdef WITH_SIMD
517
154
        if (jsimd_can_h2v2_fancy_upsample())
518
0
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
519
154
        else
520
154
#endif
521
154
          upsample->methods[ci] = h2v2_fancy_upsample;
522
154
        upsample->pub.need_context_rows = TRUE;
523
240
      } else {
524
240
#ifdef WITH_SIMD
525
240
        if (jsimd_can_h2v2_upsample())
526
0
          upsample->methods[ci] = jsimd_h2v2_upsample;
527
240
        else
528
240
#endif
529
240
          upsample->methods[ci] = h2v2_upsample;
530
240
      }
531
1.71k
    } else if ((h_out_group % h_in_group) == 0 &&
532
1.71k
               (v_out_group % v_in_group) == 0) {
533
      /* Generic integral-factors upsampling method */
534
#if defined(WITH_SIMD) && defined(__mips__)
535
      if (jsimd_can_int_upsample())
536
        upsample->methods[ci] = jsimd_int_upsample;
537
      else
538
#endif
539
1.69k
        upsample->methods[ci] = int_upsample;
540
1.69k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
541
1.69k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
542
1.69k
    } else
543
13
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
544
6.56k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
545
3.12k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
546
3.12k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
547
3.12k
         (JDIMENSION)jround_up((long)cinfo->output_width,
548
3.12k
                               (long)cinfo->max_h_samp_factor),
549
3.12k
         (JDIMENSION)cinfo->max_v_samp_factor);
550
3.12k
    }
551
6.56k
  }
552
3.95k
}
j12init_upsampler
Line
Count
Source
417
1.25k
{
418
1.25k
  my_upsample_ptr upsample;
419
1.25k
  int ci;
420
1.25k
  jpeg_component_info *compptr;
421
1.25k
  boolean need_buffer, do_fancy;
422
1.25k
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
1.25k
#ifdef D_LOSSLESS_SUPPORTED
425
1.25k
  if (cinfo->master->lossless) {
426
#if BITS_IN_JSAMPLE == 8
427
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
428
#else
429
793
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
430
793
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
431
0
#endif
432
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
433
793
  } else
434
462
#endif
435
462
  {
436
462
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
437
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
438
462
  }
439
440
1.25k
  if (!cinfo->master->jinit_upsampler_no_alloc) {
441
1.25k
    upsample = (my_upsample_ptr)
442
1.25k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
443
1.25k
                                  sizeof(my_upsampler));
444
1.25k
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
445
1.25k
    upsample->pub.start_pass = start_pass_upsample;
446
1.25k
    upsample->pub._upsample = sep_upsample;
447
1.25k
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
448
1.25k
  } else
449
0
    upsample = (my_upsample_ptr)cinfo->upsample;
450
451
1.25k
  if (cinfo->CCIR601_sampling)  /* this isn't supported */
452
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
453
454
  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
455
   * so don't ask for it.
456
   */
457
1.25k
  do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
458
459
  /* Verify we can handle the sampling factors, select per-component methods,
460
   * and create storage as needed.
461
   */
462
4.05k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
463
2.79k
       ci++, compptr++) {
464
    /* Compute size of an "input group" after IDCT scaling.  This many samples
465
     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
466
     */
467
2.79k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
468
2.79k
                 cinfo->_min_DCT_scaled_size;
469
2.79k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
470
2.79k
                 cinfo->_min_DCT_scaled_size;
471
2.79k
    h_out_group = cinfo->max_h_samp_factor;
472
2.79k
    v_out_group = cinfo->max_v_samp_factor;
473
2.79k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
474
2.79k
    need_buffer = TRUE;
475
2.79k
    if (!compptr->component_needed) {
476
      /* Don't bother to upsample an uninteresting component. */
477
0
      upsample->methods[ci] = noop_upsample;
478
0
      need_buffer = FALSE;
479
2.79k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
480
      /* Fullsize components can be processed without any work. */
481
654
      upsample->methods[ci] = fullsize_upsample;
482
654
      need_buffer = FALSE;
483
2.14k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
484
      /* Special cases for 2h1v upsampling */
485
280
      if (do_fancy && compptr->downsampled_width > 2) {
486
#ifdef WITH_SIMD
487
        if (jsimd_can_h2v1_fancy_upsample())
488
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
489
        else
490
#endif
491
114
          upsample->methods[ci] = h2v1_fancy_upsample;
492
166
      } else {
493
#ifdef WITH_SIMD
494
        if (jsimd_can_h2v1_upsample())
495
          upsample->methods[ci] = jsimd_h2v1_upsample;
496
        else
497
#endif
498
166
          upsample->methods[ci] = h2v1_upsample;
499
166
      }
500
1.86k
    } else if (h_in_group == h_out_group &&
501
1.86k
               v_in_group * 2 == v_out_group && do_fancy) {
502
      /* Non-fancy upsampling is handled by the generic method */
503
#if defined(WITH_SIMD) && (defined(__arm__) || defined(__aarch64__) || \
504
                           defined(_M_ARM) || defined(_M_ARM64) || \
505
                           defined(_M_ARM64EC))
506
      if (jsimd_can_h1v2_fancy_upsample())
507
        upsample->methods[ci] = jsimd_h1v2_fancy_upsample;
508
      else
509
#endif
510
149
        upsample->methods[ci] = h1v2_fancy_upsample;
511
149
      upsample->pub.need_context_rows = TRUE;
512
1.71k
    } else if (h_in_group * 2 == h_out_group &&
513
1.71k
               v_in_group * 2 == v_out_group) {
514
      /* Special cases for 2h2v upsampling */
515
176
      if (do_fancy && compptr->downsampled_width > 2) {
516
#ifdef WITH_SIMD
517
        if (jsimd_can_h2v2_fancy_upsample())
518
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
519
        else
520
#endif
521
56
          upsample->methods[ci] = h2v2_fancy_upsample;
522
56
        upsample->pub.need_context_rows = TRUE;
523
120
      } else {
524
#ifdef WITH_SIMD
525
        if (jsimd_can_h2v2_upsample())
526
          upsample->methods[ci] = jsimd_h2v2_upsample;
527
        else
528
#endif
529
120
          upsample->methods[ci] = h2v2_upsample;
530
120
      }
531
1.53k
    } else if ((h_out_group % h_in_group) == 0 &&
532
1.53k
               (v_out_group % v_in_group) == 0) {
533
      /* Generic integral-factors upsampling method */
534
#if defined(WITH_SIMD) && defined(__mips__)
535
      if (jsimd_can_int_upsample())
536
        upsample->methods[ci] = jsimd_int_upsample;
537
      else
538
#endif
539
1.52k
        upsample->methods[ci] = int_upsample;
540
1.52k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
541
1.52k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
542
1.52k
    } else
543
9
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
544
2.79k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
545
2.13k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
546
2.13k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
547
2.13k
         (JDIMENSION)jround_up((long)cinfo->output_width,
548
2.13k
                               (long)cinfo->max_h_samp_factor),
549
2.13k
         (JDIMENSION)cinfo->max_v_samp_factor);
550
2.13k
    }
551
2.79k
  }
552
1.25k
}
j16init_upsampler
Line
Count
Source
417
894
{
418
894
  my_upsample_ptr upsample;
419
894
  int ci;
420
894
  jpeg_component_info *compptr;
421
894
  boolean need_buffer, do_fancy;
422
894
  int h_in_group, v_in_group, h_out_group, v_out_group;
423
424
894
#ifdef D_LOSSLESS_SUPPORTED
425
894
  if (cinfo->master->lossless) {
426
#if BITS_IN_JSAMPLE == 8
427
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
428
#else
429
894
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
430
894
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
431
0
#endif
432
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
433
894
  } else
434
0
#endif
435
0
  {
436
0
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
437
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
438
0
  }
439
440
894
  if (!cinfo->master->jinit_upsampler_no_alloc) {
441
894
    upsample = (my_upsample_ptr)
442
894
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
443
894
                                  sizeof(my_upsampler));
444
894
    cinfo->upsample = (struct jpeg_upsampler *)upsample;
445
894
    upsample->pub.start_pass = start_pass_upsample;
446
894
    upsample->pub._upsample = sep_upsample;
447
894
    upsample->pub.need_context_rows = FALSE; /* until we find out differently */
448
894
  } else
449
0
    upsample = (my_upsample_ptr)cinfo->upsample;
450
451
894
  if (cinfo->CCIR601_sampling)  /* this isn't supported */
452
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
453
454
  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
455
   * so don't ask for it.
456
   */
457
894
  do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
458
459
  /* Verify we can handle the sampling factors, select per-component methods,
460
   * and create storage as needed.
461
   */
462
2.96k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
463
2.06k
       ci++, compptr++) {
464
    /* Compute size of an "input group" after IDCT scaling.  This many samples
465
     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
466
     */
467
2.06k
    h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
468
2.06k
                 cinfo->_min_DCT_scaled_size;
469
2.06k
    v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
470
2.06k
                 cinfo->_min_DCT_scaled_size;
471
2.06k
    h_out_group = cinfo->max_h_samp_factor;
472
2.06k
    v_out_group = cinfo->max_v_samp_factor;
473
2.06k
    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
474
2.06k
    need_buffer = TRUE;
475
2.06k
    if (!compptr->component_needed) {
476
      /* Don't bother to upsample an uninteresting component. */
477
0
      upsample->methods[ci] = noop_upsample;
478
0
      need_buffer = FALSE;
479
2.06k
    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
480
      /* Fullsize components can be processed without any work. */
481
365
      upsample->methods[ci] = fullsize_upsample;
482
365
      need_buffer = FALSE;
483
1.70k
    } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) {
484
      /* Special cases for 2h1v upsampling */
485
141
      if (do_fancy && compptr->downsampled_width > 2) {
486
#ifdef WITH_SIMD
487
        if (jsimd_can_h2v1_fancy_upsample())
488
          upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
489
        else
490
#endif
491
0
          upsample->methods[ci] = h2v1_fancy_upsample;
492
141
      } else {
493
#ifdef WITH_SIMD
494
        if (jsimd_can_h2v1_upsample())
495
          upsample->methods[ci] = jsimd_h2v1_upsample;
496
        else
497
#endif
498
141
          upsample->methods[ci] = h2v1_upsample;
499
141
      }
500
1.56k
    } else if (h_in_group == h_out_group &&
501
1.56k
               v_in_group * 2 == v_out_group && do_fancy) {
502
      /* Non-fancy upsampling is handled by the generic method */
503
#if defined(WITH_SIMD) && (defined(__arm__) || defined(__aarch64__) || \
504
                           defined(_M_ARM) || defined(_M_ARM64) || \
505
                           defined(_M_ARM64EC))
506
      if (jsimd_can_h1v2_fancy_upsample())
507
        upsample->methods[ci] = jsimd_h1v2_fancy_upsample;
508
      else
509
#endif
510
0
        upsample->methods[ci] = h1v2_fancy_upsample;
511
0
      upsample->pub.need_context_rows = TRUE;
512
1.56k
    } else if (h_in_group * 2 == h_out_group &&
513
1.56k
               v_in_group * 2 == v_out_group) {
514
      /* Special cases for 2h2v upsampling */
515
78
      if (do_fancy && compptr->downsampled_width > 2) {
516
#ifdef WITH_SIMD
517
        if (jsimd_can_h2v2_fancy_upsample())
518
          upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
519
        else
520
#endif
521
0
          upsample->methods[ci] = h2v2_fancy_upsample;
522
0
        upsample->pub.need_context_rows = TRUE;
523
78
      } else {
524
#ifdef WITH_SIMD
525
        if (jsimd_can_h2v2_upsample())
526
          upsample->methods[ci] = jsimd_h2v2_upsample;
527
        else
528
#endif
529
78
          upsample->methods[ci] = h2v2_upsample;
530
78
      }
531
1.48k
    } else if ((h_out_group % h_in_group) == 0 &&
532
1.48k
               (v_out_group % v_in_group) == 0) {
533
      /* Generic integral-factors upsampling method */
534
#if defined(WITH_SIMD) && defined(__mips__)
535
      if (jsimd_can_int_upsample())
536
        upsample->methods[ci] = jsimd_int_upsample;
537
      else
538
#endif
539
1.47k
        upsample->methods[ci] = int_upsample;
540
1.47k
      upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);
541
1.47k
      upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);
542
1.47k
    } else
543
7
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
544
2.06k
    if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) {
545
1.69k
      upsample->color_buf[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
546
1.69k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
547
1.69k
         (JDIMENSION)jround_up((long)cinfo->output_width,
548
1.69k
                               (long)cinfo->max_h_samp_factor),
549
1.69k
         (JDIMENSION)cinfo->max_v_samp_factor);
550
1.69k
    }
551
2.06k
  }
552
894
}
553
554
#endif /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */