/// <summary> /// Function to load a texture from a file. /// </summary> /// <param name="graphics">The graphics interface that will own the texture.</param> /// <param name="filePath">The path to the file.</param> /// <param name="codec">The codec that is used to decode the the data in the stream.</param> /// <param name="options">[Optional] Options used to further define the texture.</param> /// <returns>A new <see cref="GorgonTexture3DReadWriteView"/></returns> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="graphics"/>, <paramref name="filePath"/>, or the <paramref name="codec"/> parameter is <b>null</b>.</exception> /// <exception cref="ArgumentEmptyException">Thrown when the <paramref name="filePath"/> parameter is empty.</exception> /// <remarks> /// <para> /// This will load an <see cref="IGorgonImage"/> from a file on disk and put it into a <see cref="GorgonTexture3D"/> object and return a <see cref="GorgonTexture3DReadWriteView"/>. /// </para> /// <para> /// If specified, the <paramref name="options"/>parameter will define how Gorgon and shaders should handle the texture. The <see cref="GorgonTextureLoadOptions"/> type contains the following: /// <list type="bullet"> /// <item> /// <term>Binding</term> /// <description>When defined, will indicate the <see cref="TextureBinding"/> that defines how the texture will be bound to the graphics pipeline. If it is omitted, then the binding will be /// <see cref="TextureBinding.ShaderResource"/>.</description> /// </item> /// <item> /// <term>Usage</term> /// <description>When defined, will indicate the preferred usage for the texture. If it is omitted, then the usage will be set to <see cref="ResourceUsage.Default"/>.</description> /// </item> /// <item> /// <term>Multisample info</term> /// <description>When defined (i.e. not <b>null</b>), defines the multisampling to apply to the texture. If omitted, then the default is <see cref="GorgonMultisampleInfo.NoMultiSampling"/>.</description> /// </item> /// </list> /// </para> /// <para> /// Since the <see cref="GorgonTexture3D"/> created by this method is linked to the <see cref="GorgonTexture3DReadWriteView"/> returned, disposal of either one will dispose of the other on your behalf. If /// the user created a <see cref="GorgonTexture3DReadWriteView"/> from the <see cref="GorgonTexture3D.GetShaderResourceView"/> method on the <see cref="GorgonTexture3D"/>, then it's assumed the user knows /// what they are doing and will handle the disposal of the texture and view on their own. /// </para> /// </remarks> public static GorgonTexture3DReadWriteView FromFile(GorgonGraphics graphics, string filePath, IGorgonImageCodec codec, GorgonTextureLoadOptions options = null) { if (graphics == null) { throw new ArgumentNullException(nameof(graphics)); } if (filePath == null) { throw new ArgumentNullException(nameof(filePath)); } if (string.IsNullOrWhiteSpace(filePath)) { throw new ArgumentEmptyException(nameof(filePath)); } if (codec == null) { throw new ArgumentNullException(nameof(codec)); } using (IGorgonImage image = codec.LoadFromFile(filePath)) { GorgonTexture3D texture = image.ToTexture3D(graphics, options); GorgonTexture3DReadWriteView view = texture.GetReadWriteView(); view.OwnsResource = true; return(view); } }
/// <summary> /// Function to read the textures for the font. /// </summary> /// <param name="filePath">The path to the font file.</param> /// <param name="fontInfo">The information about the font.</param> /// <returns>A list of textures.</returns> private IReadOnlyList <GorgonTexture2D> ReadTextures(string filePath, BmFontInfo fontInfo) { var textures = new GorgonTexture2D[fontInfo.FontTextures.Length]; var directory = new DirectoryInfo(Path.GetDirectoryName(filePath).FormatDirectory(Path.DirectorySeparatorChar)); Debug.Assert(directory.Exists, "Font directory should exist, but does not."); for (int i = 0; i < fontInfo.FontTextures.Length; ++i) { var fileInfo = new FileInfo(directory.FullName.FormatDirectory(Path.DirectorySeparatorChar) + fontInfo.FontTextures[i].FormatFileName()); if (!fileInfo.Exists) { throw new FileNotFoundException(string.Format(Resources.GORGFX_ERR_FONT_TEXTURE_FILE_NOT_FOUND, fileInfo.FullName)); } IGorgonImageCodec codec = GetImageCodec(fileInfo.Extension); using (IGorgonImage image = codec.LoadFromFile(fileInfo.FullName)) { image.ToTexture2D(Factory.Graphics, new GorgonTexture2DLoadOptions { Name = $"BmFont_Texture_{Guid.NewGuid():N}" }); } } return(textures); }
/// <summary>Imports the data.</summary> /// <param name="temporaryDirectory">The temporary directory for writing any transitory data.</param> /// <param name="cancelToken">The cancel token.</param> /// <remarks> /// <para> /// The <paramref name="temporaryDirectory"/> should be used to write any working/temporary data used by the import. Note that all data written into this directory will be deleted when the /// project is unloaded from memory. /// </para> /// </remarks> public FileInfo ImportData(DirectoryInfo temporaryDirectory, CancellationToken cancelToken) { var ddsCodec = new GorgonCodecDds(); _log.Print($"Importing file '{SourceFile.FullName}' (Codec: {_codec.Name})...", LoggingLevel.Verbose); using (IGorgonImage image = _codec.LoadFromFile(SourceFile.FullName)) { var tempFile = new FileInfo(Path.Combine(temporaryDirectory.FullName, Path.GetFileNameWithoutExtension(SourceFile.Name))); _log.Print($"Converting '{SourceFile.FullName}' to DDS file format. Image format [{image.Format}].", LoggingLevel.Verbose); ddsCodec.SaveToFile(image, tempFile.FullName); return(tempFile); } }
/// <summary> /// Function to load a texture from a file. /// </summary> /// <param name="graphics">The graphics interface that will own the texture.</param> /// <param name="filePath">The path to the file.</param> /// <param name="codec">The codec that is used to decode the the data in the stream.</param> /// <param name="options">[Optional] Options used to further define the texture.</param> /// <returns>A new <see cref="GorgonTexture2DView"/></returns> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="graphics"/>, <paramref name="filePath"/>, or the <paramref name="codec"/> parameter is <b>null</b>.</exception> /// <exception cref="ArgumentEmptyException">Thrown when the <paramref name="filePath"/> parameter is empty.</exception> /// <remarks> /// <para> /// This will load an <see cref="IGorgonImage"/> from a file on disk and put it into a <see cref="GorgonTexture2D"/> object and return a <see cref="GorgonTexture2DView"/>. /// </para> /// <para> /// If specified, the <paramref name="options"/>parameter will define how Gorgon and shaders should handle the texture. The <see cref="GorgonTextureLoadOptions"/> type contains the following: /// <list type="bullet"> /// <item> /// <term>Binding</term> /// <description>When defined, will indicate the <see cref="TextureBinding"/> that defines how the texture will be bound to the graphics pipeline. If it is omitted, then the binding will be /// <see cref="TextureBinding.ShaderResource"/>.</description> /// </item> /// <item> /// <term>Usage</term> /// <description>When defined, will indicate the preferred usage for the texture. If it is omitted, then the usage will be set to <see cref="ResourceUsage.Default"/>.</description> /// </item> /// <item> /// <term>Multisample info</term> /// <description>When defined (i.e. not <b>null</b>), defines the multisampling to apply to the texture. If omitted, then the default is <see cref="GorgonMultisampleInfo.NoMultiSampling"/>.</description> /// </item> /// <item> /// <term>ConvertToPremultipliedAlpha</term> /// <description>Converts the image to premultiplied alpha before uploading the image data to the texture.</description> /// </item> /// </list> /// </para> /// <para> /// Since the <see cref="GorgonTexture2D"/> created by this method is linked to the <see cref="GorgonTexture2DView"/> returned, disposal of either one will dispose of the other on your behalf. If /// the user created a <see cref="GorgonTexture2DView"/> from the <see cref="GorgonTexture2D.GetShaderResourceView"/> method on the <see cref="GorgonTexture2D"/>, then it's assumed the user knows /// what they are doing and will handle the disposal of the texture and view on their own. /// </para> /// </remarks> public static GorgonTexture2DView FromFile(GorgonGraphics graphics, string filePath, IGorgonImageCodec codec, GorgonTexture2DLoadOptions options = null) { if (graphics == null) { throw new ArgumentNullException(nameof(graphics)); } if (filePath == null) { throw new ArgumentNullException(nameof(filePath)); } if (string.IsNullOrWhiteSpace(filePath)) { throw new ArgumentEmptyException(nameof(filePath)); } if (codec == null) { throw new ArgumentNullException(nameof(codec)); } using (IGorgonImage image = codec.LoadFromFile(filePath)) { if (options == null) { options = new GorgonTexture2DLoadOptions { Name = Path.GetFileNameWithoutExtension(filePath), Usage = ResourceUsage.Default, Binding = TextureBinding.ShaderResource, IsTextureCube = image.ImageType == ImageType.ImageCube }; } GorgonTexture2D texture = image.ToTexture2D(graphics, options); GorgonTexture2DView view = texture.GetShaderResourceView(); view.OwnsResource = true; return(view); } }
/// <summary> /// Function to convert the image to use our custom codec. /// </summary> private void ConvertImage() { // The path to our image file for our custom codec. string tempPath = Path.ChangeExtension(Path.GetTempPath().FormatDirectory(Path.DirectorySeparatorChar) + Path.GetRandomFileName(), "tvImage"); try { // Save the current texture using our useless new custom codec. _customCodec.SaveToFile(_image.ConvertToFormat(BufferFormat.R8G8B8A8_UNorm), tempPath); _image.Dispose(); _texture?.Dispose(); _image = _customCodec.LoadFromFile(tempPath); _texture = _image.ToTexture2D(_graphics, new GorgonTexture2DLoadOptions { Name = "Converted Texture" }).GetShaderResourceView(); } catch { // Clean up the new texture should we have an exception (this shouldn't happen, better safe than sorry). _image?.Dispose(); throw; } finally { try { File.Delete(tempPath); } // ReSharper disable once EmptyGeneralCatchClause catch { // Intentionally left blank. // If we can't clean up the temp file, then it's no big deal right now. } } }