-
Notifications
You must be signed in to change notification settings - Fork 0
/
exanim.c
235 lines (215 loc) · 4.98 KB
/
exanim.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
long read_long (FILE *fp);
void write_long (FILE *fp, unsigned long x);
long file_length (char *path);
void write_short (FILE *fp, unsigned short x);
void write_bmp (char *fname, int width, int height, unsigned char *pal,
char *data,
int red, int green, int blue, int mult);
struct tab_entry
{
long offset;
long anim;
int width;
int height;
int xoff;
int yoff;
long myst;
};
struct tab_entry read_tab(FILE *fp);
int main (int argc, char **argv)
{
char *buffer;
int picnum=0;
int animnum=0;
int r, c, ni, i;
long curanim;
long animwidth=0;
long animheight=0;
long animstart=0;
FILE *tabfp;
FILE *datfp;
FILE *palfp;
FILE *animfp=NULL;
char fname[50];
char palette[768];
char g;
int nanims;
struct tab_entry tab;
animfp = fopen ("anims.txt", "wb");
fclose (animfp);
tabfp = fopen ("creature.tab", "rb");
if (!tabfp)
{
printf ("Can't open creature.tab.\n");
return 1;
}
datfp = fopen ("creature.jty", "rb");
if (!datfp)
{
printf ("Can't open creature.jty.\n");
return 1;
}
palfp = fopen ("main.pal", "rb");
if (!palfp)
{
printf ("Can't open main.pal.\n");
return 1;
}
fread (palette, 768, 1, palfp);
while (picnum < 9137)
{
fprintf (animfp, "anim%d.gif: ", animnum++);
/* Find out the size of the animation */
animstart = ftell (tabfp);
tab = read_tab (tabfp);
nanims=1;
curanim=tab.anim;
animwidth=tab.width+tab.xoff;
animheight=tab.height+tab.yoff;
tab = read_tab (tabfp);
while (tab.anim == curanim)
{
nanims++;
if (tab.width+tab.xoff > animwidth)
animwidth=tab.width+tab.xoff;
if (tab.height+tab.yoff > animheight)
animheight=tab.height+tab.yoff;
tab = read_tab (tabfp);
}
fseek (tabfp, animstart, SEEK_SET);
for (ni=0; ni < nanims; ni++)
{
tab = read_tab (tabfp);
sprintf (fname, "pic%d.bmp ", picnum);
fprintf (animfp, "%s ", fname);
printf ("\rWorking... frame number %d", picnum);
sprintf (fname, "pic%d.bmp", picnum++);
fflush (stdout);
buffer = malloc (animwidth*animheight);
for (i=0; i < animwidth*animheight; i++)
buffer[i]=0;
r=tab.yoff;
c=tab.xoff;
fseek (datfp, tab.offset, SEEK_SET);
while (r-tab.yoff < tab.height)
{
g = (char) (fgetc (datfp)&255);
if (g < 0)
c-=g;
else if (!g)
{
c=tab.xoff;
r++;
}
else
{
for (i=0; i < g; i++)
buffer[(animwidth*r)+(c++)]=fgetc (datfp);
}
}
write_bmp (fname, animwidth, animheight,
palette, buffer, 0, 1, 2, 4);
free (buffer);
}
fprintf (animfp, "\n");
}
return 0;
}
void write_bmp (char *fname, int width, int height,
unsigned char *pal, char *data,
int red, int green, int blue, int mult)
{
long l;
int i, j;
FILE *out;
out = fopen (fname, "wb");
if (!out)
{
printf ("\nCan't open file %s. Aborting.\n", fname);
exit (1);
}
l = width*height;
fprintf (out, "BM");
write_long (out, l+0x436);
write_long (out, 0);
write_long (out, 0x436);
write_long (out, 40);
write_long (out, width);
write_long (out, height);
write_short (out, 1);
write_short (out, 8);
write_long (out, 0);
write_long (out, 0);
write_long (out, 0);
write_long (out, 0);
write_long (out, 0);
write_long (out, 0);
for (i=0; i < 256; i++)
{
fputc (pal[i*3+blue]*mult, out);
fputc (pal[i*3+green]*mult, out);
fputc (pal[i*3+red]*mult, out);
fputc (0, out);
}
for (i=1; i <= height; i++)
{
fwrite (data+(height-i)*width, width, 1, out);
if (width & 3)
for (j=0; j < 4-(width&3); j++)
fputc (0, out);
}
fclose (out);
}
void write_short (FILE *fp, unsigned short x)
{
fputc ((int) (x&255), fp);
fputc ((int) ((x>>8)&255), fp);
}
void write_long (FILE *fp, unsigned long x)
{
fputc ((int) (x&255), fp);
fputc ((int) ((x>>8)&255), fp);
fputc ((int) ((x>>16)&255), fp);
fputc ((int) ((x>>24)&255), fp);
}
long read_long (FILE *fp)
{
long l;
l = fgetc (fp);
l += fgetc (fp)<<8;
l += fgetc (fp)<<16;
l += fgetc (fp)<<24;
return l;
}
long file_length (char *path)
{
FILE *fp;
long ret;
fp = fopen (path, "rb");
if (!fp)
return -1;
fseek (fp, 0, SEEK_END);
ret = ftell (fp);
fclose (fp);
return ret;
}
struct tab_entry read_tab(FILE *fp)
{
struct tab_entry ret;
if (feof (fp))
{
printf ("End of tab file!\n");
exit (1);
}
ret.offset = read_long (fp);
ret.width = fgetc (fp);
ret.height = fgetc (fp);
ret.anim = read_long (fp);
ret.xoff = fgetc (fp);
ret.yoff = fgetc (fp);
ret.myst = read_long (fp);
return ret;
}