]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/jpeg6/jddctmgr.cpp
more eol-style
[xonotic/netradiant.git] / libs / jpeg6 / jddctmgr.cpp
1 /*
2
3  * jddctmgr.c
4
5  *
6
7  * Copyright (C) 1994-1995, Thomas G. Lane.
8
9  * This file is part of the Independent JPEG Group's software.
10
11  * For conditions of distribution and use, see the accompanying README file.
12
13  *
14
15  * This file contains the inverse-DCT management logic.
16
17  * This code selects a particular IDCT implementation to be used,
18
19  * and it performs related housekeeping chores.  No code in this file
20
21  * is executed per IDCT step, only during output pass setup.
22
23  *
24
25  * Note that the IDCT routines are responsible for performing coefficient
26
27  * dequantization as well as the IDCT proper.  This module sets up the
28
29  * dequantization multiplier table needed by the IDCT routine.
30
31  */
32
33
34
35 #define JPEG_INTERNALS
36
37 #include "jinclude.h"
38
39 #include "radiant_jpeglib.h"
40
41 #include "jdct.h"               /* Private declarations for DCT subsystem */
42
43
44
45
46
47 /*
48
49  * The decompressor input side (jdinput.c) saves away the appropriate
50
51  * quantization table for each component at the start of the first scan
52
53  * involving that component.  (This is necessary in order to correctly
54
55  * decode files that reuse Q-table slots.)
56
57  * When we are ready to make an output pass, the saved Q-table is converted
58
59  * to a multiplier table that will actually be used by the IDCT routine.
60
61  * The multiplier table contents are IDCT-method-dependent.  To support
62
63  * application changes in IDCT method between scans, we can remake the
64
65  * multiplier tables if necessary.
66
67  * In buffered-image mode, the first output pass may occur before any data
68
69  * has been seen for some components, and thus before their Q-tables have
70
71  * been saved away.  To handle this case, multiplier tables are preset
72
73  * to zeroes; the result of the IDCT will be a neutral gray level.
74
75  */
76
77
78
79
80
81 /* Private subobject for this module */
82
83
84
85 typedef struct {
86
87   struct jpeg_inverse_dct pub;  /* public fields */
88
89
90
91   /* This array contains the IDCT method code that each multiplier table
92
93    * is currently set up for, or -1 if it's not yet set up.
94
95    * The actual multiplier tables are pointed to by dct_table in the
96
97    * per-component comp_info structures.
98
99    */
100
101   int cur_method[MAX_COMPONENTS];
102
103 } my_idct_controller;
104
105
106
107 typedef my_idct_controller * my_idct_ptr;
108
109
110
111
112
113 /* Allocated multiplier tables: big enough for any supported variant */
114
115
116
117 typedef union {
118
119   ISLOW_MULT_TYPE islow_array[DCTSIZE2];
120
121 #ifdef DCT_IFAST_SUPPORTED
122
123   IFAST_MULT_TYPE ifast_array[DCTSIZE2];
124
125 #endif
126
127 #ifdef DCT_FLOAT_SUPPORTED
128
129   FLOAT_MULT_TYPE float_array[DCTSIZE2];
130
131 #endif
132
133 } multiplier_table;
134
135
136
137
138
139 /* The current scaled-IDCT routines require ISLOW-style multiplier tables,
140
141  * so be sure to compile that code if either ISLOW or SCALING is requested.
142
143  */
144
145 #ifdef DCT_ISLOW_SUPPORTED
146
147 #define PROVIDE_ISLOW_TABLES
148
149 #else
150
151 #ifdef IDCT_SCALING_SUPPORTED
152
153 #define PROVIDE_ISLOW_TABLES
154
155 #endif
156
157 #endif
158
159
160
161
162
163 /*
164
165  * Prepare for an output pass.
166
167  * Here we select the proper IDCT routine for each component and build
168
169  * a matching multiplier table.
170
171  */
172
173
174
175 METHODDEF void
176
177 start_pass (j_decompress_ptr cinfo)
178
179 {
180
181   my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
182
183   int ci, i;
184
185   jpeg_component_info *compptr;
186
187   int method = 0;
188
189   inverse_DCT_method_ptr method_ptr = NULL;
190
191   JQUANT_TBL * qtbl;
192
193
194
195   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
196
197        ci++, compptr++) {
198
199     /* Select the proper IDCT routine for this component's scaling */
200
201     switch (compptr->DCT_scaled_size) {
202
203 #ifdef IDCT_SCALING_SUPPORTED
204
205     case 1:
206
207       method_ptr = jpeg_idct_1x1;
208
209       method = JDCT_ISLOW;      /* jidctred uses islow-style table */
210
211       break;
212
213     case 2:
214
215       method_ptr = jpeg_idct_2x2;
216
217       method = JDCT_ISLOW;      /* jidctred uses islow-style table */
218
219       break;
220
221     case 4:
222
223       method_ptr = jpeg_idct_4x4;
224
225       method = JDCT_ISLOW;      /* jidctred uses islow-style table */
226
227       break;
228
229 #endif
230
231     case DCTSIZE:
232
233       switch (cinfo->dct_method) {
234
235 #ifdef DCT_ISLOW_SUPPORTED
236
237       case JDCT_ISLOW:
238
239         method_ptr = jpeg_idct_islow;
240
241         method = JDCT_ISLOW;
242
243         break;
244
245 #endif
246
247 #ifdef DCT_IFAST_SUPPORTED
248
249       case JDCT_IFAST:
250
251         method_ptr = jpeg_idct_ifast;
252
253         method = JDCT_IFAST;
254
255         break;
256
257 #endif
258
259 #ifdef DCT_FLOAT_SUPPORTED
260
261       case JDCT_FLOAT:
262
263         method_ptr = jpeg_idct_float;
264
265         method = JDCT_FLOAT;
266
267         break;
268
269 #endif
270
271       default:
272
273         ERREXIT(cinfo, JERR_NOT_COMPILED);
274
275         break;
276
277       }
278
279       break;
280
281     default:
282
283       ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
284
285       break;
286
287     }
288
289     idct->pub.inverse_DCT[ci] = method_ptr;
290
291     /* Create multiplier table from quant table.
292
293      * However, we can skip this if the component is uninteresting
294
295      * or if we already built the table.  Also, if no quant table
296
297      * has yet been saved for the component, we leave the
298
299      * multiplier table all-zero; we'll be reading zeroes from the
300
301      * coefficient controller's buffer anyway.
302
303      */
304
305     if (! compptr->component_needed || idct->cur_method[ci] == method)
306
307       continue;
308
309     qtbl = compptr->quant_table;
310
311     if (qtbl == NULL)           /* happens if no data yet for component */
312
313       continue;
314
315     idct->cur_method[ci] = method;
316
317     switch (method) {
318
319 #ifdef PROVIDE_ISLOW_TABLES
320
321     case JDCT_ISLOW:
322
323       {
324
325         /* For LL&M IDCT method, multipliers are equal to raw quantization
326
327          * coefficients, but are stored in natural order as ints.
328
329          */
330
331         ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
332
333         for (i = 0; i < DCTSIZE2; i++) {
334
335           ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[jpeg_zigzag_order[i]];
336
337         }
338
339       }
340
341       break;
342
343 #endif
344
345 #ifdef DCT_IFAST_SUPPORTED
346
347     case JDCT_IFAST:
348
349       {
350
351         /* For AA&N IDCT method, multipliers are equal to quantization
352
353          * coefficients scaled by scalefactor[row]*scalefactor[col], where
354
355          *   scalefactor[0] = 1
356
357          *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
358
359          * For integer operation, the multiplier table is to be scaled by
360
361          * IFAST_SCALE_BITS.  The multipliers are stored in natural order.
362
363          */
364
365         IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
366
367 #define CONST_BITS 14
368
369         static const INT16 aanscales[DCTSIZE2] = {
370
371           /* precomputed values scaled up by 14 bits */
372
373           16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
374
375           22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
376
377           21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
378
379           19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
380
381           16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
382
383           12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
384
385            8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
386
387            4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
388
389         };
390
391         SHIFT_TEMPS
392
393
394
395         for (i = 0; i < DCTSIZE2; i++) {
396
397           ifmtbl[i] = (IFAST_MULT_TYPE)
398
399             DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[jpeg_zigzag_order[i]],
400
401                                   (INT32) aanscales[i]),
402
403                     CONST_BITS-IFAST_SCALE_BITS);
404
405         }
406
407       }
408
409       break;
410
411 #endif
412
413 #ifdef DCT_FLOAT_SUPPORTED
414
415     case JDCT_FLOAT:
416
417       {
418
419         /* For float AA&N IDCT method, multipliers are equal to quantization
420
421          * coefficients scaled by scalefactor[row]*scalefactor[col], where
422
423          *   scalefactor[0] = 1
424
425          *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
426
427          * The multipliers are stored in natural order.
428
429          */
430
431         FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
432
433         int row, col;
434
435         static const double aanscalefactor[DCTSIZE] = {
436
437           1.0, 1.387039845, 1.306562965, 1.175875602,
438
439           1.0, 0.785694958, 0.541196100, 0.275899379
440
441         };
442
443
444
445         i = 0;
446
447         for (row = 0; row < DCTSIZE; row++) {
448
449           for (col = 0; col < DCTSIZE; col++) {
450
451             fmtbl[i] = (FLOAT_MULT_TYPE)
452
453               ((double) qtbl->quantval[jpeg_zigzag_order[i]] *
454
455                aanscalefactor[row] * aanscalefactor[col]);
456
457             i++;
458
459           }
460
461         }
462
463       }
464
465       break;
466
467 #endif
468
469     default:
470
471       ERREXIT(cinfo, JERR_NOT_COMPILED);
472
473       break;
474
475     }
476
477   }
478
479 }
480
481
482
483
484
485 /*
486
487  * Initialize IDCT manager.
488
489  */
490
491
492
493 GLOBAL void
494
495 jinit_inverse_dct (j_decompress_ptr cinfo)
496
497 {
498
499   my_idct_ptr idct;
500
501   int ci;
502
503   jpeg_component_info *compptr;
504
505
506
507   idct = (my_idct_ptr)
508
509     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
510
511                                 SIZEOF(my_idct_controller));
512
513   cinfo->idct = (struct jpeg_inverse_dct *) idct;
514
515   idct->pub.start_pass = start_pass;
516
517
518
519   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
520
521        ci++, compptr++) {
522
523     /* Allocate and pre-zero a multiplier table for each component */
524
525     compptr->dct_table =
526
527       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
528
529                                   SIZEOF(multiplier_table));
530
531     MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
532
533     /* Mark multiplier table not yet set up for any method */
534
535     idct->cur_method[ci] = -1;
536
537   }
538
539 }
540