source: trunk/src/opengl/glide/cvg/texus/3df.c

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

Added $Id:$ keyword.

File size: 7.1 KB
Line 
1/* $Id: 3df.c,v 1.2 2001-09-05 14:30:43 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 */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <math.h>
23
24#include "texusint.h"
25
26/*************************************** 3df files ****************************/
27/* Read word, msb first */
28static FxBool
29_txRead16 (FILE *stream, FxU16* data)
30{
31 FxU8 byte[2];
32
33 if (fread (byte, 2, 1, stream) != 1) return FXFALSE;
34 *data = (((FxU16) byte[0]) << 8) | ((FxU16) byte[1]);
35 return FXTRUE;
36}
37
38/* Read long word, msb first */
39static FxBool
40_txRead32 (FILE *stream, FxU32* data)
41{
42 FxU8 byte[4];
43
44 if (fread (byte, 4, 1, stream) != 1) return FXFALSE;
45
46 *data = (((FxU32) byte[0]) << 24) |
47 (((FxU32) byte[1]) << 16) |
48 (((FxU32) byte[2]) << 8) |
49 ((FxU32) byte[3]);
50
51 return FXTRUE;
52}
53
54
55/* Read NCC table */
56static FxBool
57_txRead3DFNCCTable (FILE* stream, FxI32* ncc_table)
58{
59 FxU32 i;
60 FxI16 data;
61
62 /* read Y */
63 for (i = 0; i < 16; i ++) {
64 if (FXFALSE == _txRead16 (stream, (FxU16 *)&data)) return FXFALSE;
65 ncc_table[i] = (FxU8) data;
66 }
67 /* read A */
68 for (i = 0; i < 4; i++) {
69 if (!_txRead16 (stream, (FxU16 *)&data)) return FXFALSE;
70 ncc_table[16 + 3 * i + 0] = (FxI32) data;
71 if (!_txRead16 (stream, (FxU16 *)&data)) return FXFALSE;
72 ncc_table[16 + 3 * i + 1] = (FxI32) data;
73 if (!_txRead16 (stream, (FxU16 *)&data)) return FXFALSE;
74 ncc_table[16 + 3 * i + 2] = (FxI32) data;
75 }
76
77 /* read B */
78 for (i = 0; i < 4; i++) {
79 if (!_txRead16 (stream, (FxU16 *)&data)) return FXFALSE;
80 ncc_table[28 + 3 * i + 0] = (FxI32) data;
81 if (!_txRead16 (stream, (FxU16 *)&data)) return FXFALSE;
82 ncc_table[28 + 3 * i + 1] = (FxI32) data;
83 if (!_txRead16 (stream, (FxU16 *)&data)) return FXFALSE;
84 ncc_table[28 + 3 * i + 2] = (FxI32) data;
85 }
86
87 return FXTRUE;
88}
89
90static FxBool
91_txRead3DFPalTable (FILE* stream, FxI32* pal)
92{
93 FxU32 i;
94
95 /* read Y */
96 for (i = 0; i < 256; i ++) {
97 if (FXFALSE == _txRead32 (stream, (FxU32 *)&pal[i])) return FXFALSE;
98 }
99 return FXTRUE;
100}
101
102/* Read 3df header */
103FxBool
104_txRead3DFHeader(FILE* stream, FxU32 cookie, TxMip *txMip)
105{
106 int c, i;
107 char version[6];
108 char color_format[10];
109 int lod_min, lod_max;
110 int aspect_width, aspect_height;
111 int w, h;
112
113 /* magic cookie starts with 3df v%6...., of which we've eaten 3d */
114 // printf("3df file header\n");
115 if (fscanf (stream, "f v%6s", version) != 1) return FXFALSE;
116
117 /*
118 * skip comments
119 */
120 while (((c = getc (stream)) != EOF) && (c == '#')) {
121 while (((c = getc (stream)) != EOF) && (c != '\n'));
122 if (c == EOF) return FXFALSE;
123 }
124 if (c == EOF) return FXFALSE;
125 ungetc (c, stream);
126
127 /* color format, lod range, aspect ratio */
128 if (5 != fscanf (stream, "%10s lod range: %i %i aspect ratio: %i %i",
129 color_format, &lod_min, &lod_max, &aspect_width, &aspect_height))
130 return FXFALSE;
131
132 // printf("%s %d %d (lods) %d %d (aspect)\n", color_format, lod_min, lod_max, aspect_width, aspect_height);
133
134 /* eat final nl */
135 if (((c = getc (stream)) == EOF) && (c != '\n'))
136 return FXFALSE;
137
138 /* make sure null terminated */
139 color_format[9] = 0;
140
141 /* lookup name */
142 for (i = 0; i <= GR_TEXFMT_ARGB_8888; i++)
143 if (strcmp (Format_Name[i], color_format) == 0) break;
144 if (i > GR_TEXFMT_ARGB_8888) return FXFALSE;
145 txMip->format = i;
146
147 /* Validate lod */
148 if ((lod_max & (lod_max-1)) || (lod_min & (lod_min-1))) return FXFALSE;
149 if ((lod_max > 256) || (lod_max < 1)) return FXFALSE;
150 if ((lod_min > 256) || (lod_min < 1)) return FXFALSE;
151 if (lod_min > lod_max) return FXFALSE;
152
153 /* validate aspect ratio */
154 w = h = lod_max;
155 switch ((aspect_width << 4) | (aspect_height)) {
156 case 0x81: h = h/8; break;
157 case 0x41: h = h/4; break;
158 case 0x21: h = h/2; break;
159 case 0x11: h = h/1; break;
160 case 0x12: w = w/2; break;
161 case 0x14: w = w/4; break;
162 case 0x18: w = w/8; break;
163 default: return FXFALSE;
164 }
165
166 txMip->width = w;
167 txMip->height = h;
168 txMip->size = w * h;
169
170 for (txMip->depth=1; lod_max > lod_min; lod_max >>= 1) {
171 // printf("w = %d, h = %d, lod_max = %d\n", w, h, lod_max);
172 txMip->depth++;
173 if (w > 1) w >>= 1;
174 if (h > 1) h >>= 1;
175 txMip->size += (w * h);
176 }
177 txMip->size *= GR_TEXFMT_SIZE(txMip->format);
178
179 // printf("3df file: %dx%dx%d:%d\n", txMip->width, txMip->height, txMip->depth, txMip->size);
180 return FXTRUE;
181}
182
183FxBool
184_txRead3DFData( FILE *stream, TxMip *txMip)
185{
186 int i, npixels;
187
188 // printf("3df file data\n");
189 /* First read NCC tables */
190 if ((txMip->format == GR_TEXFMT_YIQ_422) ||
191 (txMip->format == GR_TEXFMT_AYIQ_8422)) {
192 if (!_txRead3DFNCCTable (stream, (FxI32 *)txMip->pal)) {
193 txError("Bad Ncc table\n");
194 return FXFALSE;
195 }
196 }
197
198 if ((txMip->format == GR_TEXFMT_P_8) ||
199 (txMip->format == GR_TEXFMT_AP_88)) {
200 if (!_txRead3DFPalTable (stream, (FxI32*)txMip->pal)) {
201 txError("Bad Palette table\n");
202 return FXFALSE;
203 }
204 }
205
206 /* read mipmap image data */
207 if (txMip->format < GR_TEXFMT_16BIT) {
208 npixels = txMip->size;
209
210 if ((FxU32) npixels != fread (txMip->data[0], 1, npixels, stream)) {
211 txError("Bad 8 bit data");
212 return FXFALSE;
213 }
214 }
215 else if (txMip->format < GR_TEXFMT_32BIT) {
216 FxU16* data = (FxU16 *) txMip->data[0];
217 npixels = txMip->size >> 1;
218
219 for (i = 0; i < npixels; i++, data++)
220 if (FXFALSE == _txRead16 (stream, data)) {
221 txError("Bad 16 bit data");
222 return FXFALSE;
223 }
224 }
225 else {
226 FxU32* data = (FxU32*) txMip->data[0];
227 npixels = txMip->size >> 2;
228
229 for (i = 0; i < npixels; i ++, data++)
230 if (FXFALSE == _txRead32 (stream, data)) {
231 txError("Bad 32 bit data");
232 return FXFALSE;
233 }
234 }
235 return FXTRUE;
236}
Note: See TracBrowser for help on using the repository browser.