Coverage Report

Created: 2025-06-16 07:00

/src/libjpeg-turbo/src/jcsample.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jcsample.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1991-1996, Thomas G. Lane.
6
 * Lossless JPEG Modifications:
7
 * Copyright (C) 1999, Ken Murchison.
8
 * libjpeg-turbo Modifications:
9
 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
10
 * Copyright (C) 2014, MIPS Technologies, Inc., California.
11
 * Copyright (C) 2015, 2019, 2022, 2024, D. R. Commander.
12
 * For conditions of distribution and use, see the accompanying README.ijg
13
 * file.
14
 *
15
 * This file contains downsampling routines.
16
 *
17
 * Downsampling input data is counted in "row groups".  A row group
18
 * is defined to be max_v_samp_factor pixel rows of each component,
19
 * from which the downsampler produces v_samp_factor sample rows.
20
 * A single row group is processed in each call to the downsampler module.
21
 *
22
 * The downsampler is responsible for edge-expansion of its output data
23
 * to fill an integral number of DCT blocks horizontally.  The source buffer
24
 * may be modified if it is helpful for this purpose (the source buffer is
25
 * allocated wide enough to correspond to the desired output width).
26
 * The caller (the prep controller) is responsible for vertical padding.
27
 *
28
 * The downsampler may request "context rows" by setting need_context_rows
29
 * during startup.  In this case, the input arrays will contain at least
30
 * one row group's worth of pixels above and below the passed-in data;
31
 * the caller will create dummy rows at image top and bottom by replicating
32
 * the first or last real pixel row.
33
 *
34
 * An excellent reference for image resampling is
35
 *   Digital Image Warping, George Wolberg, 1990.
36
 *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
37
 *
38
 * The downsampling algorithm used here is a simple average of the source
39
 * pixels covered by the output pixel.  The hi-falutin sampling literature
40
 * refers to this as a "box filter".  In general the characteristics of a box
41
 * filter are not very good, but for the specific cases we normally use (1:1
42
 * and 2:1 ratios) the box is equivalent to a "triangle filter" which is not
43
 * nearly so bad.  If you intend to use other sampling ratios, you'd be well
44
 * advised to improve this code.
45
 *
46
 * A simple input-smoothing capability is provided.  This is mainly intended
47
 * for cleaning up color-dithered GIF input files (if you find it inadequate,
48
 * we suggest using an external filtering program such as pnmconvol).  When
49
 * enabled, each input pixel P is replaced by a weighted sum of itself and its
50
 * eight neighbors.  P's weight is 1-8*SF and each neighbor's weight is SF,
51
 * where SF = (smoothing_factor / 1024).
52
 * Currently, smoothing is only supported for 2h2v sampling factors.
53
 */
54
55
#define JPEG_INTERNALS
56
#include "jinclude.h"
57
#include "jpeglib.h"
58
#include "jsimd.h"
59
#include "jsamplecomp.h"
60
61
62
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
63
64
/* Pointer to routine to downsample a single component */
65
typedef void (*downsample1_ptr) (j_compress_ptr cinfo,
66
                                 jpeg_component_info *compptr,
67
                                 _JSAMPARRAY input_data,
68
                                 _JSAMPARRAY output_data);
69
70
/* Private subobject */
71
72
typedef struct {
73
  struct jpeg_downsampler pub;  /* public fields */
74
75
  /* Downsampling method pointers, one per component */
76
  downsample1_ptr methods[MAX_COMPONENTS];
77
} my_downsampler;
78
79
typedef my_downsampler *my_downsample_ptr;
80
81
82
/*
83
 * Initialize for a downsampling pass.
84
 */
85
86
METHODDEF(void)
87
start_pass_downsample(j_compress_ptr cinfo)
88
6.48k
{
89
  /* no work for now */
90
6.48k
}
jcsample-8.c:start_pass_downsample
Line
Count
Source
88
6.48k
{
89
  /* no work for now */
90
6.48k
}
Unexecuted instantiation: jcsample-12.c:start_pass_downsample
Unexecuted instantiation: jcsample-16.c:start_pass_downsample
91
92
93
/*
94
 * Expand a component horizontally from width input_cols to width output_cols,
95
 * by duplicating the rightmost samples.
96
 */
97
98
LOCAL(void)
99
expand_right_edge(_JSAMPARRAY image_data, int num_rows, JDIMENSION input_cols,
100
                  JDIMENSION output_cols)
101
7.88M
{
102
7.88M
  register _JSAMPROW ptr;
103
7.88M
  register _JSAMPLE pixval;
104
7.88M
  register int count;
105
7.88M
  int row;
106
7.88M
  int numcols = (int)(output_cols - input_cols);
107
108
7.88M
  if (numcols > 0) {
109
16.9M
    for (row = 0; row < num_rows; row++) {
110
10.3M
      ptr = image_data[row] + input_cols;
111
10.3M
      pixval = ptr[-1];
112
71.9M
      for (count = numcols; count > 0; count--)
113
61.5M
        *ptr++ = pixval;
114
10.3M
    }
115
6.59M
  }
116
7.88M
}
jcsample-8.c:expand_right_edge
Line
Count
Source
101
7.88M
{
102
7.88M
  register _JSAMPROW ptr;
103
7.88M
  register _JSAMPLE pixval;
104
7.88M
  register int count;
105
7.88M
  int row;
106
7.88M
  int numcols = (int)(output_cols - input_cols);
107
108
7.88M
  if (numcols > 0) {
109
16.9M
    for (row = 0; row < num_rows; row++) {
110
10.3M
      ptr = image_data[row] + input_cols;
111
10.3M
      pixval = ptr[-1];
112
71.9M
      for (count = numcols; count > 0; count--)
113
61.5M
        *ptr++ = pixval;
114
10.3M
    }
115
6.59M
  }
116
7.88M
}
Unexecuted instantiation: jcsample-12.c:expand_right_edge
Unexecuted instantiation: jcsample-16.c:expand_right_edge
117
118
119
/*
120
 * Do downsampling for a whole row group (all components).
121
 *
122
 * In this version we simply downsample each component independently.
123
 */
124
125
METHODDEF(void)
126
sep_downsample(j_compress_ptr cinfo, _JSAMPIMAGE input_buf,
127
               JDIMENSION in_row_index, _JSAMPIMAGE output_buf,
128
               JDIMENSION out_row_group_index)
129
4.13M
{
130
4.13M
  my_downsample_ptr downsample = (my_downsample_ptr)cinfo->downsample;
131
4.13M
  int ci;
132
4.13M
  jpeg_component_info *compptr;
133
4.13M
  _JSAMPARRAY in_ptr, out_ptr;
134
135
12.0M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
136
7.88M
       ci++, compptr++) {
137
7.88M
    in_ptr = input_buf[ci] + in_row_index;
138
7.88M
    out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor);
139
7.88M
    (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr);
140
7.88M
  }
141
4.13M
}
jcsample-8.c:sep_downsample
Line
Count
Source
129
4.13M
{
130
4.13M
  my_downsample_ptr downsample = (my_downsample_ptr)cinfo->downsample;
131
4.13M
  int ci;
132
4.13M
  jpeg_component_info *compptr;
133
4.13M
  _JSAMPARRAY in_ptr, out_ptr;
134
135
12.0M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
136
7.88M
       ci++, compptr++) {
137
7.88M
    in_ptr = input_buf[ci] + in_row_index;
138
7.88M
    out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor);
139
7.88M
    (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr);
140
7.88M
  }
141
4.13M
}
Unexecuted instantiation: jcsample-12.c:sep_downsample
Unexecuted instantiation: jcsample-16.c:sep_downsample
142
143
144
/*
145
 * Downsample pixel values of a single component.
146
 * One row group is processed per call.
147
 * This version handles arbitrary integral sampling ratios, without smoothing.
148
 * Note that this version is not actually used for customary sampling ratios.
149
 */
150
151
METHODDEF(void)
152
int_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,
153
               _JSAMPARRAY input_data, _JSAMPARRAY output_data)
154
1.05M
{
155
1.05M
  int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
156
1.05M
  JDIMENSION outcol, outcol_h;  /* outcol_h == outcol*h_expand */
157
1.05M
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
158
1.05M
  JDIMENSION output_cols = compptr->width_in_blocks * data_unit;
159
1.05M
  _JSAMPROW inptr, outptr;
160
1.05M
  JLONG outvalue;
161
162
1.05M
  h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
163
1.05M
  v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
164
1.05M
  numpix = h_expand * v_expand;
165
1.05M
  numpix2 = numpix / 2;
166
167
  /* Expand input data enough to let all the output samples be generated
168
   * by the standard loop.  Special-casing padded output would be more
169
   * efficient.
170
   */
171
1.05M
  expand_right_edge(input_data, cinfo->max_v_samp_factor, cinfo->image_width,
172
1.05M
                    output_cols * h_expand);
173
174
1.05M
  inrow = 0;
175
2.65M
  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
176
1.60M
    outptr = output_data[outrow];
177
1.12G
    for (outcol = 0, outcol_h = 0; outcol < output_cols;
178
1.12G
         outcol++, outcol_h += h_expand) {
179
1.12G
      outvalue = 0;
180
3.50G
      for (v = 0; v < v_expand; v++) {
181
2.37G
        inptr = input_data[inrow + v] + outcol_h;
182
5.40G
        for (h = 0; h < h_expand; h++) {
183
3.02G
          outvalue += (JLONG)(*inptr++);
184
3.02G
        }
185
2.37G
      }
186
1.12G
      *outptr++ = (_JSAMPLE)((outvalue + numpix2) / numpix);
187
1.12G
    }
188
1.60M
    inrow += v_expand;
189
1.60M
  }
190
1.05M
}
jcsample-8.c:int_downsample
Line
Count
Source
154
1.05M
{
155
1.05M
  int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
156
1.05M
  JDIMENSION outcol, outcol_h;  /* outcol_h == outcol*h_expand */
157
1.05M
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
158
1.05M
  JDIMENSION output_cols = compptr->width_in_blocks * data_unit;
159
1.05M
  _JSAMPROW inptr, outptr;
160
1.05M
  JLONG outvalue;
161
162
1.05M
  h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
163
1.05M
  v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
164
1.05M
  numpix = h_expand * v_expand;
165
1.05M
  numpix2 = numpix / 2;
166
167
  /* Expand input data enough to let all the output samples be generated
168
   * by the standard loop.  Special-casing padded output would be more
169
   * efficient.
170
   */
171
1.05M
  expand_right_edge(input_data, cinfo->max_v_samp_factor, cinfo->image_width,
172
1.05M
                    output_cols * h_expand);
173
174
1.05M
  inrow = 0;
175
2.65M
  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
176
1.60M
    outptr = output_data[outrow];
177
1.12G
    for (outcol = 0, outcol_h = 0; outcol < output_cols;
178
1.12G
         outcol++, outcol_h += h_expand) {
179
1.12G
      outvalue = 0;
180
3.50G
      for (v = 0; v < v_expand; v++) {
181
2.37G
        inptr = input_data[inrow + v] + outcol_h;
182
5.40G
        for (h = 0; h < h_expand; h++) {
183
3.02G
          outvalue += (JLONG)(*inptr++);
184
3.02G
        }
185
2.37G
      }
186
1.12G
      *outptr++ = (_JSAMPLE)((outvalue + numpix2) / numpix);
187
1.12G
    }
188
1.60M
    inrow += v_expand;
189
1.60M
  }
190
1.05M
}
Unexecuted instantiation: jcsample-12.c:int_downsample
Unexecuted instantiation: jcsample-16.c:int_downsample
191
192
193
/*
194
 * Downsample pixel values of a single component.
195
 * This version handles the special case of a full-size component,
196
 * without smoothing.
197
 */
198
199
METHODDEF(void)
200
fullsize_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,
201
                    _JSAMPARRAY input_data, _JSAMPARRAY output_data)
202
5.30M
{
203
5.30M
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
204
205
  /* Copy the data */
206
5.30M
  _jcopy_sample_rows(input_data, 0, output_data, 0, cinfo->max_v_samp_factor,
207
5.30M
                     cinfo->image_width);
208
  /* Edge-expand */
209
5.30M
  expand_right_edge(output_data, cinfo->max_v_samp_factor, cinfo->image_width,
210
5.30M
                    compptr->width_in_blocks * data_unit);
211
5.30M
}
jcsample-8.c:fullsize_downsample
Line
Count
Source
202
5.30M
{
203
5.30M
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
204
205
  /* Copy the data */
206
5.30M
  _jcopy_sample_rows(input_data, 0, output_data, 0, cinfo->max_v_samp_factor,
207
5.30M
                     cinfo->image_width);
208
  /* Edge-expand */
209
5.30M
  expand_right_edge(output_data, cinfo->max_v_samp_factor, cinfo->image_width,
210
5.30M
                    compptr->width_in_blocks * data_unit);
211
5.30M
}
Unexecuted instantiation: jcsample-12.c:fullsize_downsample
Unexecuted instantiation: jcsample-16.c:fullsize_downsample
212
213
214
/*
215
 * Downsample pixel values of a single component.
216
 * This version handles the common case of 2:1 horizontal and 1:1 vertical,
217
 * without smoothing.
218
 *
219
 * A note about the "bias" calculations: when rounding fractional values to
220
 * integer, we do not want to always round 0.5 up to the next integer.
221
 * If we did that, we'd introduce a noticeable bias towards larger values.
222
 * Instead, this code is arranged so that 0.5 will be rounded up or down at
223
 * alternate pixel locations (a simple ordered dither pattern).
224
 */
225
226
METHODDEF(void)
227
h2v1_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,
228
                _JSAMPARRAY input_data, _JSAMPARRAY output_data)
229
1.33M
{
230
1.33M
  int outrow;
231
1.33M
  JDIMENSION outcol;
232
1.33M
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
233
1.33M
  JDIMENSION output_cols = compptr->width_in_blocks * data_unit;
234
1.33M
  register _JSAMPROW inptr, outptr;
235
1.33M
  register int bias;
236
237
  /* Expand input data enough to let all the output samples be generated
238
   * by the standard loop.  Special-casing padded output would be more
239
   * efficient.
240
   */
241
1.33M
  expand_right_edge(input_data, cinfo->max_v_samp_factor, cinfo->image_width,
242
1.33M
                    output_cols * 2);
243
244
3.08M
  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
245
1.75M
    outptr = output_data[outrow];
246
1.75M
    inptr = input_data[outrow];
247
1.75M
    bias = 0;                   /* bias = 0,1,0,1,... for successive samples */
248
1.41G
    for (outcol = 0; outcol < output_cols; outcol++) {
249
1.41G
      *outptr++ = (_JSAMPLE)((inptr[0] + inptr[1] + bias) >> 1);
250
1.41G
      bias ^= 1;                /* 0=>1, 1=>0 */
251
1.41G
      inptr += 2;
252
1.41G
    }
253
1.75M
  }
254
1.33M
}
jcsample-8.c:h2v1_downsample
Line
Count
Source
229
1.33M
{
230
1.33M
  int outrow;
231
1.33M
  JDIMENSION outcol;
232
1.33M
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
233
1.33M
  JDIMENSION output_cols = compptr->width_in_blocks * data_unit;
234
1.33M
  register _JSAMPROW inptr, outptr;
235
1.33M
  register int bias;
236
237
  /* Expand input data enough to let all the output samples be generated
238
   * by the standard loop.  Special-casing padded output would be more
239
   * efficient.
240
   */
241
1.33M
  expand_right_edge(input_data, cinfo->max_v_samp_factor, cinfo->image_width,
242
1.33M
                    output_cols * 2);
243
244
3.08M
  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
245
1.75M
    outptr = output_data[outrow];
246
1.75M
    inptr = input_data[outrow];
247
1.75M
    bias = 0;                   /* bias = 0,1,0,1,... for successive samples */
248
1.41G
    for (outcol = 0; outcol < output_cols; outcol++) {
249
1.41G
      *outptr++ = (_JSAMPLE)((inptr[0] + inptr[1] + bias) >> 1);
250
1.41G
      bias ^= 1;                /* 0=>1, 1=>0 */
251
1.41G
      inptr += 2;
252
1.41G
    }
253
1.75M
  }
254
1.33M
}
Unexecuted instantiation: jcsample-12.c:h2v1_downsample
Unexecuted instantiation: jcsample-16.c:h2v1_downsample
255
256
257
/*
258
 * Downsample pixel values of a single component.
259
 * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
260
 * without smoothing.
261
 */
262
263
METHODDEF(void)
264
h2v2_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,
265
                _JSAMPARRAY input_data, _JSAMPARRAY output_data)
266
193k
{
267
193k
  int inrow, outrow;
268
193k
  JDIMENSION outcol;
269
193k
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
270
193k
  JDIMENSION output_cols = compptr->width_in_blocks * data_unit;
271
193k
  register _JSAMPROW inptr0, inptr1, outptr;
272
193k
  register int bias;
273
274
  /* Expand input data enough to let all the output samples be generated
275
   * by the standard loop.  Special-casing padded output would be more
276
   * efficient.
277
   */
278
193k
  expand_right_edge(input_data, cinfo->max_v_samp_factor, cinfo->image_width,
279
193k
                    output_cols * 2);
280
281
193k
  inrow = 0;
282
410k
  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
283
217k
    outptr = output_data[outrow];
284
217k
    inptr0 = input_data[inrow];
285
217k
    inptr1 = input_data[inrow + 1];
286
217k
    bias = 1;                   /* bias = 1,2,1,2,... for successive samples */
287
75.7M
    for (outcol = 0; outcol < output_cols; outcol++) {
288
75.4M
      *outptr++ = (_JSAMPLE)
289
75.4M
        ((inptr0[0] + inptr0[1] + inptr1[0] + inptr1[1] + bias) >> 2);
290
75.4M
      bias ^= 3;                /* 1=>2, 2=>1 */
291
75.4M
      inptr0 += 2;  inptr1 += 2;
292
75.4M
    }
293
217k
    inrow += 2;
294
217k
  }
295
193k
}
jcsample-8.c:h2v2_downsample
Line
Count
Source
266
193k
{
267
193k
  int inrow, outrow;
268
193k
  JDIMENSION outcol;
269
193k
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
270
193k
  JDIMENSION output_cols = compptr->width_in_blocks * data_unit;
271
193k
  register _JSAMPROW inptr0, inptr1, outptr;
272
193k
  register int bias;
273
274
  /* Expand input data enough to let all the output samples be generated
275
   * by the standard loop.  Special-casing padded output would be more
276
   * efficient.
277
   */
278
193k
  expand_right_edge(input_data, cinfo->max_v_samp_factor, cinfo->image_width,
279
193k
                    output_cols * 2);
280
281
193k
  inrow = 0;
282
410k
  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
283
217k
    outptr = output_data[outrow];
284
217k
    inptr0 = input_data[inrow];
285
217k
    inptr1 = input_data[inrow + 1];
286
217k
    bias = 1;                   /* bias = 1,2,1,2,... for successive samples */
287
75.7M
    for (outcol = 0; outcol < output_cols; outcol++) {
288
75.4M
      *outptr++ = (_JSAMPLE)
289
75.4M
        ((inptr0[0] + inptr0[1] + inptr1[0] + inptr1[1] + bias) >> 2);
290
75.4M
      bias ^= 3;                /* 1=>2, 2=>1 */
291
75.4M
      inptr0 += 2;  inptr1 += 2;
292
75.4M
    }
293
217k
    inrow += 2;
294
217k
  }
295
193k
}
Unexecuted instantiation: jcsample-12.c:h2v2_downsample
Unexecuted instantiation: jcsample-16.c:h2v2_downsample
296
297
298
#ifdef INPUT_SMOOTHING_SUPPORTED
299
300
/*
301
 * Downsample pixel values of a single component.
302
 * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
303
 * with smoothing.  One row of context is required.
304
 */
305
306
METHODDEF(void)
307
h2v2_smooth_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,
308
                       _JSAMPARRAY input_data, _JSAMPARRAY output_data)
309
0
{
310
0
  int inrow, outrow;
311
0
  JDIMENSION colctr;
312
0
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
313
0
  JDIMENSION output_cols = compptr->width_in_blocks * data_unit;
314
0
  register _JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr;
315
0
  JLONG membersum, neighsum, memberscale, neighscale;
316
317
  /* Expand input data enough to let all the output samples be generated
318
   * by the standard loop.  Special-casing padded output would be more
319
   * efficient.
320
   */
321
0
  expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
322
0
                    cinfo->image_width, output_cols * 2);
323
324
  /* We don't bother to form the individual "smoothed" input pixel values;
325
   * we can directly compute the output which is the average of the four
326
   * smoothed values.  Each of the four member pixels contributes a fraction
327
   * (1-8*SF) to its own smoothed image and a fraction SF to each of the three
328
   * other smoothed pixels, therefore a total fraction (1-5*SF)/4 to the final
329
   * output.  The four corner-adjacent neighbor pixels contribute a fraction
330
   * SF to just one smoothed pixel, or SF/4 to the final output; while the
331
   * eight edge-adjacent neighbors contribute SF to each of two smoothed
332
   * pixels, or SF/2 overall.  In order to use integer arithmetic, these
333
   * factors are scaled by 2^16 = 65536.
334
   * Also recall that SF = smoothing_factor / 1024.
335
   */
336
337
0
  memberscale = 16384 - cinfo->smoothing_factor * 80; /* scaled (1-5*SF)/4 */
338
0
  neighscale = cinfo->smoothing_factor * 16; /* scaled SF/4 */
339
340
0
  inrow = 0;
341
0
  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
342
0
    outptr = output_data[outrow];
343
0
    inptr0 = input_data[inrow];
344
0
    inptr1 = input_data[inrow + 1];
345
0
    above_ptr = input_data[inrow - 1];
346
0
    below_ptr = input_data[inrow + 2];
347
348
    /* Special case for first column: pretend column -1 is same as column 0 */
349
0
    membersum = inptr0[0] + inptr0[1] + inptr1[0] + inptr1[1];
350
0
    neighsum = above_ptr[0] + above_ptr[1] + below_ptr[0] + below_ptr[1] +
351
0
               inptr0[0] + inptr0[2] + inptr1[0] + inptr1[2];
352
0
    neighsum += neighsum;
353
0
    neighsum += above_ptr[0] + above_ptr[2] + below_ptr[0] + below_ptr[2];
354
0
    membersum = membersum * memberscale + neighsum * neighscale;
355
0
    *outptr++ = (_JSAMPLE)((membersum + 32768) >> 16);
356
0
    inptr0 += 2;  inptr1 += 2;  above_ptr += 2;  below_ptr += 2;
357
358
0
    for (colctr = output_cols - 2; colctr > 0; colctr--) {
359
      /* sum of pixels directly mapped to this output element */
360
0
      membersum = inptr0[0] + inptr0[1] + inptr1[0] + inptr1[1];
361
      /* sum of edge-neighbor pixels */
362
0
      neighsum = above_ptr[0] + above_ptr[1] + below_ptr[0] + below_ptr[1] +
363
0
                 inptr0[-1] + inptr0[2] + inptr1[-1] + inptr1[2];
364
      /* The edge-neighbors count twice as much as corner-neighbors */
365
0
      neighsum += neighsum;
366
      /* Add in the corner-neighbors */
367
0
      neighsum += above_ptr[-1] + above_ptr[2] + below_ptr[-1] + below_ptr[2];
368
      /* form final output scaled up by 2^16 */
369
0
      membersum = membersum * memberscale + neighsum * neighscale;
370
      /* round, descale and output it */
371
0
      *outptr++ = (_JSAMPLE)((membersum + 32768) >> 16);
372
0
      inptr0 += 2;  inptr1 += 2;  above_ptr += 2;  below_ptr += 2;
373
0
    }
374
375
    /* Special case for last column */
376
0
    membersum = inptr0[0] + inptr0[1] + inptr1[0] + inptr1[1];
377
0
    neighsum = above_ptr[0] + above_ptr[1] + below_ptr[0] + below_ptr[1] +
378
0
               inptr0[-1] + inptr0[1] + inptr1[-1] + inptr1[1];
379
0
    neighsum += neighsum;
380
0
    neighsum += above_ptr[-1] + above_ptr[1] + below_ptr[-1] + below_ptr[1];
381
0
    membersum = membersum * memberscale + neighsum * neighscale;
382
0
    *outptr = (_JSAMPLE)((membersum + 32768) >> 16);
383
384
0
    inrow += 2;
385
0
  }
386
0
}
Unexecuted instantiation: jcsample-8.c:h2v2_smooth_downsample
Unexecuted instantiation: jcsample-12.c:h2v2_smooth_downsample
Unexecuted instantiation: jcsample-16.c:h2v2_smooth_downsample
387
388
389
/*
390
 * Downsample pixel values of a single component.
391
 * This version handles the special case of a full-size component,
392
 * with smoothing.  One row of context is required.
393
 */
394
395
METHODDEF(void)
396
fullsize_smooth_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,
397
                           _JSAMPARRAY input_data, _JSAMPARRAY output_data)
398
0
{
399
0
  int outrow;
400
0
  JDIMENSION colctr;
401
0
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
402
0
  JDIMENSION output_cols = compptr->width_in_blocks * data_unit;
403
0
  register _JSAMPROW inptr, above_ptr, below_ptr, outptr;
404
0
  JLONG membersum, neighsum, memberscale, neighscale;
405
0
  int colsum, lastcolsum, nextcolsum;
406
407
  /* Expand input data enough to let all the output samples be generated
408
   * by the standard loop.  Special-casing padded output would be more
409
   * efficient.
410
   */
411
0
  expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
412
0
                    cinfo->image_width, output_cols);
413
414
  /* Each of the eight neighbor pixels contributes a fraction SF to the
415
   * smoothed pixel, while the main pixel contributes (1-8*SF).  In order
416
   * to use integer arithmetic, these factors are multiplied by 2^16 = 65536.
417
   * Also recall that SF = smoothing_factor / 1024.
418
   */
419
420
0
  memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */
421
0
  neighscale = cinfo->smoothing_factor * 64; /* scaled SF */
422
423
0
  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
424
0
    outptr = output_data[outrow];
425
0
    inptr = input_data[outrow];
426
0
    above_ptr = input_data[outrow - 1];
427
0
    below_ptr = input_data[outrow + 1];
428
429
    /* Special case for first column */
430
0
    colsum = (*above_ptr++) + (*below_ptr++) + inptr[0];
431
0
    membersum = *inptr++;
432
0
    nextcolsum = above_ptr[0] + below_ptr[0] + inptr[0];
433
0
    neighsum = colsum + (colsum - membersum) + nextcolsum;
434
0
    membersum = membersum * memberscale + neighsum * neighscale;
435
0
    *outptr++ = (_JSAMPLE)((membersum + 32768) >> 16);
436
0
    lastcolsum = colsum;  colsum = nextcolsum;
437
438
0
    for (colctr = output_cols - 2; colctr > 0; colctr--) {
439
0
      membersum = *inptr++;
440
0
      above_ptr++;  below_ptr++;
441
0
      nextcolsum = above_ptr[0] + below_ptr[0] + inptr[0];
442
0
      neighsum = lastcolsum + (colsum - membersum) + nextcolsum;
443
0
      membersum = membersum * memberscale + neighsum * neighscale;
444
0
      *outptr++ = (_JSAMPLE)((membersum + 32768) >> 16);
445
0
      lastcolsum = colsum;  colsum = nextcolsum;
446
0
    }
447
448
    /* Special case for last column */
449
0
    membersum = *inptr;
450
0
    neighsum = lastcolsum + (colsum - membersum) + colsum;
451
0
    membersum = membersum * memberscale + neighsum * neighscale;
452
0
    *outptr = (_JSAMPLE)((membersum + 32768) >> 16);
453
454
0
  }
455
0
}
Unexecuted instantiation: jcsample-8.c:fullsize_smooth_downsample
Unexecuted instantiation: jcsample-12.c:fullsize_smooth_downsample
Unexecuted instantiation: jcsample-16.c:fullsize_smooth_downsample
456
457
#endif /* INPUT_SMOOTHING_SUPPORTED */
458
459
460
/*
461
 * Module initialization routine for downsampling.
462
 * Note that we must select a routine for each component.
463
 */
464
465
GLOBAL(void)
466
_jinit_downsampler(j_compress_ptr cinfo)
467
6.58k
{
468
6.58k
  my_downsample_ptr downsample;
469
6.58k
  int ci;
470
6.58k
  jpeg_component_info *compptr;
471
6.58k
  boolean smoothok = TRUE;
472
473
6.58k
#ifdef C_LOSSLESS_SUPPORTED
474
6.58k
  if (cinfo->master->lossless) {
475
#if BITS_IN_JSAMPLE == 8
476
0
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
477
#else
478
0
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
479
0
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
480
0
#endif
481
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
482
0
  } else
483
6.58k
#endif
484
6.58k
  {
485
6.58k
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
486
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
487
6.58k
  }
488
489
6.58k
  downsample = (my_downsample_ptr)
490
6.58k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
491
6.58k
                                sizeof(my_downsampler));
492
6.58k
  cinfo->downsample = (struct jpeg_downsampler *)downsample;
493
6.58k
  downsample->pub.start_pass = start_pass_downsample;
494
6.58k
  downsample->pub._downsample = sep_downsample;
495
6.58k
  downsample->pub.need_context_rows = FALSE;
496
497
6.58k
  if (cinfo->CCIR601_sampling)
498
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
499
500
  /* Verify we can handle the sampling factors, and set up method pointers */
501
20.6k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
502
14.0k
       ci++, compptr++) {
503
14.0k
    if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&
504
14.0k
        compptr->v_samp_factor == cinfo->max_v_samp_factor) {
505
6.18k
#ifdef INPUT_SMOOTHING_SUPPORTED
506
6.18k
      if (cinfo->smoothing_factor) {
507
0
        downsample->methods[ci] = fullsize_smooth_downsample;
508
0
        downsample->pub.need_context_rows = TRUE;
509
0
      } else
510
6.18k
#endif
511
6.18k
        downsample->methods[ci] = fullsize_downsample;
512
7.83k
    } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
513
7.83k
               compptr->v_samp_factor == cinfo->max_v_samp_factor) {
514
1.61k
      smoothok = FALSE;
515
#ifdef WITH_SIMD
516
1.61k
      if (jsimd_can_h2v1_downsample())
517
0
        downsample->methods[ci] = jsimd_h2v1_downsample;
518
1.61k
      else
519
1.61k
#endif
520
1.61k
        downsample->methods[ci] = h2v1_downsample;
521
6.22k
    } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
522
6.22k
               compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) {
523
671
#ifdef INPUT_SMOOTHING_SUPPORTED
524
671
      if (cinfo->smoothing_factor) {
525
#if defined(WITH_SIMD) && defined(__mips__)
526
        if (jsimd_can_h2v2_smooth_downsample())
527
          downsample->methods[ci] = jsimd_h2v2_smooth_downsample;
528
        else
529
#endif
530
0
          downsample->methods[ci] = h2v2_smooth_downsample;
531
0
        downsample->pub.need_context_rows = TRUE;
532
0
      } else
533
671
#endif
534
671
      {
535
#ifdef WITH_SIMD
536
671
        if (jsimd_can_h2v2_downsample())
537
0
          downsample->methods[ci] = jsimd_h2v2_downsample;
538
671
        else
539
671
#endif
540
671
          downsample->methods[ci] = h2v2_downsample;
541
671
      }
542
5.55k
    } else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&
543
5.55k
               (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) {
544
5.55k
      smoothok = FALSE;
545
5.55k
      downsample->methods[ci] = int_downsample;
546
5.55k
    } else
547
0
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
548
14.0k
  }
549
550
6.58k
#ifdef INPUT_SMOOTHING_SUPPORTED
551
6.58k
  if (cinfo->smoothing_factor && !smoothok)
552
0
    TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL);
553
6.58k
#endif
554
6.58k
}
jinit_downsampler
Line
Count
Source
467
6.58k
{
468
6.58k
  my_downsample_ptr downsample;
469
6.58k
  int ci;
470
6.58k
  jpeg_component_info *compptr;
471
6.58k
  boolean smoothok = TRUE;
472
473
6.58k
#ifdef C_LOSSLESS_SUPPORTED
474
6.58k
  if (cinfo->master->lossless) {
475
0
#if BITS_IN_JSAMPLE == 8
476
0
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
477
#else
478
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
479
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
480
#endif
481
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
482
0
  } else
483
6.58k
#endif
484
6.58k
  {
485
6.58k
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
486
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
487
6.58k
  }
488
489
6.58k
  downsample = (my_downsample_ptr)
490
6.58k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
491
6.58k
                                sizeof(my_downsampler));
492
6.58k
  cinfo->downsample = (struct jpeg_downsampler *)downsample;
493
6.58k
  downsample->pub.start_pass = start_pass_downsample;
494
6.58k
  downsample->pub._downsample = sep_downsample;
495
6.58k
  downsample->pub.need_context_rows = FALSE;
496
497
6.58k
  if (cinfo->CCIR601_sampling)
498
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
499
500
  /* Verify we can handle the sampling factors, and set up method pointers */
501
20.6k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
502
14.0k
       ci++, compptr++) {
503
14.0k
    if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&
504
14.0k
        compptr->v_samp_factor == cinfo->max_v_samp_factor) {
505
6.18k
#ifdef INPUT_SMOOTHING_SUPPORTED
506
6.18k
      if (cinfo->smoothing_factor) {
507
0
        downsample->methods[ci] = fullsize_smooth_downsample;
508
0
        downsample->pub.need_context_rows = TRUE;
509
0
      } else
510
6.18k
#endif
511
6.18k
        downsample->methods[ci] = fullsize_downsample;
512
7.83k
    } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
513
7.83k
               compptr->v_samp_factor == cinfo->max_v_samp_factor) {
514
1.61k
      smoothok = FALSE;
515
1.61k
#ifdef WITH_SIMD
516
1.61k
      if (jsimd_can_h2v1_downsample())
517
0
        downsample->methods[ci] = jsimd_h2v1_downsample;
518
1.61k
      else
519
1.61k
#endif
520
1.61k
        downsample->methods[ci] = h2v1_downsample;
521
6.22k
    } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
522
6.22k
               compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) {
523
671
#ifdef INPUT_SMOOTHING_SUPPORTED
524
671
      if (cinfo->smoothing_factor) {
525
#if defined(WITH_SIMD) && defined(__mips__)
526
        if (jsimd_can_h2v2_smooth_downsample())
527
          downsample->methods[ci] = jsimd_h2v2_smooth_downsample;
528
        else
529
#endif
530
0
          downsample->methods[ci] = h2v2_smooth_downsample;
531
0
        downsample->pub.need_context_rows = TRUE;
532
0
      } else
533
671
#endif
534
671
      {
535
671
#ifdef WITH_SIMD
536
671
        if (jsimd_can_h2v2_downsample())
537
0
          downsample->methods[ci] = jsimd_h2v2_downsample;
538
671
        else
539
671
#endif
540
671
          downsample->methods[ci] = h2v2_downsample;
541
671
      }
542
5.55k
    } else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&
543
5.55k
               (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) {
544
5.55k
      smoothok = FALSE;
545
5.55k
      downsample->methods[ci] = int_downsample;
546
5.55k
    } else
547
0
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
548
14.0k
  }
549
550
6.58k
#ifdef INPUT_SMOOTHING_SUPPORTED
551
6.58k
  if (cinfo->smoothing_factor && !smoothok)
552
0
    TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL);
553
6.58k
#endif
554
6.58k
}
Unexecuted instantiation: j12init_downsampler
Unexecuted instantiation: j16init_downsampler
555
556
#endif /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */