texture/texels.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #include <array> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <cstdint> | ||
| 7 | #include <cstring> | ||
| 8 | #include <optional> | ||
| 9 | |||
| 10 | #include "chrono_batch_render/util/span.h" | ||
| 11 | |||
| 12 | namespace chrono::batch_render { | ||
| 13 | |||
| 14 | inline constexpr std::array<char, 8> kTexelMagic = {'C', 'B', 'R', 'T', 'E', 'X', 0, 1}; | ||
| 15 | |||
| 16 | struct TexelHeader { | ||
| 17 | std::array<char, 8> magic = kTexelMagic; | ||
| 18 | uint32_t width = 0; | ||
| 19 | uint32_t height = 0; | ||
| 20 | uint32_t channels = 0; | ||
| 21 | uint32_t floats = 0; | ||
| 22 | }; | ||
| 23 | |||
| 24 | static_assert(sizeof(TexelHeader) == 24); | ||
| 25 | |||
| 26 | 14 | inline std::optional<TexelHeader> ReadTexelHeader(Span<const std::byte> bytes) { | |
| 27 | 14 | TexelHeader header; | |
| 28 | 14 | if (bytes.size() < sizeof(header)) { | |
| 29 | ✗ | return std::nullopt; | |
| 30 | } | ||
| 31 | 14 | std::memcpy(&header, bytes.data(), sizeof(header)); | |
| 32 | 14 | if (header.magic != kTexelMagic) { | |
| 33 | 11 | return std::nullopt; | |
| 34 | } | ||
| 35 | 3 | return header; | |
| 36 | } | ||
| 37 | |||
| 38 | } | ||
| 39 |