Coverage Report

Created: 2025-10-10 07:05

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) 2014, MIPS Technologies, Inc., California.
11
 * Copyright (C) 2015, 2019, 2022, 2024-2025, 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
#ifdef WITH_SIMD
59
#include "../simd/jsimd.h"
60
#endif
61
#include "jsamplecomp.h"
62
63
64
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
65
66
/* Pointer to routine to downsample a single component */
67
typedef void (*downsample1_ptr) (j_compress_ptr cinfo,
68
                                 jpeg_component_info *compptr,
69
                                 _JSAMPARRAY input_data,
70
                                 _JSAMPARRAY output_data);
71
72
/* Private subobject */
73
74
typedef struct {
75
  struct jpeg_downsampler pub;  /* public fields */
76
77
  /* Downsampling method pointers, one per component */
78
  downsample1_ptr methods[MAX_COMPONENTS];
79
} my_downsampler;
80
81
typedef my_downsampler *my_downsample_ptr;
82
83
84
/*
85
 * Initialize for a downsampling pass.
86
 */
87
88
METHODDEF(void)
89
start_pass_downsample(j_compress_ptr cinfo)
90
0
{
91
  /* no work for now */
92
0
}
Unexecuted instantiation: jcsample-8.c:start_pass_downsample
Unexecuted instantiation: jcsample-12.c:start_pass_downsample
Unexecuted instantiation: jcsample-16.c:start_pass_downsample
93
94
95
/*
96
 * Expand a component horizontally from width input_cols to width output_cols,
97
 * by duplicating the rightmost samples.
98
 */
99
100
LOCAL(void)
101
expand_right_edge(_JSAMPARRAY image_data, int num_rows, JDIMENSION input_cols,
102
                  JDIMENSION output_cols)
103
52.9M
{
104
52.9M
  register _JSAMPROW ptr;
105
52.9M
  register _JSAMPLE pixval;
106
52.9M
  register int count;
107
52.9M
  int row;
108
52.9M
  int numcols = (int)(output_cols - input_cols);
109
110
52.9M
  if (numcols > 0) {
111
108M
    for (row = 0; row < num_rows; row++) {
112
55.7M
      ptr = image_data[row] + input_cols;
113
55.7M
      pixval = ptr[-1];
114
684M
      for (count = numcols; count > 0; count--)
115
628M
        *ptr++ = pixval;
116
55.7M
    }
117
52.9M
  }
118
52.9M
}
jcsample-8.c:expand_right_edge
Line
Count
Source
103
52.9M
{
104
52.9M
  register _JSAMPROW ptr;
105
52.9M
  register _JSAMPLE pixval;
106
52.9M
  register int count;
107
52.9M
  int row;
108
52.9M
  int numcols = (int)(output_cols - input_cols);
109
110
52.9M
  if (numcols > 0) {
111
108M
    for (row = 0; row < num_rows; row++) {
112
55.7M
      ptr = image_data[row] + input_cols;
113
55.7M
      pixval = ptr[-1];
114
684M
      for (count = numcols; count > 0; count--)
115
628M
        *ptr++ = pixval;
116
55.7M
    }
117
52.9M
  }
118
52.9M
}
Unexecuted instantiation: jcsample-12.c:expand_right_edge
Unexecuted instantiation: jcsample-16.c:expand_right_edge
119
120
121
/*
122
 * Do downsampling for a whole row group (all components).
123
 *
124
 * In this version we simply downsample each component independently.
125
 */
126
127
METHODDEF(void)
128
sep_downsample(j_compress_ptr cinfo, _JSAMPIMAGE input_buf,
129
               JDIMENSION in_row_index, _JSAMPIMAGE output_buf,
130
               JDIMENSION out_row_group_index)
131
30.1M
{
132
30.1M
  my_downsample_ptr downsample = (my_downsample_ptr)cinfo->downsample;
133
30.1M
  int ci;
134
30.1M
  jpeg_component_info *compptr;
135
30.1M
  _JSAMPARRAY in_ptr, out_ptr;
136
137
100M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
138
70.0M
       ci++, compptr++) {
139
70.0M
    in_ptr = input_buf[ci] + in_row_index;
140
70.0M
    out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor);
141
70.0M
    (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr);
142
70.0M
  }
143
30.1M
}
jcsample-8.c:sep_downsample
Line
Count
Source
131
30.1M
{
132
30.1M
  my_downsample_ptr downsample = (my_downsample_ptr)cinfo->downsample;
133
30.1M
  int ci;
134
30.1M
  jpeg_component_info *compptr;
135
30.1M
  _JSAMPARRAY in_ptr, out_ptr;
136
137
100M
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
138
70.0M
       ci++, compptr++) {
139
70.0M
    in_ptr = input_buf[ci] + in_row_index;
140
70.0M
    out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor);
141
70.0M
    (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr);
142
70.0M
  }
143
30.1M
}
Unexecuted instantiation: jcsample-12.c:sep_downsample
Unexecuted instantiation: jcsample-16.c:sep_downsample
144
145
146
/*
147
 * Downsample pixel values of a single component.
148
 * One row group is processed per call.
149
 * This version handles arbitrary integral sampling ratios, without smoothing.
150
 * Note that this version is not actually used for customary sampling ratios.
151
 */
152
153
METHODDEF(void)
154
int_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,
155
               _JSAMPARRAY input_data, _JSAMPARRAY output_data)
156
11.4M
{
157
11.4M
  int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
158
11.4M
  JDIMENSION outcol, outcol_h;  /* outcol_h == outcol*h_expand */
159
11.4M
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
160
11.4M
  JDIMENSION output_cols = compptr->width_in_blocks * data_unit;
161
11.4M
  _JSAMPROW inptr, outptr;
162
11.4M
  JLONG outvalue;
163
164
11.4M
  h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
165
11.4M
  v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
166
11.4M
  numpix = h_expand * v_expand;
167
11.4M
  numpix2 = numpix / 2;
168
169
  /* Expand input data enough to let all the output samples be generated
170
   * by the standard loop.  Special-casing padded output would be more
171
   * efficient.
172
   */
173
11.4M
  expand_right_edge(input_data, cinfo->max_v_samp_factor, cinfo->image_width,
174
11.4M
                    output_cols * h_expand);
175
176
11.4M
  inrow = 0;
177
22.8M
  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
178
11.4M
    outptr = output_data[outrow];
179
105M
    for (outcol = 0, outcol_h = 0; outcol < output_cols;
180
94.1M
         outcol++, outcol_h += h_expand) {
181
94.1M
      outvalue = 0;
182
188M
      for (v = 0; v < v_expand; v++) {
183
94.1M
        inptr = input_data[inrow + v] + outcol_h;
184
470M
        for (h = 0; h < h_expand; h++) {
185
376M
          outvalue += (JLONG)(*inptr++);
186
376M
        }
187
94.1M
      }
188
94.1M
      *outptr++ = (_JSAMPLE)((outvalue + numpix2) / numpix);
189
94.1M
    }
190
11.4M
    inrow += v_expand;
191
11.4M
  }
192
11.4M
}
jcsample-8.c:int_downsample
Line
Count
Source
156
11.4M
{
157
11.4M
  int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
158
11.4M
  JDIMENSION outcol, outcol_h;  /* outcol_h == outcol*h_expand */
159
11.4M
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
160
11.4M
  JDIMENSION output_cols = compptr->width_in_blocks * data_unit;
161
11.4M
  _JSAMPROW inptr, outptr;
162
11.4M
  JLONG outvalue;
163
164
11.4M
  h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
165
11.4M
  v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
166
11.4M
  numpix = h_expand * v_expand;
167
11.4M
  numpix2 = numpix / 2;
168
169
  /* Expand input data enough to let all the output samples be generated
170
   * by the standard loop.  Special-casing padded output would be more
171
   * efficient.
172
   */
173
11.4M
  expand_right_edge(input_data, cinfo->max_v_samp_factor, cinfo->image_width,
174
11.4M
                    output_cols * h_expand);
175
176
11.4M
  inrow = 0;
177
22.8M
  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
178
11.4M
    outptr = output_data[outrow];
179
105M
    for (outcol = 0, outcol_h = 0; outcol < output_cols;
180
94.1M
         outcol++, outcol_h += h_expand) {
181
94.1M
      outvalue = 0;
182
188M
      for (v = 0; v < v_expand; v++) {
183
94.1M
        inptr = input_data[inrow + v] + outcol_h;
184
470M
        for (h = 0; h < h_expand; h++) {
185
376M
          outvalue += (JLONG)(*inptr++);
186
376M
        }
187
94.1M
      }
188
94.1M
      *outptr++ = (_JSAMPLE)((outvalue + numpix2) / numpix);
189
94.1M
    }
190
11.4M
    inrow += v_expand;
191
11.4M
  }
192
11.4M
}
Unexecuted instantiation: jcsample-12.c:int_downsample
Unexecuted instantiation: jcsample-16.c:int_downsample
193
194
195
/*
196
 * Downsample pixel values of a single component.
197
 * This version handles the special case of a full-size component,
198
 * without smoothing.
199
 */
200
201
METHODDEF(void)
202
fullsize_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,
203
                    _JSAMPARRAY input_data, _JSAMPARRAY output_data)
204
41.5M
{
205
41.5M
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
206
207
  /* Copy the data */
208
41.5M
  _jcopy_sample_rows(input_data, 0, output_data, 0, cinfo->max_v_samp_factor,
209
41.5M
                     cinfo->image_width);
210
  /* Edge-expand */
211
41.5M
  expand_right_edge(output_data, cinfo->max_v_samp_factor, cinfo->image_width,
212
41.5M
                    compptr->width_in_blocks * data_unit);
213
41.5M
}
jcsample-8.c:fullsize_downsample
Line
Count
Source
204
41.5M
{
205
41.5M
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
206
207
  /* Copy the data */
208
41.5M
  _jcopy_sample_rows(input_data, 0, output_data, 0, cinfo->max_v_samp_factor,
209
41.5M
                     cinfo->image_width);
210
  /* Edge-expand */
211
41.5M
  expand_right_edge(output_data, cinfo->max_v_samp_factor, cinfo->image_width,
212
41.5M
                    compptr->width_in_blocks * data_unit);
213
41.5M
}
Unexecuted instantiation: jcsample-12.c:fullsize_downsample
Unexecuted instantiation: jcsample-16.c:fullsize_downsample
214
215
216
/*
217
 * Downsample pixel values of a single component.
218
 * This version handles the common case of 2:1 horizontal and 1:1 vertical,
219
 * without smoothing.
220
 *
221
 * A note about the "bias" calculations: when rounding fractional values to
222
 * integer, we do not want to always round 0.5 up to the next integer.
223
 * If we did that, we'd introduce a noticeable bias towards larger values.
224
 * Instead, this code is arranged so that 0.5 will be rounded up or down at
225
 * alternate pixel locations (a simple ordered dither pattern).
226
 */
227
228
METHODDEF(void)
229
h2v1_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,
230
                _JSAMPARRAY input_data, _JSAMPARRAY output_data)
231
0
{
232
0
  int outrow;
233
0
  JDIMENSION outcol;
234
0
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
235
0
  JDIMENSION output_cols = compptr->width_in_blocks * data_unit;
236
0
  register _JSAMPROW inptr, outptr;
237
0
  register int bias;
238
239
  /* Expand input data enough to let all the output samples be generated
240
   * by the standard loop.  Special-casing padded output would be more
241
   * efficient.
242
   */
243
0
  expand_right_edge(input_data, cinfo->max_v_samp_factor, cinfo->image_width,
244
0
                    output_cols * 2);
245
246
0
  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
247
0
    outptr = output_data[outrow];
248
0
    inptr = input_data[outrow];
249
0
    bias = 0;                   /* bias = 0,1,0,1,... for successive samples */
250
0
    for (outcol = 0; outcol < output_cols; outcol++) {
251
0
      *outptr++ = (_JSAMPLE)((inptr[0] + inptr[1] + bias) >> 1);
252
0
      bias ^= 1;                /* 0=>1, 1=>0 */
253
0
      inptr += 2;
254
0
    }
255
0
  }
256
0
}
Unexecuted instantiation: jcsample-8.c:h2v1_downsample
Unexecuted instantiation: jcsample-12.c:h2v1_downsample
Unexecuted instantiation: jcsample-16.c:h2v1_downsample
257
258
259
/*
260
 * Downsample pixel values of a single component.
261
 * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
262
 * without smoothing.
263
 */
264
265
METHODDEF(void)
266
h2v2_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,
267
                _JSAMPARRAY input_data, _JSAMPARRAY output_data)
268
0
{
269
0
  int inrow, outrow;
270
0
  JDIMENSION outcol;
271
0
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
272
0
  JDIMENSION output_cols = compptr->width_in_blocks * data_unit;
273
0
  register _JSAMPROW inptr0, inptr1, outptr;
274
0
  register int bias;
275
276
  /* Expand input data enough to let all the output samples be generated
277
   * by the standard loop.  Special-casing padded output would be more
278
   * efficient.
279
   */
280
0
  expand_right_edge(input_data, cinfo->max_v_samp_factor, cinfo->image_width,
281
0
                    output_cols * 2);
282
283
0
  inrow = 0;
284
0
  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
285
0
    outptr = output_data[outrow];
286
0
    inptr0 = input_data[inrow];
287
0
    inptr1 = input_data[inrow + 1];
288
0
    bias = 1;                   /* bias = 1,2,1,2,... for successive samples */
289
0
    for (outcol = 0; outcol < output_cols; outcol++) {
290
0
      *outptr++ = (_JSAMPLE)
291
0
        ((inptr0[0] + inptr0[1] + inptr1[0] + inptr1[1] + bias) >> 2);
292
0
      bias ^= 3;                /* 1=>2, 2=>1 */
293
0
      inptr0 += 2;  inptr1 += 2;
294
0
    }
295
0
    inrow += 2;
296
0
  }
297
0
}
Unexecuted instantiation: jcsample-8.c:h2v2_downsample
Unexecuted instantiation: jcsample-12.c:h2v2_downsample
Unexecuted instantiation: jcsample-16.c:h2v2_downsample
298
299
300
#ifdef INPUT_SMOOTHING_SUPPORTED
301
302
/*
303
 * Downsample pixel values of a single component.
304
 * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
305
 * with smoothing.  One row of context is required.
306
 */
307
308
METHODDEF(void)
309
h2v2_smooth_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,
310
                       _JSAMPARRAY input_data, _JSAMPARRAY output_data)
311
0
{
312
0
  int inrow, outrow;
313
0
  JDIMENSION colctr;
314
0
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
315
0
  JDIMENSION output_cols = compptr->width_in_blocks * data_unit;
316
0
  register _JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr;
317
0
  JLONG membersum, neighsum, memberscale, neighscale;
318
319
  /* Expand input data enough to let all the output samples be generated
320
   * by the standard loop.  Special-casing padded output would be more
321
   * efficient.
322
   */
323
0
  expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
324
0
                    cinfo->image_width, output_cols * 2);
325
326
  /* We don't bother to form the individual "smoothed" input pixel values;
327
   * we can directly compute the output which is the average of the four
328
   * smoothed values.  Each of the four member pixels contributes a fraction
329
   * (1-8*SF) to its own smoothed image and a fraction SF to each of the three
330
   * other smoothed pixels, therefore a total fraction (1-5*SF)/4 to the final
331
   * output.  The four corner-adjacent neighbor pixels contribute a fraction
332
   * SF to just one smoothed pixel, or SF/4 to the final output; while the
333
   * eight edge-adjacent neighbors contribute SF to each of two smoothed
334
   * pixels, or SF/2 overall.  In order to use integer arithmetic, these
335
   * factors are scaled by 2^16 = 65536.
336
   * Also recall that SF = smoothing_factor / 1024.
337
   */
338
339
0
  memberscale = 16384 - cinfo->smoothing_factor * 80; /* scaled (1-5*SF)/4 */
340
0
  neighscale = cinfo->smoothing_factor * 16; /* scaled SF/4 */
341
342
0
  inrow = 0;
343
0
  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
344
0
    outptr = output_data[outrow];
345
0
    inptr0 = input_data[inrow];
346
0
    inptr1 = input_data[inrow + 1];
347
0
    above_ptr = input_data[inrow - 1];
348
0
    below_ptr = input_data[inrow + 2];
349
350
    /* Special case for first column: pretend column -1 is same as column 0 */
351
0
    membersum = inptr0[0] + inptr0[1] + inptr1[0] + inptr1[1];
352
0
    neighsum = above_ptr[0] + above_ptr[1] + below_ptr[0] + below_ptr[1] +
353
0
               inptr0[0] + inptr0[2] + inptr1[0] + inptr1[2];
354
0
    neighsum += neighsum;
355
0
    neighsum += above_ptr[0] + above_ptr[2] + below_ptr[0] + below_ptr[2];
356
0
    membersum = membersum * memberscale + neighsum * neighscale;
357
0
    *outptr++ = (_JSAMPLE)((membersum + 32768) >> 16);
358
0
    inptr0 += 2;  inptr1 += 2;  above_ptr += 2;  below_ptr += 2;
359
360
0
    for (colctr = output_cols - 2; colctr > 0; colctr--) {
361
      /* sum of pixels directly mapped to this output element */
362
0
      membersum = inptr0[0] + inptr0[1] + inptr1[0] + inptr1[1];
363
      /* sum of edge-neighbor pixels */
364
0
      neighsum = above_ptr[0] + above_ptr[1] + below_ptr[0] + below_ptr[1] +
365
0
                 inptr0[-1] + inptr0[2] + inptr1[-1] + inptr1[2];
366
      /* The edge-neighbors count twice as much as corner-neighbors */
367
0
      neighsum += neighsum;
368
      /* Add in the corner-neighbors */
369
0
      neighsum += above_ptr[-1] + above_ptr[2] + below_ptr[-1] + below_ptr[2];
370
      /* form final output scaled up by 2^16 */
371
0
      membersum = membersum * memberscale + neighsum * neighscale;
372
      /* round, descale and output it */
373
0
      *outptr++ = (_JSAMPLE)((membersum + 32768) >> 16);
374
0
      inptr0 += 2;  inptr1 += 2;  above_ptr += 2;  below_ptr += 2;
375
0
    }
376
377
    /* Special case for last column */
378
0
    membersum = inptr0[0] + inptr0[1] + inptr1[0] + inptr1[1];
379
0
    neighsum = above_ptr[0] + above_ptr[1] + below_ptr[0] + below_ptr[1] +
380
0
               inptr0[-1] + inptr0[1] + inptr1[-1] + inptr1[1];
381
0
    neighsum += neighsum;
382
0
    neighsum += above_ptr[-1] + above_ptr[1] + below_ptr[-1] + below_ptr[1];
383
0
    membersum = membersum * memberscale + neighsum * neighscale;
384
0
    *outptr = (_JSAMPLE)((membersum + 32768) >> 16);
385
386
0
    inrow += 2;
387
0
  }
388
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
389
390
391
/*
392
 * Downsample pixel values of a single component.
393
 * This version handles the special case of a full-size component,
394
 * with smoothing.  One row of context is required.
395
 */
396
397
METHODDEF(void)
398
fullsize_smooth_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,
399
                           _JSAMPARRAY input_data, _JSAMPARRAY output_data)
400
0
{
401
0
  int outrow;
402
0
  JDIMENSION colctr;
403
0
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
404
0
  JDIMENSION output_cols = compptr->width_in_blocks * data_unit;
405
0
  register _JSAMPROW inptr, above_ptr, below_ptr, outptr;
406
0
  JLONG membersum, neighsum, memberscale, neighscale;
407
0
  int colsum, lastcolsum, nextcolsum;
408
409
  /* Expand input data enough to let all the output samples be generated
410
   * by the standard loop.  Special-casing padded output would be more
411
   * efficient.
412
   */
413
0
  expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
414
0
                    cinfo->image_width, output_cols);
415
416
  /* Each of the eight neighbor pixels contributes a fraction SF to the
417
   * smoothed pixel, while the main pixel contributes (1-8*SF).  In order
418
   * to use integer arithmetic, these factors are multiplied by 2^16 = 65536.
419
   * Also recall that SF = smoothing_factor / 1024.
420
   */
421
422
0
  memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */
423
0
  neighscale = cinfo->smoothing_factor * 64; /* scaled SF */
424
425
0
  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
426
0
    outptr = output_data[outrow];
427
0
    inptr = input_data[outrow];
428
0
    above_ptr = input_data[outrow - 1];
429
0
    below_ptr = input_data[outrow + 1];
430
431
    /* Special case for first column */
432
0
    colsum = (*above_ptr++) + (*below_ptr++) + inptr[0];
433
0
    membersum = *inptr++;
434
0
    nextcolsum = above_ptr[0] + below_ptr[0] + inptr[0];
435
0
    neighsum = colsum + (colsum - membersum) + nextcolsum;
436
0
    membersum = membersum * memberscale + neighsum * neighscale;
437
0
    *outptr++ = (_JSAMPLE)((membersum + 32768) >> 16);
438
0
    lastcolsum = colsum;  colsum = nextcolsum;
439
440
0
    for (colctr = output_cols - 2; colctr > 0; colctr--) {
441
0
      membersum = *inptr++;
442
0
      above_ptr++;  below_ptr++;
443
0
      nextcolsum = above_ptr[0] + below_ptr[0] + inptr[0];
444
0
      neighsum = lastcolsum + (colsum - membersum) + nextcolsum;
445
0
      membersum = membersum * memberscale + neighsum * neighscale;
446
0
      *outptr++ = (_JSAMPLE)((membersum + 32768) >> 16);
447
0
      lastcolsum = colsum;  colsum = nextcolsum;
448
0
    }
449
450
    /* Special case for last column */
451
0
    membersum = *inptr;
452
0
    neighsum = lastcolsum + (colsum - membersum) + colsum;
453
0
    membersum = membersum * memberscale + neighsum * neighscale;
454
0
    *outptr = (_JSAMPLE)((membersum + 32768) >> 16);
455
456
0
  }
457
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
458
459
#endif /* INPUT_SMOOTHING_SUPPORTED */
460
461
462
/*
463
 * Module initialization routine for downsampling.
464
 * Note that we must select a routine for each component.
465
 */
466
467
GLOBAL(void)
468
_jinit_downsampler(j_compress_ptr cinfo)
469
10.2k
{
470
10.2k
  my_downsample_ptr downsample;
471
10.2k
  int ci;
472
10.2k
  jpeg_component_info *compptr;
473
10.2k
  boolean smoothok = TRUE;
474
475
10.2k
#ifdef C_LOSSLESS_SUPPORTED
476
10.2k
  if (cinfo->master->lossless) {
477
#if BITS_IN_JSAMPLE == 8
478
0
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
479
#else
480
0
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
481
0
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
482
0
#endif
483
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
484
0
  } else
485
10.2k
#endif
486
10.2k
  {
487
10.2k
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
488
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
489
10.2k
  }
490
491
10.2k
  downsample = (my_downsample_ptr)
492
10.2k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
493
10.2k
                                sizeof(my_downsampler));
494
10.2k
  cinfo->downsample = (struct jpeg_downsampler *)downsample;
495
10.2k
  downsample->pub.start_pass = start_pass_downsample;
496
10.2k
  downsample->pub._downsample = sep_downsample;
497
10.2k
  downsample->pub.need_context_rows = FALSE;
498
499
10.2k
  if (cinfo->CCIR601_sampling)
500
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
501
502
  /* Verify we can handle the sampling factors, and set up method pointers */
503
34.4k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
504
24.2k
       ci++, compptr++) {
505
24.2k
    if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&
506
13.7k
        compptr->v_samp_factor == cinfo->max_v_samp_factor) {
507
13.7k
#ifdef INPUT_SMOOTHING_SUPPORTED
508
13.7k
      if (cinfo->smoothing_factor) {
509
0
        downsample->methods[ci] = fullsize_smooth_downsample;
510
0
        downsample->pub.need_context_rows = TRUE;
511
0
      } else
512
13.7k
#endif
513
13.7k
        downsample->methods[ci] = fullsize_downsample;
514
13.7k
    } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
515
6.99k
               compptr->v_samp_factor == cinfo->max_v_samp_factor) {
516
3.49k
      smoothok = FALSE;
517
#ifdef WITH_SIMD
518
3.49k
      if (jsimd_set_h2v1_downsample(cinfo))
519
3.49k
        downsample->methods[ci] = jsimd_h2v1_downsample;
520
0
      else
521
0
#endif
522
0
        downsample->methods[ci] = h2v1_downsample;
523
6.99k
    } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
524
3.49k
               compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) {
525
3.49k
#ifdef INPUT_SMOOTHING_SUPPORTED
526
3.49k
      if (cinfo->smoothing_factor) {
527
#if defined(WITH_SIMD) && SIMD_ARCHITECTURE == MIPS
528
        if (jsimd_set_h2v2_smooth_downsample(cinfo))
529
          downsample->methods[ci] = jsimd_h2v2_smooth_downsample;
530
        else
531
#endif
532
0
          downsample->methods[ci] = h2v2_smooth_downsample;
533
0
        downsample->pub.need_context_rows = TRUE;
534
0
      } else
535
3.49k
#endif
536
3.49k
      {
537
#ifdef WITH_SIMD
538
3.49k
        if (jsimd_set_h2v2_downsample(cinfo))
539
3.49k
          downsample->methods[ci] = jsimd_h2v2_downsample;
540
0
        else
541
0
#endif
542
0
          downsample->methods[ci] = h2v2_downsample;
543
3.49k
      }
544
3.49k
    } else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&
545
3.49k
               (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) {
546
3.49k
      smoothok = FALSE;
547
3.49k
      downsample->methods[ci] = int_downsample;
548
3.49k
    } else
549
0
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
550
24.2k
  }
551
552
10.2k
#ifdef INPUT_SMOOTHING_SUPPORTED
553
10.2k
  if (cinfo->smoothing_factor && !smoothok)
554
0
    TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL);
555
10.2k
#endif
556
10.2k
}
jinit_downsampler
Line
Count
Source
469
10.2k
{
470
10.2k
  my_downsample_ptr downsample;
471
10.2k
  int ci;
472
10.2k
  jpeg_component_info *compptr;
473
10.2k
  boolean smoothok = TRUE;
474
475
10.2k
#ifdef C_LOSSLESS_SUPPORTED
476
10.2k
  if (cinfo->master->lossless) {
477
0
#if BITS_IN_JSAMPLE == 8
478
0
    if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
479
#else
480
    if (cinfo->data_precision > BITS_IN_JSAMPLE ||
481
        cinfo->data_precision < BITS_IN_JSAMPLE - 3)
482
#endif
483
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
484
0
  } else
485
10.2k
#endif
486
10.2k
  {
487
10.2k
    if (cinfo->data_precision != BITS_IN_JSAMPLE)
488
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
489
10.2k
  }
490
491
10.2k
  downsample = (my_downsample_ptr)
492
10.2k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
493
10.2k
                                sizeof(my_downsampler));
494
10.2k
  cinfo->downsample = (struct jpeg_downsampler *)downsample;
495
10.2k
  downsample->pub.start_pass = start_pass_downsample;
496
10.2k
  downsample->pub._downsample = sep_downsample;
497
10.2k
  downsample->pub.need_context_rows = FALSE;
498
499
10.2k
  if (cinfo->CCIR601_sampling)
500
0
    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
501
502
  /* Verify we can handle the sampling factors, and set up method pointers */
503
34.4k
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
504
24.2k
       ci++, compptr++) {
505
24.2k
    if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&
506
13.7k
        compptr->v_samp_factor == cinfo->max_v_samp_factor) {
507
13.7k
#ifdef INPUT_SMOOTHING_SUPPORTED
508
13.7k
      if (cinfo->smoothing_factor) {
509
0
        downsample->methods[ci] = fullsize_smooth_downsample;
510
0
        downsample->pub.need_context_rows = TRUE;
511
0
      } else
512
13.7k
#endif
513
13.7k
        downsample->methods[ci] = fullsize_downsample;
514
13.7k
    } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
515
6.99k
               compptr->v_samp_factor == cinfo->max_v_samp_factor) {
516
3.49k
      smoothok = FALSE;
517
3.49k
#ifdef WITH_SIMD
518
3.49k
      if (jsimd_set_h2v1_downsample(cinfo))
519
3.49k
        downsample->methods[ci] = jsimd_h2v1_downsample;
520
0
      else
521
0
#endif
522
0
        downsample->methods[ci] = h2v1_downsample;
523
6.99k
    } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
524
3.49k
               compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) {
525
3.49k
#ifdef INPUT_SMOOTHING_SUPPORTED
526
3.49k
      if (cinfo->smoothing_factor) {
527
#if defined(WITH_SIMD) && SIMD_ARCHITECTURE == MIPS
528
        if (jsimd_set_h2v2_smooth_downsample(cinfo))
529
          downsample->methods[ci] = jsimd_h2v2_smooth_downsample;
530
        else
531
#endif
532
0
          downsample->methods[ci] = h2v2_smooth_downsample;
533
0
        downsample->pub.need_context_rows = TRUE;
534
0
      } else
535
3.49k
#endif
536
3.49k
      {
537
3.49k
#ifdef WITH_SIMD
538
3.49k
        if (jsimd_set_h2v2_downsample(cinfo))
539
3.49k
          downsample->methods[ci] = jsimd_h2v2_downsample;
540
0
        else
541
0
#endif
542
0
          downsample->methods[ci] = h2v2_downsample;
543
3.49k
      }
544
3.49k
    } else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&
545
3.49k
               (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) {
546
3.49k
      smoothok = FALSE;
547
3.49k
      downsample->methods[ci] = int_downsample;
548
3.49k
    } else
549
0
      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
550
24.2k
  }
551
552
10.2k
#ifdef INPUT_SMOOTHING_SUPPORTED
553
10.2k
  if (cinfo->smoothing_factor && !smoothok)
554
0
    TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL);
555
10.2k
#endif
556
10.2k
}
Unexecuted instantiation: j12init_downsampler
Unexecuted instantiation: j16init_downsampler
557
558
#endif /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */