Rendering Engine 0.2.0
Modular Graphics Rendering Engine | v0.2.0
Loading...
Searching...
No Matches
image_data_tests.cpp
Go to the documentation of this file.
1#include <stdio.h>
2#include <cstdint>
3#include "gtest/gtest.h"
5
6using ::testing::EmptyTestEventListener;
7using ::testing::InitGoogleTest;
8using ::testing::Test;
9using ::testing::TestEventListeners;
10using ::testing::TestInfo;
11using ::testing::TestPartResult;
12using ::testing::TestSuite;
13using ::testing::UnitTest;
14
15using namespace rendering_engine;
16
17TEST(ImageDataTests, PixelAssignment)
18{
19 Color pixel1(1,2,3, 50);
20 Color pixel2 = pixel1;
21
22 EXPECT_EQ(pixel1.a, pixel2.a);
23 EXPECT_EQ(pixel1.r, pixel2.r);
24 EXPECT_EQ(pixel1.g, pixel2.g);
25 EXPECT_EQ(pixel1.b, pixel2.b);
26}
27
28TEST(ImageDataTests, ImageDataAssignment)
29{
30 ImageData image1(3U, 3U);
31 Color const testColor(115U, 100U, 10U, 150U);
32 image1.Fill(testColor);
33
34 ImageData image2(1, 1);
35 image2 = image1;
36
37 ASSERT_EQ(image2.GetHeight(), image1.GetHeight());
38 ASSERT_EQ(image2.GetWidth(), image1.GetWidth());
39
40 for( unsigned int y = 0; y < image1.GetHeight(); ++y )
41 {
42 for( unsigned int x = 0; x < image1.GetWidth(); ++x )
43 {
44 EXPECT_EQ(image1.GetPixel(x, y), image2.GetPixel(x, y));
45 }
46 }
47}
48
49TEST(ImageDataTests, GetImageDataEmpty)
50{
51 ImageData image;
52 EXPECT_TRUE(image.GetImageDataRGBA().empty());
53 image = ImageData(3, 3);
54 image.Fill(Color(255, 255, 255, 255));
55 EXPECT_FALSE(image.GetImageDataRGBA().empty());
56}
57
58TEST(ImageDataTests, InappropriateAccess)
59{
60 ImageData image(3, 3);
61 image.Fill(Color(255, 255, 255, 255));
62
63 EXPECT_THROW(image.GetPixel(4, 4), std::runtime_error);
64}
65
66TEST(ImageDataTest, SaveLoadTextureJpg)
67{
68 std::string const filepath{"test_jpeg_texture_file.jpg"};
69 ImageData image1(10U, 10U);
70 image1.Fill(Color(255, 0, 0, 255));
71 image1.WriteJpegFile(filepath.c_str());
72
73 ImageData image2("test_jpeg_texture_file.jpg");
74
75 EXPECT_EQ( image1.GetHeight(), image2.GetHeight() );
76 EXPECT_EQ( image1.GetWidth(), image2.GetWidth() );
77 //Pixels in the output jpeg file don't match completely to source pixels, because the compression algorithm in jpeg might slightly change values.
78}
79
80TEST(ImageDataTest, SaveLoadTexturePng)
81{
82 std::string const filepath{ "test_png_texture_file.png" };
83 ImageData image1(10U, 10U);
84 image1.Fill(Color(255, 0, 0, 255));
85 image1.WritePngFile(filepath.c_str());
86
87 ImageData image2("test_png_texture_file.png");
88
89 EXPECT_EQ(image1.GetHeight(), image2.GetHeight());
90 EXPECT_EQ(image1.GetWidth(), image2.GetWidth());
91 EXPECT_EQ(image1.GetImageDataRGBA(), image2.GetImageDataRGBA());
92}
93
94TEST(ImageDataTest, IncorrectTextureFilepath)
95{
96 std::string const filepath{ "missing_texture_file.png" };
97
98 EXPECT_THROW(ImageData image(filepath), std::runtime_error);
99}
100
Represents raw 2D image data stored in memory.
unsigned int GetWidth() const
Returns the image width.
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.
void WritePngFile(char const *filename)
Writes the image data to a PNG file.
void WriteJpegFile(char const *filename)
Writes the image data to a JPEG file.
const Color GetPixel(unsigned int x, unsigned int y) const
Retrieves the color of a specific pixel.
TEST(ImageDataTests, PixelAssignment)
Represents a color with red, green, blue, and alpha channels.