7 * Copyright (C) 1994-1995, Thomas G. Lane.
\r
9 * This file is part of the Independent JPEG Group's software.
\r
11 * For conditions of distribution and use, see the accompanying README file.
\r
15 * This file contains the inverse-DCT management logic.
\r
17 * This code selects a particular IDCT implementation to be used,
\r
19 * and it performs related housekeeping chores. No code in this file
\r
21 * is executed per IDCT step, only during output pass setup.
\r
25 * Note that the IDCT routines are responsible for performing coefficient
\r
27 * dequantization as well as the IDCT proper. This module sets up the
\r
29 * dequantization multiplier table needed by the IDCT routine.
\r
35 #define JPEG_INTERNALS
\r
37 #include "jinclude.h"
\r
39 #include "radiant_jpeglib.h"
\r
41 #include "jdct.h" /* Private declarations for DCT subsystem */
\r
49 * The decompressor input side (jdinput.c) saves away the appropriate
\r
51 * quantization table for each component at the start of the first scan
\r
53 * involving that component. (This is necessary in order to correctly
\r
55 * decode files that reuse Q-table slots.)
\r
57 * When we are ready to make an output pass, the saved Q-table is converted
\r
59 * to a multiplier table that will actually be used by the IDCT routine.
\r
61 * The multiplier table contents are IDCT-method-dependent. To support
\r
63 * application changes in IDCT method between scans, we can remake the
\r
65 * multiplier tables if necessary.
\r
67 * In buffered-image mode, the first output pass may occur before any data
\r
69 * has been seen for some components, and thus before their Q-tables have
\r
71 * been saved away. To handle this case, multiplier tables are preset
\r
73 * to zeroes; the result of the IDCT will be a neutral gray level.
\r
81 /* Private subobject for this module */
\r
87 struct jpeg_inverse_dct pub; /* public fields */
\r
91 /* This array contains the IDCT method code that each multiplier table
\r
93 * is currently set up for, or -1 if it's not yet set up.
\r
95 * The actual multiplier tables are pointed to by dct_table in the
\r
97 * per-component comp_info structures.
\r
101 int cur_method[MAX_COMPONENTS];
\r
103 } my_idct_controller;
\r
107 typedef my_idct_controller * my_idct_ptr;
\r
113 /* Allocated multiplier tables: big enough for any supported variant */
\r
119 ISLOW_MULT_TYPE islow_array[DCTSIZE2];
\r
121 #ifdef DCT_IFAST_SUPPORTED
\r
123 IFAST_MULT_TYPE ifast_array[DCTSIZE2];
\r
127 #ifdef DCT_FLOAT_SUPPORTED
\r
129 FLOAT_MULT_TYPE float_array[DCTSIZE2];
\r
133 } multiplier_table;
\r
139 /* The current scaled-IDCT routines require ISLOW-style multiplier tables,
\r
141 * so be sure to compile that code if either ISLOW or SCALING is requested.
\r
145 #ifdef DCT_ISLOW_SUPPORTED
\r
147 #define PROVIDE_ISLOW_TABLES
\r
151 #ifdef IDCT_SCALING_SUPPORTED
\r
153 #define PROVIDE_ISLOW_TABLES
\r
165 * Prepare for an output pass.
\r
167 * Here we select the proper IDCT routine for each component and build
\r
169 * a matching multiplier table.
\r
177 start_pass (j_decompress_ptr cinfo)
\r
181 my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
\r
185 jpeg_component_info *compptr;
\r
189 inverse_DCT_method_ptr method_ptr = NULL;
\r
195 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
\r
199 /* Select the proper IDCT routine for this component's scaling */
\r
201 switch (compptr->DCT_scaled_size) {
\r
203 #ifdef IDCT_SCALING_SUPPORTED
\r
207 method_ptr = jpeg_idct_1x1;
\r
209 method = JDCT_ISLOW; /* jidctred uses islow-style table */
\r
215 method_ptr = jpeg_idct_2x2;
\r
217 method = JDCT_ISLOW; /* jidctred uses islow-style table */
\r
223 method_ptr = jpeg_idct_4x4;
\r
225 method = JDCT_ISLOW; /* jidctred uses islow-style table */
\r
233 switch (cinfo->dct_method) {
\r
235 #ifdef DCT_ISLOW_SUPPORTED
\r
239 method_ptr = jpeg_idct_islow;
\r
241 method = JDCT_ISLOW;
\r
247 #ifdef DCT_IFAST_SUPPORTED
\r
251 method_ptr = jpeg_idct_ifast;
\r
253 method = JDCT_IFAST;
\r
259 #ifdef DCT_FLOAT_SUPPORTED
\r
263 method_ptr = jpeg_idct_float;
\r
265 method = JDCT_FLOAT;
\r
273 ERREXIT(cinfo, JERR_NOT_COMPILED);
\r
283 ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
\r
289 idct->pub.inverse_DCT[ci] = method_ptr;
\r
291 /* Create multiplier table from quant table.
\r
293 * However, we can skip this if the component is uninteresting
\r
295 * or if we already built the table. Also, if no quant table
\r
297 * has yet been saved for the component, we leave the
\r
299 * multiplier table all-zero; we'll be reading zeroes from the
\r
301 * coefficient controller's buffer anyway.
\r
305 if (! compptr->component_needed || idct->cur_method[ci] == method)
\r
309 qtbl = compptr->quant_table;
\r
311 if (qtbl == NULL) /* happens if no data yet for component */
\r
315 idct->cur_method[ci] = method;
\r
319 #ifdef PROVIDE_ISLOW_TABLES
\r
325 /* For LL&M IDCT method, multipliers are equal to raw quantization
\r
327 * coefficients, but are stored in natural order as ints.
\r
331 ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
\r
333 for (i = 0; i < DCTSIZE2; i++) {
\r
335 ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[jpeg_zigzag_order[i]];
\r
345 #ifdef DCT_IFAST_SUPPORTED
\r
351 /* For AA&N IDCT method, multipliers are equal to quantization
\r
353 * coefficients scaled by scalefactor[row]*scalefactor[col], where
\r
355 * scalefactor[0] = 1
\r
357 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
\r
359 * For integer operation, the multiplier table is to be scaled by
\r
361 * IFAST_SCALE_BITS. The multipliers are stored in natural order.
\r
365 IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
\r
367 #define CONST_BITS 14
\r
369 static const INT16 aanscales[DCTSIZE2] = {
\r
371 /* precomputed values scaled up by 14 bits */
\r
373 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
\r
375 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
\r
377 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
\r
379 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
\r
381 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
\r
383 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
\r
385 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
\r
387 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
\r
395 for (i = 0; i < DCTSIZE2; i++) {
\r
397 ifmtbl[i] = (IFAST_MULT_TYPE)
\r
399 DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[jpeg_zigzag_order[i]],
\r
401 (INT32) aanscales[i]),
\r
403 CONST_BITS-IFAST_SCALE_BITS);
\r
413 #ifdef DCT_FLOAT_SUPPORTED
\r
419 /* For float AA&N IDCT method, multipliers are equal to quantization
\r
421 * coefficients scaled by scalefactor[row]*scalefactor[col], where
\r
423 * scalefactor[0] = 1
\r
425 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
\r
427 * The multipliers are stored in natural order.
\r
431 FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
\r
435 static const double aanscalefactor[DCTSIZE] = {
\r
437 1.0, 1.387039845, 1.306562965, 1.175875602,
\r
439 1.0, 0.785694958, 0.541196100, 0.275899379
\r
447 for (row = 0; row < DCTSIZE; row++) {
\r
449 for (col = 0; col < DCTSIZE; col++) {
\r
451 fmtbl[i] = (FLOAT_MULT_TYPE)
\r
453 ((double) qtbl->quantval[jpeg_zigzag_order[i]] *
\r
455 aanscalefactor[row] * aanscalefactor[col]);
\r
471 ERREXIT(cinfo, JERR_NOT_COMPILED);
\r
487 * Initialize IDCT manager.
\r
495 jinit_inverse_dct (j_decompress_ptr cinfo)
\r
503 jpeg_component_info *compptr;
\r
507 idct = (my_idct_ptr)
\r
509 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
\r
511 SIZEOF(my_idct_controller));
\r
513 cinfo->idct = (struct jpeg_inverse_dct *) idct;
\r
515 idct->pub.start_pass = start_pass;
\r
519 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
\r
523 /* Allocate and pre-zero a multiplier table for each component */
\r
525 compptr->dct_table =
\r
527 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
\r
529 SIZEOF(multiplier_table));
\r
531 MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
\r
533 /* Mark multiplier table not yet set up for any method */
\r
535 idct->cur_method[ci] = -1;
\r