diff options
| author | tazy <gardendistrict0x0@outlook.com> | 2026-01-04 21:19:13 +0000 |
|---|---|---|
| committer | tazy <gardendistrict0x0@outlook.com> | 2026-01-04 21:19:13 +0000 |
| commit | d33aeddbfb1510c7a1846067c5512cfb942ccb17 (patch) | |
| tree | 635055db5399af3d9b3e87b5a5241a3886e1090b | |
| parent | ab3873609baf282f9158566c8242be2c9b126f5d (diff) | |
feat(dbg): Add debugging to png.c
| -rw-r--r-- | include/tinyff/common.h | 9 | ||||
| -rw-r--r-- | include/tinyff/dbg.h | 29 | ||||
| -rw-r--r-- | include/tinyff/image/png.h | 14 | ||||
| -rw-r--r-- | include/tinyff/result.h | 7 | ||||
| -rw-r--r-- | src/format/image/dbg.c | 20 | ||||
| -rw-r--r-- | src/format/image/png.c | 84 |
6 files changed, 129 insertions, 34 deletions
diff --git a/include/tinyff/common.h b/include/tinyff/common.h index d53adae..1fc4efb 100644 --- a/include/tinyff/common.h +++ b/include/tinyff/common.h @@ -5,10 +5,11 @@ #include <stdint.h> -// Human readable number types -// NOTE: The only reason I created these was so -// that debugging will be easier -typedef uint8_t ff_byte; +// Flags +typedef bool ff_flag; + +#define FF_ENABLE true +#define FF_DISABLE false // Functions diff --git a/include/tinyff/dbg.h b/include/tinyff/dbg.h new file mode 100644 index 0000000..7392f5a --- /dev/null +++ b/include/tinyff/dbg.h @@ -0,0 +1,29 @@ +#ifndef DBG_H +#define DBG_H + +// Debug writes extra messages to a stream of the user's choice +// To enable debug, the user needs to include this header +// Then they can use the ff_set_debug_stream function to set the stream +// And then use ff_dprintf to print debug messages +// By default, debug is disabled + + +#define FF_DEBUG_ENABLED 1 + +#include <stdio.h> +#include <stdarg.h> +#include "result.h" +#include "common.h" + +static FILE *intff_debug_stream = NULL; + +// This flag is the same name as FF_DEBUG_ENABLED but is local to this file +// Thats why it has the int prefix +static ff_flag intff_debug_enabled = 0; + +ff_result ff_set_debug_stream(FILE *stream); + + +ff_result ff_dprintf(const char *format, ...); + +#endif
\ No newline at end of file diff --git a/include/tinyff/image/png.h b/include/tinyff/image/png.h index b2f400f..80e8fe6 100644 --- a/include/tinyff/image/png.h +++ b/include/tinyff/image/png.h @@ -38,7 +38,7 @@ typedef struct { // Chunk handling -typedef (*ff_png_chunk_handler_ptr)(uint8_t *data, size_t len, ff_png_ctx* ctx); +typedef (*ff_png_chunk_handler_ptr)(uint8_t *buf, size_t len, ff_png_ctx* ctx); typedef struct { const char *type; @@ -54,7 +54,17 @@ const ff_png_chunk_handler ff_png_chunk_handlers[] = { {NULL, NULL} // Terminator }; -void ff_png_header_handler(uint8_t *data, size_t len, ff_png_ctx* ctx); +// Handler declarations +// Massive W.I.P + +// Required by definition +void ff_png_header_handler(uint8_t *buf, size_t len, ff_png_ctx* ctx); // IHDR +void ff_png_data_handler(uint8_t *buf, size_t len, ff_png_ctx* ctx); // IDAT +void ff_png_end_handler(uint8_t *buf, size_t len, ff_png_ctx* ctx); // IEND + +// PLTE is a funny one +// Its only required for indexed color types + ff_result ff_png_isvalid(FILE *file); ff_result ff_open_png(const char *filepath, ff_png_ctx **out_ctx); diff --git a/include/tinyff/result.h b/include/tinyff/result.h index 9b17d4a..daea32e 100644 --- a/include/tinyff/result.h +++ b/include/tinyff/result.h @@ -2,8 +2,15 @@ #define RESULT_H typedef enum { + // -- SUCCESS -- FF_RESULT_OK = 0, + // -- WARNINGS -- + FF_RESULT_WARN_DEBUG_DISABLED, + FF_RESULT_WARN_NO_IMPL, + + // -- ERRORS -- + // File FF_RESULT_ERROR_INVALID_FILE_SIGNITURE, FF_RESULT_ERROR_INVALID_FILE, diff --git a/src/format/image/dbg.c b/src/format/image/dbg.c new file mode 100644 index 0000000..56cb53b --- /dev/null +++ b/src/format/image/dbg.c @@ -0,0 +1,20 @@ +#include "dbg.h" + +ff_result ff_set_debug_stream(FILE *stream) +{ + intff_debug_stream = stream; + intff_debug_enabled = (stream != NULL); + return FF_RESULT_OK; +} + +ff_result ff_dprintf(const char *format, ...) +{ + if (!intff_debug_enabled || !intff_debug_stream) return FF_RESULT_WARN_DEBUG_DISABLED; + + va_list args; + va_start(args, format); + vfprintf(intff_debug_stream, format, args); + va_end(args); + + return FF_RESULT_OK; +} diff --git a/src/format/image/png.c b/src/format/image/png.c index 1170af2..d177819 100644 --- a/src/format/image/png.c +++ b/src/format/image/png.c @@ -1,56 +1,84 @@ #include "tinyff/image/png.h" -#include "png.h" - - +#include "tinyff/dbg.h" ff_result ff_png_isvalid(FILE *file) { + ff_dprintf("png: validating signature\n"); + char raw_sig[8]; - if (fread(raw_sig, sizeof(char), 8, file) != 8) { - // Unable to read first 8 bytes + if (fread(raw_sig, 1, 8, file) != 8) { + ff_dprintf("png: failed to read signature bytes\n"); return FF_RESULT_ERROR_INVALID_FILE; } + ff_dprintf( + "png: signature read: %02X %02X %02X %02X %02X %02X %02X %02X\n", + raw_sig[0], raw_sig[1], raw_sig[2], raw_sig[3], + raw_sig[4], raw_sig[5], raw_sig[6], raw_sig[7] + ); + if (memcmp(raw_sig, PNG_SIGNATURE, 8) != 0) { - // Signature did not match + ff_dprintf("png: signature mismatch\n"); return FF_RESULT_ERROR_INVALID_FILE_SIGNITURE; } + ff_dprintf("png: signature valid\n"); return FF_RESULT_OK; } -ff_result ff_open_png(const char *filepath, ff_png_ctx **out_ctx) { - // Validate - +ff_result ff_open_png(const char *filepath, ff_png_ctx **out_ctx) +{ + ff_dprintf("png: opening file '%s'\n", filepath); + ff_png_ctx ctx; ctx.raw = fopen(filepath, "rb"); if (!ctx.raw) { + ff_dprintf("png: fopen failed\n"); return FF_RESULT_ERROR_READ_FILE_FAILURE; } - if (ff_png_isvalid(ctx.raw) != FF_RESULT_OK) { - return ff_png_isvalid(ctx.raw); + ff_dprintf("png: file opened successfully\n"); + + ff_result res = ff_png_isvalid(ctx.raw); + if (res != FF_RESULT_OK) { + ff_dprintf("png: validation failed (%d)\n", res); + fclose(ctx.raw); + return res; } - // This is a heavy W.I.P - + ff_dprintf("png: validation passed\n"); + + *out_ctx = NULL; + ff_dprintf("png: open_png reached WIP end\n"); + + return FF_RESULT_OK; } -void ff_png_header_handler(uint8_t *data, size_t len, ff_png_ctx *ctx) +void ff_png_header_handler(uint8_t *buf, size_t len, ff_png_ctx *ctx) { - if (len != 13) return; // Invalid length - printf("Width: %d\n", get_big_endian(data)); - printf("Height: %d\n", get_big_endian(data + 4)); - printf("Bit depth: %d\n", (unsigned char)data[8]); - printf("Color type: %d\n", (unsigned char)data[9]); - printf("Compression method: %d\n", (unsigned char)data[10]); - printf("Filter method: %d\n", (unsigned char)data[11]); - printf("Interlace method: %d\n", (unsigned char)data[12]); - - // Store in context - ctx->width = get_big_endian(data); - ctx->height = get_big_endian(data + 4); - ctx->bit_depth = (unsigned char)data[8]; - ctx->color_type = (unsigned char)data[9]; + ff_dprintf("png: IHDR chunk received (len=%zu)\n", len); + + if (len != 13) { + ff_dprintf("png: invalid IHDR length\n"); + return; + } + + uint32_t w = get_big_endian(buf); + uint32_t h = get_big_endian(buf + 4); + + ff_dprintf("Width: %u\n", w); + ff_dprintf("Height: %u\n", h); + ff_dprintf("Bit depth: %u\n", buf[8]); + ff_dprintf("Color type: %u\n", buf[9]); + ff_dprintf("Compression method: %u\n", buf[10]); + ff_dprintf("Filter method: %u\n", buf[11]); + ff_dprintf("Interlace method: %u\n", buf[12]); + + ctx->width = w; + ctx->height = h; + ctx->bit_depth = buf[8]; + ctx->color_type = buf[9]; + + ff_dprintf("png: IHDR stored in context\n"); }
\ No newline at end of file |
