Files
o3de/Gems/Atom/Utils/Code/Tests/PngFileTests.cpp
T
Artur K 56904d1799 Cry Legacy: the Lessening (#5961)
* Generic Cleanup

Removals:

* Unused cvars,member variables and defines
* Windows media center edition support routines
* CSystem - removed debug_GetCallStackRaw/GetRootWindowMessageHandler/
UnloadDLL/ShutdownModuleLibraries

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* Fix CryMessageBox return values in Windows build

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* Remove named selection group and a few smaller unused functionalities

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* Remove ObjectManager export functionality + 2 deprecated functions

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* Remove object legacy freeze/hide support from ObjectManager

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* Remove ObjectManager duplicate name dection, as well as object renaming

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* Remove ObjectManager serialization and selection callbacks

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* ObjectManager - remove MoveObjects/HitTestObject/EndEditParams and related members/functions

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* Remove a bunch of unused clone related functionality + misc

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* ObjectManager - misc removals

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* ObjectManager - more removals

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* Remove unused object selection/retrieval in ObjectManager and LoadObjects in ObjectLoader

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* CBaseObject - remove unused material layers mask support

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* More CBaseObject cleanups.

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* CBaseObject - remove SubObj functions and IMouseCreateCallback

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* Remove unused procedural floor management, helper scale and tags.

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* CBaseObject - remove more unused methods.

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* Removals in multiple places.

CBaseObject - unused `OBJTYPE_DUMMY` flag removed, member `IsSameClass`
CObjectArchive removed unused methods/members
CObjectManager removed unused `Update` method

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* More removals

IDisplayViewport - HitTestLine/GetGridStep/setHitcontext/
GetConstructionPlane
Unused Cry_Matrix44 template specializations.

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* O3DE.exe Project-Centric "Open Editor" fix (#5852)

* The O3DE.exe Open Editor button now attempts to open the Editor in the
build directory of the project being opened.

If their is no Editor within the build directory of the Project, it uses
the Editor.exe in the current O3DE.exe executable directory if it exists

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Engine .gitignore now ignores the build directory if placed in the
AutomatedTesting project

Previously it was just ignoring a `[Bb]uild` directory if it was
directly within the engine root.
This change matches the behavior of the project templates.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Renamed the ProjectUtils GetEditorDirectory function to
GetEditorExecutablePath

Added a platform specific implementation for retrieving the path to the
Editor executable in the GetEditorExectuablePath function.

It first attempts to locate the Editor via checking the project build
directory for an Editor executable before falling back to checking the
binary directory of the currently running O3DE executable.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Correct the MacOS GetEditorExecutablePath to return the Editor path

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>
Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* Adding missing C++20 std::erase implementations (#5735)

There were already implementations for std::erase_if.
This adds the counterpart AZStd::erase versions

resolves #5734

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>
Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* More removals and refactors

`ShowMessage` and `CryMessageBox` return void now
Simplify code in `CSystem::WarningV`
Remove unused `CryGetTicksPerSec`
Remove unused WinBase functionality
Replace `nanosleep` call in WinBase with `std::this_thread::sleep_for`
Remove unused Win32Wrapper routines
Remove unused IFunctorBase.h and IEntityObjectListener.h
Fix VectorAndArray.cpp compilation
Use QMessageBox instead of CryMessageBox in the editor.
Remove empty ArchiveVars platform specific files

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* Fix test code.

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* Remove an unused  function

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* CBaseObject and undo description removals.

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* A bunch of removals

* Remove IRenderNode
* Remove editor's KDTree
* Remove StatObjBus, InstanceStatObjEventBus::ReleaseData is redundant (
same functionality in the only available handler is triggered by
`OnCryEditorCloseScene`)
* Remove CExportManager::AddStatObj/AddMeshes/AddMesh,
IExportManager::ExportSingleStatObj
* Remove CIconManager/IIconManager::GetObject
* Remove CBaseObject::IntersectRayMesh
* Remove IIndexedMesh and related structs.
* Unused IUndoObject::GetDescription and all derived implementations.
* Unused CUndoBaseLibrary/CUndoBaseLibraryManager
* Unused Matrix34_tpl typedefs
* Legacy Xml classes cleanup

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* Remove unused `Vec3ToVector3D` helper

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* fix - remove unused static variable

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

Co-authored-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>
2021-11-29 11:21:06 -07:00

315 lines
13 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/Utils/PngFile.h>
#include <AzCore/UnitTest/TestTypes.h>
#include <AzCore/Math/Color.h>
#include <AzCore/std/containers/array.h>
#include <AzCore/IO/FileIO.h>
#include <AzCore/IO/Path/Path.h>
#include <AzFramework/IO/LocalFileIO.h>
namespace UnitTest
{
using namespace AZ::Utils;
class PngFileTests
: public AllocatorsFixture
{
protected:
AZ::IO::Path m_testImageFolder;
AZ::IO::Path m_tempPngFilePath;
AZStd::vector<uint8_t> m_primaryColors3x1;
AZStd::unique_ptr<AZ::IO::FileIOBase> m_localFileIO;
void SetUp() override
{
AllocatorsFixture::SetUp();
m_testImageFolder =
AZ::IO::Path(AZ::Test::GetEngineRootPath(), '/') / AZ::IO::Path("Gems/Atom/Utils/Code/Tests/PngTestImages");
m_tempPngFilePath = m_testImageFolder / "temp.png";
m_localFileIO.reset(aznew AZ::IO::LocalFileIO());
AZ::IO::FileIOBase::SetInstance(m_localFileIO.get());
AZ::IO::FileIOBase::GetInstance()->Remove(m_tempPngFilePath.c_str());
m_primaryColors3x1 = {
255u, 0u, 0u, 255u,
0u, 255u, 0u, 255u,
0u, 0u, 255u, 255u
};
}
void TearDown() override
{
m_testImageFolder = AZ::IO::Path{};
m_tempPngFilePath = AZ::IO::Path{};
m_primaryColors3x1 = AZStd::vector<uint8_t>{};
AZ::IO::FileIOBase::SetInstance(nullptr);
m_localFileIO.reset();
AllocatorsFixture::TearDown();
}
struct Color3 : public AZStd::array<uint8_t, 3>
{
using Base = AZStd::array<uint8_t, 3>;
Color3(uint8_t r, uint8_t g, uint8_t b) : Base({r, g, b}) {}
Color3(const uint8_t* raw) : Base({raw[0], raw[1], raw[2]}) {}
};
struct Color4 : public AZStd::array<uint8_t, 4>
{
using Base = AZStd::array<uint8_t, 4>;
Color4(uint8_t r, uint8_t g, uint8_t b, uint8_t a) : Base({r, g, b, a}) {}
Color4(const uint8_t* raw) : Base({raw[0], raw[1], raw[2], raw[3]}) {}
};
};
TEST_F(PngFileTests, LoadRgb)
{
PngFile image = PngFile::Load((m_testImageFolder / "ColorChart_rgb.png").c_str());
EXPECT_TRUE(image.IsValid());
EXPECT_EQ(image.GetBufferFormat(), PngFile::Format::RGB);
EXPECT_EQ(image.GetWidth(), 3);
EXPECT_EQ(image.GetHeight(), 2);
EXPECT_EQ(image.GetBuffer().size(), 18);
EXPECT_EQ(Color3(image.GetBuffer().begin() + 0), Color3(255u, 0u, 0u));
EXPECT_EQ(Color3(image.GetBuffer().begin() + 3), Color3(0u, 255u, 0u));
EXPECT_EQ(Color3(image.GetBuffer().begin() + 6), Color3(0u, 0u, 255u));
EXPECT_EQ(Color3(image.GetBuffer().begin() + 9), Color3(255u, 255u, 0u));
EXPECT_EQ(Color3(image.GetBuffer().begin() + 12), Color3(0u, 255u, 255u));
EXPECT_EQ(Color3(image.GetBuffer().begin() + 15), Color3(255u, 0u, 255u));
}
TEST_F(PngFileTests, LoadRgba)
{
PngFile image = PngFile::Load((m_testImageFolder / "ColorChart_rgba.png").c_str());
EXPECT_TRUE(image.IsValid());
EXPECT_EQ(image.GetBufferFormat(), PngFile::Format::RGBA);
EXPECT_EQ(image.GetWidth(), 3);
EXPECT_EQ(image.GetHeight(), 2);
EXPECT_EQ(image.GetBuffer().size(), 24);
EXPECT_EQ(Color4(image.GetBuffer().begin() + 0), Color4(255u, 0u, 0u, 200u));
EXPECT_EQ(Color4(image.GetBuffer().begin() + 4), Color4(0u, 255u, 0u, 150u));
EXPECT_EQ(Color4(image.GetBuffer().begin() + 8), Color4(0u, 0u, 255u, 100u));
EXPECT_EQ(Color4(image.GetBuffer().begin() + 12), Color4(255u, 255u, 0u, 125u));
EXPECT_EQ(Color4(image.GetBuffer().begin() + 16), Color4(0u, 255u, 255u, 175u));
EXPECT_EQ(Color4(image.GetBuffer().begin() + 20), Color4(255u, 0u, 255u, 75u));
}
TEST_F(PngFileTests, LoadRgbaStripAlpha)
{
PngFile::LoadSettings loadSettings;
loadSettings.m_stripAlpha = true;
PngFile image = PngFile::Load((m_testImageFolder / "ColorChart_rgba.png").c_str(), loadSettings);
// Note these checks are identical to the LoadRgb test.
EXPECT_TRUE(image.IsValid());
EXPECT_EQ(image.GetBufferFormat(), PngFile::Format::RGB);
EXPECT_EQ(image.GetWidth(), 3);
EXPECT_EQ(image.GetHeight(), 2);
EXPECT_EQ(image.GetBuffer().size(), 18);
EXPECT_EQ(Color3(image.GetBuffer().begin() + 0), Color3(255u, 0u, 0u));
EXPECT_EQ(Color3(image.GetBuffer().begin() + 3), Color3(0u, 255u, 0u));
EXPECT_EQ(Color3(image.GetBuffer().begin() + 6), Color3(0u, 0u, 255u));
EXPECT_EQ(Color3(image.GetBuffer().begin() + 9), Color3(255u, 255u, 0u));
EXPECT_EQ(Color3(image.GetBuffer().begin() + 12), Color3(0u, 255u, 255u));
EXPECT_EQ(Color3(image.GetBuffer().begin() + 15), Color3(255u, 0u, 255u));
}
TEST_F(PngFileTests, LoadColorPaletteTwoBits)
{
PngFile image = PngFile::Load((m_testImageFolder / "ColorPalette_2bit.png").c_str());
EXPECT_TRUE(image.IsValid());
EXPECT_EQ(image.GetBufferFormat(), PngFile::Format::RGB);
EXPECT_EQ(image.GetWidth(), 1);
EXPECT_EQ(image.GetHeight(), 3);
EXPECT_EQ(image.GetBuffer().size(), 9);
EXPECT_EQ(Color3(image.GetBuffer().begin() + 0), Color3(255u, 0u, 0u));
EXPECT_EQ(Color3(image.GetBuffer().begin() + 3), Color3(0u, 255u, 0u));
EXPECT_EQ(Color3(image.GetBuffer().begin() + 6), Color3(0u, 0u, 255u));
}
TEST_F(PngFileTests, LoadGrayscaleOneBit)
{
PngFile image = PngFile::Load((m_testImageFolder / "GrayPalette_1bit.png").c_str());
EXPECT_TRUE(image.IsValid());
EXPECT_EQ(image.GetBufferFormat(), PngFile::Format::RGB);
EXPECT_EQ(image.GetWidth(), 1);
EXPECT_EQ(image.GetHeight(), 2);
EXPECT_EQ(image.GetBuffer().size(), 6);
EXPECT_EQ(Color3(image.GetBuffer().begin() + 0), Color3(0u, 0u, 0u));
EXPECT_EQ(Color3(image.GetBuffer().begin() + 3), Color3(255u, 255u, 255u));
}
TEST_F(PngFileTests, LoadRgba64Bits)
{
PngFile image = PngFile::Load((m_testImageFolder / "Gradient_rgb_16bpc.png").c_str());
EXPECT_TRUE(image.IsValid());
EXPECT_EQ(image.GetBufferFormat(), PngFile::Format::RGB);
EXPECT_EQ(image.GetWidth(), 5);
EXPECT_EQ(image.GetHeight(), 1);
EXPECT_EQ(image.GetBuffer().size(), 15);
// The values in this file are 30.0f, 30.1f, 30.2f, 30.3f, 30.4f. But we use PNG_TRANSFORM_STRIP_16 to reduce them to 8 bits per channel for simplicity.
EXPECT_EQ(Color3(image.GetBuffer().begin() + 0), Color3(76u, 0u, 0u));
EXPECT_EQ(Color3(image.GetBuffer().begin() + 3), Color3(77u, 0u, 0u));
EXPECT_EQ(Color3(image.GetBuffer().begin() + 6), Color3(77u, 0u, 0u));
EXPECT_EQ(Color3(image.GetBuffer().begin() + 9), Color3(77u, 0u, 0u));
EXPECT_EQ(Color3(image.GetBuffer().begin() + 12), Color3(77u, 0u, 0u));
}
TEST_F(PngFileTests, CreateCopy)
{
AZStd::vector<uint8_t> data = m_primaryColors3x1;
PngFile savedImage = PngFile::Create(AZ::RHI::Size{3, 1, 0}, AZ::RHI::Format::R8G8B8A8_UNORM, data);
EXPECT_TRUE(savedImage.IsValid());
EXPECT_EQ(savedImage.GetWidth(), 3);
EXPECT_EQ(savedImage.GetHeight(), 1);
EXPECT_EQ(savedImage.GetBuffer(), data);
}
TEST_F(PngFileTests, CreateMove)
{
AZStd::vector<uint8_t> data = m_primaryColors3x1;
PngFile savedImage = PngFile::Create(AZ::RHI::Size{3, 1, 0}, AZ::RHI::Format::R8G8B8A8_UNORM, AZStd::move(data));
EXPECT_TRUE(savedImage.IsValid());
EXPECT_EQ(savedImage.GetWidth(), 3);
EXPECT_EQ(savedImage.GetHeight(), 1);
EXPECT_EQ(savedImage.GetBuffer(), m_primaryColors3x1);
EXPECT_TRUE(data.empty()); // The data should have been moved
}
TEST_F(PngFileTests, SaveRgba)
{
PngFile savedImage = PngFile::Create(AZ::RHI::Size{3, 1, 0}, AZ::RHI::Format::R8G8B8A8_UNORM, m_primaryColors3x1);
bool result = savedImage.Save(m_tempPngFilePath.c_str());
EXPECT_TRUE(result);
PngFile loadedImage = PngFile::Load(m_tempPngFilePath.c_str());
EXPECT_TRUE(loadedImage.IsValid());
EXPECT_EQ(loadedImage.GetBufferFormat(), savedImage.GetBufferFormat());
EXPECT_EQ(loadedImage.GetWidth(), savedImage.GetWidth());
EXPECT_EQ(loadedImage.GetHeight(), savedImage.GetHeight());
EXPECT_EQ(loadedImage.GetBuffer(), savedImage.GetBuffer());
}
TEST_F(PngFileTests, SaveRgbaStripAlpha)
{
PngFile savedImage = PngFile::Create(AZ::RHI::Size{3, 1, 0}, AZ::RHI::Format::R8G8B8A8_UNORM, m_primaryColors3x1);
PngFile::SaveSettings saveSettings;
saveSettings.m_stripAlpha = true;
bool result = savedImage.Save(m_tempPngFilePath.c_str(), saveSettings);
EXPECT_TRUE(result);
// The alpha was stripped when saving. Now we load the data without stripping anything and should find
// that there is no alpha channel.
PngFile loadedImage = PngFile::Load(m_tempPngFilePath.c_str());
// The dimensions are the same...
EXPECT_TRUE(loadedImage.IsValid());
EXPECT_EQ(loadedImage.GetWidth(), savedImage.GetWidth());
EXPECT_EQ(loadedImage.GetHeight(), savedImage.GetHeight());
// ... but the format is different
EXPECT_NE(loadedImage.GetBufferFormat(), savedImage.GetBufferFormat());
EXPECT_EQ(loadedImage.GetBufferFormat(), PngFile::Format::RGB);
// ... and the loaded data is smaller
EXPECT_NE(loadedImage.GetBuffer(), savedImage.GetBuffer());
EXPECT_EQ(Color3(loadedImage.GetBuffer().begin() + 0), Color3(255u, 0u, 0u));
EXPECT_EQ(Color3(loadedImage.GetBuffer().begin() + 3), Color3(0u, 255u, 0u));
EXPECT_EQ(Color3(loadedImage.GetBuffer().begin() + 6), Color3(0u, 0u, 255u));
}
TEST_F(PngFileTests, Error_CreateUnsupportedFormat)
{
AZStd::vector<uint8_t> data = m_primaryColors3x1;
AZStd::string gotErrorMessage;
PngFile savedImage = PngFile::Create(AZ::RHI::Size{3, 1, 0}, AZ::RHI::Format::R32_UINT, data,
[&gotErrorMessage](const char* errorMessage) { gotErrorMessage = errorMessage; });
EXPECT_FALSE(savedImage.IsValid());
EXPECT_TRUE(gotErrorMessage.find("unsupported format R32_UINT") != AZStd::string::npos);
}
TEST_F(PngFileTests, Error_CreateIncorrectBufferSize)
{
AZStd::vector<uint8_t> data = m_primaryColors3x1;
AZStd::string gotErrorMessage;
PngFile savedImage = PngFile::Create(AZ::RHI::Size{3, 2, 0}, AZ::RHI::Format::R8G8B8A8_UNORM, data,
[&gotErrorMessage](const char* errorMessage) { gotErrorMessage = errorMessage; });
EXPECT_FALSE(savedImage.IsValid());
EXPECT_TRUE(gotErrorMessage.find("does not match") != AZStd::string::npos);
}
TEST_F(PngFileTests, Error_LoadFileNotFound)
{
AZStd::string gotErrorMessage;
PngFile::LoadSettings loadSettings;
loadSettings.m_errorHandler = [&gotErrorMessage](const char* errorMessage) { gotErrorMessage = errorMessage; };
PngFile image = PngFile::Load((m_testImageFolder / "DoesNotExist.png").c_str(), loadSettings);
EXPECT_FALSE(image.IsValid());
EXPECT_TRUE(gotErrorMessage.find("not open file") != AZStd::string::npos);
}
TEST_F(PngFileTests, Error_LoadEmptyFile)
{
AZStd::string gotErrorMessage;
PngFile::LoadSettings loadSettings;
loadSettings.m_errorHandler = [&gotErrorMessage](const char* errorMessage) { gotErrorMessage = errorMessage; };
PngFile image = PngFile::Load((m_testImageFolder / "EmptyFile.png").c_str(), loadSettings);
EXPECT_FALSE(image.IsValid());
EXPECT_TRUE(gotErrorMessage.find("Invalid png header") != AZStd::string::npos);
}
TEST_F(PngFileTests, Error_LoadNotPngFile)
{
AZStd::string gotErrorMessage;
PngFile::LoadSettings loadSettings;
loadSettings.m_errorHandler = [&gotErrorMessage](const char* errorMessage) { gotErrorMessage = errorMessage; };
PngFile image = PngFile::Load((m_testImageFolder / "ColorChart_rgba.jpg").c_str(), loadSettings);
EXPECT_FALSE(image.IsValid());
EXPECT_TRUE(gotErrorMessage.find("Invalid png header") != AZStd::string::npos);
}
TEST_F(PngFileTests, Error_SaveInvalidPngFile)
{
AZStd::string gotErrorMessage;
PngFile::SaveSettings saveSettings;
saveSettings.m_errorHandler = [&gotErrorMessage](const char* errorMessage) { gotErrorMessage = errorMessage; };
PngFile savedImage;
bool result = savedImage.Save(m_tempPngFilePath.c_str(), saveSettings);
EXPECT_FALSE(result);
EXPECT_TRUE(gotErrorMessage.find("PngFile is invalid") != AZStd::string::npos);
EXPECT_FALSE(AZ::IO::FileIOBase::GetInstance()->Exists(m_tempPngFilePath.c_str()));
}
}