shithub: riscv

Download patch

ref: d5e3e4326cf24190d11c6f33e1c2c8c563b736b6
parent: b78e9525cf41f99ed845c5d3d878c0936cba6e7f
author: ppatience0 <[email protected]>
date: Sun May 12 12:11:43 EDT 2013

toppm: add -r flag for raw ppm

--- a/sys/man/1/jpg
+++ b/sys/man/1/jpg
@@ -85,6 +85,8 @@
 .B -c
 .I comment
 ] [
+.B -r
+] [
 .I file
 ]
 .br
@@ -93,7 +95,6 @@
 .B -c
 .I comment
 ] [
-[
 .B -g
 .I gamma
 ] [
@@ -126,7 +127,7 @@
 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 @@
 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.
--- a/sys/src/cmd/jpg/imagefile.h
+++ b/sys/src/cmd/jpg/imagefile.h
@@ -71,8 +71,8 @@
 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*);
 
--- a/sys/src/cmd/jpg/toppm.c
+++ b/sys/src/cmd/jpg/toppm.c
@@ -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 @@
 {
 	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 @@
 			usage();
 		}
 		break;
+	case 'r':
+		rflag = 1;
+		break;
 	default:
 		usage();
 	}ARGEND
@@ -59,7 +63,7 @@
 			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 @@
 			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);
 	}
--- a/sys/src/cmd/jpg/writeppm.c
+++ b/sys/src/cmd/jpg/writeppm.c
@@ -12,12 +12,32 @@
 	-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 @@
 				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 @@
 				}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 @@
 		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 @@
 
 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 @@
 		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);
 }