-
Notifications
You must be signed in to change notification settings - Fork 0
/
TIM.cpp
210 lines (150 loc) · 6.66 KB
/
TIM.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
#include "TIM.h"
TIM::TIM() {
// Inicializa variaveis
timSize = 0;
timData = NULL;
rawTexture = NULL;
}
TIM::~TIM() {
// Limpa memória
for (unsigned int i = 0; i < timTexture.numbCluts; i++) {
free(colorTable[i]);
}
if (rawTexture != NULL)
free(rawTexture);
rawTexture = NULL;
colorTable = NULL;
timData = NULL;
}
void TIM::readFromPtr(unsigned char *data) {
timData = data;
// Leitura do Header até numbCluts, numbCluts é o tamanho do
// clutData que valos alocar dinamicamente
memcpy(&timTexture.MAGIC_NUM, timData, sizeof(unsigned int));
if (timTexture.MAGIC_NUM != 0x10) {
std::cout << "TIM INVALIDO !" << std::endl;
return;
}
memcpy(&timTexture.TIM_BPP, timData+4, sizeof(unsigned int));
memcpy(&timTexture.UNK_32, timData+8, sizeof(unsigned int));
memcpy(&timTexture.paletteOrgX, timData+12, sizeof(unsigned short));
memcpy(&timTexture.paletteOrgY, timData+14, sizeof(unsigned short));
memcpy(&timTexture.unk, timData+16, sizeof(unsigned short));
memcpy(&timTexture.numbCluts, timData+18, sizeof(unsigned short));
// Criamos uma ColorTable onde vai ficar todas as cores em formato RGB555(15bpp)
// Que é suportado pelo OpenGL
colorTable = (unsigned int **) malloc (sizeof(unsigned int) * timTexture.numbCluts);
unsigned short colorByte = 0;
// Pega o número de cluts, e aloca uma matriz com as cores
// Como as cores está no padrão do TIM 8bpp, temos que converter
// Nesse caso para 15bpp(TIM aqui é 8bpp)
for (unsigned int i = 0; i < timTexture.numbCluts; i++) {
colorTable[i] = (unsigned int *) malloc (sizeof (unsigned int) * 256);
for (unsigned int a = 0; a < 256; a++) {
memcpy(&colorByte, (timData+20+(a*sizeof(unsigned short))+ (512 * i)), sizeof(unsigned short));
unsigned char r = ((colorByte >> 10) & 0x1F);
unsigned char g = ((colorByte >> 5) & 0x1F);
unsigned char b = ((colorByte) & 0x1F);
r = ((r << 3) | (r >> 2));
g = ((g << 3) | (g >> 2));
b = ((b << 3) | (b >> 2));
colorTable[i][a] = (r << 16) | (g << 8) | (b);
}
}
// Desloca para o último byte após o array e começa a ler o resto do header
unsigned short offsetRel = (20+timTexture.numbCluts*512);
memcpy(&timTexture.UNK_32_2, timData+offsetRel, sizeof(unsigned int));
memcpy(&timTexture.imageOrgX, timData+offsetRel+4, sizeof(unsigned short));
memcpy(&timTexture.imageOrgY, timData+offsetRel+6, sizeof(unsigned short));
memcpy(&timTexture.imageWidth,timData+offsetRel+8, sizeof(unsigned short));
memcpy(&timTexture.imageHeight, timData+offsetRel+10, sizeof(unsigned short));
rawTexture = (unsigned char*) malloc (timTexture.imageHeight * (timTexture.imageWidth * 2) * 3);
unsigned int offset = 0;
unsigned char yOffset = 0;
unsigned int color = 0;
for (unsigned short y = 0; y < timTexture.imageHeight; y++) {
for(unsigned short x = 0; x < (timTexture.imageWidth*2); x++) {
memcpy(&yOffset,(timData+offsetRel+12+x+(y*timTexture.imageWidth*2)), sizeof(unsigned char));
color = colorTable[x / 128][yOffset];
rawTexture[offset+2] = ((color & 0x00FF0000) >> 16);
rawTexture[offset+1] = ((color & 0x0000FF00) >> 8);
rawTexture[offset] = (color & 0x000000FF);
offset+=3;
}
}
}
// Leitura de arquivo .TIM, o nome do arquivo será passado
// por um std::string
void TIM::timLoad(std::string imgName) {
// Inicializa variavel de arquivos
FILE *timFile = NULL;
// Abre o arquivo .tim
timFile = fopen(imgName.c_str(), "rb");
// Verifica se o arquivo foi realmente aberto
if (timFile == NULL) {
std::cout << "TIM File" << imgName << " não encontrado !" << std::endl;
}
// Pula para o final do arquivo, pega o tamanho, reorganiza ele
fseek(timFile, 0, SEEK_END);
timSize = ftell(timFile);
rewind(timFile);
// Faz alocação dinâmica com o tamanho do arquivo
timData = (unsigned char*) malloc (8 * timSize);
// Leitura de todos os dados para o timData, FILE é inútil a partir de agora
fread(timData, timSize, 1, timFile);
fclose(timFile);
// Leitura do Header até numbCluts, numbCluts é o tamanho do
// clutData que valos alocar dinamicamente
memcpy(&timTexture.MAGIC_NUM, timData, sizeof(unsigned int));
if (timTexture.MAGIC_NUM != 0x10) {
std::cout << "TIM INVALIDO !" << std::endl;
return;
}
memcpy(&timTexture.TIM_BPP, timData+4, sizeof(unsigned int));
memcpy(&timTexture.UNK_32, timData+8, sizeof(unsigned int));
memcpy(&timTexture.paletteOrgX, timData+12, sizeof(unsigned short));
memcpy(&timTexture.paletteOrgY, timData+14, sizeof(unsigned short));
memcpy(&timTexture.unk, timData+16, sizeof(unsigned short));
memcpy(&timTexture.numbCluts, timData+18, sizeof(unsigned short));
// Criamos uma ColorTable onde vai ficar todas as cores em formato RGB555(15bpp)
// Que é suportado pelo OpenGL
colorTable = (unsigned int **) malloc (sizeof(unsigned int) * timTexture.numbCluts);
unsigned short colorByte = 0;
// Pega o número de cluts, e aloca uma matriz com as cores
// Como as cores está no padrão do TIM 8bpp, temos que converter
// Nesse caso para 15bpp(TIM aqui é 8bpp)
for (unsigned int i = 0; i < timTexture.numbCluts; i++) {
colorTable[i] = (unsigned int *) malloc (sizeof (unsigned int) * 256);
for (unsigned int a = 0; a < 256; a++) {
memcpy(&colorByte, (timData+20+(a*sizeof(unsigned short))+ (512 * i)), sizeof(unsigned short));
unsigned char r = ((colorByte >> 10) & 0x1F);
unsigned char g = ((colorByte >> 5) & 0x1F);
unsigned char b = ((colorByte) & 0x1F);
r = ((r << 3) | (r >> 2));
g = ((g << 3) | (g >> 2));
b = ((b << 3) | (b >> 2));
colorTable[i][a] = (r << 16) | (g << 8) | (b);
}
}
// Desloca para o último byte após o array e começa a ler o resto do header
unsigned short offsetRel = (20+timTexture.numbCluts*512);
memcpy(&timTexture.UNK_32_2, timData+offsetRel, sizeof(unsigned int));
memcpy(&timTexture.imageOrgX, timData+offsetRel+4, sizeof(unsigned short));
memcpy(&timTexture.imageOrgY, timData+offsetRel+6, sizeof(unsigned short));
memcpy(&timTexture.imageWidth,timData+offsetRel+8, sizeof(unsigned short));
memcpy(&timTexture.imageHeight, timData+offsetRel+10, sizeof(unsigned short));
rawTexture = (unsigned char*) malloc (timTexture.imageHeight * (timTexture.imageWidth * 2) * 3);
unsigned int offset = 0;
unsigned char yOffset = 0;
unsigned int color = 0;
for (unsigned short y = 0; y < timTexture.imageHeight; y++) {
for(unsigned short x = 0; x < (timTexture.imageWidth*2); x++) {
memcpy(&yOffset,(timData+offsetRel+12+x+(y*timTexture.imageWidth*2)), sizeof(unsigned char));
color = colorTable[x / 128][yOffset];
rawTexture[offset+2] = ((color & 0x00FF0000) >> 16);
rawTexture[offset+1] = ((color & 0x0000FF00) >> 8);
rawTexture[offset] = (color & 0x000000FF);
offset+=3;
}
}
}