ansilove

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

commit ca293e615d6d9916f64b6929054b4dfcad661f86
parent 2672671afa7c28fd3c416ceb658817e297002a3d
Author: Frederic Cambus <fred@statdns.com>
Date:   Tue, 18 Apr 2017 11:41:12 +0200

Create an output function to unify the way output files are created (Fixes #12)

Diffstat:
MCMakeLists.txt | 2+-
Msrc/ansilove.h | 2+-
Msrc/loaders/ansi.c | 13++-----------
Msrc/loaders/ansi.h | 2+-
Msrc/loaders/artworx.c | 13++-----------
Msrc/loaders/artworx.h | 2+-
Msrc/loaders/binary.c | 14++------------
Msrc/loaders/binary.h | 2+-
Msrc/loaders/icedraw.c | 13++-----------
Msrc/loaders/icedraw.h | 2+-
Msrc/loaders/pcboard.c | 14++------------
Msrc/loaders/pcboard.h | 2+-
Msrc/loaders/tundra.c | 14++------------
Msrc/loaders/tundra.h | 2+-
Msrc/loaders/xbin.c | 13++-----------
Msrc/loaders/xbin.h | 2+-
Asrc/output.c | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/output.h | 23+++++++++++++++++++++++
Dsrc/retina.c | 30------------------------------
Dsrc/retina.h | 21---------------------
20 files changed, 97 insertions(+), 140 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -18,7 +18,7 @@ find_path(GD_INCLUDE_DIRS gd.h) find_library(GD_LIBRARIES NAMES gd REQUIRED) include_directories(${GD_INCLUDE_DIRS}) -set(SRC src/main.c src/fonts.c src/ansilove.c src/explode.c src/strtolower.c src/retina.c src/sauce.c) +set(SRC src/main.c src/fonts.c src/ansilove.c src/explode.c src/strtolower.c src/output.c src/sauce.c) set(LOADERS src/loaders/ansi.c src/loaders/artworx.c src/loaders/binary.c src/loaders/icedraw.c src/loaders/pcboard.c src/loaders/tundra.c src/loaders/xbin.c) diff --git a/src/ansilove.h b/src/ansilove.h @@ -19,7 +19,7 @@ #include "config.h" #include "fonts.h" #include "explode.h" -#include "retina.h" +#include "output.h" #include "sauce.h" #ifndef HAVE_STRTONUM diff --git a/src/loaders/ansi.c b/src/loaders/ansi.c @@ -15,7 +15,7 @@ #include "ansi.h" -void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output, char *retinaout, char *font, int32_t bits, char *mode, bool icecolors, char *fext, bool createRetinaRep) +void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, char *font, int32_t bits, char *mode, bool icecolors, char *fext, bool createRetinaRep) { // ladies and gentlemen, it's type declaration time struct fontStruct fontData; @@ -512,17 +512,8 @@ void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output, c } // create output image - FILE *file_Out = fopen(output, "wb"); - gdImagePng(im_ANSi, file_Out); - fclose(file_Out); - - // in case Retina image output is wanted - if (createRetinaRep) { - retina(im_ANSi, retinaout); - } + output(im_ANSi, outputFile, retinaout, createRetinaRep); // free memory free(ansi_buffer); - - gdImageDestroy(im_ANSi); } diff --git a/src/loaders/ansi.h b/src/loaders/ansi.h @@ -26,6 +26,6 @@ struct ansiChar { bool underline; }; -void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output, char *retinaout, char *font, int32_t bits, char *mode, bool icecolors, char *fext, bool createRetinaRep); +void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, char *font, int32_t bits, char *mode, bool icecolors, char *fext, bool createRetinaRep); #endif diff --git a/src/loaders/artworx.c b/src/loaders/artworx.c @@ -11,7 +11,7 @@ #include "artworx.h" -void artworx(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output, char *retinaout, bool createRetinaRep) +void artworx(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, bool createRetinaRep) { const unsigned char *font_data; unsigned char *font_data_adf; @@ -81,17 +81,8 @@ void artworx(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output } // create output file - FILE *file_Out = fopen(output, "wb"); - gdImagePng(im_ADF, file_Out); - fclose(file_Out); - - // in case Retina image output is wanted - if (createRetinaRep) { - retina(im_ADF, retinaout); - } + output(im_ADF, outputFile, retinaout, createRetinaRep); // nuke garbage free(font_data_adf); - - gdImageDestroy(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(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output, char *retinaout, bool createRetinaRep); +void artworx(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, 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(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output, char *retinaout, int32_t columns, char *font, int32_t bits, bool icecolors, bool createRetinaRep) +void binary(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, int32_t columns, char *font, int32_t bits, bool icecolors, bool createRetinaRep) { // some type declarations struct fontStruct fontData; @@ -85,15 +85,5 @@ void binary(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output, } // create output image - FILE *file_Out = fopen(output, "wb"); - gdImagePng(im_Binary, file_Out); - fclose(file_Out); - - // in case Retina image output is wanted - if (createRetinaRep) { - retina(im_Binary, retinaout); - } - - // free memory - gdImageDestroy(im_Binary); + output(im_Binary, outputFile, retinaout, createRetinaRep); } diff --git a/src/loaders/binary.h b/src/loaders/binary.h @@ -14,7 +14,7 @@ #ifndef binary_h #define binary_h -void binary(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output, char *retinaout, int32_t columns, char *font, int32_t bits, bool icecolors, bool createRetinaRep); +void binary(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, int32_t columns, char *font, int32_t bits, bool icecolors, bool createRetinaRep); #endif diff --git a/src/loaders/icedraw.c b/src/loaders/icedraw.c @@ -11,7 +11,7 @@ #include "icedraw.h" -void icedraw(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output, char *retinaout, bool createRetinaRep) +void icedraw(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, bool createRetinaRep) { const unsigned char *font_data; unsigned char *font_data_idf; @@ -140,17 +140,8 @@ void icedraw(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output free(idf_buffer); // create output file - FILE *file_Out = fopen(output, "wb"); - gdImagePng(im_IDF, file_Out); - fclose(file_Out); - - // in case Retina image output is wanted - if (createRetinaRep) { - retina(im_IDF, retinaout); - } + output(im_IDF, outputFile, retinaout, createRetinaRep); // nuke garbage free (font_data_idf); - - gdImageDestroy(im_IDF); } diff --git a/src/loaders/icedraw.h b/src/loaders/icedraw.h @@ -14,7 +14,7 @@ #ifndef icedraw_h #define icedraw_h -void icedraw(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output, char *retinaout, bool createRetinaRep); +void icedraw(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, bool createRetinaRep); #endif diff --git a/src/loaders/pcboard.c b/src/loaders/pcboard.c @@ -11,7 +11,7 @@ #include "pcboard.h" -void pcboard(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output, char *retinaout, char *font, int32_t bits, bool createRetinaRep) +void pcboard(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, char *font, int32_t bits, bool createRetinaRep) { // some type declarations struct fontStruct fontData; @@ -188,18 +188,8 @@ void pcboard(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output } // create output image - FILE *file_Out = fopen(output, "wb"); - gdImagePng(im_PCB, file_Out); - fclose(file_Out); - - // in case Retina image output is wanted - if (createRetinaRep) - { - retina(im_PCB, retinaout); - } + output(im_PCB, outputFile, retinaout, createRetinaRep); // free memory free(pcboard_buffer); - - gdImageDestroy(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(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output, char *retinaout, char *font, int32_t bits, bool createRetinaRep); +void pcboard(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, char *font, int32_t bits, bool createRetinaRep); #endif diff --git a/src/loaders/tundra.c b/src/loaders/tundra.c @@ -11,7 +11,7 @@ #include "tundra.h" -void tundra(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output, char *retinaout, char *font, int32_t bits, bool createRetinaRep) +void tundra(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, char *font, int32_t bits, bool createRetinaRep) { // some type declarations struct fontStruct fontData; @@ -179,16 +179,6 @@ void tundra(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output, } // create output image - FILE *file_Out = fopen(output, "wb"); - gdImagePng(im_Tundra, file_Out); - fclose(file_Out); - - // in case Retina image output is wanted - if (createRetinaRep) { - retina(im_Tundra, retinaout); - } - - // free memory - gdImageDestroy(im_Tundra); + output(im_Tundra, outputFile, retinaout, createRetinaRep); } diff --git a/src/loaders/tundra.h b/src/loaders/tundra.h @@ -14,6 +14,6 @@ #ifndef tundra_h #define tundra_h -void tundra(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output, char *retinaout, char *font, int32_t bits, bool createRetinaRep); +void tundra(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, char *font, int32_t bits, bool createRetinaRep); #endif diff --git a/src/loaders/xbin.c b/src/loaders/xbin.c @@ -11,7 +11,7 @@ #include "xbin.h" -void xbin(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output, char *retinaout, bool createRetinaRep) +void xbin(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, bool createRetinaRep) { const unsigned char *font_data; unsigned char *font_data_xbin = NULL; @@ -186,17 +186,8 @@ void xbin(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output, c } // create output file - FILE *file_Out = fopen(output, "wb"); - gdImagePng(im_XBIN, file_Out); - fclose(file_Out); - - // in case Retina image output is wanted - if (createRetinaRep) { - retina(im_XBIN, retinaout); - } + output(im_XBIN, outputFile, retinaout, createRetinaRep); // nuke garbage free(font_data_xbin); - - gdImageDestroy(im_XBIN); } diff --git a/src/loaders/xbin.h b/src/loaders/xbin.h @@ -14,6 +14,6 @@ #ifndef xbin_h #define xbin_h -void xbin(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output, char *retinaout, bool createRetinaRep); +void xbin(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, bool createRetinaRep); #endif diff --git a/src/output.c b/src/output.c @@ -0,0 +1,51 @@ +// +// output.c +// AnsiLove/C +// +// Copyright (C) 2011-2017 Stefan Vogt, Brian Cassidy, and Frederic Cambus. +// All rights reserved. +// +// This source code is licensed under the BSD 2-Clause License. +// See the file LICENSE for details. +// + +#include "output.h" + +void output(gdImagePtr im_Source, char *output, char *retinaout, bool createRetinaRep) { + FILE *file_Out = fopen(output, "wb"); + + if (file_Out) { + gdImagePng(im_Source, file_Out); + fclose(file_Out); + } else { + perror("Can't create output file"); + exit(1); + } + + // in case Retina image output is wanted + if (createRetinaRep) { + gdImagePtr im_Retina; + + // make the Retina image @2x as large as im_Source + im_Retina = gdImageCreate(im_Source->sx * 2, im_Source->sy * 2); + + gdImageCopyResized(im_Retina, im_Source, 0, 0, 0, 0, + im_Retina->sx, im_Retina->sy, + im_Source->sx, im_Source->sy); + + // create retina output image + FILE *file_RetinaOut = fopen(retinaout, "wb"); + + if (file_RetinaOut) { + gdImagePng(im_Retina, file_RetinaOut); + fclose(file_RetinaOut); + } else { + perror("Can't create output file"); + exit(1); + } + + gdImageDestroy(im_Retina); + } + + gdImageDestroy(im_Source); +} diff --git a/src/output.h b/src/output.h @@ -0,0 +1,23 @@ +// +// output.h +// AnsiLove/C +// +// Copyright (C) 2011-2017 Stefan Vogt, Brian Cassidy, and Frederic Cambus. +// All rights reserved. +// +// This source code is licensed under the BSD 2-Clause License. +// See the file LICENSE for details. +// + +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <gd.h> + +#ifndef output_h +#define output_h + +// prototypes +void output(gdImagePtr im_Source, char *output, char *retinaout, bool createRetinaRep); + +#endif diff --git a/src/retina.c b/src/retina.c @@ -1,30 +0,0 @@ -// -// retina.c -// AnsiLove/C -// -// Copyright (C) 2011-2017 Stefan Vogt, Brian Cassidy, and Frederic Cambus. -// All rights reserved. -// -// This source code is licensed under the BSD 2-Clause License. -// See the file LICENSE for details. -// - -#include "retina.h" - -void retina(gdImagePtr im_Source, char *retinaout) { - gdImagePtr im_Retina; - - // make the Retina image @2x as large as im_Source - im_Retina = gdImageCreate(im_Source->sx * 2, im_Source->sy * 2); - - gdImageCopyResized(im_Retina, im_Source, 0, 0, 0, 0, - im_Retina->sx, im_Retina->sy, - im_Source->sx, im_Source->sy); - - // create retina output image - FILE *file_RetinaOut = fopen(retinaout, "wb"); - gdImagePng(im_Retina, file_RetinaOut); - fclose(file_RetinaOut); - - gdImageDestroy(im_Retina); -} diff --git a/src/retina.h b/src/retina.h @@ -1,21 +0,0 @@ -// -// retina.h -// AnsiLove/C -// -// Copyright (C) 2011-2017 Stefan Vogt, Brian Cassidy, and Frederic Cambus. -// All rights reserved. -// -// This source code is licensed under the BSD 2-Clause License. -// See the file LICENSE for details. -// - -#include <stdio.h> -#include <gd.h> - -#ifndef retina_h -#define retina_h - -// prototypes -void retina(gdImagePtr im_Source, char *retinaout); - -#endif