IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
Metadata.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
6
8
16
20public abstract class MetadataKey
21{
27 protected MetadataKey(string name, Type valueType)
28 {
29 Name = name;
30 ValueType = valueType;
31 }
32
37 public string Name { get; }
38
43 public Type ValueType { get; }
44}
45
54public sealed class MetadataKeyT<T> : MetadataKey
55{
64 internal MetadataKeyT(string name)
65 : base(name, typeof(T))
66 {
67 }
68}
69
79
83public static class MetadataKeys
84{
95
98 public static readonly MetadataKeyT<ulong> DeviceTimestamp = new("DeviceTimestamp");
99
110
113 public static readonly MetadataKeyT<ulong> DeviceFrameID = new("DeviceFrameID");
114
121
124 public static readonly MetadataKeyT<uint> BinningHorizontal = new("BinningHorizontal");
125
132
135 public static readonly MetadataKeyT<uint> BinningVertical = new("BinningVertical");
136
145
148 public static readonly MetadataKeyT<RectangleU> Roi = new("Roi");
149
156
159 public static readonly MetadataKeyT<double> DeviceExposureTime = new("DeviceExposureTime");
160
174
177 public static readonly MetadataKeyT<ulong> SystemTimestamp = new("SystemTimestamp");
178
190
193 public static readonly MetadataKeyT<List<double>> DeviceExposureTimeSequence = new("DeviceExposureTimeSequence");
194
198
201 public static readonly MetadataKeyT<double> DeviceGain = new("DeviceGain");
202
212
215 public static readonly MetadataKeyT<List<double>> DeviceGainSequence = new("DeviceGainSequence");
216}
217
228
232public sealed class Metadata
233{
234 private readonly Dictionary<string, object> _data = new();
235
244 public object GetValueByKey(MetadataKey key)
245 {
246 return !_data.TryGetValue(key.Name, out var value)
247 ? throw new CommonOutOfRangeException($"There is no value named {key.Name}")
248 : value;
249 }
250
264 {
265 var value = GetValueByKey((MetadataKey) key);
266 try
267 {
268 return (T) value;
269 }
270 catch (InvalidCastException ex)
271 {
273 $"Metadata entry '{key.Name}' is not of type {typeof(T).FullName}", ex);
274 }
275 }
276
285 public object GetValueByName(string name)
286 {
287 try
288 {
289 return _data[name];
290 }
291 catch (KeyNotFoundException ex)
292 {
293 throw new CommonOutOfRangeException($"There is no value named {name}", ex);
294 }
295 }
296
308 public bool TryGetValueByKey(MetadataKey key, out object? value)
309 {
310 return _data.TryGetValue(key.Name, out value);
311 }
312
325 public bool TryGetValueByKey<T>(MetadataKeyT<T> key, out T? value)
326 {
327 if (_data.TryGetValue(key.Name, out var objValue) && objValue is T typedValue)
328 {
329 value = typedValue;
330 return true;
331 }
332
333 value = default;
334 return false;
335 }
336
348 public bool TryGetValueByName(string name, out object? value)
349 {
350 return _data.TryGetValue(name, out value);
351 }
352
356 public IReadOnlyCollection<string> Keys => _data.Keys;
357
369 public void SetValueByKey(MetadataKey key, object value)
370 {
371 if (_data.ContainsKey(key.Name) && _data[key.Name].Equals(value))
372 {
373 return;
374 }
375
376 if (!key.ValueType.IsInstanceOfType(value))
377 {
379 $"Value for '{key.Name}' must be assignable to {key.ValueType.FullName} (was {value.GetType().FullName})");
380 }
381
382 _data[key.Name] = value;
383 OnChanged();
384 }
385
391 public void SetValueByName(string key, object value)
392 {
393 if (_data.ContainsKey(key) && _data[key].Equals(value))
394 {
395 return;
396 }
397
398 _data[key] = value;
399 OnChanged();
400 }
401
407 public bool HasEntryByKey(MetadataKey key)
408 {
409 return _data.ContainsKey(key.Name);
410 }
411
417 public bool HasEntryByName(string key)
418 {
419 return _data.ContainsKey(key);
420 }
421
426 public override string ToString()
427 {
428 var result = string.Join(", ", _data.Select(item => $"{item.Key} = {item.Value}"));
429 return $"{nameof(Metadata)}{{ {result} }}";
430 }
431
443 {
444 var copy = new Metadata();
445 foreach (var kvp in Keys)
446 {
447 copy.SetValueByName(kvp, GetValueByName(kvp));
448 }
449
450 return copy;
451 }
452
455 public event EventHandler? Changed;
456
457 private void OnChanged()
458 {
459 Changed?.Invoke(this, EventArgs.Empty);
460 }
461}
Exception thrown when an invalid cast or type conversion is attempted.
Exception thrown when a value is outside the valid range.
Strongly typed container for image acquisition metadata.
Definition Metadata.cs:233
T GetValueByKey< T >(MetadataKeyT< T > key)
Retrieves the value associated with a strongly typed metadata key.
Definition Metadata.cs:263
void SetValueByName(string key, object value)
Stores a metadata value using a string-based key.
Definition Metadata.cs:391
object GetValueByName(string name)
Retrieves a metadata value by its string key.
Definition Metadata.cs:285
IReadOnlyCollection< string > Keys
Gets a read-only collection of all metadata key names.
Definition Metadata.cs:356
bool TryGetValueByKey(MetadataKey key, out object? value)
Attempts to retrieve a value associated with a strongly typed metadata key.
Definition Metadata.cs:308
void SetValueByKey(MetadataKey key, object value)
Stores a value associated with a strongly typed metadata key.
Definition Metadata.cs:369
override string ToString()
Returns a human-readable representation of the metadata contents.
Definition Metadata.cs:426
bool HasEntryByName(string key)
Determines whether a value exists for the specified string key.
Definition Metadata.cs:417
Metadata Clone()
Creates a copy of this Metadata instance.
Definition Metadata.cs:442
bool HasEntryByKey(MetadataKey key)
Determines whether a value exists for the specified strongly typed key.
Definition Metadata.cs:407
bool TryGetValueByKey< T >(MetadataKeyT< T > key, out T? value)
Attempts to retrieve a value associated with a strongly typed metadata key.
Definition Metadata.cs:325
bool TryGetValueByName(string name, out object? value)
Attempts to retrieve a metadata value using a string-based key.
Definition Metadata.cs:348
EventHandler? Changed
Occurs when a metadata value is modified.
Definition Metadata.cs:455
object GetValueByKey(MetadataKey key)
Retrieves the value associated with a strongly typed metadata key.
Definition Metadata.cs:244
Base class for metadata keys used to identify and access metadata values.
Definition Metadata.cs:21
Type ValueType
Gets the required type of the value associated with this key.
Definition Metadata.cs:43
string Name
Gets the unique name of the metadata key.
Definition Metadata.cs:37
Represents a strongly-typed metadata key.
Definition Metadata.cs:55
Provides a centralized registry of predefined MetadataKey instances.
Definition Metadata.cs:84
static readonly MetadataKeyT< double > DeviceExposureTime
Device exposure time the image was captured with.
Definition Metadata.cs:159
static readonly MetadataKeyT< List< double > > DeviceExposureTimeSequence
Device exposure time sequence the image was captured with.
Definition Metadata.cs:193
static readonly MetadataKeyT< RectangleU > Roi
Region of interest applied to the image.
Definition Metadata.cs:148
static readonly MetadataKeyT< uint > BinningVertical
Vertical pixel binning factor.
Definition Metadata.cs:135
static readonly MetadataKeyT< ulong > DeviceFrameID
A sequentially incremented identifier for the frame.
Definition Metadata.cs:113
static readonly MetadataKeyT< ulong > DeviceTimestamp
Device-provided image acquisition timestamp in nanoseconds.
Definition Metadata.cs:98
static readonly MetadataKeyT< ulong > SystemTimestamp
Timestamp of the system since unix epoch in nanoseconds.
Definition Metadata.cs:177
static readonly MetadataKeyT< double > DeviceGain
Device gain the image was captured with.
Definition Metadata.cs:201
static readonly MetadataKeyT< uint > BinningHorizontal
Horizontal pixel binning factor.
Definition Metadata.cs:124
static readonly MetadataKeyT< List< double > > DeviceGainSequence
Device gain sequence the image was captured with.
Definition Metadata.cs:215