From 67991882c40a6a2e7a582d03207dc355b51f23be Mon Sep 17 00:00:00 2001 From: TazyFoundSoup Date: Thu, 15 Jan 2026 21:15:40 +1100 Subject: feat(png): Implement PLTE chunk handling --- src/format/image/png.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'src/format') diff --git a/src/format/image/png.c b/src/format/image/png.c index 7de02d7..fc3d84a 100644 --- a/src/format/image/png.c +++ b/src/format/image/png.c @@ -102,12 +102,27 @@ ff_result ff_png_palette_handler(uint8_t *buf, size_t len, ff_png_ctx *ctx) { ff_dprintf("png: PLTE chunk received (len=%zu)\n", len); + if (len % 3 != 0) { + ff_dprintf("png: invalid PLTE length\n"); + return FF_RESULT_ERROR_INVALID_FILE; + } + uint16_t num_entries = len / 3; ff_dprintf("png: PLTE contains %u palette entries\n", num_entries); - - - return FF_RESULT_WARN_NO_IMPL; + ctx->palette_size = num_entries; + ctx->palette = malloc(len); + if (!ctx->palette) return FF_RESULT_ERROR_MEMORY_ALLOCATION; + + for (uint16_t i = 0; i < num_entries; i++) { + // There's 4 bytes per entry just because if there's + // a tRNS chunk later, we can just fill in the alpha values + ctx->palette[i * 4 + 0] = buf[i * 3 + 0]; // R + ctx->palette[i * 4 + 1] = buf[i * 3 + 1]; // R + ctx->palette[i * 4 + 2] = buf[i * 3 + 2]; // R + } + + return FF_RESULT_OK; } ff_result ff_png_data_handler(uint8_t *buf, size_t len, ff_png_ctx *ctx) -- cgit v1.2.3