10 static int G3d_readIndex(G3D_Map * map)
12 unsigned char *tmp, *tmp2;
13 int dummy1, dummy2, indexLength, tileIndex;
16 indexLast = lseek(map->data_fd, (
long)0, SEEK_END);
17 if (indexLast == -1) {
18 G3d_error(
"G3d_readIndex: can't position file");
22 indexLength = indexLast - map->indexOffset;
24 if (lseek(map->data_fd, map->indexOffset, SEEK_SET) == -1) {
25 G3d_error(
"G3d_readIndex: can't position file");
29 tmp =
G3d_malloc(map->indexLongNbytes * map->nTiles);
32 G3d_error(
"G3d_readIndex: error in G3d_malloc");
36 if (indexLength < map->indexLongNbytes * map->nTiles) {
38 if (indexLength >
sizeof(
long) * map->nTiles) {
43 G3d_error(
"G3d_readIndex: error in G3d_malloc");
48 tmp2 = (
unsigned char *)map->index;
50 if (read(map->data_fd, tmp2, indexLength) != indexLength) {
51 G3d_error(
"G3d_readIndex: can't read file");
55 G_rle_decode(tmp2, tmp, map->indexLongNbytes * map->nTiles, 1,
58 if (indexLength >
sizeof(
long) * map->nTiles)
61 else if (read(map->data_fd, tmp, indexLength) != indexLength) {
62 G3d_error(
"G3d_readIndex: can't read file");
66 G3d_longDecode(tmp, map->index, map->nTiles, map->indexLongNbytes);
68 for (tileIndex = 0; tileIndex < map->nTiles; tileIndex++)
69 if (map->index[tileIndex] == 0)
70 map->index[tileIndex] = -1;
81 int sizeCompressed, indexLength, tileIndex;
88 map->indexOffset = lseek(map->data_fd, (
long)0, SEEK_END);
89 if (map->indexOffset == -1) {
90 G3d_error(
"G3d_flushIndex: can't rewind file");
95 (
unsigned char *)&ldummy, 1);
99 G3d_error(
"G3d_flushIndex: error in G3d_malloc");
103 for (tileIndex = 0; tileIndex < map->nTiles; tileIndex++)
104 if (map->index[tileIndex] == -1)
105 map->index[tileIndex] = 0;
111 if (sizeCompressed >= map->nTiles *
sizeof(
long)) {
112 indexLength = map->nTiles *
sizeof(long);
113 if (write(map->data_fd, tmp, indexLength) != indexLength) {
114 G3d_error(
"G3d_flushIndex: can't write file");
119 indexLength = sizeCompressed;
120 G_rle_encode(tmp, (
char *)map->index,
sizeof(
long) * map->nTiles, 1);
121 if (write(map->data_fd, map->index, sizeCompressed) != sizeCompressed) {
122 G3d_error(
"G3d_flushIndex: can't write file");
128 if (!G3d_readIndex(map)) {
129 G3d_error(
"G3d_flushIndex: error in G3d_readIndex");
138 static long *cmpIndex;
140 static int indexSortCompare(
const void *a,
const void *
b)
142 long offset1, offset2;
144 offset1 = cmpIndex[*((
const int *)a)];
145 offset2 = cmpIndex[*((
const int *)
b)];
147 if (offset1 > offset2)
149 if (offset1 < offset2)
159 int i0, i1, i2, i3, i4, i5, offset, nofElts;
162 map->hasIndex = hasIndex;
163 map->index =
G3d_malloc(
sizeof(
long) * map->nTiles);
164 map->tileLength =
G3d_malloc(
sizeof(
int) * map->nTiles);
166 if ((map->index ==
NULL) || (map->tileLength ==
NULL)) {
167 G3d_error(
"G3d_initIndex: error in G3d_malloc");
172 for (tile = 0; tile < map->nTiles; tile++)
173 map->index[tile] = -1;
177 if (!map->hasIndex) {
179 for (tile = 0; tile < map->nTiles; tile++) {
180 map->index[
tile] = offset * map->numLengthExtern + map->offset;
182 (map, tile, &i0, &i1, &i2, &i3, &i4, &i5);
183 map->tileLength[
tile] = nofElts * map->numLengthExtern;
189 if (!G3d_readIndex(map)) {
190 G3d_error(
"G3d_initIndex: error in G3d_readIndex");
194 offsetP =
G3d_malloc(
sizeof(
int) * map->nTiles);
195 if (offsetP ==
NULL) {
196 G3d_error(
"G3d_initIndex: error in G3d_malloc");
200 for (tile = 0; tile < map->nTiles; tile++)
201 offsetP[tile] = tile;
202 cmpIndex = map->index;
203 qsort(offsetP, map->nTiles,
sizeof(
int), indexSortCompare);
205 for (tile = 0; tile < map->nTiles - 1; tile++) {
206 if (map->index[offsetP[tile]] == -1) {
207 map->tileLength[offsetP[
tile]] = 0;
211 map->tileLength[offsetP[
tile]] = map->index[offsetP[tile + 1]] -
212 map->index[offsetP[
tile]];
215 if (map->index[offsetP[map->nTiles - 1]] == -1)
216 map->tileLength[offsetP[map->nTiles - 1]] = 0;
218 map->tileLength[offsetP[map->nTiles - 1]] =
219 map->indexOffset - map->index[offsetP[map->nTiles - 1]];
void G3d_free(void *buf)
Same as free (ptr).
void G3d_error(const char *msg,...)
int G_rle_count_only(char *src, int nofElts, int eltLength)
int G3d_initIndex(G3D_Map *map, int hasIndex)
void G_rle_encode(char *src, char *dst, int nofElts, int eltLength)
int G3d_longEncode(long *source, unsigned char *dst, int nofNums)
int G3d_computeClippedTileDimensions(G3D_Map *map, int tileIndex, int *rows, int *cols, int *depths, int *xRedundant, int *yRedundant, int *zRedundant)
Computes the dimensions of the tile when clipped to fit the region of map. The clipped dimensions are...
int G3d_flushIndex(G3D_Map *map)
void * G3d_malloc(int nBytes)
Same as malloc (nBytes), except that in case of error G3d_error() is invoked.
void G_rle_decode(char *src, char *dst, int nofElts, int eltLength, int *lengthEncode, int *lengthDecode)
void G3d_longDecode(unsigned char *source, long *dst, int nofNums, int longNbytes)