1 | /* $Id: dequant.c,v 1.2 2001-09-05 14:30:44 bird Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | ** THIS SOFTWARE IS SUBJECT TO COPYRIGHT PROTECTION AND IS OFFERED ONLY
|
---|
5 | ** PURSUANT TO THE 3DFX GLIDE GENERAL PUBLIC LICENSE. THERE IS NO RIGHT
|
---|
6 | ** TO USE THE GLIDE TRADEMARK WITHOUT PRIOR WRITTEN PERMISSION OF 3DFX
|
---|
7 | ** INTERACTIVE, INC. A COPY OF THIS LICENSE MAY BE OBTAINED FROM THE
|
---|
8 | ** DISTRIBUTOR OR BY CONTACTING 3DFX INTERACTIVE INC(info@3dfx.com).
|
---|
9 | ** THIS PROGRAM IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
|
---|
10 | ** EXPRESSED OR IMPLIED. SEE THE 3DFX GLIDE GENERAL PUBLIC LICENSE FOR A
|
---|
11 | ** FULL TEXT OF THE NON-WARRANTY PROVISIONS.
|
---|
12 | **
|
---|
13 | ** USE, DUPLICATION OR DISCLOSURE BY THE GOVERNMENT IS SUBJECT TO
|
---|
14 | ** RESTRICTIONS AS SET FORTH IN SUBDIVISION (C)(1)(II) OF THE RIGHTS IN
|
---|
15 | ** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 252.227-7013,
|
---|
16 | ** AND/OR IN SIMILAR OR SUCCESSOR CLAUSES IN THE FAR, DOD OR NASA FAR
|
---|
17 | ** SUPPLEMENT. UNPUBLISHED RIGHTS RESERVED UNDER THE COPYRIGHT LAWS OF
|
---|
18 | ** THE UNITED STATES.
|
---|
19 | **
|
---|
20 | ** COPYRIGHT 3DFX INTERACTIVE, INC. 1999, ALL RIGHTS RESERVED
|
---|
21 | **
|
---|
22 | ** $Revision: 1.2 $
|
---|
23 | ** $Date: 2001-09-05 14:30:44 $
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include <stdio.h>
|
---|
27 | #include <stdlib.h>
|
---|
28 | #include <string.h>
|
---|
29 | #include <math.h>
|
---|
30 |
|
---|
31 | #include "texusint.h"
|
---|
32 |
|
---|
33 | /*
|
---|
34 | * Pn_8 = convert n bits (n <= 6) to 8 bits by replicating the msb's of input
|
---|
35 | * into the lsb's of the output.
|
---|
36 | */
|
---|
37 | static FxU8 P1_8[] = {0x00,0xff};
|
---|
38 | static FxU8 P2_8[] = {0x00,0x55,0xaa,0xff};
|
---|
39 | static FxU8 P3_8[] = {0x00,0x24,0x49,0x6d,0x92,0xb6,0xdb,0xff};
|
---|
40 | static FxU8 P4_8[] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,
|
---|
41 | 0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff};
|
---|
42 | static FxU8 P5_8[] = {0x00,0x08,0x10,0x18,0x21,0x29,0x31,0x39,
|
---|
43 | 0x42,0x4a,0x52,0x5a,0x63,0x6b,0x73,0x7b,
|
---|
44 | 0x84,0x8c,0x94,0x9c,0xa5,0xad,0xb5,0xbd,
|
---|
45 | 0xc6,0xce,0xd6,0xde,0xe7,0xef,0xf7,0xff};
|
---|
46 | static FxU8 P6_8[] = {0x00,0x04,0x08,0x0c,0x10,0x14,0x18,0x1c,
|
---|
47 | 0x20,0x24,0x28,0x2c,0x30,0x34,0x38,0x3c,
|
---|
48 | 0x41,0x45,0x49,0x4d,0x51,0x55,0x59,0x5d,
|
---|
49 | 0x61,0x65,0x69,0x6d,0x71,0x75,0x79,0x7d,
|
---|
50 | 0x82,0x86,0x8a,0x8e,0x92,0x96,0x9a,0x9e,
|
---|
51 | 0xa2,0xa6,0xaa,0xae,0xb2,0xb6,0xba,0xbe,
|
---|
52 | 0xc3,0xc7,0xcb,0xcf,0xd3,0xd7,0xdb,0xdf,
|
---|
53 | 0xe3,0xe7,0xeb,0xef,0xf3,0xf7,0xfb,0xff};
|
---|
54 | static FxU32
|
---|
55 | _txPixRgb332to8888 (FxU8 c332)
|
---|
56 | {
|
---|
57 | FxU32 a, r, g, b;
|
---|
58 | a = 0xff;
|
---|
59 | r = P3_8[(c332>>5)&7];
|
---|
60 | g = P3_8[(c332>>2)&7];
|
---|
61 | b = P2_8[(c332 )&3];
|
---|
62 | return (a << 24) | (r << 16) | (g << 8) | b;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /* YIQ treated at the image level */
|
---|
66 |
|
---|
67 | static FxU32
|
---|
68 | _txPixA8to8888 (FxU8 a8)
|
---|
69 | {
|
---|
70 | FxU32 p = a8;
|
---|
71 |
|
---|
72 | return (p << 24) | (0xffffff);
|
---|
73 | }
|
---|
74 |
|
---|
75 | static FxU32
|
---|
76 | _txPixI8to8888 (FxU8 i8)
|
---|
77 | {
|
---|
78 | FxU32 p = i8;
|
---|
79 |
|
---|
80 | return (0xff << 24) | (p << 16) | (p << 8) | p;
|
---|
81 | }
|
---|
82 |
|
---|
83 | static FxU32
|
---|
84 | _txPixAi44to8888 (FxU8 c44)
|
---|
85 | {
|
---|
86 | FxU32 a, i;
|
---|
87 |
|
---|
88 | a = P4_8[(c44 & 0xF0) >> 4];
|
---|
89 | i = P4_8[(c44 & 0x0F) ];
|
---|
90 |
|
---|
91 | return (a << 24) | (i << 16) | (i << 8) | i;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /* P8 treated at the image level */
|
---|
95 |
|
---|
96 | /* 16 bit pixels */
|
---|
97 | static FxU32
|
---|
98 | _txPixArgb8332to8888(FxU16 c8332)
|
---|
99 | {
|
---|
100 | FxU32 a, r, g, b;
|
---|
101 | a = (c8332 >> 8);
|
---|
102 | r = P3_8[(c8332 >> 5) & 0x7];
|
---|
103 | g = P3_8[(c8332 >> 2) & 0x7];
|
---|
104 | b = P2_8[(c8332 ) & 0x3];
|
---|
105 | return (a << 24) | (r << 16) | (g << 8) | b;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /* AYIQ8422 treated at image level */
|
---|
109 |
|
---|
110 | static FxU32
|
---|
111 | _txPixRgb565to8888 (FxU16 c565)
|
---|
112 | {
|
---|
113 | FxU32 a, r, g, b;
|
---|
114 | a = 0xFF;
|
---|
115 | r = P5_8[(c565 >> 11) ];
|
---|
116 | g = P6_8[(c565 >> 5) & 0x3f];
|
---|
117 | b = P5_8[(c565 ) & 0x1f];
|
---|
118 | return (a << 24) | (r << 16) | (g << 8) | b;
|
---|
119 | }
|
---|
120 |
|
---|
121 | static FxU32
|
---|
122 | _txPixArgb1555to8888 (FxU16 c1555)
|
---|
123 | {
|
---|
124 | FxU32 a, r, g, b;
|
---|
125 | a = P1_8[(c1555 >> 15) ];
|
---|
126 | r = P5_8[(c1555 >> 10) & 0x1f];
|
---|
127 | g = P5_8[(c1555 >> 5) & 0x1f];
|
---|
128 | b = P5_8[(c1555 ) & 0x1f];
|
---|
129 | return (a << 24) | (r << 16) | (g << 8) | b;
|
---|
130 | }
|
---|
131 |
|
---|
132 | static FxU32
|
---|
133 | _txPixRgba4444to8888 (FxU16 c4444)
|
---|
134 | {
|
---|
135 | FxU32 a, r, g, b;
|
---|
136 | a = P4_8[(c4444 >> 12) & 0xf];
|
---|
137 | r = P4_8[(c4444 >> 8) & 0xf];
|
---|
138 | g = P4_8[(c4444 >> 4) & 0xf];
|
---|
139 | b = P4_8[(c4444 ) & 0xf];
|
---|
140 | return (a << 24) | (r << 16) | (g << 8) | b;
|
---|
141 | }
|
---|
142 |
|
---|
143 | static FxU32
|
---|
144 | _txPixAi88to8888(FxU16 c88)
|
---|
145 | {
|
---|
146 | FxU32 a, r, g, b;
|
---|
147 | a = c88 >> 8;
|
---|
148 | r = g = b = (c88 & 0xff);
|
---|
149 | return (a << 24) | (r << 16) | (g << 8) | b;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /******************************************************************************/
|
---|
153 | static void
|
---|
154 | _txImgDequantizeRGB332(FxU32 *out, FxU8 *in, int w, int h)
|
---|
155 | {
|
---|
156 | int n = w * h;
|
---|
157 |
|
---|
158 | out += n;
|
---|
159 | in += n;
|
---|
160 | while (n--) *--out = _txPixRgb332to8888(*--in);
|
---|
161 | }
|
---|
162 |
|
---|
163 | static void
|
---|
164 | _txImgDequantizeYIQ422(FxU32 *out, FxU8 *in, int w, int h, const long *yabTable)
|
---|
165 | {
|
---|
166 | int n = w * h;
|
---|
167 | FxU32 pal[256];
|
---|
168 |
|
---|
169 | txYABtoPal256((long *)pal, (long *)yabTable);
|
---|
170 | out += n;
|
---|
171 | in += n;
|
---|
172 | while (n--) *--out = pal[*--in] | 0xff000000;
|
---|
173 | }
|
---|
174 |
|
---|
175 | static void
|
---|
176 | _txImgDequantizeA8(FxU32 *out, FxU8 *in, int w, int h)
|
---|
177 | {
|
---|
178 | int n = w * h;
|
---|
179 |
|
---|
180 | out += n;
|
---|
181 | in += n;
|
---|
182 | while (n--) *--out = _txPixA8to8888(*--in);
|
---|
183 | }
|
---|
184 |
|
---|
185 | static void
|
---|
186 | _txImgDequantizeI8(FxU32 *out, FxU8 *in, int w, int h)
|
---|
187 | {
|
---|
188 | int n = w * h;
|
---|
189 |
|
---|
190 | out += n;
|
---|
191 | in += n;
|
---|
192 | while (n--) *--out = _txPixI8to8888(*--in);
|
---|
193 | }
|
---|
194 |
|
---|
195 | static void
|
---|
196 | _txImgDequantizeAI44(FxU32 *out, FxU8 *in, int w, int h)
|
---|
197 | {
|
---|
198 | int n = w * h;
|
---|
199 |
|
---|
200 | out += n;
|
---|
201 | in += n;
|
---|
202 | while (n--) *--out = _txPixAi44to8888(*--in);
|
---|
203 | }
|
---|
204 |
|
---|
205 | static void
|
---|
206 | _txImgDequantizeP8(FxU32 *out, FxU8 *in, int w, int h, const FxU32 *pal)
|
---|
207 | {
|
---|
208 | int n = w * h;
|
---|
209 |
|
---|
210 | out += n;
|
---|
211 | in += n;
|
---|
212 | while (n--) *--out = pal[*--in] | 0xff000000;
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | static void
|
---|
217 | _txImgDequantizeARGB8332(FxU32 *out, FxU16 *in, int w, int h)
|
---|
218 | {
|
---|
219 | int n = w * h;
|
---|
220 |
|
---|
221 | out += n;
|
---|
222 | in += n;
|
---|
223 | while (n--) *--out = _txPixArgb8332to8888 (*--in);
|
---|
224 | }
|
---|
225 |
|
---|
226 | static void
|
---|
227 | _txImgDequantizeAYIQ8422(FxU32 *out, FxU16 *in, int w, int h, const long *yab)
|
---|
228 | {
|
---|
229 | int n = w * h;
|
---|
230 | long pal[256];
|
---|
231 |
|
---|
232 | txYABtoPal256(pal, yab);
|
---|
233 | out += n;
|
---|
234 | in += n;
|
---|
235 | while (n--) {
|
---|
236 | in--;
|
---|
237 | *--out = (pal[(*in) & 0xff] & 0x00ffffff) | ((*in & 0xFF00) << 16);
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | static void
|
---|
242 | _txImgDequantizeRGB565(FxU32 *out, FxU16 *in, int w, int h)
|
---|
243 | {
|
---|
244 | int n = w * h;
|
---|
245 |
|
---|
246 | out += n;
|
---|
247 | in += n;
|
---|
248 | while (n--) *--out = _txPixRgb565to8888(*--in);
|
---|
249 | }
|
---|
250 |
|
---|
251 | static void
|
---|
252 | _txImgDequantizeARGB1555(FxU32 *out, FxU16 *in, int w, int h)
|
---|
253 | {
|
---|
254 | int n = w * h;
|
---|
255 |
|
---|
256 | out += n;
|
---|
257 | in += n;
|
---|
258 | while (n--) *--out = _txPixArgb1555to8888 (*--in);
|
---|
259 | }
|
---|
260 |
|
---|
261 | static void
|
---|
262 | _txImgDequantizeARGB4444(FxU32 *out, FxU16 *in, int w, int h)
|
---|
263 | {
|
---|
264 | int n = w * h;
|
---|
265 |
|
---|
266 | out += n;
|
---|
267 | in += n;
|
---|
268 | while (n--) *--out = _txPixRgba4444to8888 (*--in);
|
---|
269 | }
|
---|
270 |
|
---|
271 | static void
|
---|
272 | _txImgDequantizeAI88(FxU32 *out, FxU16 *in, int w, int h)
|
---|
273 | {
|
---|
274 | int n = w * h;
|
---|
275 |
|
---|
276 | out += n;
|
---|
277 | in += n;
|
---|
278 | while (n--) *--out = _txPixAi88to8888(*--in);
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 | static void
|
---|
283 | _txImgDequantizeAP88(FxU32 *out, FxU16 *in, int w, int h, const FxU32 *pal)
|
---|
284 | {
|
---|
285 | int n = w * h;
|
---|
286 |
|
---|
287 | out += n;
|
---|
288 | in += n;
|
---|
289 |
|
---|
290 | while (n--) {
|
---|
291 | in--;
|
---|
292 | *--out = (pal[(*in) & 0xff] & 0x00ffffff) | ((*in & 0xFF00) << 16);
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 |
|
---|
297 | static void
|
---|
298 | _txImgDequantizeARGB8888(FxU32 *out, FxU32 *in, int w, int h)
|
---|
299 | {
|
---|
300 | int n = w * h;
|
---|
301 |
|
---|
302 | out += n;
|
---|
303 | in += n;
|
---|
304 |
|
---|
305 | while (n--) *--out = *--in;
|
---|
306 | }
|
---|
307 |
|
---|
308 | static void
|
---|
309 | _txImgDequantizeRGB888(FxU32 *out, FxU32 *in_, int w, int h)
|
---|
310 | {
|
---|
311 | int n = w * h;
|
---|
312 | int i;
|
---|
313 | FxU8 *in = ( FxU8 * )in_;
|
---|
314 |
|
---|
315 | for( i = 0; i < n; i++ )
|
---|
316 | {
|
---|
317 | out[i] =
|
---|
318 | ( ( ( FxU32 )0xff ) << 24 ) |
|
---|
319 | ( ( ( FxU32 )in[i*3] )<< 16 ) |
|
---|
320 | ( ( ( FxU32 )in[i*3+1] )<< 8 ) |
|
---|
321 | ( ( ( FxU32 )in[i*3+2] ) );
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 | void
|
---|
326 | txMipDequantize(TxMip *txMip, TxMip *pxMip)
|
---|
327 | {
|
---|
328 | /* Walk through all mipmap levels, and convert to ARGB8888 format */
|
---|
329 | int i, w, h;
|
---|
330 |
|
---|
331 | if( txVerbose )
|
---|
332 | {
|
---|
333 | printf("Dequant: (from %s) ..", Format_Name[pxMip->format]);
|
---|
334 | }
|
---|
335 | w = pxMip->width;
|
---|
336 | h = pxMip->height;
|
---|
337 | for (i=0; i< txMip->depth; i++) {
|
---|
338 | void *src, *dst;
|
---|
339 |
|
---|
340 | src = pxMip->data[i];
|
---|
341 | dst = txMip->data[i];
|
---|
342 |
|
---|
343 | if( txVerbose )
|
---|
344 | {
|
---|
345 | printf(" %dx%d", w, h); fflush(stdout);
|
---|
346 | }
|
---|
347 |
|
---|
348 | switch(pxMip->format) {
|
---|
349 | case GR_TEXFMT_RGB_332: _txImgDequantizeRGB332(dst, src, w, h);
|
---|
350 | break;
|
---|
351 | case GR_TEXFMT_YIQ_422: _txImgDequantizeYIQ422(dst, src, w, h,
|
---|
352 | (long *)pxMip->pal); break;
|
---|
353 | case GR_TEXFMT_A_8: _txImgDequantizeA8(dst, src, w, h);
|
---|
354 | break;
|
---|
355 | case GR_TEXFMT_I_8: _txImgDequantizeI8(dst, src, w, h);
|
---|
356 | break;
|
---|
357 | case GR_TEXFMT_AI_44: _txImgDequantizeAI44(dst, src, w, h);
|
---|
358 | break;
|
---|
359 | case GR_TEXFMT_P_8: _txImgDequantizeP8(dst, src, w, h,
|
---|
360 | pxMip->pal); break;
|
---|
361 |
|
---|
362 | case GR_TEXFMT_ARGB_8332: _txImgDequantizeARGB8332(dst, src, w, h);
|
---|
363 | break;
|
---|
364 | case GR_TEXFMT_AYIQ_8422: _txImgDequantizeAYIQ8422(dst, src, w, h,
|
---|
365 | (long *)pxMip->pal); break;
|
---|
366 | case GR_TEXFMT_RGB_565: _txImgDequantizeRGB565(dst, src, w, h);
|
---|
367 | break;
|
---|
368 | case GR_TEXFMT_ARGB_1555: _txImgDequantizeARGB1555(dst, src, w, h);
|
---|
369 | break;
|
---|
370 | case GR_TEXFMT_ARGB_4444: _txImgDequantizeARGB4444(dst, src, w, h);
|
---|
371 | break;
|
---|
372 | case GR_TEXFMT_AI_88: _txImgDequantizeAI88(dst, src, w, h);
|
---|
373 | break;
|
---|
374 | case GR_TEXFMT_AP_88: _txImgDequantizeAP88(dst, src, w, h,
|
---|
375 | pxMip->pal); break;
|
---|
376 | case GR_TEXFMT_ARGB_8888: _txImgDequantizeARGB8888(dst, src, w, h);
|
---|
377 | break;
|
---|
378 | case GR_TEXFMT_RGB_888: _txImgDequantizeRGB888(dst, src, w, h);
|
---|
379 | break;
|
---|
380 |
|
---|
381 | default:
|
---|
382 | break;
|
---|
383 | }
|
---|
384 |
|
---|
385 | if (w > 1) w >>= 1;
|
---|
386 | if (h > 1) h >>= 1;
|
---|
387 | }
|
---|
388 | if( txVerbose )
|
---|
389 | {
|
---|
390 | printf(".\n"); fflush(stdout);
|
---|
391 | }
|
---|
392 | }
|
---|