shithub: imgtools

ref: 172c1e097c06d3a707992034cae962f513c1c73c
dir: /convert.c/

View raw version
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <memdraw.h>

static struct {
	char *name[2];
	ulong chan;
}chans[] = {
	{{"grey1", "k1"}, GREY1},
	{{"grey2", "k2"}, GREY2},
	{{"grey4", "k4"}, GREY4},
	{{"grey8", "k8"}, GREY8},
	{{"cmap8", "m8"}, CMAP8},
	{{"rgb15", "x1r5g5b5"}, RGB15},
	{{"rgb16", "r5g6b5"}, RGB16},
	{{"rgb24", "r8g8b8"}, RGB24},
	{{"rgba32", "r8g8b8a8"}, RGBA32},
	{{"argb32", "a8r8g8b8"}, ARGB32},
	{{"xrgb32", "x8r8g8b8"}, XRGB32},
	{{"bgr24", "b8g8r8"}, BGR24},
	{{"abgr32", "a8b8g8r8"}, ABGR32},
	{{"xbgr32", "x8b8g8r8"}, XBGR32},
};

static void
usage(void)
{
	fprint(2, "usage: %s [-c CHAN]\n", argv0);
	exits("usage");
}

void
main(int argc, char **argv)
{
	Memimage *a, *b;
	char *s;
	int ci;

	ci = -1;
	ARGBEGIN{
	case 'c':
		s = EARGF(usage());
		for(ci = 0;
		    ci < nelem(chans) &&
		    cistrcmp(chans[ci].name[0], s) != 0 &&
		    cistrcmp(chans[ci].name[1], s) != 0;
		    ci++)
		;
		if(ci >= nelem(chans)){
			fprint(2, "invalid chan %s\n", s);
			sysfatal("chan");
		}
		break;
	default:
		usage();
	}ARGEND

	memimageinit();
	if((a = readmemimage(0)) == nil)
		sysfatal("memory");
	if(ci >= 0 && a->chan != chans[ci].chan){
		if((b = allocmemimage(a->r, chans[ci].chan)) == nil)
			sysfatal("memory");
		memimagedraw(b, a->r, a, ZP, nil, ZP, S);
		freememimage(a);
		a = b;
	}
	writememimage(1, a);

	exits(nil);
}