Haio
0.2.X
Homebrew Asset IO/CDN
Toggle main menu visibility
Loading...
Searching...
No Matches
rgba5551.cpp
Go to the documentation of this file.
1
#include <
haio_convert.hpp
>
2
3
namespace
Haio::Codecs
{
4
5
/**
6
* @addtogroup move
7
* @{
8
*/
9
/**
10
* five bits a channel and one of alpha.
11
*
12
* the alpha rounds like everything else does, at half: a pixel that is more there
13
* than not stays, and the rest goes. there is no third answer to give, which is the
14
* whole reason a one bit alpha is worth asking for rather than something to apologise
15
* for -- a sprite sheet for hardware that only ever tested one bit wants exactly this.
16
*/
17
template
<>
18
Result<void>
Move<Color::RGBA8888, Color::RGBA5551>
(
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 * 4 || dst.size() < pixels * 2) {
21
HAIO_FAIL
(
InvalidInput
,
"rgba8888 to rgba5551 got a buffer that is too small"
);
22
}
23
24
for
(
size_t
at = 0, to = 0; at < pixels * 4; at += 4, to += 2) {
25
const
auto
r =
static_cast<
uint16_t
>
((src[at + 0] * 31 + 127) / 255);
26
const
auto
g =
static_cast<
uint16_t
>
((src[at + 1] * 31 + 127) / 255);
27
const
auto
b =
static_cast<
uint16_t
>
((src[at + 2] * 31 + 127) / 255);
28
const
auto
a =
static_cast<
uint16_t
>
(src[at + 3] >= 128 ? 1 : 0);
29
const
auto
packed =
static_cast<
uint16_t
>
((r << 11) | (g << 6) | (b << 1) | a);
30
dst[to + 0] =
static_cast<
uint8_t
>
(packed);
31
dst[to + 1] =
static_cast<
uint8_t
>
(packed >> 8);
32
}
33
return
{};
34
}
35
/** @} */
36
37
/**
38
* @addtogroup convert
39
* @{
40
*/
41
template
<>
42
Result<Image<Color::RGBA5551>
>
Convert<Color::RGBA8888, Color::RGBA5551>
(
Image<Color::RGBA8888>
src) {
43
return
convertVia<Color::RGBA8888, Color::RGBA5551>
(std::move(src));
44
}
45
/** @} */
46
47
}
Haio::Codecs::Convert< Color::RGBA8888, Color::RGBA5551 >
Result< Image< Color::RGBA5551 > > Convert< Color::RGBA8888, Color::RGBA5551 >(Image< Color::RGBA8888 > src)
Definition
rgba5551.cpp:42
Haio::Codecs::Move< Color::RGBA8888, Color::RGBA5551 >
Result< void > Move< Color::RGBA8888, Color::RGBA5551 >(Bytes src, std::span< uint8_t > dst, Size size)
Definition
rgba5551.cpp:18
HAIO_FAIL
#define HAIO_FAIL(why, message)
Definition
haio_codec.hpp:258
haio_convert.hpp
Haio::Codecs
Definition
haio_codec.hpp:136
Haio::Codecs::convertVia
Result< Image< To > > convertVia(Image< From > src)
Definition
haio_convert.hpp:21
Haio::Result
std::expected< T, Error > Result
Definition
haio_codec.hpp:32
Haio::Size::height
int height
Definition
haio_object.hpp:14
Haio::ErrorCode::InvalidInput
@ InvalidInput
Definition
haio_codec.hpp:18
Haio::Bytes
std::span< const uint8_t > Bytes
Definition
haio_codec.hpp:14
Haio::Size::width
int width
Definition
haio_object.hpp:13
Haio::Size
Definition
haio_object.hpp:12
Haio::Image
Definition
haio_codec.hpp:36
library
backend
codecs
color
rgba8888
rgba5551.cpp
Generated by
1.18.0