Browse code

add chr2img for Plan 9

Sigrid Solveig Haflínudóttir authored on 23/06/2021 13:45:02
Showing 2 changed files
... ...
@@ -1,6 +1,6 @@
1 1
 </$objtype/mkfile
2 2
 
3
-TARG=bin/uxncli bin/uxnasm bin/uxnemu
3
+TARG=bin/uxncli bin/uxnasm bin/uxnemu bin/chr2img
4 4
 USM=`{walk -f projects/ | grep '\.tal$' | grep -v blank.tal}
5 5
 ROM=${USM:%.tal=%.rom}
6 6
 CFLAGS=$CFLAGS -I/sys/include/npe -I/sys/include/npe/SDL2
... ...
@@ -38,7 +38,10 @@ bin/uxnasm: uxnasm.$O
38 38
 bin/uxnemu: uxnemu.$O apu.$O mpu.$O ppu.$O uxn.$O
39 39
 	$LD $LDFLAGS -o $target $prereq
40 40
 
41
-(uxnasm|uxncli|uxnemu|uxn)\.$O:R: src/\1.c
41
+bin/chr2img: chr2img.$O
42
+	$LD $LDFLAGS -o $target $prereq
43
+
44
+(uxnasm|uxncli|uxnemu|uxn|chr2img)\.$O:R: src/\1.c
42 45
 	$CC $CFLAGS -Isrc -o $target src/$stem1.c
43 46
 
44 47
 (apu|mpu|ppu)\.$O:R: src/devices/\1.c
45 48
new file mode 100644
... ...
@@ -0,0 +1,108 @@
1
+/* note: this is for Plan 9 only */
2
+#include <u.h>
3
+#include <libc.h>
4
+#include <draw.h>
5
+#include <memdraw.h>
6
+
7
+static int hor = 44, ver = 26, bpp = 1;
8
+static int SZ;
9
+
10
+#define xy2n(x, y) ((y & 7) + (x/8 + y/8 * hor)*bpp*8)
11
+
12
+static u8int *
13
+readall(int f, int *isz)
14
+{
15
+	int bufsz, sz, n;
16
+	u8int *s;
17
+
18
+	bufsz = 1023;
19
+	s = nil;
20
+	for(sz = 0;; sz += n){
21
+		if(bufsz-sz < 1024){
22
+			bufsz *= 2;
23
+			s = realloc(s, bufsz);
24
+		}
25
+		if((n = readn(f, s+sz, bufsz-sz)) < 1)
26
+			break;
27
+	}
28
+	if(n < 0 || sz < 1){
29
+		free(s);
30
+		return nil;
31
+	}
32
+	*isz = sz;
33
+
34
+	return s;
35
+}
36
+
37
+static int
38
+getcoli(int x, int y, u8int *p)
39
+{
40
+	int ch1, ch2, r;
41
+
42
+	r = xy2n(x, y);
43
+	ch1 = (p[r+0] >> (7 - x & 7)) & 1;
44
+	ch2 = bpp < 2 ? 0 : (p[r+8] >> (7 - x & 7)) & 1;
45
+
46
+	return ch2<<1 | ch1;
47
+}
48
+
49
+static void
50
+usage(void)
51
+{
52
+	fprint(2, "usage: %s [-1] [-2] [-w WIDTH]\n", argv0);
53
+	exits("usage");
54
+}
55
+
56
+void
57
+main(int argc, char **argv)
58
+{
59
+	int sz, esz, h, w, x, y;
60
+	Memimage *m;
61
+	Memdata d;
62
+	u8int *p;
63
+	ulong col[2][4] = {
64
+		{DWhite, DBlack, 0, 0},
65
+		{DTransparent, DWhite, 0x72dec2ff, 0x666666ff},
66
+	};
67
+
68
+	ARGBEGIN{
69
+	case '1':
70
+		bpp = 1;
71
+		break;
72
+	case '2':
73
+		bpp = 2;
74
+		break;
75
+	case 'w':
76
+		hor = atoi(EARGF(usage()));
77
+		break;
78
+	}ARGEND
79
+
80
+	memimageinit();
81
+
82
+	if((p = readall(0, &sz)) == nil)
83
+		sysfatal("%r");
84
+
85
+	ver = sz / (bpp*8) / hor;
86
+	esz = (hor * ver * (bpp*8));
87
+	w = hor*8;
88
+	h = ver*8;
89
+	if(sz != esz)
90
+		fprint(2, "warning: size differs (%d vs %d), dimensions must be wrong\n", sz, esz);
91
+
92
+	memset(&d, 0, sizeof(d));
93
+	if((d.base = malloc(4*w*h)) == nil)
94
+		sysfatal("memory");
95
+	d.bdata = (uchar*)d.base;
96
+
97
+	for(y = 0; y < h; y++){
98
+		for(x = 0; x < w; x++)
99
+			d.base[y*w + x] = col[bpp-1][getcoli(x, y, p)];
100
+	}
101
+
102
+	if((m = allocmemimaged(Rect(0, 0, w, h), RGBA32, &d)) == nil)
103
+		sysfatal("%r");
104
+	if(writememimage(1, m) != 0)
105
+		sysfatal("%r");
106
+
107
+	exits(nil);
108
+}