Utility/Image: Fix functions taking a Color

This commit is contained in:
SirLynix 2023-11-14 15:41:59 +01:00
parent a4b10749f7
commit cf222941ec
2 changed files with 46 additions and 13 deletions

View File

@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/Image.hpp>
#include <NazaraUtils/StackArray.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/ErrorFlags.hpp>
#include <Nazara/Core/StringExt.hpp>
@ -310,10 +311,11 @@ namespace Nz
#endif
UInt8 bpp = PixelFormatInfo::GetBytesPerPixel(m_sharedImage->format);
std::unique_ptr<UInt8[]> colorBuffer(new UInt8[bpp]);
if (!PixelFormatInfo::Convert(PixelFormat::RGBA8, m_sharedImage->format, &color.r, colorBuffer.get()))
StackArray<UInt8> colorBuffer = NazaraStackArrayNoInit(UInt8, bpp);
if (!PixelFormatInfo::Convert(PixelFormat::RGBA32F, m_sharedImage->format, &color.r, &colorBuffer[0]))
{
NazaraErrorFmt("failed to convert RGBA8 to {0}", PixelFormatInfo::GetName(m_sharedImage->format));
NazaraErrorFmt("failed to convert RGBA32F to {0}", PixelFormatInfo::GetName(m_sharedImage->format));
return false;
}
@ -335,7 +337,7 @@ namespace Nz
while (ptr < end)
{
std::memcpy(ptr, colorBuffer.get(), bpp);
std::memcpy(ptr, &colorBuffer[0], bpp);
ptr += bpp;
}
@ -388,10 +390,11 @@ namespace Nz
EnsureOwnership();
UInt8 bpp = PixelFormatInfo::GetBytesPerPixel(m_sharedImage->format);
std::unique_ptr<UInt8[]> colorBuffer(new UInt8[bpp]);
if (!PixelFormatInfo::Convert(PixelFormat::RGBA8, m_sharedImage->format, &color.r, colorBuffer.get()))
StackArray<UInt8> colorBuffer = NazaraStackArrayNoInit(UInt8, bpp);
if (!PixelFormatInfo::Convert(PixelFormat::RGBA32F, m_sharedImage->format, &color.r, &colorBuffer[0]))
{
NazaraErrorFmt("failed to convert RGBA8 to {0}", PixelFormatInfo::GetName(m_sharedImage->format));
NazaraErrorFmt("failed to convert RGBA32F to {0}", PixelFormatInfo::GetName(m_sharedImage->format));
return false;
}
@ -409,7 +412,7 @@ namespace Nz
UInt8* end = facePixels + srcStride;
while (start < end)
{
std::memcpy(start, colorBuffer.get(), bpp);
std::memcpy(start, &colorBuffer[0], bpp);
start += bpp;
}
@ -460,10 +463,11 @@ namespace Nz
EnsureOwnership();
UInt8 bpp = PixelFormatInfo::GetBytesPerPixel(m_sharedImage->format);
std::unique_ptr<UInt8[]> colorBuffer(new UInt8[bpp]);
if (!PixelFormatInfo::Convert(PixelFormat::RGBA8, m_sharedImage->format, &color.r, colorBuffer.get()))
StackArray<UInt8> colorBuffer = NazaraStackArrayNoInit(UInt8, bpp);
if (!PixelFormatInfo::Convert(PixelFormat::RGBA32F, m_sharedImage->format, &color.r, &colorBuffer[0]))
{
NazaraErrorFmt("failed to convert RGBA8 to {0}", PixelFormatInfo::GetName(m_sharedImage->format));
NazaraErrorFmt("failed to convert RGBA32F to {0}", PixelFormatInfo::GetName(m_sharedImage->format));
return false;
}
@ -477,7 +481,7 @@ namespace Nz
UInt8* end = dstPixels + srcStride;
while (start < end)
{
std::memcpy(start, colorBuffer.get(), bpp);
std::memcpy(start, &colorBuffer[0], bpp);
start += bpp;
}
@ -1218,7 +1222,7 @@ namespace Nz
UInt8* pixel = GetPixelPtr(m_sharedImage->levels[0].get(), PixelFormatInfo::GetBytesPerPixel(m_sharedImage->format), x, y, z, m_sharedImage->width, m_sharedImage->height);
if (!PixelFormatInfo::Convert(PixelFormat::RGBA8, m_sharedImage->format, &color.r, pixel))
if (!PixelFormatInfo::Convert(PixelFormat::RGBA32F, m_sharedImage->format, &color.r, pixel))
{
NazaraError("failed to convert RGBA8 to image's format");
return false;

View File

@ -1370,6 +1370,31 @@ namespace Nz
return dst;
}
/**********************************RGBA32F**********************************/
template<>
UInt8* ConvertPixels<PixelFormat::RGBA32F, PixelFormat::RGBA8>(const UInt8* start, const UInt8* end, UInt8* dst)
{
while (start < end)
{
const float* ptr = reinterpret_cast<const float*>(start);
*dst++ = static_cast<UInt8>(ptr[0] * 255.f);
*dst++ = static_cast<UInt8>(ptr[1] * 255.f);
*dst++ = static_cast<UInt8>(ptr[2] * 255.f);
*dst++ = static_cast<UInt8>(ptr[3] * 255.f);
start += 16;
}
return dst;
}
template<>
UInt8* ConvertPixels<PixelFormat::RGBA32F, PixelFormat::RGBA8_SRGB>(const UInt8* start, const UInt8* end, UInt8* dst)
{
return ConvertPixels<PixelFormat::RGBA32F, PixelFormat::RGBA8>(start, end, dst);
}
template<PixelFormat Format1, PixelFormat Format2>
void RegisterConverter()
{
@ -1775,6 +1800,10 @@ namespace Nz
RegisterConverter<PixelFormat::RGBA8, PixelFormat::RGBA8_SRGB>();
RegisterConverter<PixelFormat::RGBA8, PixelFormat::RGBA32F>();
/**********************************RGBA32F**********************************/
RegisterConverter<PixelFormat::RGBA32F, PixelFormat::RGBA8>();
RegisterConverter<PixelFormat::RGBA32F, PixelFormat::RGBA8_SRGB>();
return true;
}