Added proper logging

This commit is contained in:
Casey 2022-02-06 14:21:48 +03:00
parent 153533ff86
commit d3ccc83203
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
9 changed files with 76 additions and 21 deletions

View File

@ -89,7 +89,9 @@ void usage(int argc, char **argv)
fprintf(stderr, "[-vhd] [-O FILE] [-W WIDTH] [-H HEIGHT] "); fprintf(stderr, "[-vhd] [-O FILE] [-W WIDTH] [-H HEIGHT] ");
fprintf(stderr, "[-M MODE] [-S STYLE] [-F FORMAT] [-P PALETTE] "); fprintf(stderr, "[-M MODE] [-S STYLE] [-F FORMAT] [-P PALETTE] ");
fprintf(stderr, "FILE\n\n"); fprintf(stderr, "FILE\n\n");
#ifndef DISABLE_LOGGING
fprintf(stderr, "-v\t\tEnable verbose mode\n"); fprintf(stderr, "-v\t\tEnable verbose mode\n");
#endif
fprintf(stderr, "-V\t\tShow version\n"); fprintf(stderr, "-V\t\tShow version\n");
fprintf(stderr, "-h\t\tShow this help\n"); fprintf(stderr, "-h\t\tShow this help\n");
fprintf(stderr, "\n"); fprintf(stderr, "\n");
@ -139,16 +141,24 @@ int parse_args(int argc, char **argv, asc_args_t *args)
args->out_style = ASC_STL_256COLOR; args->out_style = ASC_STL_256COLOR;
args->mode = ASC_MOD_BLOCKS; args->mode = ASC_MOD_BLOCKS;
args->dither = false; args->dither = false;
#ifndef DISABLE_LOGGING
args->verbose = false; args->verbose = false;
#endif
args->charset = " .'-*+$@"; args->charset = " .'-*+$@";
int c; int c;
#ifdef DISABLE_LOGGING
while ((c = getopt(argc, argv, "hdVW:H:M:S:F:P:O:")) != -1)
#else
while ((c = getopt(argc, argv, "vhdVW:H:M:S:F:P:O:")) != -1) while ((c = getopt(argc, argv, "vhdVW:H:M:S:F:P:O:")) != -1)
#endif
{ {
switch (c) switch (c)
{ {
#ifndef DISABLE_LOGGING
case 'v': case 'v':
args->verbose = true; args->verbose = true;
break; break;
#endif
case 'h': case 'h':
usage(argc, argv); usage(argc, argv);
return 1; return 1;
@ -239,6 +249,9 @@ int parse_args(int argc, char **argv, asc_args_t *args)
return -2; return -2;
} }
args->input_filename = argv[optind]; args->input_filename = argv[optind];
#ifndef DISABLE_LOGGING
b_logging = args->verbose;
#endif
return 0; return 0;
} }
@ -253,6 +266,7 @@ int prepare_state(int argc, char **argv, asc_args_t args, asc_state_t *state)
// Loading image // Loading image
FILE *image_file; FILE *image_file;
LOG("Opening image file %s", args.input_filename);
if ((image_file = fopen(args.input_filename, "rb")) == NULL if ((image_file = fopen(args.input_filename, "rb")) == NULL
|| ferror(image_file) != 0) || ferror(image_file) != 0)
{ {
@ -261,14 +275,19 @@ int prepare_state(int argc, char **argv, asc_args_t args, asc_state_t *state)
args.input_filename, err, strerror(err)); args.input_filename, err, strerror(err));
return -100 - err; return -100 - err;
} }
LOG("Loading image data...");
state->source_image = image_load(image_file); state->source_image = image_load(image_file);
fclose(image_file); fclose(image_file);
LOG("Image loaded: %p", state->source_image);
LOG("Image size: %dx%d",
state->source_image->width, state->source_image->height);
// Palette configuration // Palette configuration
switch (args.out_style) switch (args.out_style)
{ {
case ASC_STL_PALETTE: case ASC_STL_PALETTE:
{ {
LOG("Loading palette file...");
FILE *fp = fopen(args.palette_filename, "rb"); FILE *fp = fopen(args.palette_filename, "rb");
if (fp == NULL) if (fp == NULL)
{ {
@ -285,6 +304,7 @@ int prepare_state(int argc, char **argv, asc_args_t args, asc_state_t *state)
return -7; return -7;
} }
fclose(fp); fclose(fp);
LOG("Loaded %d colors", state->palette->n_colors);
} }
break; break;
case ASC_STL_256COLOR: case ASC_STL_256COLOR:

View File

@ -57,7 +57,9 @@ typedef struct {
asc_style_t out_style; asc_style_t out_style;
asc_mode_t mode; asc_mode_t mode;
bool dither; bool dither;
#ifndef DISABLE_LOGGING
bool verbose; bool verbose;
#endif
char *charset; char *charset;
} asc_args_t; } asc_args_t;

View File

@ -1,4 +1,5 @@
#include "colors.h" #include "colors.h"
#include "commons.h"
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
@ -128,6 +129,7 @@ rgba8 clamp_to_pal(palette_t pal, rgba8 color)
void make_pal256(palette_t *dst, palette_t ansi) void make_pal256(palette_t *dst, palette_t ansi)
{ {
LOG("Creating 256-colors palette");
if (dst->n_colors == 256) return; if (dst->n_colors == 256) return;
dst->n_colors = 256; dst->n_colors = 256;
for (int i = 0; i < 256; i++) for (int i = 0; i < 256; i++)
@ -144,9 +146,11 @@ float calc_brightness(rgba8 c)
bool load_palette_gpl(palette_t *pal, FILE *fp) bool load_palette_gpl(palette_t *pal, FILE *fp)
{ {
LOG("Loading GIMP palette");
static char buf[8192]; static char buf[8192];
fgets(buf, 8192, fp); // GIMP Palette fgets(buf, 8192, fp); // GIMP Palette
fgets(buf, 8192, fp); // Name: %s fgets(buf, 8192, fp); // Name: %s
LOG("Palette name: %s", buf);
fgets(buf, 8192, fp); // Columns: %d fgets(buf, 8192, fp); // Columns: %d
pal->n_colors = 0; pal->n_colors = 0;
@ -167,6 +171,7 @@ bool load_palette_gpl(palette_t *pal, FILE *fp)
bool load_palette_raw(palette_t *pal, FILE *fp) bool load_palette_raw(palette_t *pal, FILE *fp)
{ {
LOG("Loading raw RGB palette");
while (!feof(fp) && pal->n_colors < 256) while (!feof(fp) && pal->n_colors < 256)
{ {
size_t sz = fread(&pal->palette[pal->n_colors++], 1, sizeof(rgba8), fp); size_t sz = fread(&pal->palette[pal->n_colors++], 1, sizeof(rgba8), fp);
@ -178,6 +183,7 @@ bool load_palette_raw(palette_t *pal, FILE *fp)
bool load_palette(palette_t *pal, FILE *fp) bool load_palette(palette_t *pal, FILE *fp)
{ {
LOG("Guessing palette type");
static char head[16]; static char head[16];
if (fread(head, sizeof(char), 12, fp) < 12) return false; if (fread(head, sizeof(char), 12, fp) < 12) return false;
if (fseek(fp, 0, SEEK_SET) != 0) return false; if (fseek(fp, 0, SEEK_SET) != 0) return false;

View File

@ -2,6 +2,11 @@
#include "commons.h" #include "commons.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h>
#ifndef DISABLE_LOGGING
bool b_logging = false;
#endif
void m_prepare_dither(asc_state_t *sta) void m_prepare_dither(asc_state_t *sta)
{ {
@ -26,6 +31,19 @@ void m_prepare_dither(asc_state_t *sta)
sta->image = res; sta->image = res;
} }
#ifndef DISABLE_LOGGING
void _log(const char *fmt, ...)
{
if (!b_logging) return;
va_list args;
va_start(args, fmt);
fprintf(stderr, "[INFO] ");
vfprintf(stderr, fmt, args);
fputc('\n', stderr);
va_end(args);
}
#endif
void c_fatal(int code, const char *reason) void c_fatal(int code, const char *reason)
{ {
fprintf(stderr, "Error: %s\n", reason); fprintf(stderr, "Error: %s\n", reason);

View File

@ -21,6 +21,16 @@
#include "args.h" #include "args.h"
#define CLAMP(min, val, max) ((val)>(max)?(max):((val)<(min)?(min):(val))) #define CLAMP(min, val, max) ((val)>(max)?(max):((val)<(min)?(min):(val)))
#ifdef DISABLE_LOGGING
#define LOG(...)
#else
#define LOG(...) _log(__VA_ARGS__)
#endif
#ifndef DISABLE_LOGGING
extern bool b_logging;
void _log(const char *fmt, ...);
#endif
void c_fatal(int code, const char *reason); void c_fatal(int code, const char *reason);
void m_prepare_dither(asc_state_t *state); void m_prepare_dither(asc_state_t *state);

View File

@ -19,6 +19,7 @@ image_t *image_load(FILE *file)
image_t *image_resize(image_t *img, int width, int height) image_t *image_resize(image_t *img, int width, int height)
{ {
LOG("Resizing image %p", img);
image_t *res = calloc(1, sizeof(image_t)); image_t *res = calloc(1, sizeof(image_t));
res->width = width; res->width = width;
res->height = height; res->height = height;
@ -32,8 +33,10 @@ image_t *image_resize(image_t *img, int width, int height)
} }
else else
{ {
LOG("Same dimensions, just copying original data");
memcpy(res->pixels, img->pixels, width * height * sizeof(rgba8)); memcpy(res->pixels, img->pixels, width * height * sizeof(rgba8));
} }
LOG("Resized image: %p", res);
return res; return res;
} }
@ -52,6 +55,7 @@ void __dither_update_pixel(image_t *img, int x, int y, int err[3], float bias)
image_t *image_dither_fn(image_t *img, dither_quantizer_t quantize, void *param) image_t *image_dither_fn(image_t *img, dither_quantizer_t quantize, void *param)
{ {
LOG("Dithering image %p", img);
image_t *res = calloc(1, sizeof(image_t)); image_t *res = calloc(1, sizeof(image_t));
int w = res->width = img->width; int w = res->width = img->width;
int h = res->height = img->height; int h = res->height = img->height;
@ -77,6 +81,7 @@ image_t *image_dither_fn(image_t *img, dither_quantizer_t quantize, void *param)
__dither_update_pixel(res, x + 1, y + 1, err, 1.0f / 16.0f); __dither_update_pixel(res, x + 1, y + 1, err, 1.0f / 16.0f);
} }
} }
LOG("Dithered image: %p", res);
return res; return res;
} }
@ -94,6 +99,7 @@ image_t *image_dither(image_t *img, palette_t pal)
void image_unload(image_t *img) void image_unload(image_t *img)
{ {
LOG("Unloading image %p", img);
free(img->pixels); free(img->pixels);
free(img); free(img);
} }

View File

@ -29,33 +29,18 @@ int main(int argc, char **argv)
asc_args_t args; asc_args_t args;
int res = parse_args(argc, argv, &args); int res = parse_args(argc, argv, &args);
if (res == 1) return 0; if (res == 1) return 0;
if (res < 0) return -res; else if (res < 0) return -res;
asc_state_t state; asc_state_t state;
res = prepare_state(argc, argv, args, &state); res = prepare_state(argc, argv, args, &state);
if (res == 1) return 0; if (res == 1) return 0;
if (res < 0) return -res; else if (res < 0) return -res;
if (args.verbose)
{
fprintf(stderr, "Source image size: %dx%d\n",
state.source_image->width,
state.source_image->height);
}
asc_handler_t handler = asc_handlers[args.mode]; asc_handler_t handler = asc_handlers[args.mode];
if (handler.prepare == NULL) if (handler.prepare == NULL)
c_fatal(12, "this mode is not implemented yet"); c_fatal(12, "this mode is not implemented yet");
handler.prepare(&state); handler.prepare(&state);
if (args.verbose)
{
fprintf(stderr, "Resized image size: %dx%d\n",
state.image->width,
state.image->height);
}
handler.main(state); handler.main(state);
image_unload(state.image); image_unload(state.image);

View File

@ -25,7 +25,11 @@ void mod_blocks_prepare(asc_state_t *state)
get_size_keep_aspect( get_size_keep_aspect(
state->source_image->width, state->source_image->height, state->source_image->width, state->source_image->height,
state->args.width, state->args.height * 2, &width, &height); state->args.width, state->args.height * 2, &width, &height);
LOG("Source size: %dx%d", state->source_image->width,
state->source_image->height);
LOG("Requested size: %dx%d", state->args.width, state->args.height * 2);
height = (height / 2) * 2; height = (height / 2) * 2;
LOG("Resizing image to %dx%d", width, height);
state->image = image_resize(state->source_image, width, height); state->image = image_resize(state->source_image, width, height);
if (state->args.dither) if (state->args.dither)
m_prepare_dither(state); m_prepare_dither(state);

View File

@ -32,12 +32,16 @@ void __bra_update2x4(image_t *img, rgba8 block[8], int x, int y)
void mod_braille_prepare(asc_state_t *state) void mod_braille_prepare(asc_state_t *state)
{ {
int w, h; int width, height;
get_size_keep_aspect( get_size_keep_aspect(
state->source_image->width, state->source_image->height, state->source_image->width, state->source_image->height,
state->args.width * 2, state->args.height * 4, &w, &h); state->args.width * 2, state->args.height * 4, &width, &height);
w = (w / 2) * 2; h = (h / 4) * 4; LOG("Source size: %dx%d", state->source_image->width,
state->image = image_resize(state->source_image, w, h); state->source_image->height);
LOG("Requested size: %dx%d", state->args.width * 2, state->args.height * 4);
width = (width / 2) * 2; height = (height / 4) * 4;
LOG("Resizing image to %dx%d", width, height);
state->image = image_resize(state->source_image, width, height);
if (state->args.dither) if (state->args.dither)
m_prepare_dither(state); m_prepare_dither(state);
} }