commit 91320ecf28647b75e31b0cb2633f8ae18d70c74f
parent 3581169806a8a7ce56892907ccb320dec94b993d
Author: Frederic Cambus <fred@statdns.com>
Date: Tue, 11 Sep 2018 23:18:42 +0200
Add ansilove_savefile() function to output PNG data to a file
Diffstat:
3 files changed, 35 insertions(+), 1 deletion(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
@@ -21,7 +21,7 @@ find_path(GD_INCLUDE_DIRS gd.h)
find_library(GD_LIBRARIES NAMES gd REQUIRED)
include_directories(${GD_INCLUDE_DIRS})
-set(SRC src/drawchar.c src/fonts.c src/loadfile.c src/init.c src/output.c)
+set(SRC src/drawchar.c src/fonts.c src/loadfile.c src/init.c src/output.c src/savefile.c)
set(LOADERS src/loaders/ansi.c src/loaders/artworx.c src/loaders/binary.c src/loaders/icedraw.c src/loaders/pcboard.c src/loaders/tundra.c src/loaders/xbin.c)
if(NOT HAVE_STRTONUM)
diff --git a/include/ansilove.h b/include/ansilove.h
@@ -46,7 +46,9 @@ struct ansilove_options {
};
void ansilove_init(struct ansilove_ctx *, struct ansilove_options *);
+
int ansilove_loadfile(struct ansilove_ctx *, char *);
+int ansilove_savefile(struct ansilove_ctx *, char *);
int ansilove_ansi(struct ansilove_ctx *, struct ansilove_options *);
int ansilove_artworx(struct ansilove_ctx *, struct ansilove_options *);
diff --git a/src/savefile.c b/src/savefile.c
@@ -0,0 +1,32 @@
+//
+// savefile.c
+// AnsiLove/C
+//
+// Copyright (c) 2011-2018 Stefan Vogt, Brian Cassidy, and Frederic Cambus.
+// All rights reserved.
+//
+// This source code is licensed under the BSD 2-Clause License.
+// See the LICENSE file for details.
+//
+
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include "ansilove.h"
+
+int
+ansilove_savefile(struct ansilove_ctx *ctx, char *output) {
+
+ FILE *file = fopen(output, "wb");
+
+ if (file) {
+ fwrite(ctx->png.buffer, ctx->png.length, 1, file);
+ fclose(file);
+ } else {
+ // XXX Set error code
+ return -1;
+ }
+
+ return 0;
+}