ascii-gen

simple ascii image converter
git clone git://git.jakekoroman.com/ascii-gen
Log | Files | Refs | README

commit 5a47c0e88dfc5d04f2314316f45a2c95a5ddcabb
parent 0cd317758c4d173d04789a15364cd074ec893d7a
Author: Jake Koroman <jakekoroman@gmail.com>
Date:   Tue,  8 Oct 2024 14:43:45 -0400

ascii-gen.c: fix formatting.

Diffstat:
Mascii_gen.c | 263+++++++++++++++++++++++++++++++++++++------------------------------------------
1 file changed, 122 insertions(+), 141 deletions(-)

diff --git a/ascii_gen.c b/ascii_gen.c @@ -24,40 +24,40 @@ typedef uint8_t uint8; #define SET_SIZE_SHORT 10 typedef enum { - SET_TYPE_SHORT, - SET_TYPE_LONG, - SET_TYPE_CUSTOM, + SET_TYPE_SHORT, + SET_TYPE_LONG, + SET_TYPE_CUSTOM, } SetType; typedef struct { - char in_file[BUF_CAP]; - char out_file[BUF_CAP]; - char set[BUF_CAP]; - SetType set_type; // defaults to short - int set_size; - int scale; - bool debug; + char in_file[BUF_CAP]; + char out_file[BUF_CAP]; + char set[BUF_CAP]; + SetType set_type; // defaults to short + int set_size; + int scale; + bool verbose; bool inverted; } Options; internal inline int map(int x, int a, int b, int c, int d) { - return (x - a) * (d - c) / (b - a); + return (x - a) * (d - c) / (b - a); } internal void print_docs(const char *fname) { - printf("Usage: %s img [-o file] [-s scale] [-S set] [-hltv]\n", fname); - printf(" -h: Shows help\n"); - printf(" -o: Output to file, if no file is given it will output to stdout\n"); - printf(" -s: The scale of the output image, defaults to 1\n"); - printf(" -S: The character set that will be used to generate the output, defaults to the builtin 'short' character set\n"); - printf(" -l: Uses the builtin 'long' character set for generating the output\n"); - printf(" -t: Uses the builtin 'short' character set for generating the output\n"); - printf(" -i: Uses the builtin 'short' character set but inverted\n"); - printf(" -v: Shows debug info\n"); + printf("Usage: %s img [-o file] [-s scale] [-S set] [-hltv]\n", fname); + printf(" -h: Shows help\n"); + printf(" -o: Output to file, if no file is given it will output to stdout\n"); + printf(" -s: The scale of the output image, defaults to 1\n"); + printf(" -S: The character set that will be used to generate the output, defaults to the builtin 'short' character set\n"); + printf(" -l: Uses the builtin 'long' character set for generating the output\n"); + printf(" -t: Uses the builtin 'short' character set for generating the output\n"); + printf(" -i: Uses the builtin 'short' character set but inverted\n"); + printf(" -v: Shows verbose info\n"); } // Parses args with getopt and fills an Options struct of all the options passed @@ -65,59 +65,52 @@ print_docs(const char *fname) internal void parse_args(int argc, char **argv, Options *options) { - int opt; - options->scale = 1; - - while ((opt = getopt(argc, argv, ":o:hls:S:vic")) != -1) - { - switch (opt) - { - case 'o': + int opt; + options->scale = 1; + + while ((opt = getopt(argc, argv, ":o:hls:S:vic")) != -1) + { + switch (opt) { + case 'o': strncpy(options->out_file, optarg, BUF_CAP); - } break; + break; case 'h': - { print_docs(argv[0]); exit(0); - } break; + break; - case 's': - { + case 's': { options->scale = atoi(optarg); if (options->scale == 0) { fprintf(stderr, "Invalid scale '%s'\n", optarg); exit(69); } - } break; + } break; case 'v': - { - options->debug = true; - } break; + options->verbose = true; + break; case 'l': - { options->set_type = SET_TYPE_LONG; options->set_size = SET_SIZE_LONG; strncpy(options->set, SET_LONG, BUF_CAP); - } break; + break; - case 't': // 't' for tiny I guess? wanted to keep 's' for scale - { + // 't' for tiny I guess? wanted to keep 's' for scale: + case 't': // Redundant because it will default to short but sometimes // it's nice to request it even though its default. options->set_type = SET_TYPE_SHORT; - } break; + break; case 'i': - { options->inverted = true; - } break; + break; - case 'S': - { + case 'S': { int size = strnlen(optarg, BUF_CAP); options->set_size = size; @@ -127,125 +120,113 @@ parse_args(int argc, char **argv, Options *options) } break; case '?': - { printf("Unknown Option: %c\n", optopt); exit(69); - } break; + break; case ':': - { printf("Missing arg for %c\n", optopt); exit(69); - } break; + break; default: - { printf("Something broke\n"); break; } - } - } + } - if (options->set_type == SET_TYPE_SHORT) { - options->set_size = SET_SIZE_SHORT; + if (options->set_type == SET_TYPE_SHORT) { + options->set_size = SET_SIZE_SHORT; if (options->inverted) strncpy(options->set, SET_SHORT_INVERTED, BUF_CAP); else strncpy(options->set, SET_SHORT, BUF_CAP); - } - - if (optind < argc) { - strncpy(options->in_file, argv[optind], BUF_CAP); - } + } + if (optind < argc) + strncpy(options->in_file, argv[optind], BUF_CAP); } int main(int argc, char **argv) { - if (argc < 2) { - printf("Basic Usage: %s img [-o file]\n", argv[0]); - exit(69); - } - - Options options = {0}; - parse_args(argc, argv, &options); - - if (options.debug) { - printf("In File : %s\n", options.in_file); - printf("Out File : %s\n", options.out_file); - printf("Scale : %d\n", options.scale); - printf("Set size : %d\n", options.set_size); - printf("Set type : %d\n", options.set_type); - printf("Set : %s\n", options.set); - } - - // TODO(jake): Maybe bundle this stuff up? including the resized stuff? - int width, height; - int channels = 1; - uint8 *image_bytes = stbi_load(options.in_file, &width, &height, NULL, channels); - - if (!image_bytes) { - fprintf(stderr, "Couldn't Open file %s\n", options.in_file); - exit(69); - } - - int nw = width / options.scale; - int nh = height / options.scale; - bool resized = false; - uint8 *resized_bytes; - - if (options.scale > 1) { - int stride = 0; - resized = true; - resized_bytes = malloc(sizeof(uint8) * (nw * nh)); - if (!stbir_resize_uint8(image_bytes, width, height, stride, - resized_bytes, nw, nh, stride, channels)) // only resizes if the scale is greater than 1 - { - printf("Couldn't Resize Image\n"); - exit(69); - } - } else { - resized_bytes = image_bytes; - } - - FILE *f = NULL; - if (strncmp(options.out_file, "", BUF_CAP)) { - f = fopen(options.out_file, "w"); + if (argc < 2) { + printf("Basic Usage: %s img [-o file]\n", argv[0]); + exit(69); + } + + Options options = {0}; + parse_args(argc, argv, &options); + + if (options.verbose) { + printf("In File : %s\n", options.in_file); + printf("Out File : %s\n", options.out_file); + printf("Scale : %d\n", options.scale); + printf("Set size : %d\n", options.set_size); + printf("Set type : %d\n", options.set_type); + printf("Set : %s\n", options.set); + } + + // TODO(jake): Maybe bundle this stuff up? including the resized stuff? + int width, height; + int channels = 1; + uint8 *image_bytes = stbi_load(options.in_file,&width, &height, NULL, channels); + + if (!image_bytes) { + fprintf(stderr, "Couldn't Open file %s\n", options.in_file); + exit(69); + } + + int nw = width / options.scale; + int nh = height / options.scale; + bool resized = false; + uint8 *resized_bytes; + + if (options.scale > 1) { + int stride = 0; + resized = true; + resized_bytes = malloc(sizeof(uint8) * (nw * nh)); + if (!stbir_resize_uint8(image_bytes, width, height, stride, + resized_bytes, nw, nh, stride, channels)) // only resizes if the scale is greater than 1 + { + printf("Couldn't Resize Image\n"); + exit(69); + } + } else { + resized_bytes = image_bytes; + } + + FILE *f = NULL; + if (strncmp(options.out_file, "", BUF_CAP)) { + f = fopen(options.out_file, "w"); if (f == NULL) { printf("Couldnt't create output file '%s'\n", options.out_file); exit(69); } - } else { - f = stdout; - } - - for (size_t i = 0; - i < nw * nh; - ++i) - { - int map_result = map(resized_bytes[i], 0, 255, 0, options.set_size - 1); - int idx = (options.set_size - 1) - map_result; - - if (i % nw == 0) { - fprintf(f, "\n"); - } - - fprintf(f, "%c", options.set[idx]); - } - fprintf(f, "\n"); - - if (!(f == NULL || f == stdout)) { - fclose(f); - } - - if (image_bytes) { - stbi_image_free(image_bytes); - } - - if (resized_bytes && resized) { - stbi_image_free(resized_bytes); - } - - return 0; + } else { + f = stdout; + } + + for (size_t i = 0; i < nw * nh; ++i) { + int map_result = map(resized_bytes[i], 0, 255, 0, options.set_size - 1); + int idx = (options.set_size - 1) - map_result; + + if (i % nw == 0) { + fprintf(f, "\n"); + } + + fprintf(f, "%c", options.set[idx]); + } + fprintf(f, "\n"); + + if (!(f == NULL || f == stdout)) + fclose(f); + + if (image_bytes) + stbi_image_free(image_bytes); + + if (resized_bytes && resized) + stbi_image_free(resized_bytes); + + return 0; }