File: dequant.c

package info (click to toggle)
glide 2002.04.10ds1-16
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 59,540 kB
  • sloc: ansic: 290,125; asm: 23,305; sh: 8,089; pascal: 3,854; makefile: 1,276; perl: 73
file content (391 lines) | stat: -rw-r--r-- 11,277 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391

/*
** THIS SOFTWARE IS SUBJECT TO COPYRIGHT PROTECTION AND IS OFFERED ONLY
** PURSUANT TO THE 3DFX GLIDE GENERAL PUBLIC LICENSE. THERE IS NO RIGHT
** TO USE THE GLIDE TRADEMARK WITHOUT PRIOR WRITTEN PERMISSION OF 3DFX
** INTERACTIVE, INC. A COPY OF THIS LICENSE MAY BE OBTAINED FROM THE 
** DISTRIBUTOR OR BY CONTACTING 3DFX INTERACTIVE INC(info@3dfx.com). 
** THIS PROGRAM IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER 
** EXPRESSED OR IMPLIED. SEE THE 3DFX GLIDE GENERAL PUBLIC LICENSE FOR A
** FULL TEXT OF THE NON-WARRANTY PROVISIONS.  
** 
** USE, DUPLICATION OR DISCLOSURE BY THE GOVERNMENT IS SUBJECT TO
** RESTRICTIONS AS SET FORTH IN SUBDIVISION (C)(1)(II) OF THE RIGHTS IN
** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 252.227-7013,
** AND/OR IN SIMILAR OR SUCCESSOR CLAUSES IN THE FAR, DOD OR NASA FAR
** SUPPLEMENT. UNPUBLISHED RIGHTS RESERVED UNDER THE COPYRIGHT LAWS OF
** THE UNITED STATES.  
** 
** COPYRIGHT 3DFX INTERACTIVE, INC. 1999, ALL RIGHTS RESERVED
**
** $Revision: 1.2 $
** $Date: 2000/06/15 00:11:40 $
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "texusint.h"

/* 
 * Pn_8 = convert n bits (n <= 6) to 8 bits by replicating the msb's of input
 * into the lsb's of the output.
 */
static  FxU8    P1_8[] = {0x00,0xff};
static  FxU8    P2_8[] = {0x00,0x55,0xaa,0xff};
static  FxU8    P3_8[] = {0x00,0x24,0x49,0x6d,0x92,0xb6,0xdb,0xff};
static  FxU8    P4_8[] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,
                                          0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff};
static  FxU8    P5_8[] = {0x00,0x08,0x10,0x18,0x21,0x29,0x31,0x39,
                                          0x42,0x4a,0x52,0x5a,0x63,0x6b,0x73,0x7b,
                                          0x84,0x8c,0x94,0x9c,0xa5,0xad,0xb5,0xbd,
                                          0xc6,0xce,0xd6,0xde,0xe7,0xef,0xf7,0xff};
static  FxU8    P6_8[] = {0x00,0x04,0x08,0x0c,0x10,0x14,0x18,0x1c,
                                          0x20,0x24,0x28,0x2c,0x30,0x34,0x38,0x3c,
                                          0x41,0x45,0x49,0x4d,0x51,0x55,0x59,0x5d,
                                          0x61,0x65,0x69,0x6d,0x71,0x75,0x79,0x7d,
                                          0x82,0x86,0x8a,0x8e,0x92,0x96,0x9a,0x9e,
                                          0xa2,0xa6,0xaa,0xae,0xb2,0xb6,0xba,0xbe,
                                          0xc3,0xc7,0xcb,0xcf,0xd3,0xd7,0xdb,0xdf,
                                          0xe3,0xe7,0xeb,0xef,0xf3,0xf7,0xfb,0xff};
static FxU32
_txPixRgb332to8888 (FxU8 c332)
{
    FxU32       a, r, g, b;
    a = 0xff;
    r = P3_8[(c332>>5)&7];
    g = P3_8[(c332>>2)&7];
    b = P2_8[(c332   )&3];
    return (a << 24) | (r << 16) | (g << 8) | b;
}

/* YIQ treated at the image level */

static FxU32
_txPixA8to8888 (FxU8 a8)
{
    FxU32       p = a8;

    return (p << 24) | (0xffffff);
}

static FxU32
_txPixI8to8888 (FxU8 i8)
{
    FxU32       p = i8;

    return (0xff << 24) | (p << 16) | (p << 8) | p;
}

static FxU32
_txPixAi44to8888 (FxU8 c44)
{
    FxU32       a, i;

    a = P4_8[(c44 & 0xF0) >> 4];
    i = P4_8[(c44 & 0x0F)     ];

    return (a << 24) | (i << 16) | (i << 8) | i;
}

/* P8 treated at the image level */

/* 16 bit pixels */
static FxU32 
_txPixArgb8332to8888(FxU16 c8332)
{
    FxU32       a, r, g, b;
    a =      (c8332 >>  8);      
    r = P3_8[(c8332 >>  5) & 0x7];
    g = P3_8[(c8332 >>  2) & 0x7];
    b = P2_8[(c8332      ) & 0x3];
    return (a << 24) | (r << 16) | (g << 8) | b;
}

/* AYIQ8422 treated at image level */

static FxU32
_txPixRgb565to8888 (FxU16 c565)
{
    FxU32       a, r, g, b;
    a = 0xFF;
    r = P5_8[(c565 >> 11)       ];
    g = P6_8[(c565 >>  5) & 0x3f];
    b = P5_8[(c565      ) & 0x1f];
    return (a << 24) | (r << 16) | (g << 8) | b;
}

static FxU32
_txPixArgb1555to8888 (FxU16 c1555)
{
    FxU32       a, r, g, b;
    a = P1_8[(c1555 >> 15)       ];
    r = P5_8[(c1555 >> 10) & 0x1f];
    g = P5_8[(c1555 >>  5) & 0x1f];
    b = P5_8[(c1555      ) & 0x1f];
    return (a << 24) | (r << 16) | (g << 8) | b;
}

static FxU32
_txPixRgba4444to8888 (FxU16 c4444)
{
    FxU32       a, r, g, b;
    a = P4_8[(c4444 >> 12) & 0xf];
    r = P4_8[(c4444 >>  8) & 0xf];
    g = P4_8[(c4444 >>  4) & 0xf];
    b = P4_8[(c4444      ) & 0xf];
    return (a << 24) | (r << 16) | (g << 8) | b;
}

static FxU32
_txPixAi88to8888(FxU16 c88)
{
    FxU32       a, r, g, b;
    a = c88 >> 8;
    r = g = b = (c88 & 0xff);
    return (a << 24) | (r << 16) | (g << 8) | b;
}

/******************************************************************************/
static void
_txImgDequantizeRGB332(FxU32 *out, FxU8 *in, int w, int h)
{
    int         n = w * h;

    out += n;
    in  += n;
    while (n--) *--out = _txPixRgb332to8888(*--in);
}

static void
_txImgDequantizeYIQ422(FxU32 *out, FxU8 *in, int w, int h, const FxU32 *yabTable)
{
    int                         n = w * h;
    FxU32                       pal[256];

    txYABtoPal256(pal, yabTable);
    out += n;
    in  += n;
    while (n--) *--out = pal[*--in] | 0xff000000;
}

static void
_txImgDequantizeA8(FxU32 *out, FxU8 *in, int w, int h)
{
    int         n = w * h;

    out += n;
    in  += n;
    while (n--) *--out = _txPixA8to8888(*--in);
}

static void
_txImgDequantizeI8(FxU32 *out, FxU8 *in, int w, int h)
{
    int         n = w * h;

    out += n;
    in  += n;
    while (n--) *--out = _txPixI8to8888(*--in);
}

static void
_txImgDequantizeAI44(FxU32 *out, FxU8 *in, int w, int h)
{
    int         n = w * h;

    out += n;
    in  += n;
    while (n--) *--out = _txPixAi44to8888(*--in);
}

static void
_txImgDequantizeP8(FxU32 *out, FxU8 *in, int w, int h, const FxU32 *pal)
{
    int         n = w * h;

    out += n;
    in  += n;
    while (n--) *--out = pal[*--in] | 0xff000000;
}


static void
_txImgDequantizeARGB8332(FxU32 *out, FxU16 *in, int w, int h)
{
    int         n = w * h;

    out += n;
    in  += n;
    while (n--) *--out = _txPixArgb8332to8888 (*--in);
}

static void
_txImgDequantizeAYIQ8422(FxU32 *out, FxU16 *in, int w, int h, const FxU32 *yab)
{
    int         n = w * h;
    FxU32       pal[256];

    txYABtoPal256(pal, yab);
    out += n;
    in  += n;
    while (n--) {
        in--; 
        *--out = (pal[(*in) & 0xff] & 0x00ffffff) | ((*in & 0xFF00) << 16);
    }
}

static void
_txImgDequantizeRGB565(FxU32 *out, FxU16 *in, int w, int h)
{
    int         n = w * h;

    out += n;
    in  += n;
    while (n--) *--out = _txPixRgb565to8888(*--in);
}

static void
_txImgDequantizeARGB1555(FxU32 *out, FxU16 *in, int w, int h)
{
    int         n = w * h;

    out += n;
    in  += n;
    while (n--) *--out = _txPixArgb1555to8888 (*--in);
}

static void
_txImgDequantizeARGB4444(FxU32 *out, FxU16 *in, int w, int h)
{
    int         n = w * h;

    out += n;
    in  += n;
    while (n--) *--out = _txPixRgba4444to8888 (*--in);
}

static void
_txImgDequantizeAI88(FxU32 *out, FxU16 *in, int w, int h)
{
    int         n = w * h;

    out += n;
    in  += n;
    while (n--) *--out = _txPixAi88to8888(*--in);
}


static void
_txImgDequantizeAP88(FxU32 *out, FxU16 *in, int w, int h, const FxU32 *pal)
{
    int         n = w * h;

    out += n;
    in  += n;

    while (n--) {
        in--;
        *--out = (pal[(*in) & 0xff] & 0x00ffffff) | ((*in & 0xFF00) << 16);
    }
}


static void
_txImgDequantizeARGB8888(FxU32 *out, FxU32 *in, int w, int h)
{
    int         n = w * h;

    out += n;
    in  += n;

    while (n--) *--out = *--in; 
}

static void
_txImgDequantizeRGB888(FxU32 *out, FxU32 *in_, int w, int h)
{
    int         n = w * h;
    int i;
    FxU8 *in = ( FxU8 * )in_;

    for( i = 0; i < n; i++ )
      {
        out[i] = 
          ( ( ( FxU32 )0xff ) << 24 ) | 
          ( ( ( FxU32 )in[i*3] )<< 16 ) |
          ( ( ( FxU32 )in[i*3+1] )<< 8 ) |
          ( ( ( FxU32 )in[i*3+2] ) );
      }
}

void    
txMipDequantize(TxMip *txMip, TxMip *pxMip)
{
    /* Walk through all mipmap levels, and convert to ARGB8888 format */
    int         i, w, h;

    if( txVerbose )
      {
        printf("Dequant: (from %s) ..", Format_Name[pxMip->format]);
      }
    w = pxMip->width;
    h = pxMip->height;
    for (i=0; i< txMip->depth; i++) {
        void    *src, *dst;

        src = pxMip->data[i];
        dst = txMip->data[i];

        if( txVerbose )
          {
            printf(" %dx%d", w, h); fflush(stdout);
          }
            
        switch(pxMip->format) {
        case GR_TEXFMT_RGB_332:         _txImgDequantizeRGB332(dst, src, w, h);         
                                                                break;
        case GR_TEXFMT_YIQ_422:         _txImgDequantizeYIQ422(dst, src, w, h, 
                                                                        pxMip->pal); break;
        case GR_TEXFMT_A_8:                     _txImgDequantizeA8(dst, src, w, h);             
                                                                break;
        case GR_TEXFMT_I_8:                     _txImgDequantizeI8(dst, src, w, h);             
                                                                break;
        case GR_TEXFMT_AI_44:           _txImgDequantizeAI44(dst, src, w, h);           
                                                                break;
        case GR_TEXFMT_P_8:                     _txImgDequantizeP8(dst, src, w, h, 
                                                                        pxMip->pal); break;

        case GR_TEXFMT_ARGB_8332:       _txImgDequantizeARGB8332(dst, src, w, h);       
                                                                break;
        case GR_TEXFMT_AYIQ_8422:       _txImgDequantizeAYIQ8422(dst, src, w, h, 
                                                                        pxMip->pal); break;
        case GR_TEXFMT_RGB_565:         _txImgDequantizeRGB565(dst, src, w, h); 
                                                                break;
        case GR_TEXFMT_ARGB_1555:       _txImgDequantizeARGB1555(dst, src, w, h);       
                                                                break;
        case GR_TEXFMT_ARGB_4444:       _txImgDequantizeARGB4444(dst, src, w, h);       
                                                                break;
        case GR_TEXFMT_AI_88:           _txImgDequantizeAI88(dst, src, w, h);           
                                                                break;
        case GR_TEXFMT_AP_88:           _txImgDequantizeAP88(dst, src, w, h, 
                                                                        pxMip->pal); break;
        case GR_TEXFMT_ARGB_8888:       _txImgDequantizeARGB8888(dst, src, w, h);       
                                                                break;
        case GR_TEXFMT_RGB_888:         _txImgDequantizeRGB888(dst, src, w, h);       
                                                                break;

        default:                                                                                                        
                                                                break;
        }

        if (w > 1) w >>= 1;
        if (h > 1) h >>= 1;
    }
    if( txVerbose )
      {
        printf(".\n"); fflush(stdout);
      }
}