toppm: add -r flag for raw ppm

This commit is contained in:
ppatience0 2013-05-12 16:11:43 -04:00
parent b78e9525cf
commit d5e3e4326c
4 changed files with 71 additions and 20 deletions

View file

@ -85,6 +85,8 @@ jpg, gif, png, ppm, bmp, v210, yuv, ico, tga, togif, toppm, topng, toico \- view
.B -c
.I comment
] [
.B -r
] [
.I file
]
.br
@ -93,7 +95,6 @@ jpg, gif, png, ppm, bmp, v210, yuv, ico, tga, togif, toppm, topng, toico \- view
.B -c
.I comment
] [
[
.B -g
.I gamma
] [
@ -126,7 +127,7 @@ read files in the corresponding formats and, by default, display
them in the current window; options cause them instead to convert the images
to Plan 9 image format and write them to standard output.
.IR Togif ,
.IR Toppm ,
.IR toppm ,
and
.I topng
read Plan 9 images files, convert them to GIF, PPM, or PNG, and write them to standard output.
@ -212,6 +213,12 @@ and have no display capability.
Both accept an option
.B -c
to set the comment field of the resulting file.
The
.B -r
option makes
.I toppm
output raw PPM.
The default is to output plain PPM.
If there is only one input picture,
.I togif
converts the image to GIF format.

View file

@ -71,8 +71,8 @@ void memendgif(Biobuf*);
Image* onechan(Image*);
Memimage* memonechan(Memimage*);
char* writeppm(Biobuf*, Image*, char*);
char* memwriteppm(Biobuf*, Memimage*, char*);
char* writeppm(Biobuf*, Image*, char*, int);
char* memwriteppm(Biobuf*, Memimage*, char*, int);
Image* multichan(Image*);
Memimage* memmultichan(Memimage*);

View file

@ -9,7 +9,7 @@
void
usage(void)
{
fprint(2, "usage: toppm [-c 'comment'] [file]\n");
fprint(2, "usage: toppm [-c 'comment'] [-r] [file]\n");
exits("usage");
}
@ -18,10 +18,11 @@ main(int argc, char *argv[])
{
Biobuf bout;
Memimage *i, *ni;
int fd;
int fd, rflag;
char buf[256];
char *err, *comment;
rflag = 0;
comment = nil;
ARGBEGIN{
case 'c':
@ -33,6 +34,9 @@ main(int argc, char *argv[])
usage();
}
break;
case 'r':
rflag = 1;
break;
default:
usage();
}ARGEND
@ -59,7 +63,7 @@ main(int argc, char *argv[])
i = ni;
}
if(err == nil)
err = memwriteppm(&bout, i, comment);
err = memwriteppm(&bout, i, comment, rflag);
}else{
fd = open(argv[0], OREAD);
if(fd < 0)
@ -76,10 +80,10 @@ main(int argc, char *argv[])
i = ni;
}
if(comment)
err = memwriteppm(&bout, i, comment);
err = memwriteppm(&bout, i, comment, rflag);
else{
snprint(buf, sizeof buf, "Converted by Plan 9 from %s", argv[0]);
err = memwriteppm(&bout, i, buf);
err = memwriteppm(&bout, i, buf, rflag);
}
freememimage(i);
}

View file

@ -12,12 +12,32 @@ static int log2[] = {
-1, -1, -1, -1, -1, -1, -1, 4 /* BUG */, -1, -1, -1, -1, -1, -1, -1, 5
};
static int bitc = 0;
static int nbit = 0;
static
void
Bputbit(Biobufhdr *b, int c)
{
if(c >= 0x0) {
bitc = (bitc << 1) | (c & 0x1);
nbit++;
} else if(nbit > 0) {
for(; nbit < 8; nbit++)
bitc <<= 1;
}
if(nbit == 8) {
Bputc(b, bitc);
bitc = nbit = 0;
}
}
/*
* Write data
*/
static
char*
writedata(Biobuf *fd, Image *image, Memimage *memimage)
writedata(Biobuf *fd, Image *image, Memimage *memimage, int rflag)
{
char *err;
uchar *data;
@ -70,6 +90,13 @@ writedata(Biobuf *fd, Image *image, Memimage *memimage)
pix = (data[i]>>depth*((xmask-x)&xmask))&pmask;
if(((x+1)&xmask) == 0)
i++;
if(rflag) {
if(chan == GREY1)
Bputbit(fd, pix);
else
Bputc(fd, pix);
continue;
}
col += Bprint(fd, "%d", pix);
if(col >= MAXLINE-(2+1)){
Bprint(fd, "\n");
@ -77,10 +104,16 @@ writedata(Biobuf *fd, Image *image, Memimage *memimage)
}else
col += Bprint(fd, " ");
}
if(rflag)
Bputbit(fd, -1);
}
break;
case GREY8:
for(i=0; i<ndata; i++){
if(rflag) {
Bputc(fd, data[i]);
continue;
}
col += Bprint(fd, "%d", data[i]);
if(col >= MAXLINE-(4+1)){
Bprint(fd, "\n");
@ -91,6 +124,12 @@ writedata(Biobuf *fd, Image *image, Memimage *memimage)
break;
case RGB24:
for(i=0; i<ndata; i+=3){
if(rflag) {
Bputc(fd, data[i+2]);
Bputc(fd, data[i+1]);
Bputc(fd, data[i]);
continue;
}
col += Bprint(fd, "%d %d %d", data[i+2], data[i+1], data[i]);
if(col >= MAXLINE-(4+4+4+1)){
Bprint(fd, "\n");
@ -108,21 +147,21 @@ writedata(Biobuf *fd, Image *image, Memimage *memimage)
static
char*
writeppm0(Biobuf *fd, Image *image, Memimage *memimage, Rectangle r, int chan, char *comment)
writeppm0(Biobuf *fd, Image *image, Memimage *memimage, Rectangle r, int chan, char *comment, int rflag)
{
char *err;
switch(chan){
case GREY1:
Bprint(fd, "P1\n");
Bprint(fd, "%s\n", rflag? "P4": "P1");
break;
case GREY2:
case GREY4:
case GREY8:
Bprint(fd, "P2\n");
Bprint(fd, "%s\n", rflag? "P5": "P2");
break;
case RGB24:
Bprint(fd, "P3\n");
Bprint(fd, "%s\n", rflag? "P6": "P3");
break;
default:
return "WritePPM: can't handle channel type";
@ -149,21 +188,22 @@ writeppm0(Biobuf *fd, Image *image, Memimage *memimage, Rectangle r, int chan, c
break;
}
err = writedata(fd, image, memimage);
err = writedata(fd, image, memimage, rflag);
Bprint(fd, "\n");
if(!rflag)
Bprint(fd, "\n");
Bflush(fd);
return err;
}
char*
writeppm(Biobuf *fd, Image *image, char *comment)
writeppm(Biobuf *fd, Image *image, char *comment, int rflag)
{
return writeppm0(fd, image, nil, image->r, image->chan, comment);
return writeppm0(fd, image, nil, image->r, image->chan, comment, rflag);
}
char*
memwriteppm(Biobuf *fd, Memimage *memimage, char *comment)
memwriteppm(Biobuf *fd, Memimage *memimage, char *comment, int rflag)
{
return writeppm0(fd, nil, memimage, memimage->r, memimage->chan, comment);
return writeppm0(fd, nil, memimage, memimage->r, memimage->chan, comment, rflag);
}