Haio
0.2.X
Homebrew Asset IO/CDN
Toggle main menu visibility
Loading...
Searching...
No Matches
encode.cpp
Go to the documentation of this file.
1
#include <
haio_codec.hpp
>
2
#include <
haio_convert.hpp
>
3
#include <
haio_util.hpp
>
4
5
namespace
Haio::Codecs
{
6
namespace
{
7
8
template
<Color P>
9
Result<Blob>
encodeDds(
Image<P>
img) {
10
const
Size
size{img.
width
, img.
height
};
11
HAIO_TRY
(expected,
sizeOf
(P, size));
12
if
(img.
data
.size() != expected)
HAIO_FAIL
(
InvalidInput
,
"dds payload does not match its size"
);
13
14
const
auto
pitch =
static_cast<
uint32_t
>
(size.
width
* (P ==
Color::RGB565
? 2 : 4));
15
const
auto
flags = P ==
Color::RGB565
? 0x40u : 0x41u;
16
const
auto
bits = P ==
Color::RGB565
? 16u : 32u;
17
const
auto
r = P ==
Color::RGB565
? 0xf800u : 0x000000ffu;
18
const
auto
g = P ==
Color::RGB565
? 0x07e0u : 0x0000ff00u;
19
const
auto
b = P ==
Color::RGB565
? 0x001fu : 0x00ff0000u;
20
const
auto
a = P ==
Color::RGB565
? 0u : 0xff000000u;
21
22
std::vector<uint8_t> out;
23
for
(
auto
v : {
Util::fourccLE
(
'D'
,
'D'
,
'S'
,
' '
), 124u, 0x100fu,
static_cast<
uint32_t
>
(size.
height
),
24
static_cast<
uint32_t
>
(size.
width
), pitch, 0u, 0u}) {
25
Util::appendU32LE
(out, v);
26
}
27
for
(
int
i = 0; i < 11; i++)
Util::appendU32LE
(out, 0);
28
for
(
auto
v : {32u, flags, 0u, bits, r, g, b, a, 0x1000u, 0u, 0u, 0u, 0u})
Util::appendU32LE
(out, v);
29
out.insert(out.end(), img.
data
.begin(), img.
data
.end());
30
31
return
Blob
{
Format::DDS
, P,
"image/vnd-ms.dds"
, {}, std::move(out)};
32
}
33
34
}
35
36
/**
37
* @addtogroup encode
38
* @{
39
*/
40
template
<>
41
Result<Blob>
Encode<Format::DDS, Color::RGB565>
(
Image<Color::RGB565>
img) {
42
return
encodeDds<Color::RGB565>(std::move(img));
43
}
44
/** @} */
45
46
/**
47
* @addtogroup encode
48
* @{
49
*/
50
template
<>
51
Result<Blob>
Encode<Format::DDS, Color::RGBA8888>
(
Image<Color::RGBA8888>
img) {
52
return
encodeDds<Color::RGBA8888>(std::move(img));
53
}
54
/** @} */
55
56
}
Haio::Codecs::Encode< Format::DDS, Color::RGBA8888 >
Result< Blob > Encode< Format::DDS, Color::RGBA8888 >(Image< Color::RGBA8888 > img)
Definition
encode.cpp:51
Haio::Codecs::Encode< Format::DDS, Color::RGB565 >
Result< Blob > Encode< Format::DDS, Color::RGB565 >(Image< Color::RGB565 > img)
Definition
encode.cpp:41
haio_codec.hpp
HAIO_TRY
#define HAIO_TRY(name, expr)
Definition
haio_codec.hpp:246
HAIO_FAIL
#define HAIO_FAIL(why, message)
Definition
haio_codec.hpp:258
haio_convert.hpp
haio_util.hpp
Haio::Codecs
Definition
haio_codec.hpp:136
Haio::Util::fourccLE
constexpr uint32_t fourccLE(char a, char b, char c, char d)
Definition
haio_util.hpp:29
Haio::Util::appendU32LE
void appendU32LE(std::vector< uint8_t > &out, uint32_t value)
Definition
bytes.cpp:9
Haio::Color::RGB565
@ RGB565
Definition
haio_formats.hpp:109
Haio::Result
std::expected< T, Error > Result
Definition
haio_codec.hpp:32
Haio::sizeOf
Result< size_t > sizeOf(Color color, Size size)
Definition
sizes.cpp:6
Haio::Size::height
int height
Definition
haio_object.hpp:14
Haio::Format::DDS
@ DDS
Definition
haio_formats.hpp:49
Haio::ErrorCode::InvalidInput
@ InvalidInput
Definition
haio_codec.hpp:18
Haio::Size::width
int width
Definition
haio_object.hpp:13
Haio::Blob
Definition
haio_codec.hpp:122
Haio::Size
Definition
haio_object.hpp:12
Haio::Image
Definition
haio_codec.hpp:36
Haio::Image::height
int height
Definition
haio_codec.hpp:39
Haio::Image::width
int width
Definition
haio_codec.hpp:38
Haio::Image::data
std::vector< uint8_t > data
Definition
haio_codec.hpp:40
library
backend
codecs
container
dds
encode.cpp
Generated by
1.18.0