]> icculus.org git repositories - crow/jumpnbump.git/blob - modify/gobpack.c
Finished gobpack and included it into the build process.
[crow/jumpnbump.git] / modify / gobpack.c
1 #include <stdio.h>
2 #include <malloc.h>
3 #include <string.h>
4
5 typedef struct {
6         int num_images;
7         int *width;
8         int *height;
9         int *hs_x;
10         int *hs_y;
11         void **data;
12         void **orig_data;
13 } gob_t;
14
15 static void read_pcx(FILE * handle, void *buf, int buf_len, char *pal)
16 {
17         unsigned char *buffer=buf;
18         short c1;
19         short a, b;
20         long ofs1;
21         if (buffer != 0) {
22                 fseek(handle, 128, SEEK_CUR);
23                 ofs1 = 0;
24                 while (ofs1 < buf_len) {
25                         a = fgetc(handle);
26                         if ((a & 0xc0) == 0xc0) {
27                                 b = fgetc(handle);
28                                 a &= 0x3f;
29                                 for (c1 = 0; c1 < a && ofs1 < buf_len; c1++)
30                                         buffer[ofs1++] = (char) b;
31                         } else
32                                 buffer[ofs1++] = (char) a;
33                 }
34                 if (pal != 0) {
35                         fseek(handle, 1, SEEK_CUR);
36                         for (c1 = 0; c1 < 768; c1++)
37                                 pal[c1] = fgetc(handle) >> 2;
38                 }
39         }
40 }
41
42 static void write_pcx(FILE *pcxfile, unsigned char *data, int width, int height, unsigned char *palette)
43 {
44         int    i;
45
46         fputc(0x0a, pcxfile); // manufacturer
47         fputc(5, pcxfile); // version
48         fputc(1, pcxfile); // encoding
49         fputc(8, pcxfile); // bits_per_pixel
50         fputc(0, pcxfile); // xmin
51         fputc(0, pcxfile);
52         fputc(0, pcxfile); // ymin
53         fputc(0, pcxfile);
54         fputc((width - 1) & 0xff, pcxfile); // xmax
55         fputc(((width - 1) >> 8) & 0xff, pcxfile);
56         fputc((height - 1) & 0xff, pcxfile); // ymax
57         fputc(((height - 1) >> 8) & 0xff, pcxfile);
58         fputc(width & 0xff, pcxfile); // hres
59         fputc((width >> 8) & 0xff, pcxfile);
60         fputc(height & 0xff, pcxfile); // vres
61         fputc((height >> 8) & 0xff, pcxfile);
62         for (i = 0; i < 48; i++) // palette
63                 fputc(0, pcxfile);
64         fputc(0, pcxfile); // reserved
65         fputc(1, pcxfile); // color_planes
66         fputc(width & 0xff, pcxfile); // bytes_per_line
67         fputc((width >> 8) & 0xff, pcxfile);
68         fputc(1 & 0xff, pcxfile); // palette_type
69         fputc((1 >> 8) & 0xff, pcxfile);
70         for (i = 0; i < 58; i++) // filler
71                 fputc(0, pcxfile);
72
73         // pack the image
74
75         for (i = 0 ; i < width*height ; i++)
76                 if ( (*data & 0xc0) != 0xc0)
77                         fputc(*data++, pcxfile);
78                 else
79                 {
80                         fputc(0xc1, pcxfile);
81                         fputc(*data++, pcxfile);
82                 }
83
84         // write the palette
85
86         fputc(0x0c, pcxfile); // palette ID byte
87         if (palette)
88                 for (i = 0 ; i < 768 ; i++)
89                         fputc(*palette++, pcxfile);
90         else
91                 for (i = 0 ; i < 768 ; i++)
92                         fputc(i / 3, pcxfile);
93 }
94
95 int read_gob(FILE *handle, gob_t *gob, int len)
96 {
97         unsigned char *gob_data;
98         int i;
99
100         gob_data = malloc(len);
101         fread(gob_data, 1, len, handle);
102
103         gob->num_images = (short)((gob_data[0]) + (gob_data[1] << 8));
104
105         gob->width = malloc(gob->num_images*sizeof(int));
106         gob->height = malloc(gob->num_images*sizeof(int));
107         gob->hs_x = malloc(gob->num_images*sizeof(int));
108         gob->hs_y = malloc(gob->num_images*sizeof(int));
109         gob->data = malloc(gob->num_images*sizeof(void *));
110         gob->orig_data = malloc(gob->num_images*sizeof(void *));
111         for (i=0; i<gob->num_images; i++) {
112                 int image_size;
113                 int offset;
114
115                 offset = (gob_data[i*4+2]) + (gob_data[i*4+3] << 8) + (gob_data[i*4+4] << 16) + (gob_data[i*4+5] << 24);
116
117                 gob->width[i]  = (short)((gob_data[offset]) + (gob_data[offset+1] << 8)); offset += 2;
118                 gob->height[i] = (short)((gob_data[offset]) + (gob_data[offset+1] << 8)); offset += 2;
119                 gob->hs_x[i]   = (short)((gob_data[offset]) + (gob_data[offset+1] << 8)); offset += 2;
120                 gob->hs_y[i]   = (short)((gob_data[offset]) + (gob_data[offset+1] << 8)); offset += 2;
121
122                 image_size = gob->width[i] * gob->height[i];
123                 gob->orig_data[i] = malloc(image_size);
124                 memcpy(gob->orig_data[i], &gob_data[offset], image_size);
125                 gob->data[i] = (unsigned short *)gob->orig_data[i];
126         }
127         free(gob_data);
128         return 0;
129 }
130
131 int write_gob(FILE *handle, gob_t *gob)
132 {
133         int i;
134         int offset;
135
136         fputc((gob->num_images >> 0) & 0xff, handle);
137         fputc((gob->num_images >> 8) & 0xff, handle);
138
139         offset = 2 + (gob->num_images * 4);
140
141         for (i=0; i<gob->num_images; i++) {
142                 fputc((offset >> 0) & 0xff, handle);
143                 fputc((offset >> 8) & 0xff, handle);
144                 fputc((offset >> 16) & 0xff, handle);
145                 fputc((offset >> 24) & 0xff, handle);
146
147                 offset += 8;
148                 offset += gob->width[i] * gob->height[i];
149         }
150         for (i=0; i<gob->num_images; i++) {
151                 fputc((gob->width[i] >> 0) & 0xff, handle);
152                 fputc((gob->width[i] >> 8) & 0xff, handle);
153
154                 fputc((gob->height[i] >> 0) & 0xff, handle);
155                 fputc((gob->height[i] >> 8) & 0xff, handle);
156
157                 fputc((gob->hs_x[i] >> 0) & 0xff, handle);
158                 fputc((gob->hs_x[i] >> 8) & 0xff, handle);
159
160                 fputc((gob->hs_y[i] >> 0) & 0xff, handle);
161                 fputc((gob->hs_y[i] >> 8) & 0xff, handle);
162
163                 fwrite(gob->data[i], 1, gob->width[i] * gob->height[i], handle);
164         }
165         return 0;
166 }
167
168 int main(int argc, char **argv)
169 {
170         int usage = 0;
171         int unpack = 0;
172         FILE *f;
173         int len;
174         gob_t gob;
175         char *filename = NULL;
176
177         if (argc < 2) {
178                 usage = 1;
179         }
180         if (argv[1][0] == '-') {
181                 if (argv[1][1] == 'u') {
182                         if (argc < 3)
183                                 usage = 1;
184                         unpack = 1;
185                 } else
186                         usage = 1;
187         }
188         
189         if (usage) {
190                 printf("Usage: gobpack [-u] <file> [palette.pcx] <\n\t-u to unpack the gob\n");
191                 return 1;
192         }
193
194         if (unpack) {
195                 int width, height;
196                 int x_count, y_count;
197                 int xi, yi;
198                 int i;
199                 unsigned char *data;
200                 unsigned char *dst;
201                 unsigned char palette[768];
202                 unsigned char *pal = NULL;
203
204                 if (argc > 3) {
205                         f = fopen(argv[3], "rb");
206                         if (f) {
207                                 fseek(f, -769, SEEK_END);
208                                 i = fgetc(f);
209                                 if (i == 0x0c) {
210                                         pal = palette;
211                                         fread(pal, 1, 768, f);
212                                 }
213                                 fclose(f);
214                         }
215                 }
216
217                 filename = malloc(strlen(argv[2]) + 5);
218                 if (!filename) {
219                         printf("Not enough memory!\n");
220                         return -1;
221                 }
222
223                 strcpy(filename, argv[2]);
224                 strcat(filename, ".gob");
225                 f = fopen(filename, "rb");
226                 if (!f) {
227                         printf("Couldn't open file %s\n", filename);
228                         return -1;
229                 }
230                 fseek(f, 0, SEEK_END);
231                 len = ftell(f);
232                 fseek(f, 0, SEEK_SET);
233
234                 read_gob(f, &gob, len);
235
236                 fclose(f);
237
238                 width = 0;
239                 height = 0;
240                 for (i = 0; i < gob.num_images; i++) {
241                         if (gob.height[i] > height)
242                                 height = gob.height[i];
243                         if (gob.width[i] > width)
244                                 width = gob.width[i];
245                 }
246                 width+=2;
247                 height+=2;
248
249                 data = malloc(400*256);
250                 if (!data) {
251                         printf("Not enough memory!\n");
252                         return -1;
253                 }
254                 memset(data, 0, 400*256);
255
256                 x_count = 400 / width;
257                 y_count = 256 / width;
258
259                 for (yi = 0; yi < y_count; yi++) {
260                         for (xi = 0; xi < x_count; xi++) {
261                                 int x,y;
262                                 unsigned char *src;
263
264                                 i = yi * x_count + xi;
265                                 if (i >= gob.num_images)
266                                         continue;
267
268                                 src = gob.data[i];
269                                 dst = &data[(yi * height) * 400 + (xi * width)];
270                                 for (y = 0; y < gob.height[i]; y++) {
271                                         for (x = 0; x < gob.width[i]; x++) {
272                                                 dst[y * 400 + x] = src[y * gob.width[i] + x];
273                                         }
274                                 }
275                         }
276                 }
277                 
278                 strcpy(filename, argv[2]);
279                 strcat(filename, ".pcx");
280                 f = fopen(filename, "wb");
281                 if (!f) {
282                         printf("Couldn't open file %s\n", filename);
283                         return -1;
284                 }
285
286                 write_pcx(f, data, 400, 256, pal);
287
288                 fclose(f);
289
290                 strcpy(filename, argv[2]);
291                 strcat(filename, ".txt");
292                 f = fopen(filename, "w");
293                 if (!f) {
294                         printf("Couldn't open file %s\n", filename);
295                         return -1;
296                 }
297
298                 fprintf(f, "num_images: %i\n\n", gob.num_images);
299                 for (yi = 0; yi < y_count; yi++) {
300                         for (xi = 0; xi < x_count; xi++) {
301                                 
302                                 i = yi * x_count + xi;
303                                 if (i >= gob.num_images)
304                                         continue;
305
306                                 fprintf(f, "image: %i\n", i + 1);
307                                 fprintf(f, "x: %i\n", (xi * width));
308                                 fprintf(f, "y: %i\n", (yi * height));
309                                 fprintf(f, "width: %i\n", gob.width[i]);
310                                 fprintf(f, "height: %i\n", gob.height[i]);
311                                 fprintf(f, "hotspot_x: %i\n", gob.hs_x[i]);
312                                 fprintf(f, "hotspot_y: %i\n", gob.hs_y[i]);
313                                 fprintf(f, "\n");
314                         }
315                 }
316
317                 fclose(f);
318         } else {
319                 unsigned char *data;
320                 int i;
321                 int x_pos, y_pos;
322
323                 data = malloc(400*256);
324                 if (!data) {
325                         printf("Not enough memory!\n");
326                         return -1;
327                 }
328
329                 filename = malloc(strlen(argv[1]) + 5);
330                 if (!filename) {
331                         printf("Not enough memory!\n");
332                         return -1;
333                 }
334
335                 strcpy(filename, argv[1]);
336                 strcat(filename, ".pcx");
337                 f = fopen(filename, "rb");
338                 if (!f) {
339                         printf("Couldn't open file %s\n", filename);
340                         return -1;
341                 }
342
343                 read_pcx(f, data, 400*256, NULL);
344
345                 fclose(f);
346
347                 strcpy(filename, argv[1]);
348                 strcat(filename, ".txt");
349                 f = fopen(filename, "r");
350                 if (!f) {
351                         printf("Couldn't open file %s\n", filename);
352                         return -1;
353                 }
354
355                 gob.num_images = 0;
356
357                 while (!feof(f)) {
358                         char buffer[1024];
359                         int value;
360
361                         fscanf(f, "%s %i\n", &buffer, &value);
362                         if (strcmp(buffer, "num_images:") == 0) {
363                                 if (gob.num_images != 0) {
364                                         printf("Parse error in %s\n", filename);
365                                         return -1;
366                                 }
367                                 gob.num_images = value;
368                                 gob.width = malloc(gob.num_images*sizeof(int));
369                                 gob.height = malloc(gob.num_images*sizeof(int));
370                                 gob.hs_x = malloc(gob.num_images*sizeof(int));
371                                 gob.hs_y = malloc(gob.num_images*sizeof(int));
372                                 gob.data = malloc(gob.num_images*sizeof(void *));
373                                 gob.orig_data = malloc(gob.num_images*sizeof(void *));
374                         } else if (strcmp(buffer, "image:") == 0) {
375                                 i = value - 1;
376                         } else if (strcmp(buffer, "x:") == 0) {
377                                 x_pos = value;
378                         } else if (strcmp(buffer, "y:") == 0) {
379                                 y_pos = value;
380                         } else if (strcmp(buffer, "width:") == 0) {
381                                 gob.width[i] = value;
382                         } else if (strcmp(buffer, "height:") == 0) {
383                                 gob.height[i] = value;
384                         } else if (strcmp(buffer, "hotspot_x:") == 0) {
385                                 gob.hs_x[i] = value;
386                         } else if (strcmp(buffer, "hotspot_y:") == 0) {
387                                 int x, y;
388                                 unsigned char *dst;
389
390                                 gob.hs_y[i] = value;
391                                 gob.orig_data[i] = malloc(gob.width[i] * gob.height[i]);
392                                 gob.data[i] = gob.orig_data[i];
393                                 dst = gob.data[i];
394                                 for (y = 0; y < gob.height[i]; y++) {
395                                         for (x = 0; x < gob.width[i]; x++) {
396                                                 dst[y * gob.width[i] + x] = data[(y_pos + y) * 400 + (x_pos + x)];
397                                         }
398                                 }
399                         }
400                 }
401
402                 fclose(f);
403
404                 strcpy(filename, argv[1]);
405                 strcat(filename, ".gob");
406                 f = fopen(filename, "wb");
407                 if (!f) {
408                         printf("Couldn't open file %s\n", filename);
409                         return -1;
410                 }
411
412                 write_gob(f, &gob);
413
414                 fclose(f);
415
416                 printf("%s build\n", filename);
417         }
418
419         return 0;
420 }