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
|
/*
** 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.3 $
** $Date: 2000/10/03 18:31:53 $
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "texusint.h"
#define TX_3DF 0x100
#define TX_PPM 0x101
#define TX_BMP 0x102
#define TX_IFF 0x103
#define TX_PCX 0x104
#define TX_SBI 0x105
#define TX_RGT 0x106
#define TX_UNK 0x200 // TGA is unknown from cookie signature.
int
_txReadHeader( FILE *stream, TxMip *info )
{
FxU32 cookie;
int c;
int fformat; // format of the image file.
int status = 0;
if ( stream == NULL ) {
txError("Bad file handle.");
return 0;
}
if ((c=getc( stream )) == EOF ) {
txError("Unexpected end of file");
return 0;
}
cookie = (c << 8);
if ((c=getc( stream )) == EOF ) {
txError("Unexpected end of file");
return 0;
}
cookie |= c;
switch( cookie )
{
case ('P' << 8) | '9': fformat = TX_SBI; break;
case ('P' << 8) | '6': fformat = TX_PPM; break;
case ('3' << 8) | 'd': fformat = TX_3DF; break;
case ('3' << 8) | 'D': fformat = TX_3DF; break;
case 0x01DA: // byte swapped RGT file.
case 0xDA01: fformat = TX_RGT; break;
default: fformat = TX_UNK; break;
}
switch (fformat) {
case TX_SBI: status = _txReadSBIHeader( stream, cookie, info ); break;
case TX_RGT: status = _txReadRGTHeader( stream, cookie, info ); break;
case TX_PPM: status = _txReadPPMHeader( stream, cookie, info ); break;
case TX_3DF: status = _txRead3DFHeader( stream, cookie, info ); break;
case TX_UNK: status = _txReadTGAHeader( stream, cookie, info ); break;
}
/* Not reached. */
return (status) ? fformat : 0;
}
static FxBool
_txReadData( FILE *stream, int fformat, TxMip *info )
{
switch (fformat) {
case TX_SBI: return _txReadSBIData( stream, info );
case TX_RGT: return _txReadRGTData( stream, info );
case TX_PPM: return _txReadPPMData( stream, info );
case TX_3DF: return _txRead3DFData( stream, info );
case TX_UNK: return _txReadTGAData( stream, info );
}
/* Not reached. */
return FXFALSE;
}
FxBool
txMipRead(TxMip *txMip, const char *filename, int prefFormat)
{
FILE *file;
FxBool retval;
file = fopen(filename, "rb");
if( file == NULL )
{
fprintf( stderr,"Error: can't open input file %s\n", filename );
exit(2);
}
retval = txMipReadFromFP( txMip, filename, file, prefFormat );
fclose(file);
return retval;
}
FxBool
txMipReadFromFP(TxMip *txMip, const char *debug_filename, FILE *file, int prefFormat)
{
int i, format;
TxMip txTrue;
int w, h;
if ((prefFormat != GR_TEXFMT_ARGB_8888) &&
(prefFormat != GR_TEXFMT_ANY))
{
txPanic("txMipRead: bad preferred format.");
return FXFALSE;
}
if ( (format = _txReadHeader( file, txMip )) == 0 ) {
fprintf(stderr,"Error: reading info for %s, %s\n",
debug_filename, "");
exit(2);
}
if( txVerbose )
{
fprintf(stderr,"Loading image file ");
fprintf (stderr,"%s (%dw x %dh x %d Bpp x %d mips) .. ", debug_filename,
txMip->width,txMip->height, GR_TEXFMT_SIZE(txMip->format), txMip->depth);
}
/*
* Allocate memory requested in data[0];
*/
w = txMip->width;
h = txMip->height;
txMip->data[0] = txMalloc(txMip->size);
for (i=1; i< TX_MAX_LEVEL; i++) {
if (i >= txMip->depth) {
txMip->data[i] = NULL;
continue;
}
txMip->data[i] = (FxU8*)txMip->data[i-1] +
w * h * GR_TEXFMT_SIZE(txMip->format);
if (w > 1) w >>= 1;
if (h > 1) h >>= 1;
}
if( txVerbose ) {
fprintf( stderr, "mip-> format: %d width: %d height: %d depth: %d size: %d\n",
txMip->format, txMip->width, txMip->height, txMip->depth,
txMip->size );
fflush( stderr );
}
if ( _txReadData( file, format, txMip ) == FXFALSE ) {
fprintf(stderr, "\nError: reading data for %s\n",debug_filename);
exit(4);
}
if( txVerbose )
fprintf(stderr," done.\n");
if (prefFormat == GR_TEXFMT_ANY) return FXTRUE;
/*
* Now convert to ARGB8888 if requested.
*/
txTrue.format = GR_TEXFMT_ARGB_8888;
txTrue.width = txMip->width;
txTrue.height = txMip->height;
txTrue.depth = txMip->depth;
if (txMipAlloc(&txTrue) == FXFALSE) return FXFALSE;
/*
* Dequantize from src to dest.
*/
if( txVerbose )
{
fprintf(stderr, "Dequantizing Input from %s to argb8888.\n",
Format_Name[txMip->format]);
}
txMipDequantize(&txTrue, txMip);
/*
* Free the original mipmap, and copy new mipmap into it.
*/
txFree(txMip->data[0]);
*txMip = txTrue;
return FXTRUE;
}
FxBool _txReadSBIHeader( FILE *stream, FxU32 cookie, TxMip *info){ return FXFALSE;}
FxBool _txReadSBIData( FILE *stream, TxMip *info){ return FXFALSE;}
|