Browse code

chr2img: port to Unix-like operating systems

Sigrid Solveig Haflínudóttir authored on 23/06/2021 14:58:03
Showing 2 changed files
... ...
@@ -3,7 +3,7 @@
3 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
-CFLAGS=$CFLAGS -I/sys/include/npe -I/sys/include/npe/SDL2
6
+CFLAGS=$CFLAGS -D__plan9__ -I/sys/include/npe -I/sys/include/npe/SDL2
7 7
 HFILES=\
8 8
 	/sys/include/npe/stdio.h\
9 9
 	src/devices/apu.h\
... ...
@@ -1,11 +1,46 @@
1
-/* note: this is for Plan 9 only */
1
+#ifdef __plan9__
2 2
 #include <u.h>
3 3
 #include <libc.h>
4 4
 #include <draw.h>
5 5
 #include <memdraw.h>
6
+#else
7
+#include <stdio.h>
8
+#include <stdint.h>
9
+#include <string.h>
10
+#include <unistd.h>
11
+#include <stdlib.h>
12
+typedef uint8_t u8int;
13
+typedef uint32_t u32int;
14
+#define nil NULL
15
+typedef struct {
16
+}Memimage;
17
+typedef struct {
18
+	u32int *base;
19
+	u8int *bdata;
20
+}Memdata;
21
+static char *argv0;
22
+#define fprint(x, arg...) fprintf(stderr, arg)
23
+#define exits(s) exit(s == NULL ? 0 : 1)
24
+#define sysfatal(s) do{ fprintf(stderr, "error\n"); exit(1); }while(0)
25
+#define	ARGBEGIN \
26
+	for(((argv0=*argv)),argv++,argc--; \
27
+		argv[0] && argv[0][0]=='-' && argv[0][1]; \
28
+		argc--, argv++){ \
29
+			char *_args, _argc, *_argt; \
30
+			_args = &argv[0][1]; \
31
+			if(_args[0]=='-' && _args[1]==0){ \
32
+				argc--; argv++; break; \
33
+			} \
34
+			_argc = 0; \
35
+			while(*_args && (_argc = *_args++)) \
36
+			switch(_argc)
37
+#define	ARGEND };
38
+#define	EARGF(x)\
39
+	(_argt=_args, _args="",\
40
+				(*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0)))
41
+#endif
6 42
 
7 43
 static int hor = 44, ver = 26, bpp = 1;
8
-static int SZ;
9 44
 
10 45
 #define xy2n(x, y) ((y & 7) + (x/8 + y/8 * hor)*bpp*8)
11 46
 
... ...
@@ -22,7 +57,7 @@ readall(int f, int *isz)
22 57
 			bufsz *= 2;
23 58
 			s = realloc(s, bufsz);
24 59
 		}
25
-		if((n = readn(f, s+sz, bufsz-sz)) < 1)
60
+		if((n = read(f, s+sz, bufsz-sz)) < 1)
26 61
 			break;
27 62
 	}
28 63
 	if(n < 0 || sz < 1){
... ...
@@ -40,12 +75,52 @@ getcoli(int x, int y, u8int *p)
40 75
 	int ch1, ch2, r;
41 76
 
42 77
 	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;
78
+	ch1 = (p[r+0] >> (7 - (x & 7))) & 1;
79
+	ch2 = bpp < 2 ? 0 : (p[r+8] >> (7 - (x & 7))) & 1;
45 80
 
46 81
 	return ch2<<1 | ch1;
47 82
 }
48 83
 
84
+static int
85
+writebmp(int w, int h, u32int *p)
86
+{
87
+	u8int hd[14+40+4*4] = {
88
+		'B', 'M',
89
+		0, 0, 0, 0, /* file size */
90
+		0, 0,
91
+		0, 0,
92
+		14+40+4*4, 0, 0, 0, /* pixel data offset */
93
+		40+4*4, 0, 0, 0, /* BITMAPINFOHEADER */
94
+		w, w>>8, 0, 0, /* width */
95
+		h, h>>8, 0, 0, /* height */
96
+		1, 0, /* color planes */
97
+		32, 0, /* bpp = rgba */
98
+		3, 0, 0, 0, /* no compression */
99
+		0, 0, 0, 0, /* dummy raw size */
100
+		0, 0, 0, 0, /* dummy hor ppm */
101
+		0, 0, 0, 0, /* dummy ver ppm */
102
+		0, 0, 0, 0, /* dummy num of colors */
103
+		0, 0, 0, 0, /* dummy important colors */
104
+		0, 0, 0, 0xff,
105
+		0, 0, 0xff, 0,
106
+		0, 0xff, 0, 0,
107
+		0xff, 0, 0, 0,
108
+	};
109
+	int sz;
110
+
111
+	sz = 14+40+4*4 + 4*w*h;
112
+	hd[2] = sz;
113
+	hd[3] = sz>>8;
114
+	hd[4] = sz>>16;
115
+	hd[5] = sz>>24;
116
+
117
+	write(1, hd, sizeof(hd));
118
+	while(h-- >= 0)
119
+		write(1, p+w*h, 4*w);
120
+
121
+	return 0;
122
+}
123
+
49 124
 static void
50 125
 usage(void)
51 126
 {
... ...
@@ -53,16 +128,16 @@ usage(void)
53 128
 	exits("usage");
54 129
 }
55 130
 
56
-void
131
+int
57 132
 main(int argc, char **argv)
58 133
 {
59 134
 	int sz, esz, h, w, x, y;
60 135
 	Memimage *m;
61 136
 	Memdata d;
62 137
 	u8int *p;
63
-	ulong col[2][4] = {
64
-		{DWhite, DBlack, 0, 0},
65
-		{DTransparent, DWhite, 0x72dec2ff, 0x666666ff},
138
+	u32int col[2][4] = {
139
+		{0xffffffff, 0x000000ff, 0x000000ff, 0x000000ff},
140
+		{0xffffff00, 0xffffffff, 0x72dec2ff, 0x666666ff},
66 141
 	};
67 142
 
68 143
 	ARGBEGIN{
... ...
@@ -75,10 +150,10 @@ main(int argc, char **argv)
75 150
 	case 'w':
76 151
 		hor = atoi(EARGF(usage()));
77 152
 		break;
153
+	default:
154
+		usage();
78 155
 	}ARGEND
79 156
 
80
-	memimageinit();
81
-
82 157
 	if((p = readall(0, &sz)) == nil)
83 158
 		sysfatal("%r");
84 159
 
... ...
@@ -92,17 +167,24 @@ main(int argc, char **argv)
92 167
 	memset(&d, 0, sizeof(d));
93 168
 	if((d.base = malloc(4*w*h)) == nil)
94 169
 		sysfatal("memory");
95
-	d.bdata = (uchar*)d.base;
170
+	d.bdata = (u8int*)d.base;
96 171
 
97 172
 	for(y = 0; y < h; y++){
98 173
 		for(x = 0; x < w; x++)
99 174
 			d.base[y*w + x] = col[bpp-1][getcoli(x, y, p)];
100 175
 	}
101 176
 
177
+#ifdef __plan9__
178
+	memimageinit();
102 179
 	if((m = allocmemimaged(Rect(0, 0, w, h), RGBA32, &d)) == nil)
103 180
 		sysfatal("%r");
104 181
 	if(writememimage(1, m) != 0)
105 182
 		sysfatal("%r");
183
+#else
184
+	(void)m;
185
+	writebmp(w, h, d.base);
186
+#endif
106 187
 
107 188
 	exits(nil);
189
+	return 0;
108 190
 }