-
Notifications
You must be signed in to change notification settings - Fork 1
/
slicer.c
123 lines (120 loc) · 3.05 KB
/
slicer.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "pnmread.c"
struct _tile
{
unsigned char tilepix[16 * 16 / 2];
struct _tile *next;
int index;
} *hashlist[256];
int hashcount[256];
int tilecount = 0;
int addtile(FILE *tilefile, unsigned char *mapptr)
{
struct _tile newtile, *matchtile;
unsigned char hash;
int row, col, tileindex;
hash = 0;
for (row = 0; row < 16; row++)
{
for (col = 0; col < 8; col++)
{
hash += mapptr[col];
newtile.tilepix[row * 8 + col] = mapptr[col];
}
mapptr += mapwidth / 2;
}
matchtile = hashlist[hash];
while (matchtile)
{
if (memcmp(newtile.tilepix, matchtile->tilepix, 16 * 8) == 0)
break;
matchtile = matchtile->next;
}
if (!matchtile)
{
newtile.index = tileindex = tilecount++;
newtile.next = hashlist[hash];
hashlist[hash] = malloc(sizeof(struct _tile));
memcpy(hashlist[hash], &newtile, sizeof(struct _tile));
fwrite(newtile.tilepix, 16, 8, tilefile);
hashcount[hash]++;
}
else
tileindex = matchtile->index;
return tileindex;
}
int main(int argc, char **argv)
{
FILE *tilefile, *mapfile;
int x, y, tilenum;
unsigned char dither, stats, mapnums[4];
float gammafunc;
char *basename, pnmname[80], tilename[80], mapname[80];
gammafunc = 1.0;
dither = 1;
stats = 0;
while (argc > 1 && argv[1][0] == '-')
{
if (argc > 2 && argv[1][1] == 'g')
{
gammafunc = atof(argv[2]);
argc--;
argv++;
}
else if (argv[1][1] == 'n')
{
dither = 0;
}
else if (argv[1][1] == 's')
{
stats = 1;
}
argc--;
argv++;
}
if (argc > 1)
{
basename = argv[1];
sprintf(pnmname, "%s.pnm", basename);
if (!readimage(pnmname, dither, gammafunc))
return -1;
}
else
{
fprintf(stderr, "Missing basename\n");
return -1;
}
sprintf(tilename, "%s.set", basename);
sprintf(mapname, "%s.map", basename);
for (tilenum = 0; tilenum < 256; tilenum++)
{
hashlist[tilenum] = NULL;
hashcount[tilenum] = 0;
}
tilefile = fopen(tilename, "wb");
mapfile = fopen(mapname, "wb");
mapnums[0] = (mapwidth / 16);
mapnums[1] = (mapwidth / 16) >> 8;
mapnums[2] = (mapheight / 16);
mapnums[3] = (mapheight / 16) >> 8;
fwrite(mapnums, 1, 4, mapfile);
for (y = 0; y < mapheight; y += 16)
for (x = 0; x < mapwidth; x += 16)
{
tilenum = addtile(tilefile, pixmap + (y * mapwidth + x) / 2);
mapnums[0] = tilenum;
mapnums[1] = tilenum >> 8;
fwrite(mapnums, 1, 2, mapfile);
}
fclose(tilefile);
fclose(mapfile);
if (stats)
{
for (tilenum = 0; tilenum < 256; tilenum++)
printf("Hash count[%d]: %d\n", tilenum ,hashcount[tilenum]);
printf("Tile count: %d\n", tilecount);
}
}