1f9b284de2
Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> # Conflicts: # Code/Editor/Plugins/ComponentEntityEditorPlugin/ComponentEntityEditorPlugin_precompiled.h # Code/Editor/Plugins/EditorCommon/EditorCommon_precompiled.h # Code/Editor/Plugins/EditorCommon/stdafx.cpp # Code/Editor/Plugins/FFMPEGPlugin/FFMPEGPlugin_precompiled.h # Code/Editor/Plugins/PerforcePlugin/PerforcePlugin_precompiled.h # Code/Editor/Plugins/ProjectSettingsTool/ProjectSettingsTool_precompiled.h # Code/Framework/AzToolsFramework/AzToolsFramework/AzToolsFramework_precompiled.h # Code/Tools/AssetProcessor/native/precompiled.h # Code/Tools/Standalone/StandaloneTools_precompiled.h # Gems/AssetMemoryAnalyzer/Code/Source/AssetMemoryAnalyzer_precompiled.h # Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageProcessing_precompiled.h # Gems/Atom/RHI/DX12/Code/Source/RHI/Atom_RHI_DX12_precompiled.h # Gems/Atom/RHI/Metal/Code/Include/Platform/Mac/Atom_RHI_Metal_precompiled_Platform.h # Gems/Atom/RHI/Metal/Code/Include/Platform/iOS/Atom_RHI_Metal_precompiled_Platform.h # Gems/Atom/RHI/Metal/Code/Source/Atom_RHI_Metal_precompiled.h # Gems/Atom/RHI/Metal/Code/atom_rhi_metal_common_files.cmake # Gems/Atom/RHI/Null/Code/Source/Atom_RHI_Null_precompiled.h # Gems/Atom/RHI/Null/Code/atom_rhi_null_common_files.cmake # Gems/Atom/RHI/Vulkan/Code/Include/Platform/Android/Atom_RHI_Vulkan_precompiled_Platform.h # Gems/Atom/RHI/Vulkan/Code/Include/Platform/Linux/Atom_RHI_Vulkan_precompiled_Platform.h # Gems/Atom/RHI/Vulkan/Code/Include/Platform/Mac/Atom_RHI_Vulkan_precompiled_Platform.h # Gems/Atom/RHI/Vulkan/Code/Include/Platform/Windows/Atom_RHI_Vulkan_precompiled_Platform.h # Gems/Atom/RHI/Vulkan/Code/Source/Atom_RHI_Vulkan_precompiled.h # Gems/Atom/RHI/Vulkan/Code/Source/RHI/SwapChain.cpp # Gems/Atom/RHI/Vulkan/Code/atom_rhi_vulkan_common_files.cmake # Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/AtomFont_precompiled.h # Gems/Blast/Code/Source/StdAfx.cpp # Gems/Camera/Code/Source/Camera_precompiled.h # Gems/EMotionFX/Code/Source/EMotionFX_precompiled.h # Gems/FastNoise/Code/Source/FastNoise_precompiled.h # Gems/Gestures/Code/Source/Gestures_precompiled.h # Gems/GradientSignal/Code/Source/GradientSignal_precompiled.h # Gems/GraphCanvas/Code/precompiled.h # Gems/ImGui/Code/Source/ImGui_precompiled.h # Gems/InAppPurchases/Code/Source/InAppPurchases_precompiled.h # Gems/LmbrCentral/Code/Source/LmbrCentral_precompiled.h # Gems/LmbrCentral/Code/Tests/ShapeGeometryUtilTest.cpp # Gems/LyShine/Code/Editor/UiCanvasEditor_precompiled.h # Gems/LyShine/Code/Source/Animation/LyShine_precompiled.h # Gems/LyShine/Code/Source/LyShine_precompiled.h # Gems/LyShineExamples/Code/Source/LyShineExamples_precompiled.h # Gems/Maestro/Code/Source/Cinematics/Maestro_precompiled.h # Gems/Maestro/Code/Source/Maestro_precompiled.h # Gems/MessagePopup/Code/Source/MessagePopup_precompiled.h # Gems/Metastream/Code/Source/Metastream_precompiled.h # Gems/Microphone/Code/Source/Microphone_precompiled.h # Gems/Multiplayer/Code/Source/Multiplayer_precompiled.h # Gems/PhysX/Code/NumericalMethods/Source/NumericalMethods_precompiled.h # Gems/PhysX/Code/Source/PhysXUnsupported_precompiled.h # Gems/PhysX/Code/Source/PhysX_precompiled.h # Gems/PhysX/Code/physx_unsupported_files.cmake # Gems/PhysXDebug/Code/Source/PhysXDebugUnsupported_precompiled.h # Gems/PhysXDebug/Code/Source/PhysXDebug_precompiled.h # Gems/ScriptCanvas/Code/Editor/precompiled.h # Gems/ScriptCanvas/Code/Source/precompiled.h # Gems/ScriptCanvasDeveloper/Code/Source/precompiled.h # Gems/ScriptCanvasPhysics/Code/Source/ScriptCanvasPhysics_precompiled.h # Gems/ScriptEvents/Code/Source/precompiled.h # Gems/ScriptEvents/Code/Tests/Editor/EditorTests.cpp # Gems/ScriptedEntityTweener/Code/Source/ScriptedEntityTweener_precompiled.h # Gems/SliceFavorites/Code/Source/SliceFavorites_precompiled.h # Gems/StartingPointCamera/Code/Source/StartingPointCamera_precompiled.h # Gems/StartingPointInput/Code/Source/StartingPointInput_precompiled.h # Gems/StartingPointMovement/Code/Source/StartingPointMovement_precompiled.h # Gems/SurfaceData/Code/Source/SurfaceData_precompiled.h # Gems/TextureAtlas/Code/Source/TextureAtlas_precompiled.h # Gems/TickBusOrderViewer/Code/Source/TickBusOrderViewer_precompiled.h # Gems/Twitch/Code/Source/Twitch_precompiled.h # Gems/VirtualGamepad/Code/Source/VirtualGamepad_precompiled.h # Gems/WhiteBox/Code/Source/WhiteBoxUnsupported_precompiled.h # Gems/WhiteBox/Code/Source/WhiteBox_precompiled.h
254 lines
8.1 KiB
C++
254 lines
8.1 KiB
C++
/*
|
|
* Copyright (c) Contributors to the Open 3D Engine Project.
|
|
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
|
*
|
|
*/
|
|
|
|
#include <Atom/ImageProcessing/ImageObject.h>
|
|
#include <Processing/ImageToProcess.h>
|
|
#include <Processing/PixelFormatInfo.h>
|
|
#include <Processing/ImageFlags.h>
|
|
#include <Atom/ImageProcessing/PixelFormats.h>
|
|
|
|
#include <Converters/FIR-Weights.h>
|
|
#include <Converters/PixelOperation.h>
|
|
|
|
namespace ImageProcessingAtom
|
|
{
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
// Lookup table for a function 'float fn(float x)'.
|
|
// Computed function values are stored in the table for x in [0.0; 1.0].
|
|
//
|
|
// If passed x is less than xMin (xMin must be >= 0) or greater than 1.0,
|
|
// then the original function is called.
|
|
// Otherwise, a value from the table (linearly interpolated)
|
|
// is returned.
|
|
template <int TABLE_SIZE>
|
|
class FunctionLookupTable
|
|
{
|
|
public:
|
|
FunctionLookupTable(float(*fn)(float x), float xMin, float maxAllowedDifference)
|
|
: m_fn(fn)
|
|
, m_xMin(xMin)
|
|
, m_fMaxDiff(maxAllowedDifference)
|
|
{
|
|
}
|
|
|
|
void Initialize() const
|
|
{
|
|
m_initialized = true;
|
|
AZ_Assert(m_xMin >= 0.0f, "wrong initial data for m_xMin");
|
|
for (int i = 0; i <= TABLE_SIZE; ++i)
|
|
{
|
|
const float x = i / (float)TABLE_SIZE;
|
|
const float y = (*m_fn)(x);
|
|
m_table[i] = y;
|
|
}
|
|
}
|
|
|
|
inline float compute(float x) const
|
|
{
|
|
if (x < m_xMin || x > 1)
|
|
{
|
|
return m_fn(x);
|
|
}
|
|
|
|
const float f = x * TABLE_SIZE;
|
|
|
|
const int i = int(f);
|
|
|
|
if (!m_initialized)
|
|
{
|
|
Initialize();
|
|
}
|
|
|
|
if (i >= TABLE_SIZE)
|
|
{
|
|
return m_table[TABLE_SIZE];
|
|
}
|
|
|
|
const float alpha = f - i;
|
|
return (1 - alpha) * m_table[i] + alpha * m_table[i + 1];
|
|
}
|
|
|
|
public:
|
|
bool Test(const float maxDifferenceAllowed) const
|
|
{
|
|
if (int(-0.99f) != 0 ||
|
|
int(+0.00f) != 0 ||
|
|
int(+0.01f) != 0 ||
|
|
int(+0.99f) != 0 ||
|
|
int(+1.00f) != 1 ||
|
|
int(+1.01f) != 1 ||
|
|
int(+1.99f) != 1 ||
|
|
int(+2.00f) != 2 ||
|
|
int(+2.01f) != 2)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (m_xMin < 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
const int n = 1000000;
|
|
for (int i = 0; i <= n; ++i)
|
|
{
|
|
const float x = 1.1f * (i / (float)n);
|
|
const float resOriginal = m_fn(x);
|
|
const float resTable = compute(x);
|
|
const float difference = resOriginal - resTable;
|
|
|
|
if (fabs(difference) > maxDifferenceAllowed)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private:
|
|
float(* m_fn)(float x);
|
|
float m_xMin;
|
|
mutable float m_table[TABLE_SIZE + 1];
|
|
mutable bool m_initialized = false;
|
|
float m_fMaxDiff = 0.0f;
|
|
};
|
|
|
|
|
|
static float GammaToLinear(float x)
|
|
{
|
|
return (x <= 0.04045f) ? x / 12.92f : powf((x + 0.055f) / 1.055f, 2.4f);
|
|
}
|
|
|
|
static float LinearToGamma(float x)
|
|
{
|
|
return (x <= 0.0031308f) ? x * 12.92f : 1.055f * powf(x, 1.0f / 2.4f) - 0.055f;
|
|
}
|
|
|
|
static FunctionLookupTable<1024> s_lutGammaToLinear(GammaToLinear, 0.04045f, 0.00001f);
|
|
static FunctionLookupTable<1024> s_lutLinearToGamma(LinearToGamma, 0.05f, 0.00001f);
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
bool ImageToProcess::GammaToLinearRGBA32F(bool bDeGamma)
|
|
{
|
|
// return immediately if there is no need to de-gamma image and the source is in the desired format
|
|
EPixelFormat srcFmt = m_img->GetPixelFormat();
|
|
if (!bDeGamma && (srcFmt == ePixelFormat_R32G32B32A32F))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
//convert to 32F first
|
|
if (!CPixelFormats::GetInstance().IsPixelFormatUncompressed(srcFmt))
|
|
{
|
|
ConvertFormat(ePixelFormat_R32G32B32A32F);
|
|
srcFmt = ePixelFormat_R32G32B32A32F;
|
|
}
|
|
|
|
IImageObjectPtr srcImage = m_img;
|
|
EPixelFormat dstFmt = ePixelFormat_R32G32B32A32F;
|
|
IImageObjectPtr dstImage(m_img->AllocateImage(dstFmt));
|
|
|
|
//create pixel operation function for src and dst images
|
|
IPixelOperationPtr srcOp = CreatePixelOperation(srcFmt);
|
|
IPixelOperationPtr dstOp = CreatePixelOperation(dstFmt);
|
|
|
|
//get count of bytes per pixel for both src and dst images
|
|
uint32 srcPixelBytes = CPixelFormats::GetInstance().GetPixelFormatInfo(srcFmt)->bitsPerBlock / 8;
|
|
uint32 dstPixelBytes = CPixelFormats::GetInstance().GetPixelFormatInfo(dstFmt)->bitsPerBlock / 8;
|
|
|
|
const uint32 dwMips = dstImage->GetMipCount();
|
|
float r, g, b, a;
|
|
for (uint32 dwMip = 0; dwMip < dwMips; ++dwMip)
|
|
{
|
|
uint8* srcPixelBuf;
|
|
uint32 srcPitch;
|
|
srcImage->GetImagePointer(dwMip, srcPixelBuf, srcPitch);
|
|
uint8* dstPixelBuf;
|
|
uint32 dstPitch;
|
|
dstImage->GetImagePointer(dwMip, dstPixelBuf, dstPitch);
|
|
|
|
const uint32 pixelCount = srcImage->GetPixelCount(dwMip);
|
|
|
|
for (uint32 i = 0; i < pixelCount; ++i, srcPixelBuf += srcPixelBytes, dstPixelBuf += dstPixelBytes)
|
|
{
|
|
srcOp->GetRGBA(srcPixelBuf, r, g, b, a);
|
|
if (bDeGamma)
|
|
{
|
|
r = s_lutGammaToLinear.compute(r);
|
|
g = s_lutGammaToLinear.compute(g);
|
|
b = s_lutGammaToLinear.compute(b);
|
|
}
|
|
|
|
dstOp->SetRGBA(dstPixelBuf, r, g, b, a);
|
|
}
|
|
}
|
|
|
|
m_img = dstImage;
|
|
|
|
if (bDeGamma)
|
|
{
|
|
m_img->RemoveImageFlags(EIF_SRGBRead);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void ImageToProcess::LinearToGamma()
|
|
{
|
|
if (Get()->HasImageFlags(EIF_SRGBRead))
|
|
{
|
|
AZ_Assert(false, "%s: input image is already SRGB", __FUNCTION__);
|
|
return;
|
|
}
|
|
|
|
if (!CPixelFormats::GetInstance().IsPixelFormatUncompressed(m_img->GetPixelFormat()))
|
|
{
|
|
AZ_Assert(false, "This is not common user case with compressed format input. But it may continue");
|
|
ConvertFormat(ePixelFormat_R32G32B32A32F);
|
|
}
|
|
|
|
EPixelFormat srcFmt = m_img->GetPixelFormat();
|
|
IImageObjectPtr srcImage = m_img;
|
|
|
|
IImageObjectPtr dstImage(m_img->AllocateImage(srcFmt));
|
|
|
|
//create pixel operation function
|
|
IPixelOperationPtr pixelOp = CreatePixelOperation(srcFmt);
|
|
|
|
//get count of bytes per pixel for both src and dst images
|
|
uint32 pixelBytes = CPixelFormats::GetInstance().GetPixelFormatInfo(srcFmt)->bitsPerBlock / 8;
|
|
|
|
const uint32 dwMips = srcImage->GetMipCount();
|
|
float r, g, b, a;
|
|
for (uint32 dwMip = 0; dwMip < dwMips; ++dwMip)
|
|
{
|
|
uint8* srcPixelBuf;
|
|
uint32 srcPitch;
|
|
srcImage->GetImagePointer(dwMip, srcPixelBuf, srcPitch);
|
|
uint8* dstPixelBuf;
|
|
uint32 dstPitch;
|
|
dstImage->GetImagePointer(dwMip, dstPixelBuf, dstPitch);
|
|
|
|
const uint32 pixelCount = srcImage->GetPixelCount(dwMip);
|
|
|
|
for (uint32 i = 0; i < pixelCount; ++i, srcPixelBuf += pixelBytes, dstPixelBuf += pixelBytes)
|
|
{
|
|
pixelOp->GetRGBA(srcPixelBuf, r, g, b, a);
|
|
r = s_lutLinearToGamma.compute(r);
|
|
g = s_lutLinearToGamma.compute(g);
|
|
b = s_lutLinearToGamma.compute(b);
|
|
pixelOp->SetRGBA(dstPixelBuf, r, g, b, a);
|
|
}
|
|
}
|
|
|
|
m_img = dstImage;
|
|
Get()->AddImageFlags(EIF_SRGBRead);
|
|
}
|
|
} // namespace ImageProcessingAtom
|