source: trunk/src/opengl/glide/cvg/texus/read.c

Last change on this file was 6653, checked in by bird, 24 years ago

Added $Id:$ keyword.

File size: 6.5 KB
Line 
1/* $Id: read.c,v 1.2 2001-09-05 14:30:46 bird Exp $ */
2/*
3** THIS SOFTWARE IS SUBJECT TO COPYRIGHT PROTECTION AND IS OFFERED ONLY
4** PURSUANT TO THE 3DFX GLIDE GENERAL PUBLIC LICENSE. THERE IS NO RIGHT
5** TO USE THE GLIDE TRADEMARK WITHOUT PRIOR WRITTEN PERMISSION OF 3DFX
6** INTERACTIVE, INC. A COPY OF THIS LICENSE MAY BE OBTAINED FROM THE
7** DISTRIBUTOR OR BY CONTACTING 3DFX INTERACTIVE INC(info@3dfx.com).
8** THIS PROGRAM IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
9** EXPRESSED OR IMPLIED. SEE THE 3DFX GLIDE GENERAL PUBLIC LICENSE FOR A
10** FULL TEXT OF THE NON-WARRANTY PROVISIONS.
11**
12** USE, DUPLICATION OR DISCLOSURE BY THE GOVERNMENT IS SUBJECT TO
13** RESTRICTIONS AS SET FORTH IN SUBDIVISION (C)(1)(II) OF THE RIGHTS IN
14** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 252.227-7013,
15** AND/OR IN SIMILAR OR SUCCESSOR CLAUSES IN THE FAR, DOD OR NASA FAR
16** SUPPLEMENT. UNPUBLISHED RIGHTS RESERVED UNDER THE COPYRIGHT LAWS OF
17** THE UNITED STATES.
18**
19** COPYRIGHT 3DFX INTERACTIVE, INC. 1999, ALL RIGHTS RESERVED
20**
21** $Revision: 1.2 $
22** $Date: 2001-09-05 14:30:46 $
23*/
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <math.h>
29
30#include "texusint.h"
31
32#define TX_3DF 0x100
33#define TX_PPM 0x101
34#define TX_BMP 0x102
35#define TX_IFF 0x103
36#define TX_PCX 0x104
37#define TX_SBI 0x105
38#define TX_RGT 0x106
39#define TX_UNK 0x200 // TGA is unknown from cookie signature.
40
41int
42_txReadHeader( FILE *stream, TxMip *info )
43{
44 FxU32 cookie;
45 int c;
46 int fformat; // format of the image file.
47 int status;
48
49 if ( stream == NULL ) {
50 txError("Bad file handle.");
51 return 0;
52 }
53
54 if ((c=getc( stream )) == EOF ) {
55 txError("Unexpected end of file");
56 return 0;
57 }
58 cookie = (c << 8);
59
60 if ((c=getc( stream )) == EOF ) {
61 txError("Unexpected end of file");
62 return 0;
63 }
64 cookie |= c;
65
66 switch( cookie )
67 {
68 case ('P' << 8) | '9': fformat = TX_SBI; break;
69 case ('P' << 8) | '6': fformat = TX_PPM; break;
70 case ('3' << 8) | 'd': fformat = TX_3DF; break;
71 case ('3' << 8) | 'D': fformat = TX_3DF; break;
72 case 0x01DA: // byte swapped RGT file.
73 case 0xDA01: fformat = TX_RGT; break;
74 default: fformat = TX_UNK; break;
75 }
76
77 switch (fformat) {
78 case TX_SBI: status = _txReadSBIHeader( stream, cookie, info ); break;
79 case TX_RGT: status = _txReadRGTHeader( stream, cookie, info ); break;
80 case TX_PPM: status = _txReadPPMHeader( stream, cookie, info ); break;
81 case TX_3DF: status = _txRead3DFHeader( stream, cookie, info ); break;
82 case TX_UNK: status = _txReadTGAHeader( stream, cookie, info ); break;
83 }
84
85 /* Not reached. */
86 return (status) ? fformat : 0;
87}
88
89static FxBool
90_txReadData( FILE *stream, int fformat, TxMip *info )
91{
92 switch (fformat) {
93 case TX_SBI: return _txReadSBIData( stream, info );
94 case TX_RGT: return _txReadRGTData( stream, info );
95 case TX_PPM: return _txReadPPMData( stream, info );
96 case TX_3DF: return _txRead3DFData( stream, info );
97 case TX_UNK: return _txReadTGAData( stream, info );
98 }
99
100 /* Not reached. */
101 return FXFALSE;
102}
103
104
105FxBool
106txMipRead(TxMip *txMip, const char *filename, int prefFormat)
107{
108 FILE *file;
109 FxBool retval;
110
111 file = fopen(filename, "rb");
112 if( file == NULL )
113 {
114 fprintf( stderr,"Error: can't open input file %s\n", filename );
115 exit(2);
116 }
117
118 retval = txMipReadFromFP( txMip, filename, file, prefFormat );
119 fclose(file);
120 return retval;
121}
122
123FxBool
124txMipReadFromFP(TxMip *txMip, const char *debug_filename, FILE *file, int prefFormat)
125{
126 int i, format;
127 TxMip txTrue;
128 int w, h;
129
130 if ((prefFormat != GR_TEXFMT_ARGB_8888) &&
131 (prefFormat != GR_TEXFMT_ANY))
132 {
133 txPanic("txMipRead: bad preferred format.");
134 return FXFALSE;
135 }
136
137 if ( (format = _txReadHeader( file, txMip )) == 0 ) {
138 fprintf(stderr,"Error: reading info for %s, %s\n",
139 debug_filename, "");
140 exit(2);
141 }
142 if( txVerbose )
143 {
144 fprintf(stderr,"Loading image file ");
145
146 fprintf (stderr,"%s (%dw x %dh x %d Bpp x %d mips) .. ", debug_filename,
147 txMip->width,txMip->height, GR_TEXFMT_SIZE(txMip->format), txMip->depth);
148 }
149
150 /*
151 * Allocate memory requested in data[0];
152 */
153
154 w = txMip->width;
155 h = txMip->height;
156 txMip->data[0] = txMalloc(txMip->size);
157 for (i=1; i< TX_MAX_LEVEL; i++) {
158 if (i >= txMip->depth) {
159 txMip->data[i] = NULL;
160 continue;
161 }
162 txMip->data[i] = (FxU8*)txMip->data[i-1] +
163 w * h * GR_TEXFMT_SIZE(txMip->format);
164 if (w > 1) w >>= 1;
165 if (h > 1) h >>= 1;
166 }
167
168 if( txVerbose ) {
169 fprintf( stderr, "mip-> format: %d width: %d height: %d depth: %d size: %d\n",
170 txMip->format, txMip->width, txMip->height, txMip->depth,
171 txMip->size );
172 fflush( stderr );
173 }
174
175 if ( _txReadData( file, format, txMip ) == FXFALSE ) {
176 fprintf(stderr, "\nError: reading data for %s\n",debug_filename);
177 exit(4);
178 }
179
180 if( txVerbose )
181 fprintf(stderr," done.\n");
182 if (prefFormat == GR_TEXFMT_ANY) return FXTRUE;
183
184 /*
185 * Now convert to ARGB8888 if requested.
186 */
187 txTrue.format = GR_TEXFMT_ARGB_8888;
188 txTrue.width = txMip->width;
189 txTrue.height = txMip->height;
190 txTrue.depth = txMip->depth;
191 if (txMipAlloc(&txTrue) == FXFALSE) return FXFALSE;
192
193 /*
194 * Dequantize from src to dest.
195 */
196 if( txVerbose )
197 {
198 fprintf(stderr, "Dequantizing Input from %s to argb8888.\n",
199 Format_Name[txMip->format]);
200 }
201 txMipDequantize(&txTrue, txMip);
202
203 /*
204 * Free the original mipmap, and copy new mipmap into it.
205 */
206 txFree(txMip->data[0]);
207 *txMip = txTrue;
208
209 return FXTRUE;
210}
211
212
213FxBool _txReadSBIHeader( FILE *stream, FxU32 cookie, TxMip *info){ return FXFALSE;}
214FxBool _txReadSBIData( FILE *stream, TxMip *info){ return FXFALSE;}
215
Note: See TracBrowser for help on using the repository browser.