1 /*
2 * output.c
3 * libansilove 1.2.8
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
output(struct ansilove_ctx * ctx,struct ansilove_options * options,gdImagePtr source)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 if (options->scale_factor < 2 || options->scale_factor > 8) {
41 ctx->error = ANSILOVE_RANGE_ERROR;
42 return -1;
43 }
44
45 uint32_t width, height;
46 gdImagePtr retina;
47
48 width = source->sx * options->scale_factor;
49 height = source->sy * options->scale_factor;
50
51 retina = gdImageTrueColor(source) ?
52 gdImageCreateTrueColor(width, height) :
53 gdImageCreate(width, height);
54
55 if (!retina) {
56 ctx->error = ANSILOVE_GD_ERROR;
57 return -1;
58 }
59
60 gdImageCopyResized(retina, source, 0, 0, 0, 0,
61 retina->sx, retina->sy, source->sx, source->sy);
62
63 gdImageDestroy(source);
64 source = retina;
65 }
66
67 /* Handle transparency */
68 if (options->mode == ANSILOVE_MODE_TRANSPARENT)
69 gdImageColorTransparent(source, 0);
70
71 ctx->png.buffer = gdImagePngPtr(source, &ctx->png.length);
72
73 gdImageDestroy(source);
74
75 return 0;
76 }
77