Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / compiler / libjpeg / main / jddctmgr.c
blob565d1b1bda3748063fc1bde018b3ac371697e57b
1 /*
2 $Id$
3 */
5 /*
6 * jddctmgr.c
8 * Copyright (C) 1994-1998, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains the inverse-DCT management logic.
13 * This code selects a particular IDCT implementation to be used,
14 * and it performs related housekeeping chores. No code in this file
15 * is executed per IDCT step, only during output pass setup.
17 * Note that the IDCT routines are responsible for performing coefficient
18 * dequantization as well as the IDCT proper. This module sets up the
19 * dequantization multiplier table needed by the IDCT routine.
22 #define JPEG_INTERNALS
23 #include "jinclude.h"
24 #include "jpeglib.h"
25 #include "jlossy.h" /* Private declarations for lossy subsystem */
26 #include "jdct.h" /* Private declarations for DCT subsystem */
30 * The decompressor input side (jdinput.c) saves away the appropriate
31 * quantization table for each component at the start of the first scan
32 * involving that component. (This is necessary in order to correctly
33 * decode files that reuse Q-table slots.)
34 * When we are ready to make an output pass, the saved Q-table is converted
35 * to a multiplier table that will actually be used by the IDCT routine.
36 * The multiplier table contents are IDCT-method-dependent. To support
37 * application changes in IDCT method between scans, we can remake the
38 * multiplier tables if necessary.
39 * In buffered-image mode, the first output pass may occur before any data
40 * has been seen for some components, and thus before their Q-tables have
41 * been saved away. To handle this case, multiplier tables are preset
42 * to zeroes; the result of the IDCT will be a neutral gray level.
46 /* Private subobject for this module */
48 typedef struct {
49 /* This array contains the IDCT method code that each multiplier table
50 * is currently set up for, or -1 if it's not yet set up.
51 * The actual multiplier tables are pointed to by dct_table in the
52 * per-component comp_info structures.
54 int cur_method[MAX_COMPONENTS];
55 } idct_controller;
57 typedef idct_controller * idct_ptr;
60 /* Allocated multiplier tables: big enough for any supported variant */
62 typedef union {
63 ISLOW_MULT_TYPE islow_array[DCTSIZE2];
64 #ifdef DCT_IFAST_SUPPORTED
65 IFAST_MULT_TYPE ifast_array[DCTSIZE2];
66 #endif
67 #ifdef DCT_FLOAT_SUPPORTED
68 FLOAT_MULT_TYPE float_array[DCTSIZE2];
69 #endif
70 } multiplier_table;
73 /* The current scaled-IDCT routines require ISLOW-style multiplier tables,
74 * so be sure to compile that code if either ISLOW or SCALING is requested.
76 #ifdef DCT_ISLOW_SUPPORTED
77 #define PROVIDE_ISLOW_TABLES
78 #else
79 #ifdef IDCT_SCALING_SUPPORTED
80 #define PROVIDE_ISLOW_TABLES
81 #endif
82 #endif
86 * Prepare for an output pass.
87 * Here we select the proper IDCT routine for each component and build
88 * a matching multiplier table.
91 METHODDEF(void)
92 start_pass (j_decompress_ptr cinfo)
94 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
95 idct_ptr idct = (idct_ptr) lossyd->idct_private;
96 int ci, i;
97 jpeg_component_info *compptr;
98 int method = 0;
99 inverse_DCT_method_ptr method_ptr = NULL;
100 JQUANT_TBL * qtbl;
102 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
103 ci++, compptr++) {
104 /* Select the proper IDCT routine for this component's scaling */
105 switch (compptr->codec_data_unit) {
106 #ifdef IDCT_SCALING_SUPPORTED
107 case 1:
108 method_ptr = jpeg_idct_1x1;
109 method = JDCT_ISLOW; /* jidctred uses islow-style table */
110 break;
111 case 2:
112 method_ptr = jpeg_idct_2x2;
113 method = JDCT_ISLOW; /* jidctred uses islow-style table */
114 break;
115 case 4:
116 method_ptr = jpeg_idct_4x4;
117 method = JDCT_ISLOW; /* jidctred uses islow-style table */
118 break;
119 #endif
120 case DCTSIZE:
121 switch (cinfo->dct_method) {
122 #ifdef DCT_ISLOW_SUPPORTED
123 case JDCT_ISLOW:
124 method_ptr = jpeg_idct_islow;
125 method = JDCT_ISLOW;
126 break;
127 #endif
128 #ifdef DCT_IFAST_SUPPORTED
129 case JDCT_IFAST:
130 method_ptr = jpeg_idct_ifast;
131 method = JDCT_IFAST;
132 break;
133 #endif
134 #ifdef DCT_FLOAT_SUPPORTED
135 case JDCT_FLOAT:
136 method_ptr = jpeg_idct_float;
137 method = JDCT_FLOAT;
138 break;
139 #endif
140 default:
141 ERREXIT(cinfo, JERR_NOT_COMPILED);
142 break;
144 break;
145 default:
146 ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->codec_data_unit);
147 break;
149 lossyd->inverse_DCT[ci] = method_ptr;
150 /* Create multiplier table from quant table.
151 * However, we can skip this if the component is uninteresting
152 * or if we already built the table. Also, if no quant table
153 * has yet been saved for the component, we leave the
154 * multiplier table all-zero; we'll be reading zeroes from the
155 * coefficient controller's buffer anyway.
157 if (! compptr->component_needed || idct->cur_method[ci] == method)
158 continue;
159 qtbl = compptr->quant_table;
160 if (qtbl == NULL) /* happens if no data yet for component */
161 continue;
162 idct->cur_method[ci] = method;
163 switch (method) {
164 #ifdef PROVIDE_ISLOW_TABLES
165 case JDCT_ISLOW:
167 /* For LL&M IDCT method, multipliers are equal to raw quantization
168 * coefficients, but are stored as ints to ensure access efficiency.
170 ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
171 for (i = 0; i < DCTSIZE2; i++) {
172 ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
175 break;
176 #endif
177 #ifdef DCT_IFAST_SUPPORTED
178 case JDCT_IFAST:
180 /* For AA&N IDCT method, multipliers are equal to quantization
181 * coefficients scaled by scalefactor[row]*scalefactor[col], where
182 * scalefactor[0] = 1
183 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
184 * For integer operation, the multiplier table is to be scaled by
185 * IFAST_SCALE_BITS.
187 IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
188 #define CONST_BITS 14
189 static const INT16 aanscales[DCTSIZE2] = {
190 /* precomputed values scaled up by 14 bits */
191 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
192 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
193 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
194 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
195 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
196 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
197 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
198 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
200 SHIFT_TEMPS
202 for (i = 0; i < DCTSIZE2; i++) {
203 ifmtbl[i] = (IFAST_MULT_TYPE)
204 DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
205 (INT32) aanscales[i]),
206 CONST_BITS-IFAST_SCALE_BITS);
209 break;
210 #endif
211 #ifdef DCT_FLOAT_SUPPORTED
212 case JDCT_FLOAT:
214 /* For float AA&N IDCT method, multipliers are equal to quantization
215 * coefficients scaled by scalefactor[row]*scalefactor[col], where
216 * scalefactor[0] = 1
217 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
219 FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
220 int row, col;
221 static const double aanscalefactor[DCTSIZE] = {
222 1.0, 1.387039845, 1.306562965, 1.175875602,
223 1.0, 0.785694958, 0.541196100, 0.275899379
226 i = 0;
227 for (row = 0; row < DCTSIZE; row++) {
228 for (col = 0; col < DCTSIZE; col++) {
229 fmtbl[i] = (FLOAT_MULT_TYPE)
230 ((double) qtbl->quantval[i] *
231 aanscalefactor[row] * aanscalefactor[col]);
232 i++;
236 break;
237 #endif
238 default:
239 ERREXIT(cinfo, JERR_NOT_COMPILED);
240 break;
247 * Initialize IDCT manager.
250 JGLOBAL(void)
251 jinit_inverse_dct (j_decompress_ptr cinfo)
253 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
254 idct_ptr idct;
255 int ci;
256 jpeg_component_info *compptr;
258 idct = (idct_ptr)
259 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
260 SIZEOF(idct_controller));
261 lossyd->idct_private = (void *) idct;
262 lossyd->idct_start_pass = start_pass;
264 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
265 ci++, compptr++) {
266 /* Allocate and pre-zero a multiplier table for each component */
267 compptr->dct_table =
268 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
269 SIZEOF(multiplier_table));
270 MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
271 /* Mark multiplier table not yet set up for any method */
272 idct->cur_method[ci] = -1;