Coverage Report

Created: 2026-03-12 07:02

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