Improve JPEG file detection (mainly for background image use)

This commit is contained in:
Henrik Rydgård 2020-08-18 15:31:16 +02:00
parent b4efca9480
commit 60801e066f
2 changed files with 7 additions and 4 deletions

View File

@ -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 {

View File

@ -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");