Plan 9 from Bell Labs’s /usr/web/sources/extra/i/dumpicon.c

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


// dump an uncompressed plan9 bitmap in a format
// suitable for including in icons.c.
// Use, e.g., gif -9 file.gif > file.bit
// to get a suitable input file
#include <u.h>
#include <libc.h>

static void usage(void);
static void dumparray(char*, int, uchar*, int, int);

void
main(int argc, char* argv[])
{
	char *name;
	int f;
	ulong n;
	Dir statbuf;
	uchar* buf;
	int tflag = 0;
	int tindex = 0;
	int x0, y0, x1, y1, w, h, nb;

	ARGBEGIN{
	case 't':
		tflag = 1;
		tindex = strtol(ARGF(), nil, 0);
		break;
	default:
		usage();
	}ARGEND
	if(argc == 0)
		usage();
	if((f = open(name = *argv, OREAD)) == -1){
		perror(name);
		exits("open");
	}
	if(dirfstat(f, &statbuf) == -1){
		perror(name);
		exits("stat");
	}
	n = (ulong)statbuf.length;
	if(n < 5*12){
		print("file too short");
		exits("file length");
	}
	buf = (uchar*)malloc(n);
	if(buf == nil){
		print("can't malloc %u bytes\n", n);
		exits("malloc");
	}
	if(readn(f, buf, n) != n) {
		perror(name);
		exits("read");
	}
	if(!(buf[9] == 'm' && buf[10] == '8')) {
		print("wrong file format");
		exits("format");
	}
	x0 = strtol((char*)buf+1*12, nil, 10);
	y0 = strtol((char*)buf+2*12, nil, 10);
	x1 = strtol((char*)buf+3*12, nil, 10);
	y1 = strtol((char*)buf+4*12, nil, 10);
	w = x1-x0;
	h = y1-y0;
	nb = w*h;
	if(n != 5*12 + nb){
		print("wrong file length: %d, expected %d", n, 5*12 + nb);
		exits("length");
	}
	print("#define iconwidth %d\n", w);
	print("#define iconheight %d\n", h);
	dumparray("icondata", nb, buf+5*12, 0, 0);
	if(tflag)
		dumparray("iconmask", nb, buf+5*12, 1, tindex);
}

static void
dumparray(char* name, int n, uchar* buf, int tflag, int tindex)
{
	int i, v;

	print("static uchar %s[%d] = {", name, n);
	for(i = 0; i < n; i++) {
		if((i%8)==0)
			print("\n\t");
		v = buf[i];
		if(tflag)
			v = (v == tindex)? 0 : 255;
		print("%d, ", v);
	}
	print("\n};\n");
}

static void
usage()
{
	print("usage: dumpicon [-t transparentindex] plan9bitmapfile\n");
	exits("usage");
}

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.