-
Notifications
You must be signed in to change notification settings - Fork 6
/
tilecache.c
330 lines (280 loc) · 10.4 KB
/
tilecache.c
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
/*
* QTC: tilecache.c (c) 2011, 2012 50m30n3
*
* This file is part of QTC.
*
* QTC 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.
*
* QTC 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 QTC. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "tilecache.h"
const int indexsize = 0x10000;
/*******************************************************************************
* Function to compute the fletcher16 checksum of a block of data *
* *
* data is the data the checksum should be computed of *
* length is the length aof the data in bytes *
* *
* Returns the fletcher16 checksum of the data *
*******************************************************************************/
static unsigned short int fletcher16( unsigned char *data, int length )
{
unsigned char s1, s2;
int i;
s1 = s2 = 0;
for( i=0; i<length; i++ )
{
s1 += data[i];
s2 += s1;
}
return (s2<<8)|s1;
}
/*******************************************************************************
* Function to create a new tile cache *
* *
* size is the number of tiles to cache *
* blocksize is the width and height of the quadratic tiles *
* *
* Returns a new tile cache or NULL on failure *
*******************************************************************************/
struct tilecache *tilecache_create( int size, int blocksize )
{
struct tilecache *cache;
int i;
cache = malloc( sizeof( *cache ) );
if( cache == NULL )
{
perror( "tilecache_create: malloc" );
return NULL;
}
cache->size = size;
cache->blocksize = blocksize;
cache->tilesize = sizeof( *cache->data )*blocksize*blocksize;
if( size <= 0x1<<16 )
cache->indexbits = 16;
else if( size <= 0x1<<24 )
cache->indexbits = 24;
else
cache->indexbits = 32;
cache->index = 0;
cache->numblocks = 0;
cache->hits = 0;
cache->tiles = malloc( sizeof( *cache->tiles ) * size );
if( cache->tiles == NULL )
{
perror( "tilecache_create: malloc" );
return NULL;
}
cache->tileindex = malloc( sizeof( *cache->tileindex ) * indexsize );
if( cache->tileindex == NULL )
{
perror( "tilecache_create: malloc" );
return NULL;
}
cache->data = malloc( sizeof( *cache->data ) * size * blocksize * blocksize );
if( cache->data == NULL )
{
perror( "tilecache_create: malloc" );
return NULL;
}
cache->tempdata = malloc( sizeof( *cache->tempdata ) * blocksize * blocksize );
if( cache->tempdata == NULL )
{
perror( "tilecache_create: malloc" );
return NULL;
}
for( i=0; i<size; i++ )
{
cache->tiles[i].present = 0;
cache->tiles[i].next = -1;
cache->tiles[i].data = &cache->data[i*blocksize*blocksize];
}
for( i=0; i<indexsize; i++ )
cache->tileindex[i] = -1;
return cache;
}
/*******************************************************************************
* Function to free a tile buffer structure *
* *
* cache is the tile cache to free *
* *
* Modifies tile cache *
*******************************************************************************/
void tilecache_free( struct tilecache *cache )
{
free( cache->tiles );
free( cache->tileindex );
free( cache->data );
free( cache->tempdata );
free( cache );
}
/*******************************************************************************
* Function to reset a tile buffer to its initial state *
* *
* cache is the tile cache to be reset *
* *
* Modifies tile cache *
*******************************************************************************/
void tilecache_reset( struct tilecache *cache )
{
int i;
cache->index = 0;
for( i=0; i<cache->size; i++ )
{
cache->tiles[i].present = 0;
cache->tiles[i].next = -1;
}
for( i=0; i<indexsize; i++ )
cache->tileindex[i] = -1;
}
/*******************************************************************************
* Function to write a new tile to the cache *
* *
* cache is the tile cache to use *
* pixels is a pointer to the pixel array containing the tile *
* x1, x2, y1, y2 describe the tile position *
* with is the width of the complete image *
* mask is the channel mask used during write *
* *
* Modifies tile cache, returns index of tile if already cached, -1 otherwise *
*******************************************************************************/
int tilecache_write( struct tilecache *cache, unsigned int *pixels, int x1, int x2, int y1, int y2, int width, unsigned int mask )
{
int size;
int x, y, i, j;
int found;
unsigned short int hash;
cache->numblocks++;
memset( (unsigned char *)cache->tempdata, 0, cache->tilesize );
j = 0;
for( y=y1; y<y2; y++ )
{
i = x1 + y*width;
for( x=x1; x<x2; x++ )
cache->tempdata[j++] = pixels[i++]&mask;
}
size = (x2-x1)*(y2-y1);
hash = fletcher16( (unsigned char *)cache->tempdata, cache->tilesize );
i = cache->tileindex[hash];
found = 0;
while( ( i != -1 ) && ( ! found ) )
{
if( ( cache->tiles[i].size == size ) && ( memcmp( cache->tempdata, cache->tiles[i].data, cache->tilesize ) == 0 ) )
{
found = 1;
cache->hits++;
}
else
{
i = cache->tiles[i].next;
}
}
if( ! found )
{
cache->index++;
cache->index %= cache->size;
if( cache->tiles[cache->index].present )
{
i = cache->tileindex[cache->tiles[cache->index].hash];
if( i == cache->index )
{
cache->tileindex[cache->tiles[cache->index].hash] = cache->tiles[i].next;
}
else
{
j = i;
i = cache->tiles[i].next;
while( i != -1 )
{
if( i == cache->index )
{
cache->tiles[j].next = cache->tiles[i].next;
break;
}
j = i;
i = cache->tiles[i].next;
}
}
}
cache->tiles[cache->index].present = 1;
cache->tiles[cache->index].size = size;
cache->tiles[cache->index].hash = hash;
cache->tiles[cache->index].next = cache->tileindex[hash];
cache->tileindex[hash] = cache->index;
memcpy( cache->tiles[cache->index].data, cache->tempdata, cache->tilesize );
return -1;
}
else
{
return i;
}
}
/*******************************************************************************
* Function to read a tile from a tile cache *
* *
* cache is the tile cache to use *
* pixels is a pointer to the pixel array the tile should be written to *
* index is the index of the tile to retreive *
* x1, x2, y1, y2 describe the tile position *
* with is the width of the complete image *
* mask is the channel mask used during write *
* *
* Modifies tile pixels *
*******************************************************************************/
void tilecache_read( struct tilecache *cache, unsigned int *pixels, int index, int x1, int x2, int y1, int y2, int width, unsigned int mask )
{
int x, y, i, j;
unsigned int invmask;
unsigned int *data;
invmask = ~mask;
data = cache->tiles[index].data;
j = 0;
for( y=y1; y<y2; y++ )
{
i = x1 + y*width;
for( x=x1; x<x2; x++ )
{
pixels[i] &= invmask;
pixels[i++] |= data[j++]&mask;
}
}
}
/*******************************************************************************
* Function to add a tile to a tile cache *
* *
* cache is the tile cache to use *
* pixels is a pointer to the pixel array containing the tile *
* x1, x2, y1, y2 describe the tile position *
* with is the width of the complete image *
* mask is the channel mask used during write *
* *
* Modifies tile cache *
*******************************************************************************/
void tilecache_add( struct tilecache *cache, unsigned int *pixels, int x1, int x2, int y1, int y2, int width, unsigned int mask )
{
int x, y, i, j;
cache->numblocks++;
memset( (unsigned char *)cache->tempdata, 0, cache->tilesize );
j = 0;
for( y=y1; y<y2; y++ )
{
i = x1 + y*width;
for( x=x1; x<x2; x++ )
cache->tempdata[j++] = pixels[i++]&mask;
}
cache->index++;
cache->index %= cache->size;
memcpy( cache->tiles[cache->index].data, cache->tempdata, cache->tilesize );
}