home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include "pic.h"
-
- main(ac, av)
- int ac;
- char *av[];
- {
- struct P_FSIG pfsig;
- struct WS_PIC_HEADER *picheader;
- int crc, num, nb, c, i, w, dogray = 0, wanted = 0;
- char *buf, *bp;
- extern needmotorolaorder;
-
- if(ac != 1 && ac != 2)
- {
- fprintf(stderr, "Usage: pictopbm [filenum[g]] <in > out\n");
- exit(1);
- }
- if(ac == 2)
- {
- wanted = atoi(av[1]);
- if(*(av[1] + strlen(av[1]) - 1) == 'g')
- dogray = 1;
- }
-
- needmotorolaorder = 0;
- if(fread(&pfsig, sizeof(pfsig), 1, stdin) != 1 ||
- strncmp(pfsig.id, PIC_MAGIC, 4))
- {
- fprintf(stderr, "Not a PIC file\n");
- exit(1);
- }
- pfsig.count = ShortSwap(pfsig.count);
-
- picheader = (struct WS_PIC_HEADER *)malloc(sizeof(*picheader) * pfsig.count);
- for(num = 0; num < pfsig.count; num++)
- if(fread(&picheader[num], sizeof(*picheader), 1, stdin) != 1)
- {
- fprintf(stderr, "Corrupted file at header %d\n", num+1);
- exit(1);
- }
- else
- {
- picheader[num].crc = ShortSwap(picheader[num].crc);
- picheader[num].size.x = ShortSwap(picheader[num].size.x);
- picheader[num].size.y = ShortSwap(picheader[num].size.y);
- picheader[num].byte_size = ShortSwap(picheader[num].byte_size);
- picheader[num].offset = LongSwap(picheader[num].offset);
- }
- if(pfsig.count > 1 && !wanted)
- fprintf(stderr, "Warning, more than 1 file present (%d)\n", pfsig.count);
- if(pfsig.count < wanted)
- {
- fprintf(stderr, "Warning, no %d files present(%d)\n", wanted,pfsig.count);
- exit(0);
- }
- for(num = 0; num < pfsig.count; num++)
- {
- if(wanted && num != wanted -1)
- {
- for(i = picheader[num].byte_size; i; i--)
- getchar();
- continue;
- }
- if(dogray < 2 )
- {
- putchar('P');
- putchar(dogray ? '2' : '1' );
- putchar('\n');
- printf("%d %d\n", picheader[num].size.x, picheader[num].size.y);
- if(dogray)
- printf("%d\n", 2);
- buf = (char *)malloc(picheader[num].size.x * picheader[num].size.y);
- }
- bp = buf;
- w = picheader[num].size.x;
- crc = 0;
- for(nb = 0; nb < picheader[num].byte_size; nb++)
- {
- c = getchar();
- crc = docrc16_1(crc, c);
- for(i = 0; i < 8; i++)
- {
- switch(dogray)
- {
- case 0:
- putchar(c & 1 ? '1' : '0');
- putchar(' ');
- break;
- case 1:
- *bp = c & 1 ? 1 : 0;
- break;
- case 2:
- putchar(*bp ? '0' : (c & 1 ? '1' : '2'));
- putchar(' ');
- break;
- }
- bp++;
- c >>= 1;
- if(--w == 0)
- {
- w = picheader[num].size.x;
- if(-w & 8) /* Magic */
- {
- nb++;
- c = getchar();
- crc = docrc16_1(crc, c);
- }
- break;
- }
- }
- if(nb % 4 == 3 && dogray != 1)
- putchar('\n');
- }
- if(crc != picheader[num].crc)
- fprintf(stderr, "CRC error: %#x != %#x\n", crc, picheader[num].crc);
- if(dogray != 1)
- {
- if(nb % 4 != 0)
- putchar('\n');
- break;
- }
- else
- {
- dogray++; wanted++;
- }
- }
- return 0;
- }
-