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

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


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

stderr: ref Sys->FD;

# usage: draw xpos ypos img.bit [xpos ypos img.bit]...

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

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

	argv = tl argv;
	if (argv == nil || len argv % 3 != 0) {
		sys->fprint(stderr, "usage: draw xpos ypos img.bit [xpos ypos img.bit]...\n");
		raise "fail:usage";
	}
	displ := ctxt.display;
	base := displ.readimage(sys->fildes(0));
	if (base == nil) {
		sys->fprint(stderr, "draw: cannot read base image: %r\n");
		raise "fail:error";
	}
	for (; argv != nil; argv = tl tl tl argv) {
		(xpos, ypos, imgfile) := (int hd argv, int hd tl argv, hd tl tl argv);
		img := displ.open(imgfile);
		if (img == nil) {
			sys->fprint(stderr, "draw: cannot open %s: %r\n", imgfile);
			raise "fail:error";
		}
		r := Rect((xpos, ypos), (xpos + img.r.dx(), ypos + img.r.dy()));
		base.draw(r, img, nil, img.r.min);
	}
	displ.writeimage(sys->fildes(1), base);
}

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.