/// <summary> /// Sets a specific image encoder as the encoder for a specific image format. /// </summary> /// <param name="imageFormat">The image format to register the encoder for.</param> /// <param name="encoder">The encoder to use,</param> public void SetEncoder(ITextureFormat imageFormat, ITextureEncoder encoder) { Guard.NotNull(imageFormat, nameof(imageFormat)); Guard.NotNull(encoder, nameof(encoder)); this.AddImageFormat(imageFormat); this.mimeTypeEncoders.AddOrUpdate(imageFormat, encoder, (s, e) => encoder); }
/// <summary> /// Sets a specific image decoder as the decoder for a specific image format. /// </summary> /// <param name="imageFormat">The image format to register the encoder for.</param> /// <param name="decoder">The decoder to use,</param> public void SetDecoder(ITextureFormat imageFormat, ITextureDecoder decoder) { Guard.NotNull(imageFormat, nameof(imageFormat)); Guard.NotNull(decoder, nameof(decoder)); this.AddImageFormat(imageFormat); this.mimeTypeDecoders.AddOrUpdate(imageFormat, decoder, (s, e) => decoder); }
/// <summary> /// Create a new instance of the <see cref="Texture"/> class from the given file. /// The pixel type is selected by the decoder. /// </summary> /// <param name="config">The configuration options.</param> /// <param name="path">The file path to the image.</param> /// <param name="format">The mime type of the decoded image.</param> /// <exception cref="NotSupportedException"> /// Thrown if the stream is not readable nor seekable. /// </exception> public static Texture Load(Configuration config, string path, out ITextureFormat format) { using (Stream stream = config.FileSystem.OpenRead(path)) { return(Load(config, stream, out format)); } }
/// <summary> /// Reads the raw image information from the specified stream without fully decoding it. /// </summary> /// <param name="config">The configuration.</param> /// <param name="stream">The image stream to read the information from.</param> /// <param name="format">The format type of the decoded image.</param> /// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception> /// <returns> /// The <see cref="ITextueInfo"/> or null if suitable info detector is not found. /// </returns> public static ITextueInfo Identify(Configuration config, Stream stream, out ITextureFormat format) { (ITextueInfo info, ITextureFormat format)data = WithSeekableStream(config, stream, s => InternalIdentity(s, config ?? Configuration.Default)); format = data.format; return(data.info); }
/// <summary> /// For the specified mime type find the encoder. /// </summary> /// <param name="format">The format to discover</param> /// <returns>The <see cref="ITextureEncoder"/> if found otherwise null</returns> public ITextureEncoder FindEncoder(ITextureFormat format) { Guard.NotNull(format, nameof(format)); return(this.mimeTypeEncoders.TryGetValue(format, out ITextureEncoder encoder) ? encoder : null); }
/// <summary> /// Registers a new format provider. /// </summary> /// <param name="format">The format to register as a known format.</param> public void AddImageFormat(ITextureFormat format) { Guard.NotNull(format, nameof(format)); Guard.NotNull(format.MimeTypes, nameof(format.MimeTypes)); Guard.NotNull(format.FileExtensions, nameof(format.FileExtensions)); lock (HashLock) { if (!this.imageFormats.Contains(format)) { this.imageFormats.Add(format); } } }
/// <summary> /// By reading the header on the provided byte array this calculates the images format. /// </summary> /// <param name="config">The configuration.</param> /// <param name="data">The byte array containing encoded texture data to read the header from.</param> /// <returns>The mime type or null if none found.</returns> public static ITextureFormat DetectFormat(Configuration config, ReadOnlySpan <byte> data) { int maxHeaderSize = config.MaxHeaderSize; if (maxHeaderSize <= 0) { return(null); } foreach (ITextureFormatDetector detector in config.ImageFormatsManager.FormatDetectors) { ITextureFormat f = detector.DetectFormat(data); if (f != null) { return(f); } } return(default);
/// <summary> /// Decode a new instance of the <see cref="Texture"/> class from the given stream. /// The pixel format is selected by the decoder. /// </summary> /// <param name="config">The configuration options.</param> /// <param name="stream">The stream containing image information.</param> /// <param name="format">The format type of the decoded image.</param> /// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception> /// <exception cref="UnknownTextureFormatException">Image cannot be loaded.</exception> /// <returns>A new <see cref="Texture"/>.</returns> public static Texture Load(Configuration config, Stream stream, out ITextureFormat format) { config = config ?? Configuration.Default; (Texture img, ITextureFormat format)data = WithSeekableStream(config, stream, s => DecodeTexture(s, config)); format = data.format; if (data.img != null) { return(data.img); } var sb = new StringBuilder(); sb.AppendLine("Image cannot be loaded. Available decoders:"); foreach (KeyValuePair <ITextureFormat, ITextureDecoder> val in config.ImageFormatsManager.ImageDecoders) { sb.AppendLine($" - {val.Key.Name} : {val.Value.GetType().Name}"); } throw new UnknownTextureFormatException(sb.ToString()); }
/// <summary> /// By reading the header on the provided stream this calculates the images format. /// </summary> /// <param name="stream">The image stream to read the header from.</param> /// <param name="config">The configuration.</param> /// <param name="format">The IImageFormat.</param> /// <returns>The image format or null if none found.</returns> private static ITextureDecoder DiscoverDecoder(Stream stream, Configuration config, out ITextureFormat format) { format = InternalDetectFormat(stream, config); return(format != null ? config.ImageFormatsManager.FindDecoder(format) : null); }
/// <summary> /// Decode a new instance of the <see cref="Texture"/> class from the given stream. /// The pixel format is selected by the decoder. /// </summary> /// <param name="stream">The stream containing image information.</param> /// <param name="format">The format type of the decoded image.</param> /// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception> /// <exception cref="UnknownTextureFormatException">Image cannot be loaded.</exception> /// <returns>The <see cref="Texture"/>.</returns> public static Texture Load(Stream stream, out ITextureFormat format) => Load(Configuration.Default, stream, out format);
/// <summary> /// By reading the header on the provided stream this reads the raw image information. /// </summary> /// <param name="stream">The image stream to read the header from.</param> /// <param name="format">The format type of the decoded image.</param> /// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception> /// <returns> /// The <see cref="ITextueInfo"/> or null if suitable info detector not found. /// </returns> public static ITextueInfo Identify(Stream stream, out ITextureFormat format) => Identify(Configuration.Default, stream, out format);
/// <summary> /// Create a new instance of the <see cref="Texture"/> class from the given file. /// </summary> /// <param name="path">The file path to the image.</param> /// <param name="format">The mime type of the decoded image.</param> /// <exception cref="NotSupportedException"> /// Thrown if the stream is not readable nor seekable. /// </exception> /// <returns>A new <see cref="Texture"/>.</returns> public static Texture Load(string path, out ITextureFormat format) => Load(Configuration.Default, path, out format);
/// <summary> /// Reads the raw texture information from the specified stream without fully decoding it. /// </summary> /// <param name="configuration">The configuration.</param> /// <param name="filePath">The image file to open and to read the header from.</param> /// <param name="format">The format type of the decoded texture.</param> /// <exception cref="ArgumentNullException">The configuration is null.</exception> /// <returns> /// The <see cref="ITextureInfo"/> or null if suitable info detector is not found. /// </returns> public static ITextureInfo Identify(Configuration configuration, string filePath, out ITextureFormat format) { Guard.NotNull(configuration, nameof(configuration)); using (Stream file = configuration.FileSystem.OpenRead(filePath)) { return(Identify(configuration, file, out format)); } }
/// <summary> /// Reads the raw texture information from the specified stream without fully decoding it. /// </summary> /// <param name="filePath">The image file to open and to read the header from.</param> /// <param name="format">The format type of the decoded texture.</param> /// <returns> /// The <see cref="ITextureInfo"/> or null if suitable info detector not found. /// </returns> public static ITextureInfo Identify(string filePath, out ITextureFormat format) => Identify(Configuration.Default, filePath, out format);