ansilove

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

commit ee1f9c2d71a3aeea1a230a33de5a54cfab6301ed
parent ba092eb82f31752af5539f57b7dcf85c6c3c51f5
Author: Frederic Cambus <fred@statdns.com>
Date:   Mon, 18 Dec 2017 22:12:34 +0100

Allow specifying custom retina scale factor

Diffstat:
Msrc/loaders/ansi.c | 4++--
Msrc/loaders/ansi.h | 2+-
Msrc/loaders/artworx.c | 4++--
Msrc/loaders/artworx.h | 2+-
Msrc/loaders/binary.c | 4++--
Msrc/loaders/binary.h | 2+-
Msrc/loaders/icedraw.c | 4++--
Msrc/loaders/icedraw.h | 2+-
Msrc/loaders/pcboard.c | 4++--
Msrc/loaders/pcboard.h | 2+-
Msrc/loaders/tundra.c | 4++--
Msrc/loaders/tundra.h | 2+-
Msrc/loaders/xbin.c | 4++--
Msrc/loaders/xbin.h | 2+-
Msrc/main.c | 35+++++++++++++++++++++--------------
Msrc/output.c | 9+++++----
Msrc/output.h | 2+-
17 files changed, 48 insertions(+), 40 deletions(-)

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 *outputFile, 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, int retinaScaleFactor) { // ladies and gentlemen, it's type declaration time struct fontStruct fontData; @@ -496,7 +496,7 @@ void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil } // create output image - output(canvas, outputFile, retinaout, createRetinaRep); + output(canvas, outputFile, retinaout, retinaScaleFactor); // free memory free(ansi_buffer); 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 *outputFile, 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, int retinaScaleFactor); #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 *outputFile, char *retinaout, bool createRetinaRep) +void artworx(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, int retinaScaleFactor) { const unsigned char *font_data; unsigned char *font_data_adf; @@ -81,7 +81,7 @@ void artworx(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output } // create output file - output(canvas, outputFile, retinaout, createRetinaRep); + output(canvas, outputFile, retinaout, retinaScaleFactor); // nuke garbage free(font_data_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 *outputFile, char *retinaout, bool createRetinaRep); +void artworx(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, int retinaScaleFactor); #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 *outputFile, 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, int retinaScaleFactor) { // binary files must have an even size if (inputFileSize % 2) { @@ -80,5 +80,5 @@ void binary(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputF } // create output image - output(canvas, outputFile, retinaout, createRetinaRep); + output(canvas, outputFile, retinaout, retinaScaleFactor); } 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 *outputFile, 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, int retinaScaleFactor); #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 *outputFile, char *retinaout, bool createRetinaRep) +void icedraw(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, int retinaScaleFactor) { const unsigned char *font_data; unsigned char *font_data_idf; @@ -137,7 +137,7 @@ void icedraw(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output } // create output file - output(canvas, outputFile, retinaout, createRetinaRep); + output(canvas, outputFile, retinaout, retinaScaleFactor); // free memory free(font_data_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 *outputFile, char *retinaout, bool createRetinaRep); +void icedraw(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, int retinaScaleFactor); #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 *outputFile, 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, int retinaScaleFactor) { // some type declarations struct fontStruct fontData; @@ -177,7 +177,7 @@ void pcboard(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output } // create output image - output(canvas, outputFile, retinaout, createRetinaRep); + output(canvas, outputFile, retinaout, retinaScaleFactor); // free memory free(pcboard_buffer); 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 *outputFile, 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, int retinaScaleFactor); #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 *outputFile, 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, int retinaScaleFactor) { // some type declarations struct fontStruct fontData; @@ -176,6 +176,6 @@ void tundra(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputF } // create output image - output(canvas, outputFile, retinaout, createRetinaRep); + output(canvas, outputFile, retinaout, retinaScaleFactor); } 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 *outputFile, 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, int retinaScaleFactor); #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 *outputFile, char *retinaout, bool createRetinaRep) +void xbin(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, int retinaScaleFactor) { const unsigned char *font_data; unsigned char *font_data_xbin = NULL; @@ -175,7 +175,7 @@ void xbin(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil } // create output file - output(canvas, outputFile, retinaout, createRetinaRep); + output(canvas, outputFile, retinaout, retinaScaleFactor); // nuke garbage free(font_data_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 *outputFile, char *retinaout, bool createRetinaRep); +void xbin(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, int retinaScaleFactor); #endif diff --git a/src/main.c b/src/main.c @@ -122,8 +122,8 @@ int main(int argc, char *argv[]) { bool justDisplaySAUCE = false; bool fileHasSAUCE = false; - // retina output bool type - bool createRetinaRep = false; + // retina output scale factor + int retinaScaleFactor = 0; // iCE colors bool type bool icecolors = false; @@ -155,7 +155,7 @@ int main(int argc, char *argv[]) { err(EXIT_FAILURE, "pledge"); } - while ((getoptFlag = getopt(argc, argv, "b:c:ef:him:o:rsv")) != -1) { + while ((getoptFlag = getopt(argc, argv, "b:c:ef:him:o:r:sv")) != -1) { switch(getoptFlag) { case 'b': // convert numeric command line flags to integer values @@ -196,7 +196,14 @@ int main(int argc, char *argv[]) { output = optarg; break; case 'r': - createRetinaRep = true; + // convert numeric command line flags to integer values + retinaScaleFactor = strtonum(optarg, 2, 4, &errstr); + + if (errstr) { + fprintf(stderr, "\nInvalid value for retina scale factor.\n\n"); + return EXIT_FAILURE; + } + break; case 's': justDisplaySAUCE = true; @@ -245,8 +252,8 @@ int main(int argc, char *argv[]) { outputFile = outputName; } - if (createRetinaRep) { - asprintf(&retinaout, "%s%s", outputName, "@2x.png"); + if (retinaScaleFactor) { + asprintf(&retinaout, "%s@%ix.png", outputName, retinaScaleFactor); } // default to empty string if mode option is not specified @@ -263,7 +270,7 @@ int main(int argc, char *argv[]) { fprintf(stderr, "\nInput File: %s\n", input); fprintf(stderr, "Output File: %s\n", outputFile); - if (createRetinaRep) { + if (retinaScaleFactor) { fprintf(stderr, "Retina Output File: %s\n", retinaout); } @@ -317,27 +324,27 @@ 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(inputFileBuffer, inputFileSize, outputFile, retinaout, font, bits, createRetinaRep); + pcboard(inputFileBuffer, inputFileSize, outputFile, retinaout, font, bits, retinaScaleFactor); fileIsPCBoard = true; } else if (!strcmp(fext, ".bin")) { // params: input, output, columns, font, bits, icecolors - binary(inputFileBuffer, inputFileSize, outputFile, retinaout, columns, font, bits, icecolors, createRetinaRep); + binary(inputFileBuffer, inputFileSize, outputFile, retinaout, columns, font, bits, icecolors, retinaScaleFactor); fileIsBinary = true; } else if (!strcmp(fext, ".adf")) { // params: input, output, bits - artworx(inputFileBuffer, inputFileSize, outputFile, retinaout, createRetinaRep); + artworx(inputFileBuffer, inputFileSize, outputFile, retinaout, retinaScaleFactor); } else if (!strcmp(fext, ".idf")) { // params: input, output, bits - icedraw(inputFileBuffer, inputFileSize, outputFile, retinaout, createRetinaRep); + icedraw(inputFileBuffer, inputFileSize, outputFile, retinaout, retinaScaleFactor); } else if (!strcmp(fext, ".tnd")) { - tundra(inputFileBuffer, inputFileSize, outputFile, retinaout, font, bits, createRetinaRep); + tundra(inputFileBuffer, inputFileSize, outputFile, retinaout, font, bits, retinaScaleFactor); fileIsTundra = true; } else if (!strcmp(fext, ".xb")) { // params: input, output, bits - xbin(inputFileBuffer, inputFileSize, outputFile, retinaout, createRetinaRep); + xbin(inputFileBuffer, inputFileSize, outputFile, retinaout, retinaScaleFactor); } else { // params: input, output, font, bits, icecolors, fext - ansi(inputFileBuffer, inputFileSize, outputFile, retinaout, font, bits, mode, icecolors, fext, createRetinaRep); + ansi(inputFileBuffer, inputFileSize, outputFile, retinaout, font, bits, mode, icecolors, fext, retinaScaleFactor); fileIsANSi = true; } diff --git a/src/output.c b/src/output.c @@ -11,7 +11,7 @@ #include "output.h" -void output(gdImagePtr im_Source, char *output, char *retinaout, bool createRetinaRep) { +void output(gdImagePtr im_Source, char *output, char *retinaout, int retinaScaleFactor) { FILE *file_Out = fopen(output, "wb"); if (file_Out) { @@ -23,11 +23,12 @@ void output(gdImagePtr im_Source, char *output, char *retinaout, bool createReti } // in case Retina image output is wanted - if (createRetinaRep) { + if (retinaScaleFactor) { gdImagePtr im_Retina; - // make the Retina image @2x as large as im_Source - im_Retina = gdImageCreate(im_Source->sx * 2, im_Source->sy * 2); + // make the Retina image retinaScaleFactor as large as im_Source + im_Retina = gdImageCreate(im_Source->sx * retinaScaleFactor, + im_Source->sy * retinaScaleFactor); gdImageCopyResized(im_Retina, im_Source, 0, 0, 0, 0, im_Retina->sx, im_Retina->sy, diff --git a/src/output.h b/src/output.h @@ -18,6 +18,6 @@ #define output_h // prototypes -void output(gdImagePtr im_Source, char *output, char *retinaout, bool createRetinaRep); +void output(gdImagePtr im_Source, char *output, char *retinaout, int retinaScaleFactor); #endif