format chr2img.c, weirdly

This commit is contained in:
Sigrid Solveig Haflínudóttir 2021-06-23 17:33:14 +02:00
parent bdde0cc43e
commit 62d809147b
2 changed files with 54 additions and 41 deletions

View File

@ -12,6 +12,7 @@ clang-format -i src/devices/mpu.c
clang-format -i src/uxnasm.c clang-format -i src/uxnasm.c
clang-format -i src/uxnemu.c clang-format -i src/uxnemu.c
clang-format -i src/uxncli.c clang-format -i src/uxncli.c
clang-format -i src/chr2img.c
echo "Cleaning.." echo "Cleaning.."
rm -f ./bin/uxnasm rm -f ./bin/uxnasm

View File

@ -13,36 +13,44 @@ typedef uint8_t u8int;
typedef uint32_t u32int; typedef uint32_t u32int;
#define nil NULL #define nil NULL
typedef struct { typedef struct {
}Memimage; } Memimage;
typedef struct { typedef struct {
u32int *base; u32int *base;
u8int *bdata; u8int *bdata;
}Memdata; } Memdata;
static char *argv0; static char *argv0;
#define fprint(x, arg...) fprintf(stderr, arg) #define fprint(x, arg...) fprintf(stderr, arg)
#define exits(s) exit(s == NULL ? 0 : 1) #define exits(s) exit(s == NULL ? 0 : 1)
#define sysfatal(s) do{ fprintf(stderr, "error\n"); exit(1); }while(0) #define sysfatal(s) \
#define ARGBEGIN \ do { \
for(((argv0=*argv)),argv++,argc--; \ fprintf(stderr, "error\n"); \
argv[0] && argv[0][0]=='-' && argv[0][1]; \ exit(1); \
argc--, argv++){ \ } while(0)
char *_args, _argc, *_argt; \ #define ARGBEGIN \
_args = &argv[0][1]; \ for(((argv0 = *argv)), argv++, argc--; \
if(_args[0]=='-' && _args[1]==0){ \ argv[0] && argv[0][0] == '-' && argv[0][1]; \
argc--; argv++; break; \ argc--, argv++) { \
} \ char *_args, _argc, *_argt; \
_argc = 0; \ _args = &argv[0][1]; \
while(*_args && (_argc = *_args++)) \ if(_args[0] == '-' && _args[1] == 0) { \
argc--; \
argv++; \
break; \
} \
_argc = 0; \
while(*_args && (_argc = *_args++)) \
switch(_argc) switch(_argc)
#define ARGEND }; #define ARGEND \
#define EARGF(x)\ } \
(_argt=_args, _args="",\ ;
(*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0))) #define EARGF(x) \
(_argt = _args, _args = "", (*_argt ? _argt : argv[1] ? (argc--, *++argv) \
: ((x), abort(), (char *)0)))
#endif #endif
static int hor = 44, ver = 26, bpp = 1; static int hor = 44, ver = 26, bpp = 1;
#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)
static u8int * static u8int *
readall(int f, int *isz) readall(int f, int *isz)
@ -52,15 +60,15 @@ readall(int f, int *isz)
bufsz = 1023; bufsz = 1023;
s = nil; s = nil;
for(sz = 0;; sz += n){ for(sz = 0;; sz += n) {
if(bufsz-sz < 1024){ if(bufsz - sz < 1024) {
bufsz *= 2; bufsz *= 2;
s = realloc(s, bufsz); s = realloc(s, bufsz);
} }
if((n = read(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) {
free(s); free(s);
return nil; return nil;
} }
@ -75,15 +83,16 @@ 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 static int
writebmp(int w, int h, u32int *p) writebmp(int w, int h, u32int *p)
{ {
/* clang-format off */
u8int hd[14+40+4*4] = { u8int hd[14+40+4*4] = {
'B', 'M', 'B', 'M',
0, 0, 0, 0, /* file size */ 0, 0, 0, 0, /* file size */
@ -107,16 +116,17 @@ writebmp(int w, int h, u32int *p)
0xff, 0, 0, 0, 0xff, 0, 0, 0,
}; };
int sz; int sz;
/* clang-format on */
sz = 14+40+4*4 + 4*w*h; sz = 14 + 40 + 4 * 4 + 4 * w * h;
hd[2] = sz; hd[2] = sz;
hd[3] = sz>>8; hd[3] = sz >> 8;
hd[4] = sz>>16; hd[4] = sz >> 16;
hd[5] = sz>>24; hd[5] = sz >> 24;
write(1, hd, sizeof(hd)); write(1, hd, sizeof(hd));
while(h-- >= 0) while(h-- >= 0)
write(1, p+w*h, 4*w); write(1, p + w * h, 4 * w);
return 0; return 0;
} }
@ -140,7 +150,8 @@ main(int argc, char **argv)
{0xffffff00, 0xffffffff, 0x72dec2ff, 0x666666ff}, {0xffffff00, 0xffffffff, 0x72dec2ff, 0x666666ff},
}; };
ARGBEGIN{ ARGBEGIN
{
case '1': case '1':
bpp = 1; bpp = 1;
break; break;
@ -152,26 +163,27 @@ main(int argc, char **argv)
break; break;
default: default:
usage(); usage();
}ARGEND }
ARGEND
if((p = readall(0, &sz)) == nil) if((p = readall(0, &sz)) == nil)
sysfatal("%r"); sysfatal("%r");
ver = sz / (bpp*8) / hor; ver = sz / (bpp * 8) / hor;
esz = (hor * ver * (bpp*8)); esz = (hor * ver * (bpp * 8));
w = hor*8; w = hor * 8;
h = ver*8; h = ver * 8;
if(sz != esz) if(sz != esz)
fprint(2, "warning: size differs (%d vs %d), dimensions must be wrong\n", sz, esz); fprint(2, "warning: size differs (%d vs %d), dimensions must be wrong\n", sz, esz);
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 = (u8int*)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__ #ifdef __plan9__