uxn

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

commit 62d809147bdcd0cf3c3a4ac076553876c0565adb
parent bdde0cc43e51f98ead0f4ec196e6b2adb23cfa59
Author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
Date:   Wed, 23 Jun 2021 17:33:14 +0200

format chr2img.c, weirdly

Diffstat:
Mbuild.sh | 1+
Msrc/chr2img.c | 94++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
2 files changed, 54 insertions(+), 41 deletions(-)

diff --git a/build.sh b/build.sh @@ -12,6 +12,7 @@ clang-format -i src/devices/mpu.c clang-format -i src/uxnasm.c clang-format -i src/uxnemu.c clang-format -i src/uxncli.c +clang-format -i src/chr2img.c echo "Cleaning.." rm -f ./bin/uxnasm diff --git a/src/chr2img.c b/src/chr2img.c @@ -13,36 +13,44 @@ typedef uint8_t u8int; typedef uint32_t u32int; #define nil NULL typedef struct { -}Memimage; +} Memimage; typedef struct { u32int *base; u8int *bdata; -}Memdata; +} 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++)) \ +#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))) +#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; -#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 * readall(int f, int *isz) @@ -52,15 +60,15 @@ readall(int f, int *isz) bufsz = 1023; s = nil; - for(sz = 0;; sz += n){ - if(bufsz-sz < 1024){ + for(sz = 0;; sz += n) { + if(bufsz - sz < 1024) { bufsz *= 2; s = realloc(s, bufsz); } - if((n = read(f, s+sz, bufsz-sz)) < 1) + if((n = read(f, s + sz, bufsz - sz)) < 1) break; } - if(n < 0 || sz < 1){ + if(n < 0 || sz < 1) { free(s); return nil; } @@ -75,15 +83,16 @@ 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; + return ch2 << 1 | ch1; } static int writebmp(int w, int h, u32int *p) { + /* clang-format off */ u8int hd[14+40+4*4] = { 'B', 'M', 0, 0, 0, 0, /* file size */ @@ -107,16 +116,17 @@ writebmp(int w, int h, u32int *p) 0xff, 0, 0, 0, }; 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[3] = sz>>8; - hd[4] = sz>>16; - hd[5] = sz>>24; + 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); + write(1, p + w * h, 4 * w); return 0; } @@ -140,7 +150,8 @@ main(int argc, char **argv) {0xffffff00, 0xffffffff, 0x72dec2ff, 0x666666ff}, }; - ARGBEGIN{ + ARGBEGIN + { case '1': bpp = 1; break; @@ -152,26 +163,27 @@ main(int argc, char **argv) break; default: usage(); - }ARGEND + } + ARGEND if((p = readall(0, &sz)) == nil) sysfatal("%r"); - ver = sz / (bpp*8) / hor; - esz = (hor * ver * (bpp*8)); - w = hor*8; - h = ver*8; + ver = sz / (bpp * 8) / hor; + esz = (hor * ver * (bpp * 8)); + w = hor * 8; + h = ver * 8; if(sz != esz) fprint(2, "warning: size differs (%d vs %d), dimensions must be wrong\n", sz, esz); memset(&d, 0, sizeof(d)); - if((d.base = malloc(4*w*h)) == nil) + if((d.base = malloc(4 * w * h)) == nil) 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++) - 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__