Plan 9 from Bell Labs’s /usr/web/sources/contrib/rog/sh-examples/draw/mkcomplete.b

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


implement Mkcomplete;

# join many cards together to make a complete
# image that can be read substantially faster.

include "sys.m";
	sys: Sys;
include "draw.m";
	draw: Draw;
	Rect, Point, Display, Image: import draw;

Mkcomplete: module {
	init: fn(ctxt: ref Draw->Context, argv: list of string);
};

stderr: ref Sys->FD;

# usage: mkcomplete prefix

init(ctxt: ref Draw->Context, argv: list of string)
{
	sys = load Sys Sys->PATH;
	draw = load Draw Draw->PATH;
	stderr = sys->fildes(2);

	if (len argv != 2)
		error("usage: mkcomplete prefix");
	if (ctxt == nil)
		error("mkcomplete: draw context required");
	displ := ctxt.display;
	prefix := hd tl argv;

	img := displ.open(prefix + "0.bit");
	if (img == nil)
		error("mkcomplete: no images found");
	cardsize := dxy(img.r);
	chans := img.chans;
	imgs := img :: nil;
	for (i := 1; ; i++) {
		img = displ.open(prefix + string i + ".bit");
		if (img == nil)
			break;
		if (!dxy(img.r).eq(cardsize))
			sys->fprint(stderr, "mkcomplete: warning image size inconsistent in %s%d.bit\n", prefix, i);
		imgs = img :: imgs;
	}
	n := i;
	complete := displ.newimage(((0, 0), (cardsize.x * n, cardsize.y)), chans, 0, 0);
	for (i = n - 1; i >= 0; i--) {
		(img, imgs) = (hd imgs, tl imgs);
		r := ((i * cardsize.x, 0), ((i + 1) * cardsize.x, cardsize.y));
		complete.clipr = r;
		complete.draw(r, img, nil, img.r.min);
	}
	complete.clipr = complete.r;
	fd := sys->create(prefix + ".all.bit", Sys->OWRITE, 8r644);
	if (fd == nil)
		error(sys->sprint("mkcomplete: cannot create %s: %r", prefix + ".all.bit"));
	displ.writeimage(fd, complete);
	sys->print("cardsize %d %d\n", cardsize.x, cardsize.y);
}

error(e: string)
{
	sys->fprint(stderr, "%s\n", e);
	raise "fail:error";
}

dxy(r: Rect): Point
{
	return (r.max.x - r.min.x, r.max.y - r.min.y);
}

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.