ansilove

ANSI and ASCII art to PNG converter in C
Log | Files | Refs | README | LICENSE

commit 613959a3dc65ea76e350dceeae5c48c48bbb8801
parent 6634d54e814f8772c28b1bbee5bfe4460a142afd
Author: Frederic Cambus <fred@statdns.com>
Date:   Fri, 22 Jul 2016 12:33:33 +0200

Converting the Artworx, Binary, PCBoard and XBin loaders to get input file buffer as function parameter

Diffstat:
Msrc/loaders/artworx.c | 31+------------------------------
Msrc/loaders/artworx.h | 2+-
Msrc/loaders/binary.c | 31+------------------------------
Msrc/loaders/binary.h | 2+-
Msrc/loaders/pcboard.c | 31+------------------------------
Msrc/loaders/pcboard.h | 2+-
Msrc/loaders/xbin.c | 31+------------------------------
Msrc/loaders/xbin.h | 2+-
Msrc/main.c | 8++++----
9 files changed, 12 insertions(+), 128 deletions(-)

diff --git a/src/loaders/artworx.c b/src/loaders/artworx.c @@ -11,40 +11,11 @@ #include "artworx.h" -void artworx(char *input, char *output, char *retinaout, bool createRetinaRep) +void artworx(unsigned char *input_file_buffer, int32_t input_file_size, char *output, char *retinaout, bool createRetinaRep) { const unsigned char *font_data; unsigned char *font_data_adf; - // load input file - FILE *input_file = fopen(input, "r"); - if (input_file == NULL) { - fputs("\nFile error.\n\n", stderr); exit (1); - } - - // get the file size (bytes) - size_t get_file_size = filesize(input); - int32_t input_file_size = (int32_t)get_file_size; - - // next up is loading our file into a dynamically allocated memory buffer - unsigned char *input_file_buffer; - int32_t result; - - // allocate memory to contain the whole file - input_file_buffer = (unsigned char *) malloc(sizeof(unsigned char)*input_file_size); - if (input_file_buffer == NULL) { - fputs ("\nMemory error.\n\n", stderr); exit (2); - } - - // copy the file into the buffer - result = fread(input_file_buffer, 1, input_file_size, input_file); - if (result != input_file_size) { - fputs ("\nReading error.\n\n", stderr); exit (3); - } // whole file is now loaded into input_file_buffer - - // close input file, we don't need it anymore - fclose(input_file); - // libgd image pointers gdImagePtr im_ADF; diff --git a/src/loaders/artworx.h b/src/loaders/artworx.h @@ -14,6 +14,6 @@ #ifndef artworx_h #define artworx_h -void artworx(char *input, char *output, char *retinaout, bool createRetinaRep); +void artworx(unsigned char *input_file_buffer, int32_t input_file_size, char *output, char *retinaout, bool createRetinaRep); #endif diff --git a/src/loaders/binary.c b/src/loaders/binary.c @@ -11,7 +11,7 @@ #include "binary.h" -void binary(char *input, char *output, char *retinaout, int32_t int_columns, char *font, int32_t int_bits, bool icecolors, bool createRetinaRep) +void binary(unsigned char *input_file_buffer, int32_t input_file_size, char *output, char *retinaout, int32_t int_columns, char *font, int32_t int_bits, bool icecolors, bool createRetinaRep) { // some type declarations struct fontStruct fontData; @@ -19,35 +19,6 @@ void binary(char *input, char *output, char *retinaout, int32_t int_columns, cha // font selection alSelectFont(&fontData, font); - // load input file - FILE *input_file = fopen(input, "r"); - if (input_file == NULL) { - fputs("\nFile error.\n\n", stderr); exit (1); - } - - // get the file size (bytes) - size_t get_file_size = filesize(input); - int32_t input_file_size = (int32_t)get_file_size; - - // next up is loading our file into a dynamically allocated memory buffer - unsigned char *input_file_buffer; - int32_t result; - - // allocate memory to contain the whole file - input_file_buffer = (unsigned char *) malloc(sizeof(unsigned char)*input_file_size); - if (input_file_buffer == NULL) { - fputs ("\nMemory error.\n\n", stderr); exit (2); - } - - // copy the file into the buffer - result = fread(input_file_buffer, 1, input_file_size, input_file); - if (result != input_file_size) { - fputs ("\nReading error.\n\n", stderr); exit (3); - } // whole file is now loaded into input_file_buffer - - // close input file, we don't need it anymore - fclose(input_file); - // libgd image pointers gdImagePtr im_Binary; diff --git a/src/loaders/binary.h b/src/loaders/binary.h @@ -14,7 +14,7 @@ #ifndef binary_h #define binary_h -void binary(char *input, char *output, char *retinaout, int32_t int_columns, char *font, int32_t int_bits, bool icecolors, bool createRetinaRep); +void binary(unsigned char *input_file_buffer, int32_t input_file_size, char *output, char *retinaout, int32_t int_columns, char *font, int32_t int_bits, bool icecolors, bool createRetinaRep); #endif diff --git a/src/loaders/pcboard.c b/src/loaders/pcboard.c @@ -11,7 +11,7 @@ #include "pcboard.h" -void pcboard(char *input, char *output, char *retinaout, char *font, int32_t int_bits, bool createRetinaRep) +void pcboard(unsigned char *input_file_buffer, int32_t input_file_size, char *output, char *retinaout, char *font, int32_t int_bits, bool createRetinaRep) { // some type declarations struct fontStruct fontData; @@ -21,35 +21,6 @@ void pcboard(char *input, char *output, char *retinaout, char *font, int32_t int // font selection alSelectFont(&fontData, font); - // load input file - FILE *input_file = fopen(input, "r"); - if (input_file == NULL) { - fputs("\nFile error.\n\n", stderr); exit (1); - } - - // get the file size (bytes) - size_t get_file_size = filesize(input); - int32_t input_file_size = (int32_t)get_file_size; - - // next up is loading our file into a dynamically allocated memory buffer - unsigned char *input_file_buffer; - int32_t result; - - // allocate memory to contain the whole file - input_file_buffer = (unsigned char *) malloc(sizeof(unsigned char)*input_file_size); - if (input_file_buffer == NULL) { - fputs ("\nMemory error.\n\n", stderr); exit (2); - } - - // copy the file into the buffer - result = fread(input_file_buffer, 1, input_file_size, input_file); - if (result != input_file_size) { - fputs ("\nReading error.\n\n", stderr); exit (3); - } // whole file is now loaded into input_file_buffer - - // close input file, we don't need it anymore - fclose(input_file); - // libgd image pointers gdImagePtr im_PCB; diff --git a/src/loaders/pcboard.h b/src/loaders/pcboard.h @@ -23,6 +23,6 @@ struct pcbChar { int32_t current_character; }; -void pcboard(char *input, char *output, char *retinaout, char *font, int32_t int_bits, bool createRetinaRep); +void pcboard(unsigned char *input_file_buffer, int32_t input_file_size, char *output, char *retinaout, char *font, int32_t int_bits, bool createRetinaRep); #endif diff --git a/src/loaders/xbin.c b/src/loaders/xbin.c @@ -11,40 +11,11 @@ #include "xbin.h" -void xbin(char *input, char *output, char *retinaout, bool createRetinaRep) +void xbin(unsigned char *input_file_buffer, int32_t input_file_size, char *output, char *retinaout, bool createRetinaRep) { const unsigned char *font_data; unsigned char *font_data_xbin; - // load input file - FILE *input_file = fopen(input, "r"); - if (input_file == NULL) { - fputs("\nFile error.\n\n", stderr); exit (1); - } - - // get the file size (bytes) - size_t get_file_size = filesize(input); - int32_t input_file_size = (int32_t)get_file_size; - - // next up is loading our file into a dynamically allocated memory buffer - unsigned char *input_file_buffer; - int32_t result; - - // allocate memory to contain the whole file - input_file_buffer = (unsigned char *) malloc(sizeof(unsigned char)*input_file_size); - if (input_file_buffer == NULL) { - fputs ("\nMemory error.\n\n", stderr); exit (2); - } - - // copy the file into the buffer - result = fread(input_file_buffer, 1, input_file_size, input_file); - if (result != input_file_size) { - fputs ("\nReading error.\n\n", stderr); exit (3); - } // whole file is now loaded into input_file_buffer - - // close input file, we don't need it anymore - fclose(input_file); - if (strncmp((char *)input_file_buffer, "XBIN\x1a", 5) != 0) { fputs("\nNot an XBin.\n\n", stderr); exit (4); } diff --git a/src/loaders/xbin.h b/src/loaders/xbin.h @@ -14,6 +14,6 @@ #ifndef xbin_h #define xbin_h -void xbin(char *input, char *output, char *retinaout, bool createRetinaRep); +void xbin(unsigned char *input_file_buffer, int32_t input_file_size, char *output, char *retinaout, bool createRetinaRep); #endif diff --git a/src/main.c b/src/main.c @@ -312,15 +312,15 @@ int main(int argc, char *argv[]) { // create the output file by invoking the appropiate function if (!strcmp(fext, ".pcb")) { // params: input, output, font, bits, icecolors - pcboard(input, outputFile, retinaout, font, int_bits, createRetinaRep); + pcboard(input_file_buffer, input_file_size, outputFile, retinaout, font, int_bits, createRetinaRep); fileIsPCBoard = true; } else if (!strcmp(fext, ".bin")) { // params: input, output, columns, font, bits, icecolors - binary(input, outputFile, retinaout, int_columns, font, int_bits, icecolors, createRetinaRep); + binary(input_file_buffer, input_file_size, outputFile, retinaout, int_columns, font, int_bits, icecolors, createRetinaRep); fileIsBinary = true; } else if (!strcmp(fext, ".adf")) { // params: input, output, bits - artworx(input, outputFile, retinaout, createRetinaRep); + artworx(input_file_buffer, input_file_size, outputFile, retinaout, createRetinaRep); } else if (!strcmp(fext, ".idf")) { // params: input, output, bits icedraw(input, outputFile, retinaout, fileHasSAUCE, createRetinaRep); @@ -329,7 +329,7 @@ int main(int argc, char *argv[]) { fileIsTundra = true; } else if (!strcmp(fext, ".xb")) { // params: input, output, bits - xbin(input, outputFile, retinaout, createRetinaRep); + xbin(input_file_buffer, input_file_size, outputFile, retinaout, createRetinaRep); } else { // params: input, output, font, bits, icecolors, fext ansi(input_file_buffer, input_file_size, outputFile, retinaout, font, int_bits, mode, icecolors, fext, createRetinaRep);