libansilove

Library for converting ANSI, ASCII, and other formats to PNG
Log | Files | Refs | README | LICENSE

commit 5994f152ef230f181f93aa746dab9cbc7647499f
parent 97aa5e6d83044db6ad2d305f570e1b48da250ff4
Author: Frederic Cambus <fred@statdns.com>
Date:   Mon, 22 Jan 2018 21:25:12 +0100

Use input and output structures to pass parameters to the loaders

Diffstat:
Minclude/ansilove.h | 35++++++++++++++++++++++++++++-------
Msrc/ansilove.h | 1+
Msrc/loaders/ansi.c | 51++++++++++++++++++++++++++-------------------------
Msrc/loaders/artworx.c | 30+++++++++++++++---------------
Msrc/loaders/binary.c | 28++++++++++++++--------------
Msrc/loaders/icedraw.c | 34+++++++++++++++++-----------------
Msrc/loaders/pcboard.c | 43+++++++++++++++++++++----------------------
Msrc/loaders/tundra.c | 71+++++++++++++++++++++++++++++++++++------------------------------------
Msrc/loaders/xbin.c | 54+++++++++++++++++++++++++++---------------------------
9 files changed, 184 insertions(+), 163 deletions(-)

diff --git a/include/ansilove.h b/include/ansilove.h @@ -12,12 +12,33 @@ #ifndef ansilove_h #define ansilove_h -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); -void artworx(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, int retinaScaleFactor); -void binary(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, int32_t columns, char *font, int32_t bits, bool icecolors, int retinaScaleFactor); -void icedraw(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, int retinaScaleFactor); -void pcboard(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, char *font, int32_t bits, int retinaScaleFactor); -void tundra(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, char *font, int32_t bits, int retinaScaleFactor); -void xbin(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, int retinaScaleFactor); +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> + +struct input { + unsigned char *data; + size_t size; + char *fext; + uint32_t columns; +}; + +struct output { + char *fileName; + char *retina; + char *font; + uint32_t bits; + char *mode; + bool icecolors; + uint32_t retinaScaleFactor; +}; + +void ansi(struct input *, struct output *); +void artworx(struct input *, struct output *); +void binary(struct input *, struct output *); +void icedraw(struct input *, struct output *); +void pcboard(struct input *, struct output *); +void tundra(struct input *, struct output *); +void xbin(struct input *, struct output *); #endif diff --git a/src/ansilove.h b/src/ansilove.h @@ -9,6 +9,7 @@ // See the LICENSE file for details. // +#include "../include/ansilove.h" #include "config.h" #include "drawchar.h" #include "explode.h" diff --git a/src/loaders/ansi.c b/src/loaders/ansi.c @@ -25,7 +25,7 @@ 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, int retinaScaleFactor) +void ansi(struct input *inputFile, struct output *outputFile) { // ladies and gentlemen, it's type declaration time struct fontStruct fontData; @@ -40,21 +40,21 @@ void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil const char *errstr; // font selection - alSelectFont(&fontData, font); + alSelectFont(&fontData, outputFile->font); // to deal with the bits flag, we declared handy bool types - if (!strcmp(mode, "ced")) { + if (!strcmp(outputFile->mode, "ced")) { ced = true; } - else if (!strcmp(mode, "transparent")) { + else if (!strcmp(outputFile->mode, "transparent")) { transparent = true; } - else if (!strcmp(mode, "workbench")) { + else if (!strcmp(outputFile->mode, "workbench")) { workbench = true; } // check if current file has a .diz extension - if (!strcmp(fext, ".diz")) { + if (!strcmp(inputFile->fext, ".diz")) { isDizFile = true; } @@ -62,7 +62,8 @@ void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil gdImagePtr canvas; // ANSi processing loops - int32_t loop = 0, ansi_sequence_loop, seq_graphics_loop; + size_t loop = 0; + uint32_t ansi_sequence_loop, seq_graphics_loop; // character definitions int32_t current_character, next_character, character; @@ -79,7 +80,7 @@ void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil int32_t saved_row = 0, saved_column = 0; // sequence parsing variables - int32_t seqValue, seqArrayCount, seq_line, seq_column; + uint32_t seqValue, seqArrayCount, seq_line, seq_column; char *seqGrab; char **seqArray; @@ -91,10 +92,10 @@ void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil ansi_buffer = malloc(sizeof (struct ansiChar)); // ANSi interpreter - while (loop < inputFileSize) + while (loop < inputFile->size) { - current_character = inputFileBuffer[loop]; - next_character = inputFileBuffer[loop + 1]; + current_character = inputFile->data[loop]; + next_character = inputFile->data[loop + 1]; if (column == 80) { @@ -133,13 +134,13 @@ void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil { for (ansi_sequence_loop = 0; ansi_sequence_loop < 12; ansi_sequence_loop++) { - ansi_sequence_character = inputFileBuffer[loop + 2 + ansi_sequence_loop]; + ansi_sequence_character = inputFile->data[loop + 2 + ansi_sequence_loop]; // cursor position if (ansi_sequence_character == 'H' || ansi_sequence_character == 'f') { // create substring from the sequence's content - seqGrab = strndup((char *)inputFileBuffer + loop + 2, ansi_sequence_loop); + seqGrab = strndup((char *)inputFile->data + loop + 2, ansi_sequence_loop); // create sequence content array seqArrayCount = explode(&seqArray, ';', seqGrab); @@ -167,7 +168,7 @@ void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil if (ansi_sequence_character == 'A') { // create substring from the sequence's content - seqGrab = strndup((char *)inputFileBuffer + loop + 2, ansi_sequence_loop); + seqGrab = strndup((char *)inputFile->data + loop + 2, ansi_sequence_loop); // now get escape sequence's position value int32_t seq_line = strtonum(seqGrab, 0, INT32_MAX, &errstr); @@ -183,7 +184,7 @@ void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil if (ansi_sequence_character == 'B') { // create substring from the sequence's content - seqGrab = strndup((char *)inputFileBuffer + loop + 2, ansi_sequence_loop); + seqGrab = strndup((char *)inputFile->data + loop + 2, ansi_sequence_loop); // now get escape sequence's position value int32_t seq_line = strtonum(seqGrab, 0, INT32_MAX, &errstr); @@ -199,7 +200,7 @@ void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil if (ansi_sequence_character == 'C') { // create substring from the sequence's content - seqGrab = strndup((char *)inputFileBuffer + loop + 2, ansi_sequence_loop); + seqGrab = strndup((char *)inputFile->data + loop + 2, ansi_sequence_loop); // now get escape sequence's position value int32_t seq_column = strtonum(seqGrab, 0, INT32_MAX, &errstr); @@ -220,7 +221,7 @@ void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil if (ansi_sequence_character == 'D') { // create substring from the sequence's content - seqGrab = strndup((char *)inputFileBuffer + loop + 2, ansi_sequence_loop); + seqGrab = strndup((char *)inputFile->data + loop + 2, ansi_sequence_loop); // now get escape sequence's content length int32_t seq_column = strtonum(seqGrab, 0, INT32_MAX, &errstr); @@ -261,7 +262,7 @@ void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil if (ansi_sequence_character == 'J') { // create substring from the sequence's content - seqGrab = strndup((char *)inputFileBuffer + loop + 2, ansi_sequence_loop); + seqGrab = strndup((char *)inputFile->data + loop + 2, ansi_sequence_loop); // convert grab to an integer int32_t eraseDisplayInt = strtonum(seqGrab, 0, INT32_MAX, &errstr); @@ -288,7 +289,7 @@ void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil if (ansi_sequence_character == 'm') { // create substring from the sequence's content - seqGrab = strndup((char *)inputFileBuffer + loop + 2, ansi_sequence_loop); + seqGrab = strndup((char *)inputFile->data + loop + 2, ansi_sequence_loop); // create sequence content array seqArrayCount = explode(&seqArray, ';', seqGrab); @@ -352,7 +353,7 @@ void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil { background = seqValue - 40; - if (blink && icecolors) + if (blink && outputFile->icecolors) { background += 8; } @@ -435,7 +436,7 @@ void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil } // create that damn thingy - canvas = gdImageCreate(columns * bits, (rowMax)*fontData.height); + canvas = gdImageCreate(columns * outputFile->bits, (rowMax)*fontData.height); if (!canvas) { perror("Can't allocate ANSi buffer image memory"); @@ -474,7 +475,7 @@ void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil } // even more definitions, sigh - int32_t ansiBufferItems = structIndex; + uint32_t ansiBufferItems = structIndex; // render ANSi for (loop = 0; loop < ansiBufferItems; loop++) @@ -490,10 +491,10 @@ void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil row = ansi_buffer[loop].row; if (ced) { - drawchar(canvas, fontData.font_data, bits, fontData.height, + drawchar(canvas, fontData.font_data, outputFile->bits, fontData.height, column, row, ced_background, ced_foreground, character); } else { - drawchar(canvas, fontData.font_data, bits, fontData.height, + drawchar(canvas, fontData.font_data, outputFile->bits, fontData.height, column, row, colors[background], colors[foreground], character); } @@ -506,7 +507,7 @@ void ansi(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil } // create output image - output(canvas, outputFile, retinaout, retinaScaleFactor); + output(canvas, outputFile->fileName, outputFile->retina, outputFile->retinaScaleFactor); // free memory free(ansi_buffer); diff --git a/src/loaders/artworx.c b/src/loaders/artworx.c @@ -11,7 +11,7 @@ #include "../ansilove.h" -void artworx(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, int retinaScaleFactor) +void artworx(struct input *inputFile, struct output *outputFile) { const unsigned char *font_data; unsigned char *font_data_adf; @@ -20,7 +20,7 @@ void artworx(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output gdImagePtr canvas; // create ADF instance - canvas = gdImageCreate(640, (((inputFileSize - 192 - 4096 -1) / 2) / 80) * 16); + canvas = gdImageCreate(640, (((inputFile->size - 192 - 4096 -1) / 2) / 80) * 16); // error output if (!canvas) { @@ -29,10 +29,10 @@ void artworx(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output } // ADF color palette array - int32_t adf_colors[16] = { 0, 1, 2, 3, 4, 5, 20, 7, 56, 57, 58, 59, 60, 61, 62, 63 }; + uint32_t adf_colors[16] = { 0, 1, 2, 3, 4, 5, 20, 7, 56, 57, 58, 59, 60, 61, 62, 63 }; - int32_t loop; - int32_t index; + uint32_t loop; + uint32_t index; // process ADF font font_data_adf = (unsigned char *)malloc(sizeof (unsigned char)*4096); @@ -40,7 +40,7 @@ void artworx(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output perror("Memory error"); exit(7); } - memcpy(font_data_adf, inputFileBuffer+193, 4096); + memcpy(font_data_adf, inputFile->data+193, 4096); font_data = font_data_adf; @@ -48,19 +48,19 @@ void artworx(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output for (loop = 0; loop < 16; loop++) { index = (adf_colors[loop] * 3) + 1; - gdImageColorAllocate(canvas, (inputFileBuffer[index] << 2 | inputFileBuffer[index] >> 4), - (inputFileBuffer[index + 1] << 2 | inputFileBuffer[index + 1] >> 4), - (inputFileBuffer[index + 2] << 2 | inputFileBuffer[index + 2] >> 4)); + gdImageColorAllocate(canvas, (inputFile->data[index] << 2 | inputFile->data[index] >> 4), + (inputFile->data[index + 1] << 2 | inputFile->data[index + 1] >> 4), + (inputFile->data[index + 2] << 2 | inputFile->data[index + 2] >> 4)); } gdImageColorAllocate(canvas, 0, 0, 0); // process ADF - int32_t column = 0, row = 0; - int32_t character, attribute, foreground, background; + uint32_t column = 0, row = 0; + uint32_t character, attribute, foreground, background; loop = 192 + 4096 + 1; - while (loop < inputFileSize) + while (loop < inputFile->size) { if (column == 80) { @@ -68,8 +68,8 @@ void artworx(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output row++; } - character = inputFileBuffer[loop]; - attribute = inputFileBuffer[loop+1]; + character = inputFile->data[loop]; + attribute = inputFile->data[loop+1]; background = (attribute & 240) >> 4; foreground = attribute & 15; @@ -81,7 +81,7 @@ void artworx(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output } // create output file - output(canvas, outputFile, retinaout, retinaScaleFactor); + output(canvas, outputFile->fileName, outputFile->retina, outputFile->retinaScaleFactor); // nuke garbage free(font_data_adf); diff --git a/src/loaders/binary.c b/src/loaders/binary.c @@ -11,10 +11,10 @@ #include "../ansilove.h" -void binary(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, int32_t columns, char *font, int32_t bits, bool icecolors, int retinaScaleFactor) +void binary(struct input *inputFile, struct output *outputFile) { // binary files must have an even size - if (inputFileSize % 2) { + if (inputFile->size % 2) { fprintf(stderr, "\nBinary file is not valid.\n"); exit(1); } @@ -23,14 +23,14 @@ void binary(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputF struct fontStruct fontData; // font selection - alSelectFont(&fontData, font); + alSelectFont(&fontData, outputFile->font); // libgd image pointers gdImagePtr canvas; // allocate buffer image memory - canvas = gdImageCreate(columns * bits, - ((inputFileSize / 2) / columns * fontData.height)); + canvas = gdImageCreate(inputFile->columns * outputFile->bits, + ((inputFile->size / 2) / inputFile->columns * fontData.height)); if (!canvas) { perror("Error, can't allocate buffer image memory"); @@ -50,29 +50,29 @@ void binary(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputF } // process binary - int32_t character, attribute, background, foreground; - int32_t loop = 0, column = 0, row = 0; + uint32_t character, attribute, background, foreground; + uint32_t loop = 0, column = 0, row = 0; - while (loop < inputFileSize) + while (loop < inputFile->size) { - if (column == columns) + if (column == inputFile->columns) { column = 0; row++; } - character = inputFileBuffer[loop]; - attribute = inputFileBuffer[loop+1]; + character = inputFile->data[loop]; + attribute = inputFile->data[loop+1]; background = (attribute & 240) >> 4; foreground = (attribute & 15); - if (background > 8 && !icecolors) + if (background > 8 && !outputFile->icecolors) { background -= 8; } - drawchar(canvas, fontData.font_data, bits, fontData.height, + drawchar(canvas, fontData.font_data, outputFile->bits, fontData.height, column, row, colors[background], colors[foreground], character); column++; @@ -80,5 +80,5 @@ void binary(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputF } // create output image - output(canvas, outputFile, retinaout, retinaScaleFactor); + output(canvas, outputFile->fileName, outputFile->retina, outputFile->retinaScaleFactor); } diff --git a/src/loaders/icedraw.c b/src/loaders/icedraw.c @@ -11,18 +11,18 @@ #include "../ansilove.h" -void icedraw(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, int retinaScaleFactor) +void icedraw(struct input *inputFile, struct output *outputFile) { const unsigned char *font_data; unsigned char *font_data_idf; // extract relevant part of the IDF header, 16-bit endian unsigned short - int32_t x2 = (inputFileBuffer[9] << 8) + inputFileBuffer[8]; + int32_t x2 = (inputFile->data[9] << 8) + inputFile->data[8]; // libgd image pointers gdImagePtr canvas; - int32_t loop; + uint32_t loop; int32_t index; int32_t colors[16]; @@ -32,13 +32,13 @@ void icedraw(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output perror("Memory error"); exit(7); } - memcpy(font_data_idf, inputFileBuffer+(inputFileSize - 48 - 4096), 4096); + memcpy(font_data_idf, inputFile->data+(inputFile->size - 48 - 4096), 4096); font_data = font_data_idf; // process IDF loop = 12; - int32_t idf_sequence_length, idf_sequence_loop, i = 0; + uint32_t idf_sequence_length, idf_sequence_loop, i = 0; // dynamically allocated memory buffer for IDF data unsigned char *idf_buffer, *temp; @@ -46,14 +46,14 @@ void icedraw(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output int16_t idf_data, idf_data_length; - while (loop < inputFileSize - 4096 - 48) + while (loop < inputFile->size - 4096 - 48) { - memcpy(&idf_data, inputFileBuffer+loop, 2); + memcpy(&idf_data, inputFile->data+loop, 2); // RLE compressed data if (idf_data == 1) { - memcpy(&idf_data_length, inputFileBuffer+loop+2, 2); + memcpy(&idf_data_length, inputFile->data+loop+2, 2); idf_sequence_length = idf_data_length & 255; @@ -69,8 +69,8 @@ void icedraw(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output exit(7); } - idf_buffer[i] = inputFileBuffer[loop + 4]; - idf_buffer[i+1] = inputFileBuffer[loop + 5]; + idf_buffer[i] = inputFile->data[loop + 4]; + idf_buffer[i+1] = inputFile->data[loop + 5]; i += 2; } loop += 4; @@ -87,8 +87,8 @@ void icedraw(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output } // normal character - idf_buffer[i] = inputFileBuffer[loop]; - idf_buffer[i+1] = inputFileBuffer[loop + 1]; + idf_buffer[i] = inputFile->data[loop]; + idf_buffer[i+1] = inputFile->data[loop + 1]; i += 2; } loop += 2; @@ -107,10 +107,10 @@ void icedraw(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output // process IDF palette for (loop = 0; loop < 16; loop++) { - index = (loop * 3) + inputFileSize - 48; - colors[loop] = gdImageColorAllocate(canvas, (inputFileBuffer[index] << 2 | inputFileBuffer[index] >> 4), - (inputFileBuffer[index + 1] << 2 | inputFileBuffer[index + 1] >> 4), - (inputFileBuffer[index + 2] << 2 | inputFileBuffer[index + 2] >> 4)); + index = (loop * 3) + inputFile->size - 48; + colors[loop] = gdImageColorAllocate(canvas, (inputFile->data[index] << 2 | inputFile->data[index] >> 4), + (inputFile->data[index + 1] << 2 | inputFile->data[index + 1] >> 4), + (inputFile->data[index + 2] << 2 | inputFile->data[index + 2] >> 4)); } // render IDF @@ -137,7 +137,7 @@ void icedraw(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output } // create output file - output(canvas, outputFile, retinaout, retinaScaleFactor); + output(canvas, outputFile->fileName, outputFile->retina, outputFile->retinaScaleFactor); // free memory free(font_data_idf); diff --git a/src/loaders/pcboard.c b/src/loaders/pcboard.c @@ -20,23 +20,22 @@ struct pcbChar { int32_t current_character; }; -void pcboard(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, char *font, int32_t bits, int retinaScaleFactor) +void pcboard(struct input *inputFile, struct output *outputFile) { // some type declarations struct fontStruct fontData; - int32_t columns = 80; - int32_t loop, structIndex; + uint32_t loop, structIndex; // font selection - alSelectFont(&fontData, font); + alSelectFont(&fontData, outputFile->font); // libgd image pointers gdImagePtr canvas; // process PCBoard - int32_t character, current_character, next_character; - int32_t background = 0, foreground = 7; - int32_t column = 0, row = 0, columnMax = 0, rowMax = 0; + uint32_t character, current_character, next_character; + uint32_t background = 0, foreground = 7; + uint32_t column = 0, row = 0, columnMax = 0, rowMax = 0; // PCB buffer structure array definition struct pcbChar *pcboard_buffer, *temp; @@ -48,10 +47,10 @@ void pcboard(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output loop = 0; structIndex = 0; - while (loop < inputFileSize) + while (loop < inputFile->size) { - current_character = inputFileBuffer[loop]; - next_character = inputFileBuffer[loop+1]; + current_character = inputFile->data[loop]; + next_character = inputFile->data[loop+1]; if (column == 80) { @@ -89,12 +88,12 @@ void pcboard(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output if (current_character == 64 && next_character == 88) { // set graphics rendition - background = inputFileBuffer[loop+2]; - foreground = inputFileBuffer[loop+3]; + background = inputFile->data[loop+2]; + foreground = inputFile->data[loop+3]; loop += 3; } else if (current_character == 64 && next_character == 67 && - inputFileBuffer[loop+2] == 'L' && inputFileBuffer[loop+3] == 'S') + inputFile->data[loop+2] == 'L' && inputFile->data[loop+3] == 'S') { // erase display column = 0; @@ -105,18 +104,18 @@ void pcboard(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output loop += 4; } - else if (current_character == 64 && next_character == 80 && inputFileBuffer[loop+2] == 'O' - && inputFileBuffer[loop+3] == 'S' && inputFileBuffer[loop+4] == ':') + else if (current_character == 64 && next_character == 80 && inputFile->data[loop+2] == 'O' + && inputFile->data[loop+3] == 'S' && inputFile->data[loop+4] == ':') { // cursor position - if (inputFileBuffer[loop+6] == '@') + if (inputFile->data[loop+6] == '@') { - column = ((inputFileBuffer[loop+5])-48)-1; + column = ((inputFile->data[loop+5])-48)-1; loop += 5; } else { - column = (10 * ((inputFileBuffer[loop+5])-48) + (inputFileBuffer[loop+6])-48)-1; + column = (10 * ((inputFile->data[loop+5])-48) + (inputFile->data[loop+6])-48)-1; loop += 6; } } @@ -153,7 +152,7 @@ void pcboard(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output rowMax++; // allocate buffer image memory - canvas = gdImageCreate(columns * bits, (rowMax)*fontData.height); + canvas = gdImageCreate(inputFile->columns * outputFile->bits, (rowMax)*fontData.height); // allocate black color and create background canvas gdImageColorAllocate(canvas, 0, 0, 0); @@ -169,7 +168,7 @@ void pcboard(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output } // the last value of loop tells us how many items are stored in there - int32_t pcbBufferItems = structIndex; + uint32_t pcbBufferItems = structIndex; // render PCB for (loop = 0; loop < pcbBufferItems; loop++) @@ -181,12 +180,12 @@ void pcboard(unsigned char *inputFileBuffer, int32_t inputFileSize, char *output foreground = pcboard_buffer[loop].foreground; character = pcboard_buffer[loop].current_character; - drawchar(canvas, fontData.font_data, bits, fontData.height, + drawchar(canvas, fontData.font_data, outputFile->bits, fontData.height, column, row, colors[background], colors[foreground], character); } // create output image - output(canvas, outputFile, retinaout, retinaScaleFactor); + output(canvas, outputFile->fileName, outputFile->retina, outputFile->retinaScaleFactor); // free memory free(pcboard_buffer); diff --git a/src/loaders/tundra.c b/src/loaders/tundra.c @@ -11,23 +11,22 @@ #include "../ansilove.h" -void tundra(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, char *font, int32_t bits, int retinaScaleFactor) +void tundra(struct input *inputFile, struct output *outputFile) { // some type declarations struct fontStruct fontData; - int32_t columns = 80; char tundra_version; char tundra_header[8]; // font selection - alSelectFont(&fontData, font); + alSelectFont(&fontData, outputFile->font); // libgd image pointers gdImagePtr canvas; // extract tundra header - tundra_version = inputFileBuffer[0]; - memcpy(&tundra_header, inputFileBuffer+1, 8); + tundra_version = inputFile->data[0]; + memcpy(&tundra_header, inputFile->data+1, 8); // need to add check for "TUNDRA24" string in the header if (tundra_version != 24) @@ -36,10 +35,10 @@ void tundra(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputF } // read tundra file a first time to find the image size - int32_t character, background = 0, foreground = 0; - int32_t loop = 9, column = 0, row = 1; + uint32_t character, background = 0, foreground = 0; + uint32_t loop = 9, column = 0, row = 1; - while (loop < inputFileSize) + while (loop < inputFile->size) { if (column == 80) { @@ -47,38 +46,38 @@ void tundra(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputF row++; } - character = inputFileBuffer[loop]; + character = inputFile->data[loop]; if (character == 1) { row = - (inputFileBuffer[loop + 1] << 24) + (inputFileBuffer[loop + 2] << 16) + - (inputFileBuffer[loop + 3] << 8) + inputFileBuffer[loop+4]; + (inputFile->data[loop + 1] << 24) + (inputFile->data[loop + 2] << 16) + + (inputFile->data[loop + 3] << 8) + inputFile->data[loop+4]; column = - (inputFileBuffer[loop + 5] << 24) + (inputFileBuffer[loop + 6] << 16) + - (inputFileBuffer[loop + 7] << 8) + inputFileBuffer[loop+8]; + (inputFile->data[loop + 5] << 24) + (inputFile->data[loop + 6] << 16) + + (inputFile->data[loop + 7] << 8) + inputFile->data[loop+8]; loop += 8; } if (character == 2) { - character = inputFileBuffer[loop + 1]; + character = inputFile->data[loop + 1]; loop += 5; } if (character == 4) { - character = inputFileBuffer[loop + 1]; + character = inputFile->data[loop + 1]; loop += 5; } if (character == 6) { - character = inputFileBuffer[loop + 1]; + character = inputFile->data[loop + 1]; loop += 9; } @@ -92,7 +91,7 @@ void tundra(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputF } // allocate buffer image memory - canvas = gdImageCreateTrueColor(columns * bits, (row) * fontData.height); + canvas = gdImageCreateTrueColor(inputFile->columns * outputFile->bits, (row) * fontData.height); if (!canvas) { perror("Error, can't allocate buffer image memory"); @@ -105,7 +104,7 @@ void tundra(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputF loop = 9; - while (loop < inputFileSize) + while (loop < inputFile->size) { if (column == 80) { @@ -113,17 +112,17 @@ void tundra(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputF row++; } - character = inputFileBuffer[loop]; + character = inputFile->data[loop]; if (character == 1) { row = - (inputFileBuffer[loop + 1] << 24) + (inputFileBuffer[loop + 2] << 16) + - (inputFileBuffer[loop + 3] << 8) + inputFileBuffer[loop + 4]; + (inputFile->data[loop + 1] << 24) + (inputFile->data[loop + 2] << 16) + + (inputFile->data[loop + 3] << 8) + inputFile->data[loop + 4]; column = - (inputFileBuffer[loop + 5] << 24) + (inputFileBuffer[loop + 6] << 16) + - (inputFileBuffer[loop + 7] << 8) + inputFileBuffer[loop + 8]; + (inputFile->data[loop + 5] << 24) + (inputFile->data[loop + 6] << 16) + + (inputFile->data[loop + 7] << 8) + inputFile->data[loop + 8]; loop += 8; } @@ -131,20 +130,20 @@ void tundra(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputF if (character == 2) { foreground = - (inputFileBuffer[loop + 3] << 16) + (inputFileBuffer[loop + 4] << 8) + - inputFileBuffer[loop + 5]; + (inputFile->data[loop + 3] << 16) + (inputFile->data[loop + 4] << 8) + + inputFile->data[loop + 5]; - character = inputFileBuffer[loop+1]; + character = inputFile->data[loop+1]; loop += 5; } if (character == 4) { - background = (inputFileBuffer[loop + 3] << 16) + (inputFileBuffer[loop + 4] << 8) + - inputFileBuffer[loop+5]; + background = (inputFile->data[loop + 3] << 16) + (inputFile->data[loop + 4] << 8) + + inputFile->data[loop+5]; - character = inputFileBuffer[loop+1]; + character = inputFile->data[loop+1]; loop += 5; } @@ -152,21 +151,21 @@ void tundra(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputF if (character == 6) { foreground = - (inputFileBuffer[loop + 3] << 16) + (inputFileBuffer[loop + 4] << 8) + - inputFileBuffer[loop+5]; + (inputFile->data[loop + 3] << 16) + (inputFile->data[loop + 4] << 8) + + inputFile->data[loop+5]; background = - (inputFileBuffer[loop + 7] << 16) + (inputFileBuffer[loop + 8] << 8) + - inputFileBuffer[loop+9]; + (inputFile->data[loop + 7] << 16) + (inputFile->data[loop + 8] << 8) + + inputFile->data[loop+9]; - character = inputFileBuffer[loop+1]; + character = inputFile->data[loop+1]; loop += 9; } if (character != 1 && character != 2 && character != 4 && character != 6) { - drawchar(canvas, fontData.font_data, bits, fontData.height, + drawchar(canvas, fontData.font_data, outputFile->bits, fontData.height, column, row, background, foreground, character); column++; @@ -176,6 +175,6 @@ void tundra(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputF } // create output image - output(canvas, outputFile, retinaout, retinaScaleFactor); + output(canvas, outputFile->fileName, outputFile->retina, outputFile->retinaScaleFactor); } diff --git a/src/loaders/xbin.c b/src/loaders/xbin.c @@ -11,19 +11,19 @@ #include "../ansilove.h" -void xbin(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFile, char *retinaout, int retinaScaleFactor) +void xbin(struct input *inputFile, struct output *outputFile) { const unsigned char *font_data; unsigned char *font_data_xbin = NULL; - if (strncmp((char *)inputFileBuffer, "XBIN\x1a", 5) != 0) { + if (strncmp((char *)inputFile->data, "XBIN\x1a", 5) != 0) { fputs("\nNot an XBin.\n\n", stderr); exit(4); } - int32_t xbin_width = (inputFileBuffer[6] << 8) + inputFileBuffer[5]; - int32_t xbin_height = (inputFileBuffer[8] << 8) + inputFileBuffer[7]; - int32_t xbin_fontsize = inputFileBuffer[9]; - int32_t xbin_flags = inputFileBuffer[10]; + int32_t xbin_width = (inputFile->data[6] << 8) + inputFile->data[5]; + int32_t xbin_height = (inputFile->data[8] << 8) + inputFile->data[7]; + int32_t xbin_fontsize = inputFile->data[9]; + int32_t xbin_flags = inputFile->data[10]; gdImagePtr canvas; @@ -37,8 +37,8 @@ void xbin(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil // allocate black color gdImageColorAllocate(canvas, 0, 0, 0); - int32_t colors[16]; - int32_t offset = 11; + uint32_t colors[16]; + uint32_t offset = 11; // palette if ((xbin_flags & 1) == 1) { @@ -49,9 +49,9 @@ void xbin(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil { index = (loop * 3) + offset; - colors[loop] = gdImageColorAllocate(canvas, (inputFileBuffer[index] << 2 | inputFileBuffer[index] >> 4), - (inputFileBuffer[index + 1] << 2 | inputFileBuffer[index + 1] >> 4), - (inputFileBuffer[index + 2] << 2 | inputFileBuffer[index + 2] >> 4)); + colors[loop] = gdImageColorAllocate(canvas, (inputFile->data[index] << 2 | inputFile->data[index] >> 4), + (inputFile->data[index + 1] << 2 | inputFile->data[index + 1] >> 4), + (inputFile->data[index + 2] << 2 | inputFile->data[index + 2] >> 4)); } offset += 48; @@ -74,7 +74,7 @@ void xbin(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil perror("Memory error"); exit(5); } - memcpy(font_data_xbin, inputFileBuffer+offset, (xbin_fontsize * numchars)); + memcpy(font_data_xbin, inputFile->data+offset, (xbin_fontsize * numchars)); font_data = font_data_xbin; @@ -90,10 +90,10 @@ void xbin(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil // read compressed xbin if ((xbin_flags & 4) == 4) { - while (offset < inputFileSize && row != xbin_height) + while (offset < inputFile->size && row != xbin_height) { - int32_t ctype = inputFileBuffer[offset] & 0xC0; - int32_t counter = (inputFileBuffer[offset] & 0x3F) + 1; + int32_t ctype = inputFile->data[offset] & 0xC0; + int32_t counter = (inputFile->data[offset] & 0x3F) + 1; character = -1; attribute = -1; @@ -102,36 +102,36 @@ void xbin(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil while (counter--) { // none if (ctype == 0) { - character = inputFileBuffer[offset]; - attribute = inputFileBuffer[offset + 1]; + character = inputFile->data[offset]; + attribute = inputFile->data[offset + 1]; offset += 2; } // char else if (ctype == 0x40) { if (character == -1) { - character = inputFileBuffer[offset]; + character = inputFile->data[offset]; offset++; } - attribute = inputFileBuffer[offset]; + attribute = inputFile->data[offset]; offset++; } // attr else if (ctype == 0x80) { if (attribute == -1) { - attribute = inputFileBuffer[offset]; + attribute = inputFile->data[offset]; offset++; } - character = inputFileBuffer[offset]; + character = inputFile->data[offset]; offset++; } // both else { if (character == -1) { - character = inputFileBuffer[offset]; + character = inputFile->data[offset]; offset++; } if (attribute == -1) { - attribute = inputFileBuffer[offset]; + attribute = inputFile->data[offset]; offset++; } } @@ -153,7 +153,7 @@ void xbin(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil } // read uncompressed xbin else { - while (offset < inputFileSize && row != xbin_height) + while (offset < inputFile->size && row != xbin_height) { if (column == xbin_width) { @@ -161,8 +161,8 @@ void xbin(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil row++; } - character = inputFileBuffer[offset]; - attribute = inputFileBuffer[offset+1]; + character = inputFile->data[offset]; + attribute = inputFile->data[offset+1]; background = (attribute & 240) >> 4; foreground = attribute & 15; @@ -175,7 +175,7 @@ void xbin(unsigned char *inputFileBuffer, int32_t inputFileSize, char *outputFil } // create output file - output(canvas, outputFile, retinaout, retinaScaleFactor); + output(canvas, outputFile->fileName, outputFile->retina, outputFile->retinaScaleFactor); // nuke garbage free(font_data_xbin);