From 1ed81ae973df283765def13b6f86cdbf5ec6ee52 Mon Sep 17 00:00:00 2001 From: Chris Galvan Date: Thu, 13 Jan 2022 10:27:19 -0600 Subject: [PATCH] Added API for retrieving region of streaming image asset pixel values Signed-off-by: Chris Galvan --- .../RPI.Reflect/Image/StreamingImageAsset.h | 5 +- .../RPI.Reflect/Image/StreamingImageAsset.cpp | 50 +++++++++++++++---- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/Gems/Atom/RPI/Code/Include/Atom/RPI.Reflect/Image/StreamingImageAsset.h b/Gems/Atom/RPI/Code/Include/Atom/RPI.Reflect/Image/StreamingImageAsset.h index bdb68601a9..e934ac8bcf 100644 --- a/Gems/Atom/RPI/Code/Include/Atom/RPI.Reflect/Image/StreamingImageAsset.h +++ b/Gems/Atom/RPI/Code/Include/Atom/RPI.Reflect/Image/StreamingImageAsset.h @@ -85,10 +85,13 @@ namespace AZ //! Get image data for specified mip and slice. It may return empty array if its mipchain assets are not loaded AZStd::array_view GetSubImageData(uint32_t mip, uint32_t slice); - //! Get image pixel value for specified mip and slice + //! Get single image pixel value for specified mip and slice template T GetSubImagePixelValue(uint32_t x, uint32_t y, uint32_t componentIndex = 0, uint32_t mip = 0, uint32_t slice = 0); + //! Retrieve a region of image pixel values (float) for specified mip and slice + void GetSubImagePixelValues(AZStd::pair topLeft, AZStd::pair bottomRight, AZStd::array_view outValues, uint32_t componentIndex = 0, uint32_t mip = 0, uint32_t slice = 0); + //! Returns streaming image pool asset id of the pool that will be used to create the streaming image. const Data::AssetId& GetPoolAssetId() const; diff --git a/Gems/Atom/RPI/Code/Source/RPI.Reflect/Image/StreamingImageAsset.cpp b/Gems/Atom/RPI/Code/Source/RPI.Reflect/Image/StreamingImageAsset.cpp index 37f0e04116..e005fb7f9e 100644 --- a/Gems/Atom/RPI/Code/Source/RPI.Reflect/Image/StreamingImageAsset.cpp +++ b/Gems/Atom/RPI/Code/Source/RPI.Reflect/Image/StreamingImageAsset.cpp @@ -18,13 +18,19 @@ namespace AZ template float RetrieveFloatValue(const AZ::u8* mem, size_t index) { - AZ_Assert(false, "Unsupported pixel format"); + static_assert(false, "Unsupported pixel format"); } template AZ::u32 RetrieveUintValue(const AZ::u8* mem, size_t index) { - AZ_Assert(false, "Unsupported pixel format"); + static_assert(false, "Unsupported pixel format"); + } + + template + AZ::s32 RetrieveIntValue(const AZ::u8* mem, size_t index) + { + static_assert(false, "Unsupported pixel format"); } template <> @@ -217,18 +223,14 @@ namespace AZ template<> float StreamingImageAsset::GetSubImagePixelValue(uint32_t x, uint32_t y, uint32_t componentIndex, uint32_t mip, uint32_t slice) { - // TODO: Use the component index - (void)componentIndex; + AZStd::vector values; + auto position = AZStd::make_pair(x, y); - auto imageData = GetSubImageData(mip, slice); + GetSubImagePixelValues(position, position, values, componentIndex, mip, slice); - if (!imageData.empty()) + if (values.size() == 1) { - const AZ::RHI::ImageDescriptor imageDescriptor = GetImageDescriptor(); - auto width = imageDescriptor.m_size.m_width; - size_t index = (y * width) + x; - - return Internal::RetrieveFloatValue(imageData.data(), index, imageDescriptor.m_format); + return values[0]; } return 0.0f; @@ -253,5 +255,31 @@ namespace AZ return 0; } + + void StreamingImageAsset::GetSubImagePixelValues(AZStd::pair topLeft, AZStd::pair bottomRight, AZStd::array_view outValues, uint32_t componentIndex, uint32_t mip, uint32_t slice) + { + // TODO: Use the component index + (void)componentIndex; + + auto imageData = GetSubImageData(mip, slice); + + if (!imageData.empty()) + { + const AZ::RHI::ImageDescriptor imageDescriptor = GetImageDescriptor(); + auto width = imageDescriptor.m_size.m_width; + + size_t outValuesIndex = 0; + for (uint32_t x = topLeft.first; x < bottomRight.first; ++x) + { + for (uint32_t y = topLeft.second; y < bottomRight.second; ++y) + { + size_t imageDataIndex = (y * width) + x; + + auto& outValue = const_cast(outValues[outValuesIndex++]); + outValue = Internal::RetrieveFloatValue(imageData.data(), imageDataIndex, imageDescriptor.m_format); + } + } + } + } } }