// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // The supported format base implement this class when building a supported format. // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Imaging.Formats { using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; /// /// The supported format base. Implement this class when building a supported format. /// public abstract class FormatBase : ISupportedImageFormat { /// /// Initializes a new instance of the class. /// protected FormatBase() { this.Quality = 90; } /// /// Gets the file headers. /// public abstract byte[][] FileHeaders { get; } /// /// Gets the list of file extensions. /// public abstract string[] FileExtensions { get; } /// /// Gets the standard identifier used on the Internet to indicate the type of data that a file contains. /// public abstract string MimeType { get; } /// /// Gets the default file extension. /// public string DefaultExtension { get { return this.MimeType.Replace("image/", string.Empty); } } /// /// Gets the file format of the image. /// public abstract ImageFormat ImageFormat { get; } /// /// Gets or sets a value indicating whether the image format is indexed. /// public bool IsIndexed { get; set; } /// /// Gets or sets the quality of output for images. /// public int Quality { get; set; } /// /// Applies the given processor the current image. /// /// The processor delegate. /// The . public virtual void ApplyProcessor(Func processor, ImageFactory factory) { factory.Image = processor.Invoke(factory); } /// /// Decodes the image to process. /// /// /// The containing the image information. /// /// /// The . /// public virtual Image Load(Stream stream) { return Image.FromStream(stream, true); } /// /// Saves the current image to the specified output stream. /// /// The to save the image information to. /// The to save. /// /// The . /// public virtual Image Save(Stream stream, Image image) { image.Save(stream, this.ImageFormat); stream.Position = 0; return image; } /// /// Saves the current image to the specified file path. /// /// The path to save the image to. /// The /// to save. /// /// The . /// public virtual Image Save(string path, Image image) { image.Save(path, this.ImageFormat); return image; } /// /// Determines whether the specified , is equal to this instance. /// /// The to compare with this instance. /// /// true if the specified is equal to this instance; otherwise, false. /// public override bool Equals(object obj) { ISupportedImageFormat format = obj as ISupportedImageFormat; if (format == null) { return false; } return this.MimeType.Equals(format.MimeType) && this.IsIndexed.Equals(format.IsIndexed); } /// /// Returns a hash code for this instance. /// /// /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// public override int GetHashCode() { return this.MimeType.GetHashCode(); } } }