Files
o3de/Gems/Atom/Asset/ImageProcessingAtom/Code/Source/Converters/PixelOperation.cpp
T
Esteban Papp 1f9b284de2 Merge branch 'development' into cmake/SPEC-7179
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
2021-07-16 15:42:37 -07:00

547 lines
16 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 <AzCore/std/smart_ptr/make_shared.h>
#include <Processing/ImageObjectImpl.h>
#include <Processing/ImageConvert.h>
#include <Processing/PixelFormatInfo.h>
#include <Converters/PixelOperation.h>
///////////////////////////////////////////////////////////////////////////////////
//functions for maintaining alpha coverage.
namespace ImageProcessingAtom
{
//convertion: all data type supported by pixel channel <=> float
float U8ToF32(uint8 in)
{
return in / 255.f;
}
uint8 F32ToU8(float in)
{
return static_cast<uint8>(round(AZ::GetClamp<float>(in, 0.f, 1.f) * 255.f));
}
float U16ToF32(uint16 in)
{
return in / 65535.f;
}
uint16 F32ToU16(float in)
{
return static_cast<uint16>(round(AZ::GetClamp<float>(in, 0.f, 1.f) * 65535.f));
}
float HalfToF32(SHalf in)
{
return in;
}
SHalf F32ToHalf(float in)
{
return SHalf(in);
}
//stucture for RGBE pixel format
struct RgbE
{
static const int RGB9E5_EXPONENT_BITS = 5;
static const int RGB9E5_MANTISSA_BITS = 9;
static const int RGB9E5_EXP_BIAS = 15;
static const int RGB9E5_MAX_VALID_BIASED_EXP = 31;
static const int MAX_RGB9E5_EXP = (RGB9E5_MAX_VALID_BIASED_EXP - RGB9E5_EXP_BIAS);
static const int RGB9E5_MANTISSA_VALUES = (1 << RGB9E5_MANTISSA_BITS);
static const int MAX_RGB9E5_MANTISSA = (RGB9E5_MANTISSA_VALUES - 1);
static float MAX_RGB9E5;
unsigned int r : 9;
unsigned int g : 9;
unsigned int b : 9;
unsigned int e : 5;
static int log2(float x)
{
int bitfield = *((int*)(&x));
bitfield &= ~0x80000000;
return ((bitfield >> 23) - 127);
}
void GetRGBF(float& outR, float& outG, float& outB) const
{
int exponent = e - RGB9E5_EXP_BIAS - RGB9E5_MANTISSA_BITS;
float scale = powf(2.0f, static_cast<float>(exponent));
outR = r * scale;
outG = g * scale;
outB = b * scale;
}
void SetRGBF(const float& inR, const float& inG, const float& inB)
{
float rf = AZStd::GetMax(0.0f, AZStd::GetMin(inR, MAX_RGB9E5));
float gf = AZStd::GetMax(0.0f, AZStd::GetMin(inG, MAX_RGB9E5));
float bf = AZStd::GetMax(0.0f, AZStd::GetMin(inB, MAX_RGB9E5));
float mf = AZStd::GetMax(rf, AZStd::GetMax(gf, bf));
e = AZStd::GetMax(0, log2(mf) + (RGB9E5_EXP_BIAS + 1));
int exponent = e - RGB9E5_EXP_BIAS - RGB9E5_MANTISSA_BITS;
float scale = powf(2.0f, static_cast<float>(exponent));
r = AZStd::GetMin(511, (int)floorf(rf / scale + 0.5f));
g = AZStd::GetMin(511, (int)floorf(gf / scale + 0.5f));
b = AZStd::GetMin(511, (int)floorf(bf / scale + 0.5f));
}
};
float RgbE::MAX_RGB9E5 = (((float)MAX_RGB9E5_MANTISSA) / RGB9E5_MANTISSA_VALUES * (1 << MAX_RGB9E5_EXP));
//ePixelFormat_R8G8B8A8
class PixelOperationR8G8B8A8
: public IPixelOperation
{
void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
{
const uint8* data = buf;
r = U8ToF32(data[0]);
g = U8ToF32(data[1]);
b = U8ToF32(data[2]);
a = U8ToF32(data[3]);
}
void SetRGBA(uint8* buf, const float& r, const float& g, const float& b, const float& a) override
{
uint8* data = buf;
data[0] = F32ToU8(r);
data[1] = F32ToU8(g);
data[2] = F32ToU8(b);
data[3] = F32ToU8(a);
}
};
//ePixelFormat_R8G8B8X8
class PixelOperationR8G8B8X8
: public IPixelOperation
{
void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
{
const uint8* data = buf;
r = U8ToF32(data[0]);
g = U8ToF32(data[1]);
b = U8ToF32(data[2]);
a = 1.f;
}
void SetRGBA(uint8* buf, const float& r, const float& g, const float& b, [[maybe_unused]] const float& a) override
{
uint8* data = buf;
data[0] = F32ToU8(r);
data[1] = F32ToU8(g);
data[2] = F32ToU8(b);
data[3] = 0xff;
}
};
//ePixelFormat_B8G8R8A8
class PixelOperationB8G8R8A8
: public IPixelOperation
{
void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
{
const uint8* data = buf;
r = U8ToF32(data[2]);
g = U8ToF32(data[1]);
b = U8ToF32(data[0]);
a = U8ToF32(data[3]);
}
void SetRGBA(uint8* buf, const float& r, const float& g, const float& b, const float& a) override
{
uint8* data = buf;
data[0] = F32ToU8(b);
data[1] = F32ToU8(g);
data[2] = F32ToU8(r);
data[3] = F32ToU8(a);
}
};
//ePixelFormat_R8G8B8
class PixelOperationR8G8B8
: public IPixelOperation
{
void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
{
const uint8* data = buf;
r = U8ToF32(data[0]);
g = U8ToF32(data[1]);
b = U8ToF32(data[2]);
a = 1.0f;
}
void SetRGBA(uint8* buf, const float& r, const float& g, const float& b, [[maybe_unused]] const float& a) override
{
uint8* data = buf;
data[0] = F32ToU8(r);
data[1] = F32ToU8(g);
data[2] = F32ToU8(b);
}
};
//ePixelFormat_B8G8R8
class PixelOperationB8G8R8
: public IPixelOperation
{
void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
{
const uint8* data = buf;
r = U8ToF32(data[2]);
g = U8ToF32(data[1]);
b = U8ToF32(data[0]);
a = 1.0f;
}
void SetRGBA(uint8* buf, const float& r, const float& g, const float& b, [[maybe_unused]] const float& a) override
{
uint8* data = buf;
data[0] = F32ToU8(b);
data[1] = F32ToU8(g);
data[2] = F32ToU8(r);
}
};
//ePixelFormat_R8G8
class PixelOperationR8G8
: public IPixelOperation
{
void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
{
const uint8* data = buf;
r = U8ToF32(data[0]);
g = U8ToF32(data[1]);
b = 0.f;
a = 1.f;
}
void SetRGBA(uint8* buf, const float& r, const float& g, [[maybe_unused]] const float& b, [[maybe_unused]] const float& a) override
{
uint8* data = buf;
data[0] = F32ToU8(r);
data[1] = F32ToU8(g);
}
};
//ePixelFormat_R8
class PixelOperationR8
: public IPixelOperation
{
void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
{
const uint8* data = buf;
r = U8ToF32(data[0]);
g = 0.f;
b = 0.f;
a = 1.f;
}
void SetRGBA(uint8* buf, const float& r, [[maybe_unused]] const float& g, [[maybe_unused]] const float& b, [[maybe_unused]] const float& a) override
{
uint8* data = buf;
data[0] = F32ToU8(r);
}
};
//ePixelFormat_A8
class PixelOperationA8
: public IPixelOperation
{
void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
{
const uint8* data = buf;
a = U8ToF32(data[0]);
//save alpha information to rgb too. useful for preview.
r = a;
g = a;
b = a;
}
void SetRGBA(uint8* buf, [[maybe_unused]] const float& r, [[maybe_unused]] const float& g, [[maybe_unused]] const float& b, const float& a) override
{
uint8* data = buf;
data[0] = F32ToU8(a);
}
};
//ePixelFormat_R16G16B16A16
class PixelOperationR16G16B16A16
: public IPixelOperation
{
void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
{
const uint16* data = (uint16*)(buf);
r = U16ToF32(data[0]);
g = U16ToF32(data[1]);
b = U16ToF32(data[2]);
a = U16ToF32(data[3]);
}
void SetRGBA(uint8* buf, const float& r, const float& g, const float& b, const float& a) override
{
uint16* data = (uint16*)(buf);
data[0] = F32ToU16(r);
data[1] = F32ToU16(g);
data[2] = F32ToU16(b);
data[3] = F32ToU16(a);
}
};
//ePixelFormat_R16G16
class PixelOperationR16G16
: public IPixelOperation
{
void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
{
const uint16* data = (uint16*)(buf);
r = U16ToF32(data[0]);
g = U16ToF32(data[1]);
b = 0.f;
a = 1.f;
}
void SetRGBA(uint8* buf, const float& r, const float& g, [[maybe_unused]] const float& b, [[maybe_unused]] const float& a) override
{
uint16* data = (uint16*)(buf);
data[0] = F32ToU16(r);
data[1] = F32ToU16(g);
}
};
//ePixelFormat_R16
class PixelOperationR16
: public IPixelOperation
{
void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
{
const uint16* data = (uint16*)(buf);
r = U16ToF32(data[0]);
g = 0.f;
b = 0.f;
a = 1.f;
}
void SetRGBA(uint8* buf, const float& r, [[maybe_unused]] const float& g, [[maybe_unused]] const float& b, [[maybe_unused]] const float& a) override
{
uint16* data = (uint16*)(buf);
data[0] = F32ToU16(r);
}
};
//ePixelFormat_R9G9B9E5
class PixelOperationR9G9B9E5
: public IPixelOperation
{
void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
{
const RgbE* data = (RgbE*)(buf);
data->GetRGBF(r, g, b);
a = 1.f;
}
void SetRGBA(uint8* buf, const float& r, const float& g, const float& b, [[maybe_unused]] const float& a) override
{
RgbE* data = (RgbE*)(buf);
data->SetRGBF(r, g, b);
}
};
//ePixelFormat_R32G32B32A32F
class PixelOperationR32G32B32A32F
: public IPixelOperation
{
public:
void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
{
const float* data = (float*)(buf);
r = data[0];
g = data[1];
b = data[2];
a = data[3];
}
void SetRGBA(uint8* buf, const float& r, const float& g, const float& b, const float& a) override
{
float* data = (float*)(buf);
data[0] = r;
data[1] = g;
data[2] = b;
data[3] = a;
}
};
//ePixelFormat_R32G32F
class PixelOperationR32G32F
: public IPixelOperation
{
public:
void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
{
const float* data = (float*)(buf);
r = data[0];
g = data[1];
b = 0.f;
a = 1.f;
}
void SetRGBA(uint8* buf, const float& r, const float& g, [[maybe_unused]] const float& b, [[maybe_unused]] const float& a) override
{
float* data = (float*)(buf);
data[0] = r;
data[1] = g;
}
};
//ePixelFormat_R32F
class PixelOperationR32F
: public IPixelOperation
{
public:
void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
{
const float* data = (float*)(buf);
r = data[0];
g = 0.f;
b = 0.f;
a = 1.f;
}
void SetRGBA(uint8* buf, const float& r, [[maybe_unused]] const float& g, [[maybe_unused]] const float& b, [[maybe_unused]] const float& a) override
{
float* data = (float*)(buf);
data[0] = r;
}
};
//ePixelFormat_R16G16B16A16F
class PixelOperationR16G16B16A16F
: public IPixelOperation
{
public:
void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
{
const SHalf* data = (SHalf*)(buf);
r = data[0];
g = data[1];
b = data[2];
a = data[3];
}
void SetRGBA(uint8* buf, const float& r, const float& g, const float& b, const float& a) override
{
SHalf* data = (SHalf*)(buf);
data[0] = SHalf(r);
data[1] = SHalf(g);
data[2] = SHalf(b);
data[3] = SHalf(a);
}
};
//ePixelFormat_R16G16F
class PixelOperationR16G16F
: public IPixelOperation
{
public:
void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
{
const SHalf* data = (SHalf*)(buf);
r = data[0];
g = data[1];
b = 0.f;
a = 1.f;
}
void SetRGBA(uint8* buf, const float& r, const float& g, [[maybe_unused]] const float& b, [[maybe_unused]] const float& a) override
{
SHalf* data = (SHalf*)(buf);
data[0] = SHalf(r);
data[1] = SHalf(g);
}
};
//ePixelFormat_R16F
class PixelOperationR16F
: public IPixelOperation
{
public:
void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
{
const SHalf* data = (SHalf*)(buf);
r = data[0];
g = 0.f;
b = 0.f;
a = 1.f;
}
void SetRGBA(uint8* buf, const float& r, [[maybe_unused]] const float& g, [[maybe_unused]] const float& b, [[maybe_unused]] const float& a) override
{
SHalf* data = (SHalf*)(buf);
data[0] = SHalf(r);
}
};
IPixelOperationPtr CreatePixelOperation(EPixelFormat pixelFmt)
{
switch (pixelFmt)
{
case ePixelFormat_R8G8B8A8:
return AZStd::make_shared<PixelOperationR8G8B8A8>();
case ePixelFormat_R8G8B8X8:
return AZStd::make_shared<PixelOperationR8G8B8X8>();
case ePixelFormat_B8G8R8A8:
return AZStd::make_shared<PixelOperationB8G8R8A8>();
case ePixelFormat_B8G8R8:
return AZStd::make_shared<PixelOperationB8G8R8>();
case ePixelFormat_R8G8B8:
return AZStd::make_shared<PixelOperationR8G8B8>();
case ePixelFormat_R8G8:
return AZStd::make_shared<PixelOperationR8G8>();
case ePixelFormat_R8:
return AZStd::make_shared<PixelOperationR8>();
case ePixelFormat_A8:
return AZStd::make_shared<PixelOperationA8>();
case ePixelFormat_R16G16B16A16:
return AZStd::make_shared<PixelOperationR16G16B16A16>();
case ePixelFormat_R16G16:
return AZStd::make_shared<PixelOperationR16G16>();
case ePixelFormat_R16:
return AZStd::make_shared<PixelOperationR16>();
case ePixelFormat_R9G9B9E5:
return AZStd::make_shared<PixelOperationR9G9B9E5>();
case ePixelFormat_R32G32B32A32F:
return AZStd::make_shared<PixelOperationR32G32B32A32F>();
case ePixelFormat_R32G32F:
return AZStd::make_shared<PixelOperationR32G32F>();
case ePixelFormat_R32F:
return AZStd::make_shared<PixelOperationR32F>();
case ePixelFormat_R16G16B16A16F:
return AZStd::make_shared<PixelOperationR16G16B16A16F>();
case ePixelFormat_R16G16F:
return AZStd::make_shared<PixelOperationR16G16F>();
case ePixelFormat_R16F:
return AZStd::make_shared<PixelOperationR16F>();
default:
AZ_Assert(false, "This function should be only called for uncompressed pixel format");
break;
}
return nullptr;
}
} // namespace ImageProcessingAtom