From 60801e066fa0aa1e8fd3d0ffd66c18a0c7c282ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Tue, 18 Aug 2020 15:31:16 +0200 Subject: [PATCH] Improve JPEG file detection (mainly for background image use) --- UI/TextureUtil.cpp | 5 ++++- Windows/MainWindowMenu.cpp | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/UI/TextureUtil.cpp b/UI/TextureUtil.cpp index eaf2b82faf..7fac93cc5c 100644 --- a/UI/TextureUtil.cpp +++ b/UI/TextureUtil.cpp @@ -25,13 +25,16 @@ static Draw::DataFormat ZimToT3DFormat(int zim) { } static ImageFileType DetectImageFileType(const uint8_t *data, size_t size) { + if (size < 4) { + return TYPE_UNKNOWN; + } if (!memcmp(data, "ZIMG", 4)) { return ZIM; } else if (!memcmp(data, "\x89\x50\x4E\x47", 4)) { return PNG; } - else if (!memcmp(data, "\xff\xd8\xff\xe0", 4)) { + else if (!memcmp(data, "\xff\xd8\xff\xe0", 4) || !memcmp(data, "\xff\xd8\xff\xe1", 4)) { return JPEG; } else { diff --git a/Windows/MainWindowMenu.cpp b/Windows/MainWindowMenu.cpp index fcc1b2528d..858a4e919f 100644 --- a/Windows/MainWindowMenu.cpp +++ b/Windows/MainWindowMenu.cpp @@ -399,14 +399,14 @@ namespace MainWindow { } void BrowseBackground() { - static std::wstring filter = L"All supported images (*.jpg *.png)|*.jpg;*.png|All files (*.*)|*.*||"; + static std::wstring filter = L"All supported images (*.jpg *.jpeg *.png)|*.jpg;*.jpeg;*.png|All files (*.*)|*.*||"; for (size_t i = 0; i < filter.length(); i++) { if (filter[i] == '|') filter[i] = '\0'; } W32Util::MakeTopMost(GetHWND(), false); - browseImageDialog = new W32Util::AsyncBrowseDialog(W32Util::AsyncBrowseDialog::OPEN, GetHWND(), WM_USER_BROWSE_BG_DONE, L"LoadFile", L"", filter, L"*.jpg;*.png;"); + browseImageDialog = new W32Util::AsyncBrowseDialog(W32Util::AsyncBrowseDialog::OPEN, GetHWND(), WM_USER_BROWSE_BG_DONE, L"LoadFile", L"", filter, L"*.jpg;*.jpeg;*.png;"); } void BrowseBackgroundDone() { @@ -414,7 +414,7 @@ namespace MainWindow { if (browseImageDialog->GetResult(filename)) { std::wstring src = ConvertUTF8ToWString(filename); std::wstring dest; - if (filename.size() >= 4 && filename.substr(filename.size() - 4) == ".jpg") { + if (filename.size() >= 5 && (filename.substr(filename.size() - 4) == ".jpg" || filename.substr(filename.size() - 5) == ".jpeg")) { dest = ConvertUTF8ToWString(GetSysDirectory(DIRECTORY_SYSTEM) + "background.jpg"); } else { dest = ConvertUTF8ToWString(GetSysDirectory(DIRECTORY_SYSTEM) + "background.png");