Haio 0.2.X
Homebrew Asset IO/CDN
Loading...
Searching...
No Matches
rgba8888.cpp
Go to the documentation of this file.
1#include <haio_convert.hpp>
2
3namespace {
4
5/** a five bit channel widens by repeating its high bits, so 31 reaches 255 exactly */
6constexpr uint8_t expand5(uint16_t value) { return static_cast<uint8_t>((value << 3) | (value >> 2)); }
7
8}
9
10namespace Haio::Codecs {
11
12/**
13 * @addtogroup move
14 * @{
15 */
16/** the spare top bit is dropped rather than read: in this colour it means nothing */
17template <>
18Result<void> Move<Color::RGB555, Color::RGBA8888>(Bytes src, std::span<uint8_t> dst, Size size) {
19 const auto pixels = static_cast<size_t>(size.width) * static_cast<size_t>(size.height);
20 if (src.size() < pixels * 2 || dst.size() < pixels * 4) {
21 HAIO_FAIL(InvalidInput, "rgb555 to rgba8888 got a buffer that is too small");
22 }
23
24 for (size_t at = 0, to = 0; at < pixels * 2; at += 2, to += 4) {
25 const auto packed = static_cast<uint16_t>(src[at + 0] | (src[at + 1] << 8));
26 dst[to + 0] = expand5((packed >> 10) & 0x1f);
27 dst[to + 1] = expand5((packed >> 5) & 0x1f);
28 dst[to + 2] = expand5(packed & 0x1f);
29 dst[to + 3] = 255;
30 }
31 return {};
32}
33/** @} */
34
35/**
36 * @addtogroup convert
37 * @{
38 */
39template <>
41 return convertVia<Color::RGB555, Color::RGBA8888>(std::move(src));
42}
43/** @} */
44
45}
Result< Image< Color::RGBA8888 > > Convert< Color::RGB555, Color::RGBA8888 >(Image< Color::RGB555 > src)
Definition rgba8888.cpp:40
Result< void > Move< Color::RGB555, Color::RGBA8888 >(Bytes src, std::span< uint8_t > dst, Size size)
Definition rgba8888.cpp:18
#define HAIO_FAIL(why, message)
Result< Image< To > > convertVia(Image< From > src)
std::expected< T, Error > Result
std::span< const uint8_t > Bytes