libansilove

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

commit 1d1339e448a178b9ee62549b8b9ab5443d5e2363
parent 0def3c497ceffb80fb476806e2f89334f53701c8
Author: Frederic Cambus <fred@statdns.com>
Date:   Mon,  2 Jul 2018 18:36:28 +0200

Formatting fixes, try to use 1TBS everywhere

Diffstat:
Msrc/drawchar.c | 3+--
Msrc/explode.c | 9+++------
Msrc/loaders/ansi.c | 95++++++++++++++++++++++++++-----------------------------------------------------
Msrc/loaders/artworx.c | 9+++------
Msrc/loaders/binary.c | 6++----
Msrc/loaders/icedraw.c | 24++++++++----------------
Msrc/loaders/pcboard.c | 28+++++++++-------------------
Msrc/loaders/tundra.c | 39+++++++++++++--------------------------
Msrc/loaders/xbin.c | 26+++++++++-----------------
9 files changed, 79 insertions(+), 160 deletions(-)

diff --git a/src/drawchar.c b/src/drawchar.c @@ -14,8 +14,7 @@ // shared method for drawing characters void drawchar(gdImagePtr im, const unsigned char *font_data, int32_t bits, int32_t height, int32_t column, int32_t row, - int32_t background, int32_t foreground, unsigned char character) -{ + int32_t background, int32_t foreground, unsigned char character) { int32_t x, y; gdImageFilledRectangle(im, column * bits, row*height, column * bits + diff --git a/src/explode.c b/src/explode.c @@ -11,14 +11,12 @@ #include "explode.h" -int32_t explode(char ***arr_ptr, char delimiter, char *str) -{ +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) - { + while ((end = strchr(src, delimiter)) != NULL) { ++size; src = end + 1; } @@ -27,8 +25,7 @@ int32_t explode(char ***arr_ptr, char delimiter, char *str) src = str; dst = (char *)arr + size * sizeof (char *); - for (i = 0; i < size; ++i) - { + for (i = 0; i < size; ++i) { if ((end = strchr(src, delimiter)) == NULL) end = src + strlen(src); arr[i] = dst; diff --git a/src/loaders/ansi.c b/src/loaders/ansi.c @@ -45,11 +45,9 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) // to deal with the bits flag, we declared handy bool types if (!strcmp(outputFile->mode, "ced")) { ced = true; - } - else if (!strcmp(outputFile->mode, "transparent")) { + } else if (!strcmp(outputFile->mode, "transparent")) { transparent = true; - } - else if (!strcmp(outputFile->mode, "workbench")) { + } else if (!strcmp(outputFile->mode, "workbench")) { workbench = true; } @@ -91,13 +89,11 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) ansi_buffer = malloc(sizeof (struct ansiChar)); // ANSi interpreter - while (loop < inputFile->length) - { + while (loop < inputFile->length) { current_character = inputFile->buffer[loop]; next_character = inputFile->buffer[loop + 1]; - if (column == 80) - { + if (column == 80) { row++; column = 0; } @@ -110,8 +106,7 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) } // LF - if (current_character == 10) - { + if (current_character == 10) { row++; column = 0; } @@ -125,15 +120,12 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) break; // ANSi sequence - if (current_character == 27 && next_character == 91) - { - for (ansi_sequence_loop = 0; ansi_sequence_loop < 14; ansi_sequence_loop++) - { + if (current_character == 27 && next_character == 91) { + for (ansi_sequence_loop = 0; ansi_sequence_loop < 14; ansi_sequence_loop++) { ansi_sequence_character = inputFile->buffer[loop + 2 + ansi_sequence_loop]; // cursor position - if (ansi_sequence_character == 'H' || ansi_sequence_character == 'f') - { + if (ansi_sequence_character == 'H' || ansi_sequence_character == 'f') { // create substring from the sequence's content seqGrab = strndup((char *)inputFile->buffer + loop + 2, ansi_sequence_loop); @@ -149,8 +141,7 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) // finally set the positions row = seq_line-1; column = seq_column-1; - } - else { + } else { // no coordinates specified? we move to the home position row = 0; column = 0; @@ -160,8 +151,7 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) } // cursor up - if (ansi_sequence_character == 'A') - { + if (ansi_sequence_character == 'A') { // create substring from the sequence's content seqGrab = strndup((char *)inputFile->buffer + loop + 2, ansi_sequence_loop); @@ -176,8 +166,7 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) } // cursor down - if (ansi_sequence_character == 'B') - { + if (ansi_sequence_character == 'B') { // create substring from the sequence's content seqGrab = strndup((char *)inputFile->buffer + loop + 2, ansi_sequence_loop); @@ -192,8 +181,7 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) } // cursor forward - if (ansi_sequence_character == 'C') - { + if (ansi_sequence_character == 'C') { // create substring from the sequence's content seqGrab = strndup((char *)inputFile->buffer + loop + 2, ansi_sequence_loop); @@ -211,8 +199,7 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) } // cursor backward - if (ansi_sequence_character == 'D') - { + if (ansi_sequence_character == 'D') { // create substring from the sequence's content seqGrab = strndup((char *)inputFile->buffer + loop + 2, ansi_sequence_loop); @@ -230,8 +217,7 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) } // save cursor position - if (ansi_sequence_character == 's') - { + if (ansi_sequence_character == 's') { saved_row = row; saved_column = column; @@ -240,8 +226,7 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) } // restore cursor position - if (ansi_sequence_character == 'u') - { + if (ansi_sequence_character == 'u') { row = saved_row; column = saved_column; @@ -250,8 +235,7 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) } // erase display - if (ansi_sequence_character == 'J') - { + if (ansi_sequence_character == 'J') { // create substring from the sequence's content seqGrab = strndup((char *)inputFile->buffer + loop + 2, ansi_sequence_loop); @@ -259,8 +243,7 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) int32_t eraseDisplayInt = strtonum(seqGrab, 0, INT32_MAX, &errstr); free(seqGrab); - if (eraseDisplayInt == 2) - { + if (eraseDisplayInt == 2) { column = 0; row = 0; @@ -277,8 +260,7 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) } // set graphics mode - if (ansi_sequence_character == 'm') - { + if (ansi_sequence_character == 'm') { // create substring from the sequence's content seqGrab = strndup((char *)inputFile->buffer + loop + 2, ansi_sequence_loop); @@ -287,13 +269,11 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) free(seqGrab); // a loophole in limbo - for (seq_graphics_loop = 0; seq_graphics_loop < seqArrayCount; seq_graphics_loop++) - { + 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) - { + if (seqValue == 0) { background = 0; foreground = 7; bold = false; @@ -302,10 +282,8 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) blink = false; } - if (seqValue == 1) - { - if (!workbench) - { + if (seqValue == 1) { + if (!workbench) { foreground += 8; } bold = true; @@ -347,29 +325,25 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) } // cursor (de)activation (Amiga ANSi) - if (ansi_sequence_character == 'p') - { + if (ansi_sequence_character == 'p') { loop += ansi_sequence_loop+2; break; } // skipping set mode and reset mode sequences - if (ansi_sequence_character == 'h' || ansi_sequence_character == 'l') - { + if (ansi_sequence_character == 'h' || ansi_sequence_character == 'l') { loop += ansi_sequence_loop+2; break; } // skipping erase in line (EL) sequences - if (ansi_sequence_character == 'K') - { + if (ansi_sequence_character == 'K') { loop += ansi_sequence_loop+2; break; } // skipping PabloDraw 24-bit ANSI sequences - if (ansi_sequence_character == 't') - { + if (ansi_sequence_character == 't') { loop += ansi_sequence_loop+2; break; } @@ -385,8 +359,7 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) rowMax = row; // write current character in ansiChar structure - if (!fontData.isAmigaFont || (current_character != 12 && current_character != 13)) - { + 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; @@ -429,14 +402,11 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) int32_t ced_background = 0, ced_foreground = 0; - if (ced) - { + if (ced) { ced_background = gdImageColorAllocate(canvas, 170, 170, 170); ced_foreground = gdImageColorAllocate(canvas, 0, 0, 0); gdImageFill(canvas, 0, 0, ced_background); - } - else if (workbench) - { + } else if (workbench) { gdImageFill(canvas, 0, 0, 0); for (int i = 0; i < 16; i++) { @@ -444,9 +414,7 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) workbench_palette[i*3+1], workbench_palette[i*3+2]); } - } - else - { + } else { // Allocate standard ANSi color palette for (int i = 0; i < 16; i++) { @@ -460,8 +428,7 @@ int ansilove_ansi(struct input *inputFile, struct output *outputFile) uint32_t ansiBufferItems = structIndex; // render ANSi - for (loop = 0; loop < ansiBufferItems; loop++) - { + for (loop = 0; loop < ansiBufferItems; loop++) { // grab ANSi char from our structure array background = ansi_buffer[loop].background; foreground = ansi_buffer[loop].foreground; diff --git a/src/loaders/artworx.c b/src/loaders/artworx.c @@ -32,8 +32,7 @@ int ansilove_artworx(struct input *inputFile, struct output *outputFile) uint32_t index; // process ADF palette - for (loop = 0; loop < 16; loop++) - { + for (loop = 0; loop < 16; loop++) { index = (adf_colors[loop] * 3) + 1; gdImageColorAllocate(canvas, (inputFile->buffer[index] << 2 | inputFile->buffer[index] >> 4), (inputFile->buffer[index + 1] << 2 | inputFile->buffer[index + 1] >> 4), @@ -47,10 +46,8 @@ int ansilove_artworx(struct input *inputFile, struct output *outputFile) uint32_t character, attribute, foreground, background; loop = 192 + 4096 + 1; - while (loop < inputFile->length) - { - if (column == 80) - { + while (loop < inputFile->length) { + if (column == 80) { column = 0; row++; } diff --git a/src/loaders/binary.c b/src/loaders/binary.c @@ -53,10 +53,8 @@ int ansilove_binary(struct input *inputFile, struct output *outputFile) uint32_t character, attribute, background, foreground; uint32_t loop = 0, column = 0, row = 0; - while (loop < inputFile->length) - { - if (column == outputFile->columns) - { + while (loop < inputFile->length) { + if (column == outputFile->columns) { column = 0; row++; } diff --git a/src/loaders/icedraw.c b/src/loaders/icedraw.c @@ -32,13 +32,11 @@ int ansilove_icedraw(struct input *inputFile, struct output *outputFile) int16_t idf_data, idf_data_length; - while (loop < inputFile->length - 4096 - 48) - { + while (loop < inputFile->length - 4096 - 48) { memcpy(&idf_data, inputFile->buffer+loop, 2); // RLE compressed data - if (idf_data == 1) - { + if (idf_data == 1) { memcpy(&idf_data_length, inputFile->buffer+loop+2, 2); idf_sequence_length = idf_data_length & 255; @@ -49,8 +47,7 @@ int ansilove_icedraw(struct input *inputFile, struct output *outputFile) temp = realloc(idf_buffer, i + 2); if (idf_buffer != NULL) { idf_buffer = temp; - } - else { + } else { perror("Error allocating IDF buffer memory"); return -1; } @@ -60,14 +57,12 @@ int ansilove_icedraw(struct input *inputFile, struct output *outputFile) i += 2; } loop += 4; - } - else { + } else { // reallocate IDF buffer memory temp = realloc(idf_buffer, i + 2); if (idf_buffer != NULL) { idf_buffer = temp; - } - else { + } else { perror("Error allocating IDF buffer memory"); return -1; } @@ -91,8 +86,7 @@ int ansilove_icedraw(struct input *inputFile, struct output *outputFile) gdImageColorAllocate(canvas, 0, 0, 0); // process IDF palette - for (loop = 0; loop < 16; loop++) - { + for (loop = 0; loop < 16; loop++) { index = (loop * 3) + inputFile->length - 48; colors[loop] = gdImageColorAllocate(canvas, (inputFile->buffer[index] << 2 | inputFile->buffer[index] >> 4), (inputFile->buffer[index + 1] << 2 | inputFile->buffer[index + 1] >> 4), @@ -103,10 +97,8 @@ int ansilove_icedraw(struct input *inputFile, struct output *outputFile) int32_t column = 0, row = 0; int32_t character, attribute, foreground, background; - for (loop = 0; loop < i; loop += 2) - { - if (column == x2 + 1) - { + for (loop = 0; loop < i; loop += 2) { + if (column == x2 + 1) { column = 0; row++; } diff --git a/src/loaders/pcboard.c b/src/loaders/pcboard.c @@ -47,13 +47,11 @@ int ansilove_pcboard(struct input *inputFile, struct output *outputFile) loop = 0; structIndex = 0; - while (loop < inputFile->length) - { + while (loop < inputFile->length) { current_character = inputFile->buffer[loop]; next_character = inputFile->buffer[loop+1]; - if (column == 80) - { + if (column == 80) { row++; column = 0; } @@ -66,8 +64,7 @@ int ansilove_pcboard(struct input *inputFile, struct output *outputFile) } // LF - if (current_character == 10) - { + if (current_character == 10) { row++; column = 0; } @@ -81,16 +78,14 @@ int ansilove_pcboard(struct input *inputFile, struct output *outputFile) break; // PCB sequence - if (current_character == 64 && next_character == 88) - { + if (current_character == 64 && next_character == 88) { // set graphics rendition background = inputFile->buffer[loop+2]; foreground = inputFile->buffer[loop+3]; loop += 3; } else if (current_character == 64 && next_character == 67 && - inputFile->buffer[loop+2] == 'L' && inputFile->buffer[loop+3] == 'S') - { + inputFile->buffer[loop+2] == 'L' && inputFile->buffer[loop+3] == 'S') { // erase display column = 0; row = 0; @@ -99,10 +94,8 @@ int ansilove_pcboard(struct input *inputFile, struct output *outputFile) rowMax = 0; loop += 4; - } - else if (current_character == 64 && next_character == 80 && inputFile->buffer[loop+2] == 'O' - && inputFile->buffer[loop+3] == 'S' && inputFile->buffer[loop+4] == ':') - { + } else if (current_character == 64 && next_character == 80 && inputFile->buffer[loop+2] == 'O' + && inputFile->buffer[loop+3] == 'S' && inputFile->buffer[loop+4] == ':') { // cursor position if (inputFile->buffer[loop+6] == '@') { @@ -114,9 +107,7 @@ int ansilove_pcboard(struct input *inputFile, struct output *outputFile) column = (10 * ((inputFile->buffer[loop+5])-48) + (inputFile->buffer[loop+6])-48)-1; loop += 6; } - } - else if (current_character != 10 && current_character != 13 && current_character != 9) - { + } else if (current_character != 10 && current_character != 13 && current_character != 9) { // record number of columns and lines used if (column > columnMax) columnMax = column; @@ -163,8 +154,7 @@ int ansilove_pcboard(struct input *inputFile, struct output *outputFile) uint32_t pcbBufferItems = structIndex; // render PCB - for (loop = 0; loop < pcbBufferItems; loop++) - { + for (loop = 0; loop < pcbBufferItems; loop++) { // grab our chars out of the structure column = pcboard_buffer[loop].column; row = pcboard_buffer[loop].row; diff --git a/src/loaders/tundra.c b/src/loaders/tundra.c @@ -36,18 +36,15 @@ int ansilove_tundra(struct input *inputFile, struct output *outputFile) uint32_t character, background = 0, foreground = 0; uint32_t loop = 9, column = 0, row = 1; - while (loop < inputFile->length) - { - if (column == 80) - { + while (loop < inputFile->length) { + if (column == 80) { column = 0; row++; } character = inputFile->buffer[loop]; - if (character == 1) - { + if (character == 1) { row = (inputFile->buffer[loop + 1] << 24) + (inputFile->buffer[loop + 2] << 16) + (inputFile->buffer[loop + 3] << 8) + inputFile->buffer[loop+4]; @@ -59,22 +56,19 @@ int ansilove_tundra(struct input *inputFile, struct output *outputFile) loop += 8; } - if (character == 2) - { + if (character == 2) { character = inputFile->buffer[loop + 1]; loop += 5; } - if (character == 4) - { + if (character == 4) { character = inputFile->buffer[loop + 1]; loop += 5; } - if (character == 6) - { + if (character == 6) { character = inputFile->buffer[loop + 1]; loop += 9; @@ -100,18 +94,15 @@ int ansilove_tundra(struct input *inputFile, struct output *outputFile) loop = 9; - while (loop < inputFile->length) - { - if (column == 80) - { + while (loop < inputFile->length) { + if (column == 80) { column = 0; row++; } character = inputFile->buffer[loop]; - if (character == 1) - { + if (character == 1) { row = (inputFile->buffer[loop + 1] << 24) + (inputFile->buffer[loop + 2] << 16) + (inputFile->buffer[loop + 3] << 8) + inputFile->buffer[loop + 4]; @@ -123,8 +114,7 @@ int ansilove_tundra(struct input *inputFile, struct output *outputFile) loop += 8; } - if (character == 2) - { + if (character == 2) { foreground = (inputFile->buffer[loop + 3] << 16) + (inputFile->buffer[loop + 4] << 8) + inputFile->buffer[loop + 5]; @@ -134,8 +124,7 @@ int ansilove_tundra(struct input *inputFile, struct output *outputFile) loop += 5; } - if (character == 4) - { + if (character == 4) { background = (inputFile->buffer[loop + 3] << 16) + (inputFile->buffer[loop + 4] << 8) + inputFile->buffer[loop+5]; @@ -144,8 +133,7 @@ int ansilove_tundra(struct input *inputFile, struct output *outputFile) loop += 5; } - if (character == 6) - { + if (character == 6) { foreground = (inputFile->buffer[loop + 3] << 16) + (inputFile->buffer[loop + 4] << 8) + inputFile->buffer[loop+5]; @@ -159,8 +147,7 @@ int ansilove_tundra(struct input *inputFile, struct output *outputFile) loop += 9; } - if (character != 1 && character != 2 && character != 4 && character != 6) - { + if (character != 1 && character != 2 && character != 4 && character != 6) { drawchar(canvas, fontData.font_data, outputFile->bits, fontData.height, column, row, background, foreground, character); diff --git a/src/loaders/xbin.c b/src/loaders/xbin.c @@ -46,8 +46,7 @@ int ansilove_xbin(struct input *inputFile, struct output *outputFile) int32_t loop; int32_t index; - for (loop = 0; loop < 16; loop++) - { + for (loop = 0; loop < 16; loop++) { index = (loop * 3) + offset; colors[loop] = gdImageColorAllocate(canvas, (inputFile->buffer[index] << 2 | inputFile->buffer[index] >> 4), @@ -56,8 +55,7 @@ int ansilove_xbin(struct input *inputFile, struct output *outputFile) } offset += 48; - } - else { + } else { for (int i = 0; i < 16; i++) { colors[i] = gdImageColorAllocate(canvas, binary_palette[i*3], binary_palette[i*3+1], @@ -80,8 +78,7 @@ int ansilove_xbin(struct input *inputFile, struct output *outputFile) font_data = font_data_xbin; offset += (xbin_fontsize * numchars); - } - else { + } else { // using default 80x25 font font_data = font_pc_80x25; } @@ -91,8 +88,7 @@ int ansilove_xbin(struct input *inputFile, struct output *outputFile) // read compressed xbin if ((xbin_flags & 4) == 4) { - while (offset < inputFile->length && row != xbin_height) - { + while (offset < inputFile->length && row != xbin_height) { int32_t ctype = inputFile->buffer[offset] & 0xC0; int32_t counter = (inputFile->buffer[offset] & 0x3F) + 1; @@ -144,20 +140,16 @@ int ansilove_xbin(struct input *inputFile, struct output *outputFile) column++; - if (column == xbin_width) - { + if (column == xbin_width) { column = 0; row++; } } } - } - // read uncompressed xbin - else { - while (offset < inputFile->length && row != xbin_height) - { - if (column == xbin_width) - { + } else { + // read uncompressed xbin + while (offset < inputFile->length && row != xbin_height) { + if (column == xbin_width) { column = 0; row++; }