Added proper logging
This commit is contained in:
parent
153533ff86
commit
d3ccc83203
20
src/args.c
20
src/args.c
|
@ -89,7 +89,9 @@ void usage(int argc, char **argv)
|
|||
fprintf(stderr, "[-vhd] [-O FILE] [-W WIDTH] [-H HEIGHT] ");
|
||||
fprintf(stderr, "[-M MODE] [-S STYLE] [-F FORMAT] [-P PALETTE] ");
|
||||
fprintf(stderr, "FILE\n\n");
|
||||
#ifndef DISABLE_LOGGING
|
||||
fprintf(stderr, "-v\t\tEnable verbose mode\n");
|
||||
#endif
|
||||
fprintf(stderr, "-V\t\tShow version\n");
|
||||
fprintf(stderr, "-h\t\tShow this help\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->mode = ASC_MOD_BLOCKS;
|
||||
args->dither = false;
|
||||
#ifndef DISABLE_LOGGING
|
||||
args->verbose = false;
|
||||
#endif
|
||||
args->charset = " .'-*+$@";
|
||||
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)
|
||||
#endif
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
#ifndef DISABLE_LOGGING
|
||||
case 'v':
|
||||
args->verbose = true;
|
||||
break;
|
||||
#endif
|
||||
case 'h':
|
||||
usage(argc, argv);
|
||||
return 1;
|
||||
|
@ -239,6 +249,9 @@ int parse_args(int argc, char **argv, asc_args_t *args)
|
|||
return -2;
|
||||
}
|
||||
args->input_filename = argv[optind];
|
||||
#ifndef DISABLE_LOGGING
|
||||
b_logging = args->verbose;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -253,6 +266,7 @@ int prepare_state(int argc, char **argv, asc_args_t args, asc_state_t *state)
|
|||
|
||||
// Loading image
|
||||
FILE *image_file;
|
||||
LOG("Opening image file %s", args.input_filename);
|
||||
if ((image_file = fopen(args.input_filename, "rb")) == NULL
|
||||
|| 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));
|
||||
return -100 - err;
|
||||
}
|
||||
LOG("Loading image data...");
|
||||
state->source_image = image_load(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
|
||||
switch (args.out_style)
|
||||
{
|
||||
case ASC_STL_PALETTE:
|
||||
{
|
||||
LOG("Loading palette file...");
|
||||
FILE *fp = fopen(args.palette_filename, "rb");
|
||||
if (fp == NULL)
|
||||
{
|
||||
|
@ -285,6 +304,7 @@ int prepare_state(int argc, char **argv, asc_args_t args, asc_state_t *state)
|
|||
return -7;
|
||||
}
|
||||
fclose(fp);
|
||||
LOG("Loaded %d colors", state->palette->n_colors);
|
||||
}
|
||||
break;
|
||||
case ASC_STL_256COLOR:
|
||||
|
|
|
@ -57,7 +57,9 @@ typedef struct {
|
|||
asc_style_t out_style;
|
||||
asc_mode_t mode;
|
||||
bool dither;
|
||||
#ifndef DISABLE_LOGGING
|
||||
bool verbose;
|
||||
#endif
|
||||
char *charset;
|
||||
} asc_args_t;
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include "colors.h"
|
||||
#include "commons.h"
|
||||
#include <math.h>
|
||||
#include <stdbool.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)
|
||||
{
|
||||
LOG("Creating 256-colors palette");
|
||||
if (dst->n_colors == 256) return;
|
||||
dst->n_colors = 256;
|
||||
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)
|
||||
{
|
||||
LOG("Loading GIMP palette");
|
||||
static char buf[8192];
|
||||
fgets(buf, 8192, fp); // GIMP Palette
|
||||
fgets(buf, 8192, fp); // Name: %s
|
||||
LOG("Palette name: %s", buf);
|
||||
fgets(buf, 8192, fp); // Columns: %d
|
||||
|
||||
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)
|
||||
{
|
||||
LOG("Loading raw RGB palette");
|
||||
while (!feof(fp) && pal->n_colors < 256)
|
||||
{
|
||||
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)
|
||||
{
|
||||
LOG("Guessing palette type");
|
||||
static char head[16];
|
||||
if (fread(head, sizeof(char), 12, fp) < 12) return false;
|
||||
if (fseek(fp, 0, SEEK_SET) != 0) return false;
|
||||
|
|
|
@ -2,6 +2,11 @@
|
|||
#include "commons.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#ifndef DISABLE_LOGGING
|
||||
bool b_logging = false;
|
||||
#endif
|
||||
|
||||
void m_prepare_dither(asc_state_t *sta)
|
||||
{
|
||||
|
@ -26,6 +31,19 @@ void m_prepare_dither(asc_state_t *sta)
|
|||
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)
|
||||
{
|
||||
fprintf(stderr, "Error: %s\n", reason);
|
||||
|
|
|
@ -21,6 +21,16 @@
|
|||
#include "args.h"
|
||||
|
||||
#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 m_prepare_dither(asc_state_t *state);
|
||||
|
|
|
@ -19,6 +19,7 @@ image_t *image_load(FILE *file)
|
|||
|
||||
image_t *image_resize(image_t *img, int width, int height)
|
||||
{
|
||||
LOG("Resizing image %p", img);
|
||||
image_t *res = calloc(1, sizeof(image_t));
|
||||
res->width = width;
|
||||
res->height = height;
|
||||
|
@ -32,8 +33,10 @@ image_t *image_resize(image_t *img, int width, int height)
|
|||
}
|
||||
else
|
||||
{
|
||||
LOG("Same dimensions, just copying original data");
|
||||
memcpy(res->pixels, img->pixels, width * height * sizeof(rgba8));
|
||||
}
|
||||
LOG("Resized image: %p", 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)
|
||||
{
|
||||
LOG("Dithering image %p", img);
|
||||
image_t *res = calloc(1, sizeof(image_t));
|
||||
int w = res->width = img->width;
|
||||
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);
|
||||
}
|
||||
}
|
||||
LOG("Dithered image: %p", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
@ -94,6 +99,7 @@ image_t *image_dither(image_t *img, palette_t pal)
|
|||
|
||||
void image_unload(image_t *img)
|
||||
{
|
||||
LOG("Unloading image %p", img);
|
||||
free(img->pixels);
|
||||
free(img);
|
||||
}
|
||||
|
|
19
src/main.c
19
src/main.c
|
@ -29,33 +29,18 @@ int main(int argc, char **argv)
|
|||
asc_args_t args;
|
||||
int res = parse_args(argc, argv, &args);
|
||||
if (res == 1) return 0;
|
||||
if (res < 0) return -res;
|
||||
else if (res < 0) return -res;
|
||||
|
||||
asc_state_t state;
|
||||
res = prepare_state(argc, argv, args, &state);
|
||||
if (res == 1) return 0;
|
||||
if (res < 0) return -res;
|
||||
|
||||
if (args.verbose)
|
||||
{
|
||||
fprintf(stderr, "Source image size: %dx%d\n",
|
||||
state.source_image->width,
|
||||
state.source_image->height);
|
||||
}
|
||||
else if (res < 0) return -res;
|
||||
|
||||
asc_handler_t handler = asc_handlers[args.mode];
|
||||
if (handler.prepare == NULL)
|
||||
c_fatal(12, "this mode is not implemented yet");
|
||||
|
||||
handler.prepare(&state);
|
||||
|
||||
if (args.verbose)
|
||||
{
|
||||
fprintf(stderr, "Resized image size: %dx%d\n",
|
||||
state.image->width,
|
||||
state.image->height);
|
||||
}
|
||||
|
||||
handler.main(state);
|
||||
|
||||
image_unload(state.image);
|
||||
|
|
|
@ -25,7 +25,11 @@ void mod_blocks_prepare(asc_state_t *state)
|
|||
get_size_keep_aspect(
|
||||
state->source_image->width, state->source_image->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;
|
||||
LOG("Resizing image to %dx%d", width, height);
|
||||
state->image = image_resize(state->source_image, width, height);
|
||||
if (state->args.dither)
|
||||
m_prepare_dither(state);
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
int w, h;
|
||||
int width, height;
|
||||
get_size_keep_aspect(
|
||||
state->source_image->width, state->source_image->height,
|
||||
state->args.width * 2, state->args.height * 4, &w, &h);
|
||||
w = (w / 2) * 2; h = (h / 4) * 4;
|
||||
state->image = image_resize(state->source_image, w, h);
|
||||
state->args.width * 2, state->args.height * 4, &width, &height);
|
||||
LOG("Source size: %dx%d", state->source_image->width,
|
||||
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)
|
||||
m_prepare_dither(state);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue