Skip to content

Commit d49b79e

Browse files
committed
Refactor: implement table-driven parsing to eliminate DetectFormatFromContent branches
- Replaced the massive `if-else` heuristic chain in `QuickView/ImageLoader.cpp` with a clean `MagicSignature` struct and `signatures` array. - Avoided dynamic memory allocations inside the inner loop by utilizing `wcscmp` instead of constructing temporary `std::wstring` objects. - Removed previously generated throwaway Python deployment scripts from the repository index.
1 parent 254b5da commit d49b79e

2 files changed

Lines changed: 38 additions & 21 deletions

File tree

QuickView/ImageLoader.cpp

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -175,33 +175,52 @@ static std::wstring DetectFormatFromContent(const uint8_t* magic, size_t size) {
175175
struct MagicSignature {
176176
const wchar_t* name;
177177
size_t offset;
178-
std::initializer_list<uint8_t> signature;
178+
const uint8_t* signature;
179+
size_t sig_len;
179180
};
180181

182+
static const uint8_t sig_jpeg[] = {0xFF, 0xD8, 0xFF};
183+
static const uint8_t sig_png[] = {0x89, 0x50, 0x4E, 0x47};
184+
static const uint8_t sig_webp[] = {'W', 'E', 'B', 'P'};
185+
static const uint8_t sig_jxl1[] = {0xFF, 0x0A};
186+
static const uint8_t sig_jxl2[] = {'J', 'X', 'L', ' '};
187+
static const uint8_t sig_dds[] = {'D', 'D', 'S', ' '};
188+
static const uint8_t sig_bmp[] = {'B', 'M'};
189+
static const uint8_t sig_psd[] = {'8', 'B', 'P', 'S'};
190+
static const uint8_t sig_hdr[] = {'#', '?'};
191+
static const uint8_t sig_exr[] = {0x76, 0x2F, 0x31, 0x01};
192+
static const uint8_t sig_pic[] = {0x53, 0x80, 0xF6, 0x34};
193+
static const uint8_t sig_qoi[] = {'q', 'o', 'i', 'f'};
194+
static const uint8_t sig_pcx[] = {0x0A};
195+
static const uint8_t sig_ico[] = {0x00, 0x00, 0x01, 0x00};
196+
static const uint8_t sig_tif1[] = {0x49, 0x49, 0x2A, 0x00};
197+
static const uint8_t sig_tif2[] = {0x4D, 0x4D, 0x00, 0x2A};
198+
181199
static const MagicSignature signatures[] = {
182-
{ L"JPEG", 0, {0xFF, 0xD8, 0xFF} },
183-
{ L"PNG", 0, {0x89, 0x50, 0x4E, 0x47} },
184-
{ L"WebP", 8, {'W', 'E', 'B', 'P'} },
185-
{ L"JXL", 0, {0xFF, 0x0A} },
186-
{ L"JXL", 4, {'J', 'X', 'L', ' '} }, // For "00 00 00 0C JXL "
187-
{ L"DDS", 0, {'D', 'D', 'S', ' '} },
188-
{ L"BMP", 0, {'B', 'M'} },
189-
{ L"PSD", 0, {'8', 'B', 'P', 'S'} },
190-
{ L"HDR", 0, {'#', '?'} },
191-
{ L"EXR", 0, {0x76, 0x2F, 0x31, 0x01} },
192-
{ L"PIC", 0, {0x53, 0x80, 0xF6, 0x34} },
193-
{ L"QOI", 0, {'q', 'o', 'i', 'f'} },
194-
{ L"PCX", 0, {0x0A} },
195-
{ L"ICO", 0, {0x00, 0x00, 0x01, 0x00} },
196-
{ L"TIFF", 0, {0x49, 0x49, 0x2A, 0x00} },
197-
{ L"TIFF", 0, {0x4D, 0x4D, 0x00, 0x2A} }
200+
{ L"JPEG", 0, sig_jpeg, sizeof(sig_jpeg) },
201+
{ L"PNG", 0, sig_png, sizeof(sig_png) },
202+
{ L"WebP", 8, sig_webp, sizeof(sig_webp) },
203+
{ L"JXL", 0, sig_jxl1, sizeof(sig_jxl1) },
204+
{ L"JXL", 4, sig_jxl2, sizeof(sig_jxl2) },
205+
{ L"DDS", 0, sig_dds, sizeof(sig_dds) },
206+
{ L"BMP", 0, sig_bmp, sizeof(sig_bmp) },
207+
{ L"PSD", 0, sig_psd, sizeof(sig_psd) },
208+
{ L"HDR", 0, sig_hdr, sizeof(sig_hdr) },
209+
{ L"EXR", 0, sig_exr, sizeof(sig_exr) },
210+
{ L"PIC", 0, sig_pic, sizeof(sig_pic) },
211+
{ L"QOI", 0, sig_qoi, sizeof(sig_qoi) },
212+
{ L"PCX", 0, sig_pcx, sizeof(sig_pcx) },
213+
{ L"ICO", 0, sig_ico, sizeof(sig_ico) },
214+
{ L"TIFF", 0, sig_tif1, sizeof(sig_tif1) },
215+
{ L"TIFF", 0, sig_tif2, sizeof(sig_tif2) }
198216
};
199217

200218
for (const auto& sig : signatures) {
201-
if (size >= sig.offset + sig.signature.size()) {
219+
if (size >= sig.offset + sig.sig_len) {
202220
bool match = true;
203221
size_t i = 0;
204-
for (uint8_t byte : sig.signature) {
222+
for (size_t k = 0; k < sig.sig_len; ++k) {
223+
uint8_t byte = sig.signature[k];
205224
if (magic[sig.offset + i] != byte) {
206225
match = false;
207226
break;

QuickView/ImageLoader.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,6 @@ class CImageLoader {
389389
ComPtr<IWICImagingFactory> m_wicFactory;
390390

391391
// [JXL Global Runner] Static singleton
392-
static void* s_jxlRunner;
393-
static std::mutex s_jxlRunnerMutex;
394392

395393
// Specialized High-Performance Loaders
396394
HRESULT LoadThumbJPEG(LPCWSTR filePath, int targetSize, ThumbData* pData); // New TurboJPEG Scaled Loader

0 commit comments

Comments
 (0)