19 static void write_pcx(FILE *pcxfile, unsigned char *data, int width, int height, unsigned char *palette)
23 fputc(0x0a, pcxfile); // manufacturer
24 fputc(5, pcxfile); // version
25 fputc(1, pcxfile); // encoding
26 fputc(8, pcxfile); // bits_per_pixel
27 fputc(0, pcxfile); // xmin
29 fputc(0, pcxfile); // ymin
31 fputc((width - 1) & 0xff, pcxfile); // xmax
32 fputc(((width - 1) >> 8) & 0xff, pcxfile);
33 fputc((height - 1) & 0xff, pcxfile); // ymax
34 fputc(((height - 1) >> 8) & 0xff, pcxfile);
35 fputc(width & 0xff, pcxfile); // hres
36 fputc((width >> 8) & 0xff, pcxfile);
37 fputc(height & 0xff, pcxfile); // vres
38 fputc((height >> 8) & 0xff, pcxfile);
39 for (i = 0; i < 48; i++) // palette
41 fputc(0, pcxfile); // reserved
42 fputc(1, pcxfile); // color_planes
43 fputc(width & 0xff, pcxfile); // bytes_per_line
44 fputc((width >> 8) & 0xff, pcxfile);
45 fputc(1 & 0xff, pcxfile); // palette_type
46 fputc((1 >> 8) & 0xff, pcxfile);
47 for (i = 0; i < 58; i++) // filler
52 for (i = 0 ; i < width*height ; i++)
53 if ( (*data & 0xc0) != 0xc0)
54 fputc(*data++, pcxfile);
58 fputc(*data++, pcxfile);
63 fputc(0x0c, pcxfile); // palette ID byte
65 for (i = 0 ; i < 768 ; i++)
66 fputc(*palette++, pcxfile);
68 for (i = 0 ; i < 768 ; i++)
69 fputc(i / 3, pcxfile);
72 int read_gob(FILE *handle, gob_t *gob, int len)
74 unsigned char *gob_data;
77 gob_data = malloc(len);
78 fread(gob_data, 1, len, handle);
80 gob->num_images = (short)((gob_data[0]) + (gob_data[1] << 8));
82 gob->width = malloc(gob->num_images*sizeof(int));
83 gob->height = malloc(gob->num_images*sizeof(int));
84 gob->hs_x = malloc(gob->num_images*sizeof(int));
85 gob->hs_y = malloc(gob->num_images*sizeof(int));
86 gob->data = malloc(gob->num_images*sizeof(void *));
87 gob->orig_data = malloc(gob->num_images*sizeof(void *));
88 for (i=0; i<gob->num_images; i++) {
92 offset = (gob_data[i*4+2]) + (gob_data[i*4+3] << 8) + (gob_data[i*4+4] << 16) + (gob_data[i*4+5] << 24);
94 gob->width[i] = (short)((gob_data[offset]) + (gob_data[offset+1] << 8)); offset += 2;
95 gob->height[i] = (short)((gob_data[offset]) + (gob_data[offset+1] << 8)); offset += 2;
96 gob->hs_x[i] = (short)((gob_data[offset]) + (gob_data[offset+1] << 8)); offset += 2;
97 gob->hs_y[i] = (short)((gob_data[offset]) + (gob_data[offset+1] << 8)); offset += 2;
99 image_size = gob->width[i] * gob->height[i];
100 gob->orig_data[i] = malloc(image_size);
101 memcpy(gob->orig_data[i], &gob_data[offset], image_size);
102 gob->data[i] = (unsigned short *)gob->orig_data[i];
108 int main(int argc, char **argv)
118 } else if (argc == 5) {
120 if ((argv[1][0] != '-') || (argv[1][1] != 'u'))
125 printf("Usage: gobpack [-u] <file.gob> <file.pcx> <filedefs.txt>\n\t-u to unpack the gob\n");
135 f = fopen(argv[2], "rb");
137 printf("Couldn't open file %s\n", argv[2]);
140 fseek(f, 0, SEEK_END);
142 fseek(f, 0, SEEK_SET);
144 read_gob(f, &gob, len);
150 for (i = 0; i < gob.num_images; i++) {
151 height += gob.height[i];
152 if (gob.width[i] > width)
153 width = gob.width[i];
156 data = malloc(width*height);
157 memset(data, 0, width*height);
160 for (i = 0; i < gob.num_images; i++) {
165 for (y = 0; y < gob.height[i]; y++) {
166 for (x = 0; x < gob.width[i]; x++) {
167 dst[y * width + x] = src[y * gob.width[i] + x];
170 dst += gob.height[i] * width;
173 f = fopen(argv[3], "wb");
175 printf("Couldn't open file %s\n", argv[3]);
179 write_pcx(f, data, width, height, NULL);
183 f = fopen(argv[4], "w");
185 printf("Couldn't open file %s\n", argv[4]);
189 fprintf(f, "num_images: %i\n\n", gob.num_images);
190 for (i = 0; i < gob.num_images; i++) {
191 fprintf(f, "width: %i\n", gob.width[i]);
192 fprintf(f, "height: %i\n", gob.height[i]);
193 fprintf(f, "hotspot_x: %i\n", -gob.hs_x[i]);
194 fprintf(f, "hotspot_y: %i\n", -gob.hs_y[i]);