7#include <gtest/gtest.h>
13using Positions = std::map<char, std::pair<unsigned int, unsigned int>>;
22void ExpectAtlasPixels(
const ImageData& atlas,
const std::map<char, ImageData>& sources,
23 const Positions& positions)
25 ASSERT_EQ(positions.size(), sources.size());
27 for (
unsigned y = 0; y < atlas.
GetHeight(); ++y)
29 for (
unsigned x = 0; x < atlas.
GetWidth(); ++x)
31 SCOPED_TRACE(::testing::Message() <<
"pixel " << x <<
", " << y);
32 Color expected(0, 0, 0, 255);
33 unsigned coveringImages = 0;
34 for (
const auto& source : sources)
36 const auto found = positions.find(source.first);
37 ASSERT_NE(found, positions.end());
38 const auto left = found->second.first;
39 const auto top = found->second.second;
40 if (x >= left && y >= top && x - left < source.second.GetWidth() &&
41 y - top < source.second.GetHeight())
44 expected = source.second.GetPixel(x - left, y - top);
47 EXPECT_LE(coveringImages, 1U);
48 EXPECT_EQ(atlas.
GetPixel(x, y), expected);
59 EXPECT_FALSE(maker.CreateTextureAtlas(positions, atlas));
60 EXPECT_TRUE(positions.empty());
68 ImageData source(2, 2, std::vector<std::uint8_t>{
69 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
70 const std::map<char, ImageData> images{{
'A', source}};
77 const Positions expected{{
'A', {0, 0}}};
78 EXPECT_EQ(positions, expected);
84 const std::map<char, ImageData> images{
85 {
'C', SolidImage(2, 2,
Color(0, 0, 255, 64))},
86 {
'A', SolidImage(2, 2,
Color(255, 0, 0, 128))},
87 {
'B', SolidImage(2, 2,
Color(0, 255, 0, 192))}
96 const Positions expected{{
'A', {0, 0}}, {
'B', {2, 0}}, {
'C', {0, 2}}};
97 EXPECT_EQ(positions, expected);
98 ExpectAtlasPixels(atlas, images, positions);
103 const std::map<char, ImageData> images{
104 {
'A', SolidImage(2, 3,
Color(255, 0, 0))},
105 {
'B', SolidImage(4, 1,
Color(0, 255, 0))},
106 {
'C', SolidImage(1, 2,
Color(0, 0, 255))}
115 const Positions expected{{
'A', {0, 0}}, {
'B', {4, 0}}, {
'C', {0, 3}}};
116 EXPECT_EQ(positions, expected);
117 ExpectAtlasPixels(atlas, images, positions);
Represents raw 2D image data stored in memory.
unsigned int GetWidth() const
Returns the image width.
std::vector< uint8_t > GetImageDataRGBA() const
Gets raw image data in RGBA format.
unsigned int GetHeight() const
Returns the image height.
const Color GetPixel(unsigned int x, unsigned int y) const
Retrieves the color of a specific pixel.
Builds texture atlases from collections of images.
bool CreateTextureAtlas(std::map< char, std::pair< unsigned int, unsigned int > > &texAtlasData, ImageData &texAtlasImage)
Creates a texture atlas from the stored image collection.
Represents a color with red, green, blue, and alpha channels.
TEST(TextureAtlasMaker, EmptyCollectionReportsFailure)