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;
180 if (argv[1][0] == '-') {
181 if (argv[1][1] == 'u') {
190 printf("Usage: gobpack [-u] <file> [palette.pcx] <\n\t-u to unpack the gob\n");
196 int x_count, y_count;
201 unsigned char palette[768];
202 unsigned char *pal = NULL;
205 f = fopen(argv[3], "rb");
207 fseek(f, -769, SEEK_END);
211 fread(pal, 1, 768, f);
217 filename = malloc(strlen(argv[2]) + 5);
219 printf("Not enough memory!\n");
223 strcpy(filename, argv[2]);
224 strcat(filename, ".gob");
225 f = fopen(filename, "rb");
227 printf("Couldn't open file %s\n", filename);
230 fseek(f, 0, SEEK_END);
232 fseek(f, 0, SEEK_SET);
234 read_gob(f, &gob, len);
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];
249 data = malloc(400*256);
251 printf("Not enough memory!\n");
254 memset(data, 0, 400*256);
256 x_count = 400 / width;
257 y_count = 256 / width;
259 for (yi = 0; yi < y_count; yi++) {
260 for (xi = 0; xi < x_count; xi++) {
264 i = yi * x_count + xi;
265 if (i >= gob.num_images)
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];
278 strcpy(filename, argv[2]);
279 strcat(filename, ".pcx");
280 f = fopen(filename, "wb");
282 printf("Couldn't open file %s\n", filename);
286 write_pcx(f, data, 400, 256, pal);
290 strcpy(filename, argv[2]);
291 strcat(filename, ".txt");
292 f = fopen(filename, "w");
294 printf("Couldn't open file %s\n", filename);
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++) {
302 i = yi * x_count + xi;
303 if (i >= gob.num_images)
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]);
323 data = malloc(400*256);
325 printf("Not enough memory!\n");
329 filename = malloc(strlen(argv[1]) + 5);
331 printf("Not enough memory!\n");
335 strcpy(filename, argv[1]);
336 strcat(filename, ".pcx");
337 f = fopen(filename, "rb");
339 printf("Couldn't open file %s\n", filename);
343 read_pcx(f, data, 400*256, NULL);
347 strcpy(filename, argv[1]);
348 strcat(filename, ".txt");
349 f = fopen(filename, "r");
351 printf("Couldn't open file %s\n", filename);
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);
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) {
376 } else if (strcmp(buffer, "x:") == 0) {
378 } else if (strcmp(buffer, "y:") == 0) {
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) {
386 } else if (strcmp(buffer, "hotspot_y:") == 0) {
391 gob.orig_data[i] = malloc(gob.width[i] * gob.height[i]);
392 gob.data[i] = gob.orig_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)];
404 strcpy(filename, argv[1]);
405 strcat(filename, ".gob");
406 f = fopen(filename, "wb");
408 printf("Couldn't open file %s\n", filename);
416 printf("%s build\n", filename);