home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 1B
/
DATAFILE_PDCD1B.iso
/
_pocketbk
/
pocketbook
/
opl
/
pbm2p008_t
/
pbm2p008_t~
/
conv
/
pbmtopic.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-02-17
|
3KB
|
136 lines
#include <stdio.h>
#include "pic.h"
#define ISWHITE(c) (c == ' ' || c == '\t' || c == '\n' || c == '\r')
int
getnum()
{
char buf[256], *bp;
int c;
bp = buf;
while((c = getchar()) != EOF)
if(c == '#')
{
while((c = getchar()) != EOF)
if(c == '\n')
break;
}
else
if(!ISWHITE(c))
break;
*bp++ = c;
while((c = getchar()) != EOF)
if(ISWHITE(c) || c == '#')
break;
else
*bp++ = c;
*bp++ = 0;
if(c == EOF)
{
fprintf(stderr, "Premature EOF\n");
exit(1);
}
return atoi(buf);
}
main(ac, av)
int ac;
char *av[];
{
extern needmotorolaorder;
int c, c1, dogray, doascii, tw, w, h,
mx, mxb, mxg, i, j, v, size;
unsigned char *bplane, *gplane, *bp, *gp;
struct P_FSIG pfsig;
struct WS_PIC_HEADER ph;
if(ac != 1)
{
fprintf(stderr, "Usage: pbmtopic < in > out\n");
exit(1);
}
needmotorolaorder = 0;
c = getchar(); c1 = getchar();
if(c != 'P' || (c1 != '1' && c1 != '2' && c1 != '4' && c1 != '5'))
{
fprintf(stderr, "Only PBM/PGM images please.\n");
exit(1);
}
if(c1 == '2' || c1 == '5') dogray = 1;
if(c1 == '1' || c1 == '2') doascii = 1;
w = getnum(); h = getnum();
if(dogray)
{
mx = getnum();
mxb = mx / 4;
mxg = 3 * mx / 4;
}
tw = w + ((-w & 8) ? 8 : 0); /* Magic */
size = ((tw+7)/8) * h;
bp = bplane = (unsigned char *)calloc(size, 1);
if(dogray)
gp = gplane = (unsigned char *)calloc(size, 1);
for(i = 0; i < h; i++)
{
bp = bplane + i * ((tw+7)/8);
gp = gplane + i * ((tw+7)/8);
for(c1 = j = 0; j < w; j++)
{
if(dogray || doascii)
c = doascii ? getnum() : getchar();
else
{
if(c1 == 0)
v = getchar();
c = v & 0x80;
v <<= 1;
}
if(dogray)
{
if(c < mxb)
*bp |= (1<<c1);
else if(c < mxg)
*gp |= (1<<c1);
}
else if(c == 0)
*bp |= (1<<c1);
if(j == w-1 || ++c1 == 8)
{
c1 = 0;
bp++; gp++;
}
}
}
bcopy(PIC_MAGIC, pfsig.id, 4);
pfsig.file_ver = 0x30;
pfsig.app_ver = 0x30;
pfsig.count = ShortSwap(dogray ? 2 : 1);
fwrite(&pfsig, sizeof(pfsig), 1, stdout);
ph.crc = ShortSwap(docrc16(bplane, size));
ph.size.x = ShortSwap(w);
ph.size.y = ShortSwap(h);
ph.byte_size = ShortSwap(size);
ph.offset = LongSwap(12);
fwrite(&ph, sizeof(ph), 1, stdout);
if(dogray)
{
ph.crc = ShortSwap(docrc16(gplane, size));
ph.offset = LongSwap(size);
fwrite(&ph, sizeof(ph), 1, stdout);
}
fwrite(bplane, size, 1, stdout);
if(dogray)
fwrite(gplane, size, 1, stdout);
fflush(stdout);
exit(0);
}