/
ImageLoading.cpp
239 lines (195 loc) · 6.93 KB
/
ImageLoading.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "RadiantTest.h"
#include "iimage.h"
#include "RGBAImage.h"
// Helpers for examining pixel data
using RGB8 = BasicVector3<uint8_t>;
// Override operator<< to print RGB8 components as numbers, rather than random
// ASCII characters
std::ostream& operator<< (std::ostream& os, const RGB8& rgb)
{
return os << "[" << int(rgb.x()) << ", " << int(rgb.y()) << ", "
<< int(rgb.z()) << "]";
}
// Helper class for retrieving pixels by X and Y coordinates and casting them to
// the appropriate pixel type.
template<typename Pixel_T> class Pixelator
{
const Image& _image;
public:
// Construct with image to access
Pixelator(const Image& im): _image(im)
{}
// Get pixel at given coordinates
Pixel_T& operator() (int x, int y)
{
Pixel_T* p0 = reinterpret_cast<Pixel_T*>(_image.getPixels());
return *(p0 + x + y * _image.getWidth());
}
};
namespace test
{
// Test fixture for image loading. Provides a convenient method to load an image
// relative to the test project path.
class ImageLoadingTest: public RadiantTest
{
protected:
// Load an image from the given path
ImagePtr loadImage(const std::string& path)
{
auto filePath = _context.getTestProjectPath() + path;
return GlobalImageLoader().imageFromFile(filePath);
}
};
TEST_F(ImageLoadingTest, LoadPng8Bit)
{
auto img = loadImage("textures/pngs/twentyone_8bit.png");
EXPECT_EQ(img->getWidth(), 32);
EXPECT_EQ(img->getHeight(), 32);
}
TEST_F(ImageLoadingTest, LoadPng16Bit)
{
auto img = loadImage("textures/pngs/twentyone_16bit.png");
EXPECT_EQ(img->getWidth(), 32);
EXPECT_EQ(img->getHeight(), 32);
}
TEST_F(ImageLoadingTest, LoadPngGreyscaleWithAlpha)
{
// This is a 8-Bit Greyscale PNG with Alpha channel, so pixel depth is 16 bits
auto img = loadImage("textures/pngs/transparent_greyscale.png");
EXPECT_EQ(img->getWidth(), 32);
EXPECT_EQ(img->getHeight(), 32);
EXPECT_FALSE(img->isPrecompressed());
// If the image loader interprets the file correctly, we should have an RGBA
// image with the colour values being the same for R, G and B.
// If the image loader didn't convert grey to RGB, the grey value is
// smeared across the whole RGB channels and they are not uniform
EXPECT_TRUE(std::dynamic_pointer_cast<RGBAImage>(img));
auto pixels = reinterpret_cast<RGBAPixel*>(img->getPixels());
auto numPixels = img->getWidth() * img->getHeight();
for (auto i = 0; i < numPixels; ++i)
{
EXPECT_EQ(pixels[i].blue, pixels[i].green) << "Expected Green == Blue";
EXPECT_EQ(pixels[i].red, pixels[i].green) << "Expected Red == Blue";
if (pixels[i].blue != pixels[i].green || pixels[i].red != pixels[i].green)
{
break;
}
}
}
TEST_F(ImageLoadingTest, LoadInvalidDDS)
{
auto img = loadImage("textures/dds/not_a_dds.dds");
ASSERT_FALSE(img);
}
TEST_F(ImageLoadingTest, LoadDDSUncompressed)
{
auto img = loadImage("textures/dds/test_16x16_uncomp.dds");
ASSERT_TRUE(img);
// Check properties are correct
EXPECT_EQ(img->getWidth(), 16);
EXPECT_EQ(img->getHeight(), 16);
EXPECT_EQ(img->getLevels(), 1);
EXPECT_FALSE(img->isPrecompressed());
// Examine pixel data
Pixelator<RGB8> pixels(*img);
EXPECT_EQ(pixels(0, 0), RGB8(0, 0, 0)); // border
EXPECT_EQ(pixels(2, 1), RGB8(255, 255, 255)); // background
EXPECT_EQ(pixels(6, 7), RGB8(0, 255, 0)); // green band
EXPECT_EQ(pixels(7, 14), RGB8(255, 255, 0)); // cyan pillar (BGR)
EXPECT_EQ(pixels(8, 1), RGB8(255, 0, 255)); // magenta pillar
EXPECT_EQ(pixels(8, 8), RGB8(0, 0, 255)); // red centre (BGR)
EXPECT_EQ(pixels(14, 13), RGB8(255, 255, 255)); // background
EXPECT_EQ(pixels(15, 15), RGB8(0, 0, 0)); // border
}
TEST_F(ImageLoadingTest, LoadDDSUncompressedMipMaps)
{
auto img = loadImage("textures/dds/test_16x16_uncomp_mips.dds");
ASSERT_TRUE(img);
// Overall size is unchanged
EXPECT_EQ(img->getWidth(), 16);
EXPECT_EQ(img->getHeight(), 16);
EXPECT_FALSE(img->isPrecompressed());
// 5 mipmap levels (16, 8, 4, 2, 1)
EXPECT_EQ(img->getLevels(), 5);
EXPECT_EQ(img->getWidth(0), 16);
EXPECT_EQ(img->getWidth(1), 8);
EXPECT_EQ(img->getHeight(1), 8);
EXPECT_EQ(img->getWidth(2), 4);
EXPECT_EQ(img->getHeight(3), 2);
EXPECT_EQ(img->getHeight(4), 1);
}
TEST_F(ImageLoadingTest, LoadDDSUncompressedNPOT)
{
auto img = loadImage("textures/dds/test_10x16_uncomp.dds");
ASSERT_TRUE(img);
EXPECT_EQ(img->getWidth(), 10);
EXPECT_EQ(img->getHeight(), 16);
EXPECT_FALSE(img->isPrecompressed());
// Examine pixel data
Pixelator<RGB8> pixels(*img);
EXPECT_EQ(pixels(0, 0), RGB8(0, 0, 0)); // border
EXPECT_EQ(pixels(1, 1), RGB8(0, 0, 255)); // red diag
EXPECT_EQ(pixels(8, 1), RGB8(255, 0, 255)); // magenta pillar
EXPECT_EQ(pixels(8, 14), RGB8(255, 255, 0)); // cyan pillar
EXPECT_EQ(pixels(9, 15), RGB8(0, 0, 0)); // border
}
TEST_F(ImageLoadingTest, LoadDDSCompressedDXT1)
{
auto img = loadImage("textures/dds/test_128x128_dxt1.dds");
ASSERT_TRUE(img);
// 128x128 image with no mipmaps
EXPECT_EQ(img->getWidth(), 128);
EXPECT_EQ(img->getHeight(), 128);
EXPECT_EQ(img->getLevels(), 1);
// Must be compressed
EXPECT_TRUE(img->isPrecompressed());
}
TEST_F(ImageLoadingTest, LoadDDSCompressedDXT5NPOT)
{
auto img = loadImage("textures/dds/test_60x128_dxt5.dds");
ASSERT_TRUE(img);
// 60x128 image with no mipmaps
EXPECT_EQ(img->getWidth(), 60);
EXPECT_EQ(img->getHeight(), 128);
EXPECT_EQ(img->getLevels(), 1);
// Must be compressed
EXPECT_TRUE(img->isPrecompressed());
}
TEST_F(ImageLoadingTest, LoadDDSCompressedDXT5MipMapsNPOT)
{
auto img = loadImage("textures/dds/test_60x128_dxt5_mips.dds");
ASSERT_TRUE(img);
// 60x128 image with 8 mipmaps
EXPECT_EQ(img->getWidth(), 60);
EXPECT_EQ(img->getHeight(), 128);
EXPECT_EQ(img->getLevels(), 8);
// Must be compressed
EXPECT_TRUE(img->isPrecompressed());
// Check mipmap size sequence
EXPECT_EQ(img->getWidth(1), 30);
EXPECT_EQ(img->getHeight(1), 64);
EXPECT_EQ(img->getWidth(2), 15);
EXPECT_EQ(img->getHeight(2), 32);
EXPECT_EQ(img->getWidth(3), 7);
EXPECT_EQ(img->getHeight(3), 16);
EXPECT_EQ(img->getWidth(4), 3);
EXPECT_EQ(img->getHeight(4), 8);
EXPECT_EQ(img->getWidth(5), 1);
EXPECT_EQ(img->getHeight(5), 4);
EXPECT_EQ(img->getWidth(6), 1);
EXPECT_EQ(img->getHeight(6), 2);
EXPECT_EQ(img->getWidth(7), 1);
EXPECT_EQ(img->getHeight(7), 1);
}
TEST_F(ImageLoadingTest, LoadDDSCompressedBC5MipMaps)
{
auto img = loadImage("textures/dds/test_16x16_bc5.dds");
ASSERT_TRUE(img);
// 16x16 image
EXPECT_EQ(img->getWidth(), 16);
EXPECT_EQ(img->getHeight(), 16);
EXPECT_EQ(img->getLevels(), 5);
// Check compressed GL format
EXPECT_EQ(img->getGLFormat(), GL_COMPRESSED_RG_RGTC2);
}
}