libansilove

Library for converting ANSI, ASCII, and other formats to PNG
Log | Files | Refs | README | LICENSE

commit 4ad1436424b2cfcd794be53efeef7ebac4f815d3
parent 3f39c3d54b90947bb122d0c2de8341295a9134df
Author: Frederic Cambus <fred@statdns.com>
Date:   Sun, 11 Oct 2020 09:58:44 +0200

Move variables declaration to the top of each loader function.

Diffstat:
Msrc/loaders/ansi.c | 79++++++++++++++++++++++++++++++++++++++-----------------------------------------
Msrc/loaders/artworx.c | 18+++++++++---------
Msrc/loaders/binary.c | 24+++++++++++-------------
Msrc/loaders/icedraw.c | 24+++++++++++-------------
Msrc/loaders/pcboard.c | 30++++++++++++++----------------
Msrc/loaders/tundra.c | 21++++++++++-----------
Msrc/loaders/xbin.c | 22++++++++++------------
7 files changed, 103 insertions(+), 115 deletions(-)

diff --git a/src/loaders/ansi.c b/src/loaders/ansi.c @@ -50,47 +50,8 @@ struct ansiChar { int ansilove_ansi(struct ansilove_ctx *ctx, struct ansilove_options *options) { - if (ctx == NULL || options == NULL) { - if (ctx) - ctx->error = ANSILOVE_INVALID_PARAM; - - return -1; - } - - if (!ctx->length) { - ctx->error = ANSILOVE_FORMAT_ERROR; - goto error; - } - - /* ladies and gentlemen, it's type declaration time */ - struct fontStruct fontData; - - /* Default to 80 columns if columns option wasn't set */ - options->columns = options->columns ? options->columns : 80; - - int16_t columns = options->columns; - - bool ced = false; - bool workbench = false; - const char *errstr; - /* font selection */ - memset(&fontData, 0, sizeof(struct fontStruct)); - select_font(&fontData, options->font); - - switch (options->mode) { - case ANSILOVE_MODE_CED: - ced = true; - break; - case ANSILOVE_MODE_WORKBENCH: - workbench = true; - break; - } - - /* libgd image pointers */ - gdImagePtr canvas; - /* ANSi processing loops */ size_t loop = 0, ansi_sequence_loop; @@ -102,6 +63,7 @@ ansilove_ansi(struct ansilove_ctx *ctx, struct ansilove_options *options) /* default color values */ uint32_t background = 0, foreground = 7; uint32_t background24 = 0, foreground24 = 0; + uint32_t colors[16]; /* text attributes */ bool bold = false, blink = false, invert = false; @@ -119,9 +81,46 @@ ansilove_ansi(struct ansilove_ctx *ctx, struct ansilove_options *options) /* ANSi buffer structure array definition */ size_t structIndex = 0; struct ansiChar *ptr, *ansi_buffer; + struct fontStruct fontData; size_t ansi_buffer_size = ANSI_BUFFER_SIZE; + /* libgd image pointers */ + gdImagePtr canvas; + + if (ctx == NULL || options == NULL) { + if (ctx) + ctx->error = ANSILOVE_INVALID_PARAM; + + return -1; + } + + if (!ctx->length) { + ctx->error = ANSILOVE_FORMAT_ERROR; + goto error; + } + + /* Default to 80 columns if columns option wasn't set */ + options->columns = options->columns ? options->columns : 80; + + int16_t columns = options->columns; + + bool ced = false; + bool workbench = false; + + /* font selection */ + memset(&fontData, 0, sizeof(struct fontStruct)); + select_font(&fontData, options->font); + + switch (options->mode) { + case ANSILOVE_MODE_CED: + ced = true; + break; + case ANSILOVE_MODE_WORKBENCH: + workbench = true; + break; + } + /* ANSi buffer dynamic memory allocation */ ansi_buffer = malloc(ansi_buffer_size * sizeof(struct ansiChar)); @@ -505,8 +504,6 @@ ansilove_ansi(struct ansilove_ctx *ctx, struct ansilove_options *options) goto error; } - uint32_t colors[16]; - uint32_t ced_background = 0, ced_foreground = 0; if (ced) { diff --git a/src/loaders/artworx.c b/src/loaders/artworx.c @@ -26,6 +26,15 @@ int ansilove_artworx(struct ansilove_ctx *ctx, struct ansilove_options *options) { + uint8_t character, attribute, *cursor, state = STATE_CHARACTER; + uint32_t column = 0, row = 0; + uint32_t foreground, background; + uint32_t width, height; + size_t index, loop; + + /* libgd image pointers */ + gdImagePtr canvas; + if (ctx == NULL || options == NULL) { if (ctx) ctx->error = ANSILOVE_INVALID_PARAM; @@ -38,12 +47,7 @@ ansilove_artworx(struct ansilove_ctx *ctx, struct ansilove_options *options) return -1; } - /* libgd image pointers */ - gdImagePtr canvas; - /* create ADF instance */ - - uint32_t width, height; width = 640; height = (ctx->length - ADF_HEADER_LENGTH) / 2 / 80 * 16; @@ -59,7 +63,6 @@ ansilove_artworx(struct ansilove_ctx *ctx, struct ansilove_options *options) return -1; } - size_t index, loop; /* process ADF palette */ for (loop = 0; loop < 16; loop++) { @@ -71,9 +74,6 @@ ansilove_artworx(struct ansilove_ctx *ctx, struct ansilove_options *options) } /* process ADF */ - uint8_t character, attribute, *cursor, state = STATE_CHARACTER; - uint32_t column = 0, row = 0; - uint32_t foreground, background; loop = ADF_HEADER_LENGTH; while (loop < ctx->length) { diff --git a/src/loaders/binary.c b/src/loaders/binary.c @@ -26,6 +26,17 @@ int ansilove_binary(struct ansilove_ctx *ctx, struct ansilove_options *options) { + uint8_t character, attribute, *cursor, state = STATE_CHARACTER; + int32_t column = 0, row = 0; + uint32_t background, foreground; + uint32_t width, height; + uint32_t colors[16]; + size_t loop = 0; + struct fontStruct fontData; + + /* libgd image pointers */ + gdImagePtr canvas; + if (ctx == NULL || options == NULL) { if (ctx) ctx->error = ANSILOVE_INVALID_PARAM; @@ -38,19 +49,13 @@ ansilove_binary(struct ansilove_ctx *ctx, struct ansilove_options *options) return -1; } - struct fontStruct fontData; - /* font selection */ memset(&fontData, 0, sizeof(struct fontStruct)); select_font(&fontData, options->font); - /* libgd image pointers */ - gdImagePtr canvas; - /* Default to 160 columns if columns option wasn't set */ options->columns = options->columns ? options->columns : 160; - uint32_t width, height; width = options->columns * options->bits; height = ctx->length / 2 / options->columns * fontData.height; @@ -68,18 +73,11 @@ ansilove_binary(struct ansilove_ctx *ctx, struct ansilove_options *options) } /* allocate color palette */ - uint32_t colors[16]; - for (size_t i = 0; i < 16; i++) colors[i] = gdImageColorAllocate(canvas, vga_palette_red[i], vga_palette_green[i], vga_palette_blue[i]); /* process binary */ - uint8_t character, attribute, *cursor, state = STATE_CHARACTER; - uint32_t background, foreground; - size_t loop = 0; - int32_t column = 0, row = 0; - while (loop < ctx->length) { cursor = &ctx->buffer[loop]; diff --git a/src/loaders/icedraw.c b/src/loaders/icedraw.c @@ -24,6 +24,17 @@ int ansilove_icedraw(struct ansilove_ctx *ctx, struct ansilove_options *options) { + size_t index, loop = IDF_HEADER_LENGTH; + uint8_t *ptr, *idf_buffer; + uint32_t width, height; + uint32_t column = 0, row = 0; + uint32_t character, attribute, foreground, background; + uint32_t colors[16]; + uint32_t idf_sequence_length, i = 0; + + /* libgd image pointers */ + gdImagePtr canvas; + if (ctx == NULL || options == NULL) { if (ctx) ctx->error = ANSILOVE_INVALID_PARAM; @@ -39,17 +50,8 @@ ansilove_icedraw(struct ansilove_ctx *ctx, struct ansilove_options *options) /* Get number of columns, 16-bit endian unsigned short */ uint32_t x2 = (ctx->buffer[9] << 8) + ctx->buffer[8] + 1; - /* libgd image pointers */ - gdImagePtr canvas; - - size_t index, loop = IDF_HEADER_LENGTH; - uint32_t colors[16]; - /* process IDF */ - uint32_t idf_sequence_length, i = 0; - /* dynamically allocated memory buffer for IDF data */ - uint8_t *ptr, *idf_buffer; idf_buffer = malloc(2); if (idf_buffer == NULL) { @@ -96,7 +98,6 @@ ansilove_icedraw(struct ansilove_ctx *ctx, struct ansilove_options *options) loop += 2; } - uint32_t width, height; width = x2 * 8; height = i / 2 / 80 * 16; @@ -124,9 +125,6 @@ ansilove_icedraw(struct ansilove_ctx *ctx, struct ansilove_options *options) } /* render IDF */ - uint32_t column = 0, row = 0; - uint32_t character, attribute, foreground, background; - for (loop = 0; loop < i; loop += 2) { if (column == x2) { column = 0; diff --git a/src/loaders/pcboard.c b/src/loaders/pcboard.c @@ -40,6 +40,20 @@ struct pcbChar { int ansilove_pcboard(struct ansilove_ctx *ctx, struct ansilove_options *options) { + uint8_t *cursor, state = STATE_TEXT; + uint32_t background = '0', foreground = '7'; + uint32_t column = 0, row = 0, rowMax = 0; + uint32_t width, height; + uint32_t colors[16]; + size_t loop = 0, structIndex = 0; + struct fontStruct fontData; + + /* PCB buffer structure array definition */ + struct pcbChar *ptr, *pcboard_buffer; + + /* libgd image pointers */ + gdImagePtr canvas; + if (ctx == NULL || options == NULL) { if (ctx) ctx->error = ANSILOVE_INVALID_PARAM; @@ -52,9 +66,6 @@ ansilove_pcboard(struct ansilove_ctx *ctx, struct ansilove_options *options) goto error; } - struct fontStruct fontData; - size_t loop = 0, structIndex = 0; - options->columns = options->columns ? options->columns : 80; uint16_t columns = options->columns; @@ -62,16 +73,6 @@ ansilove_pcboard(struct ansilove_ctx *ctx, struct ansilove_options *options) memset(&fontData, 0, sizeof(struct fontStruct)); select_font(&fontData, options->font); - /* libgd image pointers */ - gdImagePtr canvas; - - uint8_t *cursor, state = STATE_TEXT; - uint32_t background = '0', foreground = '7'; - uint32_t column = 0, row = 0, rowMax = 0; - - /* PCB buffer structure array definition */ - struct pcbChar *ptr, *pcboard_buffer; - /* PCB buffer dynamic memory allocation */ pcboard_buffer = malloc(sizeof (struct pcbChar)); @@ -174,7 +175,6 @@ ansilove_pcboard(struct ansilove_ctx *ctx, struct ansilove_options *options) } rowMax++; - uint32_t width, height; width = columns * options->bits; height = rowMax * fontData.height; @@ -192,8 +192,6 @@ ansilove_pcboard(struct ansilove_ctx *ctx, struct ansilove_options *options) } /* allocate color palette */ - uint32_t colors[16]; - for (size_t i = 0; i < 16; i++) colors[i] = gdImageColorAllocate(canvas, ansi_palette_red[i], ansi_palette_green[i], ansi_palette_blue[i]); diff --git a/src/loaders/tundra.c b/src/loaders/tundra.c @@ -32,6 +32,16 @@ int ansilove_tundra(struct ansilove_ctx *ctx, struct ansilove_options *options) { + char tundra_version; + int32_t column = 0, row = 1; + uint32_t cursor, character, background = 0, foreground = 0; + uint32_t width, height; + size_t loop = TUNDRA_HEADER_LENGTH; + struct fontStruct fontData; + + /* libgd image pointers */ + gdImagePtr canvas; + if (ctx == NULL || options == NULL) { if (ctx) ctx->error = ANSILOVE_INVALID_PARAM; @@ -42,9 +52,6 @@ ansilove_tundra(struct ansilove_ctx *ctx, struct ansilove_options *options) if (ctx->length < TUNDRA_HEADER_LENGTH) goto error; - struct fontStruct fontData; - char tundra_version; - options->columns = options->columns ? options->columns : 80; int16_t columns = options->columns; @@ -52,9 +59,6 @@ ansilove_tundra(struct ansilove_ctx *ctx, struct ansilove_options *options) memset(&fontData, 0, sizeof(struct fontStruct)); select_font(&fontData, options->font); - /* libgd image pointers */ - gdImagePtr canvas; - /* extract tundra header */ tundra_version = ctx->buffer[0]; @@ -63,10 +67,6 @@ ansilove_tundra(struct ansilove_ctx *ctx, struct ansilove_options *options) goto error; /* read tundra file a first time to find the image size */ - uint32_t cursor, character, background = 0, foreground = 0; - size_t loop = TUNDRA_HEADER_LENGTH; - int32_t column = 0, row = 1; - while (loop < ctx->length) { if (column == columns) { column = 0; @@ -113,7 +113,6 @@ ansilove_tundra(struct ansilove_ctx *ctx, struct ansilove_options *options) loop++; } - uint32_t width, height; width = columns * options->bits; height = row * fontData.height; diff --git a/src/loaders/xbin.c b/src/loaders/xbin.c @@ -26,6 +26,16 @@ int ansilove_xbin(struct ansilove_ctx *ctx, struct ansilove_options *options) { + const uint8_t *font_data; + uint8_t *font_data_xbin = NULL; + uint32_t width, height; + uint32_t colors[16]; + uint32_t offset = XBIN_HEADER_LENGTH; + uint32_t column = 0, row = 0, foreground, background; + int32_t character, attribute; + + gdImagePtr canvas; + if (ctx == NULL || options == NULL) { if (ctx) ctx->error = ANSILOVE_INVALID_PARAM; @@ -38,9 +48,6 @@ ansilove_xbin(struct ansilove_ctx *ctx, struct ansilove_options *options) return -1; } - const uint8_t *font_data; - uint8_t *font_data_xbin = NULL; - if (strncmp((char *)ctx->buffer, "XBIN\x1a", 5) != 0) { ctx->error = ANSILOVE_FORMAT_ERROR; return -1; @@ -60,9 +67,6 @@ ansilove_xbin(struct ansilove_ctx *ctx, struct ansilove_options *options) return -1; } - gdImagePtr canvas; - - uint32_t width, height; width = 8 * xbin_width; height = xbin_fontsize * xbin_height; @@ -78,9 +82,6 @@ ansilove_xbin(struct ansilove_ctx *ctx, struct ansilove_options *options) return -1; } - uint32_t colors[16]; - uint32_t offset = XBIN_HEADER_LENGTH; - /* palette */ if ((xbin_flags & 1) == 1) { size_t index, loop; @@ -135,9 +136,6 @@ ansilove_xbin(struct ansilove_ctx *ctx, struct ansilove_options *options) xbin_fontsize = 16; } - uint32_t column = 0, row = 0, foreground, background; - int32_t character, attribute; - /* read compressed xbin */ if ((xbin_flags & 4) == 4) { while (offset + 1 < ctx->length && row != xbin_height) {