15 static void read_pcx(FILE * handle, void *buf, int buf_len, char *pal)
17 unsigned char *buffer=buf;
22 fseek(handle, 128, SEEK_CUR);
24 while (ofs1 < buf_len) {
26 if ((a & 0xc0) == 0xc0) {
29 for (c1 = 0; c1 < a && ofs1 < buf_len; c1++)
30 buffer[ofs1++] = (char) b;
32 buffer[ofs1++] = (char) a;
35 fseek(handle, 1, SEEK_CUR);
36 for (c1 = 0; c1 < 768; c1++)
37 pal[c1] = fgetc(handle) >> 2;
42 static void write_pcx(FILE *pcxfile, unsigned char *data, int width, int height, unsigned char *palette)
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
52 fputc(0, pcxfile); // ymin
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
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
75 for (i = 0 ; i < width*height ; i++)
76 if ( (*data & 0xc0) != 0xc0)
77 fputc(*data++, pcxfile);
81 fputc(*data++, pcxfile);
86 fputc(0x0c, pcxfile); // palette ID byte
88 for (i = 0 ; i < 768 ; i++)
89 fputc(*palette++, pcxfile);
91 for (i = 0 ; i < 768 ; i++)
92 fputc(i / 3, pcxfile);
95 int read_gob(FILE *handle, gob_t *gob, int len)
97 unsigned char *gob_data;
100 gob_data = malloc(len);
101 fread(gob_data, 1, len, handle);
103 gob->num_images = (short)((gob_data[0]) + (gob_data[1] << 8));
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++) {
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);
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;
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];
131 int write_gob(FILE *handle, gob_t *gob)
136 fputc((gob->num_images >> 0) & 0xff, handle);
137 fputc((gob->num_images >> 8) & 0xff, handle);
139 offset = 2 + (gob->num_images * 4);
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);
148 offset += gob->width[i] * gob->height[i];
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);
154 fputc((gob->height[i] >> 0) & 0xff, handle);
155 fputc((gob->height[i] >> 8) & 0xff, handle);
157 fputc((gob->hs_x[i] >> 0) & 0xff, handle);
158 fputc((gob->hs_x[i] >> 8) & 0xff, handle);
160 fputc((gob->hs_y[i] >> 0) & 0xff, handle);
161 fputc((gob->hs_y[i] >> 8) & 0xff, handle);
163 fwrite(gob->data[i], 1, gob->width[i] * gob->height[i], handle);
168 int main(int argc, char **argv)
175 char *filename = NULL;
181 if (argv[1][0] == '-') {
182 if (argv[1][1] == 'u') {
191 printf("Usage: gobpack [-u] <file> [palette.pcx] <\n\t-u to unpack the gob\n");
197 int x_count, y_count;
202 unsigned char palette[768];
203 unsigned char *pal = NULL;
206 f = fopen(argv[3], "rb");
208 fseek(f, -769, SEEK_END);
212 fread(pal, 1, 768, f);
218 filename = malloc(strlen(argv[2]) + 5);
220 printf("Not enough memory!\n");
224 strcpy(filename, argv[2]);
225 strcat(filename, ".gob");
226 f = fopen(filename, "rb");
228 printf("Couldn't open file %s\n", filename);
231 fseek(f, 0, SEEK_END);
233 fseek(f, 0, SEEK_SET);
235 read_gob(f, &gob, len);
241 for (i = 0; i < gob.num_images; i++) {
242 if (gob.height[i] > height)
243 height = gob.height[i];
244 if (gob.width[i] > width)
245 width = gob.width[i];
250 data = malloc(400*256);
252 printf("Not enough memory!\n");
255 memset(data, 0, 400*256);
257 x_count = 400 / width;
258 y_count = 256 / width;
260 for (yi = 0; yi < y_count; yi++) {
261 for (xi = 0; xi < x_count; xi++) {
265 i = yi * x_count + xi;
266 if (i >= gob.num_images)
270 dst = &data[(yi * height) * 400 + (xi * width)];
271 for (y = 0; y < gob.height[i]; y++) {
272 for (x = 0; x < gob.width[i]; x++) {
273 dst[y * 400 + x] = src[y * gob.width[i] + x];
279 strcpy(filename, argv[2]);
280 strcat(filename, ".pcx");
281 f = fopen(filename, "wb");
283 printf("Couldn't open file %s\n", filename);
287 write_pcx(f, data, 400, 256, pal);
291 strcpy(filename, argv[2]);
292 strcat(filename, ".txt");
293 f = fopen(filename, "w");
295 printf("Couldn't open file %s\n", filename);
299 fprintf(f, "num_images: %i\n\n", gob.num_images);
300 for (yi = 0; yi < y_count; yi++) {
301 for (xi = 0; xi < x_count; xi++) {
303 i = yi * x_count + xi;
304 if (i >= gob.num_images)
307 fprintf(f, "image: %i\n", i + 1);
308 fprintf(f, "x: %i\n", (xi * width));
309 fprintf(f, "y: %i\n", (yi * height));
310 fprintf(f, "width: %i\n", gob.width[i]);
311 fprintf(f, "height: %i\n", gob.height[i]);
312 fprintf(f, "hotspot_x: %i\n", gob.hs_x[i]);
313 fprintf(f, "hotspot_y: %i\n", gob.hs_y[i]);
324 data = malloc(400*256);
326 printf("Not enough memory!\n");
330 filename = malloc(strlen(argv[1]) + 5);
332 printf("Not enough memory!\n");
336 strcpy(filename, argv[1]);
337 strcat(filename, ".pcx");
338 f = fopen(filename, "rb");
340 printf("Couldn't open file %s\n", filename);
344 read_pcx(f, data, 400*256, NULL);
348 strcpy(filename, argv[1]);
349 strcat(filename, ".txt");
350 f = fopen(filename, "r");
352 printf("Couldn't open file %s\n", filename);
362 fscanf(f, "%s %i\n", &buffer, &value);
363 if (strcmp(buffer, "num_images:") == 0) {
364 if (gob.num_images != 0) {
365 printf("Parse error in %s\n", filename);
368 gob.num_images = value;
369 gob.width = malloc(gob.num_images*sizeof(int));
370 gob.height = malloc(gob.num_images*sizeof(int));
371 gob.hs_x = malloc(gob.num_images*sizeof(int));
372 gob.hs_y = malloc(gob.num_images*sizeof(int));
373 gob.data = malloc(gob.num_images*sizeof(void *));
374 gob.orig_data = malloc(gob.num_images*sizeof(void *));
375 } else if (strcmp(buffer, "image:") == 0) {
377 } else if (strcmp(buffer, "x:") == 0) {
379 } else if (strcmp(buffer, "y:") == 0) {
381 } else if (strcmp(buffer, "width:") == 0) {
382 gob.width[i] = value;
383 } else if (strcmp(buffer, "height:") == 0) {
384 gob.height[i] = value;
385 } else if (strcmp(buffer, "hotspot_x:") == 0) {
387 } else if (strcmp(buffer, "hotspot_y:") == 0) {
392 gob.orig_data[i] = malloc(gob.width[i] * gob.height[i]);
393 gob.data[i] = gob.orig_data[i];
395 for (y = 0; y < gob.height[i]; y++) {
396 for (x = 0; x < gob.width[i]; x++) {
397 dst[y * gob.width[i] + x] = data[(y_pos + y) * 400 + (x_pos + x)];
405 strcpy(filename, argv[1]);
406 strcat(filename, ".gob");
407 f = fopen(filename, "wb");
409 printf("Couldn't open file %s\n", filename);
417 printf("%s build\n", filename);