From ee5792ed8874c65850bd53569089fb85183309ac Mon Sep 17 00:00:00 2001 From: Steve Howes Date: Fri, 10 Feb 2023 20:12:32 +0000 Subject: [PATCH] Initial commit --- Makefile | 11 +++++++++ aseprite2ani.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 Makefile create mode 100644 aseprite2ani.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d77d4ae --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +CC = gcc +CFLAGS = -Wall -Wextra -O2 +LDFLAGS = -ljansson + +aseprite2ani: aseprite2ani.c + $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) + +clean: + rm -f aseprite2ani + +.PHONY: clean diff --git a/aseprite2ani.c b/aseprite2ani.c new file mode 100644 index 0000000..d67772b --- /dev/null +++ b/aseprite2ani.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include + +#define BUFFER_SIZE 1024 + +int main(int argc, char *argv[]) { + if (argc < 3) { + printf("Usage: %s \n", argv[0]); + return 1; + } + + // Open the input file + FILE *input_file = fopen(argv[1], "r"); + if (!input_file) { + printf("Error: Could not open input file %s\n", argv[1]); + return 1; + } + + // Read the entire contents of the input file into a buffer + char buffer[BUFFER_SIZE]; + size_t bytes_read = fread(buffer, 1, BUFFER_SIZE, input_file); + if (bytes_read == 0) { + printf("Error: Could not read any data from input file %s\n", argv[1]); + return 1; + } + buffer[bytes_read] = '\0'; + + // Parse the JSON data + json_error_t error; + json_t *root = json_loads(buffer, 0, &error); + if (!root) { + printf("Error: Could not parse JSON data: %s (line %d, column %d)\n", error.text, error.line, error.column); + return 1; + } + + // Extract the frames object + json_t *frames = json_object_get(root, "frames"); + if (!frames || !json_is_object(frames)) { + printf("Error: Could not find frames object in JSON data\n"); + return 1; + } + + // Open the output file + FILE *output_file = fopen(argv[2], "wb"); + if (!output_file) { + printf("Error: Could not open output file %s\n", argv[2]); + return 1; + } + + // Write the frame durations to the output file + const char *key; + json_t *value; + json_object_foreach(frames, key, value) { + int duration = json_integer_value(json_object_get(value, "duration")); + fwrite(&duration, sizeof(int), 1, output_file); + } + + // Clean up + fclose(input_file); + fclose(output_file); + json_decref(root); + + return 0; +}