File size: 1,405 Bytes
ef28ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
336a9be
24463b6
 
ef28ed2
 
 
 
 
 
 
 
 
336a9be
24463b6
 
ef28ed2
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#pragma once

#include <cstdint>
#include <string>
#include <vector>

namespace astra {

struct DecodedImage {
    int width = 0;
    int height = 0;
    std::string format; // "PNG", "BMP", "PPM"
    std::vector<uint32_t> argb;
    bool valid = false;
    std::string error;
};

// Zero-build-dependency native image decoder (inspired by Firefox image/decoders/).
// Supports:
//   - PNG (8-bit RGB, RGBA, Grayscale, Grayscale+Alpha, Indexed PLTE; all 5 PNG filters)
//   - JPEG (Pure C++20 Baseline Sequential DCT: Grayscale & YCbCr 4:4:4/4:2:2/4:2:0, DRI)
//   - GIF (Pure C++20 GIF87a/GIF89a LZW decoder: GCT/LCT, transparency, interlacing)
//   - WebP (RIFF WEBP container + runtime dlopen libwebp.so.7/6 decoder)
//   - BMP (24-bit & 32-bit uncompressed DIB)
//   - PPM (P6 binary RGB)
class ImageDecoder {
public:
    static DecodedImage decode_bytes(const uint8_t* data, size_t len);
    static DecodedImage decode_file(const std::string& path);

private:
    static DecodedImage decode_png(const uint8_t* data, size_t len);
    static DecodedImage decode_jpeg(const uint8_t* data, size_t len);
    static DecodedImage decode_gif(const uint8_t* data, size_t len);
    static DecodedImage decode_webp(const uint8_t* data, size_t len);
    static DecodedImage decode_bmp(const uint8_t* data, size_t len);
    static DecodedImage decode_ppm(const uint8_t* data, size_t len);
};

} // namespace astra