From 5a8d317ad3b96906946f4c6395dd48e855b29ae0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 3 Jun 2022 16:17:32 +0200 Subject: [PATCH] AnyImage{Importer,Converter}: detect also KTX1. Even though KtxImporter / KtxImageConverter doesn't support these (and probably never will), the rationale here is to provide a somewhat better message than "unable to detect file format" when trying to open a *.ktx file, or when trying to save to a *.ktx by accident, instead of *.ktx2 (I do that quite often). The concrete plugins are able to provide a much better error message about version 1 not supported. --- .../AnyImageConverter/AnyImageConverter.cpp | 36 ++++++++++++------ .../AnyImageConverter/AnyImageConverter.h | 6 +-- .../Test/AnyImageConverterTest.cpp | 12 ++++++ .../AnyImageImporter/AnyImageImporter.cpp | 9 +++-- .../AnyImageImporter/AnyImageImporter.h | 4 +- .../Test/AnyImageImporterTest.cpp | 2 + .../AnyImageImporter/Test/CMakeLists.txt | 2 + .../AnyImageImporter/Test/version1.ktx | Bin 0 -> 132 bytes 8 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 src/MagnumPlugins/AnyImageImporter/Test/version1.ktx diff --git a/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.cpp b/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.cpp index 1a1ba784e9..91e468a623 100644 --- a/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.cpp +++ b/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.cpp @@ -73,7 +73,8 @@ bool AnyImageConverter::doConvertToFile(const ImageView1D& image, const Containe /* Detect the plugin from extension */ Containers::StringView plugin; - if(normalizedExtension == ".ktx2"_s) + if(normalizedExtension == ".ktx"_s || + normalizedExtension == ".ktx2"_s) plugin = "KtxImageConverter"_s; else { Error{} << "Trade::AnyImageConverter::convertToFile(): cannot determine the format of" << filename << "for a 1D image"; @@ -129,7 +130,8 @@ bool AnyImageConverter::doConvertToFile(const ImageView2D& image, const Containe normalizedExtension == ".jpeg"_s || normalizedExtension == ".jpe"_s) plugin = "JpegImageConverter"_s; - else if(normalizedExtension == ".ktx2"_s) + else if(normalizedExtension == ".ktx"_s || + normalizedExtension == ".ktx2"_s) plugin = "KtxImageConverter"_s; else if(normalizedExtension == ".png"_s) plugin = "PngImageConverter"_s; @@ -184,7 +186,8 @@ bool AnyImageConverter::doConvertToFile(const ImageView3D& image, const Containe plugin = "BasisImageConverter"_s; else if(normalizedExtension == ".exr"_s) plugin = "OpenExrImageConverter"_s; - else if(normalizedExtension == ".ktx2"_s) + else if(normalizedExtension == ".ktx"_s || + normalizedExtension == ".ktx2"_s) plugin = "KtxImageConverter"_s; else if(normalizedExtension == ".vdb"_s) plugin = "OpenVdbImageConverter"_s; @@ -230,7 +233,8 @@ bool AnyImageConverter::doConvertToFile(const CompressedImageView1D& image, cons /* Detect the plugin from extension */ Containers::StringView plugin; - if(normalizedExtension == ".ktx2"_s) + if(normalizedExtension == ".ktx"_s || + normalizedExtension == ".ktx2"_s) plugin = "KtxImageConverter"_s; else { Error{} << "Trade::AnyImageConverter::convertToFile(): cannot determine the format of" << filename << "for a compressed 1D image"; @@ -274,7 +278,8 @@ bool AnyImageConverter::doConvertToFile(const CompressedImageView2D& image, cons /* Detect the plugin from extension */ Containers::StringView plugin; - if(normalizedExtension == ".ktx2"_s) + if(normalizedExtension == ".ktx"_s || + normalizedExtension == ".ktx2"_s) plugin = "KtxImageConverter"_s; else { Error{} << "Trade::AnyImageConverter::convertToFile(): cannot determine the format of" << filename << "for a compressed 2D image"; @@ -318,7 +323,8 @@ bool AnyImageConverter::doConvertToFile(const CompressedImageView3D& image, cons /* Detect the plugin from extension */ Containers::StringView plugin; - if(normalizedExtension == ".ktx2"_s) + if(normalizedExtension == ".ktx"_s || + normalizedExtension == ".ktx2"_s) plugin = "KtxImageConverter"_s; else { Error{} << "Trade::AnyImageConverter::convertToFile(): cannot determine the format of" << filename << "for a compressed 3D image"; @@ -362,7 +368,8 @@ bool AnyImageConverter::doConvertToFile(const Containers::ArrayView&& data, DataFlags) { /* https://en.wikipedia.org/wiki/JPEG#Syntax_and_structure */ else if(dataString.hasPrefix("\xff\xd8\xff"_s)) plugin = "JpegImporter"_s; - /* https://github.khronos.org/KTX-Specification/#_identifier */ - else if(dataString.hasPrefix("\xabKTX 20\xbb\r\n\x1a\n"_s)) + /* https://github.khronos.org/KTX-Specification/#_identifier and + https://www.khronos.org/registry/KTX/specs/1.0/ktxspec_v1.html */ + else if(dataString.hasPrefix("\xabKTX 20\xbb\r\n\x1a\n"_s) || + dataString.hasPrefix("\xabKTX 11\xbb\r\n\x1a\n"_s)) plugin = "KtxImporter"_s; /* https://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header */ else if(dataString.hasPrefix("\x89PNG\x0d\x0a\x1a\x0a"_s)) diff --git a/src/MagnumPlugins/AnyImageImporter/AnyImageImporter.h b/src/MagnumPlugins/AnyImageImporter/AnyImageImporter.h index 1878e6eb1e..9e1406142c 100644 --- a/src/MagnumPlugins/AnyImageImporter/AnyImageImporter.h +++ b/src/MagnumPlugins/AnyImageImporter/AnyImageImporter.h @@ -78,8 +78,8 @@ Supported formats: loaded with @ref JpegImporter or any other plugin that provides it - JPEG 2000 (`*.jp2`), loaded with any plugin that provides `Jpeg2000Importer` -- KTX2 (`*.ktx2` or data with corresponding signature), loaded with - @ref KtxImporter or any other plugin that provides it +- KTX and KTX2 (`*.ktx`, `*.ktx2` or data with corresponding signature), + loaded with @ref KtxImporter or any other plugin that provides it - Multiple-image Network Graphics (`*.mng`), loaded with any plugin that provides `MngImporter` - Portable Bitmap (`*.pbm`), loaded with any plugin that provides `PbmImporter` diff --git a/src/MagnumPlugins/AnyImageImporter/Test/AnyImageImporterTest.cpp b/src/MagnumPlugins/AnyImageImporter/Test/AnyImageImporterTest.cpp index 93e8a73e39..047ea057ab 100644 --- a/src/MagnumPlugins/AnyImageImporter/Test/AnyImageImporterTest.cpp +++ b/src/MagnumPlugins/AnyImageImporter/Test/AnyImageImporterTest.cpp @@ -110,6 +110,8 @@ constexpr struct { {"JPEG data", "gray.jpg", true, "JpegImporter"}, {"JPEG uppercase", "uppercase.JPG", false, "JpegImporter"}, {"JPEG2000", "image.jp2", false, "Jpeg2000Importer"}, + {"KTX1", "version1.ktx", false, "KtxImporter"}, + {"KTX1 data", "version1.ktx", true, "KtxImporter"}, {"HDR", "rgb.hdr", false, "HdrImporter"}, {"HDR data", "rgb.hdr", true, "HdrImporter"}, {"ICO", "pngs.ico", false, "IcoImporter"}, diff --git a/src/MagnumPlugins/AnyImageImporter/Test/CMakeLists.txt b/src/MagnumPlugins/AnyImageImporter/Test/CMakeLists.txt index 9638be4461..b77a3f438e 100644 --- a/src/MagnumPlugins/AnyImageImporter/Test/CMakeLists.txt +++ b/src/MagnumPlugins/AnyImageImporter/Test/CMakeLists.txt @@ -68,6 +68,8 @@ corrade_add_test(AnyImageImporterTest AnyImageImporterTest.cpp # Generated by AnyImageConverterTest::convert{1D,3D}() 1d.ktx2 3d.ktx2 + # From KtxImporter test data (in magnum-plugins) + version1.ktx gray.jpg image.exr image.tiff diff --git a/src/MagnumPlugins/AnyImageImporter/Test/version1.ktx b/src/MagnumPlugins/AnyImageImporter/Test/version1.ktx new file mode 100644 index 0000000000000000000000000000000000000000..d8ce8e4de20c94a2296d2531fe917feb5c8a2930 GIT binary patch literal 132 zcmZ4O9TK5nXt