summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/tinyff/image/png.h29
-rw-r--r--src/format/image/png.c37
2 files changed, 66 insertions, 0 deletions
diff --git a/include/tinyff/image/png.h b/include/tinyff/image/png.h
new file mode 100644
index 0000000..caed675
--- /dev/null
+++ b/include/tinyff/image/png.h
@@ -0,0 +1,29 @@
+#ifndef PNG_H
+#define PNG_H
+
+#include <stdint.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <string.h>
+
+#include "tinyff/result.h"
+
+static const unsigned char PNG_SIGNATURE[8] = {
+ 0x89, 'P', 'N', 'G', 0x0D, 0x0A, 0x1A, 0x0A
+};
+
+typedef struct {
+ FILE *raw;
+ uint32_t width;
+ uint32_t height;
+ uint8_t bit_depth;
+ uint8_t color_type;
+ uint8_t *pixels;
+ size_t count_pixels;
+ bool valid;
+} ff_png_ctx;
+
+ff_result ff_png_isvalid(FILE *file);
+ff_png_ctx *ff_open_png(const char *filepath);
+
+#endif
diff --git a/src/format/image/png.c b/src/format/image/png.c
new file mode 100644
index 0000000..b29e271
--- /dev/null
+++ b/src/format/image/png.c
@@ -0,0 +1,37 @@
+#include "tinyff/image/png.h"
+#include "png.h"
+
+ff_result ff_png_isvalid(FILE *file)
+{
+ char raw_sig[8];
+ if (fread(raw_sig, sizeof(char), 8, file) != 8) {
+ // Inable to read first 8 bytes
+ fclose(file);
+ return FF_RESULT_ERROR_INVALID_FILE;
+ }
+
+ if (memcmp(raw_sig, PNG_SIGNATURE, 8) != 0) {
+ // Signiture did not match
+ fclose(file);
+ return FF_RESULT_ERROR_INVALID_FILE_SIGNITURE;
+ }
+
+ return FF_RESULT_OK;
+}
+
+ff_png_ctx *ff_open_png(const char *filepath) {
+ // Validate
+
+ ff_png_ctx ctx;
+ ctx.raw = fopen(filepath, "rb");
+
+ if (!ctx.raw) {
+ return NULL;
+ }
+
+ if (ff_png_isvalid(ctx.raw) != FF_RESULT_OK) {
+ return NULL;
+ }
+
+ // TODO: Parse
+} \ No newline at end of file