Small refactor on ImageComparison utils. (#7133)

* Small refactor on ImageComparison utils.

Signed-off-by: hershey5045 <43485729+hershey5045@users.noreply.github.com>

* Add aznumeric cast.

Signed-off-by: hershey5045 <43485729+hershey5045@users.noreply.github.com>

* Correction on aznumeric cast.

Signed-off-by: hershey5045 <43485729+hershey5045@users.noreply.github.com>

* Add unit test for new image comparison function.

Signed-off-by: hershey5045 <43485729+hershey5045@users.noreply.github.com>
This commit is contained in:
hershey5045
2022-01-31 12:14:27 -08:00
committed by GitHub
parent 63ce88a3ce
commit c514e1b490
3 changed files with 24 additions and 8 deletions
@@ -14,6 +14,16 @@ namespace AZ
{
namespace Utils
{
int16_t CalcMaxChannelDifference(AZStd::array_view<uint8_t> bufferA, AZStd::array_view<uint8_t> bufferB, size_t index)
{
// We use the max error from a single channel instead of accumulating the error from each channel.
// This normalizes differences so that for example black vs red has the same weight as black vs yellow.
const int16_t diffR = static_cast<int16_t>(abs(aznumeric_cast<int16_t>(bufferA[index]) - aznumeric_cast<int16_t>(bufferB[index])));
const int16_t diffG = static_cast<int16_t>(abs(aznumeric_cast<int16_t>(bufferA[index + 1]) - aznumeric_cast<int16_t>(bufferB[index + 1])));
const int16_t diffB = static_cast<int16_t>(abs(aznumeric_cast<int16_t>(bufferA[index + 2]) - aznumeric_cast<int16_t>(bufferB[index + 2])));
return AZ::GetMax(AZ::GetMax(diffR, diffG), diffB);
}
ImageDiffResultCode CalcImageDiffRms(
AZStd::span<const uint8_t> bufferA, const RHI::Size& sizeA, RHI::Format formatA,
AZStd::span<const uint8_t> bufferB, const RHI::Size& sizeB, RHI::Format formatB,
@@ -67,14 +77,7 @@ namespace AZ
for (size_t i = 0; i < bufferA.size(); i += BytesPerPixel)
{
// We use the max error from a single channel instead of accumulating the error from each channel.
// This normalizes differences so that for example black vs red has the same weight as black vs yellow.
const int16_t diffR = static_cast<int16_t>(abs(aznumeric_cast<int16_t>(bufferA[i]) - aznumeric_cast<int16_t>(bufferB[i])));
const int16_t diffG = static_cast<int16_t>(abs(aznumeric_cast<int16_t>(bufferA[i + 1]) - aznumeric_cast<int16_t>(bufferB[i + 1])));
const int16_t diffB = static_cast<int16_t>(abs(aznumeric_cast<int16_t>(bufferA[i + 2]) - aznumeric_cast<int16_t>(bufferB[i + 2])));
const int16_t maxDiff = AZ::GetMax(AZ::GetMax(diffR, diffG), diffB);
const float finalDiffNormalized = maxDiff / 255.0f;
const float finalDiffNormalized = aznumeric_cast<float>(CalcMaxChannelDifference(bufferA, bufferB, i)) / 255.0f;
const float squared = finalDiffNormalized * finalDiffNormalized;
if (diffScore)