libansilove

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

output.c (1701B)


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