📷 A modern, cross-platform, 2D Graphics library for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

169 lines
4.2 KiB

// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using System.Runtime.CompilerServices;
namespace SixLabors.ImageSharp.Metadata.Profiles.Exif
{
internal sealed class ExifLong8Array : ExifArrayValue<ulong>
{
public ExifLong8Array(ExifTag<ulong[]> tag)
: base(tag)
{
}
public ExifLong8Array(ExifTagValue tag)
: base(tag)
{
}
private ExifLong8Array(ExifLong8Array value)
: base(value)
{
}
public override ExifDataType DataType
{
get
{
if (this.Value is null)
{
return ExifDataType.Long;
}
foreach (ulong value in this.Value)
{
if (value > uint.MaxValue)
{
return ExifDataType.Long8;
}
}
return ExifDataType.Long;
}
}
public override bool TrySetValue(object value)
{
if (base.TrySetValue(value))
{
return true;
}
switch (value)
{
case int val:
return this.SetSingle((ulong)Numerics.Clamp(val, 0, int.MaxValue));
case uint val:
return this.SetSingle((ulong)val);
case short val:
return this.SetSingle((ulong)Numerics.Clamp(val, 0, short.MaxValue));
case ushort val:
return this.SetSingle((ulong)val);
case long[] array:
{
if (value.GetType() == typeof(ulong[]))
{
return this.SetArray((ulong[])value);
}
return this.SetArray(array);
}
case int[] array:
{
if (value.GetType() == typeof(uint[]))
{
return this.SetArray((uint[])value);
}
return this.SetArray(array);
}
case short[] array:
{
if (value.GetType() == typeof(ushort[]))
{
return this.SetArray((ushort[])value);
}
return this.SetArray(array);
}
}
return false;
}
public override IExifValue DeepClone() => new ExifLong8Array(this);
private bool SetSingle(ulong value)
{
this.Value = new[] { value };
return true;
}
private bool SetArray(long[] values)
{
this.Value = Unsafe.As<long[], ulong[]>(ref values);
return true;
}
private bool SetArray(ulong[] values)
{
this.Value = values;
return true;
}
private bool SetArray(int[] values)
{
var numbers = new ulong[values.Length];
for (int i = 0; i < values.Length; i++)
{
numbers[i] = (ulong)values[i];
}
this.Value = numbers;
return true;
}
private bool SetArray(uint[] values)
{
var numbers = new ulong[values.Length];
for (int i = 0; i < values.Length; i++)
{
numbers[i] = (ulong)values[i];
}
this.Value = numbers;
return true;
}
private bool SetArray(short[] values)
{
var numbers = new ulong[values.Length];
for (int i = 0; i < values.Length; i++)
{
numbers[i] = (ulong)values[i];
}
this.Value = numbers;
return true;
}
private bool SetArray(ushort[] values)
{
var numbers = new ulong[values.Length];
for (int i = 0; i < values.Length; i++)
{
numbers[i] = (ulong)values[i];
}
this.Value = numbers;
return true;
}
}
}