]> iEval git - xfishtank.git/blob - pcfshtofish/pcfshtofish.c
Fix segfault
[xfishtank.git] / pcfshtofish / pcfshtofish.c
1 #include <stdio.h>
2
3 #define MAXCOLORS 256
4 #define DEFAULT_NAME "fish"
5
6 struct rgb_color {
7 unsigned int red, green, blue;
8 };
9
10
11 void
12 ReadFshColors(ifp, colrs)
13 FILE *ifp;
14 struct rgb_color *colrs;
15 {
16 int i;
17 int val, val2;
18
19 for (i = 0; i < 132; i++) {
20 val = fgetc(ifp);
21 }
22
23 for (i = 0; i < 16; i++) {
24 val = fgetc(ifp);
25 colrs[i].blue = val;
26 val = fgetc(ifp);
27 colrs[i].green = val;
28 val = fgetc(ifp);
29 colrs[i].red = val;
30 val = fgetc(ifp);
31 }
32 }
33
34
35 unsigned char *
36 ReadFshData1(ifp)
37 FILE *ifp;
38 {
39 unsigned char *data;
40 int i, j, indx;
41 int val, val2;
42
43 data = (unsigned char *) malloc(64 * 64);
44 for (j = 63; j >= 0; j--) {
45 indx = 64 * j;
46 for (i = 0; i < 32; i++) {
47 val = fgetc(ifp);
48 val2 = (val >> 4) & 0x0f;
49 data[indx] = (unsigned char) val2;
50 indx++;
51 val2 = val & 0x0f;
52 data[indx] = (unsigned char) val2;
53 indx++;
54 }
55 }
56 return (data);
57 }
58
59
60 unsigned char *
61 ReadFshData2(ifp)
62 FILE *ifp;
63 {
64 unsigned char *data;
65 int i, j, indx;
66 int val, val2;
67
68 for (i = 0; i < (16 * 32); i++) {
69 val = fgetc(ifp);
70 }
71
72 data = (unsigned char *) malloc(64 * 64);
73 for (j = 63; j >= 0; j--) {
74 indx = 64 * j;
75 for (i = 0; i < 32; i++) {
76 val = fgetc(ifp);
77 val2 = (val >> 4) & 0x0f;
78 data[indx] = (unsigned char) val2;
79 indx++;
80 val2 = val & 0x0f;
81 data[indx] = (unsigned char) val2;
82 indx++;
83 }
84 }
85 return (data);
86 }
87
88
89 struct rgb_color
90 *
91 MapColors(data1, colrs1, data2, colrs2, width, height, Cnt)
92 unsigned char *data1;
93 struct rgb_color *colrs1;
94 unsigned char *data2;
95 struct rgb_color *colrs2;
96 int width, height;
97 int *Cnt;
98 {
99 int i, j;
100 int fcnt, indx;
101 int Mapping[MAXCOLORS];
102 unsigned char *ptr;
103 struct rgb_color *fcolrs;
104
105 fcolrs = (struct rgb_color *) malloc((MAXCOLORS + 1) * sizeof(struct rgb_color));
106
107 fcnt = 0;
108 ptr = data1;
109 for (i = 0; i < (width * height); i++) {
110 indx = (int) *ptr;
111 for (j = 0; j < fcnt; j++) {
112 if ((fcolrs[j].red == colrs1[indx].red) &&
113 (fcolrs[j].green == colrs1[indx].green) &&
114 (fcolrs[j].blue == colrs1[indx].blue)) {
115 break;
116 }
117 }
118 if (j == fcnt) {
119 fcolrs[j].red = colrs1[indx].red;
120 fcolrs[j].green = colrs1[indx].green;
121 fcolrs[j].blue = colrs1[indx].blue;
122 Mapping[indx] = j;
123 fcnt++;
124 if (fcnt > MAXCOLORS) {
125 fprintf(stderr, "Error: cannot use more than %d colors in your fish\n",
126 MAXCOLORS);
127 exit(1);
128 }
129 }
130 ptr++;
131 }
132 ptr = data1;
133 for (i = 0; i < (width * height); i++) {
134 indx = (int) *ptr;
135 *ptr = (unsigned char) (Mapping[indx]);
136 ptr++;
137 }
138
139 ptr = data2;
140 for (i = 0; i < (width * height); i++) {
141 indx = (int) *ptr;
142 for (j = 0; j < fcnt; j++) {
143 if ((fcolrs[j].red == colrs2[indx].red) &&
144 (fcolrs[j].green == colrs2[indx].green) &&
145 (fcolrs[j].blue == colrs2[indx].blue)) {
146 break;
147 }
148 }
149 if (j == fcnt) {
150 fcolrs[j].red = colrs2[indx].red;
151 fcolrs[j].green = colrs2[indx].green;
152 fcolrs[j].blue = colrs2[indx].blue;
153 Mapping[indx] = j;
154 fcnt++;
155 if (fcnt > MAXCOLORS) {
156 fprintf(stderr, "Error: cannot use more than %d colors in your fish\n",
157 MAXCOLORS);
158 exit(1);
159 }
160 }
161 ptr++;
162 }
163 ptr = data2;
164 for (i = 0; i < (width * height); i++) {
165 indx = (int) *ptr;
166 *ptr = (unsigned char) (Mapping[indx]);
167 ptr++;
168 }
169
170 for (i = 0; i < fcnt; i++) {
171 fcolrs[i].red = fcolrs[i].red * 256;
172 fcolrs[i].green = fcolrs[i].green * 256;
173 fcolrs[i].blue = fcolrs[i].blue * 256;
174 }
175 for (i = fcnt; i < 16; i++) {
176 fcolrs[i].red = 0;
177 fcolrs[i].green = 0;
178 fcolrs[i].blue = 0;
179 }
180
181 if (fcnt < 16) {
182 fcnt = 16;
183 }
184 *Cnt = fcnt;
185 return (fcolrs);
186 }
187
188
189 main(argc, argv)
190 int argc;
191 char *argv[];
192 {
193 FILE *ifp;
194 FILE *ofd;
195 int i, j, cnt, fcnt;
196 struct rgb_color colrs1[MAXCOLORS];
197 struct rgb_color *fcolrs;
198 unsigned char *data1, *data2;
199 int width, height;
200 char outfile[256];
201 char *outname;
202
203 ifp = NULL;
204 if (argc > 1) {
205 ifp = fopen(argv[1], "r");
206 }
207 if (ifp == NULL) {
208 ifp = stdin;
209 }
210
211 width = 64;
212 height = 64;
213 ReadFshColors(ifp, colrs1);
214 data1 = ReadFshData1(ifp);
215 data2 = ReadFshData2(ifp);
216
217 fcolrs = MapColors(data1, colrs1, data2, colrs1, width, height, &fcnt);
218
219 if (argc > 2) {
220 outname = argv[2];
221 } else {
222 cnt = 0;
223 if (argc > 1) {
224 while ((argv[1][cnt] != '.') && (argv[1][cnt] != '\0')) {
225 cnt++;
226 }
227 }
228 if (cnt == 0) {
229 outname = (char *) malloc(strlen(DEFAULT_NAME) + 1);
230 strcpy(outname, DEFAULT_NAME);
231 } else {
232 outname = (char *) malloc(cnt + 1);
233 strncpy(outname, argv[1], cnt);
234 outname[cnt] = '\0';
235 }
236 }
237 sprintf(outfile, "%s.h", outname);
238
239 ofd = fopen(outfile, "w");
240 if (ofd == NULL) {
241 fprintf(stderr, "Error: cannot open (%s) for writing\n", outfile);
242 exit(1);
243 }
244
245 fprintf(ofd, "#define %s_width\t\t%d\n", outname, width);
246 fprintf(ofd, "#define %s_height\t\t%d\n", outname, height);
247 fprintf(ofd, "#define %s_colors\t\t%d\n", outname, fcnt);
248 fprintf(ofd, "#define %s_back\t\t%d\n", outname, (int) data1[0]);
249 fprintf(ofd, "int\t%s_reds[] = {", outname);
250 for (i = 0; i < fcnt; i++) {
251 if (i == (fcnt - 1)) {
252 fprintf(ofd, "%d};\n", fcolrs[i].red);
253 } else {
254 fprintf(ofd, "%d, ", fcolrs[i].red);
255 }
256 }
257 fprintf(ofd, "int\t%s_greens[] = {", outname);
258 for (i = 0; i < fcnt; i++) {
259 if (i == (fcnt - 1)) {
260 fprintf(ofd, "%d};\n", fcolrs[i].green);
261 } else {
262 fprintf(ofd, "%d, ", fcolrs[i].green);
263 }
264 }
265 fprintf(ofd, "int\t%s_blues[] = {", outname);
266 for (i = 0; i < fcnt; i++) {
267 if (i == (fcnt - 1)) {
268 fprintf(ofd, "%d};\n", fcolrs[i].blue);
269 } else {
270 fprintf(ofd, "%d, ", fcolrs[i].blue);
271 }
272 }
273 fprintf(ofd, "unsigned char\t%s_rasterA[] = {\n", outname);
274 for (i = 0; i < (width * height); i++) {
275 fprintf(ofd, "0x%02x,", data1[i]);
276 }
277 fprintf(ofd, "};\n");
278 fprintf(ofd, "unsigned char\t%s_rasterB[] = {\n", outname);
279 for (i = 0; i < (width * height); i++) {
280 fprintf(ofd, "0x%02x,", data2[i]);
281 }
282 fprintf(ofd, "};\n");
283
284 fclose(ofd);
285
286 exit(0);
287 }
This page took 0.060875 seconds and 4 git commands to generate.