chr2img: port to Unix-like operating systems

This commit is contained in:
Sigrid Solveig Haflínudóttir 2021-06-23 14:58:03 +00:00
parent 99b75f2943
commit c81650352b
2 changed files with 95 additions and 13 deletions

2
mkfile
View File

@ -3,7 +3,7 @@
TARG=bin/uxncli bin/uxnasm bin/uxnemu bin/chr2img TARG=bin/uxncli bin/uxnasm bin/uxnemu bin/chr2img
USM=`{walk -f projects/ | grep '\.tal$' | grep -v blank.tal} USM=`{walk -f projects/ | grep '\.tal$' | grep -v blank.tal}
ROM=${USM:%.tal=%.rom} ROM=${USM:%.tal=%.rom}
CFLAGS=$CFLAGS -I/sys/include/npe -I/sys/include/npe/SDL2 CFLAGS=$CFLAGS -D__plan9__ -I/sys/include/npe -I/sys/include/npe/SDL2
HFILES=\ HFILES=\
/sys/include/npe/stdio.h\ /sys/include/npe/stdio.h\
src/devices/apu.h\ src/devices/apu.h\

View File

@ -1,11 +1,46 @@
/* note: this is for Plan 9 only */ #ifdef __plan9__
#include <u.h> #include <u.h>
#include <libc.h> #include <libc.h>
#include <draw.h> #include <draw.h>
#include <memdraw.h> #include <memdraw.h>
#else
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
typedef uint8_t u8int;
typedef uint32_t u32int;
#define nil NULL
typedef struct {
}Memimage;
typedef struct {
u32int *base;
u8int *bdata;
}Memdata;
static char *argv0;
#define fprint(x, arg...) fprintf(stderr, arg)
#define exits(s) exit(s == NULL ? 0 : 1)
#define sysfatal(s) do{ fprintf(stderr, "error\n"); exit(1); }while(0)
#define ARGBEGIN \
for(((argv0=*argv)),argv++,argc--; \
argv[0] && argv[0][0]=='-' && argv[0][1]; \
argc--, argv++){ \
char *_args, _argc, *_argt; \
_args = &argv[0][1]; \
if(_args[0]=='-' && _args[1]==0){ \
argc--; argv++; break; \
} \
_argc = 0; \
while(*_args && (_argc = *_args++)) \
switch(_argc)
#define ARGEND };
#define EARGF(x)\
(_argt=_args, _args="",\
(*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0)))
#endif
static int hor = 44, ver = 26, bpp = 1; static int hor = 44, ver = 26, bpp = 1;
static int SZ;
#define xy2n(x, y) ((y & 7) + (x/8 + y/8 * hor)*bpp*8) #define xy2n(x, y) ((y & 7) + (x/8 + y/8 * hor)*bpp*8)
@ -22,7 +57,7 @@ readall(int f, int *isz)
bufsz *= 2; bufsz *= 2;
s = realloc(s, bufsz); s = realloc(s, bufsz);
} }
if((n = readn(f, s+sz, bufsz-sz)) < 1) if((n = read(f, s+sz, bufsz-sz)) < 1)
break; break;
} }
if(n < 0 || sz < 1){ if(n < 0 || sz < 1){
@ -40,12 +75,52 @@ getcoli(int x, int y, u8int *p)
int ch1, ch2, r; int ch1, ch2, r;
r = xy2n(x, y); r = xy2n(x, y);
ch1 = (p[r+0] >> (7 - x & 7)) & 1; ch1 = (p[r+0] >> (7 - (x & 7))) & 1;
ch2 = bpp < 2 ? 0 : (p[r+8] >> (7 - x & 7)) & 1; ch2 = bpp < 2 ? 0 : (p[r+8] >> (7 - (x & 7))) & 1;
return ch2<<1 | ch1; return ch2<<1 | ch1;
} }
static int
writebmp(int w, int h, u32int *p)
{
u8int hd[14+40+4*4] = {
'B', 'M',
0, 0, 0, 0, /* file size */
0, 0,
0, 0,
14+40+4*4, 0, 0, 0, /* pixel data offset */
40+4*4, 0, 0, 0, /* BITMAPINFOHEADER */
w, w>>8, 0, 0, /* width */
h, h>>8, 0, 0, /* height */
1, 0, /* color planes */
32, 0, /* bpp = rgba */
3, 0, 0, 0, /* no compression */
0, 0, 0, 0, /* dummy raw size */
0, 0, 0, 0, /* dummy hor ppm */
0, 0, 0, 0, /* dummy ver ppm */
0, 0, 0, 0, /* dummy num of colors */
0, 0, 0, 0, /* dummy important colors */
0, 0, 0, 0xff,
0, 0, 0xff, 0,
0, 0xff, 0, 0,
0xff, 0, 0, 0,
};
int sz;
sz = 14+40+4*4 + 4*w*h;
hd[2] = sz;
hd[3] = sz>>8;
hd[4] = sz>>16;
hd[5] = sz>>24;
write(1, hd, sizeof(hd));
while(h-- >= 0)
write(1, p+w*h, 4*w);
return 0;
}
static void static void
usage(void) usage(void)
{ {
@ -53,16 +128,16 @@ usage(void)
exits("usage"); exits("usage");
} }
void int
main(int argc, char **argv) main(int argc, char **argv)
{ {
int sz, esz, h, w, x, y; int sz, esz, h, w, x, y;
Memimage *m; Memimage *m;
Memdata d; Memdata d;
u8int *p; u8int *p;
ulong col[2][4] = { u32int col[2][4] = {
{DWhite, DBlack, 0, 0}, {0xffffffff, 0x000000ff, 0x000000ff, 0x000000ff},
{DTransparent, DWhite, 0x72dec2ff, 0x666666ff}, {0xffffff00, 0xffffffff, 0x72dec2ff, 0x666666ff},
}; };
ARGBEGIN{ ARGBEGIN{
@ -75,10 +150,10 @@ main(int argc, char **argv)
case 'w': case 'w':
hor = atoi(EARGF(usage())); hor = atoi(EARGF(usage()));
break; break;
default:
usage();
}ARGEND }ARGEND
memimageinit();
if((p = readall(0, &sz)) == nil) if((p = readall(0, &sz)) == nil)
sysfatal("%r"); sysfatal("%r");
@ -92,17 +167,24 @@ main(int argc, char **argv)
memset(&d, 0, sizeof(d)); memset(&d, 0, sizeof(d));
if((d.base = malloc(4*w*h)) == nil) if((d.base = malloc(4*w*h)) == nil)
sysfatal("memory"); sysfatal("memory");
d.bdata = (uchar*)d.base; d.bdata = (u8int*)d.base;
for(y = 0; y < h; y++){ for(y = 0; y < h; y++){
for(x = 0; x < w; x++) for(x = 0; x < w; x++)
d.base[y*w + x] = col[bpp-1][getcoli(x, y, p)]; d.base[y*w + x] = col[bpp-1][getcoli(x, y, p)];
} }
#ifdef __plan9__
memimageinit();
if((m = allocmemimaged(Rect(0, 0, w, h), RGBA32, &d)) == nil) if((m = allocmemimaged(Rect(0, 0, w, h), RGBA32, &d)) == nil)
sysfatal("%r"); sysfatal("%r");
if(writememimage(1, m) != 0) if(writememimage(1, m) != 0)
sysfatal("%r"); sysfatal("%r");
#else
(void)m;
writebmp(w, h, d.base);
#endif
exits(nil); exits(nil);
return 0;
} }