public static Texture2D LoadTexture(IGraphicsService graphicsService, string fileName) { if (fileName == null) { throw new ArgumentNullException(nameof(fileName)); } if (fileName.Length == 0) { throw new ArgumentException("The file name must not be empty.", nameof(fileName)); } // Load API-independent texture. Texture texture = null; using (var stream = File.OpenRead(fileName)) { string extension = Path.GetExtension(fileName); if (!string.IsNullOrEmpty(extension)) { extension = extension.ToUpperInvariant(); if (extension == ".DDS") { texture = DdsHelper.Load(stream, DdsFlags.ForceRgb | DdsFlags.ExpandLuminance); } else if (extension == ".TGA") { texture = TgaHelper.Load(stream); } } if (texture == null) { // TODO: Register ImagingFactory as service. using (var imagingFactory = new ImagingFactory()) texture = WicHelper.Load(imagingFactory, stream, WicFlags.ForceRgb | WicFlags.No16Bpp); } } //Tests(texture); // Convert to XNA texture. var description = texture.Description; if (description.Dimension == TextureDimension.TextureCube) { var texture2D = new Texture2D(graphicsService.GraphicsDevice, description.Width, description.Height, false, description.Format.ToSurfaceFormat()); texture2D.SetData(texture.Images[0].Data); return(texture2D); } else { var texture2D = new Texture2D(graphicsService.GraphicsDevice, description.Width, description.Height, description.MipLevels > 1, description.Format.ToSurfaceFormat()); for (int i = 0; i < texture.Description.MipLevels; i++) { texture2D.SetData(i, null, texture.Images[i].Data, 0, texture.Images[i].Data.Length); } return(texture2D); } }
private static void Tests(Texture texture) { //var testTexture = texture.ConvertTo(TextureFormat.R32G32B32A32_Float); //TextureHelper.FlipX(testTexture); //testTexture = testTexture.ConvertTo(TextureFormat.R8G8B8A8_UNorm); //using (var stream = File.OpenWrite("c:\\temp\\FlipX.dds")) // DdsHelper.Save(testTexture, stream, DdsFlags.None); //testTexture = texture.ConvertTo(TextureFormat.R32G32B32A32_Float); //TextureHelper.FlipY(testTexture); //testTexture = testTexture.ConvertTo(TextureFormat.R8G8B8A8_UNorm); //using (var stream = File.OpenWrite("c:\\temp\\FlipY.dds")) // DdsHelper.Save(testTexture, stream, DdsFlags.None); //testTexture = texture.ConvertTo(TextureFormat.R32G32B32A32_Float); //testTexture = TextureHelper.Rotate(testTexture, 90); //testTexture = testTexture.ConvertTo(TextureFormat.R8G8B8A8_UNorm); //using (var stream = File.OpenWrite("c:\\temp\\Rotate90.dds")) // DdsHelper.Save(testTexture, stream, DdsFlags.None); //testTexture = texture.ConvertTo(TextureFormat.R32G32B32A32_Float); //testTexture = TextureHelper.Rotate(testTexture, 180); //testTexture = testTexture.ConvertTo(TextureFormat.R8G8B8A8_UNorm); //using (var stream = File.OpenWrite("c:\\temp\\Rotate180.dds")) // DdsHelper.Save(testTexture, stream, DdsFlags.None); //testTexture = texture.ConvertTo(TextureFormat.R32G32B32A32_Float); //testTexture = TextureHelper.Rotate(testTexture, 270); //testTexture = testTexture.ConvertTo(TextureFormat.R8G8B8A8_UNorm); //using (var stream = File.OpenWrite("c:\\temp\\Rotate270.dds")) // DdsHelper.Save(testTexture, stream, DdsFlags.None); //var testTexture = texture.ConvertTo(TextureFormat.R32G32B32A32_Float); //testTexture = testTexture.Resize(1000, 1000, 1, ResizeFilter.Box, false, WrapMode.Clamp); //testTexture = testTexture.ConvertTo(TextureFormat.R8G8B8A8_UNorm); //using (var stream = File.OpenWrite("c:\\temp\\Resize1000.dds")) // DdsHelper.Save(testTexture, stream, DdsFlags.None); //testTexture = texture.ConvertTo(TextureFormat.R32G32B32A32_Float); //testTexture = testTexture.Resize(200, 100, 1, ResizeFilter.Box, false, WrapMode.Clamp); //testTexture = testTexture.ConvertTo(TextureFormat.R8G8B8A8_UNorm); //using (var stream = File.OpenWrite("c:\\temp\\Resize200x100.dds")) // DdsHelper.Save(testTexture, stream, DdsFlags.None); //var testTexture = texture.ConvertTo(TextureFormat.R32G32B32A32_Float); //testTexture = testTexture.Resize(1024, 1024, 1, ResizeFilter.Box, true, WrapMode.Repeat); //testTexture.GenerateMipmaps(ResizeFilter.Box, true, WrapMode.Repeat); //testTexture = testTexture.ConvertTo(TextureFormat.R8G8B8A8_UNorm); //using (var stream = File.OpenWrite("c:\\temp\\Mipmaps.dds")) // DdsHelper.Save(testTexture, stream, DdsFlags.None); //var testTexture = texture.ConvertTo(TextureFormat.BC1_UNorm); //using (var stream = File.OpenWrite("c:\\temp\\BC1.dds")) // DdsHelper.Save(testTexture, stream, DdsFlags.None); //testTexture = texture.ConvertTo(TextureFormat.BC2_UNorm); //using (var stream = File.OpenWrite("c:\\temp\\BC2.dds")) // DdsHelper.Save(testTexture, stream, DdsFlags.None); //testTexture = texture.ConvertTo(TextureFormat.BC3_UNorm); //using (var stream = File.OpenWrite("c:\\temp\\BC3.dds")) // DdsHelper.Save(testTexture, stream, DdsFlags.None); }