-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeRezIcon.c
237 lines (181 loc) · 4.64 KB
/
DeRezIcon.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
#pragma lint -1
#pragma optimize -1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gsos.h>
#include <resources.h>
#include <memory.h>
/*
* This will DeRez icons and cursors from a resource file.
*
* Essentially "DeRez file Types.Rez" but with prettier output.
*
*/
typedef struct IconHeader {
Word iconType; // bit 15 = color (1) or b/w (0)
Word iconSize; // number of bytes
Word iconHeight; // height, in pixels
Word iconWidth; // width, in pixels.
// Byte iconImage[iconSize];
// Byte iconMask[iconSize];
} IconHeader;
typedef struct CursorHeader {
Word cursorHeight; // height, in pixels
Word cursorWidth; // width, in pixels.
// Byte cursorImage[height*width]
// Byte cursorMask[height*width]
//
} CursorHeader;
typedef struct CursorTrailer {
Word cursorY; // hotspot Y
Word cursorX; // hotspot X.
Word cursorFlags; // cursor ID
// 8 bytes filler
} CursorTrailer;
GSString255Ptr c2gs(const char *cp) {
GSString255Ptr gs;
int l = strlen(cp);
gs = malloc(l + 3);
if (gs) {
gs->length = l;
strcpy(gs->text, cp);
}
return gs;
}
void dump(const unsigned char *ptr, unsigned count) {
static char hex[] = "0123456789abcdef";
unsigned i,x;
fputs(" $\"", stdout);
for (i = 0; i < count; ++i) {
x = ptr[i];
fputc(hex[x >> 4], stdout);
fputc(hex[x & 0x0f], stdout);
}
fputs("\"\n", stdout);
}
void one_icon(ResID id, ResAttr attr, unsigned char *ptr, unsigned long length) {
IconHeader *header;
unsigned hh;
unsigned ww;
unsigned i;
header = (IconHeader *)ptr;
hh = header->iconHeight;
ww = header->iconWidth >> 1;
printf("resource rIcon(%lu, $%04x) {\n", id, attr);
printf(" $%04x, // type (%s)\n", header->iconType, header->iconType & 0x8000 ? "Color" : "B/W");
printf(" %u, // height\n", header->iconHeight);
printf(" %u, // width\n\n", header->iconWidth);
ptr += sizeof(IconHeader);
// print the icon
for (i = 0; i < hh; ++i) {
dump(ptr, ww);
ptr += ww;
}
printf(" ,\n");
// print the mask
for (i = 0; i < hh; ++i) {
dump(ptr, ww);
ptr += ww;
}
printf("};\n\n");
}
void one_cursor(ResID id, ResAttr attr, unsigned char *ptr, unsigned long length) {
CursorHeader *header;
CursorTrailer *trailer;
unsigned hh;
unsigned ww;
unsigned i;
header = (CursorHeader *)ptr;
hh = header->cursorHeight;
ww = (header->cursorWidth ) << 1;
printf("resource rCursor(%lu, $%04x) {\n", id, attr);
printf(" %u, // height\n", header->cursorHeight);
printf(" %u, // width (%d bytes)\n\n", header->cursorWidth, ww << 1);
ptr += sizeof(CursorHeader);
// print the cursor
for (i = 0; i < hh; ++i) {
dump(ptr, ww);
ptr += ww;
}
printf(" ,\n");
// print the mask
for (i = 0; i < hh; ++i) {
dump(ptr, ww);
ptr += ww;
}
printf(" ,\n");
trailer = (CursorTrailer *)ptr;
printf(" %u, // hotspot Y\n", trailer->cursorY);
printf(" %u, // hotspot X\n\n", trailer->cursorX);
printf(" $%02x // flags (%d mode)\n", trailer->cursorFlags, trailer->cursorFlags & 0x80 ? 640 : 320);
printf("};\n\n");
}
void one_file(const char *name) {
GSString255Ptr gname;
unsigned rfd;
unsigned depth;
unsigned long ri;
gname = c2gs(name);
rfd = OpenResourceFile(0x8000 | readEnable, NULL, (Pointer)gname);
if (_toolErr) {
fprintf(stderr, "%s: OpenResourceFile: $%04x\n", name, _toolErr);
free(gname);
return;
}
depth = SetResourceFileDepth(1);
// icons
for (ri = 1; ; ++ri) {
Handle h;
ResAttr attr;
ResID id = GetIndResource(rIcon, ri);
if (_toolErr == resIndexRange) break;
if (_toolErr) {
fprintf(stderr, "%s: GetIndResource: $%04x\n", name, _toolErr);
continue;
}
attr = GetResourceAttr(rIcon, id);
h = LoadResource(rIcon, id);
if (_toolErr) {
fprintf(stderr, "%s: LoadResource: $%04x\n", name, _toolErr);
continue;
}
HLock(h);
one_icon(id, attr, *(char **)h, GetHandleSize(h));
HUnlock(h);
ReleaseResource(-1, rIcon, id);
}
// cursors
for (ri = 1; ; ++ri) {
Handle h;
ResAttr attr;
ResID id = GetIndResource(rCursor, ri);
if (_toolErr == resIndexRange) break;
if (_toolErr) {
fprintf(stderr, "%s: GetIndResource: $%04x\n", name, _toolErr);
continue;
}
attr = GetResourceAttr(rCursor, id);
h = LoadResource(rCursor, id);
if (_toolErr) {
fprintf(stderr, "%s: LoadResource: $%04x\n", name, _toolErr);
continue;
}
HLock(h);
one_cursor(id, attr, *(char **)h, GetHandleSize(h));
HUnlock(h);
ReleaseResource(-1, rCursor, id);
}
SetResourceFileDepth(depth);
CloseResourceFile(rfd);
free(gname);
}
int main(int argc, char **argv) {
int i;
ResourceStartUp(MMStartUp());
for (i = 1; i < argc; ++i) {
one_file(argv[i]);
}
ResourceShutDown();
exit(0);
}