libansilove

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

output.c (1681B)


      1 /*
      2  * output.c
      3  * libansilove 1.2.8
      4  * https://www.ansilove.org
      5  *
      6  * Copyright (c) 2011-2021 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  * SPDX-License-Identifier: BSD-2-Clause
     13  */
     14 
     15 #include <gd.h>
     16 #include "ansilove.h"
     17 #include "output.h"
     18 
     19 int
     20 output(struct ansilove_ctx *ctx, struct ansilove_options *options,
     21     gdImagePtr source)
     22 {
     23 	/* Handle DOS aspect ratio */
     24 	if (options->dos) {
     25 		gdImagePtr dos = gdImageCreateTrueColor(source->sx,
     26 		    source->sy * 1.35);
     27 
     28 		if (!dos) {
     29 			ctx->error = ANSILOVE_GD_ERROR;
     30 			return -1;
     31 		}
     32 
     33 		gdImageCopyResampled(dos, source, 0, 0, 0, 0,
     34 		    dos->sx, dos->sy, source->sx, source->sy);
     35 
     36 		gdImageDestroy(source);
     37 		source = dos;
     38 	}
     39 
     40 	/* Handle resizing */
     41 	if (options->scale_factor) {
     42 		if (options->scale_factor < 2 || options->scale_factor > 8) {
     43 			ctx->error = ANSILOVE_RANGE_ERROR;
     44 			return -1;
     45 		}
     46 
     47 		uint32_t width, height;
     48 		gdImagePtr retina;
     49 
     50 		width = source->sx * options->scale_factor;
     51 		height = source->sy * options->scale_factor;
     52 
     53 		retina = gdImageTrueColor(source) ?
     54 			gdImageCreateTrueColor(width, height) :
     55 			gdImageCreate(width, height);
     56 
     57 		if (!retina) {
     58 			ctx->error = ANSILOVE_GD_ERROR;
     59 			return -1;
     60 		}
     61 
     62 		gdImageCopyResized(retina, source, 0, 0, 0, 0,
     63 		    retina->sx, retina->sy, source->sx, source->sy);
     64 
     65 		gdImageDestroy(source);
     66 		source = retina;
     67 	}
     68 
     69 	/* Handle transparency */
     70 	if (options->mode == ANSILOVE_MODE_TRANSPARENT)
     71 		gdImageColorTransparent(source, 0);
     72 
     73 	ctx->png.buffer = gdImagePngPtr(source, &ctx->png.length);
     74 
     75 	gdImageDestroy(source);
     76 
     77 	return 0;
     78 }