Haio
0.2.X
Homebrew Asset IO/CDN
Toggle main menu visibility
Loading...
Searching...
No Matches
rgb555.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, rounded rather than truncated, and the alpha is dropped: this
11
* colour has nowhere to put it and saying so here is better than leaving a caller to
12
* wonder why a transparent picture came back solid.
13
*/
14
template
<>
15
Result<void>
Move<Color::RGBA8888, Color::RGB555>
(
Bytes
src, std::span<uint8_t> dst,
Size
size) {
16
const
auto
pixels =
static_cast<
size_t
>
(size.
width
) *
static_cast<
size_t
>
(size.
height
);
17
if
(src.size() < pixels * 4 || dst.size() < pixels * 2) {
18
HAIO_FAIL
(
InvalidInput
,
"rgba8888 to rgb555 got a buffer that is too small"
);
19
}
20
21
for
(
size_t
at = 0, to = 0; at < pixels * 4; at += 4, to += 2) {
22
const
auto
r =
static_cast<
uint16_t
>
((src[at + 0] * 31 + 127) / 255);
23
const
auto
g =
static_cast<
uint16_t
>
((src[at + 1] * 31 + 127) / 255);
24
const
auto
b =
static_cast<
uint16_t
>
((src[at + 2] * 31 + 127) / 255);
25
const
auto
packed =
static_cast<
uint16_t
>
((r << 10) | (g << 5) | b);
26
dst[to + 0] =
static_cast<
uint8_t
>
(packed);
27
dst[to + 1] =
static_cast<
uint8_t
>
(packed >> 8);
28
}
29
return
{};
30
}
31
/** @} */
32
33
/**
34
* @addtogroup convert
35
* @{
36
*/
37
template
<>
38
Result<Image<Color::RGB555>
>
Convert<Color::RGBA8888, Color::RGB555>
(
Image<Color::RGBA8888>
src) {
39
return
convertVia<Color::RGBA8888, Color::RGB555>
(std::move(src));
40
}
41
/** @} */
42
43
}
Haio::Codecs::Convert< Color::RGBA8888, Color::RGB555 >
Result< Image< Color::RGB555 > > Convert< Color::RGBA8888, Color::RGB555 >(Image< Color::RGBA8888 > src)
Definition
rgb555.cpp:38
Haio::Codecs::Move< Color::RGBA8888, Color::RGB555 >
Result< void > Move< Color::RGBA8888, Color::RGB555 >(Bytes src, std::span< uint8_t > dst, Size size)
Definition
rgb555.cpp:15
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
rgb555.cpp
Generated by
1.18.0