Plan 9 from Bell Labs’s /usr/web/sources/contrib/maht/ppm2eps/ppm2eps.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include <bio.h>
#include <String.h>
#include "ppm.h"

void
usage(void) {
	fprint(2, "usage: ppm2eps [-d dpi]\n");
	exits("usage");
}

static Biobuf stdin; 
static Biobuf stdout;

void
main(int argc, char *argv[]) {

	unsigned int dpi = 100;
	ARGBEGIN {
	case 'd' :
		dpi = atoi(ARGF());
		if(dpi == 0)
			dpi = 100;
		break;
	} ARGEND

	if(Binit(&stdin, 0, OREAD) < 0 || Binit(&stdout, 1, OWRITE) < 0) {
		fprint(2, "stdin/out failed\n");
		exits("stdin/out failed");
	}

	RGBint *ppm = readppm(&stdin);
	eightbit(ppm);

	double w, h, s;
	int x,y, k, b;	
	
	s = 72.0 / dpi;

	w = s * ppm->width;
	h = s * ppm->height;
	x = (int)ceil(w);
	y = (int)ceil(h);

	Bprint(&stdout, "%%!PS-Adobe-3.0\n");
	Bprint(&stdout, "%%%%Creator: ppm2eps on Plan 9 From Bell Labs\n");
	Bprint(&stdout, "%%%%DocumentData: Clean7Bit\n");
	Bprint(&stdout, "%%%%Pages: 1\n");
	Bprint(&stdout, "%%%%BoundingBox: 0 0 %d %d\n", x, y);
	Bprint(&stdout, "%%%%BeginProlog\n");
	Bprint(&stdout, "9 dict begin\n");
	Bprint(&stdout, "%%%%EndProlog\n");
	Bprint(&stdout, "%%%%Page: 1 1\n");
	Bprint(&stdout, "0 0 translate\n");
	Bprint(&stdout, "0 %f translate\n", h);
	Bprint(&stdout, "%f %f scale\n", w, -h);
	Bprint(&stdout, "/scanline %d 3 mul string def\n", ppm->width);
	Bprint(&stdout, "%d %d 8\n", ppm->width, ppm->height);
	Bprint(&stdout, "[ %d 0 0 %d 0 0 ]\n", ppm->width, ppm->height);
	Bprint(&stdout, "{ currentfile scanline readhexstring pop } false 3\n");
	Bprint(&stdout, "colorimage\n");

	k = 0;
	b = 1;
	for(x = 0; x < ppm->width; x++)
	for(y = 0; y < ppm->height; y++, k++) {
		Bprint(&stdout, "%02x%02x%02x", ppm->r[k], ppm->g[k], ppm->b[k]);
		if(b++ % 40 == 0) // every 234 bytes (i.e. < 255)
			Bprint(&stdout, "\n");
	}
	if(b % 40 != 1)
		Bprint(&stdout, "\n");
	Bprint(&stdout, "showpage\nend\n%%%%EOF\n");

	Bterm(&stdin);
	Bterm(&stdout);
	exits(nil);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.