private void InternalCopyRationalValues <TDest>(ReadOnlySpan <byte> buffer, Span <TDest> values, Func <TiffRational, TDest>?convertFunc = null) where TDest : struct
        {
            ReadOnlySpan <byte> src  = buffer.Slice(0, 8 * values.Length);
            bool reverseEndianNeeded = _reverseEndianNeeded;

            if (typeof(TDest) == typeof(TiffRational) && convertFunc is null)
            {
                Span <TiffRational> dest = MemoryMarshal.Cast <TDest, TiffRational>(values);

                for (int i = 0; i < values.Length; i++)
                {
                    uint numerator   = MemoryMarshal.Read <uint>(src);
                    uint denominator = MemoryMarshal.Read <uint>(src.Slice(4));
                    if (reverseEndianNeeded)
                    {
                        numerator   = BinaryPrimitives.ReverseEndianness(numerator);
                        denominator = BinaryPrimitives.ReverseEndianness(denominator);
                    }

                    dest[i] = new TiffRational(numerator, denominator);
                    src     = src.Slice(8);
                }
            }
            else if (typeof(TDest) == typeof(TiffSRational) && convertFunc is null)
            {
                Span <TiffSRational> dest = MemoryMarshal.Cast <TDest, TiffSRational>(values);

                for (int i = 0; i < values.Length; i++)
                {
                    int numerator   = MemoryMarshal.Read <int>(src);
                    int denominator = MemoryMarshal.Read <int>(src.Slice(4));
                    if (reverseEndianNeeded)
                    {
                        numerator   = BinaryPrimitives.ReverseEndianness(numerator);
                        denominator = BinaryPrimitives.ReverseEndianness(denominator);
                    }

                    dest[i] = new TiffSRational(numerator, denominator);
                    src     = src.Slice(8);
                }
            }
            else
            {
                if (convertFunc is null)
                {
                    throw new ArgumentNullException(nameof(convertFunc));
                }

                for (int i = 0; i < values.Length; i++)
                {
                    uint numerator   = MemoryMarshal.Read <uint>(src);
                    uint denominator = MemoryMarshal.Read <uint>(src.Slice(4));
                    if (reverseEndianNeeded)
                    {
                        numerator   = BinaryPrimitives.ReverseEndianness(numerator);
                        denominator = BinaryPrimitives.ReverseEndianness(denominator);
                    }

                    values[i] = convertFunc(new TiffRational(numerator, denominator));
                    src       = src.Slice(8);
                }
            }
        }
Beispiel #2
0
 /// <inheritdoc />
 public bool Equals(TiffRational other)
 {
     return(Numerator == other.Numerator && Denominator == other.Denominator);
 }