ansilove

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

commit 516d3b445b1cb0d8439e05b786558859cf08acb3
parent 23dc0e459442d89b46a30e096a0551b23709c20d
Author: Frederic Cambus <fred@statdns.com>
Date:   Mon, 11 Jul 2016 12:05:30 +0200

Removed a lot of whitespace

Diffstat:
Msrc/ansilove.c | 11+++++------
Msrc/ansilove.h | 4++--
Msrc/explode.c | 8++++----
Msrc/filesize.c | 12++++++------
Msrc/loaders/ansi.c | 205+++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/loaders/artworx.c | 60++++++++++++++++++++++++++++++------------------------------
Msrc/loaders/binary.c | 43+++++++++++++++++++++----------------------
Msrc/loaders/icedraw.c | 74+++++++++++++++++++++++++++++++++++++-------------------------------------
Msrc/loaders/pcboard.c | 79+++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/loaders/xbin.c | 52++++++++++++++++++++++++++--------------------------
Msrc/main.c | 8++++----
Msrc/sauce.c | 34+++++++++++++++++-----------------
Msrc/substr.c | 10+++++-----
13 files changed, 298 insertions(+), 302 deletions(-)

diff --git a/src/ansilove.c b/src/ansilove.c @@ -12,13 +12,13 @@ #include "ansilove.h" // shared method for drawing characters -void alDrawChar(gdImagePtr im, const unsigned char *font_data, int32_t int_bits, - int32_t font_size_y, int32_t position_x, int32_t position_y, +void alDrawChar(gdImagePtr im, const unsigned char *font_data, int32_t int_bits, + int32_t font_size_y, int32_t position_x, int32_t position_y, int32_t color_background, int32_t color_foreground, unsigned char character) { int32_t column, line; - gdImageFilledRectangle(im, position_x * int_bits, position_y*font_size_y, position_x * int_bits + + gdImageFilledRectangle(im, position_x * int_bits, position_y*font_size_y, position_x * int_bits + int_bits - 1, position_y * font_size_y + font_size_y - 1, color_background); for (line = 0; line < font_size_y; line++) { @@ -26,12 +26,11 @@ void alDrawChar(gdImagePtr im, const unsigned char *font_data, int32_t int_bits, if ((font_data[line+character*font_size_y] & (0x80 >> column)) != 0) { gdImageSetPixel(im, position_x * int_bits + column, position_y*font_size_y + line, color_foreground); - + if (int_bits==9 && column==7 && character > 191 && character < 224) { - gdImageSetPixel(im, position_x * int_bits + 8, position_y * font_size_y + line, color_foreground); + gdImageSetPixel(im, position_x * int_bits + 8, position_y * font_size_y + line, color_foreground); } - } } } diff --git a/src/ansilove.h b/src/ansilove.h @@ -32,8 +32,8 @@ #define ansilove_h // prototypes -void alDrawChar(gdImagePtr im, const unsigned char *font_data, int32_t int_bits, - int32_t font_size_y, int32_t position_x, int32_t position_y, +void alDrawChar(gdImagePtr im, const unsigned char *font_data, int32_t int_bits, + int32_t font_size_y, int32_t position_x, int32_t position_y, int32_t color_background, int32_t color_foreground, unsigned char character); #endif diff --git a/src/explode.c b/src/explode.c @@ -16,15 +16,15 @@ int32_t explode(char ***arr_ptr, char delimiter, char *str) char *src = str, *end, *dst; char **arr; int32_t size = 1, i; - + while ((end = strchr(src, delimiter)) != NULL) { ++size; src = end + 1; } - + arr = malloc(size * sizeof(char *) + (strlen(str) + 1) * sizeof(char)); - + src = str; dst = (char *) arr + size * sizeof(char *); for (i = 0; i < size; ++i) @@ -38,6 +38,6 @@ int32_t explode(char ***arr_ptr, char delimiter, char *str) src = end + 1; } *arr_ptr = arr; - + return size; } diff --git a/src/filesize.c b/src/filesize.c @@ -11,15 +11,15 @@ #include "filesize.h" -size_t filesize(char *filepath) +size_t filesize(char *filepath) { // pointer to file at path size_t size; FILE *file; - + // To properly determine the size, we open it in binary mode. file = fopen(filepath, "rb"); - + if(file != NULL) { // Error while seeking to end of file? @@ -28,14 +28,14 @@ size_t filesize(char *filepath) fclose(file); return -1; } - + size = ftell(file); // Close file and return the file size. rewind(file); fclose(file); return size; - } - + } + // In case we encounter an error. return -1; } diff --git a/src/loaders/ansi.c b/src/loaders/ansi.c @@ -17,7 +17,7 @@ void ansi(char *input, char *output, char *retinaout, char *font, int32_t int_bi struct fontStruct fontData; int32_t columns = 80; - + bool isDizFile = false; bool ced = false; bool transparent = false; @@ -27,8 +27,8 @@ void ansi(char *input, char *output, char *retinaout, char *font, int32_t int_bi // font selection alSelectFont(&fontData, font); - - // to deal with the bits flag, we declared handy bool types + + // to deal with the bits flag, we declared handy bool types if (!strcmp(mode, "ced")) { ced = true; } @@ -38,33 +38,33 @@ void ansi(char *input, char *output, char *retinaout, char *font, int32_t int_bi else if (!strcmp(mode, "workbench")) { workbench = true; } - + // load input file FILE *input_file = fopen(input, "r"); - if (input_file == NULL) { + 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); @@ -77,34 +77,34 @@ void ansi(char *input, char *output, char *retinaout, char *font, int32_t int_bi gdImagePtr im_ANSi; // ANSi processing loops - int32_t loop = 0, ansi_sequence_loop, seq_graphics_loop; - + int32_t loop = 0, ansi_sequence_loop, seq_graphics_loop; + // character definitions - int32_t current_character, next_character, character; + int32_t current_character, next_character, character; unsigned char ansi_sequence_character; - + // default color values - int32_t color_background = 0, color_foreground = 7; - + int32_t color_background = 0, color_foreground = 7; + // text attributes bool bold = false, underline = false, italics = false, blink = false; - + // positions int32_t position_x = 0, position_y = 0, position_x_max = 0, position_y_max = 0; int32_t saved_position_y = 0, saved_position_x = 0; - + // sequence parsing variables int32_t seqValue, seqArrayCount, seq_line, seq_column; char *seqGrab; char **seqArray; - + // ANSi buffer structure array definition int32_t structIndex = 0; struct ansiChar *ansi_buffer, *temp; - + // ANSi buffer dynamic memory allocation ansi_buffer = malloc(sizeof(struct ansiChar)); - + // ANSi interpreter while (loop < input_file_size) { @@ -116,7 +116,7 @@ void ansi(char *input, char *output, char *retinaout, char *font, int32_t int_bi position_y++; position_x=0; } - + // CR + LF if (current_character == 13 && next_character == 10) { position_y++; @@ -130,40 +130,40 @@ void ansi(char *input, char *output, char *retinaout, char *font, int32_t int_bi position_y++; position_x = 0; } - + // tab if (current_character == 9) { position_x += 8; } - + // sub if (current_character == 26 && SUBSTITUTE_BREAK) { break; } - + // ANSi sequence if (current_character == 27 && next_character == 91) - { + { for (ansi_sequence_loop = 0; ansi_sequence_loop < 12; ansi_sequence_loop++) { ansi_sequence_character = input_file_buffer[loop + 2 + ansi_sequence_loop]; - + // cursor position if (ansi_sequence_character == 'H' || ansi_sequence_character == 'f') { // create substring from the sequence's content seqGrab = substr((char *)input_file_buffer, loop+2, ansi_sequence_loop); - + // create sequence content array seqArrayCount = explode(&seqArray, ';', seqGrab); - + if (seqArrayCount > 1) { // convert grabbed sequence content to integers seq_line = strtonum(seqArray[0], 0, INT32_MAX, &errstr); seq_column = strtonum(seqArray[1], 0, INT32_MAX, &errstr); - + // finally set the positions position_y = seq_line-1; position_x = seq_column-1; @@ -176,41 +176,41 @@ void ansi(char *input, char *output, char *retinaout, char *font, int32_t int_bi loop+=ansi_sequence_loop+2; break; } - + // cursor up if (ansi_sequence_character=='A') { // create substring from the sequence's content seqGrab = substr((char *)input_file_buffer, loop+2, ansi_sequence_loop); - + // now get escape sequence's position value int32_t seq_line = strtonum(seqGrab, 0, INT32_MAX, &errstr); - + if (seq_line == 0) { seq_line = 1; } - + position_y = position_y - seq_line; - + loop+=ansi_sequence_loop+2; break; } - + // cursor down if (ansi_sequence_character=='B') { // create substring from the sequence's content seqGrab = substr((char *)input_file_buffer, loop+2, ansi_sequence_loop); - + // now get escape sequence's position value int32_t seq_line = strtonum(seqGrab, 0, INT32_MAX, &errstr); - + if (seq_line == 0) { seq_line = 1; } - + position_y = position_y + seq_line; - + loop+=ansi_sequence_loop+2; break; } @@ -218,88 +218,88 @@ void ansi(char *input, char *output, char *retinaout, char *font, int32_t int_bi // cursor forward if (ansi_sequence_character=='C') { - // create substring from the sequence's content + // create substring from the sequence's content seqGrab = substr((char *)input_file_buffer, loop+2, ansi_sequence_loop); // now get escape sequence's position value int32_t seq_column = strtonum(seqGrab, 0, INT32_MAX, &errstr); - + if (seq_column == 0) { seq_column = 1; } - + position_x = position_x + seq_column; - + if (position_x>80) { position_x=80; } - + loop+=ansi_sequence_loop+2; break; } - + // cursor backward if (ansi_sequence_character=='D') { - // create substring from the sequence's content + // create substring from the sequence's content seqGrab = substr((char *)input_file_buffer, loop+2, ansi_sequence_loop); // now get escape sequence's content length int32_t seq_column = strtonum(seqGrab, 0, INT32_MAX, &errstr); - + if (seq_column == 0) { seq_column = 1; } - + position_x = position_x - seq_column; - + if (position_x < 0) { position_x = 0; } - + loop+=ansi_sequence_loop+2; break; } - + // save cursor position if (ansi_sequence_character=='s') { saved_position_y = position_y; saved_position_x = position_x; - + loop+=ansi_sequence_loop+2; break; } - + // restore cursor position if (ansi_sequence_character=='u') { position_y = saved_position_y; position_x = saved_position_x; - + loop+=ansi_sequence_loop+2; break; } - + // erase display if (ansi_sequence_character=='J') { - // create substring from the sequence's content + // create substring from the sequence's content seqGrab = substr((char *)input_file_buffer, loop+2, ansi_sequence_loop); - + // convert grab to an integer int32_t eraseDisplayInt = strtonum(seqGrab, 0, INT32_MAX, &errstr); - + if (eraseDisplayInt == 2) - { + { position_x=0; position_y=0; - + position_x_max=0; position_y_max=0; - + // reset ansi buffer free(ansi_buffer); ansi_buffer = malloc(sizeof(struct ansiChar)); @@ -308,22 +308,22 @@ void ansi(char *input, char *output, char *retinaout, char *font, int32_t int_bi loop+=ansi_sequence_loop+2; break; } - + // set graphics mode if (ansi_sequence_character=='m') - { + { // create substring from the sequence's content seqGrab = substr((char *)input_file_buffer, loop+2, ansi_sequence_loop); - + // create sequence content array seqArrayCount = explode(&seqArray, ';', seqGrab); - + // a loophole in limbo for (seq_graphics_loop = 0; seq_graphics_loop < seqArrayCount; seq_graphics_loop++) { // convert split content value to integer seqValue = strtonum(seqArray[seq_graphics_loop], 0, INT32_MAX, &errstr); - + if (seqValue == 0) { color_background = 0; @@ -333,7 +333,7 @@ void ansi(char *input, char *output, char *retinaout, char *font, int32_t int_bi italics = false; blink = false; } - + if (seqValue == 1) { if (!workbench) @@ -342,17 +342,17 @@ void ansi(char *input, char *output, char *retinaout, char *font, int32_t int_bi } bold = true; } - + if (seqValue == 3) { italics = true; } - + if (seqValue == 4) { underline = true; } - + if (seqValue == 5) { if (!workbench) @@ -361,32 +361,32 @@ void ansi(char *input, char *output, char *retinaout, char *font, int32_t int_bi } blink = true; } - + if (seqValue > 29 && seqValue < 38) { color_foreground = seqValue - 30; - + if (bold) { color_foreground+=8; } } - + if (seqValue > 39 && seqValue < 48) { color_background = seqValue - 40; - + if (blink && icecolors) { color_background+=8; } } } - + loop+=ansi_sequence_loop+2; break; } - + // cursor (de)activation (Amiga ANSi) if (ansi_sequence_character == 'p') { @@ -409,19 +409,19 @@ void ansi(char *input, char *output, char *retinaout, char *font, int32_t int_bi { position_x_max=position_x; } - + if (position_y>position_y_max) { position_y_max=position_y; } - + // write current character in ansiChar structure if (!fontData.isAmigaFont || (current_character != 12 && current_character != 13)) { // reallocate structure array memory temp = realloc(ansi_buffer, (structIndex + 1) * sizeof(struct ansiChar)); ansi_buffer = temp; - + ansi_buffer[structIndex].color_background = color_background; ansi_buffer[structIndex].color_foreground = color_foreground; ansi_buffer[structIndex].current_character = current_character; @@ -430,37 +430,36 @@ void ansi(char *input, char *output, char *retinaout, char *font, int32_t int_bi ansi_buffer[structIndex].underline = underline; ansi_buffer[structIndex].position_x = position_x; ansi_buffer[structIndex].position_y = position_y; - + structIndex++; position_x++; } } loop++; } - + // allocate image buffer memory position_x_max++; position_y_max++; - + if (ced) { columns = 78; } - + if (isDizFile) { columns = fmin(position_x_max,80); } - + // create that damn thingy im_ANSi = gdImageCreate(columns * int_bits,(position_y_max)*fontData.font_size_y); - + if (!im_ANSi) { fputs ("\nCan't allocate ANSi buffer image memory.\n\n", stderr); exit (6); } - + int32_t colors[16]; - int32_t ced_background = 0, ced_foreground = 0; if (ced) @@ -470,7 +469,7 @@ void ansi(char *input, char *output, char *retinaout, char *font, int32_t int_bi gdImageFill(im_ANSi, 0, 0, ced_background); } else if (workbench) - { + { gdImageFill(im_ANSi, 0, 0, 0); colors[0] = gdImageColorAllocate(im_ANSi, 170, 170, 170); @@ -494,7 +493,7 @@ void ansi(char *input, char *output, char *retinaout, char *font, int32_t int_bi else { // Allocate standard ANSi color palette - + colors[0] = gdImageColorAllocate(im_ANSi, 0, 0, 0); colors[1] = gdImageColorAllocate(im_ANSi, 170, 0, 0); colors[2] = gdImageColorAllocate(im_ANSi, 0, 170, 0); @@ -515,7 +514,7 @@ void ansi(char *input, char *output, char *retinaout, char *font, int32_t int_bi // even more definitions, sigh int32_t ansiBufferItems = structIndex; - + // render ANSi for (loop = 0; loop < ansiBufferItems; loop++) { @@ -528,17 +527,17 @@ void ansi(char *input, char *output, char *retinaout, char *font, int32_t int_bi underline = ansi_buffer[loop].underline; position_x = ansi_buffer[loop].position_x; position_y = ansi_buffer[loop].position_y; - + if (ced) { - alDrawChar(im_ANSi, fontData.font_data, int_bits, fontData.font_size_y, - position_x, position_y, ced_background, ced_foreground, character); + alDrawChar(im_ANSi, fontData.font_data, int_bits, fontData.font_size_y, + position_x, position_y, ced_background, ced_foreground, character); } else { - alDrawChar(im_ANSi, fontData.font_data, int_bits, fontData.font_size_y, + alDrawChar(im_ANSi, fontData.font_data, int_bits, fontData.font_size_y, position_x, position_y, colors[color_background], colors[color_foreground], character); } } - + // transparent flag used? if (transparent) { @@ -549,29 +548,29 @@ void ansi(char *input, char *output, char *retinaout, char *font, int32_t int_bi FILE *file_Out = fopen(output, "wb"); gdImagePng(im_ANSi, 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_ANSi im_RetinaANSi = gdImageCreate(im_ANSi->sx * 2, im_ANSi->sy * 2); - + gdImageCopyResized(im_RetinaANSi, im_ANSi, 0, 0, 0, 0, im_RetinaANSi->sx, im_RetinaANSi->sy, im_ANSi->sx, im_ANSi->sy); - + // create retina output image FILE *file_RetinaOut = fopen(retinaout, "wb"); gdImagePng(im_RetinaANSi, file_RetinaOut); fclose(file_RetinaOut); - + gdImageDestroy(im_RetinaANSi); } - + // free memory free(ansi_buffer); - + gdImageDestroy(im_ANSi); } diff --git a/src/loaders/artworx.c b/src/loaders/artworx.c @@ -14,79 +14,79 @@ void artworx(char *input, char *output, char *retinaout, bool createRetinaRep) { const unsigned char *font_data; - unsigned char *font_data_adf; - + unsigned char *font_data_adf; + // load input file FILE *input_file = fopen(input, "r"); - if (input_file == NULL) { + 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_ADF; // create ADF instance im_ADF = gdImageCreate(640,(((input_file_size - 192 - 4096 -1) / 2) / 80) * 16); - + // error output if (!im_ADF) { fputs ("\nCan't allocate buffer image memory.\n\n", stderr); exit (7); } - + // 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 }; - + int32_t loop; int32_t index; - + // process ADF font font_data_adf = (unsigned char *) malloc(sizeof(unsigned char)*4096); if (font_data_adf == NULL) { fputs ("\nMemory error.\n\n", stderr); exit (7); } memcpy(font_data_adf,input_file_buffer+193,4096); - + font_data=font_data_adf; - // process ADF palette + // process ADF palette for (loop = 0; loop < 16; loop++) { index = (adf_colors[loop] * 3) + 1; - gdImageColorAllocate(im_ADF, (input_file_buffer[index] << 2 | input_file_buffer[index] >> 4), - (input_file_buffer[index + 1] << 2 | input_file_buffer[index + 1] >> 4), + gdImageColorAllocate(im_ADF, (input_file_buffer[index] << 2 | input_file_buffer[index] >> 4), + (input_file_buffer[index + 1] << 2 | input_file_buffer[index + 1] >> 4), (input_file_buffer[index + 2] << 2 | input_file_buffer[index + 2] >> 4)); } - + gdImageColorAllocate(im_ADF, 0, 0, 0); - + // process ADF - int32_t position_x = 0, position_y = 0; + int32_t position_x = 0, position_y = 0; int32_t character, attribute, color_foreground, color_background; loop = 192 + 4096 + 1; - + while(loop < input_file_size) { if (position_x == 80) @@ -94,41 +94,41 @@ void artworx(char *input, char *output, char *retinaout, bool createRetinaRep) 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; alDrawChar(im_ADF, font_data, 8, 16, position_x, position_y, color_background, color_foreground, character); - + position_x++; loop+=2; } - + // 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) { gdImagePtr im_RetinaANSi; - + // make the Retina image @2x as large as im_ADF im_RetinaANSi = gdImageCreate(im_ADF->sx * 2, im_ADF->sy * 2); - + gdImageCopyResized(im_RetinaANSi, im_ADF, 0, 0, 0, 0, im_RetinaANSi->sx, im_RetinaANSi->sy, im_ADF->sx, im_ADF->sy); - + // create retina output image FILE *file_RetinaOut = fopen(retinaout, "wb"); gdImagePng(im_RetinaANSi, file_RetinaOut); fclose(file_RetinaOut); - + gdImageDestroy(im_RetinaANSi); } diff --git a/src/loaders/binary.c b/src/loaders/binary.c @@ -18,53 +18,53 @@ void binary(char *input, char *output, char *retinaout, int32_t int_columns, cha // 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 + // 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); @@ -93,14 +93,13 @@ void binary(char *input, char *output, char *retinaout, int32_t int_columns, cha 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; @@ -108,33 +107,33 @@ void binary(char *input, char *output, char *retinaout, int32_t int_columns, cha 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); } diff --git a/src/loaders/icedraw.c b/src/loaders/icedraw.c @@ -15,27 +15,27 @@ void icedraw(char *input, char *output, char *retinaout, bool fileHasSAUCE, bool { 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) { @@ -47,7 +47,7 @@ void icedraw(char *input, char *output, char *retinaout, bool fileHasSAUCE, bool 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); @@ -60,37 +60,37 @@ void icedraw(char *input, char *output, char *retinaout, bool fileHasSAUCE, bool int32_t loop; int32_t index; int32_t colors[16]; - + // process IDF font font_data_idf = (unsigned char *) malloc(sizeof(unsigned char)*4096); if (font_data_idf == NULL) { fputs ("\nMemory error.\n\n", stderr); exit (7); } memcpy(font_data_idf,input_file_buffer+(input_file_size - 48 - 4096),4096); - + font_data=font_data_idf; // process IDF loop = 12; int32_t idf_sequence_length, idf_sequence_loop, i = 0; - + // dynamically allocated memory buffer for IDF data unsigned char *idf_buffer, *temp; idf_buffer = malloc(sizeof(unsigned char)); - + int16_t idf_data, idf_data_length; while (loop < input_file_size - 4096 - 48) { memcpy(&idf_data,input_file_buffer+loop,2); - + // RLE compressed data if (idf_data==1) { memcpy(&idf_data_length,input_file_buffer+loop+2,2); - + idf_sequence_length = idf_data_length & 255; - + for (idf_sequence_loop = 0; idf_sequence_loop < idf_sequence_length; idf_sequence_loop++) { // reallocate IDF buffer memory @@ -101,14 +101,14 @@ void icedraw(char *input, char *output, char *retinaout, bool fileHasSAUCE, bool else { fputs ("\nError allocating IDF buffer memory.\n\n", stderr); exit (7); } - + idf_buffer[i] = input_file_buffer[loop + 4]; idf_buffer[i+1] = input_file_buffer[loop + 5]; i+=2; } loop += 4; } - else { + else { // reallocate IDF buffer memory temp = realloc(idf_buffer, (i + 2) * sizeof(unsigned char)); if (idf_buffer != NULL) { @@ -117,7 +117,7 @@ void icedraw(char *input, char *output, char *retinaout, bool fileHasSAUCE, bool else { fputs ("\nError allocating IDF buffer memory.\n\n", stderr); exit (8); } - + // normal character idf_buffer[i] = input_file_buffer[loop]; idf_buffer[i+1] = input_file_buffer[loop + 1]; @@ -125,76 +125,76 @@ void icedraw(char *input, char *output, char *retinaout, bool fileHasSAUCE, bool } loop += 2; } - + // create IDF instance im_IDF = gdImageCreate((x2 + 1) * 8, i / 2 / 80 * 16); - + // error output if (!im_IDF) { fputs ("\nCan't allocate buffer image memory.\n\n", stderr); exit (9); } gdImageColorAllocate(im_IDF, 0, 0, 0); - + // process IDF palette for (loop = 0; loop < 16; loop++) { index = (loop * 3) + input_file_size - 48; - colors[loop] = gdImageColorAllocate(im_IDF, (input_file_buffer[index] << 2 | input_file_buffer[index] >> 4), - (input_file_buffer[index + 1] << 2 | input_file_buffer[index + 1] >> 4), + colors[loop] = gdImageColorAllocate(im_IDF, (input_file_buffer[index] << 2 | input_file_buffer[index] >> 4), + (input_file_buffer[index + 1] << 2 | input_file_buffer[index + 1] >> 4), (input_file_buffer[index + 2] << 2 | input_file_buffer[index + 2] >> 4)); } - // render IDF - int32_t position_x = 0, position_y = 0; + // render IDF + int32_t position_x = 0, position_y = 0; int32_t character, attribute, color_foreground, color_background; - - for (loop = 0; loop < i ; loop +=2) + + for (loop = 0; loop < i ; loop +=2) { if (position_x == x2 + 1) { position_x = 0; position_y++; } - + character = idf_buffer[loop]; attribute = idf_buffer[loop+1]; - + color_background = (attribute & 240) >> 4; color_foreground = attribute & 15; - + alDrawChar(im_IDF, font_data, 8, 16, position_x, position_y, colors[color_background], colors[color_foreground], character); - + position_x++; } - + // free dynamically allocated memory 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) { gdImagePtr im_RetinaANSi; - + // make the Retina image @2x as large as im_IDF im_RetinaANSi = gdImageCreate(im_IDF->sx * 2, im_IDF->sy * 2); - + gdImageCopyResized(im_RetinaANSi, im_IDF, 0, 0, 0, 0, im_RetinaANSi->sx, im_RetinaANSi->sy, im_IDF->sx, im_IDF->sy); - + // create retina output image FILE *file_RetinaOut = fopen(retinaout, "wb"); gdImagePng(im_RetinaANSi, file_RetinaOut); fclose(file_RetinaOut); - + gdImageDestroy(im_RetinaANSi); } - + // nuke garbage gdImageDestroy(im_IDF); } diff --git a/src/loaders/pcboard.c b/src/loaders/pcboard.c @@ -23,10 +23,10 @@ void pcboard(char *input, char *output, char *retinaout, char *font, int32_t int // load input file FILE *input_file = fopen(input, "r"); - if (input_file == NULL) { + 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; @@ -34,58 +34,58 @@ void pcboard(char *input, char *output, char *retinaout, char *font, int32_t int // 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_PCB; - + // process PCBoard int32_t character, current_character, next_character; int32_t color_background = 0, color_foreground = 7; int32_t position_x = 0, position_y = 0, position_x_max = 0, position_y_max = 0; - + // PCB buffer structure array definition struct pcbChar *pcboard_buffer, *temp; - + // PCB buffer dynamic memory allocation pcboard_buffer = malloc(sizeof(struct pcbChar)); - + // reset loop loop = 0; structIndex = 0; - + while (loop < input_file_size) { current_character = input_file_buffer[loop]; next_character = input_file_buffer[loop+1]; - + if (position_x == 80) { position_y++; position_x = 0; } - + // CR + LF if (current_character == 13 && next_character == 10) { position_y++; position_x = 0; loop++; } - + // LF if (current_character == 10) { @@ -98,20 +98,19 @@ void pcboard(char *input, char *output, char *retinaout, char *font, int32_t int { position_x+=8; } - + // Sub if (current_character == 26) { break; } - + // PCB sequence if (current_character == 64 && next_character == 88) { // set graphics rendition color_background = input_file_buffer[loop+2]; color_foreground = input_file_buffer[loop+3]; - loop+=3; } else if (current_character == 64 && next_character == 67 && @@ -120,13 +119,13 @@ void pcboard(char *input, char *output, char *retinaout, char *font, int32_t int // erase display position_x = 0; position_y = 0; - + position_x_max = 0; position_y_max = 0; - + loop+=4; } - else if (current_character == 64 && next_character == 80 && input_file_buffer[loop+2] == 'O' + else if (current_character == 64 && next_character == 80 && input_file_buffer[loop+2] == 'O' && input_file_buffer[loop+3] == 'S' && input_file_buffer[loop+4]== ':') { // cursor position @@ -140,7 +139,7 @@ void pcboard(char *input, char *output, char *retinaout, char *font, int32_t int position_x = (10 * ((input_file_buffer[loop+5])-48) + (input_file_buffer[loop+6])-48)-1; loop+=6; } - } + } else if (current_character != 10 && current_character != 13 && current_character != 9) { // record number of columns and lines used @@ -148,23 +147,23 @@ void pcboard(char *input, char *output, char *retinaout, char *font, int32_t int { position_x_max = position_x; } - + if (position_y > position_y_max) { position_y_max = position_y; } - + // reallocate structure array memory temp = realloc(pcboard_buffer, (structIndex + 1) * sizeof(struct pcbChar)); pcboard_buffer = temp; - + // write current character in pcbChar structure pcboard_buffer[structIndex].position_x = position_x; pcboard_buffer[structIndex].position_y = position_y; pcboard_buffer[structIndex].color_background = color_background; pcboard_buffer[structIndex].color_foreground = color_foreground; pcboard_buffer[structIndex].current_character = current_character; - + position_x++; structIndex++; } @@ -172,17 +171,17 @@ void pcboard(char *input, char *output, char *retinaout, char *font, int32_t int } position_x_max++; position_y_max++; - + // allocate buffer image memory im_PCB = gdImageCreate(columns * int_bits, (position_y_max)*fontData.font_size_y); - + // allocate black color and create background canvas gdImageColorAllocate(im_PCB, 0, 0, 0); gdImageFill(im_PCB, 0, 0, 0); - - // allocate color palette + + // allocate color palette int32_t colors[71]; - + colors[48] = gdImageColorAllocate(im_PCB, 0, 0, 0); colors[49] = gdImageColorAllocate(im_PCB, 0, 0, 170); colors[50] = gdImageColorAllocate(im_PCB, 0, 170, 0); @@ -199,10 +198,10 @@ void pcboard(char *input, char *output, char *retinaout, char *font, int32_t int colors[68] = gdImageColorAllocate(im_PCB, 255, 85, 255); colors[69] = gdImageColorAllocate(im_PCB, 255, 255, 85); colors[70] = gdImageColorAllocate(im_PCB, 255, 255, 255); - + // the last value of loop tells us how many items are stored in there int32_t pcbBufferItems = structIndex; - + // render PCB for (loop = 0; loop < pcbBufferItems; loop++) { @@ -212,36 +211,36 @@ void pcboard(char *input, char *output, char *retinaout, char *font, int32_t int color_background = pcboard_buffer[loop].color_background; color_foreground = pcboard_buffer[loop].color_foreground; character = pcboard_buffer[loop].current_character; - - alDrawChar(im_PCB, fontData.font_data, int_bits, fontData.font_size_y, + + alDrawChar(im_PCB, fontData.font_data, int_bits, fontData.font_size_y, position_x, position_y, colors[color_background], colors[color_foreground], character); } - + // 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) { gdImagePtr im_RetinaANSi; - + // make the Retina image @2x as large as im_PCB im_RetinaANSi = gdImageCreate(im_PCB->sx * 2, im_PCB->sy * 2); - + gdImageCopyResized(im_RetinaANSi, im_PCB, 0, 0, 0, 0, im_RetinaANSi->sx, im_RetinaANSi->sy, im_PCB->sx, im_PCB->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_PCB); } diff --git a/src/loaders/xbin.c b/src/loaders/xbin.c @@ -15,33 +15,33 @@ void xbin(char *input, char *output, char *retinaout, bool createRetinaRep) { const unsigned char *font_data; unsigned char *font_data_xbin; - + // load input file FILE *input_file = fopen(input, "r"); - if (input_file == NULL) { + 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); @@ -55,16 +55,16 @@ void xbin(char *input, char *output, char *retinaout, bool createRetinaRep) int32_t xbin_flags = input_file_buffer[ 10 ]; gdImagePtr im_XBIN; - + im_XBIN = gdImageCreate(8 * xbin_width, xbin_fontsize * xbin_height); - + if (!im_XBIN) { fputs ("\nError, can't allocate buffer image memory.\n\n", stderr); exit (6); } - + // allocate black color gdImageColorAllocate(im_XBIN, 0, 0, 0); - + int32_t colors[16]; int32_t offset = 11; @@ -72,13 +72,13 @@ void xbin(char *input, char *output, char *retinaout, bool createRetinaRep) if( (xbin_flags & 1) == 1 ) { int32_t loop; int32_t index; - + for (loop = 0; loop < 16; loop++) { index = (loop * 3) + offset; - - colors[loop] = gdImageColorAllocate(im_XBIN, (input_file_buffer[index] << 2 | input_file_buffer[index] >> 4), - (input_file_buffer[index + 1] << 2 | input_file_buffer[index + 1] >> 4), + + colors[loop] = gdImageColorAllocate(im_XBIN, (input_file_buffer[index] << 2 | input_file_buffer[index] >> 4), + (input_file_buffer[index + 1] << 2 | input_file_buffer[index + 1] >> 4), (input_file_buffer[index + 2] << 2 | input_file_buffer[index + 2] >> 4)); } @@ -106,7 +106,7 @@ void xbin(char *input, char *output, char *retinaout, bool createRetinaRep) // font if( (xbin_flags & 2) == 2 ) { int32_t numchars = ( xbin_flags & 0x10 ? 512 : 256 ); - + // allocate memory to contain the XBin font font_data_xbin = (unsigned char *) malloc(sizeof(unsigned char)*(xbin_fontsize * numchars)); if (font_data_xbin == NULL) { @@ -176,7 +176,7 @@ void xbin(char *input, char *output, char *retinaout, bool createRetinaRep) color_background = (attribute & 240) >> 4; color_foreground = attribute & 15; - + alDrawChar(im_XBIN, font_data, 8, 16, position_x, position_y, colors[color_background], colors[color_foreground], character); position_x++; @@ -198,15 +198,15 @@ void xbin(char *input, char *output, char *retinaout, bool createRetinaRep) position_x = 0; position_y++; } - + character = input_file_buffer[offset]; attribute = input_file_buffer[offset+1]; - + color_background = (attribute & 240) >> 4; color_foreground = attribute & 15; alDrawChar(im_XBIN, font_data, 8, xbin_fontsize, position_x, position_y, colors[color_background], colors[color_foreground], character); - + position_x++; offset+=2; } @@ -216,27 +216,27 @@ void xbin(char *input, char *output, char *retinaout, bool createRetinaRep) FILE *file_Out = fopen(output, "wb"); gdImagePng(im_XBIN, 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_XBIN im_RetinaANSi = gdImageCreate(im_XBIN->sx * 2, im_XBIN->sy * 2); - + gdImageCopyResized(im_RetinaANSi, im_XBIN, 0, 0, 0, 0, im_RetinaANSi->sx, im_RetinaANSi->sy, im_XBIN->sx, im_XBIN->sy); - + // create retina output image FILE *file_RetinaOut = fopen(retinaout, "wb"); gdImagePng(im_RetinaANSi, file_RetinaOut); fclose(file_RetinaOut); - + gdImageDestroy(im_RetinaANSi); } - + // nuke garbage gdImageDestroy(im_XBIN); } diff --git a/src/main.c b/src/main.c @@ -119,10 +119,10 @@ int main(int argc, char *argv[]) { // SAUCE record related bool types bool justDisplaySAUCE = false; bool fileHasSAUCE = false; - + // retina output bool type bool createRetinaRep = false; - + // iCE colors bool type bool icecolors = false; @@ -201,7 +201,7 @@ int main(int argc, char *argv[]) { // let's check the file for a valid SAUCE record sauce *record = sauceReadFileName(input); - + // record == NULL also means there is no file, we can stop here if (record == NULL) { printf("\nFile %s not found.\n\n", input); @@ -264,7 +264,7 @@ int main(int argc, char *argv[]) { } else { int_columns = 160; } - + // default to 80x25 font if font option is not specified if (!font) { font = "80x25"; diff --git a/src/sauce.c b/src/sauce.c @@ -18,7 +18,7 @@ sauce *sauceReadFileName(char *fileName) if (file == NULL) { return NULL; } - + sauce *record = sauceReadFile(file); fclose(file); return record; @@ -29,7 +29,7 @@ sauce *sauceReadFile(FILE *file) { sauce *record; record = malloc(sizeof *record); - + if (record != NULL) { readRecord(file, record); } @@ -42,26 +42,26 @@ void readRecord(FILE *file, sauce *record) free(record); return; } - + size_t read_status = fread(record->ID, sizeof(record->ID) - 1, 1, file); record->ID[sizeof(record->ID) - 1] = '\0'; - + if (read_status != 1 || strcmp(record->ID, SAUCE_ID) != 0) { free(record); return; } fread(record->version, sizeof(record->version) - 1, 1, file); record->version[sizeof(record->version) - 1] = '\0'; - fread(record->title, sizeof(record->title) - 1, 1, file); + fread(record->title, sizeof(record->title) - 1, 1, file); record->title[sizeof(record->title) - 1] = '\0'; fread(record->author, sizeof(record->author) - 1, 1, file); record->author[sizeof(record->author) - 1] = '\0'; - fread(record->group, sizeof(record->group) - 1, 1, file); + fread(record->group, sizeof(record->group) - 1, 1, file); record->group[sizeof(record->group) - 1] = '\0'; fread(record->date, sizeof(record->date) - 1, 1, file); record->date[sizeof(record->date) - 1] = '\0'; - fread(&(record->fileSize), sizeof(record->fileSize), 1, file); - fread(&(record->dataType), sizeof(record->dataType), 1, file); + fread(&(record->fileSize), sizeof(record->fileSize), 1, file); + fread(&(record->dataType), sizeof(record->dataType), 1, file); fread(&(record->fileType), sizeof(record->fileType), 1, file); fread(&(record->tinfo1), sizeof(record->tinfo1), 1, file); fread(&(record->tinfo2), sizeof(record->tinfo2), 1, file); @@ -71,15 +71,15 @@ void readRecord(FILE *file, sauce *record) fread(&(record->flags), sizeof(record->flags), 1, file); fread(record->filler, sizeof(record->filler) - 1, 1, file); record->filler[sizeof(record->filler) - 1] = '\0'; - + if (ferror(file) != EXIT_SUCCESS) { free(record); return; } - + if (record->comments > 0) { record->comment_lines = malloc(record->comments *sizeof(*record->comment_lines)); - + if (record->comment_lines != NULL) { readComments(file, record->comment_lines, record->comments); } @@ -93,23 +93,23 @@ void readRecord(FILE *file, sauce *record) void readComments(FILE *file, char **comment_lines, int32_t comments) { int32_t i; - + if (fseek(file, 0 - (RECORD_SIZE + 5 + COMMENT_SIZE *comments), SEEK_END) == EXIT_SUCCESS) { char ID[6]; fread(ID, sizeof(ID) - 1, 1, file); ID[sizeof(ID) - 1] = '\0'; - + if (strcmp(ID, COMMENT_ID) != 0) { free(comment_lines); return; } - + for (i = 0; i < comments; i++) { char buf[COMMENT_SIZE + 1] = ""; - + fread(buf, COMMENT_SIZE, 1, file); buf[COMMENT_SIZE] = '\0'; - + if (ferror(file) == EXIT_SUCCESS) { comment_lines[i] = strdup(buf); if (comment_lines[i] == NULL) { @@ -123,7 +123,7 @@ void readComments(FILE *file, char **comment_lines, int32_t comments) } } return; - } + } free(comment_lines); return; } diff --git a/src/substr.c b/src/substr.c @@ -12,9 +12,9 @@ #include "substr.h" char *substr(char *str, size_t begin, size_t len) -{ - if (str == 0 || strlen(str) == 0) - return 0; - +{ + if (str == 0 || strlen(str) == 0) + return 0; + return strndup(str + begin, len); -} +}