From e2e9be992b948595086db6ac62cfb5b822c622d6 Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 18 Mar 2026 16:32:29 +1300 Subject: feat: Started on webp implementation Currently very basic, can only read simple things such as width and height --- src/WEBPImage.cpp | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/WEBPImage.cpp (limited to 'src/WEBPImage.cpp') diff --git a/src/WEBPImage.cpp b/src/WEBPImage.cpp new file mode 100644 index 0000000..4c4652e --- /dev/null +++ b/src/WEBPImage.cpp @@ -0,0 +1,102 @@ +#include "WEBPImage.h" +#include "reader.h" + +#define ENABLE_DEBUG +#include "debug.h" + +#include + +using std::cout, std::endl; + +namespace TehImage +{ + int WEBPImage::writeToFile(std::string filename) + { + cout << "Not implemented" << endl; + return 1; + } + + int WEBPImage::readFromFile(std::string filename) + { + static_assert(std::endian::native == std::endian::little, "Must use little endian"); + reader = std::make_unique(filename, FileEndianness::LITTLE); + + refreshBitBuffer(); + + char RIFF[4], WEBP[4]; + char RIFFExpected[] = {'R','I','F','F'}; + readFourCC(RIFF); + uint32_t imgSize = readBits(32); + readFourCC(WEBP); + char WEBPExpected[] = {'W','E','B','P'}; + if(strncmp(RIFF, RIFFExpected, 4) != 0 || strncmp(WEBP, WEBPExpected, 4) != 0) + { + cout << "Not a webp" << endl; + // cout << '#' << std::hex << RIFF.raw << " #" << RIFFExpected.raw << std::dec << endl; + // cout << '#' << std::hex << WEBP.raw << " #" << WEBPExpected.raw << std::dec << endl; + return 1; + } + + // refreshBitBuffer(); + + char VP8L[4]; + readFourCC(VP8L); + char VP8LExpected[] = {'V', 'P', '8', 'L'}; + uint32_t VP8LSize = readBits(32); + uint8_t signature = readBits(8); + if(strncmp(VP8L, VP8LExpected, 4) != 0) + { + cout << "VP8L error" << endl; + return 1; + } + + uint16_t width = readBits(14) + 1; + uint16_t height = readBits(14) + 1; + + cout << width << " " << height << endl; + + bool alpha = readBits(1); + uint8_t versionNumber = readBits(3); + + cout << "Alpha: " << (alpha?"Y":"N") << ", version: " << 0+versionNumber << endl; + + if (readBits(1)) { + cout << (int) readBits(2) << endl; + } + + cout << "Not finished" << endl; + return 1; + } + + bool WEBPImage::readBit() + { + bool res = bitBuffer & (1 << (bitBufferPos++)); + if(bitBufferPos >= 8) + refreshBitBuffer(); + return res?1:0; + } + + uint32_t WEBPImage::readBits(size_t count) + { + uint32_t out = 0; + for(int i = 0; i < count; i++) + { + out |= readBit() << i; + } + return out; + } + + void WEBPImage::readFourCC(char* dest) + { + for(int i = 0; i < 4; i++) + { + dest[i] = readBits(8); + } + } + + void WEBPImage::refreshBitBuffer() + { + bitBuffer = reader->readByte(); + bitBufferPos = 0; + } +} -- cgit v1.2.3