/*
This file is part of Magnum .
Copyright © 2010 , 2011 , 2012 , 2013 , 2014 , 2015 , 2016 , 2017 , 2018 , 2019 ,
2020 , 2021 , 2022 Vladimír Vondruš < mosra @ centrum . cz >
Permission is hereby granted , free of charge , to any person obtaining a
copy of this software and associated documentation files ( the " Software " ) ,
to deal in the Software without restriction , including without limitation
the rights to use , copy , modify , merge , publish , distribute , sublicense ,
and / or sell copies of the Software , and to permit persons to whom the
Software is furnished to do so , subject to the following conditions :
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software .
THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING
FROM , OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE .
*/
# include "TgaImporter.h"
# include <Corrade/Containers/ArrayView.h>
# include <Corrade/Containers/Optional.h>
# include <Corrade/Utility/Algorithms.h>
# include <Corrade/Utility/Endianness.h>
Pixel storage support, part 2: renamed ColorFormat to PixelFormat etc.
Yeah, sorry, I know, the enums are renamed for second or third time in a
row, first they were Image::Format, then ImageFormat, then ColorFormat
and now PixelFormat. But this time it's final and last time they are
renamed and now everything is finally consistent:
* ColorFormat::DepthComponent -- depth is not a color, thus
PixelFormat::DepthComponent makes a lot more sense.
* There will be PixelStorage classes, which will be stored in images
alonside PixelFormat/PixelType enums, making everything nicely
aligned.
* The GL documentation about glTexImage2D() etc. denotes the <format>
and <type> parameters as format and type of *pixel* data, so now we
are _finally_ consistent with the official naming.
I wonder why did I not choose PixelFormat originally. Anyway, the old
<Magnum/ColorFormat.h> header, ColorFormat, ColorType and
CompressedColorFormat types are now aliases to the new ones, are marked
as deprecated and will be removed in some future release (as always, I'm
waiting at least six months before removing the deprecated
functionality).
11 years ago
# include "Magnum/PixelFormat.h"
# include "Magnum/Math/Swizzle.h"
# include "Magnum/Math/Vector4.h"
# include "Magnum/Trade/ImageData.h"
# include "MagnumPlugins/TgaImporter/TgaHeader.h"
namespace Magnum { namespace Trade {
TgaImporter : : TgaImporter ( ) = default ;
TgaImporter : : TgaImporter ( PluginManager : : AbstractManager & manager , const Containers : : StringView & plugin ) : AbstractImporter { manager , plugin } { }
TgaImporter : : ~ TgaImporter ( ) = default ;
ImporterFeatures TgaImporter : : doFeatures ( ) const { return ImporterFeature : : OpenData ; }
bool TgaImporter : : doIsOpened ( ) const { return _in ; }
void TgaImporter : : doClose ( ) { _in = nullptr ; }
void TgaImporter : : doOpenData ( Containers : : Array < char > & & data , const DataFlags dataFlags ) {
/* Because here we're copying the data and using the _in to check if file
is opened , having them nullptr would mean openData ( ) would fail without
any error message . It ' s not possible to do this check on the importer
side , because empty file is valid in some formats ( OBJ or glTF ) . We also
can ' t do the full import here because then doImage2D ( ) would need to
copy the imported data instead anyway . This way it ' ll also work nicely
with a future openMemory ( ) . */
if ( data . isEmpty ( ) ) {
Error { } < < " Trade::TgaImporter::openData(): the file is empty " ;
return ;
}
/* Ttake over the existing array or copy the data if we can't */
if ( dataFlags & ( DataFlag : : Owned | DataFlag : : ExternallyOwned ) ) {
_in = std : : move ( data ) ;
} else {
_in = Containers : : Array < char > { NoInit , data . size ( ) } ;
Utility : : copy ( data , _in ) ;
}
}
UnsignedInt TgaImporter : : doImage2DCount ( ) const { return 1 ; }
Containers : : Optional < ImageData2D > TgaImporter : : doImage2D ( UnsignedInt , UnsignedInt ) {
/* Check if the file is long enough */
if ( _in . size ( ) < sizeof ( Implementation : : TgaHeader ) ) {
Error { } < < " Trade::TgaImporter::image2D(): file too short, expected at least " < < sizeof ( Implementation : : TgaHeader ) < < " bytes but got " < < _in . size ( ) ;
Converting APIs returning std::optional to Containers::Optional.
And removing the bundled std::optional implementation. This finally
makes this library compatible with C++17. Since this would be a huge
backwards-incompatible change that would make everyone angry, the
following had to be done in case both CORRADE_BUILD_DEPRECATED and
MAGNUM_BUILD_DEPRECATED is defined:
* Under C++11 and C++14, Containers::Optional / Containers::NullOpt is
aliased to std::optional / std::nullopt. This is no worse than the
state before, when we also provided these symbols.
* Under C++17, where standard <optional> header is available,
Containers::Optional provides implicit conversion to it. Only one-way
conversion is supported, as there was fortunately no Magnum API that
took std::optional via parameter, and there might be some corner
cases that this doesn't cover. The goal is to have all examples
compiling with the old API, at least.
* There's a new test especially for this, which checks that both the
C++11 and C++17 ways of doing things work as they should.
The typedef and conversion is marked as deprecated, so it will spit out
many warnings to push users to upgrade. I hope I can completely remove
this mess soon :/
8 years ago
return Containers : : NullOpt ;
}
const Implementation : : TgaHeader & header = * reinterpret_cast < const Implementation : : TgaHeader * > ( _in . data ( ) ) ;
/* Size in machine endian */
const Vector2i size { Utility : : Endianness : : littleEndian ( header . width ) ,
Utility : : Endianness : : littleEndian ( header . height ) } ;
/* Image format */
Pixel storage support, part 2: renamed ColorFormat to PixelFormat etc.
Yeah, sorry, I know, the enums are renamed for second or third time in a
row, first they were Image::Format, then ImageFormat, then ColorFormat
and now PixelFormat. But this time it's final and last time they are
renamed and now everything is finally consistent:
* ColorFormat::DepthComponent -- depth is not a color, thus
PixelFormat::DepthComponent makes a lot more sense.
* There will be PixelStorage classes, which will be stored in images
alonside PixelFormat/PixelType enums, making everything nicely
aligned.
* The GL documentation about glTexImage2D() etc. denotes the <format>
and <type> parameters as format and type of *pixel* data, so now we
are _finally_ consistent with the official naming.
I wonder why did I not choose PixelFormat originally. Anyway, the old
<Magnum/ColorFormat.h> header, ColorFormat, ColorType and
CompressedColorFormat types are now aliases to the new ones, are marked
as deprecated and will be removed in some future release (as always, I'm
waiting at least six months before removing the deprecated
functionality).
11 years ago
PixelFormat format ;
if ( header . colorMapType ! = 0 ) {
Error ( ) < < " Trade::TgaImporter::image2D(): paletted files are not supported " ;
Converting APIs returning std::optional to Containers::Optional.
And removing the bundled std::optional implementation. This finally
makes this library compatible with C++17. Since this would be a huge
backwards-incompatible change that would make everyone angry, the
following had to be done in case both CORRADE_BUILD_DEPRECATED and
MAGNUM_BUILD_DEPRECATED is defined:
* Under C++11 and C++14, Containers::Optional / Containers::NullOpt is
aliased to std::optional / std::nullopt. This is no worse than the
state before, when we also provided these symbols.
* Under C++17, where standard <optional> header is available,
Containers::Optional provides implicit conversion to it. Only one-way
conversion is supported, as there was fortunately no Magnum API that
took std::optional via parameter, and there might be some corner
cases that this doesn't cover. The goal is to have all examples
compiling with the old API, at least.
* There's a new test especially for this, which checks that both the
C++11 and C++17 ways of doing things work as they should.
The typedef and conversion is marked as deprecated, so it will spit out
many warnings to push users to upgrade. I hope I can completely remove
this mess soon :/
8 years ago
return Containers : : NullOpt ;
}
/* Color */
bool rle = false ;
if ( header . imageType = = 2 | | header . imageType = = 10 ) {
/* Reference: http://www.paulbourke.net/dataformats/tga/ */
rle = header . imageType = = 10 ;
switch ( header . bpp ) {
case 24 :
format = PixelFormat : : RGB8Unorm ;
break ;
case 32 :
format = PixelFormat : : RGBA8Unorm ;
break ;
default :
Error ( ) < < " Trade::TgaImporter::image2D(): unsupported color bits-per-pixel: " < < header . bpp ;
Converting APIs returning std::optional to Containers::Optional.
And removing the bundled std::optional implementation. This finally
makes this library compatible with C++17. Since this would be a huge
backwards-incompatible change that would make everyone angry, the
following had to be done in case both CORRADE_BUILD_DEPRECATED and
MAGNUM_BUILD_DEPRECATED is defined:
* Under C++11 and C++14, Containers::Optional / Containers::NullOpt is
aliased to std::optional / std::nullopt. This is no worse than the
state before, when we also provided these symbols.
* Under C++17, where standard <optional> header is available,
Containers::Optional provides implicit conversion to it. Only one-way
conversion is supported, as there was fortunately no Magnum API that
took std::optional via parameter, and there might be some corner
cases that this doesn't cover. The goal is to have all examples
compiling with the old API, at least.
* There's a new test especially for this, which checks that both the
C++11 and C++17 ways of doing things work as they should.
The typedef and conversion is marked as deprecated, so it will spit out
many warnings to push users to upgrade. I hope I can completely remove
this mess soon :/
8 years ago
return Containers : : NullOpt ;
}
/* Grayscale */
} else if ( header . imageType = = 3 | | header . imageType = = 11 ) {
/* I only discovered this by accident when using ImageMagick's
mogrify - compression RunLengthEncoded file . tga
as far as I could find , it ' s not documented in any TGA specs */
rle = header . imageType = = 11 ;
format = PixelFormat : : R8Unorm ;
if ( header . bpp ! = 8 ) {
Error ( ) < < " Trade::TgaImporter::image2D(): unsupported grayscale bits-per-pixel: " < < header . bpp ;
Converting APIs returning std::optional to Containers::Optional.
And removing the bundled std::optional implementation. This finally
makes this library compatible with C++17. Since this would be a huge
backwards-incompatible change that would make everyone angry, the
following had to be done in case both CORRADE_BUILD_DEPRECATED and
MAGNUM_BUILD_DEPRECATED is defined:
* Under C++11 and C++14, Containers::Optional / Containers::NullOpt is
aliased to std::optional / std::nullopt. This is no worse than the
state before, when we also provided these symbols.
* Under C++17, where standard <optional> header is available,
Containers::Optional provides implicit conversion to it. Only one-way
conversion is supported, as there was fortunately no Magnum API that
took std::optional via parameter, and there might be some corner
cases that this doesn't cover. The goal is to have all examples
compiling with the old API, at least.
* There's a new test especially for this, which checks that both the
C++11 and C++17 ways of doing things work as they should.
The typedef and conversion is marked as deprecated, so it will spit out
many warnings to push users to upgrade. I hope I can completely remove
this mess soon :/
8 years ago
return Containers : : NullOpt ;
}
/* Other? */
} else {
Error ( ) < < " Trade::TgaImporter::image2D(): unsupported image type: " < < header . imageType ;
Converting APIs returning std::optional to Containers::Optional.
And removing the bundled std::optional implementation. This finally
makes this library compatible with C++17. Since this would be a huge
backwards-incompatible change that would make everyone angry, the
following had to be done in case both CORRADE_BUILD_DEPRECATED and
MAGNUM_BUILD_DEPRECATED is defined:
* Under C++11 and C++14, Containers::Optional / Containers::NullOpt is
aliased to std::optional / std::nullopt. This is no worse than the
state before, when we also provided these symbols.
* Under C++17, where standard <optional> header is available,
Containers::Optional provides implicit conversion to it. Only one-way
conversion is supported, as there was fortunately no Magnum API that
took std::optional via parameter, and there might be some corner
cases that this doesn't cover. The goal is to have all examples
compiling with the old API, at least.
* There's a new test especially for this, which checks that both the
C++11 and C++17 ways of doing things work as they should.
The typedef and conversion is marked as deprecated, so it will spit out
many warnings to push users to upgrade. I hope I can completely remove
this mess soon :/
8 years ago
return Containers : : NullOpt ;
}
const std : : size_t pixelSize = header . bpp / 8 ;
const std : : size_t outputSize = std : : size_t ( size . product ( ) ) * pixelSize ;
/* Copy data directly if not RLE */
Containers : : Array < char > data { outputSize } ;
Containers : : ArrayView < const char > srcPixels = _in . exceptPrefix ( sizeof ( Implementation : : TgaHeader ) ) ;
if ( ! rle ) {
/* Files that are larger are allowed in this case (but not for RLE) */
if ( srcPixels . size ( ) < outputSize ) {
Error { } < < " Trade::TgaImporter::image2D(): file too short, expected " < < outputSize + sizeof ( Implementation : : TgaHeader ) < < " bytes but got " < < _in . size ( ) ;
return Containers : : NullOpt ;
}
Utility : : copy ( srcPixels . prefix ( data . size ( ) ) , data ) ;
/* Otherwise decode */
} else {
Containers : : ArrayView < char > dstPixels = data ;
while ( ! srcPixels . isEmpty ( ) ) {
/* Reference: http://www.paulbourke.net/dataformats/tga/ */
/* 8-bit RLE header. First bit denotes the operation, last 7 bits
denotes operation count minus 1. */
const UnsignedByte rleHeader = srcPixels [ 0 ] ;
const std : : size_t count = ( rleHeader & ~ 0x80 ) + 1 ;
/* First bit set to 1 means copying the following pixel given
number of times , 0 means copying the following number of
pixels once . We represent that operation with a stride . */
const std : : size_t dataSize = ( rleHeader & 0x80 ? 1 : count ) * pixelSize ;
const std : : ptrdiff_t stride = rleHeader & 0x80 ? 0 : pixelSize ;
/* Check bounds */
if ( 1 + dataSize > srcPixels . size ( ) ) {
Error { } < < " Trade::TgaImporter::image2D(): RLE file too short at pixel " < < ( dstPixels . begin ( ) - data . begin ( ) ) / pixelSize ;
return Containers : : NullOpt ;
}
if ( count * pixelSize > dstPixels . size ( ) ) {
Error { } < < " Trade::TgaImporter::image2D(): RLE data at byte " < < ( srcPixels . data ( ) - _in . data ( ) ) < < " contains " < < count < < " pixels but only " < < dstPixels . size ( ) / pixelSize < < " left to decode " ;
return Containers : : NullOpt ;
}
/* Copy the data */
Containers : : StridedArrayView2D < const char > src {
srcPixels . slice ( 1 , 1 + dataSize ) ,
{ count , pixelSize } , { stride , 1 } } ;
Containers : : StridedArrayView2D < char > dst {
dstPixels . prefix ( count * pixelSize ) ,
{ count , pixelSize } } ;
Utility : : copy ( src , dst ) ;
/* Update views for the next round */
srcPixels = srcPixels . exceptPrefix ( 1 + dataSize ) ;
dstPixels = dstPixels . exceptPrefix ( count * pixelSize ) ;
}
}
/* Adjust pixel storage if row size is not four byte aligned */
PixelStorage storage ;
if ( ( size . x ( ) * header . bpp / 8 ) % 4 ! = 0 )
storage . setAlignment ( 1 ) ;
if ( format = = PixelFormat : : RGB8Unorm ) {
if ( flags ( ) & ImporterFlag : : Verbose )
Debug { } < < " Trade::TgaImporter::image2D(): converting from BGR to RGB " ;
for ( Vector3ub & pixel : Containers : : arrayCast < Vector3ub > ( data ) )
pixel = Math : : gather < ' b ' , ' g ' , ' r ' > ( pixel ) ;
} else if ( format = = PixelFormat : : RGBA8Unorm ) {
if ( flags ( ) & ImporterFlag : : Verbose )
Debug { } < < " Trade::TgaImporter::image2D(): converting from BGRA to RGBA " ;
for ( Vector4ub & pixel : Containers : : arrayCast < Vector4ub > ( data ) )
pixel = Math : : gather < ' b ' , ' g ' , ' r ' , ' a ' > ( pixel ) ;
}
return ImageData2D { storage , format , size , std : : move ( data ) } ;
}
} }
plugins: new testing workflow.
The current testing workflow had quite a few major flaws and it was no
longer possible after the move of Any* plugins to core. Among the flaws
is:
* Every plugin was basically built twice, once as the real plugin and
once as a static testing library. Most of the build shared common
object files, but nevertheless it inflated build times and made the
buildsystem extremely complex.
* Because the actual plugin binary was never actually loaded during the
test, it couldn't spot problems like:
- undefined references
- errors in metadata files
- mismatched plugin interface/version, missing entry points
- broken static plugin import files
* Tests that made use of independent plugins (such as TgaImageConverter
test using TgaImporter to verify the output) had a hardcoded
dependency on such plugins, making a minimal setup very hard.
* Dynamic loading of plugins from the Any* proxies was always directed
to the install location on the filesystem with no possibility to
load these directly from the build tree. That caused random ABI
mismatch crashes, or, on the other hand, if no plugins were
installed, particular portions of the codebase weren't tested at all.
Now the workflow is the following:
* Every plugin is built exactly once, either as dynamic or as static.
* The test always loads it via the plugin manager. If it's dynamic,
it's loaded straight from the build directory; if it's static, it
gets linked to the test executable directly.
* Plugins used indirectly are always served from the build directory
(if enabled) to ensure reproducibility and independence on what's
installed on the filesystem. Missing presence of these plugins causes
particular tests to be simply skipped.
* Plugins that have extensive tests for internal functionality that's
not exposed through the plugin interface are still built in two
parts, but the internal tests are simply consuming the OBJECT files
directly instead of linking to a static library.
8 years ago
CORRADE_PLUGIN_REGISTER ( TgaImporter , Magnum : : Trade : : TgaImporter ,
" cz.mosra.magnum.Trade.AbstractImporter/0.5 " )