Merge pull request #7625 from aws-lumberyard-dev/cgalvan/AddSupportForUNORM_SRGB
Added support for several UNORM_SRGB formats
This commit is contained in:
@@ -360,18 +360,6 @@ void ReflectedPropertyControl::CreateItems(XmlNodeRef node, CVarBlockPtr& outBlo
|
||||
textureVar->Set(textureName);
|
||||
}
|
||||
}
|
||||
else if (!azstricmp(type, "color"))
|
||||
{
|
||||
CSmartVariable<Vec3> colorVar;
|
||||
AddVariable(group, colorVar, child->getTag(), humanReadableName.toUtf8().data(), strDescription.toUtf8().data(), func, pUserData, IVariable::DT_COLOR);
|
||||
ColorB color;
|
||||
if (child->getAttr("value", color))
|
||||
{
|
||||
ColorF colorLinear = ColorGammaToLinear(QColor(color.r, color.g, color.b));
|
||||
Vec3 colorVec3(colorLinear.r, colorLinear.g, colorLinear.b);
|
||||
colorVar->Set(colorVec3);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -243,9 +243,6 @@ void ReflectedPropertyItem::SetVariable(IVariable *var)
|
||||
case ePropertySelection:
|
||||
m_reflectedVarAdapter = new ReflectedVarEnumAdapter;
|
||||
break;
|
||||
case ePropertyColor:
|
||||
m_reflectedVarAdapter = new ReflectedVarColorAdapter;
|
||||
break;
|
||||
case ePropertyUser:
|
||||
m_reflectedVarAdapter = new ReflectedVarUserAdapter;
|
||||
break;
|
||||
|
||||
@@ -312,50 +312,6 @@ void ReflectedVarVector4Adapter::SyncIVarToReflectedVar(IVariable *pVariable)
|
||||
pVariable->Set(Vec4(m_reflectedVar->m_value.GetX(), m_reflectedVar->m_value.GetY(), m_reflectedVar->m_value.GetZ(), m_reflectedVar->m_value.GetW()));
|
||||
}
|
||||
|
||||
|
||||
void ReflectedVarColorAdapter::SetVariable(IVariable *pVariable)
|
||||
{
|
||||
m_reflectedVar.reset(new CReflectedVarColor(pVariable->GetHumanName().toUtf8().data()));
|
||||
m_reflectedVar->m_description = pVariable->GetDescription().toUtf8().data();
|
||||
}
|
||||
|
||||
void ReflectedVarColorAdapter::SyncReflectedVarToIVar(IVariable *pVariable)
|
||||
{
|
||||
if (pVariable->GetType() == IVariable::VECTOR)
|
||||
{
|
||||
Vec3 v(0, 0, 0);
|
||||
pVariable->Get(v);
|
||||
const QColor col = ColorLinearToGamma(ColorF(v.x, v.y, v.z));
|
||||
m_reflectedVar->m_color.Set(static_cast<float>(col.redF()), static_cast<float>(col.greenF()), static_cast<float>(col.blueF()));
|
||||
}
|
||||
else
|
||||
{
|
||||
int col(0);
|
||||
pVariable->Get(col);
|
||||
const QColor qcolor = ColorToQColor((uint32)col);
|
||||
m_reflectedVar->m_color.Set(static_cast<float>(qcolor.redF()), static_cast<float>(qcolor.greenF()), static_cast<float>(qcolor.blueF()));
|
||||
}
|
||||
}
|
||||
|
||||
void ReflectedVarColorAdapter::SyncIVarToReflectedVar(IVariable *pVariable)
|
||||
{
|
||||
if (pVariable->GetType() == IVariable::VECTOR)
|
||||
{
|
||||
ColorF colLin = ColorGammaToLinear(QColor::fromRgbF(m_reflectedVar->m_color.GetX(), m_reflectedVar->m_color.GetY(), m_reflectedVar->m_color.GetZ()));
|
||||
pVariable->Set(Vec3(colLin.r, colLin.g, colLin.b));
|
||||
}
|
||||
else
|
||||
{
|
||||
int ir = static_cast<int>(m_reflectedVar->m_color.GetX() * 255.0f);
|
||||
int ig = static_cast<int>(m_reflectedVar->m_color.GetY() * 255.0f);
|
||||
int ib = static_cast<int>(m_reflectedVar->m_color.GetZ() * 255.0f);
|
||||
|
||||
pVariable->Set(static_cast<int>(RGB(ir, ig, ib)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ReflectedVarResourceAdapter::SetVariable(IVariable *pVariable)
|
||||
{
|
||||
m_reflectedVar.reset(new CReflectedVarResource(pVariable->GetHumanName().toUtf8().data()));
|
||||
|
||||
@@ -186,21 +186,6 @@ AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
|
||||
AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
|
||||
};
|
||||
|
||||
|
||||
class EDITOR_CORE_API ReflectedVarColorAdapter
|
||||
: public ReflectedVarAdapter
|
||||
{
|
||||
public:
|
||||
void SetVariable(IVariable* pVariable) override;
|
||||
void SyncReflectedVarToIVar(IVariable* pVariable) override;
|
||||
void SyncIVarToReflectedVar(IVariable* pVariable) override;
|
||||
CReflectedVar* GetReflectedVar() override { return m_reflectedVar.data(); }
|
||||
private:
|
||||
AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
|
||||
QScopedPointer<CReflectedVarColor > m_reflectedVar;
|
||||
AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
|
||||
};
|
||||
|
||||
class EDITOR_CORE_API ReflectedVarResourceAdapter
|
||||
: public ReflectedVarAdapter
|
||||
{
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
*/
|
||||
|
||||
// Qt
|
||||
#include <QColor>
|
||||
#include <CryCommon/Cry_Color.h>
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
QColor ColorLinearToGamma(ColorF col)
|
||||
{
|
||||
float r = clamp_tpl(col.r, 0.0f, 1.0f);
|
||||
float g = clamp_tpl(col.g, 0.0f, 1.0f);
|
||||
float b = clamp_tpl(col.b, 0.0f, 1.0f);
|
||||
float a = clamp_tpl(col.a, 0.0f, 1.0f);
|
||||
|
||||
r = (float)(r <= 0.0031308 ? (12.92 * r) : (1.055 * pow((double)r, 1.0 / 2.4) - 0.055));
|
||||
g = (float)(g <= 0.0031308 ? (12.92 * g) : (1.055 * pow((double)g, 1.0 / 2.4) - 0.055));
|
||||
b = (float)(b <= 0.0031308 ? (12.92 * b) : (1.055 * pow((double)b, 1.0 / 2.4) - 0.055));
|
||||
|
||||
return QColor(int(r * 255.0f), int(g * 255.0f), int(b * 255.0f), int(a * 255.0f));
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
ColorF ColorGammaToLinear(const QColor& col)
|
||||
{
|
||||
float r = (float)col.red() / 255.0f;
|
||||
float g = (float)col.green() / 255.0f;
|
||||
float b = (float)col.blue() / 255.0f;
|
||||
float a = (float)col.alpha() / 255.0f;
|
||||
|
||||
return ColorF((float)(r <= 0.04045 ? (r / 12.92) : pow(((double)r + 0.055) / 1.055, 2.4)),
|
||||
(float)(g <= 0.04045 ? (g / 12.92) : pow(((double)g + 0.055) / 1.055, 2.4)),
|
||||
(float)(b <= 0.04045 ? (b / 12.92) : pow(((double)b + 0.055) / 1.055, 2.4)), a);
|
||||
}
|
||||
|
||||
QColor ColorToQColor(uint32 color)
|
||||
{
|
||||
return QColor::fromRgbF((float)GetRValue(color) / 255.0f,
|
||||
(float)GetGValue(color) / 255.0f,
|
||||
(float)GetBValue(color) / 255.0f);
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <AzCore/Math/Color.h>
|
||||
|
||||
#include "EditorDefs.h"
|
||||
|
||||
@@ -184,9 +185,9 @@ QColor ColorLinearToGamma(ColorF col)
|
||||
float b = clamp_tpl(col.b, 0.0f, 1.0f);
|
||||
float a = clamp_tpl(col.a, 0.0f, 1.0f);
|
||||
|
||||
r = (float)(r <= 0.0031308 ? (12.92 * r) : (1.055 * pow((double)r, 1.0 / 2.4) - 0.055));
|
||||
g = (float)(g <= 0.0031308 ? (12.92 * g) : (1.055 * pow((double)g, 1.0 / 2.4) - 0.055));
|
||||
b = (float)(b <= 0.0031308 ? (12.92 * b) : (1.055 * pow((double)b, 1.0 / 2.4) - 0.055));
|
||||
r = AZ::Color::ConvertSrgbLinearToGamma(r);
|
||||
g = AZ::Color::ConvertSrgbLinearToGamma(g);
|
||||
b = AZ::Color::ConvertSrgbLinearToGamma(b);
|
||||
|
||||
return QColor(int(r * 255.0f), int(g * 255.0f), int(b * 255.0f), int(a * 255.0f));
|
||||
}
|
||||
@@ -199,9 +200,9 @@ ColorF ColorGammaToLinear(const QColor& col)
|
||||
float b = (float)col.blue() / 255.0f;
|
||||
float a = (float)col.alpha() / 255.0f;
|
||||
|
||||
return ColorF((float)(r <= 0.04045 ? (r / 12.92) : pow(((double)r + 0.055) / 1.055, 2.4)),
|
||||
(float)(g <= 0.04045 ? (g / 12.92) : pow(((double)g + 0.055) / 1.055, 2.4)),
|
||||
(float)(b <= 0.04045 ? (b / 12.92) : pow(((double)b + 0.055) / 1.055, 2.4)), a);
|
||||
return ColorF(AZ::Color::ConvertSrgbGammaToLinear(r),
|
||||
AZ::Color::ConvertSrgbGammaToLinear(g),
|
||||
AZ::Color::ConvertSrgbGammaToLinear(b), a);
|
||||
}
|
||||
|
||||
QColor ColorToQColor(uint32 color)
|
||||
|
||||
@@ -48,7 +48,6 @@ set(FILES
|
||||
Util/Image.cpp
|
||||
Util/ImageHistogram.h
|
||||
Util/Image.h
|
||||
Util/ColorUtils.cpp
|
||||
Undo/Undo.cpp
|
||||
Undo/IUndoManagerListener.h
|
||||
Undo/IUndoObject.h
|
||||
|
||||
@@ -134,6 +134,12 @@ namespace AZ
|
||||
//! Color from u32 => 0xAABBGGRR, RGB convert from Gamma corrected to Linear values.
|
||||
void FromU32GammaToLinear(u32 c);
|
||||
|
||||
//! Convert SRGB gamma space to linear space
|
||||
static float ConvertSrgbGammaToLinear(float x);
|
||||
|
||||
//! Convert SRGB linear space to gamma space
|
||||
static float ConvertSrgbLinearToGamma(float x);
|
||||
|
||||
//! Convert color from linear to gamma corrected space.
|
||||
Color LinearToGamma() const;
|
||||
|
||||
|
||||
@@ -370,6 +370,15 @@ namespace AZ
|
||||
*this = GammaToLinear();
|
||||
}
|
||||
|
||||
AZ_MATH_INLINE float Color::ConvertSrgbGammaToLinear(float x)
|
||||
{
|
||||
return x <= 0.04045 ? (x / 12.92f) : static_cast<float>(pow((static_cast<double>(x) + 0.055) / 1.055, 2.4));
|
||||
}
|
||||
|
||||
AZ_MATH_INLINE float Color::ConvertSrgbLinearToGamma(float x)
|
||||
{
|
||||
return x <= 0.0031308 ? 12.92f * x : static_cast<float>(1.055 * pow(static_cast<double>(x), 1.0 / 2.4) - 0.055);
|
||||
}
|
||||
|
||||
AZ_MATH_INLINE Color Color::LinearToGamma() const
|
||||
{
|
||||
@@ -377,9 +386,9 @@ namespace AZ
|
||||
float g = GetG();
|
||||
float b = GetB();
|
||||
|
||||
r = (r <= 0.0031308 ? 12.92f * r : static_cast<float>(1.055 * pow(static_cast<double>(r), 1.0 / 2.4) - 0.055));
|
||||
g = (g <= 0.0031308 ? 12.92f * g : static_cast<float>(1.055 * pow(static_cast<double>(g), 1.0 / 2.4) - 0.055));
|
||||
b = (b <= 0.0031308 ? 12.92f * b : static_cast<float>(1.055 * pow(static_cast<double>(b), 1.0 / 2.4) - 0.055));
|
||||
r = ConvertSrgbLinearToGamma(r);
|
||||
g = ConvertSrgbLinearToGamma(g);
|
||||
b = ConvertSrgbLinearToGamma(b);
|
||||
|
||||
return Color(r,g,b,GetA());
|
||||
}
|
||||
@@ -391,9 +400,9 @@ namespace AZ
|
||||
float g = GetG();
|
||||
float b = GetB();
|
||||
|
||||
return Color(r <= 0.04045 ? (r / 12.92f) : static_cast<float>(pow((static_cast<double>(r) + 0.055) / 1.055, 2.4)),
|
||||
g <= 0.04045 ? (g / 12.92f) : static_cast<float>(pow((static_cast<double>(g) + 0.055) / 1.055, 2.4)),
|
||||
b <= 0.04045 ? (b / 12.92f) : static_cast<float>(pow((static_cast<double>(b) + 0.055) / 1.055, 2.4)), GetA());
|
||||
return Color(ConvertSrgbGammaToLinear(r),
|
||||
ConvertSrgbGammaToLinear(g),
|
||||
ConvertSrgbGammaToLinear(b), GetA());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <Processing/PixelFormatInfo.h>
|
||||
#include <Processing/ImageFlags.h>
|
||||
#include <Atom/ImageProcessing/PixelFormats.h>
|
||||
#include <AzCore/Math/Color.h>
|
||||
|
||||
#include <Converters/FIR-Weights.h>
|
||||
#include <Converters/PixelOperation.h>
|
||||
@@ -118,19 +119,8 @@ namespace ImageProcessingAtom
|
||||
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);
|
||||
static FunctionLookupTable<1024> s_lutGammaToLinear(AZ::Color::ConvertSrgbGammaToLinear, 0.04045f, 0.00001f);
|
||||
static FunctionLookupTable<1024> s_lutLinearToGamma(AZ::Color::ConvertSrgbLinearToGamma, 0.05f, 0.00001f);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
#include <Atom/RPI.Public/RPIUtils.h>
|
||||
#include <Atom/RPI.Public/Shader/Shader.h>
|
||||
|
||||
#include <AzCore/Math/Color.h>
|
||||
#include <AzCore/std/containers/array.h>
|
||||
#include <AzFramework/Asset/AssetSystemBus.h>
|
||||
|
||||
namespace AZ
|
||||
@@ -105,6 +107,25 @@ namespace AZ
|
||||
return ((value - origMin) / (origMax - origMin)) * (scaledMax - scaledMin) + scaledMin;
|
||||
}
|
||||
|
||||
// Pre-compute a lookup table for converting SRGB gamma to linear
|
||||
// by specifying the AZ::u8 so we don't have to do the computation
|
||||
// when retrieving pixels
|
||||
using ConversionLookupTable = AZStd::array<float, 256>;
|
||||
ConversionLookupTable CreateSrgbGammaToLinearLookupTable()
|
||||
{
|
||||
ConversionLookupTable lookupTable;
|
||||
|
||||
for (size_t i = 0; i < lookupTable.array_size; ++i)
|
||||
{
|
||||
float srgbValue = i / static_cast<float>(std::numeric_limits<AZ::u8>::max());
|
||||
lookupTable[i] = AZ::Color::ConvertSrgbGammaToLinear(srgbValue);
|
||||
}
|
||||
|
||||
return lookupTable;
|
||||
}
|
||||
|
||||
static ConversionLookupTable s_SrgbGammaToLinearLookupTable = CreateSrgbGammaToLinearLookupTable();
|
||||
|
||||
float RetrieveFloatValue(const AZ::u8* mem, size_t index, AZ::RHI::Format format)
|
||||
{
|
||||
switch (format)
|
||||
@@ -113,12 +134,23 @@ namespace AZ
|
||||
case AZ::RHI::Format::A8_UNORM:
|
||||
case AZ::RHI::Format::R8G8_UNORM:
|
||||
case AZ::RHI::Format::R8G8B8A8_UNORM:
|
||||
case AZ::RHI::Format::A8B8G8R8_UNORM:
|
||||
{
|
||||
return mem[index] / static_cast<float>(std::numeric_limits<AZ::u8>::max());
|
||||
}
|
||||
case AZ::RHI::Format::R8_UNORM_SRGB:
|
||||
case AZ::RHI::Format::R8G8_UNORM_SRGB:
|
||||
case AZ::RHI::Format::R8G8B8A8_UNORM_SRGB:
|
||||
case AZ::RHI::Format::A8B8G8R8_UNORM_SRGB:
|
||||
{
|
||||
// Use a lookup table that takes an AZ::u8 instead of a float
|
||||
// for better performance
|
||||
return s_SrgbGammaToLinearLookupTable[mem[index]];
|
||||
}
|
||||
case AZ::RHI::Format::R8_SNORM:
|
||||
case AZ::RHI::Format::R8G8_SNORM:
|
||||
case AZ::RHI::Format::R8G8B8A8_SNORM:
|
||||
case AZ::RHI::Format::A8B8G8R8_SNORM:
|
||||
{
|
||||
// Scale the value from AZ::s8 min/max to -1 to 1
|
||||
// We need to treat -128 and -127 the same, so that we get a symmetric
|
||||
@@ -452,9 +484,15 @@ namespace AZ
|
||||
case AZ::RHI::Format::A8_UNORM:
|
||||
case AZ::RHI::Format::R8G8_UNORM:
|
||||
case AZ::RHI::Format::R8G8B8A8_UNORM:
|
||||
case AZ::RHI::Format::A8B8G8R8_UNORM:
|
||||
case AZ::RHI::Format::R8_UNORM_SRGB:
|
||||
case AZ::RHI::Format::R8G8_UNORM_SRGB:
|
||||
case AZ::RHI::Format::R8G8B8A8_UNORM_SRGB:
|
||||
case AZ::RHI::Format::A8B8G8R8_UNORM_SRGB:
|
||||
case AZ::RHI::Format::R8_SNORM:
|
||||
case AZ::RHI::Format::R8G8_SNORM:
|
||||
case AZ::RHI::Format::R8G8B8A8_SNORM:
|
||||
case AZ::RHI::Format::A8B8G8R8_SNORM:
|
||||
case AZ::RHI::Format::D16_UNORM:
|
||||
case AZ::RHI::Format::R16_UNORM:
|
||||
case AZ::RHI::Format::R16G16_UNORM:
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
|
||||
//#include "CustomizeKeyboardPage.h"
|
||||
|
||||
#include <AzCore/Math/Color.h>
|
||||
|
||||
#include <Util/EditorUtils.h>
|
||||
|
||||
#include <Editor/Resource.h>
|
||||
@@ -99,9 +101,9 @@ QColor ColorLinearToGamma(ColorF col)
|
||||
float g = clamp_tpl(col.g, 0.0f, 1.0f);
|
||||
float b = clamp_tpl(col.b, 0.0f, 1.0f);
|
||||
|
||||
r = (float)(r <= 0.0031308 ? (12.92 * r) : (1.055 * pow((double)r, 1.0 / 2.4) - 0.055));
|
||||
g = (float)(g <= 0.0031308 ? (12.92 * g) : (1.055 * pow((double)g, 1.0 / 2.4) - 0.055));
|
||||
b = (float)(b <= 0.0031308 ? (12.92 * b) : (1.055 * pow((double)b, 1.0 / 2.4) - 0.055));
|
||||
r = AZ::Color::ConvertSrgbLinearToGamma(r);
|
||||
g = AZ::Color::ConvertSrgbLinearToGamma(g);
|
||||
b = AZ::Color::ConvertSrgbLinearToGamma(b);
|
||||
|
||||
return QColor(int(r * 255.0f), int(g * 255.0f), int(b * 255.0f));
|
||||
}
|
||||
@@ -113,7 +115,7 @@ ColorF ColorGammaToLinear(const QColor& col)
|
||||
float g = (float)col.green() / 255.0f;
|
||||
float b = (float)col.blue() / 255.0f;
|
||||
|
||||
return ColorF((float)(r <= 0.04045 ? (r / 12.92) : pow(((double)r + 0.055) / 1.055, 2.4)),
|
||||
(float)(g <= 0.04045 ? (g / 12.92) : pow(((double)g + 0.055) / 1.055, 2.4)),
|
||||
(float)(b <= 0.04045 ? (b / 12.92) : pow(((double)b + 0.055) / 1.055, 2.4)));
|
||||
return ColorF(AZ::Color::ConvertSrgbGammaToLinear(r),
|
||||
AZ::Color::ConvertSrgbGammaToLinear(g),
|
||||
AZ::Color::ConvertSrgbGammaToLinear(b));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user