// ========================================================== // FreeImage 3 .NET wrapper // Original FreeImage 3 functions and .NET compatible derived functions // // Design and implementation by // - Jean-Philippe Goerke (jpgoerke@users.sourceforge.net) // - Carsten Klein (cklein05@users.sourceforge.net) // // Contributors: // - David Boland (davidboland@vodafone.ie) // // Main reference : MSDN Knowlede Base // // This file is part of FreeImage 3 // // COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES // THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE // OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED // CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT // THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY // SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL // PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER // THIS DISCLAIMER. // // Use at your own risk! // ========================================================== // ========================================================== // CVS // $Revision: 1.7 $ // $Date: 2009/02/27 16:34:59 $ // $Id: ImageMetadata.cs,v 1.7 2009/02/27 16:34:59 cklein05 Exp $ // ========================================================== using System; using System.Collections; using System.Collections.Generic; using System.Reflection; using System.Diagnostics; namespace FreeImageAPI.Metadata { /// /// Class handling metadata of a FreeImage bitmap. /// public class ImageMetadata : IEnumerable, IComparable, IComparable { [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly List data; [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly FIBITMAP dib; [DebuggerBrowsable(DebuggerBrowsableState.Never)] private bool hideEmptyModels; /// /// Initializes a new instance based on the specified , /// showing all known models. /// /// Handle to a FreeImage bitmap. public ImageMetadata(FIBITMAP dib) : this(dib, false) { } /// /// Initializes a new instance based on the specified , /// showing or hiding empry models. /// /// Handle to a FreeImage bitmap. /// When true, empty metadata models /// will be hidden until a tag to this model is added. public ImageMetadata(FIBITMAP dib, bool hideEmptyModels) { if (dib.IsNull) throw new ArgumentNullException("dib"); data = new List(FreeImage.FREE_IMAGE_MDMODELS.Length); this.dib = dib; this.hideEmptyModels = hideEmptyModels; data.Add(new MDM_ANIMATION(dib)); data.Add(new MDM_COMMENTS(dib)); data.Add(new MDM_CUSTOM(dib)); data.Add(new MDM_EXIF_EXIF(dib)); data.Add(new MDM_EXIF_GPS(dib)); data.Add(new MDM_INTEROP(dib)); data.Add(new MDM_EXIF_MAIN(dib)); data.Add(new MDM_MAKERNOTE(dib)); data.Add(new MDM_GEOTIFF(dib)); data.Add(new MDM_IPTC(dib)); data.Add(new MDM_NODATA(dib)); data.Add(new MDM_XMP(dib)); } /// /// Gets or sets the of the specified type. /// In case the getter returns null the model is not contained /// by the list. /// null can be used calling the setter to destroy the model. /// /// Type of the model. /// The object of the specified type. public MetadataModel this[FREE_IMAGE_MDMODEL model] { get { for (int i = 0; i < data.Count; i++) { if (data[i].Model == model) { if (!data[i].Exists && hideEmptyModels) { return null; } return data[i]; } } return null; } } /// /// Gets or sets the at the specified index. /// In case the getter returns null the model is not contained /// by the list. /// null can be used calling the setter to destroy the model. /// /// Index of the within /// this instance. /// The /// object at the specified index. public MetadataModel this[int index] { get { if (index < 0 || index >= data.Count) { throw new ArgumentOutOfRangeException("index"); } return (hideEmptyModels && !data[index].Exists) ? null : data[index]; } } /// /// Returns a list of all visible /// MetadataModels. /// public List List { get { if (hideEmptyModels) { List result = new List(); for (int i = 0; i < data.Count; i++) { if (data[i].Exists) { result.Add(data[i]); } } return result; } else { return data; } } } /// /// Adds new tag to the bitmap or updates its value in case it already exists. /// will be used as key. /// /// The tag to add or update. /// Returns true on success, false on failure. /// /// is null. public bool AddTag(MetadataTag tag) { for (int i = 0; i < data.Count; i++) { if (tag.Model == data[i].Model) { return data[i].AddTag(tag); } } return false; } /// /// Returns the number of visible /// MetadataModels. /// public int Count { get { if (hideEmptyModels) { int count = 0; for (int i = 0; i < data.Count; i++) { if (data[i].Exists) { count++; } } return count; } else { return data.Count; } } } /// /// Gets or sets whether empty /// MetadataModels are hidden. /// public bool HideEmptyModels { get { return hideEmptyModels; } set { hideEmptyModels = value; } } /// /// Retrieves an object that can iterate through the individual /// MetadataModels /// in this . /// /// An for this . public IEnumerator GetEnumerator() { if (hideEmptyModels) { List tempList = new List(data.Count); for (int i = 0; i < data.Count; i++) { if (data[i].Exists) { tempList.Add(data[i]); } } return tempList.GetEnumerator(); } else { return data.GetEnumerator(); } } /// /// Compares this instance with a specified . /// /// An object to compare with this instance. /// A 32-bit signed integer indicating the lexical relationship between the two comparands. /// is not a . public int CompareTo(object obj) { if (obj == null) { return 1; } if (!(obj is ImageMetadata)) { throw new ArgumentException("obj"); } return CompareTo((ImageMetadata)obj); } /// /// Compares this instance with a specified object. /// /// A to compare. /// A signed number indicating the relative values of this instance /// and . public int CompareTo(ImageMetadata other) { return this.dib.CompareTo(other.dib); } } }