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