ansilove

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

commit f579dd46be04685f8a38a06036330e3270579d61
parent b3980facdc2471c40b411e3d2b9e3d5f4e3d941c
Author: Frederic Cambus <fred@statdns.com>
Date:   Fri, 22 Jul 2016 12:44:07 +0200

Converting the IceDraw and Tundra loaders to get input file buffer as function parameter

Diffstat:
Msrc/loaders/icedraw.c | 37+------------------------------------
Msrc/loaders/icedraw.h | 2+-
Msrc/loaders/tundra.c | 36+-----------------------------------
Msrc/loaders/tundra.h | 2+-
Msrc/main.c | 4++--
5 files changed, 6 insertions(+), 75 deletions(-)

diff --git a/src/loaders/icedraw.c b/src/loaders/icedraw.c @@ -11,46 +11,11 @@ #include "icedraw.h" -void icedraw(char *input, char *output, char *retinaout, bool fileHasSAUCE, bool createRetinaRep) +void icedraw(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_idf; - // 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 - - // IDF related: file contains a SAUCE record? adjust the file size - if(fileHasSAUCE) { - sauce *saucerec = sauceReadFile(input_file); - input_file_size -= 129 - ( saucerec->comments > 0 ? 5 + 64 * saucerec->comments : 0); - } - - // close input file, we don't need it anymore - fclose(input_file); - // extract relevant part of the IDF header, 16-bit endian unsigned short int32_t x2 = (input_file_buffer[9] << 8) + input_file_buffer[8]; diff --git a/src/loaders/icedraw.h b/src/loaders/icedraw.h @@ -14,7 +14,7 @@ #ifndef icedraw_h #define icedraw_h -void icedraw(char *input, char *output, char *retinaout, bool fileHasSAUCE, bool createRetinaRep); +void icedraw(unsigned char *input_file_buffer, int32_t input_file_size, char *output, char *retinaout, bool createRetinaRep); #endif diff --git a/src/loaders/tundra.c b/src/loaders/tundra.c @@ -11,7 +11,7 @@ #include "tundra.h" -void tundra(char *input, char *output, char *retinaout, char *font, int32_t int_bits, bool fileHasSAUCE, bool createRetinaRep) +void tundra(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; @@ -22,40 +22,6 @@ void tundra(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 - - // exclude SAUCE record from file buffer - if(fileHasSAUCE) { - sauce *saucerec = sauceReadFile(input_file); - input_file_size -= 129 - ( saucerec->comments > 0 ? 5 + 64 * saucerec->comments : 0); - } - // close input file, we don't need it anymore - fclose(input_file); - // libgd image pointers gdImagePtr im_Tundra; diff --git a/src/loaders/tundra.h b/src/loaders/tundra.h @@ -14,6 +14,6 @@ #ifndef tundra_h #define tundra_h -void tundra(char *input, char *output, char *retinaout, char *font, int32_t int_bits, bool fileHasSAUCE, bool createRetinaRep); +void tundra(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/main.c b/src/main.c @@ -329,9 +329,9 @@ int main(int argc, char *argv[]) { 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); + icedraw(input_file_buffer, input_file_size, outputFile, retinaout, createRetinaRep); } else if (!strcmp(fext, ".tnd")) { - tundra(input, outputFile, retinaout, font, int_bits, fileHasSAUCE, createRetinaRep); + tundra(input_file_buffer, input_file_size, outputFile, retinaout, font, int_bits, createRetinaRep); fileIsTundra = true; } else if (!strcmp(fext, ".xb")) { // params: input, output, bits