libansilove

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

artworx.c (2268B)


      1 /*
      2  * artworx.c
      3  * libansilove 1.2.7
      4  * https://www.ansilove.org
      5  *
      6  * Copyright (c) 2011-2020 Stefan Vogt, Brian Cassidy, and Frederic Cambus
      7  * All rights reserved.
      8  *
      9  * libansilove is licensed under the BSD 2-Clause License.
     10  * See LICENSE file for details.
     11  */
     12 
     13 #include <gd.h>
     14 #include <stddef.h>
     15 #include <stdint.h>
     16 #include "ansilove.h"
     17 #include "config.h"
     18 #include "drawchar.h"
     19 #include "output.h"
     20 
     21 #define ADF_HEADER_LENGTH 4289 /* 192 + 4096 + 1 */
     22 
     23 #define STATE_CHARACTER 0
     24 #define STATE_ATTRIBUTE 1
     25 
     26 int
     27 ansilove_artworx(struct ansilove_ctx *ctx, struct ansilove_options *options)
     28 {
     29 	uint8_t character, attribute, *cursor, state = STATE_CHARACTER;
     30 	uint32_t column = 0, row = 0;
     31 	uint32_t foreground, background;
     32 	uint32_t width, height;
     33 	size_t index, loop;
     34 
     35 	/* libgd image pointers */
     36 	gdImagePtr canvas;
     37 
     38 	if (ctx == NULL || options == NULL) {
     39 		if (ctx)
     40 			ctx->error = ANSILOVE_INVALID_PARAM;
     41 
     42 		return -1;
     43 	}
     44 
     45 	if (ctx->length < ADF_HEADER_LENGTH) {
     46 		ctx->error = ANSILOVE_FORMAT_ERROR;
     47 		return -1;
     48 	}
     49 
     50 	/* create ADF instance */
     51 	width = 640;
     52 	height = (ctx->length - ADF_HEADER_LENGTH) / 2 / 80 * 16;
     53 
     54 	if (!width || !height) {
     55 		ctx->error = ANSILOVE_FORMAT_ERROR;
     56 		return -1;
     57 	}
     58 
     59 	canvas = gdImageCreate(width, height);
     60 
     61 	if (!canvas) {
     62 		ctx->error = ANSILOVE_GD_ERROR;
     63 		return -1;
     64 	}
     65 
     66 
     67 	/* process ADF palette */
     68 	for (loop = 0; loop < 16; loop++) {
     69 		index = (adf_colors[loop] * 3) + 1;
     70 		gdImageColorAllocate(canvas,
     71 		    ctx->buffer[index] << 2 | ctx->buffer[index] >> 4,
     72 		    ctx->buffer[index + 1] << 2 | ctx->buffer[index + 1] >> 4,
     73 		    ctx->buffer[index + 2] << 2 | ctx->buffer[index + 2] >> 4);
     74 	}
     75 
     76 	/* process ADF */
     77 	loop = ADF_HEADER_LENGTH;
     78 
     79 	while (loop < ctx->length) {
     80 		cursor = &ctx->buffer[loop];
     81 
     82 		if (column == 80) {
     83 			column = 0;
     84 			row++;
     85 		}
     86 
     87 		switch (state) {
     88 		case STATE_CHARACTER:
     89 			character = *cursor;
     90 			state = STATE_ATTRIBUTE;
     91 			break;
     92 		case STATE_ATTRIBUTE:
     93 			attribute = *cursor;
     94 
     95 			background = (attribute & 240) >> 4;
     96 			foreground = attribute & 15;
     97 
     98 			drawchar(canvas, ctx->buffer+193, 8, 16, column, row,
     99 			    background, foreground, character);
    100 
    101 			column++;
    102 
    103 			state = STATE_CHARACTER;
    104 			break;
    105 		}
    106 
    107 		loop++;
    108 	}
    109 
    110 	/* create output file */
    111 	return output(ctx, options, canvas);
    112 }