toppm: add -r flag for raw ppm
This commit is contained in:
parent
b78e9525cf
commit
d5e3e4326c
4 changed files with 71 additions and 20 deletions
|
@ -85,6 +85,8 @@ jpg, gif, png, ppm, bmp, v210, yuv, ico, tga, togif, toppm, topng, toico \- view
|
||||||
.B -c
|
.B -c
|
||||||
.I comment
|
.I comment
|
||||||
] [
|
] [
|
||||||
|
.B -r
|
||||||
|
] [
|
||||||
.I file
|
.I file
|
||||||
]
|
]
|
||||||
.br
|
.br
|
||||||
|
@ -93,7 +95,6 @@ jpg, gif, png, ppm, bmp, v210, yuv, ico, tga, togif, toppm, topng, toico \- view
|
||||||
.B -c
|
.B -c
|
||||||
.I comment
|
.I comment
|
||||||
] [
|
] [
|
||||||
[
|
|
||||||
.B -g
|
.B -g
|
||||||
.I gamma
|
.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
|
them in the current window; options cause them instead to convert the images
|
||||||
to Plan 9 image format and write them to standard output.
|
to Plan 9 image format and write them to standard output.
|
||||||
.IR Togif ,
|
.IR Togif ,
|
||||||
.IR Toppm ,
|
.IR toppm ,
|
||||||
and
|
and
|
||||||
.I topng
|
.I topng
|
||||||
read Plan 9 images files, convert them to GIF, PPM, or PNG, and write them to standard output.
|
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
|
Both accept an option
|
||||||
.B -c
|
.B -c
|
||||||
to set the comment field of the resulting file.
|
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,
|
If there is only one input picture,
|
||||||
.I togif
|
.I togif
|
||||||
converts the image to GIF format.
|
converts the image to GIF format.
|
||||||
|
|
|
@ -71,8 +71,8 @@ void memendgif(Biobuf*);
|
||||||
Image* onechan(Image*);
|
Image* onechan(Image*);
|
||||||
Memimage* memonechan(Memimage*);
|
Memimage* memonechan(Memimage*);
|
||||||
|
|
||||||
char* writeppm(Biobuf*, Image*, char*);
|
char* writeppm(Biobuf*, Image*, char*, int);
|
||||||
char* memwriteppm(Biobuf*, Memimage*, char*);
|
char* memwriteppm(Biobuf*, Memimage*, char*, int);
|
||||||
Image* multichan(Image*);
|
Image* multichan(Image*);
|
||||||
Memimage* memmultichan(Memimage*);
|
Memimage* memmultichan(Memimage*);
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
void
|
void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
fprint(2, "usage: toppm [-c 'comment'] [file]\n");
|
fprint(2, "usage: toppm [-c 'comment'] [-r] [file]\n");
|
||||||
exits("usage");
|
exits("usage");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,10 +18,11 @@ main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
Biobuf bout;
|
Biobuf bout;
|
||||||
Memimage *i, *ni;
|
Memimage *i, *ni;
|
||||||
int fd;
|
int fd, rflag;
|
||||||
char buf[256];
|
char buf[256];
|
||||||
char *err, *comment;
|
char *err, *comment;
|
||||||
|
|
||||||
|
rflag = 0;
|
||||||
comment = nil;
|
comment = nil;
|
||||||
ARGBEGIN{
|
ARGBEGIN{
|
||||||
case 'c':
|
case 'c':
|
||||||
|
@ -33,6 +34,9 @@ main(int argc, char *argv[])
|
||||||
usage();
|
usage();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case 'r':
|
||||||
|
rflag = 1;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
}ARGEND
|
}ARGEND
|
||||||
|
@ -59,7 +63,7 @@ main(int argc, char *argv[])
|
||||||
i = ni;
|
i = ni;
|
||||||
}
|
}
|
||||||
if(err == nil)
|
if(err == nil)
|
||||||
err = memwriteppm(&bout, i, comment);
|
err = memwriteppm(&bout, i, comment, rflag);
|
||||||
}else{
|
}else{
|
||||||
fd = open(argv[0], OREAD);
|
fd = open(argv[0], OREAD);
|
||||||
if(fd < 0)
|
if(fd < 0)
|
||||||
|
@ -76,10 +80,10 @@ main(int argc, char *argv[])
|
||||||
i = ni;
|
i = ni;
|
||||||
}
|
}
|
||||||
if(comment)
|
if(comment)
|
||||||
err = memwriteppm(&bout, i, comment);
|
err = memwriteppm(&bout, i, comment, rflag);
|
||||||
else{
|
else{
|
||||||
snprint(buf, sizeof buf, "Converted by Plan 9 from %s", argv[0]);
|
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);
|
freememimage(i);
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,12 +12,32 @@ static int log2[] = {
|
||||||
-1, -1, -1, -1, -1, -1, -1, 4 /* BUG */, -1, -1, -1, -1, -1, -1, -1, 5
|
-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
|
* Write data
|
||||||
*/
|
*/
|
||||||
static
|
static
|
||||||
char*
|
char*
|
||||||
writedata(Biobuf *fd, Image *image, Memimage *memimage)
|
writedata(Biobuf *fd, Image *image, Memimage *memimage, int rflag)
|
||||||
{
|
{
|
||||||
char *err;
|
char *err;
|
||||||
uchar *data;
|
uchar *data;
|
||||||
|
@ -70,6 +90,13 @@ writedata(Biobuf *fd, Image *image, Memimage *memimage)
|
||||||
pix = (data[i]>>depth*((xmask-x)&xmask))&pmask;
|
pix = (data[i]>>depth*((xmask-x)&xmask))&pmask;
|
||||||
if(((x+1)&xmask) == 0)
|
if(((x+1)&xmask) == 0)
|
||||||
i++;
|
i++;
|
||||||
|
if(rflag) {
|
||||||
|
if(chan == GREY1)
|
||||||
|
Bputbit(fd, pix);
|
||||||
|
else
|
||||||
|
Bputc(fd, pix);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
col += Bprint(fd, "%d", pix);
|
col += Bprint(fd, "%d", pix);
|
||||||
if(col >= MAXLINE-(2+1)){
|
if(col >= MAXLINE-(2+1)){
|
||||||
Bprint(fd, "\n");
|
Bprint(fd, "\n");
|
||||||
|
@ -77,10 +104,16 @@ writedata(Biobuf *fd, Image *image, Memimage *memimage)
|
||||||
}else
|
}else
|
||||||
col += Bprint(fd, " ");
|
col += Bprint(fd, " ");
|
||||||
}
|
}
|
||||||
|
if(rflag)
|
||||||
|
Bputbit(fd, -1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case GREY8:
|
case GREY8:
|
||||||
for(i=0; i<ndata; i++){
|
for(i=0; i<ndata; i++){
|
||||||
|
if(rflag) {
|
||||||
|
Bputc(fd, data[i]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
col += Bprint(fd, "%d", data[i]);
|
col += Bprint(fd, "%d", data[i]);
|
||||||
if(col >= MAXLINE-(4+1)){
|
if(col >= MAXLINE-(4+1)){
|
||||||
Bprint(fd, "\n");
|
Bprint(fd, "\n");
|
||||||
|
@ -91,6 +124,12 @@ writedata(Biobuf *fd, Image *image, Memimage *memimage)
|
||||||
break;
|
break;
|
||||||
case RGB24:
|
case RGB24:
|
||||||
for(i=0; i<ndata; i+=3){
|
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]);
|
col += Bprint(fd, "%d %d %d", data[i+2], data[i+1], data[i]);
|
||||||
if(col >= MAXLINE-(4+4+4+1)){
|
if(col >= MAXLINE-(4+4+4+1)){
|
||||||
Bprint(fd, "\n");
|
Bprint(fd, "\n");
|
||||||
|
@ -108,21 +147,21 @@ writedata(Biobuf *fd, Image *image, Memimage *memimage)
|
||||||
|
|
||||||
static
|
static
|
||||||
char*
|
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;
|
char *err;
|
||||||
|
|
||||||
switch(chan){
|
switch(chan){
|
||||||
case GREY1:
|
case GREY1:
|
||||||
Bprint(fd, "P1\n");
|
Bprint(fd, "%s\n", rflag? "P4": "P1");
|
||||||
break;
|
break;
|
||||||
case GREY2:
|
case GREY2:
|
||||||
case GREY4:
|
case GREY4:
|
||||||
case GREY8:
|
case GREY8:
|
||||||
Bprint(fd, "P2\n");
|
Bprint(fd, "%s\n", rflag? "P5": "P2");
|
||||||
break;
|
break;
|
||||||
case RGB24:
|
case RGB24:
|
||||||
Bprint(fd, "P3\n");
|
Bprint(fd, "%s\n", rflag? "P6": "P3");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return "WritePPM: can't handle channel type";
|
return "WritePPM: can't handle channel type";
|
||||||
|
@ -149,21 +188,22 @@ writeppm0(Biobuf *fd, Image *image, Memimage *memimage, Rectangle r, int chan, c
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
err = writedata(fd, image, memimage);
|
err = writedata(fd, image, memimage, rflag);
|
||||||
|
|
||||||
Bprint(fd, "\n");
|
if(!rflag)
|
||||||
|
Bprint(fd, "\n");
|
||||||
Bflush(fd);
|
Bflush(fd);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
char*
|
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*
|
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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue