libansilove

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

output.c (1552B)


      1 /*
      2  * output.c
      3  * libansilove 1.2.4
      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 "ansilove.h"
     15 #include "output.h"
     16 
     17 int
     18 output(struct ansilove_ctx *ctx, struct ansilove_options *options,
     19     gdImagePtr source)
     20 {
     21 	/* Handle DOS aspect ratio */
     22 	if (options->dos) {
     23 		gdImagePtr dos = gdImageCreateTrueColor(source->sx,
     24 		    source->sy * 1.35);
     25 
     26 		if (!dos) {
     27 			ctx->error = ANSILOVE_GD_ERROR;
     28 			return -1;
     29 		}
     30 
     31 		gdImageCopyResampled(dos, source, 0, 0, 0, 0,
     32 		    dos->sx, dos->sy, source->sx, source->sy);
     33 
     34 		gdImageDestroy(source);
     35 		source = dos;
     36 	}
     37 
     38 	/* Handle resizing */
     39 	if (options->scale_factor) {
     40 		gdImagePtr retina;
     41 
     42 		if gdImageTrueColor(source) {
     43 			retina = gdImageCreateTrueColor(source->sx *
     44 			     options->scale_factor, source->sy * options->scale_factor);
     45 		} else {
     46 			retina = gdImageCreate(source->sx *
     47 			     options->scale_factor, source->sy * options->scale_factor);
     48 		}
     49 
     50 		if (!retina) {
     51 			ctx->error = ANSILOVE_GD_ERROR;
     52 			return -1;
     53 		}
     54 
     55 		gdImageCopyResized(retina, source, 0, 0, 0, 0,
     56 		    retina->sx, retina->sy, source->sx, source->sy);
     57 
     58 		gdImageDestroy(source);
     59 		source = retina;
     60 	}
     61 
     62 	/* Handle transparency */
     63 	if (options->mode == ANSILOVE_MODE_TRANSPARENT)
     64 		gdImageColorTransparent(source, 0);
     65 
     66 	ctx->png.buffer = gdImagePngPtr(source, &ctx->png.length);
     67 
     68 	gdImageDestroy(source);
     69 
     70 	return 0;
     71 }