ascii-gen

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

commit ee50955a9f89ef6d05c9c8aa50098de37b666965
parent 5a47c0e88dfc5d04f2314316f45a2c95a5ddcabb
Author: Jake Koroman <jake@jakekoroman.com>
Date:   Fri,  6 Jun 2025 12:08:10 -0400

Properly implement inverting the set and formatting cleanup.

Diffstat:
Mascii_gen.c | 42++++++++++++++----------------------------
1 file changed, 14 insertions(+), 28 deletions(-)

diff --git a/ascii_gen.c b/ascii_gen.c @@ -20,7 +20,6 @@ typedef uint8_t uint8; #define SET_LONG "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. " #define SET_SIZE_LONG 70 #define SET_SHORT "@%#*+=-:. " -#define SET_SHORT_INVERTED " .:-=+*#%@" #define SET_SIZE_SHORT 10 typedef enum { @@ -49,7 +48,7 @@ map(int x, int a, int b, int c, int d) internal void print_docs(const char *fname) { - printf("Usage: %s img [-o file] [-s scale] [-S set] [-hltv]\n", 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"); @@ -135,14 +134,6 @@ parse_args(int argc, char **argv, Options *options) } } - 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); } @@ -159,18 +150,19 @@ main(int argc, char **argv) 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); + fprintf(stderr, "In File : %s\n", options.in_file); + fprintf(stderr, "Out File : %s\n", options.out_file); + fprintf(stderr, "Scale : %d\n", options.scale); + fprintf(stderr, "Set size : %d\n", options.set_size); + fprintf(stderr, "Set type : %d\n", options.set_type); + fprintf(stderr, "Set : %s\n", options.set); + fprintf(stderr, "Inverted : %s\n", options.inverted ? "true" : "false"); } // 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); + 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); @@ -179,12 +171,10 @@ main(int argc, char **argv) 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 @@ -215,18 +205,14 @@ main(int argc, char **argv) fprintf(f, "\n"); } - fprintf(f, "%c", options.set[idx]); + if (options.inverted) + fprintf(f, "%c", options.set[options.set_size - idx]); + else + 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); + // the os will clean up my mess return 0; }