-
Notifications
You must be signed in to change notification settings - Fork 0
/
tgaload.cpp
398 lines (310 loc) · 10.9 KB
/
tgaload.cpp
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
392
393
394
395
396
397
/*
...
From: http://nehe.gamedev.net
Aug 19th:
Added support for runlength encoding - changed some stuff around to make this
possible. Works well :)
Sept 7th:
Improved error trapping and recovery.
Oct 22nd:
Major source clearout & Can compress the image using S3_TC algorithm if the
driver supports it.
Nov 10th:
'Settled' version of the code - traps for nearly all errors - added a
LoadAndBind function for even lazier people :)
TGA_NO_PASS was added in case you need to load an image and pass it yourself.
Finally exorcised all the paletted texture code...
*/
#include <windows.h>
#include <GL\glu.h>
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include "tgaload.h"
/* Extension Management */
PFNGLCOMPRESSEDTEXIMAGE2DARBPROC glCompressedTexImage2DARB = NULL;
PFNGLGETCOMPRESSEDTEXIMAGEARBPROC glGetCompressedTexImageARB = NULL;
/* Default support - lets be optimistic! */
bool tgaCompressedTexSupport = true;
void tgaGetExtensions ( void )
{
glCompressedTexImage2DARB = ( PFNGLCOMPRESSEDTEXIMAGE2DARBPROC )
wglGetProcAddress ( "glCompressedTexImage2DARB" );
glGetCompressedTexImageARB = ( PFNGLGETCOMPRESSEDTEXIMAGEARBPROC )
wglGetProcAddress ( "glGetCompressedTexImageARB" );
if ( glCompressedTexImage2DARB == NULL || glGetCompressedTexImageARB == NULL )
tgaCompressedTexSupport = false;
}
void tgaSetTexParams ( unsigned int min_filter, unsigned int mag_filter, unsigned int application )
{
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter );
glTexEnvf ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, application );
}
unsigned char *tgaAllocMem ( tgaHeader_t info )
{
unsigned char *block;
block = (unsigned char*) malloc ( info.bytes );
if ( block == NULL )
return 0;
memset ( block, 0x00, info.bytes );
return block;
}
void tgaPutPacketTuples ( image_t *p, unsigned char *temp_colour, int ¤t_byte )
{
if ( p->info.components == 3 )
{
p->data[current_byte] = temp_colour[2];
p->data[current_byte+1] = temp_colour[1];
p->data[current_byte+2] = temp_colour[0];
current_byte += 3;
}
if ( p->info.components == 4 ) // Because its BGR(A) not (A)BGR :(
{
p->data[current_byte] = temp_colour[2];
p->data[current_byte+1] = temp_colour[1];
p->data[current_byte+2] = temp_colour[0];
p->data[current_byte+3] = temp_colour[3];
current_byte += 4;
}
}
void tgaGetAPacket ( int ¤t_byte, image_t *p, FILE *file )
{
unsigned char packet_header;
int run_length;
unsigned char temp_colour[4] = { 0x00, 0x00, 0x00, 0x00 };
fread ( &packet_header, ( sizeof ( unsigned char )), 1, file );
run_length = ( packet_header&0x7F ) + 1;
if ( packet_header&0x80 ) // RLE packet
{
fread ( temp_colour, ( sizeof ( unsigned char )* p->info.components ), 1, file );
if ( p->info.components == 1 ) // Special optimised case :)
{
memset ( p->data + current_byte, temp_colour[0], run_length );
current_byte += run_length;
} else
for ( int i = 0; i < run_length; i++ )
tgaPutPacketTuples ( p, temp_colour, current_byte );
}
if ( !( packet_header&0x80 )) // RAW packet
{
for ( int i = 0; i < run_length; i++ )
{
fread ( temp_colour, ( sizeof ( unsigned char )* p->info.components ), 1, file );
if ( p->info.components == 1 )
{
memset ( p->data + current_byte, temp_colour[0], run_length );
current_byte += run_length;
} else
tgaPutPacketTuples ( p, temp_colour, current_byte );
}
}
}
void tgaGetPackets ( image_t *p, FILE *file )
{
int current_byte = 0;
while ( current_byte < p->info.bytes )
tgaGetAPacket ( current_byte, p, file );
}
void tgaGetImageData ( image_t *p, FILE *file )
{
unsigned char temp;
p->data = tgaAllocMem ( p->info );
/* Easy unRLE image */
if ( p->info.image_type == 1 || p->info.image_type == 2 || p->info.image_type == 3 )
{
fread ( p->data, sizeof (unsigned char), p->info.bytes, file );
/* Image is stored as BGR(A), make it RGB(A) */
for (int i = 0; i < p->info.bytes; i += p->info.components )
{
temp = p->data[i];
p->data[i] = p->data[i + 2];
p->data[i + 2] = temp;
}
}
/* RLE compressed image */
if ( p->info.image_type == 9 || p->info.image_type == 10 )
tgaGetPackets ( p, file );
}
void tgaUploadImage ( image_t *p, tgaFLAG mode )
{
/* Determine TGA_LOWQUALITY internal format
This directs OpenGL to upload the textures at half the bit
precision - saving memory
*/
GLenum internal_format = p->info.tgaColourType;
if ( mode&TGA_LOW_QUALITY )
{
switch ( p->info.tgaColourType )
{
case GL_RGB : internal_format = GL_RGB4; break;
case GL_RGBA : internal_format = GL_RGBA4; break;
case GL_LUMINANCE : internal_format = GL_LUMINANCE4; break;
case GL_ALPHA : internal_format = GL_ALPHA4; break;
}
}
/* Let OpenGL decide what the best compressed format is each case. */
if ( mode&TGA_COMPRESS && tgaCompressedTexSupport )
{
switch ( p->info.tgaColourType )
{
case GL_RGB : internal_format = GL_COMPRESSED_RGB_ARB; break;
case GL_RGBA : internal_format = GL_COMPRESSED_RGBA_ARB; break;
case GL_LUMINANCE : internal_format = GL_COMPRESSED_LUMINANCE_ARB; break;
case GL_ALPHA : internal_format = GL_COMPRESSED_ALPHA_ARB; break;
}
}
/* Pass OpenGL Texture Image */
if ( !( mode&TGA_NO_MIPMAPS ))
gluBuild2DMipmaps ( GL_TEXTURE_2D, internal_format, p->info.width,
p->info.height, p->info.tgaColourType, GL_UNSIGNED_BYTE, p->data );
else
glTexImage2D ( GL_TEXTURE_2D, 0, internal_format, p->info.width,
p->info.height, 0, p->info.tgaColourType, GL_UNSIGNED_BYTE, p->data );
}
void tgaFree ( image_t *p )
{
if ( p->data != NULL )
free ( p->data );
}
void tgaChecker ( image_t *p )
{
unsigned char TGA_CHECKER[16384];
unsigned char *pointer;
// 8bit image
p->info.image_type = 3;
p->info.width = 128;
p->info.height = 128;
p->info.pixel_depth = 8;
// Set some stats
p->info.components = 1;
p->info.bytes = p->info.width * p->info.height * p->info.components;
pointer = TGA_CHECKER;
for ( int j = 0; j < 128; j++ )
{
for ( int i = 0; i < 128; i++ )
{
if ((i ^ j) & 0x10 )
pointer[0] = 0x00;
else
pointer[0] = 0xff;
pointer ++;
}
}
p->data = TGA_CHECKER;
glTexImage2D ( GL_TEXTURE_2D, 0, GL_LUMINANCE4, p->info.width,
p->info.height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, p->data );
/* Should we free? I dunno. The scope of TGA_CHECKER _should_ be local, so it
probably gets destroyed automatically when the function completes... */
// tgaFree ( p );
}
void tgaError ( char *error_string, char *file_name, FILE *file, image_t *p )
{
printf ( "%s - %s\n", error_string, file_name );
tgaFree ( p );
fclose ( file );
tgaChecker ( p );
}
void tgaGetImageHeader ( FILE *file, tgaHeader_t *info )
{
/* Stupid byte alignment means that we have to fread each field
individually. I tried splitting tgaHeader into 3 structures, no matter
how you arrange them, colour_map_entry_size comes out as 2 bytes instead
1 as it should be. Grrr. Gotta love optimising compilers - theres a pragma
for Borland, but I dunno the number for MSVC or GCC :(
*/
fread ( &info->id_length, ( sizeof (unsigned char )), 1, file );
fread ( &info->colour_map_type, ( sizeof (unsigned char )), 1, file );
fread ( &info->image_type, ( sizeof (unsigned char )), 1, file );
fread ( &info->colour_map_first_entry, ( sizeof (short int )), 1, file );
fread ( &info->colour_map_length , ( sizeof (short int )), 1, file );
fread ( &info->colour_map_entry_size , ( sizeof (unsigned char )), 1, file );
fread ( &info->x_origin , ( sizeof (short int )), 1, file );
fread ( &info->y_origin , ( sizeof (short int )), 1, file );
fread ( &info->width, ( sizeof (short int )), 1, file );
fread ( &info->height, ( sizeof (short int )), 1, file );
fread ( &info->pixel_depth, ( sizeof (unsigned char )), 1, file );
fread ( &info->image_descriptor,( sizeof (unsigned char )), 1, file );
// Set some stats
info->components = info->pixel_depth / 8;
info->bytes = info->width * info->height * info->components;
}
int tgaLoadTheImage ( char *file_name, image_t *p, tgaFLAG mode )
{
FILE *file;
tgaGetExtensions ( );
p->data = NULL;
if (( file = fopen ( file_name, "rb" )) == NULL )
{
tgaError ( "File not found", file_name, file, p );
return 0;
}
tgaGetImageHeader ( file, &p->info );
switch ( p->info.image_type )
{
case 1 :
tgaError ( "8-bit colour no longer supported", file_name, file, p );
return 0;
case 2 :
if ( p->info.pixel_depth == 24 )
p->info.tgaColourType = GL_RGB;
else if ( p->info.pixel_depth == 32 )
p->info.tgaColourType = GL_RGBA;
else
{
tgaError ( "Unsupported RGB format", file_name, file, p );
return 0;
}
break;
case 3 :
if ( mode&TGA_LUMINANCE )
p->info.tgaColourType = GL_LUMINANCE;
else if ( mode&TGA_ALPHA )
p->info.tgaColourType = GL_ALPHA;
else
{
tgaError ( "Must be LUMINANCE or ALPHA greyscale", file_name, file, p );
return 0;
}
break;
case 9 :
tgaError ( "8-bit colour no longer supported", file_name, file, p );
return 0;
case 10 :
if ( p->info.pixel_depth == 24 )
p->info.tgaColourType = GL_RGB;
else if ( p->info.pixel_depth == 32 )
p->info.tgaColourType = GL_RGBA;
else
{
tgaError ( "Unsupported compressed RGB format", file_name, file, p );
return 0;
}
}
tgaGetImageData ( p, file );
fclose ( file );
return 1;
}
void tgaLoad ( char *file_name, image_t *p, tgaFLAG mode )
{
if ( tgaLoadTheImage ( file_name, p, mode ))
{
if ( !( mode&TGA_NO_PASS ))
tgaUploadImage ( p, mode );
if ( mode&TGA_FREE )
tgaFree ( p );
}
}
GLuint tgaLoadAndBind ( char *file_name, tgaFLAG mode )
{
GLuint texture_id;
image_t *p;
glGenTextures ( 1, &texture_id );
glBindTexture ( GL_TEXTURE_2D, texture_id );
if ( tgaLoadTheImage ( file_name, p, mode ))
{
tgaUploadImage ( p, mode );
tgaFree ( p );
}
return texture_id;
}