7#include <gtest/gtest.h>
13TEST(
Color, ConstructorsAndEqualityIncludeEveryChannel)
16 EXPECT_EQ(black.
r, 0);
17 EXPECT_EQ(black.
g, 0);
18 EXPECT_EQ(black.
b, 0);
19 EXPECT_EQ(black.
a, 255);
20 const Color rgb(12, 34, 56);
24 EXPECT_EQ(rgb.
a, 255);
25 const Color rgba(12, 34, 56, 78);
26 EXPECT_EQ(rgba.
r, 12);
27 EXPECT_EQ(rgba.
g, 34);
28 EXPECT_EQ(rgba.
b, 56);
29 EXPECT_EQ(rgba.
a, 78);
30 EXPECT_TRUE(rgba ==
Color(12, 34, 56, 78));
31 EXPECT_FALSE(rgba ==
Color(13, 34, 56, 78));
32 EXPECT_FALSE(rgba ==
Color(12, 35, 56, 78));
33 EXPECT_FALSE(rgba ==
Color(12, 34, 57, 78));
34 EXPECT_FALSE(rgba == rgb);
53 for (
unsigned y = 0; y < 2; ++y)
54 for (
unsigned x = 0; x < 3; ++x)
56 const Color fill(10, 20, 30, 40);
58 for (
unsigned y = 0; y < 2; ++y)
59 for (
unsigned x = 0; x < 3; ++x)
60 EXPECT_EQ(image.
GetPixel(x, y), fill);
66 const Color background(1, 2, 3, 4), foreground(5, 6, 7, 8);
67 image.
Fill(background);
69 for (
unsigned y = 0; y < 2; ++y)
70 for (
unsigned x = 0; x < 3; ++x)
71 EXPECT_EQ(image.
GetPixel(x, y), (x == 1 && y == 1) ? foreground : background);
75 image.
SetPixel(std::numeric_limits<unsigned>::max(), 0, foreground);
83 const std::vector<std::uint8_t> rgba = {1, 2, 3, 4, 5, 6, 7, 8,
84 9, 10, 11, 12, 13, 14, 15, 16};
94 const std::vector<std::uint8_t> rgb = {1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15};
101 EXPECT_THROW((
ImageData(2, 2, std::vector<std::uint8_t>(15))), std::runtime_error);
102 EXPECT_THROW((
ImageData(2, 2, std::vector<std::uint8_t>(17))), std::runtime_error);
121 original.Fill(
Color(1, 2, 3, 4));
122 EXPECT_EQ(&(copy = original), ©);
127 EXPECT_EQ(original.GetPixel(1, 2),
Color(1, 2, 3, 4));
143class ImageOverlay :
public ::testing::TestWithParam<OverlayCase>
149TEST_P(ImageOverlay, CopiesSourcePixelsAndPreservesEverythingOutsideIntersection)
152 const Color background(90, 80, 70, 60);
153 destination.Fill(background);
154 ImageData source(2, 2, std::vector<std::uint8_t>{
155 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
157 const auto placement = GetParam();
159 for (
unsigned y = 0; y < 3; ++y)
161 for (
unsigned x = 0; x < 3; ++x)
163 SCOPED_TRACE(::testing::Message() <<
"pixel " << x <<
", " << y);
164 const bool covered = x >= placement.x && y >= placement.y &&
165 x - placement.x < 2 && y - placement.y < 2;
166 EXPECT_EQ(destination.GetPixel(x, y), covered ?
167 source.GetPixel(x - placement.x, y - placement.y) : background);
170 EXPECT_EQ(source.GetImageDataRGBA(), sourceBefore);
174 OverlayCase{0, 0,
"Origin"}, OverlayCase{1, 1,
"Offset"},
175 OverlayCase{2, 0,
"RightClip"}, OverlayCase{0, 2,
"BottomClip"},
176 OverlayCase{2, 2,
"BothClip"}, OverlayCase{3, 0,
"OutsideRight"},
177 OverlayCase{0, 3,
"OutsideBottom"},
178 OverlayCase{std::numeric_limits<unsigned>::max(), 0,
"FarOutside"}),
179 [](const ::testing::TestParamInfo<OverlayCase>& info) {
return info.param.name; });
184 destination.Fill(
Color(1, 2, 3, 4));
185 const auto before = destination.GetImageDataRGBA();
186 ImageData empty, zeroWidth(0, 2), zeroHeight(2, 0);
187 for (
ImageData* source : {&empty, &zeroWidth, &zeroHeight})
190 EXPECT_EQ(destination.GetImageDataRGBA(), before);
192 EXPECT_TRUE(source->GetImageDataRGBA().empty());
193 EXPECT_TRUE(source->GetImageDataRGB().empty());
194 EXPECT_EQ(source->GetSizeInBytes(), 0U);
Represents raw 2D image data stored in memory.
void SetPixel(unsigned int x, unsigned int y, Color color)
Sets the color of a specific pixel.
unsigned int GetWidth() const
Returns the image width.
std::vector< uint8_t > GetImageDataRGB() const
Gets raw image data in RGB format.
static void DrawImageOnImageAtPos(unsigned int const x, unsigned int const y, ImageData &toImage, ImageData &fromImage)
Overlays one image on top of another at a given position.
void Fill(Color color)
Fills the image with a solid color.
std::vector< uint8_t > GetImageDataRGBA() const
Gets raw image data in RGBA format.
unsigned int GetHeight() const
Returns the image height.
size_t GetSizeInBytes() const
Gets the total size of the image in memory.
const Color GetPixel(unsigned int x, unsigned int y) const
Retrieves the color of a specific pixel.
INSTANTIATE_TEST_SUITE_P(Placements, ImageOverlay, ::testing::Values(OverlayCase{0, 0, "Origin"}, OverlayCase{1, 1, "Offset"}, OverlayCase{2, 0, "RightClip"}, OverlayCase{0, 2, "BottomClip"}, OverlayCase{2, 2, "BothClip"}, OverlayCase{3, 0, "OutsideRight"}, OverlayCase{0, 3, "OutsideBottom"}, OverlayCase{std::numeric_limits< unsigned >::max(), 0, "FarOutside"}), [](const ::testing::TestParamInfo< OverlayCase > &info) { return info.param.name;})
TEST(Color, ConstructorsAndEqualityIncludeEveryChannel)
TEST_P(ImageOverlay, CopiesSourcePixelsAndPreservesEverythingOutsideIntersection)
Represents a color with red, green, blue, and alpha channels.