diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/format/image/dbg.c | 20 | ||||
| -rw-r--r-- | src/format/image/png.c | 84 |
2 files changed, 76 insertions, 28 deletions
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 |
