]>
Commit | Line | Data |
---|---|---|
1 | #include <stdio.h> | |
2 | #include <stdlib.h> | |
3 | #include <string.h> | |
4 | ||
5 | #define MAXCOLORS 256 | |
6 | ||
7 | struct rgb_color { | |
8 | unsigned int red, green, blue; | |
9 | }; | |
10 | ||
11 | void | |
12 | WritePPM(char *outfile, unsigned char *data, int width, int height, struct rgb_color *colrs) | |
13 | { | |
14 | FILE *fp; | |
15 | int i; | |
16 | ||
17 | if ((fp = fopen(outfile, "w")) == NULL) | |
18 | return; | |
19 | fprintf(fp, "P6\n%d %d\n255\n", width, height); | |
20 | for (i = 0; i < width * height; i++) { | |
21 | fputc(colrs[data[i]].red, fp); | |
22 | fputc(colrs[data[i]].green, fp); | |
23 | fputc(colrs[data[i]].blue, fp); | |
24 | } | |
25 | fclose(fp); | |
26 | } | |
27 | ||
28 | unsigned char * | |
29 | ReadFish(fp, w, h, colrs, filename) | |
30 | FILE *fp; | |
31 | int *w, *h; | |
32 | struct rgb_color *colrs; | |
33 | char *filename; | |
34 | { | |
35 | int i; | |
36 | char junk[80]; | |
37 | char name[80]; | |
38 | char *cptr; | |
39 | unsigned char *data; | |
40 | unsigned char *ptr; | |
41 | int scale, val; | |
42 | ||
43 | for (i = 0; i < 4; i++) { | |
44 | fscanf(fp, "%s %s %d\n", junk, name, &val); | |
45 | cptr = strrchr(name, '_'); | |
46 | if (cptr == NULL) { | |
47 | cptr = name; | |
48 | } else { | |
49 | cptr++; | |
50 | } | |
51 | ||
52 | if (strcmp(cptr, "width") == 0) { | |
53 | *w = val; | |
54 | } else if (strcmp(cptr, "height") == 0) { | |
55 | *h = val; | |
56 | } else if (strcmp(cptr, "colors") == 0) { | |
57 | scale = val; | |
58 | } | |
59 | } | |
60 | ||
61 | cptr = strrchr(name, '_'); | |
62 | if (cptr == NULL) { | |
63 | strcpy(filename, ""); | |
64 | } else { | |
65 | *cptr = '\0'; | |
66 | strcpy(filename, name); | |
67 | } | |
68 | ||
69 | val = fgetc(fp); | |
70 | while (val != '{') { | |
71 | val = fgetc(fp); | |
72 | } | |
73 | for (i = 0; i < scale; i++) { | |
74 | fscanf(fp, " %d", &val); | |
75 | colrs[i].red = val / 256; | |
76 | val = fgetc(fp); | |
77 | } | |
78 | ||
79 | val = fgetc(fp); | |
80 | while (val != '{') { | |
81 | val = fgetc(fp); | |
82 | } | |
83 | for (i = 0; i < scale; i++) { | |
84 | fscanf(fp, " %d", &val); | |
85 | colrs[i].green = val / 256; | |
86 | val = fgetc(fp); | |
87 | } | |
88 | ||
89 | val = fgetc(fp); | |
90 | while (val != '{') { | |
91 | val = fgetc(fp); | |
92 | } | |
93 | for (i = 0; i < scale; i++) { | |
94 | fscanf(fp, " %d", &val); | |
95 | colrs[i].blue = val / 256; | |
96 | val = fgetc(fp); | |
97 | } | |
98 | ||
99 | for (i = scale; i < MAXCOLORS; i++) { | |
100 | colrs[i].red = 0; | |
101 | colrs[i].green = 0; | |
102 | colrs[i].blue = 0; | |
103 | } | |
104 | ||
105 | val = fgetc(fp); | |
106 | while (val != '{') { | |
107 | val = fgetc(fp); | |
108 | } | |
109 | data = (unsigned char *) malloc(2 * (*w) * (*h)); | |
110 | ptr = data; | |
111 | for (i = 0; i < ((*w) * (*h)); i++) { | |
112 | fscanf(fp, " 0x%x", &val); | |
113 | *ptr++ = (unsigned char) val; | |
114 | val = fgetc(fp); | |
115 | } | |
116 | ||
117 | val = fgetc(fp); | |
118 | while (val != '{') { | |
119 | val = fgetc(fp); | |
120 | } | |
121 | for (i = 0; i < ((*w) * (*h)); i++) { | |
122 | fscanf(fp, " 0x%x", &val); | |
123 | *ptr++ = (unsigned char) val; | |
124 | val = fgetc(fp); | |
125 | } | |
126 | ||
127 | return (data); | |
128 | } | |
129 | ||
130 | ||
131 | ||
132 | main(argc, argv) | |
133 | int argc; | |
134 | char *argv[]; | |
135 | { | |
136 | FILE *ifd; | |
137 | int i; | |
138 | struct rgb_color colrs[MAXCOLORS]; | |
139 | unsigned char *data; | |
140 | unsigned char *data1; | |
141 | unsigned char *data2; | |
142 | unsigned char *ptr; | |
143 | unsigned char *ptr2; | |
144 | int width, height; | |
145 | char outfile[256]; | |
146 | char outname[80]; | |
147 | ||
148 | if (argc < 2) { | |
149 | fprintf(stderr, "Usage: %s <fish.h> [<outprefix>]\n", argv[0]); | |
150 | exit(1); | |
151 | } | |
152 | ||
153 | ifd = fopen(argv[1], "r"); | |
154 | if (ifd == NULL) { | |
155 | fprintf(stderr, "Error: cannot open (%s) for read\n", argv[1]); | |
156 | exit(1); | |
157 | } | |
158 | ||
159 | data = ReadFish(ifd, &width, &height, colrs, outname); | |
160 | fclose(ifd); | |
161 | ptr = data; | |
162 | ||
163 | data1 = (unsigned char *) malloc(width * height); | |
164 | ptr2 = data1; | |
165 | for (i = 0; i < (width * height); i++) { | |
166 | *ptr2++ = *ptr++; | |
167 | } | |
168 | ||
169 | data2 = (unsigned char *) malloc(width * height); | |
170 | ptr2 = data2; | |
171 | for (i = 0; i < (width * height); i++) { | |
172 | *ptr2++ = *ptr++; | |
173 | } | |
174 | ||
175 | if (argc >= 3) { | |
176 | strcpy(outname, argv[2]); | |
177 | } | |
178 | ||
179 | sprintf(outfile, "%s_1.ppm", outname); | |
180 | ||
181 | WritePPM(outfile, data1, width, height, colrs); | |
182 | ||
183 | sprintf(outfile, "%s_2.ppm", outname); | |
184 | ||
185 | WritePPM(outfile, data2, width, height, colrs); | |
186 | ||
187 | exit(0); | |
188 | } |