Changed Atom to Use LibPng instead of OpenImageIO
Merge pull request #4006 from aws-lumberyard-dev/Atom/santora/UseLibPng OpenImageIO is a huge library and overkill for Atom's needs, which is to just save and load png files at this time. Here we remove all dependence on OpenImageIO and use libpng instead. I introduced a PngFile wrapper class to assist with loading and saving png files. Note that there is a bit more work to do before merging these changes. I am still working on adding libpng to the 3rdParty package system. Currently these changes only reflect the addition of the Windows package. We'll make the other platform packages available before merging.monroegm-disable-blank-issue-2
commit
f765dc0942
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <AzCore/std/string/string.h>
|
||||
#include <AzCore/std/containers/vector.h>
|
||||
#include <AzCore/std/containers/unordered_map.h>
|
||||
#include <AtomCore/std/containers/array_view.h>
|
||||
#include <AzCore/std/functional.h>
|
||||
|
||||
#include <Atom/RHI.Reflect/Size.h>
|
||||
#include <Atom/RHI.Reflect/Format.h>
|
||||
|
||||
namespace AZ
|
||||
{
|
||||
namespace Utils
|
||||
{
|
||||
//! This is a light wrapper class for libpng, to load and save .png files.
|
||||
//! Functionality is limited, feel free to add more features as needed.
|
||||
class PngFile
|
||||
{
|
||||
public:
|
||||
using ErrorHandler = AZStd::function<void(const char*)>;
|
||||
|
||||
struct LoadSettings
|
||||
{
|
||||
ErrorHandler m_errorHandler{}; //!< optional callback function describing any errors that are encountered
|
||||
bool m_stripAlpha = false; //!< the alpha channel will be skipped, loading an RGBA image as RGB
|
||||
LoadSettings() {}; // clang errors out if this is not provided.
|
||||
};
|
||||
|
||||
struct SaveSettings
|
||||
{
|
||||
ErrorHandler m_errorHandler{}; //!< optional callback function describing any errors that are encountered
|
||||
bool m_stripAlpha = false; //!< the alpha channel will be skipped, saving an RGBA buffer as RGB
|
||||
int m_compressionLevel = 6; //!< this is the zlib compression level. See png_set_compression_level in png.h
|
||||
SaveSettings() {}; // clang errors out if this is not provided.
|
||||
};
|
||||
|
||||
// To keep things simple for now we limit all images to RGB and RGBA, 8 bits per channel.
|
||||
enum class Format
|
||||
{
|
||||
Unknown,
|
||||
RGB,
|
||||
RGBA
|
||||
};
|
||||
|
||||
//! @return the loaded PngFile or an invalid PngFile if there was an error.
|
||||
static PngFile Load(const char* path, LoadSettings loadSettings = {});
|
||||
|
||||
//! Create a PngFile from an RHI data buffer.
|
||||
//! @param size the dimensions of the image (m_depth is not used, assumed to be 1)
|
||||
//! @param format indicates the pixel format represented by @data. Only a limited set of formats are supported, see implementation.
|
||||
//! @param data the buffer of image data. The size of the buffer must match the @size and @format parameters.
|
||||
//! @param errorHandler optional callback function describing any errors that are encountered
|
||||
//! @return the created PngFile or an invalid PngFile if there was an error.
|
||||
static PngFile Create(const RHI::Size& size, RHI::Format format, AZStd::array_view<uint8_t> data, ErrorHandler errorHandler = {});
|
||||
static PngFile Create(const RHI::Size& size, RHI::Format format, AZStd::vector<uint8_t>&& data, ErrorHandler errorHandler = {});
|
||||
|
||||
PngFile() = default;
|
||||
AZ_DEFAULT_MOVE(PngFile)
|
||||
|
||||
//! @return true if the save operation was successful
|
||||
bool Save(const char* path, SaveSettings saveSettings = {});
|
||||
|
||||
bool IsValid() const;
|
||||
operator bool() const { return IsValid(); }
|
||||
|
||||
uint32_t GetWidth() const { return m_width; }
|
||||
uint32_t GetHeight() const { return m_height; }
|
||||
|
||||
Format GetBufferFormat() const { return m_bufferFormat; }
|
||||
const AZStd::vector<uint8_t>& GetBuffer() const { return m_buffer; }
|
||||
|
||||
//! Returns a r-value reference that can be moved. This will invalidate the PngFile.
|
||||
AZStd::vector<uint8_t>&& TakeBuffer();
|
||||
|
||||
private:
|
||||
AZ_DEFAULT_COPY(PngFile)
|
||||
|
||||
static const int HeaderSize = 8;
|
||||
|
||||
static void DefaultErrorHandler(const char* message);
|
||||
|
||||
uint32_t m_width = 0;
|
||||
uint32_t m_height = 0;
|
||||
int32_t m_bitDepth = 0;
|
||||
|
||||
Format m_bufferFormat = Format::Unknown;
|
||||
AZStd::vector<uint8_t> m_buffer;
|
||||
};
|
||||
}
|
||||
} // namespace AZ
|
||||
@ -0,0 +1,324 @@
|
||||
/*
|
||||
* 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 <png.h>
|
||||
|
||||
namespace AZ
|
||||
{
|
||||
namespace Utils
|
||||
{
|
||||
namespace
|
||||
{
|
||||
void PngImage_user_error_fn(png_structp png_ptr, png_const_charp error_msg)
|
||||
{
|
||||
PngFile::ErrorHandler* errorHandler = reinterpret_cast<PngFile::ErrorHandler*>(png_get_error_ptr(png_ptr));
|
||||
(*errorHandler)(error_msg);
|
||||
}
|
||||
|
||||
void PngImage_user_warning_fn(png_structp /*png_ptr*/, png_const_charp warning_msg)
|
||||
{
|
||||
AZ_Warning("PngFile", false, "%s", warning_msg);
|
||||
}
|
||||
}
|
||||
|
||||
PngFile PngFile::Create(const RHI::Size& size, RHI::Format format, AZStd::array_view<uint8_t> data, ErrorHandler errorHandler)
|
||||
{
|
||||
return Create(size, format, AZStd::vector<uint8_t>{data.begin(), data.end()}, errorHandler);
|
||||
}
|
||||
|
||||
PngFile PngFile::Create(const RHI::Size& size, RHI::Format format, AZStd::vector<uint8_t>&& data, ErrorHandler errorHandler)
|
||||
{
|
||||
if (!errorHandler)
|
||||
{
|
||||
errorHandler = [](const char* message) { DefaultErrorHandler(message); };
|
||||
}
|
||||
|
||||
PngFile image;
|
||||
|
||||
if (RHI::Format::R8G8B8A8_UNORM == format)
|
||||
{
|
||||
if (size.m_width * size.m_height * 4 == data.size())
|
||||
{
|
||||
image.m_width = size.m_width;
|
||||
image.m_height = size.m_height;
|
||||
image.m_bitDepth = 8;
|
||||
image.m_bufferFormat = PngFile::Format::RGBA;
|
||||
image.m_buffer = AZStd::move(data);
|
||||
}
|
||||
else
|
||||
{
|
||||
errorHandler("Invalid arguments. Buffer size does not match the image dimensions.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
errorHandler(AZStd::string::format("Cannot create PngFile with unsupported format %s", AZ::RHI::ToString(format)).c_str());
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
PngFile PngFile::Load(const char* path, LoadSettings loadSettings)
|
||||
{
|
||||
if (!loadSettings.m_errorHandler)
|
||||
{
|
||||
loadSettings.m_errorHandler = [path](const char* message) { DefaultErrorHandler(AZStd::string::format("Could not load file '%s'. %s", path, message).c_str()); };
|
||||
}
|
||||
|
||||
// For documentation of this code, see http://www.libpng.org/pub/png/libpng-1.4.0-manual.pdf chapter 3
|
||||
|
||||
FILE* fp = NULL;
|
||||
azfopen(&fp, path, "rb"); // return type differs across platforms so can't do inside if
|
||||
if (!fp)
|
||||
{
|
||||
loadSettings.m_errorHandler("Cannot open file.");
|
||||
return {};
|
||||
}
|
||||
|
||||
png_byte header[HeaderSize] = {};
|
||||
|
||||
if (fread(header, 1, HeaderSize, fp) != HeaderSize)
|
||||
{
|
||||
fclose(fp);
|
||||
loadSettings.m_errorHandler("Invalid png header.");
|
||||
return {};
|
||||
}
|
||||
|
||||
bool isPng = !png_sig_cmp(header, 0, HeaderSize);
|
||||
if (!isPng)
|
||||
{
|
||||
fclose(fp);
|
||||
loadSettings.m_errorHandler("Invalid png header.");
|
||||
return {};
|
||||
}
|
||||
|
||||
png_voidp user_error_ptr = &loadSettings.m_errorHandler;
|
||||
png_error_ptr user_error_fn = PngImage_user_error_fn;
|
||||
png_error_ptr user_warning_fn = PngImage_user_warning_fn;
|
||||
|
||||
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, user_error_ptr, user_error_fn, user_warning_fn);
|
||||
if (!png_ptr)
|
||||
{
|
||||
fclose(fp);
|
||||
loadSettings.m_errorHandler("png_create_read_struct failed.");
|
||||
return {};
|
||||
}
|
||||
|
||||
png_infop info_ptr = png_create_info_struct(png_ptr);
|
||||
if (!info_ptr)
|
||||
{
|
||||
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
|
||||
fclose(fp);
|
||||
loadSettings.m_errorHandler("png_create_info_struct failed.");
|
||||
return {};
|
||||
}
|
||||
|
||||
png_infop end_info = png_create_info_struct(png_ptr);
|
||||
if (!end_info)
|
||||
{
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
|
||||
fclose(fp);
|
||||
loadSettings.m_errorHandler("png_create_info_struct failed.");
|
||||
return {};
|
||||
}
|
||||
|
||||
AZ_PUSH_DISABLE_WARNING(4611, "-Wunknown-warning-option") // Disables "interaction between '_setjmp' and C++ object destruction is non-portable". See https://docs.microsoft.com/en-us/cpp/preprocessor/warning?view=msvc-160
|
||||
if (setjmp(png_jmpbuf(png_ptr)))
|
||||
{
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
|
||||
fclose(fp);
|
||||
// We don't report an error message here because the user_error_fn should have done that already.
|
||||
return {};
|
||||
}
|
||||
AZ_POP_DISABLE_WARNING
|
||||
|
||||
png_init_io(png_ptr, fp);
|
||||
|
||||
png_set_sig_bytes(png_ptr, HeaderSize);
|
||||
|
||||
png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER, NULL, 0);
|
||||
|
||||
// To keep things simple for now we limit all images to RGB and RGBA, 8 bits per channel
|
||||
int png_transforms = PNG_TRANSFORM_PACKING | // Expand 1, 2 and 4-bit samples to bytes
|
||||
PNG_TRANSFORM_STRIP_16 | // Reduce 16 bit samples to 8 bits
|
||||
PNG_TRANSFORM_GRAY_TO_RGB;
|
||||
|
||||
if (loadSettings.m_stripAlpha)
|
||||
{
|
||||
png_transforms |= PNG_TRANSFORM_STRIP_ALPHA;
|
||||
}
|
||||
|
||||
png_read_png(png_ptr, info_ptr, png_transforms, NULL);
|
||||
|
||||
// Note that libpng will allocate row_pointers for us. If we want to manage the memory ourselves, we need to call png_set_rows.
|
||||
// In that case we would have to use the low level interface: png_read_info, png_read_image, and png_read_end.
|
||||
png_bytep* row_pointers = png_get_rows(png_ptr, info_ptr);
|
||||
|
||||
PngFile pngFile;
|
||||
|
||||
int colorType = 0;
|
||||
|
||||
png_get_IHDR(png_ptr, info_ptr, &pngFile.m_width, &pngFile.m_height, &pngFile.m_bitDepth, &colorType, NULL, NULL, NULL);
|
||||
|
||||
uint32_t bytesPerPixel = 0;
|
||||
|
||||
switch (colorType)
|
||||
{
|
||||
case PNG_COLOR_TYPE_RGB:
|
||||
pngFile.m_bufferFormat = PngFile::Format::RGB;
|
||||
bytesPerPixel = 3;
|
||||
break;
|
||||
case PNG_COLOR_TYPE_RGBA:
|
||||
pngFile.m_bufferFormat = PngFile::Format::RGBA;
|
||||
bytesPerPixel = 4;
|
||||
break;
|
||||
case PNG_COLOR_TYPE_PALETTE:
|
||||
// Handles cases where the image uses 1, 2, or 4 bit samples.
|
||||
// Note bytesPerPixel is 3 because we use PNG_TRANSFORM_PACKING
|
||||
pngFile.m_bufferFormat = PngFile::Format::RGB;
|
||||
bytesPerPixel = 3;
|
||||
break;
|
||||
default:
|
||||
AZ_Assert(false, "The png transforms should have ensured a pixel format of RGB or RGBA, 8 bits per channel");
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
|
||||
fclose(fp);
|
||||
loadSettings.m_errorHandler("Unsupported pixel format.");
|
||||
return {};
|
||||
}
|
||||
|
||||
// In the future we could use the low-level interface to avoid copying the image (and provide progress callbacks)
|
||||
pngFile.m_buffer.set_capacity(pngFile.m_width * pngFile.m_height * bytesPerPixel);
|
||||
for (uint32_t rowIndex = 0; rowIndex < pngFile.m_height; ++rowIndex)
|
||||
{
|
||||
png_bytep row = row_pointers[rowIndex];
|
||||
pngFile.m_buffer.insert(pngFile.m_buffer.end(), row, row + (pngFile.m_width * bytesPerPixel));
|
||||
}
|
||||
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
|
||||
fclose(fp);
|
||||
return pngFile;
|
||||
}
|
||||
|
||||
bool PngFile::Save(const char* path, SaveSettings saveSettings)
|
||||
{
|
||||
if (!saveSettings.m_errorHandler)
|
||||
{
|
||||
saveSettings.m_errorHandler = [path](const char* message) { DefaultErrorHandler(AZStd::string::format("Could not save file '%s'. %s", path, message).c_str()); };
|
||||
}
|
||||
|
||||
if (!IsValid())
|
||||
{
|
||||
saveSettings.m_errorHandler("This PngFile is invalid.");
|
||||
return false;
|
||||
}
|
||||
|
||||
// For documentation of this code, see http://www.libpng.org/pub/png/libpng-1.4.0-manual.pdf chapter 4
|
||||
|
||||
FILE* fp = NULL;
|
||||
azfopen(&fp, path, "wb"); // return type differs across platforms so can't do inside if
|
||||
if (!fp)
|
||||
{
|
||||
saveSettings.m_errorHandler("Cannot open file.");
|
||||
return false;
|
||||
}
|
||||
|
||||
png_voidp user_error_ptr = &saveSettings.m_errorHandler;
|
||||
png_error_ptr user_error_fn = PngImage_user_error_fn;
|
||||
png_error_ptr user_warning_fn = PngImage_user_warning_fn;
|
||||
|
||||
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, user_error_ptr, user_error_fn, user_warning_fn);
|
||||
if (!png_ptr)
|
||||
{
|
||||
fclose(fp);
|
||||
saveSettings.m_errorHandler("png_create_write_struct failed.");
|
||||
return false;
|
||||
}
|
||||
|
||||
png_infop info_ptr = png_create_info_struct(png_ptr);
|
||||
if (!info_ptr)
|
||||
{
|
||||
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
|
||||
fclose(fp);
|
||||
saveSettings.m_errorHandler("png_destroy_write_struct failed.");
|
||||
return false;
|
||||
}
|
||||
|
||||
AZ_PUSH_DISABLE_WARNING(4611, "-Wunknown-warning-option") // Disables "interaction between '_setjmp' and C++ object destruction is non-portable". See https://docs.microsoft.com/en-us/cpp/preprocessor/warning?view=msvc-160
|
||||
if (setjmp(png_jmpbuf(png_ptr)))
|
||||
{
|
||||
png_destroy_write_struct(&png_ptr, &info_ptr);
|
||||
fclose(fp);
|
||||
// We don't report an error message here because the user_error_fn should have done that already.
|
||||
return false;
|
||||
}
|
||||
AZ_POP_DISABLE_WARNING
|
||||
|
||||
png_init_io(png_ptr, fp);
|
||||
|
||||
int colorType = 0;
|
||||
if (saveSettings.m_stripAlpha || m_bufferFormat == PngFile::Format::RGB)
|
||||
{
|
||||
colorType = PNG_COLOR_TYPE_RGB;
|
||||
}
|
||||
else
|
||||
{
|
||||
colorType = PNG_COLOR_TYPE_RGBA;
|
||||
}
|
||||
|
||||
int transforms = PNG_TRANSFORM_IDENTITY;
|
||||
if (saveSettings.m_stripAlpha && m_bufferFormat == PngFile::Format::RGBA)
|
||||
{
|
||||
transforms |= PNG_TRANSFORM_STRIP_FILLER_AFTER;
|
||||
}
|
||||
|
||||
png_set_IHDR(png_ptr, info_ptr, m_width, m_height, m_bitDepth, colorType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
|
||||
|
||||
png_set_compression_level(png_ptr, saveSettings.m_compressionLevel);
|
||||
|
||||
const uint32_t bytesPerPixel = (m_bufferFormat == PngFile::Format::RGB) ? 3 : 4;
|
||||
|
||||
AZStd::vector<uint8_t*> rows;
|
||||
rows.reserve(m_height);
|
||||
for (uint32_t i = 0; i < m_height; ++i)
|
||||
{
|
||||
rows.push_back(m_buffer.begin() + m_width * bytesPerPixel * i);
|
||||
}
|
||||
|
||||
png_set_rows(png_ptr, info_ptr, rows.begin());
|
||||
|
||||
png_write_png(png_ptr, info_ptr, transforms, NULL);
|
||||
|
||||
png_destroy_write_struct(&png_ptr, &info_ptr);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void PngFile::DefaultErrorHandler(const char* message)
|
||||
{
|
||||
AZ_Error("PngFile", false, "%s", message);
|
||||
}
|
||||
|
||||
bool PngFile::IsValid() const
|
||||
{
|
||||
return
|
||||
!m_buffer.empty() &&
|
||||
m_width > 0 &&
|
||||
m_height > 0 &&
|
||||
m_bitDepth > 0;
|
||||
}
|
||||
|
||||
AZStd::vector<uint8_t>&& PngFile::TakeBuffer()
|
||||
{
|
||||
return AZStd::move(m_buffer);
|
||||
}
|
||||
|
||||
} // namespace Utils
|
||||
}// namespace AZ
|
||||
@ -0,0 +1,313 @@
|
||||
/*
|
||||
* 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()));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
temp.png
|
||||
@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:af0d0d079354495ff96aa266ecb4092d679e6c5a952e8b2d4ee743e538d54809
|
||||
size 126
|
||||
@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:01cad8dd75c9a26169e858960817424362c16cbf2a8df8bc55857f6172ce2526
|
||||
size 834
|
||||
@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:6ddb5f70eaf72fe09add6cc7cdf7cbba2d327ab312bb9bde73d7f456f80b8d6e
|
||||
size 141
|
||||
@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:b50e93bbbd320d69752df2c25b4118c0198226a0d230781a6ed93f89b7eea053
|
||||
size 142
|
||||
@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:cb56e8bf15a4727bcebba579cb97a36e1f26e4a4870b2b4a38bdbd4b4bd076fd
|
||||
size 127
|
||||
@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:710fd5c80af49a42249a471ded6c9119be79d6748218073bb8297ffb4b4e62d1
|
||||
size 137
|
||||
Loading…
Reference in New Issue