Torch
Loading...
Searching...
No Matches
BKImage.h
1#pragma once
2
3#include <cstdint>
4#include <string>
5#include <vector>
6
7namespace BK64 {
8
9// 8-bit RGBA, rows top to bottom.
10struct RgbaImage {
11 int width = 0;
12 int height = 0;
13 std::vector<uint8_t> pixels;
14};
15
16// A channel of `bits` bits to 8 and back. Each undoes the other.
17inline uint8_t ExpandBits(uint32_t value, int bits) {
18 const uint32_t max = (1u << bits) - 1;
19 return (uint8_t)((value * 255 + max / 2) / max);
20}
21
22inline uint32_t CompressBits(uint8_t value, int bits) {
23 const uint32_t max = (1u << bits) - 1;
24 return (value * max + 127) / 255;
25}
26
28 int width = 0;
29 int height = 0;
30 std::vector<uint8_t> indices;
31 std::vector<uint8_t> palette; // RGBA per entry
32};
33
34// A PNG holds one frame and a GIF one per frame, each the full canvas. Delays are in ms.
35bool DecodeImage(const std::vector<uint8_t>& file, std::vector<RgbaImage>& frames, std::vector<int>* delays,
36 std::string& error);
37// A palette PNG's own indices and palette. False for any other PNG.
38bool DecodeIndexedPng(const std::vector<uint8_t>& file, IndexedImage& out);
39bool IsGif(const std::vector<uint8_t>& file);
40
41std::vector<uint8_t> EncodePng(const RgbaImage& image);
42// One channel, or two with alpha.
43std::vector<uint8_t> EncodeGrayPng(int width, int height, const std::vector<uint8_t>& channels, bool alpha);
44std::vector<uint8_t> EncodeIndexedPng(const IndexedImage& image);
45
46// Up to `colors` entries, all exact RGBA5551. Pixels under half alpha share one transparent entry.
47IndexedImage Quantize(const RgbaImage& image, int colors);
48
49} // namespace BK64
Definition BKImage.h:27
Definition BKImage.h:10