From cf222941ec8c4e54f630159e081a26ff022b7c23 Mon Sep 17 00:00:00 2001 From: SirLynix Date: Tue, 14 Nov 2023 15:41:59 +0100 Subject: [PATCH] Utility/Image: Fix functions taking a Color --- src/Nazara/Utility/Image.cpp | 30 +++++++++++++++++------------- src/Nazara/Utility/PixelFormat.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/Nazara/Utility/Image.cpp b/src/Nazara/Utility/Image.cpp index f4c78266a..344b0b0fe 100644 --- a/src/Nazara/Utility/Image.cpp +++ b/src/Nazara/Utility/Image.cpp @@ -3,6 +3,7 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include #include #include #include @@ -310,10 +311,11 @@ namespace Nz #endif UInt8 bpp = PixelFormatInfo::GetBytesPerPixel(m_sharedImage->format); - std::unique_ptr colorBuffer(new UInt8[bpp]); - if (!PixelFormatInfo::Convert(PixelFormat::RGBA8, m_sharedImage->format, &color.r, colorBuffer.get())) + StackArray 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 colorBuffer(new UInt8[bpp]); - if (!PixelFormatInfo::Convert(PixelFormat::RGBA8, m_sharedImage->format, &color.r, colorBuffer.get())) + StackArray 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 colorBuffer(new UInt8[bpp]); - if (!PixelFormatInfo::Convert(PixelFormat::RGBA8, m_sharedImage->format, &color.r, colorBuffer.get())) + StackArray 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; diff --git a/src/Nazara/Utility/PixelFormat.cpp b/src/Nazara/Utility/PixelFormat.cpp index 3fa8ab26e..07d55c6dd 100644 --- a/src/Nazara/Utility/PixelFormat.cpp +++ b/src/Nazara/Utility/PixelFormat.cpp @@ -1370,6 +1370,31 @@ namespace Nz return dst; } + /**********************************RGBA32F**********************************/ + template<> + UInt8* ConvertPixels(const UInt8* start, const UInt8* end, UInt8* dst) + { + while (start < end) + { + const float* ptr = reinterpret_cast(start); + + *dst++ = static_cast(ptr[0] * 255.f); + *dst++ = static_cast(ptr[1] * 255.f); + *dst++ = static_cast(ptr[2] * 255.f); + *dst++ = static_cast(ptr[3] * 255.f); + + start += 16; + } + + return dst; + } + + template<> + UInt8* ConvertPixels(const UInt8* start, const UInt8* end, UInt8* dst) + { + return ConvertPixels(start, end, dst); + } + template void RegisterConverter() { @@ -1775,6 +1800,10 @@ namespace Nz RegisterConverter(); RegisterConverter(); + /**********************************RGBA32F**********************************/ + RegisterConverter(); + RegisterConverter(); + return true; }