Haio 0.2.X
Homebrew Asset IO/CDN
Loading...
Searching...
No Matches
rgba8888.cpp
Go to the documentation of this file.
2
3namespace Haio::Codecs {
4
5/**
6 * indices become the colours they point at.
7 *
8 * this one direction is always possible and never loses anything, which is why it
9 * needs no filter while the way back does.
10 */
11template <>
13 const auto pixels = static_cast<size_t>(src.width) * static_cast<size_t>(src.height);
14 if (src.data.size() != pixels) HAIO_FAIL(InvalidInput, "invalid palette image: one index per pixel");
15 if (src.entries.empty()) {
16 HAIO_FAIL(InvalidInput, "this picture is indices with no palette; give it one with -palete");
17 }
18
19 std::vector<uint8_t> out(pixels * 4);
20 for (size_t at = 0; at < pixels; at++) {
21 const auto index = src.data[at];
22 if (index >= src.entries.size()) {
23 HAIO_FAIL(InvalidInput, "the picture points at colour " + std::to_string(index)
24 + " and the palette has " + std::to_string(src.entries.size()));
25 }
26 const auto colour = src.entries[index];
27 out[at * 4 + 0] = static_cast<uint8_t>((colour >> 16) & 0xFF);
28 out[at * 4 + 1] = static_cast<uint8_t>((colour >> 8) & 0xFF);
29 out[at * 4 + 2] = static_cast<uint8_t>(colour & 0xFF);
30 out[at * 4 + 3] = 0xFF;
31 }
32 return Image<Color::RGBA8888>{src.width, src.height, std::move(out)};
33}
34
35}
#define HAIO_FAIL(why, message)
Result< Image< Color::RGBA8888 > > Convert< Color::PALETTE, Color::RGBA8888 >(Image< Color::PALETTE > src)
Definition rgba8888.cpp:12
std::expected< T, Error > Result
std::vector< uint8_t > data