mirror of https://github.com/SixLabors/ImageSharp
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.
298 lines
11 KiB
298 lines
11 KiB
// Copyright (c) Six Labors and contributors.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
|
using System;
|
|
using System.Numerics;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
namespace SixLabors.ImageSharp.PixelFormats
|
|
{
|
|
/// <summary>
|
|
/// Packed pixel type containing four 16-bit signed integer values.
|
|
/// <para>
|
|
/// Ranges from [-37267, -37267, -37267, -37267] to [37267, 37267, 37267, 37267] in vector form.
|
|
/// </para>
|
|
/// </summary>
|
|
public struct Short4 : IPixel<Short4>, IPackedVector<ulong>
|
|
{
|
|
/// <summary>
|
|
/// The maximum byte value.
|
|
/// </summary>
|
|
private static readonly Vector4 MaxBytes = new Vector4(255);
|
|
|
|
/// <summary>
|
|
/// The half the maximum byte value.
|
|
/// </summary>
|
|
private static readonly Vector4 Half = new Vector4(127);
|
|
|
|
/// <summary>
|
|
/// The vector value used for rounding.
|
|
/// </summary>
|
|
private static readonly Vector4 Round = new Vector4(.5F);
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="Short4"/> struct.
|
|
/// </summary>
|
|
/// <param name="vector">A vector containing the initial values for the components.</param>
|
|
public Short4(Vector4 vector)
|
|
{
|
|
this.PackedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="Short4"/> struct.
|
|
/// </summary>
|
|
/// <param name="x">The x-component.</param>
|
|
/// <param name="y">The y-component.</param>
|
|
/// <param name="z">The z-component.</param>
|
|
/// <param name="w">The w-component.</param>
|
|
public Short4(float x, float y, float z, float w)
|
|
{
|
|
this.PackedValue = Pack(x, y, z, w);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public ulong PackedValue { get; set; }
|
|
|
|
/// <summary>
|
|
/// Compares two <see cref="Short4"/> objects for equality.
|
|
/// </summary>
|
|
/// <param name="left">
|
|
/// The <see cref="Short4"/> on the left side of the operand.
|
|
/// </param>
|
|
/// <param name="right">
|
|
/// The <see cref="Short4"/> on the right side of the operand.
|
|
/// </param>
|
|
/// <returns>
|
|
/// True if the <paramref name="left"/> parameter is not equal to the <paramref name="right"/> parameter; otherwise, false.
|
|
/// </returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static bool operator ==(Short4 left, Short4 right)
|
|
{
|
|
return left.PackedValue == right.PackedValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Compares two <see cref="Short4"/> objects for equality.
|
|
/// </summary>
|
|
/// <param name="left">
|
|
/// The <see cref="Short4"/> on the left side of the operand.
|
|
/// </param>
|
|
/// <param name="right">
|
|
/// The <see cref="Short4"/> on the right side of the operand.
|
|
/// </param>
|
|
/// <returns>
|
|
/// True if the <paramref name="left"/> parameter is not equal to the <paramref name="right"/> parameter; otherwise, false.
|
|
/// </returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static bool operator !=(Short4 left, Short4 right)
|
|
{
|
|
return left.PackedValue != right.PackedValue;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public PixelOperations<Short4> CreatePixelOperations() => new PixelOperations<Short4>();
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void PackFromScaledVector4(Vector4 vector)
|
|
{
|
|
vector *= 65534F;
|
|
vector -= new Vector4(32767F);
|
|
this.PackFromVector4(vector);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public Vector4 ToScaledVector4()
|
|
{
|
|
var scaled = this.ToVector4();
|
|
scaled += new Vector4(32767F);
|
|
scaled /= 65534F;
|
|
return scaled;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void PackFromVector4(Vector4 vector)
|
|
{
|
|
this.PackedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public Vector4 ToVector4()
|
|
{
|
|
return new Vector4(
|
|
(short)(this.PackedValue & 0xFFFF),
|
|
(short)((this.PackedValue >> 0x10) & 0xFFFF),
|
|
(short)((this.PackedValue >> 0x20) & 0xFFFF),
|
|
(short)((this.PackedValue >> 0x30) & 0xFFFF));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void PackFromRgba32(Rgba32 source)
|
|
{
|
|
var vector = source.ToVector4();
|
|
vector *= 65534;
|
|
vector -= new Vector4(32767);
|
|
this.PackedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void PackFromArgb32(Argb32 source)
|
|
{
|
|
var vector = source.ToVector4();
|
|
vector *= 65534;
|
|
vector -= new Vector4(32767);
|
|
this.PackedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void PackFromBgra32(Bgra32 source)
|
|
{
|
|
var vector = source.ToVector4();
|
|
vector *= 65534;
|
|
vector -= new Vector4(32767);
|
|
this.PackedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void ToRgb24(ref Rgb24 dest)
|
|
{
|
|
Vector4 vector = this.ToByteScaledVector4();
|
|
dest.R = (byte)MathF.Round(vector.X);
|
|
dest.G = (byte)MathF.Round(vector.Y);
|
|
dest.B = (byte)MathF.Round(vector.Z);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void ToRgba32(ref Rgba32 dest)
|
|
{
|
|
Vector4 vector = this.ToByteScaledVector4();
|
|
dest.R = (byte)MathF.Round(vector.X);
|
|
dest.G = (byte)MathF.Round(vector.Y);
|
|
dest.B = (byte)MathF.Round(vector.Z);
|
|
dest.A = (byte)MathF.Round(vector.W);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void ToArgb32(ref Argb32 dest)
|
|
{
|
|
Vector4 vector = this.ToByteScaledVector4();
|
|
dest.R = (byte)MathF.Round(vector.X);
|
|
dest.G = (byte)MathF.Round(vector.Y);
|
|
dest.B = (byte)MathF.Round(vector.Z);
|
|
dest.A = (byte)MathF.Round(vector.W);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void ToBgr24(ref Bgr24 dest)
|
|
{
|
|
Vector4 vector = this.ToByteScaledVector4();
|
|
dest.R = (byte)MathF.Round(vector.X);
|
|
dest.G = (byte)MathF.Round(vector.Y);
|
|
dest.B = (byte)MathF.Round(vector.Z);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void ToBgra32(ref Bgra32 dest)
|
|
{
|
|
Vector4 vector = this.ToByteScaledVector4();
|
|
dest.R = (byte)MathF.Round(vector.X);
|
|
dest.G = (byte)MathF.Round(vector.Y);
|
|
dest.B = (byte)MathF.Round(vector.Z);
|
|
dest.A = (byte)MathF.Round(vector.W);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void PackFromRgb48(Rgb48 source) => this.PackFromScaledVector4(source.ToScaledVector4());
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void ToRgb48(ref Rgb48 dest) => dest.PackFromScaledVector4(this.ToScaledVector4());
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void PackFromRgba64(Rgba64 source) => this.PackFromScaledVector4(source.ToScaledVector4());
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void ToRgba64(ref Rgba64 dest) => dest.PackFromScaledVector4(this.ToScaledVector4());
|
|
|
|
/// <inheritdoc />
|
|
public override bool Equals(object obj)
|
|
{
|
|
return obj is Short4 other && this.Equals(other);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public bool Equals(Short4 other)
|
|
{
|
|
return this == other;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the hash code for the current instance.
|
|
/// </summary>
|
|
/// <returns>Hash code for the instance.</returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public override int GetHashCode() => this.PackedValue.GetHashCode();
|
|
|
|
/// <summary>
|
|
/// Returns a string representation of the current instance.
|
|
/// </summary>
|
|
/// <returns>String that represents the object.</returns>
|
|
public override string ToString()
|
|
{
|
|
return this.PackedValue.ToString("x16");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Packs the components into a <see cref="ulong"/>.
|
|
/// </summary>
|
|
/// <param name="x">The x-component</param>
|
|
/// <param name="y">The y-component</param>
|
|
/// <param name="z">The z-component</param>
|
|
/// <param name="w">The w-component</param>
|
|
/// <returns>The <see cref="ulong"/> containing the packed values.</returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private static ulong Pack(float x, float y, float z, float w)
|
|
{
|
|
// Largest two byte positive number 0xFFFF >> 1;
|
|
const float MaxPos = 0x7FFF;
|
|
|
|
// Two's complement
|
|
const float MinNeg = ~(int)MaxPos;
|
|
|
|
// Clamp the value between min and max values
|
|
ulong word4 = ((ulong)Math.Round(x.Clamp(MinNeg, MaxPos)) & 0xFFFF) << 0x00;
|
|
ulong word3 = ((ulong)Math.Round(y.Clamp(MinNeg, MaxPos)) & 0xFFFF) << 0x10;
|
|
ulong word2 = ((ulong)Math.Round(z.Clamp(MinNeg, MaxPos)) & 0xFFFF) << 0x20;
|
|
ulong word1 = ((ulong)Math.Round(w.Clamp(MinNeg, MaxPos)) & 0xFFFF) << 0x30;
|
|
|
|
return word4 | word3 | word2 | word1;
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private Vector4 ToByteScaledVector4()
|
|
{
|
|
var vector = this.ToVector4();
|
|
vector /= 65534;
|
|
vector *= 255;
|
|
vector += Half;
|
|
vector += Round;
|
|
return Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
|
|
}
|
|
}
|
|
}
|