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

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


implement Bit2mask;

include "draw.m";
include "sys.m";

Bit2mask: 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;

	Image, Display, Rect, Point : import draw;

	display := ctxt.display;
	img := display.readimage(sys->fildes(0));
	if (img == nil) {
		sys->fprint(stderr, "bit2mask: cannot read image: %r\n");
		raise "fail:error";
	}
	if (img.ldepth != 3) {
		sys->fprint(stderr, "bit2mask: can only deal with 8 bit images\n");
		raise "fail:error";
	}
	bits := array[img.r.dx() * img.r.dy()] of byte;
	img.readpixels(img.r, bits);

	for (i := 0; i < len bits; i++)
		if (bits[i] != byte 0)
			bits[i] = byte 255;

	img.writepixels(img.r, bits);

	mask := display.newimage(img.r, 0, 0, 0);
	mask.draw(mask.r, img, display.ones, img.r.min);

	display.writeimage(sys->fildes(1), mask);
}

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.