source: golgotha/src/i4/loaders/jpg/jddctmgr.cc @ 608

Last change on this file since 608 was 80, checked in by Sam Hocevar, 15 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 8.9 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9/*
10 * jddctmgr.c
11 *
12 * Copyright (C) 1994-1996, Thomas G. Lane.
13 * This file is part of the Independent JPEG Group's software.
14 * For conditions of distribution and use, see the accompanying README file.
15 *
16 * This file contains the inverse-DCT management logic.
17 * This code selects a particular IDCT implementation to be used,
18 * and it performs related housekeeping chores.  No code in this file
19 * is executed per IDCT step, only during output pass setup.
20 *
21 * Note that the IDCT routines are responsible for performing coefficient
22 * dequantization as well as the IDCT proper.  This module sets up the
23 * dequantization multiplier table needed by the IDCT routine.
24 */
25
26#define JPEG_INTERNALS
27#include "loaders/jpg/jinclude.h"
28#include "loaders/jpg/jpeglib.h"
29#include "loaders/jpg/jdct.h"           /* Private declarations for DCT subsystem */
30
31
32/*
33 * The decompressor input side (jdinput.c) saves away the appropriate
34 * quantization table for each component at the start of the first scan
35 * involving that component.  (This is necessary in order to correctly
36 * decode files that reuse Q-table slots.)
37 * When we are ready to make an output pass, the saved Q-table is converted
38 * to a multiplier table that will actually be used by the IDCT routine.
39 * The multiplier table contents are IDCT-method-dependent.  To support
40 * application changes in IDCT method between scans, we can remake the
41 * multiplier tables if necessary.
42 * In buffered-image mode, the first output pass may occur before any data
43 * has been seen for some components, and thus before their Q-tables have
44 * been saved away.  To handle this case, multiplier tables are preset
45 * to zeroes; the result of the IDCT will be a neutral gray level.
46 */
47
48
49/* Private subobject for this module */
50
51typedef struct {
52  struct jpeg_inverse_dct pub;  /* public fields */
53
54  /* This array contains the IDCT method code that each multiplier table
55   * is currently set up for, or -1 if it's not yet set up.
56   * The actual multiplier tables are pointed to by dct_table in the
57   * per-component comp_info structures.
58   */
59  int cur_method[MAX_COMPONENTS];
60} my_idct_controller;
61
62typedef my_idct_controller * my_idct_ptr;
63
64
65/* Allocated multiplier tables: big enough for any supported variant */
66
67typedef union {
68  ISLOW_MULT_TYPE islow_array[DCTSIZE2];
69#ifdef DCT_IFAST_SUPPORTED
70  IFAST_MULT_TYPE ifast_array[DCTSIZE2];
71#endif
72#ifdef DCT_FLOAT_SUPPORTED
73  FLOAT_MULT_TYPE float_array[DCTSIZE2];
74#endif
75} multiplier_table;
76
77
78/* The current scaled-IDCT routines require ISLOW-style multiplier tables,
79 * so be sure to compile that code if either ISLOW or SCALING is requested.
80 */
81#ifdef DCT_ISLOW_SUPPORTED
82#define PROVIDE_ISLOW_TABLES
83#else
84#ifdef IDCT_SCALING_SUPPORTED
85#define PROVIDE_ISLOW_TABLES
86#endif
87#endif
88
89
90/*
91 * Prepare for an output pass.
92 * Here we select the proper IDCT routine for each component and build
93 * a matching multiplier table.
94 */
95
96METHODDEF(void)
97start_pass (j_decompress_ptr cinfo)
98{
99  my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
100  int ci, i;
101  jpeg_component_info *compptr;
102  int method = 0;
103  inverse_DCT_method_ptr method_ptr = NULL;
104  JQUANT_TBL * qtbl;
105
106  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
107       ci++, compptr++) {
108    /* Select the proper IDCT routine for this component's scaling */
109    switch (compptr->DCT_scaled_size) {
110#ifdef IDCT_SCALING_SUPPORTED
111    case 1:
112      method_ptr = jpeg_idct_1x1;
113      method = JDCT_ISLOW;      /* jidctred uses islow-style table */
114      break;
115    case 2:
116      method_ptr = jpeg_idct_2x2;
117      method = JDCT_ISLOW;      /* jidctred uses islow-style table */
118      break;
119    case 4:
120      method_ptr = jpeg_idct_4x4;
121      method = JDCT_ISLOW;      /* jidctred uses islow-style table */
122      break;
123#endif
124    case DCTSIZE:
125      switch (cinfo->dct_method) {
126#ifdef DCT_ISLOW_SUPPORTED
127      case JDCT_ISLOW:
128        method_ptr = jpeg_idct_islow;
129        method = JDCT_ISLOW;
130        break;
131#endif
132#ifdef DCT_IFAST_SUPPORTED
133      case JDCT_IFAST:
134        method_ptr = jpeg_idct_ifast;
135        method = JDCT_IFAST;
136        break;
137#endif
138#ifdef DCT_FLOAT_SUPPORTED
139      case JDCT_FLOAT:
140        method_ptr = jpeg_idct_float;
141        method = JDCT_FLOAT;
142        break;
143#endif
144      default:
145        ERREXIT(cinfo, JERR_NOT_COMPILED);
146        break;
147      }
148      break;
149    default:
150      ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
151      break;
152    }
153    idct->pub.inverse_DCT[ci] = method_ptr;
154    /* Create multiplier table from quant table.
155     * However, we can skip this if the component is uninteresting
156     * or if we already built the table.  Also, if no quant table
157     * has yet been saved for the component, we leave the
158     * multiplier table all-zero; we'll be reading zeroes from the
159     * coefficient controller's buffer anyway.
160     */
161    if (! compptr->component_needed || idct->cur_method[ci] == method)
162      continue;
163    qtbl = compptr->quant_table;
164    if (qtbl == NULL)           /* happens if no data yet for component */
165      continue;
166    idct->cur_method[ci] = method;
167    switch (method) {
168#ifdef PROVIDE_ISLOW_TABLES
169    case JDCT_ISLOW:
170      {
171        /* For LL&M IDCT method, multipliers are equal to raw quantization
172         * coefficients, but are stored as ints to ensure access efficiency.
173         */
174        ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
175        for (i = 0; i < DCTSIZE2; i++) {
176          ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
177        }
178      }
179      break;
180#endif
181#ifdef DCT_IFAST_SUPPORTED
182    case JDCT_IFAST:
183      {
184        /* For AA&N IDCT method, multipliers are equal to quantization
185         * coefficients scaled by scalefactor[row]*scalefactor[col], where
186         *   scalefactor[0] = 1
187         *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
188         * For integer operation, the multiplier table is to be scaled by
189         * IFAST_SCALE_BITS.
190         */
191        IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
192#define CONST_BITS 14
193        static const INT16 aanscales[DCTSIZE2] = {
194          /* precomputed values scaled up by 14 bits */
195          16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
196          22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
197          21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
198          19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
199          16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
200          12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
201           8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
202           4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
203        };
204        SHIFT_TEMPS
205
206        for (i = 0; i < DCTSIZE2; i++) {
207          ifmtbl[i] = (IFAST_MULT_TYPE)
208            DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
209                                  (INT32) aanscales[i]),
210                    CONST_BITS-IFAST_SCALE_BITS);
211        }
212      }
213      break;
214#endif
215#ifdef DCT_FLOAT_SUPPORTED
216    case JDCT_FLOAT:
217      {
218        /* For float AA&N IDCT method, multipliers are equal to quantization
219         * coefficients scaled by scalefactor[row]*scalefactor[col], where
220         *   scalefactor[0] = 1
221         *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
222         */
223        FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
224        int row, col;
225        static const double aanscalefactor[DCTSIZE] = {
226          1.0, 1.387039845, 1.306562965, 1.175875602,
227          1.0, 0.785694958, 0.541196100, 0.275899379
228        };
229
230        i = 0;
231        for (row = 0; row < DCTSIZE; row++) {
232          for (col = 0; col < DCTSIZE; col++) {
233            fmtbl[i] = (FLOAT_MULT_TYPE)
234              ((double) qtbl->quantval[i] *
235               aanscalefactor[row] * aanscalefactor[col]);
236            i++;
237          }
238        }
239      }
240      break;
241#endif
242    default:
243      ERREXIT(cinfo, JERR_NOT_COMPILED);
244      break;
245    }
246  }
247}
248
249
250/*
251 * Initialize IDCT manager.
252 */
253
254GLOBAL(void)
255jinit_inverse_dct (j_decompress_ptr cinfo)
256{
257  my_idct_ptr idct;
258  int ci;
259  jpeg_component_info *compptr;
260
261  idct = (my_idct_ptr)
262    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
263                                SIZEOF(my_idct_controller));
264  cinfo->idct = (struct jpeg_inverse_dct *) idct;
265  idct->pub.start_pass = start_pass;
266
267  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
268       ci++, compptr++) {
269    /* Allocate and pre-zero a multiplier table for each component */
270    compptr->dct_table =
271      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
272                                  SIZEOF(multiplier_table));
273    MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
274    /* Mark multiplier table not yet set up for any method */
275    idct->cur_method[ci] = -1;
276  }
277}
Note: See TracBrowser for help on using the repository browser.