Haio 0.2.X
Homebrew Asset IO/CDN
Loading...
Searching...
No Matches
rgb565.cpp
Go to the documentation of this file.
2
3namespace Haio::Codecs {
4
5/**
6 * @addtogroup move
7 * @{
8 */
9/** five bits of red, six of green, five of blue, rounded rather than truncated */
10template <>
11Result<void> Move<Color::RGBA8888, Color::RGB565>(Bytes src, std::span<uint8_t> dst, Size size) {
12 const auto pixels = static_cast<size_t>(size.width) * static_cast<size_t>(size.height);
13 if (src.size() < pixels * 4 || dst.size() < pixels * 2) {
14 HAIO_FAIL(InvalidInput, "rgba8888 to rgb565 got a buffer that is too small");
15 }
16
17 for (size_t at = 0, to = 0; at < pixels * 4; at += 4, to += 2) {
18 const auto r = static_cast<uint16_t>((src[at + 0] * 31 + 127) / 255);
19 const auto g = static_cast<uint16_t>((src[at + 1] * 63 + 127) / 255);
20 const auto b = static_cast<uint16_t>((src[at + 2] * 31 + 127) / 255);
21 const auto packed = static_cast<uint16_t>((r << 11) | (g << 5) | b);
22 dst[to + 0] = static_cast<uint8_t>(packed);
23 dst[to + 1] = static_cast<uint8_t>(packed >> 8);
24 }
25 return {};
26}
27/** @} */
28
29/**
30 * @addtogroup convert
31 * @{
32 */
33template <>
35 return convertVia<Color::RGBA8888, Color::RGB565>(std::move(src));
36}
37/** @} */
38
39}
Result< Image< Color::RGB565 > > Convert< Color::RGBA8888, Color::RGB565 >(Image< Color::RGBA8888 > src)
Definition rgb565.cpp:34
Result< void > Move< Color::RGBA8888, Color::RGB565 >(Bytes src, std::span< uint8_t > dst, Size size)
Definition rgb565.cpp:11
#define HAIO_FAIL(why, message)
Result< Image< To > > convertVia(Image< From > src)
std::expected< T, Error > Result
std::span< const uint8_t > Bytes