Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AnyImage{Importer,Converter}: detect also KTX1 #567

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions src/MagnumPlugins/AnyImageConverter/AnyImageConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -362,7 +368,8 @@ bool AnyImageConverter::doConvertToFile(const Containers::ArrayView<const ImageV

/* 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 multi-level 1D image";
Expand Down Expand Up @@ -410,7 +417,8 @@ bool AnyImageConverter::doConvertToFile(const Containers::ArrayView<const ImageV
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 {
Error{} << "Trade::AnyImageConverter::convertToFile(): cannot determine the format of" << filename << "for a multi-level 2D image";
Expand Down Expand Up @@ -458,7 +466,8 @@ bool AnyImageConverter::doConvertToFile(const Containers::ArrayView<const ImageV
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 {
Error{} << "Trade::AnyImageConverter::convertToFile(): cannot determine the format of" << filename << "for a multi-level 3D image";
Expand Down Expand Up @@ -502,7 +511,8 @@ bool AnyImageConverter::doConvertToFile(const Containers::ArrayView<const Compre

/* 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 multi-level compressed 1D image";
Expand Down Expand Up @@ -546,7 +556,8 @@ bool AnyImageConverter::doConvertToFile(const Containers::ArrayView<const Compre

/* 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 multi-level compressed 2D image";
Expand Down Expand Up @@ -590,7 +601,8 @@ bool AnyImageConverter::doConvertToFile(const Containers::ArrayView<const Compre

/* 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 multi-level compressed 3D image";
Expand Down
6 changes: 3 additions & 3 deletions src/MagnumPlugins/AnyImageConverter/AnyImageConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ tries to convert the file with it. Supported formats for uncompressed data:
`HdrImageConverter`. Only uncompressed 2D images.
- JPEG (`*.jpg`, `*.jpe`, `*.jpeg`), converted with @ref JpegImageConverter
or any other plugin that provides it. Only uncompressed 2D images.
- KTX2 (`*.ktx2`), converted with @ref KtxImageConverter or any other plugin
that provides it. Uncompressed, compressed, 1D/2D/3D and multi-level
1D/2D/3D images.
- KTX1 and KTX2 (`*.ktx`, `*.ktx2`), converted with @ref KtxImageConverter or
any other plugin that provides it. Uncompressed, compressed, 1D/2D/3D and
multi-level 1D/2D/3D images.
- Portable Network Graphics (`*.png`), converted with @ref PngImageConverter
or any other plugin that provides it. Only uncompressed 2D images.
- Truevision TGA (`*.tga`, `*.vda`, `*.icb`, `*.vst`), converted with
Expand Down
12 changes: 12 additions & 0 deletions src/MagnumPlugins/AnyImageConverter/Test/AnyImageConverterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ constexpr struct {
const char* filename;
const char* plugin;
} Detect1DData[]{
{"KTX1", "file.ktx", "KtxImageConverter"},
{"KTX2", "file.ktx2", "KtxImageConverter"},
/* Have at least one test case with uppercase */
{"KTX2 uppercase", "FIL~1.KTX2", "KtxImageConverter"}
Expand All @@ -159,6 +160,7 @@ constexpr struct {
{"JPEG weird extension", "file.jpe", "JpegImageConverter"},
/* Have at least one test case with uppercase */
{"JPEG uppercase", "output.JPG", "JpegImageConverter"},
{"KTX1", "file.ktx", "KtxImageConverter"},
{"KTX2", "foo.ktx2", "KtxImageConverter"},
{"PNG", "file.png", "PngImageConverter"}
};
Expand All @@ -170,6 +172,7 @@ constexpr struct {
} Detect3DData[]{
{"Basis Universal", "file.basis", "BasisImageConverter"},
{"EXR", "file.exr", "OpenExrImageConverter"},
{"KTX1", "file.ktx", "KtxImageConverter"},
{"KTX2", "file.ktx2", "KtxImageConverter"},
{"OpenVDB", "volume.vdb", "OpenVdbImageConverter"},
/* Have at least one test case with uppercase */
Expand All @@ -181,6 +184,7 @@ constexpr struct {
const char* filename;
const char* plugin;
} DetectLevels1DData[]{
{"KTX1", "file.ktx", "KtxImageConverter"},
{"KTX2", "file.ktx2", "KtxImageConverter"},
/* Have at least one test case with uppercase */
{"KTX2 uppercase", "FIL~1.KTX2", "KtxImageConverter"}
Expand All @@ -193,6 +197,7 @@ constexpr struct {
} DetectLevels2DData[]{
{"Basis Universal", "file.basis", "BasisImageConverter"},
{"EXR", "file.exr", "OpenExrImageConverter"},
{"KTX1", "file.ktx", "KtxImageConverter"},
{"KTX2", "file.ktx2", "KtxImageConverter"},
/* Have at least one test case with uppercase */
{"EXR uppercase", "FIL~1.EXR", "OpenExrImageConverter"}
Expand All @@ -205,6 +210,7 @@ constexpr struct {
} DetectLevels3DData[]{
{"Basis Universal", "file.basis", "BasisImageConverter"},
{"EXR", "file.exr", "OpenExrImageConverter"},
{"KTX1", "file.ktx", "KtxImageConverter"},
{"KTX2", "file.ktx2", "KtxImageConverter"},
/* Have at least one test case with uppercase */
{"EXR uppercase", "FIL~1.EXR", "OpenExrImageConverter"}
Expand All @@ -218,6 +224,7 @@ constexpr struct {
const char* filename;
const char* plugin;
} DetectCompressed1DData[]{
{"KTX1", "file.ktx", "KtxImageConverter"},
{"KTX2", "file.ktx2", "KtxImageConverter"},
/* Have at least one test case with uppercase */
{"KTX2 uppercase", "FIL~1.KTX2", "KtxImageConverter"}
Expand All @@ -228,6 +235,7 @@ constexpr struct {
const char* filename;
const char* plugin;
} DetectCompressed2DData[]{
{"KTX1", "file.ktx", "KtxImageConverter"},
{"KTX2", "file.ktx2", "KtxImageConverter"},
/* Have at least one test case with uppercase */
{"KTX2 uppercase", "FIL~1.KTX2", "KtxImageConverter"}
Expand All @@ -238,6 +246,7 @@ constexpr struct {
const char* filename;
const char* plugin;
} DetectCompressed3DData[]{
{"KTX1", "file.ktx", "KtxImageConverter"},
{"KTX2", "file.ktx2", "KtxImageConverter"},
/* Have at least one test case with uppercase */
{"KTX2 uppercase", "FIL~1.KTX2", "KtxImageConverter"}
Expand All @@ -248,6 +257,7 @@ constexpr struct {
const char* filename;
const char* plugin;
} DetectCompressedLevels1DData[]{
{"KTX1", "file.ktx", "KtxImageConverter"},
{"KTX2", "file.ktx2", "KtxImageConverter"},
/* Have at least one test case with uppercase */
{"KTX2 uppercase", "FIL~1.KTX2", "KtxImageConverter"}
Expand All @@ -258,6 +268,7 @@ constexpr struct {
const char* filename;
const char* plugin;
} DetectCompressedLevels2DData[]{
{"KTX1", "file.ktx", "KtxImageConverter"},
{"KTX2", "file.ktx2", "KtxImageConverter"},
/* Have at least one test case with uppercase */
{"KTX2 uppercase", "FIL~1.KTX2", "KtxImageConverter"}
Expand All @@ -268,6 +279,7 @@ constexpr struct {
const char* filename;
const char* plugin;
} DetectCompressedLevels3DData[]{
{"KTX1", "file.ktx", "KtxImageConverter"},
{"KTX2", "file.ktx2", "KtxImageConverter"},
/* Have at least one test case with uppercase */
{"KTX2 uppercase", "FIL~1.KTX2", "KtxImageConverter"}
Expand Down
9 changes: 6 additions & 3 deletions src/MagnumPlugins/AnyImageImporter/AnyImageImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ void AnyImageImporter::doOpenFile(const Containers::StringView filename) {
plugin = "JpegImporter"_s;
else if(normalizedExtension == ".jp2"_s)
plugin = "Jpeg2000Importer"_s;
else if(normalizedExtension == ".ktx2"_s)
else if(normalizedExtension == ".ktx"_s ||
normalizedExtension == ".ktx2"_s)
plugin = "KtxImporter"_s;
else if(normalizedExtension == ".mng"_s)
plugin = "MngImporter"_s;
Expand Down Expand Up @@ -199,8 +200,10 @@ void AnyImageImporter::doOpenData(Containers::Array<char>&& 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))
Expand Down
4 changes: 2 additions & 2 deletions src/MagnumPlugins/AnyImageImporter/AnyImageImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
2 changes: 2 additions & 0 deletions src/MagnumPlugins/AnyImageImporter/Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file added src/MagnumPlugins/AnyImageImporter/Test/version1.ktx
Binary file not shown.