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
namespace
{
5
6
constexpr
std::array<uint8_t, 8> signature = {0x89,
'P'
,
'N'
,
'G'
,
'\r'
,
'\n'
, 0x1a,
'\n'
};
7
8
/** IHDR is the first chunk and always 13 bytes, so the colour type sits at a fixed spot */
9
constexpr
size_t
colourTypeOffset = 25;
10
constexpr
uint8_t colourGray = 0;
11
constexpr
uint8_t colourRgb = 2;
12
constexpr
uint8_t colourRgba = 6;
13
14
bool
headerSays(
Haio::Bytes
data, uint8_t colourType) {
15
return
Haio::Util::matches
(data, signature)
16
&&
Haio::Util::hasBytes
(data, colourTypeOffset, 1)
17
&&
Haio::Util::matches
(data.subspan(12, 4),
"IHDR"
)
18
&& data[colourTypeOffset] == colourType;
19
}
20
21
}
22
23
namespace
Haio::Codecs
{
24
25
/** what a png holds when nobody asks for something else */
26
template
<>
struct
DefaultColor
<
Format
::
PNG
> {
static
constexpr
Color
value
=
Color::RGBA8888
; };
27
28
/**
29
* @addtogroup detect
30
* @{
31
*/
32
template
<>
33
bool
Detect<Format::PNG, Color::RGBA8888>
(
Bytes
data) {
34
return
headerSays(data, colourRgba);
35
}
36
/** @} */
37
38
/**
39
* @addtogroup detect
40
* @{
41
*/
42
template
<>
43
bool
Detect<Format::PNG, Color::RGB888>
(
Bytes
data) {
44
return
headerSays(data, colourRgb);
45
}
46
/** @} */
47
48
/**
49
* @addtogroup detect
50
* @{
51
*/
52
/**
53
* recognised on purpose with no decoder behind it: the error names the format.
54
*
55
* @todo palette (colour type 3) and gray+alpha (type 4) are not recognised at all,
56
* because neither has a Color to name it. before this refactor wuffs expanded both
57
* to rgba and they simply worked, so this is a regression until Color grows a
58
* PALETTE8 and a GRAY_ALPHA -- and palette drags a colour table along with it.
59
*/
60
template
<>
61
bool
Detect<Format::PNG, Color::GRAY8>
(
Bytes
data) {
62
return
headerSays(data, colourGray);
63
}
64
/** @} */
65
66
}
Haio::Codecs::Detect< Format::PNG, Color::GRAY8 >
bool Detect< Format::PNG, Color::GRAY8 >(Bytes data)
Definition
detect.cpp:61
Haio::Codecs::Detect< Format::PNG, Color::RGB888 >
bool Detect< Format::PNG, Color::RGB888 >(Bytes data)
Definition
detect.cpp:43
Haio::Codecs::Detect< Format::PNG, Color::RGBA8888 >
bool Detect< Format::PNG, Color::RGBA8888 >(Bytes data)
Definition
detect.cpp:33
haio_codec.hpp
haio_util.hpp
Haio::Codecs
Definition
haio_codec.hpp:136
Haio::Codecs::DefaultColor
Definition
haio_codec.hpp:143
Haio::Util::hasBytes
bool hasBytes(Bytes data, size_t off, size_t len)
Definition
bytes.cpp:5
Haio::Util::matches
bool matches(Bytes data, Bytes magic)
Definition
matches.cpp:7
Haio::Color
Color
Definition
haio_formats.hpp:106
Haio::Color::RGBA8888
@ RGBA8888
Definition
haio_formats.hpp:107
Haio::Format
Format
Definition
haio_formats.hpp:17
Haio::Format::PNG
@ PNG
Definition
haio_formats.hpp:24
Haio::Bytes
std::span< const uint8_t > Bytes
Definition
haio_codec.hpp:14
Haio::Codecs::DefaultColor< Format::PNG >::value
static constexpr Color value
Definition
detect.cpp:26
library
backend
codecs
container
png
detect.cpp
Generated by
1.18.0