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
4namespace {
5
6constexpr 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 */
9constexpr size_t colourTypeOffset = 25;
10constexpr uint8_t colourGray = 0;
11constexpr uint8_t colourRgb = 2;
12constexpr uint8_t colourRgba = 6;
13
14bool 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
23namespace Haio::Codecs {
24
25/** what a png holds when nobody asks for something else */
26template <> struct DefaultColor<Format::PNG> { static constexpr Color value = Color::RGBA8888; };
27
28/**
29 * @addtogroup detect
30 * @{
31 */
32template <>
34 return headerSays(data, colourRgba);
35}
36/** @} */
37
38/**
39 * @addtogroup detect
40 * @{
41 */
42template <>
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 */
60template <>
62 return headerSays(data, colourGray);
63}
64/** @} */
65
66}
bool Detect< Format::PNG, Color::GRAY8 >(Bytes data)
Definition detect.cpp:61
bool Detect< Format::PNG, Color::RGB888 >(Bytes data)
Definition detect.cpp:43
bool Detect< Format::PNG, Color::RGBA8888 >(Bytes data)
Definition detect.cpp:33
bool hasBytes(Bytes data, size_t off, size_t len)
Definition bytes.cpp:5
bool matches(Bytes data, Bytes magic)
Definition matches.cpp:7
std::span< const uint8_t > Bytes