forked from tizbac/SpringMapConvNG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TileStorage.cpp
254 lines (215 loc) · 6.18 KB
/
TileStorage.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
/*
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) 2011 Tiziano <email>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TileStorage.h"
#include <iostream>
#include <cstring>
#include <math.h>
#include <IL/il.h>
#include <IL/ilu.h>
#include <sstream>
#include <stdlib.h>
inline float tilediff(uint8_t * t1,uint8_t * t2)
{
float diff = 0.0;
for ( int i = 0; i < 32*32*4; i++ )
{
float d1 = fabs(float(t1[i])-float(t2[i]));
if ( d1 < 30 )
diff += d1;
else
diff += 255.0f;//If it has a point that is VERY different , it must not be reused
}
diff /= 32.0*32.0*4.0f*10.0f;
return diff;
}
TileStorage::TileStorage()
{
Reset();
m_dictcount = 64;
}
TileStorage::~TileStorage()
{
Reset();
}
void TileStorage::Reset()
{
for ( std::map<uint64_t,uint8_t*>::iterator it = m_tiles.begin(); it != m_tiles.end(); it++ )
{
if ( m_tiles_compressed.find((*it).first) != m_tiles_compressed.end() )
{
delete m_tiles_compressed[(*it).first];
}
delete (*it).second;
}
m_tiles.clear();
m_lasttiles.clear();
m_tiles_compressed.clear();
}
uint64_t TileStorage::AddTile(uint8_t* data)
{
uint64_t checksum = tilechecksum(data);
if ( m_tiles.find(checksum) != m_tiles.end() )
{
std::cerr << "Duplicate tile detected, dropping!" << std::endl;
return checksum;
}
uint8_t* data_copy = new uint8_t[32*32*4];
memcpy(data_copy,data,32*32*4);
m_tiles.insert(std::pair<uint64_t,uint8_t*>(checksum,data_copy));
m_lasttiles.push_back(checksum);
if ( m_lasttiles.size() > m_dictcount )
m_lasttiles.pop_front();
return checksum;
}
void TileStorage::CompressAll()
{
for ( std::map<uint64_t,uint8_t*>::iterator it = m_tiles.begin(); it != m_tiles.end(); it++ )
{
if ( m_tiles_compressed.find((*it).first) == m_tiles_compressed.end() )
{
CompressTile((*it).first);
}
}
}
void TileStorage::SetDictSize(uint32_t s)
{
m_dictcount = s;
}
void TileStorage::CompressTile(uint64_t uid)
{
uint8_t * m0;
uint8_t * m1;
uint8_t * m2;
uint8_t * m3;
uint8_t * dataptr = m_tiles[uid];
uint8_t * compressedmipmaps = new uint8_t[680];
if ( !dataptr )
{
delete [] compressedmipmaps;
throw InvalidTileDataPointerException();
}
uint32_t s;
uint32_t s2 = 0;
ILuint mip1 = ilGenImage();
ilBindImage(mip1);
ilTexImage(32,32,1,4,IL_RGBA,IL_UNSIGNED_BYTE,dataptr);
/*std::stringstream ss;
ss << "Tile" << uid << ".png";
ilSaveImage(ss.str().c_str());*/
m0 = ilCompressDXT(ilGetData(),32,32,1,IL_DXT1,&s);
memcpy(&compressedmipmaps[s2],m0,s);
s2 += s;
iluScale(16,16,1);
m1 = ilCompressDXT(ilGetData(),16,16,1,IL_DXT1,&s);
memcpy(&compressedmipmaps[s2],m1,s);
s2 += s;
iluScale(8,8,1);
m2 = ilCompressDXT(ilGetData(),8,8,1,IL_DXT1,&s);
memcpy(&compressedmipmaps[s2],m2,s);
s2 += s;
iluScale(4,4,1);
m3 = ilCompressDXT(ilGetData(),4,4,1,IL_DXT1,&s);
memcpy(&compressedmipmaps[s2],m3,s);
s2 += s;
ilDeleteImage(mip1);
/*squish::CompressImage(dataptr,32,32,m0,squish::kDxt1);
squish::CompressImage(dataptr,16,16,m1,squish::kDxt1);
squish::CompressImage(dataptr,8,8,m2,squish::kDxt1);
squish::CompressImage(dataptr,4,4,m3,squish::kDxt1);*/
free(m0);
free(m1);
free(m2);
free(m3);
m_tiles_compressed[uid] = compressedmipmaps;
//std::cout << "Tile " << uid << " compressed!" << std::endl;
}
void TileStorage::WriteToFile(FILE* f, std::vector< uint64_t >& tile_order)
{
char magic[16];
strcpy(magic,"spring tilefile");
int version = 1;
int numtiles = m_tiles_compressed.size();
int tileSize = 32;
int compressionType = 1;
fwrite(magic,16,1,f);
fwrite(&version,4,1,f);
fwrite(&numtiles,4,1,f);
fwrite(&tileSize,4,1,f);
fwrite(&compressionType,4,1,f);
for ( std::vector< uint64_t >::const_iterator it = tile_order.begin(); it != tile_order.end(); it++ )
{
if ( m_tiles_compressed.find(*it) == m_tiles_compressed.end() )
{
CompressAll();
}
if ( m_tiles_compressed.find(*it) == m_tiles_compressed.end() )
{
throw InvalidTileIndexException();
}
fwrite(m_tiles_compressed[*it],680,1,f);
}
fflush(f);
}
uint64_t TileStorage::AddTileOrGetSimiliar(uint8_t* data, float th, int compresslevel)
{
uint64_t checksum = tilechecksum(data);
if ( m_tiles.find(checksum) != m_tiles.end() )
{
//std::cout << "Debug(AddTileOrGetSimiliar): " << checksum << " already exists" << std::endl;
return checksum;
}
if ( compresslevel == COMPRESS_INSANE )
{
for ( std::map<uint64_t,uint8_t*>::iterator it = m_tiles.begin(); it != m_tiles.end(); it++ )
{
if ( tilediff(data,(*it).second) < th )
{
return (*it).first;
}
}
}else if ( compresslevel == COMPRESS_REASONABLE )
{
for ( std::list<uint64_t>::iterator it = m_lasttiles.begin(); it != m_lasttiles.end(); it++ )
{
if ( tilediff(data,m_tiles[(*it)]) < th )
{
return (*it);
}
}
}else if ( compresslevel == COMPRESS_SHITTY )
{
//do nothing...
}else if ( compresslevel == COMPRESS_REASONABLE_BESTQUALITY )
{
float mindiff = 9999999.0f;
uint64_t besttile;
for ( std::list<uint64_t>::iterator it = m_lasttiles.begin(); it != m_lasttiles.end(); it++ )
{
float diff = tilediff(data,m_tiles[(*it)]);
if ( diff < mindiff )
{
besttile = (*it);
mindiff = diff;
}
}
if ( mindiff <= th )
return besttile;
}
return AddTile(data);
}
uint32_t TileStorage::GetTileCount()
{
return std::max(m_tiles_compressed.size(),m_tiles.size());
}