Haio
0.2.X
Homebrew Asset IO/CDN
Toggle main menu visibility
Loading...
Searching...
No Matches
detect.cpp
Go to the documentation of this file.
1
#include <
haio_codec.hpp
>
2
#include <
haio_util.hpp
>
3
4
#include <
haio_util.hpp
>
5
6
namespace
{
7
8
/** dds says its colour through the pixel format mask rather than a single field */
9
Haio::Result<Haio::Color>
ddsColor(
Haio::Bytes
data) {
10
const
auto
bits =
Haio::Util::readU32LE
(data, 88);
11
const
auto
r =
Haio::Util::readU32LE
(data, 92);
12
const
auto
g =
Haio::Util::readU32LE
(data, 96);
13
const
auto
b =
Haio::Util::readU32LE
(data, 100);
14
const
auto
a =
Haio::Util::readU32LE
(data, 104);
15
if
(bits == 16 && r == 0xf800 && g == 0x07e0 && b == 0x001f && a == 0)
return
Haio::Color::RGB565
;
16
if
(bits == 32 && r == 0x000000ff && g == 0x0000ff00 && b == 0x00ff0000 && a == 0xff000000)
return
Haio::Color::RGBA8888
;
17
return
std::unexpected(
Haio::Error
{
Haio::ErrorCode::UnsupportedFormat
,
"unsupported dds pixel format"
});
18
}
19
20
/** the container is only half the answer: the header also says which colour it holds */
21
bool
isContainer(
Haio::Bytes
data) {
22
23
24
// "DDS " alone is a weak signal, so the header size that follows it is checked too
25
if
(!
Haio::Util::matches
(data,
"DDS "
) || data.size() < 8)
return
false
;
26
const
uint32_t headerSize =
static_cast<
uint32_t
>
(data[4]) | (
static_cast<
uint32_t
>
(data[5]) << 8)
27
| (
static_cast<
uint32_t
>
(data[6]) << 16) | (
static_cast<
uint32_t
>
(data[7]) << 24);
28
return
headerSize == 124;
29
}
30
31
}
32
33
namespace
Haio::Codecs
{
34
35
/**
36
* @addtogroup detect
37
* @{
38
*/
39
template
<>
40
bool
Detect<Format::DDS, Color::RGB565>
(
Bytes
data) {
41
if
(!isContainer(data))
return
false
;
42
const
auto
colour = ddsColor(data);
43
return
colour && *colour ==
Color::RGB565
;
44
}
45
/** @} */
46
47
/**
48
* @addtogroup detect
49
* @{
50
*/
51
template
<>
52
bool
Detect<Format::DDS, Color::RGBA8888>
(
Bytes
data) {
53
if
(!isContainer(data))
return
false
;
54
const
auto
colour = ddsColor(data);
55
return
colour && *colour ==
Color::RGBA8888
;
56
}
57
/** @} */
58
59
}
Haio::Codecs::Detect< Format::DDS, Color::RGB565 >
bool Detect< Format::DDS, Color::RGB565 >(Bytes data)
Definition
detect.cpp:40
Haio::Codecs::Detect< Format::DDS, Color::RGBA8888 >
bool Detect< Format::DDS, Color::RGBA8888 >(Bytes data)
Definition
detect.cpp:52
haio_codec.hpp
haio_util.hpp
Haio::Codecs
Definition
haio_codec.hpp:136
Haio::Util::readU32LE
uint32_t readU32LE(Bytes data, size_t off)
Definition
bytes.cpp:21
Haio::Util::matches
bool matches(Bytes data, Bytes magic)
Definition
matches.cpp:7
Haio::Color::RGBA8888
@ RGBA8888
Definition
haio_formats.hpp:107
Haio::Color::RGB565
@ RGB565
Definition
haio_formats.hpp:109
Haio::Result
std::expected< T, Error > Result
Definition
haio_codec.hpp:32
Haio::ErrorCode::UnsupportedFormat
@ UnsupportedFormat
Definition
haio_codec.hpp:19
Haio::Bytes
std::span< const uint8_t > Bytes
Definition
haio_codec.hpp:14
Haio::Error
Definition
haio_codec.hpp:26
library
backend
codecs
container
dds
detect.cpp
Generated by
1.18.0