shithub: imgtools

Download patch

ref: d71d5fc296e01ce3c2189087c5ff190676b7564f
parent: 75872eff709f3d02bac21f1943f7432be7654ebb
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Wed Jan 20 04:03:29 EST 2021

forgot to add convert.c; update readme

--- a/README.md
+++ b/README.md
@@ -9,6 +9,8 @@
 save it as `b.img`.
 
 "Channels" (`-c` argument) are defined at `/sys/include/draw.h:133`.
+`rgb16`, `RGB16`, `r5g6b5` and `R5G6B5` mean the same thing
+(case-insensitive).
 
 ## resample
 
--- /dev/null
+++ b/convert.c
@@ -1,0 +1,72 @@
+#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);
+}