1 /*
2 * jddctmgr.c
3 *
4 * Copyright (C) 1994-1996, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
7 *
8 * This file contains the inverse-DCT management logic.
9 * This code selects a particular IDCT implementation to be used,
10 * and it performs related housekeeping chores.  No code in this file
11 * is executed per IDCT step, only during output pass setup.
12 *
13 * Note that the IDCT routines are responsible for performing coefficient
14 * dequantization as well as the IDCT proper.  This module sets up the
15 * dequantization multiplier table needed by the IDCT routine.
16 */
17 
18 #define JPEG_INTERNALS
19 #include "jinclude.h"
20 #include "jpeglib.h"
21 #include "jdct.h"		/* Private declarations for DCT subsystem */
22 
23 
24 /*
25 * The decompressor input side (jdinput.c) saves away the appropriate
26 * quantization table for each component at the start of the first scan
27 * involving that component.  (This is necessary in order to correctly
28 * decode files that reuse Q-table slots.)
29 * When we are ready to make an output pass, the saved Q-table is converted
30 * to a multiplier table that will actually be used by the IDCT routine.
31 * The multiplier table contents are IDCT-method-dependent.  To support
32 * application changes in IDCT method between scans, we can remake the
33 * multiplier tables if necessary.
34 * In buffered-image mode, the first output pass may occur before any data
35 * has been seen for some components, and thus before their Q-tables have
36 * been saved away.  To handle this case, multiplier tables are preset
37 * to zeroes; the result of the IDCT will be a neutral gray level.
38 */
39 
40 
41 /* Private subobject for this module */
42 
43 typedef struct {
44 	struct jpeg_inverse_dct pub;	/* public fields */
45 
46 	/* This array contains the IDCT method code that each multiplier table
47 	* is currently set up for, or -1 if it's not yet set up.
48 	* The actual multiplier tables are pointed to by dct_table in the
49 	* per-component comp_info structures.
50 	*/
51 	int cur_method[MAX_COMPONENTS];
52 } my_idct_controller;
53 
54 typedef my_idct_controller * my_idct_ptr;
55 
56 
57 /* Allocated multiplier tables: big enough for any supported variant */
58 
59 typedef union {
60 	ISLOW_MULT_TYPE islow_array[DCTSIZE2];
61 } multiplier_table;
62 
63 
64 /* The current scaled-IDCT routines require ISLOW-style multiplier tables,
65 * so be sure to compile that code if either ISLOW or SCALING is requested.
66 */
67 #ifdef DCT_ISLOW_SUPPORTED
68 #define PROVIDE_ISLOW_TABLES
69 #endif
70 
71 
72 /*
73 * Prepare for an output pass.
74 * Here we select the proper IDCT routine for each component and build
75 * a matching multiplier table.
76 */
77 
78 METHODDEF(void)
start_pass(j_decompress_ptr cinfo)79 start_pass (j_decompress_ptr cinfo)
80 {
81 	my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
82 	int ci, i;
83 	jpeg_component_info *compptr;
84 	int method = 0;
85 	inverse_DCT_method_ptr method_ptr = NULL;
86 	JQUANT_TBL * qtbl;
87 
88 	for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
89 		ci++, compptr++) {
90 			/* Select the proper IDCT routine for this component's scaling */
91 			switch (compptr->DCT_scaled_size) {
92 	case DCTSIZE:
93 		switch (cinfo->dct_method) {
94 #ifdef DCT_ISLOW_SUPPORTED
95 	case JDCT_ISLOW:
96 		method_ptr = jpeg_idct_islow;
97 		method = JDCT_ISLOW;
98 		break;
99 #endif
100 	default:
101 		ERREXIT(cinfo, JERR_NOT_COMPILED);
102 		break;
103 		}
104 		break;
105 	default:
106 		ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
107 		break;
108 			}
109 			idct->pub.inverse_DCT[ci] = method_ptr;
110 			/* Create multiplier table from quant table.
111 			* However, we can skip this if the component is uninteresting
112 			* or if we already built the table.  Also, if no quant table
113 			* has yet been saved for the component, we leave the
114 			* multiplier table all-zero; we'll be reading zeroes from the
115 			* coefficient controller's buffer anyway.
116 			*/
117 			if (! compptr->component_needed || idct->cur_method[ci] == method)
118 				continue;
119 			qtbl = compptr->quant_table;
120 			if (qtbl == NULL)		/* happens if no data yet for component */
121 				continue;
122 			idct->cur_method[ci] = method;
123 			switch (method) {
124 #ifdef PROVIDE_ISLOW_TABLES
125 	case JDCT_ISLOW:
126 		{
127 			/* For LL&M IDCT method, multipliers are equal to raw quantization
128 			* coefficients, but are stored as ints to ensure access efficiency.
129 			*/
130 			ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
131 			for (i = 0; i < DCTSIZE2; i++) {
132 				ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
133 			}
134 		}
135 		break;
136 #endif
137 	default:
138 		ERREXIT(cinfo, JERR_NOT_COMPILED);
139 		break;
140 			}
141 	}
142 }
143 
144 
145 /*
146 * Initialize IDCT manager.
147 */
148 
149 GLOBAL(void)
jinit_inverse_dct(j_decompress_ptr cinfo)150 jinit_inverse_dct (j_decompress_ptr cinfo)
151 {
152 	my_idct_ptr idct;
153 	int ci;
154 	jpeg_component_info *compptr;
155 
156 	idct = (my_idct_ptr)
157 		(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
158 		SIZEOF(my_idct_controller));
159 	cinfo->idct = (struct jpeg_inverse_dct *) idct;
160 	idct->pub.start_pass = start_pass;
161 
162 	for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
163 		ci++, compptr++) {
164 			/* Allocate and pre-zero a multiplier table for each component */
165 			compptr->dct_table =
166 				(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
167 				SIZEOF(multiplier_table));
168 			MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
169 			/* Mark multiplier table not yet set up for any method */
170 			idct->cur_method[ci] = -1;
171 	}
172 }
173