-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtga.h
333 lines (270 loc) · 7.98 KB
/
tga.h
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <windows.h>
//#include <gl/gl.h>
class TGA
{
public:
enum TGAFormat
{
RGB = 0x1907,
RGBA = 0x1908,
ALPHA = 0x1906,
UNKNOWN = -1
};
enum TGAError
{
TGA_NO_ERROR = 1, // No error
TGA_FILE_NOT_FOUND, // File was not found
TGA_BAD_IMAGE_TYPE, // Color mapped image or image is not uncompressed
TGA_BAD_DIMENSION, // Dimension is not a power of 2
TGA_BAD_BITS, // Image bits is not 8, 24 or 32
TGA_BAD_DATA // Image data could not be loaded
};
TGA(void) :
m_texFormat(TGA::UNKNOWN),
m_nImageWidth(0),
m_nImageHeight(0),
m_nImageBits(0),
m_nImageData(NULL) {}
~TGA(void);
TGA::TGAError load( const char *name );
TGA::TGAError saveFromExternalData( const char *name, int w, int h, TGAFormat fmt, const unsigned char *externalImage );
TGAFormat m_texFormat;
int m_nImageWidth;
int m_nImageHeight;
int m_nImageBits;
unsigned char * m_nImageData;
private:
int returnError(FILE *s, int error);
unsigned char *getRGBA(FILE *s, int size);
unsigned char *getRGB(FILE *s, int size);
unsigned char *getGray(FILE *s, int size);
void writeRGBA(FILE *s, const unsigned char *externalImage, int size);
void writeRGB(FILE *s, const unsigned char *externalImage, int size);
void writeGrayAsRGB(FILE *s, const unsigned char *externalImage, int size);
void writeGray(FILE *s, const unsigned char *externalImage, int size);
};
#ifndef TGA_NOIMPL
TGA::~TGA( void )
{
if( m_nImageData != NULL )
{
free( m_nImageData );
m_nImageData = NULL;
}
}
int TGA::returnError( FILE *s, int error )
{
// Called when there is an error loading the .tga texture file.
fclose( s );
return error;
}
unsigned char *TGA::getRGBA( FILE *s, int size )
{
// Read in RGBA data for a 32bit image.
unsigned char *rgba;
unsigned char temp;
int bread;
int i;
rgba = (unsigned char *)malloc( size * 4 );
if( rgba == NULL )
return 0;
bread = (int)fread( rgba, sizeof (unsigned char), size * 4, s );
// TGA is stored in BGRA, make it RGBA
if( bread != size * 4 )
{
free( rgba );
return 0;
}
for( i = 0; i < size * 4; i += 4 )
{
temp = rgba[i];
rgba[i] = rgba[i + 2];
rgba[i + 2] = temp;
}
m_texFormat = TGA::RGBA;
return rgba;
}
unsigned char *TGA::getRGB( FILE *s, int size )
{
// Read in RGB data for a 24bit image.
unsigned char *rgb;
unsigned char temp;
int bread;
int i;
rgb = (unsigned char*)malloc( size * 3 );
if( rgb == NULL )
return 0;
bread = (int)fread( rgb, sizeof (unsigned char), size * 3, s );
if(bread != size * 3)
{
free( rgb );
return 0;
}
// TGA is stored in BGR, make it RGB
for( i = 0; i < size * 3; i += 3 )
{
temp = rgb[i];
rgb[i] = rgb[i + 2];
rgb[i + 2] = temp;
}
m_texFormat = TGA::RGB;
return rgb;
}
unsigned char *TGA::getGray( FILE *s, int size )
{
// Gets the grayscale image data. Used as an alpha channel.
unsigned char *grayData;
int bread;
grayData = (unsigned char*)malloc( size );
if( grayData == NULL )
return 0;
bread = (int)fread( grayData, sizeof (unsigned char), size, s );
if( bread != size )
{
free( grayData );
return 0;
}
m_texFormat = TGA::ALPHA;
return grayData;
}
TGA::TGAError TGA::load( const char *name )
{
// Loads up a targa file. Supported types are 8,24 and 32
// uncompressed images.
unsigned char type[4];
unsigned char info[7];
FILE *s = NULL;
int size = 0;
if( !(s = fopen( name, "rb" )) )
return TGA_FILE_NOT_FOUND;
fread( &type, sizeof (char), 3, s ); // Read in colormap info and image type, byte 0 ignored
fseek( s, 12, SEEK_SET); // Seek past the header and useless info
fread( &info, sizeof (char), 6, s );
if( type[1] != 0 || (type[2] != 2 && type[2] != 3) )
returnError( s, TGA_BAD_IMAGE_TYPE );
m_nImageWidth = info[0] + info[1] * 256;
m_nImageHeight = info[2] + info[3] * 256;
m_nImageBits = info[4];
size = m_nImageWidth * m_nImageHeight;
// Make sure we are loading a supported type
if( m_nImageBits != 32 && m_nImageBits != 24 && m_nImageBits != 8 )
returnError( s, TGA_BAD_BITS );
if( m_nImageBits == 32 )
m_nImageData = getRGBA( s, size );
else if( m_nImageBits == 24 )
m_nImageData = getRGB( s, size );
else if( m_nImageBits == 8 )
m_nImageData = getGray( s, size );
// No image data
if( m_nImageData == NULL )
returnError( s, TGA_BAD_DATA );
fclose( s );
return TGA_NO_ERROR;
}
void TGA::writeRGBA( FILE *s, const unsigned char *externalImage, int size )
{
// Read in RGBA data for a 32bit image.
unsigned char *rgba;
int bread;
int i;
rgba = (unsigned char *)malloc( size * 4 );
// switch RGBA to BGRA
for( i = 0; i < size * 4; i += 4 )
{
rgba[i + 0] = externalImage[i + 2];
rgba[i + 1] = externalImage[i + 1];
rgba[i + 2] = externalImage[i + 0];
rgba[i + 3] = externalImage[i + 3];
}
bread = (int)fwrite( rgba, sizeof (unsigned char), size * 4, s );
free( rgba );
}
void TGA::writeRGB( FILE *s, const unsigned char *externalImage, int size )
{
// Read in RGBA data for a 32bit image.
unsigned char *rgb;
int bread;
int i;
rgb = (unsigned char *)malloc( size * 3 );
// switch RGB to BGR
for( i = 0; i < size * 3; i += 3 )
{
rgb[i + 0] = externalImage[i + 2];
rgb[i + 1] = externalImage[i + 1];
rgb[i + 2] = externalImage[i + 0];
}
bread = (int)fwrite( rgb, sizeof (unsigned char), size * 3, s );
free( rgb );
}
void TGA::writeGrayAsRGB( FILE *s, const unsigned char *externalImage, int size )
{
// Read in RGBA data for a 32bit image.
unsigned char *rgb;
int bread;
int i;
rgb = (unsigned char *)malloc( size * 3 );
// switch RGB to BGR
int j = 0;
for( i = 0; i < size * 3; i += 3, j++ )
{
rgb[i + 0] = externalImage[j];
rgb[i + 1] = externalImage[j];
rgb[i + 2] = externalImage[j];
}
bread = (int)fwrite( rgb, sizeof (unsigned char), size * 3, s );
free( rgb );
}
void TGA::writeGray( FILE *s, const unsigned char *externalImage, int size )
{
// Gets the grayscale image data. Used as an alpha channel.
int bread;
bread = (int)fwrite( externalImage, sizeof (unsigned char), size, s );
}
TGA::TGAError TGA::saveFromExternalData( const char *name, int w, int h, TGA::TGAFormat fmt, const unsigned char *externalImage )
{
static unsigned char type[] = {0,0,2};
static unsigned char dummy[] = {0,0,0,0,0,0,0,0,0};
static unsigned char info[] = {0,0,0,0,0,0};
FILE *s = NULL;
int size = 0;
if( !(s = fopen( name, "wb" )) )
return TGA_FILE_NOT_FOUND;
fwrite( type, sizeof (char), 3, s ); // Read in colormap info and image type, byte 0 ignored
fwrite( dummy, sizeof (char), 9, s ); // Read in colormap info and image type, byte 0 ignored
info[0] = w & 0xFF;
info[1] = (w>>8) & 0xFF;
info[2] = h & 0xFF;
info[3] = (h>>8) & 0xFF;
switch(fmt)
{
case ALPHA:
info[4] = 8;
break;
case RGB:
info[4] = 24;
break;
case RGBA:
info[4] = 32;
break;
}
fwrite( info, sizeof (char), 6, s );
size = w*h;
switch(fmt)
{
case ALPHA:
writeGray(s, externalImage, size);
break;
case RGB:
writeGrayAsRGB/*writeRGB*/(s, externalImage, size);
break;
case RGBA:
writeRGBA(s, externalImage, size);
break;
}
fclose( s );
return TGA_NO_ERROR;
}
#endif