ansilove

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

commit dab827d445597c02f4914342db3e2859c3edc22d
parent 18b9d1802c44f9a212445d917d05d96265619bf4
Author: Frederic Cambus <fred@statdns.com>
Date:   Sat, 16 Jan 2016 17:54:22 +0100

Moving the Binary format loader to its own files

Diffstat:
MCMakeLists.txt | 2+-
Msrc/ansilove.c | 132-------------------------------------------------------------------------------
Msrc/ansilove.h | 1-
Asrc/binary.c | 143+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/binary.h | 18++++++++++++++++++
Msrc/main.c | 2++
6 files changed, 164 insertions(+), 134 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -17,7 +17,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/albinfonts.c src/ansilove.c src/explode.c src/filesize.c src/strtolower.c src/substr.c src/sauce.c) +set(SRC src/main.c src/albinfonts.c src/ansilove.c src/binary.c src/explode.c src/filesize.c src/strtolower.c src/substr.c src/sauce.c) if(NOT HAVE_STRTONUM) set (SRC ${SRC} compat/strtonum.c) diff --git a/src/ansilove.c b/src/ansilove.c @@ -841,138 +841,6 @@ void alPcBoardLoader(char *input, char *output, char *retinaout, char *font, int gdImageDestroy(im_PCB); } -// BINARY -void alBinaryLoader(char *input, char *output, char *retinaout, int32_t int_columns, char *font, int32_t int_bits, bool icecolors, bool createRetinaRep) -{ - // some type declarations - struct fontStruct fontData; - - // 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; - - // allocate buffer image memory - im_Binary = gdImageCreate(int_columns * int_bits, - ((input_file_size / 2) / int_columns * fontData.font_size_y)); - - if (!im_Binary) { - fputs ("\nError, can't allocate buffer image memory.\n\n", stderr); exit (6); - } - - // allocate black color - gdImageColorAllocate(im_Binary, 0, 0, 0); - - // allocate color palette - int32_t colors[16]; - - colors[0] = gdImageColorAllocate(im_Binary, 0, 0, 0); - colors[1] = gdImageColorAllocate(im_Binary, 0, 0, 170); - colors[2] = gdImageColorAllocate(im_Binary, 0, 170, 0); - colors[3] = gdImageColorAllocate(im_Binary, 0, 170, 170); - colors[4] = gdImageColorAllocate(im_Binary, 170, 0, 0); - colors[5] = gdImageColorAllocate(im_Binary, 170, 0, 170); - colors[6] = gdImageColorAllocate(im_Binary, 170, 85, 0); - colors[7] = gdImageColorAllocate(im_Binary, 170, 170, 170); - colors[8] = gdImageColorAllocate(im_Binary, 85, 85, 85); - colors[9] = gdImageColorAllocate(im_Binary, 85, 85, 255); - colors[10] = gdImageColorAllocate(im_Binary, 85, 255, 85); - colors[11] = gdImageColorAllocate(im_Binary, 85, 255, 255); - colors[12] = gdImageColorAllocate(im_Binary, 255, 85, 85); - colors[13] = gdImageColorAllocate(im_Binary, 255, 85, 255); - colors[14] = gdImageColorAllocate(im_Binary, 255, 255, 85); - colors[15] = gdImageColorAllocate(im_Binary, 255, 255, 255); - - // process binary - int32_t character, attribute, color_background, color_foreground; - int32_t loop = 0, position_x = 0, position_y = 0; - - while (loop < input_file_size) - { - if (position_x == int_columns) - { - position_x = 0; - position_y++; - } - - character = input_file_buffer[loop]; - attribute = input_file_buffer[loop+1]; - - color_background = (attribute & 240) >> 4; - color_foreground = (attribute & 15); - - - if (color_background > 8 && !icecolors) - { - color_background -= 8; - } - - alDrawChar(im_Binary, fontData.font_data, int_bits, fontData.font_size_y, - position_x, position_y, colors[color_background], colors[color_foreground], character); - - position_x++; - loop+=2; - } - - // 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) - { - gdImagePtr im_RetinaANSi; - - // make the Retina image @2x as large as im_Binary - im_RetinaANSi = gdImageCreate(im_Binary->sx * 2, im_Binary->sy * 2); - - gdImageCopyResized(im_RetinaANSi, im_Binary, 0, 0, 0, 0, - im_RetinaANSi->sx, im_RetinaANSi->sy, - im_Binary->sx, im_Binary->sy); - - // create retina output image - FILE *file_RetinaOut = fopen(retinaout, "wb"); - gdImagePng(im_RetinaANSi, file_RetinaOut); - fclose(file_RetinaOut); - - gdImageDestroy(im_RetinaANSi); - } - - // free memory - gdImageDestroy(im_Binary); -} - // ADF void alArtworxLoader(char *input, char *output, char *retinaout, bool createRetinaRep) { diff --git a/src/ansilove.h b/src/ansilove.h @@ -34,7 +34,6 @@ void alDrawChar(gdImagePtr im, const unsigned char *font_data, int32_t int_bits, void alAnsiLoader(char *input, char *output, char *retinaout, char *font, int32_t int_bits, char *mode, bool icecolors, char *fext, bool createRetinaRep); void alPcBoardLoader(char *input, char *output, char *retinaout, char *font, int32_t int_bits, bool createRetinaRep); -void alBinaryLoader(char *input, char *output, char *retinaout, int32_t int_columns, char *font, int32_t int_bits, bool icecolors, bool createRetinaRep); void alArtworxLoader(char *input, char *output, char *retinaout, bool createRetinaRep); void alIcedrawLoader(char *input, char *output, char *retinaout, bool fileHasSAUCE, bool createRetinaRep); void alTundraLoader(char *input, char *output, char *retinaout, char *font, int32_t int_bits, bool fileHasSAUCE, bool createRetinaRep); diff --git a/src/binary.c b/src/binary.c @@ -0,0 +1,143 @@ +// +// binary.c +// AnsiLove/C +// +// Copyright (C) 2011-2016 Stefan Vogt, Brian Cassidy, Frederic Cambus. +// All rights reserved. +// +// This source code is licensed under the BSD 3-Clause License. +// See the file LICENSE for details. +// + +#include "ansilove.h" + +void alBinaryLoader(char *input, char *output, char *retinaout, int32_t int_columns, char *font, int32_t int_bits, bool icecolors, bool createRetinaRep) +{ + // some type declarations + struct fontStruct fontData; + + // 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; + + // allocate buffer image memory + im_Binary = gdImageCreate(int_columns * int_bits, + ((input_file_size / 2) / int_columns * fontData.font_size_y)); + + if (!im_Binary) { + fputs ("\nError, can't allocate buffer image memory.\n\n", stderr); exit (6); + } + + // allocate black color + gdImageColorAllocate(im_Binary, 0, 0, 0); + + // allocate color palette + int32_t colors[16]; + + colors[0] = gdImageColorAllocate(im_Binary, 0, 0, 0); + colors[1] = gdImageColorAllocate(im_Binary, 0, 0, 170); + colors[2] = gdImageColorAllocate(im_Binary, 0, 170, 0); + colors[3] = gdImageColorAllocate(im_Binary, 0, 170, 170); + colors[4] = gdImageColorAllocate(im_Binary, 170, 0, 0); + colors[5] = gdImageColorAllocate(im_Binary, 170, 0, 170); + colors[6] = gdImageColorAllocate(im_Binary, 170, 85, 0); + colors[7] = gdImageColorAllocate(im_Binary, 170, 170, 170); + colors[8] = gdImageColorAllocate(im_Binary, 85, 85, 85); + colors[9] = gdImageColorAllocate(im_Binary, 85, 85, 255); + colors[10] = gdImageColorAllocate(im_Binary, 85, 255, 85); + colors[11] = gdImageColorAllocate(im_Binary, 85, 255, 255); + colors[12] = gdImageColorAllocate(im_Binary, 255, 85, 85); + colors[13] = gdImageColorAllocate(im_Binary, 255, 85, 255); + colors[14] = gdImageColorAllocate(im_Binary, 255, 255, 85); + colors[15] = gdImageColorAllocate(im_Binary, 255, 255, 255); + + // process binary + int32_t character, attribute, color_background, color_foreground; + int32_t loop = 0, position_x = 0, position_y = 0; + + while (loop < input_file_size) + { + if (position_x == int_columns) + { + position_x = 0; + position_y++; + } + + character = input_file_buffer[loop]; + attribute = input_file_buffer[loop+1]; + + color_background = (attribute & 240) >> 4; + color_foreground = (attribute & 15); + + + if (color_background > 8 && !icecolors) + { + color_background -= 8; + } + + alDrawChar(im_Binary, fontData.font_data, int_bits, fontData.font_size_y, + position_x, position_y, colors[color_background], colors[color_foreground], character); + + position_x++; + loop+=2; + } + + // 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) + { + gdImagePtr im_RetinaANSi; + + // make the Retina image @2x as large as im_Binary + im_RetinaANSi = gdImageCreate(im_Binary->sx * 2, im_Binary->sy * 2); + + gdImageCopyResized(im_RetinaANSi, im_Binary, 0, 0, 0, 0, + im_RetinaANSi->sx, im_RetinaANSi->sy, + im_Binary->sx, im_Binary->sy); + + // create retina output image + FILE *file_RetinaOut = fopen(retinaout, "wb"); + gdImagePng(im_RetinaANSi, file_RetinaOut); + fclose(file_RetinaOut); + + gdImageDestroy(im_RetinaANSi); + } + + // free memory + gdImageDestroy(im_Binary); +} diff --git a/src/binary.h b/src/binary.h @@ -0,0 +1,18 @@ +// +// binary.h +// AnsiLove/C +// +// Copyright (C) 2011-2016 Stefan Vogt, Brian Cassidy, Frederic Cambus. +// All rights reserved. +// +// This source code is licensed under the BSD 3-Clause License. +// See the file LICENSE for details. +// + +#ifndef binary_h +#define binary_h + +void alBinaryLoader(char *input, char *output, char *retinaout, int32_t int_columns, char *font, int32_t int_bits, bool icecolors, bool createRetinaRep); + +#endif + diff --git a/src/main.c b/src/main.c @@ -25,6 +25,8 @@ #include "ansilove.h" #include "sauce.h" +#include "binary.h" + // prototypes void showHelp(void); void listExamples(void);