Utility/Image: Add HasAlpha() method

This commit is contained in:
Lynix 2016-11-18 01:15:58 +01:00
parent 93a2e54667
commit ff7e64494f
2 changed files with 40 additions and 0 deletions

View File

@ -91,6 +91,8 @@ namespace Nz
ImageType GetType() const;
unsigned int GetWidth(UInt8 level = 0) const;
bool HasAlpha() const;
bool IsValid() const;
// Load

View File

@ -825,6 +825,44 @@ namespace Nz
return GetLevelSize(m_sharedImage->width, level);
}
bool Image::HasAlpha() const
{
NazaraAssert(m_sharedImage != &emptyImage, "Image must be valid");
if (!PixelFormat::HasAlpha(m_sharedImage->format))
return false;
if (!PixelFormat::IsCompressed(m_sharedImage->format))
{
const PixelFormatInfo& info = PixelFormat::GetInfo(m_sharedImage->format);
const UInt8* pixel = GetConstPixels();
Bitset<> workingBitset;
std::size_t pixelCount = m_sharedImage->width * m_sharedImage->height * ((m_sharedImage->type == ImageType_Cubemap) ? 6 : m_sharedImage->depth);
if (pixelCount == 0)
return false;
auto seq = workingBitset.Read(GetConstPixels(), info.bitsPerPixel);
do
{
workingBitset &= info.alphaMask;
if (workingBitset.Count() != info.alphaMask.Count()) //< Means that at least one bit of the alpha mask of this pixel is disabled
return true;
workingBitset.Clear();
workingBitset.Read(seq, info.bitsPerPixel);
}
while (--pixelCount > 0);
return false;
}
else
{
// FIXME: Currently, we assume the pixel format is already the right one
return true;
}
}
bool Image::IsValid() const
{
return m_sharedImage != &emptyImage;