forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextureImporterTests.cs
341 lines (304 loc) · 12.5 KB
/
TextureImporterTests.cs
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
using System;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Graphics.PackedVector;
using NUnit.Framework;
namespace MonoGame.Tests.ContentPipeline
{
class TextureImporterTests
{
const string intermediateDirectory = "TestObj";
const string outputDirectory = "TestBin";
void ImportStandard(string filename, SurfaceFormat expectedSurfaceFormat, int expectedSize)
{
var importer = new TextureImporter( );
var context = new TestImporterContext(intermediateDirectory, outputDirectory);
var content = importer.Import(filename, context);
Assert.NotNull(content);
Assert.AreEqual(1, content.Faces.Count);
Assert.AreEqual(1, content.Faces[0].Count);
Assert.AreEqual(expectedSize, content.Faces[0][0].Width);
Assert.AreEqual(expectedSize, content.Faces[0][0].Height);
SurfaceFormat format;
Assert.True(content.Faces[0][0].TryGetFormat(out format));
Assert.AreEqual(expectedSurfaceFormat, format);
// Clean-up the directories it may have produced, ignoring DirectoryNotFound exceptions
try
{
Directory.Delete(intermediateDirectory, true);
Directory.Delete(outputDirectory, true);
}
catch(DirectoryNotFoundException)
{
}
}
[Test]
public void ImportBmp( )
{
ImportStandard("Assets/Textures/LogoOnly_64px.bmp", SurfaceFormat.Color, 64);
}
[Test]
public void ImportBmpRGB555( )
{
ImportStandard("Assets/Textures/Logo555.bmp", SurfaceFormat.Color, 64);
}
[Test]
public void ImportBmpRGB565( )
{
ImportStandard("Assets/Textures/Logo565.bmp", SurfaceFormat.Color, 64);
}
[Test]
public void ImportBmp4bits( )
{
ImportStandard("Assets/Textures/LogoOnly_64px-4bits.bmp", SurfaceFormat.Color, 64);
}
[Test]
public void ImportBmpV5()
{
ImportStandard("Assets/Textures/Logo_BMPV5.bmp", SurfaceFormat.Color, 256);
}
[Test]
public void ImportGif( )
{
ImportStandard("Assets/Textures/LogoOnly_64px.gif", SurfaceFormat.Color, 64);
}
[Test]
public void ImportJpg( )
{
ImportStandard("Assets/Textures/LogoOnly_64px.jpg", SurfaceFormat.Color, 64);
}
[Test]
public void ImportPng( )
{
ImportStandard("Assets/Textures/LogoOnly_64px.png", SurfaceFormat.Color, 64);
}
[Test]
public void ImportTga( )
{
ImportStandard("Assets/Textures/LogoOnly_64px.tga", SurfaceFormat.Color, 64);
}
[Test]
public void ImportTif( )
{
ImportStandard("Assets/Textures/LogoOnly_64px.tif", SurfaceFormat.Color, 64);
}
/// <summary>
/// This test tries to load a tiff file encoded in rgbf, but freeimage seems to be failing to read files with this encoding
/// Might be necessary to modify this test with future updates of freeimage.
///
/// Note that the image was created with Freeimage from a bitmap
/// </summary>
[Test]
public void ImportImageWithBadContent( )
{
Assert.Throws(typeof(InvalidContentException), ( ) => ImportStandard("Assets/Textures/rgbf.tif", SurfaceFormat.Vector4, 64));
//ImportStandard("Assets/Textures/rgbf.tif", SurfaceFormat.Color);
}
[Test]
public void ImportRGBA16Png()
{
var importer = new TextureImporter();
var context = new TestImporterContext(intermediateDirectory, outputDirectory);
var content = importer.Import("Assets/Textures/RGBA16.png", context);
ulong expectedPixelValue = 5714832815570484476;
Assert.NotNull(content);
Assert.AreEqual(content.Faces.Count, 1);
Assert.AreEqual(content.Faces[0].Count, 1);
Assert.AreEqual(content.Faces[0][0].Width, 126);
Assert.AreEqual(content.Faces[0][0].Height, 240);
SurfaceFormat format;
Assert.True(content.Faces[0][0].TryGetFormat(out format));
Assert.AreEqual(SurfaceFormat.Rgba64, format);
Assert.AreEqual(expectedPixelValue, ((PixelBitmapContent<Rgba64>)content.Faces[0][0]).GetRow(1)[12].PackedValue);
// Clean-up the directories it may have produced, ignoring DirectoryNotFound exceptions
try
{
Directory.Delete(intermediateDirectory, true);
Directory.Delete(outputDirectory, true);
}
catch (DirectoryNotFoundException)
{
}
}
[Test]
public void ImportDdsCubemapDxt1()
{
var importer = new TextureImporter();
var context = new TestImporterContext(intermediateDirectory, outputDirectory);
var content = importer.Import("Assets/Textures/SampleCube64DXT1Mips.dds", context);
Assert.NotNull(content);
Assert.AreEqual(content.Faces.Count, 6);
for (int f = 0; f < 6; ++f)
{
CheckDdsFace(content, f, 7, 64, 64);
}
SurfaceFormat format;
Assert.True(content.Faces[0][0].TryGetFormat(out format));
Assert.AreEqual(format, SurfaceFormat.Dxt1);
// Clean-up the directories it may have produced, ignoring DirectoryNotFound exceptions
try
{
Directory.Delete(intermediateDirectory, true);
Directory.Delete(outputDirectory, true);
}
catch (DirectoryNotFoundException)
{ }
}
[Test]
public void ImportDdsCubemapColor()
{
var importer = new TextureImporter();
var context = new TestImporterContext(intermediateDirectory, outputDirectory);
var content = importer.Import("Assets/Textures/Sunset.dds", context);
Assert.NotNull(content);
Assert.AreEqual(content.Faces.Count, 6);
for (int f = 0; f < 6; ++f)
{
CheckDdsFace(content, f, 1, 512, 512);
}
SurfaceFormat format;
Assert.True(content.Faces[0][0].TryGetFormat(out format));
// Ensure the red and blue bytes have been correctly swapped
Assert.AreEqual(format, SurfaceFormat.Color);
var bytes = content.Faces[0][0].GetPixelData();
Assert.AreEqual(bytes[0], 208);
Assert.AreEqual(bytes[2], 62);
// Clean-up the directories it may have produced, ignoring DirectoryNotFound exceptions
try
{
Directory.Delete(intermediateDirectory, true);
Directory.Delete(outputDirectory, true);
}
catch (DirectoryNotFoundException)
{ }
}
[Test]
public void ImportDds()
{
ImportStandard("Assets/Textures/LogoOnly_64px.dds", SurfaceFormat.Dxt3, 64);
ImportStandard("Assets/Textures/LogoOnly_64px-R8G8B8.dds", SurfaceFormat.Color, 64);
ImportStandard("Assets/Textures/LogoOnly_64px-X8R8G8B8.dds", SurfaceFormat.Color, 64);
}
[Test]
public void ImportDdsMipMap()
{
//ImportStandard("Assets/Textures/LogoOnly_64px-mipmaps.dds", SurfaceFormat.Color);
var importer = new TextureImporter();
var context = new TestImporterContext(intermediateDirectory, outputDirectory);
var content = importer.Import("Assets/Textures/LogoOnly_64px-mipmaps.dds", context);
Assert.NotNull(content);
Assert.AreEqual(content.Faces.Count, 1);
CheckDdsFace(content, 0, 7, 64, 64);
SurfaceFormat format;
Assert.True(content.Faces[0][0].TryGetFormat(out format));
Assert.AreEqual(format, SurfaceFormat.Dxt3);
// Clean-up the directories it may have produced, ignoring DirectoryNotFound exceptions
try
{
Directory.Delete(intermediateDirectory, true);
Directory.Delete(outputDirectory, true);
}
catch(DirectoryNotFoundException)
{
}
}
[Test]
public void Import24BitPngCheckColorChannels()
{
var importer = new TextureImporter();
var context = new TestImporterContext(intermediateDirectory, outputDirectory);
var content = importer.Import("Assets/Textures/color_24bit.png", context);
var bitmap = (PixelBitmapContent<Color>) content.Faces[0][0];
var pixel = bitmap.GetPixel(0, 0);
Assert.AreEqual(255, pixel.R);
Assert.AreEqual(128, pixel.G);
Assert.AreEqual(64, pixel.B);
try
{
Directory.Delete(intermediateDirectory, true);
Directory.Delete(outputDirectory, true);
}
catch (DirectoryNotFoundException)
{
}
}
[Test]
public void Import32BitPngCheckColorChannels()
{
var importer = new TextureImporter();
var context = new TestImporterContext(intermediateDirectory, outputDirectory);
var content = importer.Import("Assets/Textures/color_32bit.png", context);
var bitmap = (PixelBitmapContent<Color>)content.Faces[0][0];
var pixel = bitmap.GetPixel(0, 0);
Assert.AreEqual(255, pixel.R);
Assert.AreEqual(128, pixel.G);
Assert.AreEqual(64, pixel.B);
try
{
Directory.Delete(intermediateDirectory, true);
Directory.Delete(outputDirectory, true);
}
catch (DirectoryNotFoundException)
{
}
}
[Test]
public void Import48BitPngCheckColorChannels()
{
var importer = new TextureImporter();
var context = new TestImporterContext(intermediateDirectory, outputDirectory);
var content = importer.Import("Assets/Textures/color_48bit.png", context);
var bitmap = (PixelBitmapContent<Rgba64>)content.Faces[0][0];
var pixel = bitmap.GetPixel(0, 0).ToVector4();
AssertFloatsAreEqual(1.0f, pixel.X);
AssertFloatsAreEqual(0.5f, pixel.Y);
AssertFloatsAreEqual(0.25f, pixel.Z);
try
{
Directory.Delete(intermediateDirectory, true);
Directory.Delete(outputDirectory, true);
}
catch (DirectoryNotFoundException)
{
}
}
[Test]
public void Import64BitPngCheckColorChannels()
{
var importer = new TextureImporter();
var context = new TestImporterContext(intermediateDirectory, outputDirectory);
var content = importer.Import("Assets/Textures/color_64bit.png", context);
var bitmap = (PixelBitmapContent<Rgba64>)content.Faces[0][0];
var pixel = bitmap.GetPixel(0, 0).ToVector4();
AssertFloatsAreEqual(1.0f, pixel.X);
AssertFloatsAreEqual(0.5f, pixel.Y);
AssertFloatsAreEqual(0.25f, pixel.Z);
try
{
Directory.Delete(intermediateDirectory, true);
Directory.Delete(outputDirectory, true);
}
catch (DirectoryNotFoundException)
{
}
}
private static void AssertFloatsAreEqual(float expected, float actual)
{
// Assume floats are equal if they differ less than 1%
var maxDiff = expected * 0.01f;
Assert.GreaterOrEqual(maxDiff, Math.Abs(expected - actual));
}
private static void CheckDdsFace(TextureContent content, int faceIndex, int mipMapCount, int width, int height)
{
Assert.AreEqual(content.Faces[faceIndex].Count, mipMapCount);
for (int i = 0; i < mipMapCount; ++i)
{
Assert.AreEqual(content.Faces[faceIndex][i].Width, width >> i);
Assert.AreEqual(content.Faces[faceIndex][i].Height, height >> i);
}
}
}
}