diff options
author | June McEnroe <june@causal.agency> | 2018-05-13 00:41:58 -0400 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2018-05-13 00:42:28 -0400 |
commit | 869f6622bbb883257a0eb96742752b9969cfb0be (patch) | |
tree | 7d2b2ff7b0c4dd5395bfc23585d27fe9052802a9 /bin/gfx | |
parent | Tabify shell scripts (diff) | |
download | src-869f6622bbb883257a0eb96742752b9969cfb0be.tar.gz src-869f6622bbb883257a0eb96742752b9969cfb0be.zip |
Tabify bin
Diffstat (limited to '')
-rw-r--r-- | bin/gfx/cocoa.m | 196 | ||||
-rw-r--r-- | bin/gfx/fb.c | 76 | ||||
-rw-r--r-- | bin/gfx/none.c | 2 | ||||
-rw-r--r-- | bin/gfx/x11.c | 158 | ||||
-rw-r--r-- | bin/gfxx.c | 692 |
5 files changed, 562 insertions, 562 deletions
diff --git a/bin/gfx/cocoa.m b/bin/gfx/cocoa.m index 495b1f9f..3df6c62a 100644 --- a/bin/gfx/cocoa.m +++ b/bin/gfx/cocoa.m @@ -26,78 +26,78 @@ #define UNUSED __attribute__((unused)) @interface BufferView : NSView { - size_t bufSize; - uint32_t *buf; - CGColorSpaceRef colorSpace; - CGDataProviderRef dataProvider; + size_t bufSize; + uint32_t *buf; + CGColorSpaceRef colorSpace; + CGDataProviderRef dataProvider; } @end @implementation BufferView - (instancetype) initWithFrame: (NSRect) frameRect { - colorSpace = CGColorSpaceCreateDeviceRGB(); - return [super initWithFrame: frameRect]; + colorSpace = CGColorSpaceCreateDeviceRGB(); + return [super initWithFrame: frameRect]; } - (void) setWindowTitle { - [[self window] setTitle: [NSString stringWithUTF8String: status()]]; + [[self window] setTitle: [NSString stringWithUTF8String: status()]]; } - (void) draw { - draw(buf, [self frame].size.width, [self frame].size.height); - [self setNeedsDisplay: YES]; + draw(buf, [self frame].size.width, [self frame].size.height); + [self setNeedsDisplay: YES]; } - (void) setFrameSize: (NSSize) newSize { - [super setFrameSize: newSize]; - size_t newBufSize = 4 * newSize.width * newSize.height; - if (newBufSize > bufSize) { - bufSize = newBufSize; - buf = malloc(bufSize); - if (!buf) err(EX_OSERR, "malloc(%zu)", bufSize); - CGDataProviderRelease(dataProvider); - dataProvider = CGDataProviderCreateWithData(NULL, buf, bufSize, NULL); - } - [self draw]; + [super setFrameSize: newSize]; + size_t newBufSize = 4 * newSize.width * newSize.height; + if (newBufSize > bufSize) { + bufSize = newBufSize; + buf = malloc(bufSize); + if (!buf) err(EX_OSERR, "malloc(%zu)", bufSize); + CGDataProviderRelease(dataProvider); + dataProvider = CGDataProviderCreateWithData(NULL, buf, bufSize, NULL); + } + [self draw]; } - (void) drawRect: (NSRect) UNUSED dirtyRect { - NSSize size = [self frame].size; - CGContextRef ctx = [[NSGraphicsContext currentContext] CGContext]; - CGImageRef image = CGImageCreate( - size.width, size.height, - 8, 32, 4 * size.width, - colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst, - dataProvider, - NULL, false, kCGRenderingIntentDefault - ); - CGContextDrawImage(ctx, [self frame], image); - CGImageRelease(image); + NSSize size = [self frame].size; + CGContextRef ctx = [[NSGraphicsContext currentContext] CGContext]; + CGImageRef image = CGImageCreate( + size.width, size.height, + 8, 32, 4 * size.width, + colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst, + dataProvider, + NULL, false, kCGRenderingIntentDefault + ); + CGContextDrawImage(ctx, [self frame], image); + CGImageRelease(image); } - (BOOL) acceptsFirstResponder { - return YES; + return YES; } - (void) keyDown: (NSEvent *) event { - char in; - BOOL converted = [ - [event characters] - getBytes: &in - maxLength: 1 - usedLength: NULL - encoding: NSASCIIStringEncoding - options: 0 - range: NSMakeRange(0, 1) - remainingRange: NULL - ]; - if (converted) { - if (!input(in)) { - [NSApp terminate: self]; - } - [self draw]; - [self setWindowTitle]; - } + char in; + BOOL converted = [ + [event characters] + getBytes: &in + maxLength: 1 + usedLength: NULL + encoding: NSASCIIStringEncoding + options: 0 + range: NSMakeRange(0, 1) + remainingRange: NULL + ]; + if (converted) { + if (!input(in)) { + [NSApp terminate: self]; + } + [self draw]; + [self setWindowTitle]; + } } @end @@ -106,57 +106,57 @@ @implementation Delegate - (BOOL) applicationShouldTerminateAfterLastWindowClosed: - (NSApplication *) UNUSED sender { - return YES; + (NSApplication *) UNUSED sender { + return YES; } @end int main(int argc, char *argv[]) { - int error = init(argc, argv); - if (error) return error; - - [NSApplication sharedApplication]; - [NSApp setActivationPolicy: NSApplicationActivationPolicyRegular]; - [NSApp setDelegate: [Delegate new]]; - - NSString *name = [[NSProcessInfo processInfo] processName]; - NSMenu *menu = [NSMenu new]; - [ - menu - addItemWithTitle: @"Close Window" - action: @selector(performClose:) - keyEquivalent: @"w" - ]; - [menu addItem: [NSMenuItem separatorItem]]; - [ - menu - addItemWithTitle: [@"Quit " stringByAppendingString: name] - action: @selector(terminate:) - keyEquivalent: @"q" - ]; - NSMenuItem *menuItem = [NSMenuItem new]; - [menuItem setSubmenu: menu]; - [NSApp setMainMenu: [NSMenu new]]; - [[NSApp mainMenu] addItem: menuItem]; - - NSUInteger style = NSTitledWindowMask - | NSClosableWindowMask - | NSMiniaturizableWindowMask - | NSResizableWindowMask; - NSWindow *window = [ - [NSWindow alloc] - initWithContentRect: NSMakeRect(0, 0, 800, 600) - styleMask: style - backing: NSBackingStoreBuffered - defer: YES - ]; - [window center]; - - BufferView *view = [[BufferView alloc] initWithFrame: [window frame]]; - [window setContentView: view]; - [view setWindowTitle]; - - [window makeKeyAndOrderFront: nil]; - [NSApp activateIgnoringOtherApps: YES]; - [NSApp run]; + int error = init(argc, argv); + if (error) return error; + + [NSApplication sharedApplication]; + [NSApp setActivationPolicy: NSApplicationActivationPolicyRegular]; + [NSApp setDelegate: [Delegate new]]; + + NSString *name = [[NSProcessInfo processInfo] processName]; + NSMenu *menu = [NSMenu new]; + [ + menu + addItemWithTitle: @"Close Window" + action: @selector(performClose:) + keyEquivalent: @"w" + ]; + [menu addItem: [NSMenuItem separatorItem]]; + [ + menu + addItemWithTitle: [@"Quit " stringByAppendingString: name] + action: @selector(terminate:) + keyEquivalent: @"q" + ]; + NSMenuItem *menuItem = [NSMenuItem new]; + [menuItem setSubmenu: menu]; + [NSApp setMainMenu: [NSMenu new]]; + [[NSApp mainMenu] addItem: menuItem]; + + NSUInteger style = NSTitledWindowMask + | NSClosableWindowMask + | NSMiniaturizableWindowMask + | NSResizableWindowMask; + NSWindow *window = [ + [NSWindow alloc] + initWithContentRect: NSMakeRect(0, 0, 800, 600) + styleMask: style + backing: NSBackingStoreBuffered + defer: YES + ]; + [window center]; + + BufferView *view = [[BufferView alloc] initWithFrame: [window frame]]; + [window setContentView: view]; + [view setWindowTitle]; + + [window makeKeyAndOrderFront: nil]; + [NSApp activateIgnoringOtherApps: YES]; + [NSApp run]; } diff --git a/bin/gfx/fb.c b/bin/gfx/fb.c index 651ed80c..e1ef5e82 100644 --- a/bin/gfx/fb.c +++ b/bin/gfx/fb.c @@ -32,56 +32,56 @@ static struct termios saveTerm; static void restoreTerm(void) { - tcsetattr(STDERR_FILENO, TCSADRAIN, &saveTerm); + tcsetattr(STDERR_FILENO, TCSADRAIN, &saveTerm); } int main(int argc, char *argv[]) { - int error; + int error; - error = init(argc, argv); - if (error) return error; + error = init(argc, argv); + if (error) return error; - const char *path = getenv("FRAMEBUFFER"); - if (!path) path = "/dev/fb0"; + const char *path = getenv("FRAMEBUFFER"); + if (!path) path = "/dev/fb0"; - int fb = open(path, O_RDWR); - if (fb < 0) err(EX_OSFILE, "%s", path); + int fb = open(path, O_RDWR); + if (fb < 0) err(EX_OSFILE, "%s", path); - struct fb_var_screeninfo info; - error = ioctl(fb, FBIOGET_VSCREENINFO, &info); - if (error) err(EX_IOERR, "%s", path); + struct fb_var_screeninfo info; + error = ioctl(fb, FBIOGET_VSCREENINFO, &info); + if (error) err(EX_IOERR, "%s", path); - size_t size = 4 * info.xres * info.yres; - uint32_t *buf = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0); - if (buf == MAP_FAILED) err(EX_IOERR, "%s", path); + size_t size = 4 * info.xres * info.yres; + uint32_t *buf = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0); + if (buf == MAP_FAILED) err(EX_IOERR, "%s", path); - error = tcgetattr(STDERR_FILENO, &saveTerm); - if (error) err(EX_IOERR, "tcgetattr"); - atexit(restoreTerm); + error = tcgetattr(STDERR_FILENO, &saveTerm); + if (error) err(EX_IOERR, "tcgetattr"); + atexit(restoreTerm); - struct termios term = saveTerm; - term.c_lflag &= ~(ICANON | ECHO); - error = tcsetattr(STDERR_FILENO, TCSADRAIN, &term); - if (error) err(EX_IOERR, "tcsetattr"); + struct termios term = saveTerm; + term.c_lflag &= ~(ICANON | ECHO); + error = tcsetattr(STDERR_FILENO, TCSADRAIN, &term); + if (error) err(EX_IOERR, "tcsetattr"); - uint32_t saveBg = buf[0]; + uint32_t saveBg = buf[0]; - uint32_t back[info.xres * info.yres]; - for (;;) { - draw(back, info.xres, info.yres); - memcpy(buf, back, size); + uint32_t back[info.xres * info.yres]; + for (;;) { + draw(back, info.xres, info.yres); + memcpy(buf, back, size); - char in; - ssize_t len = read(STDERR_FILENO, &in, 1); - if (len < 0) err(EX_IOERR, "read"); - if (!len) return EX_DATAERR; + char in; + ssize_t len = read(STDERR_FILENO, &in, 1); + if (len < 0) err(EX_IOERR, "read"); + if (!len) return EX_DATAERR; - if (!input(in)) { - for (uint32_t i = 0; i < info.xres * info.yres; ++i) { - buf[i] = saveBg; - } - fprintf(stderr, "%s\n", status()); - return EX_OK; - } - } + if (!input(in)) { + for (uint32_t i = 0; i < info.xres * info.yres; ++i) { + buf[i] = saveBg; + } + fprintf(stderr, "%s\n", status()); + return EX_OK; + } + } } diff --git a/bin/gfx/none.c b/bin/gfx/none.c index e5d53a9b..31de2ff4 100644 --- a/bin/gfx/none.c +++ b/bin/gfx/none.c @@ -20,5 +20,5 @@ #include "gfx.h" int main() { - errx(EX_CONFIG, "no gfx frontend"); + errx(EX_CONFIG, "no gfx frontend"); } diff --git a/bin/gfx/x11.c b/bin/gfx/x11.c index 82d5c833..eae926c2 100644 --- a/bin/gfx/x11.c +++ b/bin/gfx/x11.c @@ -40,96 +40,96 @@ static size_t pixmapHeight; static Pixmap pixmap; static void createWindow(void) { - display = XOpenDisplay(NULL); - if (!display) errx(EX_UNAVAILABLE, "XOpenDisplay: %s", XDisplayName(NULL)); + display = XOpenDisplay(NULL); + if (!display) errx(EX_UNAVAILABLE, "XOpenDisplay: %s", XDisplayName(NULL)); - Window root = DefaultRootWindow(display); - window = XCreateSimpleWindow(display, root, 0, 0, width, height, 0, 0, 0); + Window root = DefaultRootWindow(display); + window = XCreateSimpleWindow(display, root, 0, 0, width, height, 0, 0, 0); - WM_DELETE_WINDOW = XInternAtom(display, "WM_DELETE_WINDOW", false); - XSetWMProtocols(display, window, &WM_DELETE_WINDOW, 1); + WM_DELETE_WINDOW = XInternAtom(display, "WM_DELETE_WINDOW", false); + XSetWMProtocols(display, window, &WM_DELETE_WINDOW, 1); - windowGc = XCreateGC(display, window, 0, NULL); + windowGc = XCreateGC(display, window, 0, NULL); - image = XCreateImage(display, NULL, 24, ZPixmap, 0, NULL, width, height, 32, 0); + image = XCreateImage(display, NULL, 24, ZPixmap, 0, NULL, width, height, 32, 0); } static void resizePixmap(void) { - size_t newSize = 4 * width * height; - if (newSize > bufSize) { - bufSize = newSize; - free(buf); - buf = malloc(bufSize); - if (!buf) err(EX_OSERR, "malloc(%zu)", bufSize); - } - - image->data = (char *)buf; - image->width = width; - image->height = height; - image->bytes_per_line = 4 * width; - - if (width > pixmapWidth || height > pixmapHeight) { - pixmapWidth = width; - pixmapHeight = height; - if (pixmap) XFreePixmap(display, pixmap); - pixmap = XCreatePixmap(display, window, pixmapWidth, pixmapHeight, 24); - } + size_t newSize = 4 * width * height; + if (newSize > bufSize) { + bufSize = newSize; + free(buf); + buf = malloc(bufSize); + if (!buf) err(EX_OSERR, "malloc(%zu)", bufSize); + } + + image->data = (char *)buf; + image->width = width; + image->height = height; + image->bytes_per_line = 4 * width; + + if (width > pixmapWidth || height > pixmapHeight) { + pixmapWidth = width; + pixmapHeight = height; + if (pixmap) XFreePixmap(display, pixmap); + pixmap = XCreatePixmap(display, window, pixmapWidth, pixmapHeight, 24); + } } static void drawWindow(void) { - draw(buf, width, height); - XPutImage(display, pixmap, windowGc, image, 0, 0, 0, 0, width, height); - XCopyArea(display, pixmap, window, windowGc, 0, 0, width, height, 0, 0); + draw(buf, width, height); + XPutImage(display, pixmap, windowGc, image, 0, 0, 0, 0, width, height); + XCopyArea(display, pixmap, window, windowGc, 0, 0, width, height, 0, 0); } int main(int argc, char *argv[]) { - int error = init(argc, argv); - if (error) return error; - - createWindow(); - resizePixmap(); - drawWindow(); - - XStoreName(display, window, status()); - XMapWindow(display, window); - - XEvent event; - XSelectInput(display, window, ExposureMask | StructureNotifyMask | KeyPressMask); - for (;;) { - XNextEvent(display, &event); - - switch (event.type) { - case KeyPress: { - XKeyEvent key = event.xkey; - KeySym sym = XLookupKeysym(&key, key.state & ShiftMask); - if (sym > 0x80) break; - if (!input(sym)) return EX_OK; - drawWindow(); - XStoreName(display, window, status()); - } break; - - case ConfigureNotify: { - XConfigureEvent configure = event.xconfigure; - width = configure.width; - height = configure.height; - resizePixmap(); - drawWindow(); - } break; - - case Expose: { - XExposeEvent expose = event.xexpose; - XCopyArea( - display, pixmap, window, windowGc, - expose.x, expose.y, - expose.width, expose.height, - expose.x, expose.y - ); - } break; - - case ClientMessage: { - XClientMessageEvent client = event.xclient; - if ((Atom)client.data.l[0] == WM_DELETE_WINDOW) return EX_OK; - } break; - } - } + int error = init(argc, argv); + if (error) return error; + + createWindow(); + resizePixmap(); + drawWindow(); + + XStoreName(display, window, status()); + XMapWindow(display, window); + + XEvent event; + XSelectInput(display, window, ExposureMask | StructureNotifyMask | KeyPressMask); + for (;;) { + XNextEvent(display, &event); + + switch (event.type) { + case KeyPress: { + XKeyEvent key = event.xkey; + KeySym sym = XLookupKeysym(&key, key.state & ShiftMask); + if (sym > 0x80) break; + if (!input(sym)) return EX_OK; + drawWindow(); + XStoreName(display, window, status()); + } break; + + case ConfigureNotify: { + XConfigureEvent configure = event.xconfigure; + width = configure.width; + height = configure.height; + resizePixmap(); + drawWindow(); + } break; + + case Expose: { + XExposeEvent expose = event.xexpose; + XCopyArea( + display, pixmap, window, windowGc, + expose.x, expose.y, + expose.width, expose.height, + expose.x, expose.y + ); + } break; + + case ClientMessage: { + XClientMessageEvent client = event.xclient; + if ((Atom)client.data.l[0] == WM_DELETE_WINDOW) return EX_OK; + } break; + } + } } diff --git a/bin/gfxx.c b/bin/gfxx.c index ebd53738..2a7eb36e 100644 --- a/bin/gfxx.c +++ b/bin/gfxx.c @@ -38,17 +38,17 @@ #define GRAY(n) RGB(n, n, n) static enum { - COLOR_INDEXED, - COLOR_GRAYSCALE, - COLOR_RGB, - COLOR__COUNT, + COLOR_INDEXED, + COLOR_GRAYSCALE, + COLOR_RGB, + COLOR__COUNT, } space = COLOR_RGB; static const char *COLOR__STR[COLOR__COUNT] = { "indexed", "grayscale", "rgb" }; static uint32_t palette[256]; static enum { - ENDIAN_LITTLE, - ENDIAN_BIG, + ENDIAN_LITTLE, + ENDIAN_BIG, } byteOrder, bitOrder; enum { PAD, R, G, B }; @@ -68,425 +68,425 @@ static size_t size; static uint8_t *data; int init(int argc, char *argv[]) { - const char *pal = NULL; - const char *path = NULL; - - int opt; - while (0 < (opt = getopt(argc, argv, "c:p:b:e:E:n:fmw:z:o:"))) { - switch (opt) { - case 'c': switch (optarg[0]) { - case 'i': space = COLOR_INDEXED; break; - case 'g': space = COLOR_GRAYSCALE; break; - case 'r': space = COLOR_RGB; break; - default: return EX_USAGE; - } break; - case 'p': pal = optarg; break; - case 'e': switch (optarg[0]) { - case 'l': byteOrder = ENDIAN_LITTLE; break; - case 'b': byteOrder = ENDIAN_BIG; break; - default: return EX_USAGE; - } break; - case 'E': switch (optarg[0]) { - case 'l': bitOrder = ENDIAN_LITTLE; break; - case 'b': bitOrder = ENDIAN_BIG; break; - default: return EX_USAGE; - } break; - case 'b': { - if (strlen(optarg) < 4) return EX_USAGE; - for (int i = 0; i < 4; ++i) { - bits[i] = optarg[i] - '0'; - } - } break; - case 'n': offset = strtoul(optarg, NULL, 0); break; - case 'f': flip ^= true; break; - case 'm': mirror ^= true; break; - case 'w': width = strtoul(optarg, NULL, 0); break; - case 'z': scale = strtoul(optarg, NULL, 0); break; - case 'o': prefix = optarg; break; - default: return EX_USAGE; - } - } - if (argc > optind) path = argv[optind]; - if (!width || !scale) return EX_USAGE; - - if (pal) { - FILE *file = fopen(pal, "r"); - if (!file) err(EX_NOINPUT, "%s", pal); - fread(palette, 4, 256, file); - if (ferror(file)) err(EX_IOERR, "%s", pal); - fclose(file); - } else { - for (int i = 0; i < 256; ++i) { - double h = i / 256.0 * 6.0; - double x = 1.0 - fabs(fmod(h, 2.0) - 1.0); - double r = 255.0, g = 255.0, b = 255.0; - if (h <= 1.0) { g *= x; b = 0.0; } - else if (h <= 2.0) { r *= x; b = 0.0; } - else if (h <= 3.0) { r = 0.0; b *= x; } - else if (h <= 4.0) { r = 0.0; g *= x; } - else if (h <= 5.0) { r *= x; g = 0.0; } - else if (h <= 6.0) { g = 0.0; b *= x; } - palette[i] = (uint32_t)r << 16 | (uint32_t)g << 8 | (uint32_t)b; - } - } - - if (path) { - int fd = open(path, O_RDONLY); - if (fd < 0) err(EX_NOINPUT, "%s", path); - - struct stat stat; - int error = fstat(fd, &stat); - if (error) err(EX_IOERR, "%s", path); - size = stat.st_size; - - data = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); - if (data == MAP_FAILED) err(EX_IOERR, "%s", path); - - } else { - size = 1024 * 1024; - data = malloc(size); - if (!data) err(EX_OSERR, "malloc(%zu)", size); - - size = fread(data, 1, size, stdin); - if (ferror(stdin)) err(EX_IOERR, "(stdin)"); - } - - return EX_OK; + const char *pal = NULL; + const char *path = NULL; + + int opt; + while (0 < (opt = getopt(argc, argv, "c:p:b:e:E:n:fmw:z:o:"))) { + switch (opt) { + case 'c': switch (optarg[0]) { + case 'i': space = COLOR_INDEXED; break; + case 'g': space = COLOR_GRAYSCALE; break; + case 'r': space = COLOR_RGB; break; + default: return EX_USAGE; + } break; + case 'p': pal = optarg; break; + case 'e': switch (optarg[0]) { + case 'l': byteOrder = ENDIAN_LITTLE; break; + case 'b': byteOrder = ENDIAN_BIG; break; + default: return EX_USAGE; + } break; + case 'E': switch (optarg[0]) { + case 'l': bitOrder = ENDIAN_LITTLE; break; + case 'b': bitOrder = ENDIAN_BIG; break; + default: return EX_USAGE; + } break; + case 'b': { + if (strlen(optarg) < 4) return EX_USAGE; + for (int i = 0; i < 4; ++i) { + bits[i] = optarg[i] - '0'; + } + } break; + case 'n': offset = strtoul(optarg, NULL, 0); break; + case 'f': flip ^= true; break; + case 'm': mirror ^= true; break; + case 'w': width = strtoul(optarg, NULL, 0); break; + case 'z': scale = strtoul(optarg, NULL, 0); break; + case 'o': prefix = optarg; break; + default: return EX_USAGE; + } + } + if (argc > optind) path = argv[optind]; + if (!width || !scale) return EX_USAGE; + + if (pal) { + FILE *file = fopen(pal, "r"); + if (!file) err(EX_NOINPUT, "%s", pal); + fread(palette, 4, 256, file); + if (ferror(file)) err(EX_IOERR, "%s", pal); + fclose(file); + } else { + for (int i = 0; i < 256; ++i) { + double h = i / 256.0 * 6.0; + double x = 1.0 - fabs(fmod(h, 2.0) - 1.0); + double r = 255.0, g = 255.0, b = 255.0; + if (h <= 1.0) { g *= x; b = 0.0; } + else if (h <= 2.0) { r *= x; b = 0.0; } + else if (h <= 3.0) { r = 0.0; b *= x; } + else if (h <= 4.0) { r = 0.0; g *= x; } + else if (h <= 5.0) { r *= x; g = 0.0; } + else if (h <= 6.0) { g = 0.0; b *= x; } + palette[i] = (uint32_t)r << 16 | (uint32_t)g << 8 | (uint32_t)b; + } + } + + if (path) { + int fd = open(path, O_RDONLY); + if (fd < 0) err(EX_NOINPUT, "%s", path); + + struct stat stat; + int error = fstat(fd, &stat); + if (error) err(EX_IOERR, "%s", path); + size = stat.st_size; + + data = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); + if (data == MAP_FAILED) err(EX_IOERR, "%s", path); + + } else { + size = 1024 * 1024; + data = malloc(size); + if (!data) err(EX_OSERR, "malloc(%zu)", size); + + size = fread(data, 1, size, stdin); + if (ferror(stdin)) err(EX_IOERR, "(stdin)"); + } + + return EX_OK; } static char options[128]; static void formatOptions(void) { - snprintf( - options, sizeof(options), - "gfxx -c %s -e%c -E%c -b %hhu%hhu%hhu%hhu -n 0x%zX %s%s-w %zu -z %zu", - COLOR__STR[space], - "lb"[byteOrder], - "lb"[bitOrder], - bits[PAD], bits[R], bits[G], bits[B], - offset, - flip ? "-f " : "", - mirror ? "-m " : "", - width, - scale - ); + snprintf( + options, sizeof(options), + "gfxx -c %s -e%c -E%c -b %hhu%hhu%hhu%hhu -n 0x%zX %s%s-w %zu -z %zu", + COLOR__STR[space], + "lb"[byteOrder], + "lb"[bitOrder], + bits[PAD], bits[R], bits[G], bits[B], + offset, + flip ? "-f " : "", + mirror ? "-m " : "", + width, + scale + ); } const char *status(void) { - formatOptions(); - return options; + formatOptions(); + return options; } struct Iter { - uint32_t *buf; - size_t bufWidth; - size_t bufHeight; - size_t left; - size_t x; - size_t y; + uint32_t *buf; + size_t bufWidth; + size_t bufHeight; + size_t left; + size_t x; + size_t y; }; static struct Iter iter(uint32_t *buf, size_t bufWidth, size_t bufHeight) { - struct Iter it = { .buf = buf, .bufWidth = bufWidth, .bufHeight = bufHeight }; - if (mirror) it.x = width - 1; - if (flip) it.y = bufHeight / scale - 1; - return it; + struct Iter it = { .buf = buf, .bufWidth = bufWidth, .bufHeight = bufHeight }; + if (mirror) it.x = width - 1; + if (flip) it.y = bufHeight / scale - 1; + return it; } static bool nextX(struct Iter *it) { - if (mirror) { - if (it->x == it->left) return false; - it->x--; - } else { - it->x++; - if (it->x == it->left + width) return false; - } - return true; + if (mirror) { + if (it->x == it->left) return false; + it->x--; + } else { + it->x++; + if (it->x == it->left + width) return false; + } + return true; } static bool nextY(struct Iter *it) { - if (flip) { - if (it->y == 0) { - it->left += width; - it->y = it->bufHeight / scale; - } - it->y--; - } else { - it->y++; - if (it->y == it->bufHeight / scale) { - it->left += width; - it->y = 0; - } - } - it->x = it->left; - if (mirror) it->x += width - 1; - return (it->left < it->bufWidth / scale); + if (flip) { + if (it->y == 0) { + it->left += width; + it->y = it->bufHeight / scale; + } + it->y--; + } else { + it->y++; + if (it->y == it->bufHeight / scale) { + it->left += width; + it->y = 0; + } + } + it->x = it->left; + if (mirror) it->x += width - 1; + return (it->left < it->bufWidth / scale); } static bool next(struct Iter *it) { - return nextX(it) || nextY(it); + return nextX(it) || nextY(it); } static void put(const struct Iter *it, uint32_t pixel) { - size_t scaledX = it->x * scale; - size_t scaledY = it->y * scale; - for (size_t fillY = scaledY; fillY < scaledY + scale; ++fillY) { - if (fillY >= it->bufHeight) break; - for (size_t fillX = scaledX; fillX < scaledX + scale; ++fillX) { - if (fillX >= it->bufWidth) break; - it->buf[fillY * it->bufWidth + fillX] = pixel; - } - } + size_t scaledX = it->x * scale; + size_t scaledY = it->y * scale; + for (size_t fillY = scaledY; fillY < scaledY + scale; ++fillY) { + if (fillY >= it->bufHeight) break; + for (size_t fillX = scaledX; fillX < scaledX + scale; ++fillX) { + if (fillX >= it->bufWidth) break; + it->buf[fillY * it->bufWidth + fillX] = pixel; + } + } } static uint8_t interp(uint8_t b, uint32_t n) { - if (b == 8) return n; - if (b == 0) return 0; - return n * MASK(8) / MASK(b); + if (b == 8) return n; + if (b == 0) return 0; + return n * MASK(8) / MASK(b); } static uint32_t interpolate(uint32_t rgb) { - uint32_t r, g, b; - if (bitOrder == ENDIAN_LITTLE) { - b = rgb & MASK(bits[B]); - g = (rgb >>= bits[B]) & MASK(bits[G]); - r = (rgb >>= bits[G]) & MASK(bits[R]); - } else { - r = rgb & MASK(bits[R]); - g = (rgb >>= bits[R]) & MASK(bits[G]); - b = (rgb >>= bits[G]) & MASK(bits[B]); - } - return RGB(interp(bits[R], r), interp(bits[G], g), interp(bits[B], b)); + uint32_t r, g, b; + if (bitOrder == ENDIAN_LITTLE) { + b = rgb & MASK(bits[B]); + g = (rgb >>= bits[B]) & MASK(bits[G]); + r = (rgb >>= bits[G]) & MASK(bits[R]); + } else { + r = rgb & MASK(bits[R]); + g = (rgb >>= bits[R]) & MASK(bits[G]); + b = (rgb >>= bits[G]) & MASK(bits[B]); + } + return RGB(interp(bits[R], r), interp(bits[G], g), interp(bits[B], b)); } static void drawBits(struct Iter *it) { - for (size_t i = offset; i < size; ++i) { - for (uint8_t b = 0; b < 8; b += BITS_TOTAL) { - uint8_t n; - if (byteOrder == ENDIAN_BIG) { - n = data[i] >> (8 - BITS_TOTAL - b) & MASK(BITS_TOTAL); - } else { - n = data[i] >> b & MASK(BITS_TOTAL); - } - - if (space == COLOR_INDEXED) { - put(it, palette[n]); - } else if (space == COLOR_GRAYSCALE) { - put(it, GRAY(interp(BITS_COLOR, n & MASK(BITS_COLOR)))); - } else if (space == COLOR_RGB) { - put(it, interpolate(n)); - } - - if (!next(it)) return; - } - } + for (size_t i = offset; i < size; ++i) { + for (uint8_t b = 0; b < 8; b += BITS_TOTAL) { + uint8_t n; + if (byteOrder == ENDIAN_BIG) { + n = data[i] >> (8 - BITS_TOTAL - b) & MASK(BITS_TOTAL); + } else { + n = data[i] >> b & MASK(BITS_TOTAL); + } + + if (space == COLOR_INDEXED) { + put(it, palette[n]); + } else if (space == COLOR_GRAYSCALE) { + put(it, GRAY(interp(BITS_COLOR, n & MASK(BITS_COLOR)))); + } else if (space == COLOR_RGB) { + put(it, interpolate(n)); + } + + if (!next(it)) return; + } + } } static void drawBytes(struct Iter *it) { - uint8_t bytes = (BITS_TOTAL + 7) / 8; - for (size_t i = offset; i + bytes <= size; i += bytes) { - uint32_t n = 0; - for (size_t b = 0; b < bytes; ++b) { - n <<= 8; - n |= (byteOrder == ENDIAN_BIG) ? data[i + b] : data[i + bytes - b - 1]; - } - - if (space == COLOR_INDEXED) { - put(it, palette[n & 0xFF]); - } else if (space == COLOR_GRAYSCALE) { - put(it, GRAY(interp(BITS_COLOR, n & MASK(BITS_COLOR)))); - } else if (space == COLOR_RGB) { - put(it, interpolate(n)); - } - - if (!next(it)) return; - } + uint8_t bytes = (BITS_TOTAL + 7) / 8; + for (size_t i = offset; i + bytes <= size; i += bytes) { + uint32_t n = 0; + for (size_t b = 0; b < bytes; ++b) { + n <<= 8; + n |= (byteOrder == ENDIAN_BIG) ? data[i + b] : data[i + bytes - b - 1]; + } + + if (space == COLOR_INDEXED) { + put(it, palette[n & 0xFF]); + } else if (space == COLOR_GRAYSCALE) { + put(it, GRAY(interp(BITS_COLOR, n & MASK(BITS_COLOR)))); + } else if (space == COLOR_RGB) { + put(it, interpolate(n)); + } + + if (!next(it)) return; + } } static struct { - unsigned counter; - char path[FILENAME_MAX]; - FILE *file; + unsigned counter; + char path[FILENAME_MAX]; + FILE *file; } out; static void outOpen(const char *ext) { - snprintf(out.path, sizeof(out.path), "%s%04u.%s", prefix, ++out.counter, ext); - out.file = fopen(out.path, "wx"); - if (out.file) { - printf("%s\n", out.path); - } else { - warn("%s", out.path); - } + snprintf(out.path, sizeof(out.path), "%s%04u.%s", prefix, ++out.counter, ext); + out.file = fopen(out.path, "wx"); + if (out.file) { + printf("%s\n", out.path); + } else { + warn("%s", out.path); + } } static uint32_t crc; static void pngWrite(const void *ptr, size_t size) { - fwrite(ptr, size, 1, out.file); - if (ferror(out.file)) err(EX_IOERR, "%s", out.path); - crc = crc32(crc, ptr, size); + fwrite(ptr, size, 1, out.file); + if (ferror(out.file)) err(EX_IOERR, "%s", out.path); + crc = crc32(crc, ptr, size); } static void pngUint(uint32_t host) { - uint32_t net = htonl(host); - pngWrite(&net, 4); + uint32_t net = htonl(host); + pngWrite(&net, 4); } static void pngChunk(const char *type, uint32_t size) { - pngUint(size); - crc = crc32(0, Z_NULL, 0); - pngWrite(type, 4); + pngUint(size); + crc = crc32(0, Z_NULL, 0); + pngWrite(type, 4); } static void pngDump(uint32_t *src, size_t srcWidth, size_t srcHeight) { - int error; - - size_t stride = 1 + 3 * srcWidth; - uint8_t data[stride * srcHeight]; - for (size_t y = 0; y < srcHeight; ++y) { - data[y * stride] = 0; - for (size_t x = 0; x < srcWidth; ++x) { - uint8_t *p = &data[y * stride + 1 + 3 * x]; - p[0] = src[y * srcWidth + x] >> 16; - p[1] = src[y * srcWidth + x] >> 8; - p[2] = src[y * srcWidth + x]; - } - } - - uLong deflateSize = compressBound(sizeof(data)); - uint8_t deflate[deflateSize]; - error = compress(deflate, &deflateSize, data, sizeof(data)); - if (error != Z_OK) errx(EX_SOFTWARE, "compress: %d", error); - - outOpen("png"); - if (!out.file) return; - - const uint8_t SIGNATURE[] = { 0x89, 'P', 'N', 'G', '\r', '\n', 0x1A, '\n' }; - const uint8_t HEADER[] = { 8, 2, 0, 0, 0 }; // 8-bit truecolor - const char SOFTWARE[] = "Software"; - formatOptions(); - uint8_t sbit[3] = { MAX(bits[R], 1), MAX(bits[G], 1), MAX(bits[B], 1) }; - - pngWrite(SIGNATURE, sizeof(SIGNATURE)); - - pngChunk("IHDR", 4 + 4 + sizeof(HEADER)); - pngUint(srcWidth); - pngUint(srcHeight); - pngWrite(HEADER, sizeof(HEADER)); - pngUint(crc); - - pngChunk("tEXt", sizeof(SOFTWARE) + strlen(options)); - pngWrite(SOFTWARE, sizeof(SOFTWARE)); - pngWrite(options, strlen(options)); - pngUint(crc); - - pngChunk("sBIT", sizeof(sbit)); - pngWrite(sbit, sizeof(sbit)); - pngUint(crc); - - pngChunk("IDAT", deflateSize); - pngWrite(deflate, deflateSize); - pngUint(crc); - - pngChunk("IEND", 0); - pngUint(crc); - - error = fclose(out.file); - if (error) err(EX_IOERR, "%s", out.path); + int error; + + size_t stride = 1 + 3 * srcWidth; + uint8_t data[stride * srcHeight]; + for (size_t y = 0; y < srcHeight; ++y) { + data[y * stride] = 0; + for (size_t x = 0; x < srcWidth; ++x) { + uint8_t *p = &data[y * stride + 1 + 3 * x]; + p[0] = src[y * srcWidth + x] >> 16; + p[1] = src[y * srcWidth + x] >> 8; + p[2] = src[y * srcWidth + x]; + } + } + + uLong deflateSize = compressBound(sizeof(data)); + uint8_t deflate[deflateSize]; + error = compress(deflate, &deflateSize, data, sizeof(data)); + if (error != Z_OK) errx(EX_SOFTWARE, "compress: %d", error); + + outOpen("png"); + if (!out.file) return; + + const uint8_t SIGNATURE[] = { 0x89, 'P', 'N', 'G', '\r', '\n', 0x1A, '\n' }; + const uint8_t HEADER[] = { 8, 2, 0, 0, 0 }; // 8-bit truecolor + const char SOFTWARE[] = "Software"; + formatOptions(); + uint8_t sbit[3] = { MAX(bits[R], 1), MAX(bits[G], 1), MAX(bits[B], 1) }; + + pngWrite(SIGNATURE, sizeof(SIGNATURE)); + + pngChunk("IHDR", 4 + 4 + sizeof(HEADER)); + pngUint(srcWidth); + pngUint(srcHeight); + pngWrite(HEADER, sizeof(HEADER)); + pngUint(crc); + + pngChunk("tEXt", sizeof(SOFTWARE) + strlen(options)); + pngWrite(SOFTWARE, sizeof(SOFTWARE)); + pngWrite(options, strlen(options)); + pngUint(crc); + + pngChunk("sBIT", sizeof(sbit)); + pngWrite(sbit, sizeof(sbit)); + pngUint(crc); + + pngChunk("IDAT", deflateSize); + pngWrite(deflate, deflateSize); + pngUint(crc); + + pngChunk("IEND", 0); + pngUint(crc); + + error = fclose(out.file); + if (error) err(EX_IOERR, "%s", out.path); } static enum { - DUMP_NONE, - DUMP_ONE, - DUMP_ALL, + DUMP_NONE, + DUMP_ONE, + DUMP_ALL, } dump; void draw(uint32_t *buf, size_t bufWidth, size_t bufHeight) { - memset(buf, 0, 4 * bufWidth * bufHeight); - struct Iter it = iter(buf, bufWidth, bufHeight); - if (BITS_TOTAL >= 8) { - drawBytes(&it); - } else { - drawBits(&it); - } - if (dump) pngDump(buf, bufWidth, bufHeight); - if (dump == DUMP_ONE) dump = DUMP_NONE; + memset(buf, 0, 4 * bufWidth * bufHeight); + struct Iter it = iter(buf, bufWidth, bufHeight); + if (BITS_TOTAL >= 8) { + drawBytes(&it); + } else { + drawBits(&it); + } + if (dump) pngDump(buf, bufWidth, bufHeight); + if (dump == DUMP_ONE) dump = DUMP_NONE; } static void palSample(void) { - size_t temp = scale; - scale = 1; - draw(palette, 256, 1); - scale = temp; + size_t temp = scale; + scale = 1; + draw(palette, 256, 1); + scale = temp; } static void palDump(void) { - outOpen("dat"); - if (!out.file) return; + outOpen("dat"); + if (!out.file) return; - fwrite(palette, 4, 256, out.file); - if (ferror(out.file)) err(EX_IOERR, "%s", out.path); + fwrite(palette, 4, 256, out.file); + if (ferror(out.file)) err(EX_IOERR, "%s", out.path); - int error = fclose(out.file); - if (error) err(EX_IOERR, "%s", out.path); + int error = fclose(out.file); + if (error) err(EX_IOERR, "%s", out.path); } static const uint8_t PRESETS[][4] = { - { 0, 0, 1, 0 }, - { 0, 1, 1, 0 }, - { 1, 1, 1, 1 }, - { 2, 2, 2, 2 }, - { 0, 3, 3, 2 }, - { 4, 4, 4, 4 }, - { 1, 5, 5, 5 }, - { 0, 5, 6, 5 }, - { 0, 8, 8, 8 }, - { 8, 8, 8, 8 }, + { 0, 0, 1, 0 }, + { 0, 1, 1, 0 }, + { 1, 1, 1, 1 }, + { 2, 2, 2, 2 }, + { 0, 3, 3, 2 }, + { 4, 4, 4, 4 }, + { 1, 5, 5, 5 }, + { 0, 5, 6, 5 }, + { 0, 8, 8, 8 }, + { 8, 8, 8, 8 }, }; #define PRESETS_LEN (sizeof(PRESETS) / sizeof(PRESETS[0])) static uint8_t preset = PRESETS_LEN - 1; static void setPreset(void) { - bits[PAD] = PRESETS[preset][PAD]; - bits[R] = PRESETS[preset][R]; - bits[G] = PRESETS[preset][G]; - bits[B] = PRESETS[preset][B]; + bits[PAD] = PRESETS[preset][PAD]; + bits[R] = PRESETS[preset][R]; + bits[G] = PRESETS[preset][G]; + bits[B] = PRESETS[preset][B]; } static void setBit(char in) { - static uint8_t bit = 0; - bits[bit++] = in - '0'; - bit &= 3; + static uint8_t bit = 0; + bits[bit++] = in - '0'; + bit &= 3; } bool input(char in) { - size_t pixel = (BITS_TOTAL + 7) / 8; - size_t row = width * BITS_TOTAL / 8; - switch (in) { - case 'q': return false; - break; case 'x': dump = DUMP_ONE; - break; case 'X': dump ^= DUMP_ALL; - break; case 'o': formatOptions(); printf("%s\n", options); - break; case '[': if (!space--) space = COLOR__COUNT - 1; - break; case ']': if (++space == COLOR__COUNT) space = 0; - break; case 'p': palSample(); - break; case 'P': palDump(); - break; case '{': if (!preset--) preset = PRESETS_LEN - 1; setPreset(); - break; case '}': if (++preset == PRESETS_LEN) preset = 0; setPreset(); - break; case 'e': byteOrder ^= ENDIAN_BIG; - break; case 'E': bitOrder ^= ENDIAN_BIG; - break; case 'h': if (offset) offset--; - break; case 'j': offset += pixel; - break; case 'k': if (offset >= pixel) offset -= pixel; - break; case 'l': offset++; - break; case 'H': if (offset >= row) offset -= row; - break; case 'J': offset += width * row; - break; case 'K': if (offset >= width * row) offset -= width * row; - break; case 'L': offset += row; - break; case '.': width++; - break; case ',': if (width > 1) width--; - break; case '>': width *= 2; - break; case '<': if (width > 1) width /= 2; - break; case 'f': flip ^= true; - break; case 'm': mirror ^= true; - break; case '+': scale++; - break; case '-': if (scale > 1) scale--; - break; default: if (in >= '0' && in <= '9') setBit(in); - } - return true; + size_t pixel = (BITS_TOTAL + 7) / 8; + size_t row = width * BITS_TOTAL / 8; + switch (in) { + case 'q': return false; + break; case 'x': dump = DUMP_ONE; + break; case 'X': dump ^= DUMP_ALL; + break; case 'o': formatOptions(); printf("%s\n", options); + break; case '[': if (!space--) space = COLOR__COUNT - 1; + break; case ']': if (++space == COLOR__COUNT) space = 0; + break; case 'p': palSample(); + break; case 'P': palDump(); + break; case '{': if (!preset--) preset = PRESETS_LEN - 1; setPreset(); + break; case '}': if (++preset == PRESETS_LEN) preset = 0; setPreset(); + break; case 'e': byteOrder ^= ENDIAN_BIG; + break; case 'E': bitOrder ^= ENDIAN_BIG; + break; case 'h': if (offset) offset--; + break; case 'j': offset += pixel; + break; case 'k': if (offset >= pixel) offset -= pixel; + break; case 'l': offset++; + break; case 'H': if (offset >= row) offset -= row; + break; case 'J': offset += width * row; + break; case 'K': if (offset >= width * row) offset -= width * row; + break; case 'L': offset += row; + break; case '.': width++; + break; case ',': if (width > 1) width--; + break; case '>': width *= 2; + break; case '<': if (width > 1) width /= 2; + break; case 'f': flip ^= true; + break; case 'm': mirror ^= true; + break; case '+': scale++; + break; case '-': if (scale > 1) scale--; + break; default: if (in >= '0' && in <= '9') setBit(in); + } + return true; } |