From 5a71d27678ea4e9c3dbac88b5102af174ab65234 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 1 Nov 2012 19:24:17 -0300 Subject: [PATCH] tool/: move dump_* from ant-txt to libant; add IO error checkint --- tools/ant-txt/ant-txt.c | 83 ++++++------------------------------- tools/libant/Makefile | 2 +- tools/libant/dump.c | 91 +++++++++++++++++++++++++++++++++++++++++ tools/libant/libant.h | 4 ++ 4 files changed, 108 insertions(+), 72 deletions(-) create mode 100644 tools/libant/dump.c diff --git a/tools/ant-txt/ant-txt.c b/tools/ant-txt/ant-txt.c index 03e070f..479702b 100644 --- a/tools/ant-txt/ant-txt.c +++ b/tools/ant-txt/ant-txt.c @@ -25,73 +25,6 @@ #define H 16 -static void dump_binary(const uint8_t *canvas) -{ - int x, y, i; - uint8_t v; - ssize_t wrote; - - for (x = 0; x != W; x++) { - for (y = 0; y != H; y += 8) { - v = 0; - for (i = 0; i != 8; i++) - if (canvas[((y+i)*W+x) >> 3] & (1 << (x & 7))) - v |= 1 << i; - wrote = write(1, &v, 1); - if (wrote < 0) { - perror("fwrite"); - exit(1); - } - if (!wrote) { - fprintf(stderr, "short write\n"); - exit(1); - } - } - } -} - - -static void dump_xbm(const uint8_t *canvas) -{ - int x, y, i, n; - uint8_t v = 0; - - printf("#define foo_width %d\n", W); - printf("#define foo_height %d\n", H); - printf("static unsigned char foo_bits[] = {\n"); - n = 0; - for (y = 0; y != H; y++) { - for (x = 0; x < W; x += 8) { - if (n) - printf("%s 0x%02x", - (n-1) % 12 ? "," : - n == 1 ? " " : ",\n ", v); - v = 0; - for (i = x; i != W && i != x+8; i++) - if (canvas[(y*W+i) >> 3] & (1 << (i & 7))) - v |= 1 << (i-x); - n++; - } - } - printf("%s 0x%02x};\n", (n-1) % 12 ? "," : ",\n ", v); -} - - -static void dump_text(const uint8_t *canvas) -{ - int x, y; - - for (y = 0; y != H; y++) { - for (x = 0; x != W; x++) - if (canvas[(y*W+x) >> 3] & (1 << (x & 7))) - putchar('#'); - else - putchar('.'); - putchar('\n'); - } -} - - static void usage(const char *name) { fprintf(stderr, "usage: %s [-b|-x] [-F font_dir ...] [text]\n", name); @@ -105,7 +38,7 @@ int main(int argc, char **argv) uint8_t *canvas; const char *err; int binary = 0, xbm = 0; - int i, c; + int i, c, res; while ((c = getopt(argc, argv, "bF:x")) != EOF) switch (c) { @@ -146,10 +79,18 @@ int main(int argc, char **argv) return 1; } if (binary) - dump_binary(canvas); + res = dump_binary(stdout, canvas, W, H); else if (xbm) - dump_xbm(canvas); + res = dump_xbm(stdout, canvas, W, H); else - dump_text(canvas); + res = dump_ascii(stdout, canvas, W, H); + if (res < 0) { + perror("write"); + exit(1); + } + if (fflush(stdout) == EOF) { + perror("fflush"); + exit(1); + } return 0; } diff --git a/tools/libant/Makefile b/tools/libant/Makefile index dbd8d68..887cb6d 100644 --- a/tools/libant/Makefile +++ b/tools/libant/Makefile @@ -16,7 +16,7 @@ LIB = libant.a # _GNU_SOURCE for vasprintf CFLAGS = -g -Wall -D_GNU_SOURCE -OBJS = edit.o font.o +OBJS = edit.o font.o dump.o .PHONY: all ben clean spotless diff --git a/tools/libant/dump.c b/tools/libant/dump.c new file mode 100644 index 0000000..83fbc8e --- /dev/null +++ b/tools/libant/dump.c @@ -0,0 +1,91 @@ +/* + * tools/libant/dump.c - Antorcha raw image dump + * + * Written 2012 by Werner Almesberger + * Copyright 2012 Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + + +#include +#include + +#include "libant.h" + + +int dump_binary(FILE *file, const void *canvas, int width, int height) +{ + const uint8_t *p = canvas; + int x, y, i; + uint8_t v; + + for (x = 0; x != width; x++) { + for (y = 0; y != height; y += 8) { + v = 0; + for (i = 0; i != 8; i++) + if (p[((y+i)*width+x) >> 3] & (1 << (x & 7))) + v |= 1 << i; + if (fputc(v, file) == EOF) + return -1; + } + } + return 0; +} + + +int dump_xbm(FILE *file, const void *canvas, int width, int height) +{ + const uint8_t *p = canvas; + int x, y, i, n; + uint8_t v = 0; + + if (fprintf(file, "#define foo_width %d\n", width) < 0) + return -1; + if (fprintf(file, "#define foo_height %d\n", height) < 0) + return -1; + if (fprintf(file, "static unsigned char foo_bits[] = {\n") < 0) + return -1; + n = 0; + for (y = 0; y != height; y++) { + for (x = 0; x < width; x += 8) { + if (n) + if (fprintf(file, "%s 0x%02x", + (n-1) % 12 ? "," : + n == 1 ? " " : ",\n ", v) < 0) + return -1; + v = 0; + for (i = x; i != width && i != x+8; i++) + if (p[(y*width+i) >> 3] & (1 << (i & 7))) + v |= 1 << (i-x); + n++; + } + } + if (fprintf(file, "%s 0x%02x};\n", (n-1) % 12 ? "," : ",\n ", v) < 0) + return -1; + return 0; +} + + +int dump_ascii(FILE *file, const void *canvas, int width, int height) +{ + const uint8_t *p = canvas; + int x, y; + + for (y = 0; y != height; y++) { + for (x = 0; x != width; x++) + if (p[(y*width+x) >> 3] & (1 << (x & 7))) { + if (fputc('#', file) == EOF) + return -1; + } else { + if (fputc('.', file) == EOF) + return -1; + } + if (fputc('\n', file) == EOF) + return -1; + } + return 0; +} diff --git a/tools/libant/libant.h b/tools/libant/libant.h index 3a9882f..6327249 100644 --- a/tools/libant/libant.h +++ b/tools/libant/libant.h @@ -77,4 +77,8 @@ void add_font_dir(const char *name); void *apply_edits(int width, int height, const struct edit *e, const char **error); +int dump_binary(FILE *file, const void *canvas, int width, int height); +int dump_xbm(FILE *file, const void *canvas, int width, int height); +int dump_ascii(FILE *file, const void *canvas, int width, int height); + #endif /* !LIBANT_H */