Coverage Report

Created: 2026-03-31 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mupdf/thirdparty/libjpeg/jcdctmgr.c
Line
Count
Source
1
/*
2
 * jcdctmgr.c
3
 *
4
 * Copyright (C) 1994-1996, Thomas G. Lane.
5
 * Modified 2003-2020 by Guido Vollbeding.
6
 * This file is part of the Independent JPEG Group's software.
7
 * For conditions of distribution and use, see the accompanying README file.
8
 *
9
 * This file contains the forward-DCT management logic.
10
 * This code selects a particular DCT implementation to be used,
11
 * and it performs related housekeeping chores including coefficient
12
 * quantization.
13
 */
14
15
#define JPEG_INTERNALS
16
#include "jinclude.h"
17
#include "jpeglib.h"
18
#include "jdct.h"   /* Private declarations for DCT subsystem */
19
20
21
/* Private subobject for this module */
22
23
typedef struct {
24
  struct jpeg_forward_dct pub;  /* public fields */
25
26
  /* Pointer to the DCT routine actually in use */
27
  forward_DCT_method_ptr do_dct[MAX_COMPONENTS];
28
29
#ifdef DCT_FLOAT_SUPPORTED
30
  /* Same as above for the floating-point case. */
31
  float_DCT_method_ptr do_float_dct[MAX_COMPONENTS];
32
#endif
33
} my_fdct_controller;
34
35
typedef my_fdct_controller * my_fdct_ptr;
36
37
38
/* The allocated post-DCT divisor tables -- big enough for any
39
 * supported variant and not identical to the quant table entries,
40
 * because of scaling (especially for an unnormalized DCT) --
41
 * are pointed to by dct_table in the per-component comp_info
42
 * structures.  Each table is given in normal array order.
43
 */
44
45
typedef union {
46
  DCTELEM int_array[DCTSIZE2];
47
#ifdef DCT_FLOAT_SUPPORTED
48
  FAST_FLOAT float_array[DCTSIZE2];
49
#endif
50
} divisor_table;
51
52
53
/* The current scaled-DCT routines require ISLOW-style divisor tables,
54
 * so be sure to compile that code if either ISLOW or SCALING is requested.
55
 */
56
#ifdef DCT_ISLOW_SUPPORTED
57
#define PROVIDE_ISLOW_TABLES
58
#else
59
#ifdef DCT_SCALING_SUPPORTED
60
#define PROVIDE_ISLOW_TABLES
61
#endif
62
#endif
63
64
65
/*
66
 * Perform forward DCT on one or more blocks of a component.
67
 *
68
 * The input samples are taken from the sample_data[] array starting at
69
 * position start_col, and moving to the right for any additional blocks.
70
 * The quantized coefficients are returned in coef_blocks[].
71
 */
72
73
METHODDEF(void)
74
forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
75
       JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
76
       JDIMENSION start_col, JDIMENSION num_blocks)
77
/* This version is used for integer DCT implementations. */
78
0
{
79
  /* This routine is heavily used, so it's worth coding it tightly. */
80
0
  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
81
0
  forward_DCT_method_ptr do_dct = fdct->do_dct[compptr->component_index];
82
0
  DCTELEM * divisors = (DCTELEM *) compptr->dct_table;
83
0
  DCTELEM workspace[DCTSIZE2];  /* work area for FDCT subroutine */
84
0
  JDIMENSION bi;
85
86
0
  for (bi = 0; bi < num_blocks; bi++, start_col += compptr->DCT_h_scaled_size) {
87
    /* Perform the DCT */
88
0
    (*do_dct) (workspace, sample_data, start_col);
89
90
    /* Quantize/descale the coefficients, and store into coef_blocks[] */
91
0
    { register DCTELEM temp, qval;
92
0
      register int i;
93
0
      register JCOEFPTR output_ptr = coef_blocks[bi];
94
95
0
      for (i = 0; i < DCTSIZE2; i++) {
96
0
  qval = divisors[i];
97
0
  temp = workspace[i];
98
  /* Divide the coefficient value by qval, ensuring proper rounding.
99
   * Since C does not specify the direction of rounding for negative
100
   * quotients, we have to force the dividend positive for portability.
101
   *
102
   * In most files, at least half of the output values will be zero
103
   * (at default quantization settings, more like three-quarters...)
104
   * so we should ensure that this case is fast.  On many machines,
105
   * a comparison is enough cheaper than a divide to make a special test
106
   * a win.  Since both inputs will be nonnegative, we need only test
107
   * for a < b to discover whether a/b is 0.
108
   * If your machine's division is fast enough, define FAST_DIVIDE.
109
   */
110
#ifdef FAST_DIVIDE
111
#define DIVIDE_BY(a,b)  a /= b
112
#else
113
0
#define DIVIDE_BY(a,b)  if (a >= b) a /= b; else a = 0
114
0
#endif
115
0
  if (temp < 0) {
116
0
    temp = -temp;
117
0
    temp += qval>>1;  /* for rounding */
118
0
    DIVIDE_BY(temp, qval);
119
0
    temp = -temp;
120
0
  } else {
121
0
    temp += qval>>1;  /* for rounding */
122
0
    DIVIDE_BY(temp, qval);
123
0
  }
124
0
  output_ptr[i] = (JCOEF) temp;
125
0
      }
126
0
    }
127
0
  }
128
0
}
129
130
131
#ifdef DCT_FLOAT_SUPPORTED
132
133
METHODDEF(void)
134
forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
135
       JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
136
       JDIMENSION start_col, JDIMENSION num_blocks)
137
/* This version is used for floating-point DCT implementations. */
138
0
{
139
  /* This routine is heavily used, so it's worth coding it tightly. */
140
0
  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
141
0
  float_DCT_method_ptr do_dct = fdct->do_float_dct[compptr->component_index];
142
0
  FAST_FLOAT * divisors = (FAST_FLOAT *) compptr->dct_table;
143
0
  FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
144
0
  JDIMENSION bi;
145
146
0
  for (bi = 0; bi < num_blocks; bi++, start_col += compptr->DCT_h_scaled_size) {
147
    /* Perform the DCT */
148
0
    (*do_dct) (workspace, sample_data, start_col);
149
150
    /* Quantize/descale the coefficients, and store into coef_blocks[] */
151
0
    { register FAST_FLOAT temp;
152
0
      register int i;
153
0
      register JCOEFPTR output_ptr = coef_blocks[bi];
154
155
0
      for (i = 0; i < DCTSIZE2; i++) {
156
  /* Apply the quantization and scaling factor */
157
0
  temp = workspace[i] * divisors[i];
158
  /* Round to nearest integer.
159
   * Since C does not specify the direction of rounding for negative
160
   * quotients, we have to force the dividend positive for portability.
161
   * The maximum coefficient size is +-16K (for 12-bit data), so this
162
   * code should work for either 16-bit or 32-bit ints.
163
   */
164
0
  output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
165
0
      }
166
0
    }
167
0
  }
168
0
}
169
170
#endif /* DCT_FLOAT_SUPPORTED */
171
172
173
/*
174
 * Initialize for a processing pass.
175
 * Verify that all referenced Q-tables are present, and set up
176
 * the divisor table for each one.
177
 * In the current implementation, DCT of all components is done during
178
 * the first pass, even if only some components will be output in the
179
 * first scan.  Hence all components should be examined here.
180
 */
181
182
METHODDEF(void)
183
start_pass_fdctmgr (j_compress_ptr cinfo)
184
0
{
185
0
  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
186
0
  int ci, qtblno, i;
187
0
  jpeg_component_info *compptr;
188
0
  int method = 0;
189
0
  JQUANT_TBL * qtbl;
190
0
  DCTELEM * dtbl;
191
192
0
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
193
0
       ci++, compptr++) {
194
    /* Select the proper DCT routine for this component's scaling */
195
0
    switch ((compptr->DCT_h_scaled_size << 8) + compptr->DCT_v_scaled_size) {
196
0
#ifdef DCT_SCALING_SUPPORTED
197
0
    case ((1 << 8) + 1):
198
0
      fdct->do_dct[ci] = jpeg_fdct_1x1;
199
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
200
0
      break;
201
0
    case ((2 << 8) + 2):
202
0
      fdct->do_dct[ci] = jpeg_fdct_2x2;
203
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
204
0
      break;
205
0
    case ((3 << 8) + 3):
206
0
      fdct->do_dct[ci] = jpeg_fdct_3x3;
207
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
208
0
      break;
209
0
    case ((4 << 8) + 4):
210
0
      fdct->do_dct[ci] = jpeg_fdct_4x4;
211
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
212
0
      break;
213
0
    case ((5 << 8) + 5):
214
0
      fdct->do_dct[ci] = jpeg_fdct_5x5;
215
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
216
0
      break;
217
0
    case ((6 << 8) + 6):
218
0
      fdct->do_dct[ci] = jpeg_fdct_6x6;
219
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
220
0
      break;
221
0
    case ((7 << 8) + 7):
222
0
      fdct->do_dct[ci] = jpeg_fdct_7x7;
223
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
224
0
      break;
225
0
    case ((9 << 8) + 9):
226
0
      fdct->do_dct[ci] = jpeg_fdct_9x9;
227
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
228
0
      break;
229
0
    case ((10 << 8) + 10):
230
0
      fdct->do_dct[ci] = jpeg_fdct_10x10;
231
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
232
0
      break;
233
0
    case ((11 << 8) + 11):
234
0
      fdct->do_dct[ci] = jpeg_fdct_11x11;
235
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
236
0
      break;
237
0
    case ((12 << 8) + 12):
238
0
      fdct->do_dct[ci] = jpeg_fdct_12x12;
239
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
240
0
      break;
241
0
    case ((13 << 8) + 13):
242
0
      fdct->do_dct[ci] = jpeg_fdct_13x13;
243
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
244
0
      break;
245
0
    case ((14 << 8) + 14):
246
0
      fdct->do_dct[ci] = jpeg_fdct_14x14;
247
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
248
0
      break;
249
0
    case ((15 << 8) + 15):
250
0
      fdct->do_dct[ci] = jpeg_fdct_15x15;
251
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
252
0
      break;
253
0
    case ((16 << 8) + 16):
254
0
      fdct->do_dct[ci] = jpeg_fdct_16x16;
255
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
256
0
      break;
257
0
    case ((16 << 8) + 8):
258
0
      fdct->do_dct[ci] = jpeg_fdct_16x8;
259
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
260
0
      break;
261
0
    case ((14 << 8) + 7):
262
0
      fdct->do_dct[ci] = jpeg_fdct_14x7;
263
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
264
0
      break;
265
0
    case ((12 << 8) + 6):
266
0
      fdct->do_dct[ci] = jpeg_fdct_12x6;
267
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
268
0
      break;
269
0
    case ((10 << 8) + 5):
270
0
      fdct->do_dct[ci] = jpeg_fdct_10x5;
271
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
272
0
      break;
273
0
    case ((8 << 8) + 4):
274
0
      fdct->do_dct[ci] = jpeg_fdct_8x4;
275
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
276
0
      break;
277
0
    case ((6 << 8) + 3):
278
0
      fdct->do_dct[ci] = jpeg_fdct_6x3;
279
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
280
0
      break;
281
0
    case ((4 << 8) + 2):
282
0
      fdct->do_dct[ci] = jpeg_fdct_4x2;
283
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
284
0
      break;
285
0
    case ((2 << 8) + 1):
286
0
      fdct->do_dct[ci] = jpeg_fdct_2x1;
287
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
288
0
      break;
289
0
    case ((8 << 8) + 16):
290
0
      fdct->do_dct[ci] = jpeg_fdct_8x16;
291
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
292
0
      break;
293
0
    case ((7 << 8) + 14):
294
0
      fdct->do_dct[ci] = jpeg_fdct_7x14;
295
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
296
0
      break;
297
0
    case ((6 << 8) + 12):
298
0
      fdct->do_dct[ci] = jpeg_fdct_6x12;
299
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
300
0
      break;
301
0
    case ((5 << 8) + 10):
302
0
      fdct->do_dct[ci] = jpeg_fdct_5x10;
303
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
304
0
      break;
305
0
    case ((4 << 8) + 8):
306
0
      fdct->do_dct[ci] = jpeg_fdct_4x8;
307
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
308
0
      break;
309
0
    case ((3 << 8) + 6):
310
0
      fdct->do_dct[ci] = jpeg_fdct_3x6;
311
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
312
0
      break;
313
0
    case ((2 << 8) + 4):
314
0
      fdct->do_dct[ci] = jpeg_fdct_2x4;
315
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
316
0
      break;
317
0
    case ((1 << 8) + 2):
318
0
      fdct->do_dct[ci] = jpeg_fdct_1x2;
319
0
      method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
320
0
      break;
321
0
#endif
322
0
    case ((DCTSIZE << 8) + DCTSIZE):
323
0
      switch (cinfo->dct_method) {
324
0
#ifdef DCT_ISLOW_SUPPORTED
325
0
      case JDCT_ISLOW:
326
0
  fdct->do_dct[ci] = jpeg_fdct_islow;
327
0
  method = JDCT_ISLOW;
328
0
  break;
329
0
#endif
330
0
#ifdef DCT_IFAST_SUPPORTED
331
0
      case JDCT_IFAST:
332
0
  fdct->do_dct[ci] = jpeg_fdct_ifast;
333
0
  method = JDCT_IFAST;
334
0
  break;
335
0
#endif
336
0
#ifdef DCT_FLOAT_SUPPORTED
337
0
      case JDCT_FLOAT:
338
0
  fdct->do_float_dct[ci] = jpeg_fdct_float;
339
0
  method = JDCT_FLOAT;
340
0
  break;
341
0
#endif
342
0
      default:
343
0
  ERREXIT(cinfo, JERR_NOT_COMPILED);
344
0
      }
345
0
      break;
346
0
    default:
347
0
      ERREXIT2(cinfo, JERR_BAD_DCTSIZE,
348
0
         compptr->DCT_h_scaled_size, compptr->DCT_v_scaled_size);
349
0
    }
350
0
    qtblno = compptr->quant_tbl_no;
351
    /* Make sure specified quantization table is present */
352
0
    if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
353
0
  cinfo->quant_tbl_ptrs[qtblno] == NULL)
354
0
      ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
355
0
    qtbl = cinfo->quant_tbl_ptrs[qtblno];
356
    /* Create divisor table from quant table */
357
0
    switch (method) {
358
0
#ifdef PROVIDE_ISLOW_TABLES
359
0
    case JDCT_ISLOW:
360
      /* For LL&M IDCT method, divisors are equal to raw quantization
361
       * coefficients multiplied by 8 (to counteract scaling).
362
       */
363
0
      dtbl = (DCTELEM *) compptr->dct_table;
364
0
      for (i = 0; i < DCTSIZE2; i++) {
365
0
  dtbl[i] =
366
0
    ((DCTELEM) qtbl->quantval[i]) << (compptr->component_needed ? 4 : 3);
367
0
      }
368
0
      fdct->pub.forward_DCT[ci] = forward_DCT;
369
0
      break;
370
0
#endif
371
0
#ifdef DCT_IFAST_SUPPORTED
372
0
    case JDCT_IFAST:
373
0
      {
374
  /* For AA&N IDCT method, divisors are equal to quantization
375
   * coefficients scaled by scalefactor[row]*scalefactor[col], where
376
   *   scalefactor[0] = 1
377
   *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
378
   * We apply a further scale factor of 8.
379
   */
380
0
#define CONST_BITS 14
381
0
  static const INT16 aanscales[DCTSIZE2] = {
382
    /* precomputed values scaled up by 14 bits */
383
0
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
384
0
    22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
385
0
    21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
386
0
    19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
387
0
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
388
0
    12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
389
0
     8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
390
0
     4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
391
0
  };
392
0
  SHIFT_TEMPS
393
394
0
  dtbl = (DCTELEM *) compptr->dct_table;
395
0
  for (i = 0; i < DCTSIZE2; i++) {
396
0
    dtbl[i] = (DCTELEM)
397
0
      DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
398
0
          (INT32) aanscales[i]),
399
0
        compptr->component_needed ? CONST_BITS-4 : CONST_BITS-3);
400
0
  }
401
0
      }
402
0
      fdct->pub.forward_DCT[ci] = forward_DCT;
403
0
      break;
404
0
#endif
405
0
#ifdef DCT_FLOAT_SUPPORTED
406
0
    case JDCT_FLOAT:
407
0
      {
408
  /* For float AA&N IDCT method, divisors are equal to quantization
409
   * coefficients scaled by scalefactor[row]*scalefactor[col], where
410
   *   scalefactor[0] = 1
411
   *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
412
   * We apply a further scale factor of 8.
413
   * What's actually stored is 1/divisor so that the inner loop can
414
   * use a multiplication rather than a division.
415
   */
416
0
  FAST_FLOAT * fdtbl = (FAST_FLOAT *) compptr->dct_table;
417
0
  int row, col;
418
0
  static const double aanscalefactor[DCTSIZE] = {
419
0
    1.0, 1.387039845, 1.306562965, 1.175875602,
420
0
    1.0, 0.785694958, 0.541196100, 0.275899379
421
0
  };
422
423
0
  i = 0;
424
0
  for (row = 0; row < DCTSIZE; row++) {
425
0
    for (col = 0; col < DCTSIZE; col++) {
426
0
      fdtbl[i] = (FAST_FLOAT)
427
0
        (1.0 / ((double) qtbl->quantval[i] *
428
0
          aanscalefactor[row] * aanscalefactor[col] *
429
0
          (compptr->component_needed ? 16.0 : 8.0)));
430
0
      i++;
431
0
    }
432
0
  }
433
0
      }
434
0
      fdct->pub.forward_DCT[ci] = forward_DCT_float;
435
0
      break;
436
0
#endif
437
0
    default:
438
0
      ERREXIT(cinfo, JERR_NOT_COMPILED);
439
0
    }
440
0
  }
441
0
}
442
443
444
/*
445
 * Initialize FDCT manager.
446
 */
447
448
GLOBAL(void)
449
jinit_forward_dct (j_compress_ptr cinfo)
450
0
{
451
0
  my_fdct_ptr fdct;
452
0
  int ci;
453
0
  jpeg_component_info *compptr;
454
455
0
  fdct = (my_fdct_ptr) (*cinfo->mem->alloc_small)
456
0
    ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_fdct_controller));
457
0
  cinfo->fdct = &fdct->pub;
458
0
  fdct->pub.start_pass = start_pass_fdctmgr;
459
460
0
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
461
0
       ci++, compptr++) {
462
    /* Allocate a divisor table for each component */
463
0
    compptr->dct_table = (*cinfo->mem->alloc_small)
464
0
      ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(divisor_table));
465
0
  }
466
0
}