-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageFormatDetector.h
More file actions
77 lines (64 loc) · 1.94 KB
/
Copy pathImageFormatDetector.h
File metadata and controls
77 lines (64 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once
#include <cstdint>
#include <span>
#include <string>
#include <vector>
#include <filesystem>
#include <mutex>
#include <unordered_map>
namespace ImageCore
{
enum class ImageFormat
{
Unknown,
PNG,
JPEG,
BMP,
GIF,
TIFF,
DDS,
KTX2,
WEBP,
EXR,
HDR,
TGA,
ZIP,
SEVEN_Z,
};
struct ImageFormatInfo
{
ImageFormat format { ImageFormat::Unknown };
bool isContainer { false };
bool isGPUCompressed { false };
bool supportsStreaming { false };
};
struct FormatProbe
{
ImageFormatInfo info {};
std::wstring extensionLower {}; // Lowercase extension like ".dds" (empty string if none)
std::vector<uint8_t> header {}; // Up to kProbeSize bytes
};
class ImageFormatDetector final
{
public:
static constexpr size_t kProbeSize = 64;
// Reads header from path, detects by magic -> extension order
// (includes cache: based on last_write_time)
FormatProbe ProbeFile(const std::wstring& path);
static ImageFormatInfo DetectByMagic(std::span<const uint8_t> header);
static ImageFormatInfo DetectByExtension(const std::wstring& extensionLower);
// Shared helper: returns the lowercase extension of a path (e.g. L".dds"),
// or an empty string when the path has no extension.
static std::wstring GetLowerExtension(const std::wstring& path);
private:
struct CacheEntry
{
std::filesystem::file_time_type writeTime {};
FormatProbe probe {};
};
std::mutex m_mutex {};
std::unordered_map<std::wstring, CacheEntry> m_cache {};
static std::vector<uint8_t> ReadHeader(const std::wstring& path, size_t maxBytes);
static bool StartsWith(std::span<const uint8_t> header, std::span<const uint8_t> magic);
};
}