Skip to content

Instantly share code, notes, and snippets.

@Toxantron
Created March 22, 2017 19:27
Show Gist options
  • Save Toxantron/a68b8f81c69cd0c720c4919b896dfee0 to your computer and use it in GitHub Desktop.
Save Toxantron/a68b8f81c69cd0c720c4919b896dfee0 to your computer and use it in GitHub Desktop.
Different APIs for ImageSharp Metadata
// Contains all properties all images have
public interface IImageProperties
{
// Standard properties
int Height { get; set; }
int Width { get; set; }
// -- end of standard
// This holds the more flexible values
IImageMetadata this[int key] { get; set; }
}
// Class with all base keys
public class MetaKey
{
public const int Artist = 2;
public const int Date = 4;
}
// Example for derived keys
internal class SharedKeys
{
public const int ColorSpace = 5001;
}
public class JpegKey : MetaKey
{
public const int Compression = 1001;
public static int ColorSpace { get { return SharedKeys.ColorSpace; } }
}
public class ExiffKey : MetaKey
{
public const int Foo = 2001;
public static int ColorSpace { get { return SharedKeys.ColorSpace; } }
}
// Shared interface for the different value structs
// The interface will however result in boxing and unboxing
public interface IImageMetadata
{
int Key { get; }
string Value { get; set; }
}
public struct StringImageMetadata : IImageMetadata
{
private int key;
private string value;
public StringImageMetadata(int key, string value)
{
this.key = key;
this.value = value;
}
public int Key => this.key;
public string Value
{
get { return this.value; }
set { this.value = value; }
}
}
public struct IntImageMetadata : IImageMetadata
{
private int key;
private int value;
public IntImageMetadata(int key, int value)
{
this.key = key;
this.value = value;
}
public int Key => this.key;
public string Value
{
get { return this.value.ToString("D"); }
set { this.value = int.Parse(value); }
}
}
// Usage
{
IImageProperties properties = image.Properties;
var artist = properties[MetaKey.Artist].Value;
var colorspace = properties[JpegKey.Colorspace].Value;
}
// Contains all properties all images have
public interface IImageProperties
{
// Standard properties
int Height { get; set; }
int Width { get; set; }
// -- end of standard
// This holds the more flexible values
IImageMetadata this[MetaKey key] { get; set; }
}
// Either keys of all formats or just the more common ones
// Having all keys will make the type increase steadily till it becomes unusable
// Having more than one place breaks type-safety
public enum MetaKey
{
FrameDelay,
RepeatCount,
Artist
}
// Shared interface for the different value structs
// The interface will however result in boxing and unboxing
public interface IImageMetadata
{
MetaKey Key { get; }
string Value { get; set; }
}
public struct StringImageMetadata : IImageMetadata
{
private MetaKey key;
private string value;
public StringImageMetadata(MetaKey key, string value)
{
this.key = key;
this.value = value;
}
public MetaKey Key => this.key;
public string Value
{
get { return this.value; }
set { this.value = value; }
}
}
public struct IntImageMetadata : IImageMetadata
{
private MetaKey key;
private int value;
public IntImageMetadata(MetaKey key, int value)
{
this.key = key;
this.value = value;
}
public MetaKey Key => this.key;
public string Value
{
get { return this.value.ToString("D"); }
set { this.value = int.Parse(value); }
}
}
// Usage
{
IImageProperties properties = image.Properties;
var artist = properties[MetaKey.Artist].Value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment