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); } }
/// <summary> /// Called by the XNA Framework when importing an texture file to be used as a game asset. This /// is the method called by the XNA Framework when an asset is to be imported into an object /// that can be recognized by the Content Pipeline. /// </summary> /// <param name="filename">Name of a game asset file.</param> /// <param name="context"> /// Contains information for importing a game asset, such as a logger interface. /// </param> /// <returns>Resulting game asset.</returns> public override TextureContent Import(string filename, ContentImporterContext context) { string extension = Path.GetExtension(filename); if (extension != null) { Texture texture = null; if (extension.Equals(".DDS", StringComparison.OrdinalIgnoreCase)) { using (var stream = File.OpenRead(filename)) texture = DdsHelper.Load(stream, DdsFlags.ForceRgb | DdsFlags.ExpandLuminance); } else if (extension.Equals(".TGA", StringComparison.OrdinalIgnoreCase)) { using (var stream = File.OpenRead(filename)) texture = TgaHelper.Load(stream); } if (texture != null) { #if !MONOGAME // When using the XNA content pipeline, check for MonoGame content. if (!string.IsNullOrEmpty(ContentHelper.GetMonoGamePlatform())) #endif { // These formats are not (yet) available in MonoGame. switch (texture.Description.Format) { case DataFormat.B5G5R5A1_UNORM: // (16-bit TGA files.) case DataFormat.R8_UNORM: case DataFormat.A8_UNORM: texture = texture.ConvertTo(DataFormat.R8G8B8A8_UNORM); break; } } // Convert DigitalRune Texture to XNA TextureContent. var identity = new ContentIdentity(filename, "DigitalRune"); return(TextureHelper.ToContent(texture, identity)); } } return(base.Import(filename, context)); }