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