Haio 0.2.X
Homebrew Asset IO/CDN
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
6namespace {
7
8/** dds says its colour through the pixel format mask rather than a single field */
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 */
21bool 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
33namespace Haio::Codecs {
34
35/**
36 * @addtogroup detect
37 * @{
38 */
39template <>
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 */
51template <>
53 if (!isContainer(data)) return false;
54 const auto colour = ddsColor(data);
55 return colour && *colour == Color::RGBA8888;
56}
57/** @} */
58
59}
bool Detect< Format::DDS, Color::RGB565 >(Bytes data)
Definition detect.cpp:40
bool Detect< Format::DDS, Color::RGBA8888 >(Bytes data)
Definition detect.cpp:52
uint32_t readU32LE(Bytes data, size_t off)
Definition bytes.cpp:21
bool matches(Bytes data, Bytes magic)
Definition matches.cpp:7
std::expected< T, Error > Result
std::span< const uint8_t > Bytes