uxn

Varvara Ordinator, written in ANSI C(SDL2)
git clone https://git.eamoncaddigan.net/uxn.git
Log | Files | Refs | README | LICENSE

commit c81650352bed650e10d017a1dd5e1db9a80b98e6
parent 99b75f2943cf265564a970eefe474bdcb6b49605
Author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
Date:   Wed, 23 Jun 2021 14:58:03 +0000

chr2img: port to Unix-like operating systems

Diffstat:
Mmkfile | 2+-
Msrc/chr2img.c | 106++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
2 files changed, 95 insertions(+), 13 deletions(-)

diff --git a/mkfile b/mkfile @@ -3,7 +3,7 @@ TARG=bin/uxncli bin/uxnasm bin/uxnemu bin/chr2img USM=`{walk -f projects/ | grep '\.tal$' | grep -v blank.tal} 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=\ /sys/include/npe/stdio.h\ src/devices/apu.h\ diff --git a/src/chr2img.c b/src/chr2img.c @@ -1,11 +1,46 @@ -/* note: this is for Plan 9 only */ +#ifdef __plan9__ #include <u.h> #include <libc.h> #include <draw.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 SZ; #define xy2n(x, y) ((y & 7) + (x/8 + y/8 * hor)*bpp*8) @@ -22,7 +57,7 @@ readall(int f, int *isz) bufsz *= 2; s = realloc(s, bufsz); } - if((n = readn(f, s+sz, bufsz-sz)) < 1) + if((n = read(f, s+sz, bufsz-sz)) < 1) break; } if(n < 0 || sz < 1){ @@ -40,12 +75,52 @@ getcoli(int x, int y, u8int *p) int ch1, ch2, r; r = xy2n(x, y); - ch1 = (p[r+0] >> (7 - x & 7)) & 1; - ch2 = bpp < 2 ? 0 : (p[r+8] >> (7 - x & 7)) & 1; + ch1 = (p[r+0] >> (7 - (x & 7))) & 1; + ch2 = bpp < 2 ? 0 : (p[r+8] >> (7 - (x & 7))) & 1; 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 usage(void) { @@ -53,16 +128,16 @@ usage(void) exits("usage"); } -void +int main(int argc, char **argv) { int sz, esz, h, w, x, y; Memimage *m; Memdata d; u8int *p; - ulong col[2][4] = { - {DWhite, DBlack, 0, 0}, - {DTransparent, DWhite, 0x72dec2ff, 0x666666ff}, + u32int col[2][4] = { + {0xffffffff, 0x000000ff, 0x000000ff, 0x000000ff}, + {0xffffff00, 0xffffffff, 0x72dec2ff, 0x666666ff}, }; ARGBEGIN{ @@ -75,10 +150,10 @@ main(int argc, char **argv) case 'w': hor = atoi(EARGF(usage())); break; + default: + usage(); }ARGEND - memimageinit(); - if((p = readall(0, &sz)) == nil) sysfatal("%r"); @@ -92,17 +167,24 @@ main(int argc, char **argv) memset(&d, 0, sizeof(d)); if((d.base = malloc(4*w*h)) == nil) sysfatal("memory"); - d.bdata = (uchar*)d.base; + d.bdata = (u8int*)d.base; for(y = 0; y < h; y++){ for(x = 0; x < w; x++) 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) sysfatal("%r"); if(writememimage(1, m) != 0) sysfatal("%r"); +#else + (void)m; + writebmp(w, h, d.base); +#endif exits(nil); + return 0; }