-
Notifications
You must be signed in to change notification settings - Fork 1
/
tga.cpp
executable file
·110 lines (83 loc) · 2.12 KB
/
tga.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
// A slightly rewritten version of the TGA function used in
// applications such as Dojo, Guardian and Campus Rumble
//It now uses standard ANSI C
// Loading functions to maintain some portablility
// This version also flips the image instead of just changing
// color positions
#include "tga.h"
targa::targa()
{
width = 0;
height = 0;
buffer = NULL;
}
targa::targa(char* filename)
{
tga_header tgahead;
int size;
BYTE* temp;
FILE* fp;
fp = fopen(filename, "rb");
if(!fp)
{
//should be replaced with logging functions
//MessageBox(NULL,"Could not find TGA file","TGA load error",NULL);
buffer = NULL;
return;
}
fread(&tgahead,1,sizeof(tga_header),fp);
colordepth = tgahead.bits_per_pixel;
//For 24 bit color textures
if(tgahead.bits_per_pixel==24)
{
width=tgahead.image_width;
height=tgahead.image_height;
size=width*height*3;
buffer=new BYTE[size];
fread(buffer,size,sizeof(unsigned char),fp);
temp= new BYTE[size];
memcpy(temp,buffer,size);
//flips the colors
for(int index=0;index<height;index++)
memcpy(&buffer[index*width*3],&temp[(height-index-1)*width*3],width*3);
delete temp;
}
//For 8 bit alpha textures
else if(tgahead.bits_per_pixel==8)
{
width=tgahead.image_width;
height=tgahead.image_height;
size=width*height;
buffer=new BYTE[size];
fread(buffer,size,sizeof(unsigned char),fp);
}
//Finally 32 bit RGBA textures
else if(tgahead.bits_per_pixel==32)
{
width=tgahead.image_width;
height=tgahead.image_height;
size=width*height*4;
buffer=new BYTE[size];
fread(buffer,size,sizeof(unsigned char),fp);
temp= new BYTE[size];
memcpy(temp,buffer,size);
//Flips the image
for(int index=0;index<height;index++)
memcpy(&buffer[index*width*4],&temp[(height-index-1)*width*4],width*4);
delete temp;
}
if(buffer==NULL)
{
//should be replaced with logging functions
//MessageBox(NULL,"TGA file empty?","TGA Load error",NULL);
}
fclose(fp);
}
targa::~targa()
{
if(buffer!=NULL)
{
delete [] buffer;
buffer = NULL;
}
}