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.
227 lines
7.3 KiB
227 lines
7.3 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 a single 8 bit normalized W values.
|
|
/// <para>
|
|
/// Ranges from [0, 0, 0, 0] to [0, 0, 0, 1] in vector form.
|
|
/// </para>
|
|
/// </summary>
|
|
public struct Alpha8 : IPixel<Alpha8>, IPackedVector<byte>
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="Alpha8"/> struct.
|
|
/// </summary>
|
|
/// <param name="alpha">The alpha component</param>
|
|
public Alpha8(float alpha)
|
|
{
|
|
this.PackedValue = Pack(alpha);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public byte PackedValue { get; set; }
|
|
|
|
/// <summary>
|
|
/// Compares two <see cref="Alpha8"/> objects for equality.
|
|
/// </summary>
|
|
/// <param name="left">
|
|
/// The <see cref="Alpha8"/> on the left side of the operand.
|
|
/// </param>
|
|
/// <param name="right">
|
|
/// The <see cref="Alpha8"/> on the right side of the operand.
|
|
/// </param>
|
|
/// <returns>
|
|
/// True if the <paramref name="left"/> parameter is equal to the <paramref name="right"/> parameter; otherwise, false.
|
|
/// </returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static bool operator ==(Alpha8 left, Alpha8 right)
|
|
{
|
|
return left.PackedValue == right.PackedValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Compares two <see cref="Alpha8"/> objects for equality.
|
|
/// </summary>
|
|
/// <param name="left">The <see cref="Alpha8"/> on the left side of the operand.</param>
|
|
/// <param name="right">The <see cref="Alpha8"/> 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 !=(Alpha8 left, Alpha8 right)
|
|
{
|
|
return left.PackedValue != right.PackedValue;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public PixelOperations<Alpha8> CreatePixelOperations() => new PixelOperations<Alpha8>();
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void PackFromScaledVector4(Vector4 vector)
|
|
{
|
|
this.PackFromVector4(vector);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public Vector4 ToScaledVector4()
|
|
{
|
|
return this.ToVector4();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void PackFromVector4(Vector4 vector)
|
|
{
|
|
this.PackedValue = Pack(vector.W);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public Vector4 ToVector4()
|
|
{
|
|
return new Vector4(0, 0, 0, this.PackedValue / 255F);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void PackFromRgba32(Rgba32 source)
|
|
{
|
|
this.PackedValue = source.A;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void PackFromArgb32(Argb32 source)
|
|
{
|
|
this.PackedValue = source.A;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void PackFromBgra32(Bgra32 source)
|
|
{
|
|
this.PackedValue = source.A;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void ToRgb24(ref Rgb24 dest)
|
|
{
|
|
dest = default(Rgb24);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void ToRgba32(ref Rgba32 dest)
|
|
{
|
|
dest.R = 0;
|
|
dest.G = 0;
|
|
dest.B = 0;
|
|
dest.A = this.PackedValue;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void ToArgb32(ref Argb32 dest)
|
|
{
|
|
dest.R = 0;
|
|
dest.G = 0;
|
|
dest.B = 0;
|
|
dest.A = this.PackedValue;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void ToBgr24(ref Bgr24 dest)
|
|
{
|
|
dest = default(Bgr24);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void ToBgra32(ref Bgra32 dest)
|
|
{
|
|
dest.R = 0;
|
|
dest.G = 0;
|
|
dest.B = 0;
|
|
dest.A = this.PackedValue;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void PackFromRgb48(Rgb48 source) => this.PackedValue = byte.MaxValue;
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void ToRgb48(ref Rgb48 dest)
|
|
{
|
|
dest.R = 0;
|
|
dest.G = 0;
|
|
dest.B = 0;
|
|
}
|
|
|
|
/// <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());
|
|
|
|
/// <summary>
|
|
/// Compares an object with the packed vector.
|
|
/// </summary>
|
|
/// <param name="obj">The object to compare.</param>
|
|
/// <returns>True if the object is equal to the packed vector.</returns>
|
|
public override bool Equals(object obj)
|
|
{
|
|
return obj is Alpha8 other && this.Equals(other);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Compares another Alpha8 packed vector with the packed vector.
|
|
/// </summary>
|
|
/// <param name="other">The Alpha8 packed vector to compare.</param>
|
|
/// <returns>True if the packed vectors are equal.</returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public bool Equals(Alpha8 other)
|
|
{
|
|
return this.PackedValue == other.PackedValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a string representation of the packed vector.
|
|
/// </summary>
|
|
/// <returns>A string representation of the packed vector.</returns>
|
|
public override string ToString()
|
|
{
|
|
return (this.PackedValue / 255F).ToString();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public override int GetHashCode()
|
|
{
|
|
return this.PackedValue.GetHashCode();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Packs a <see cref="float"/> into a byte.
|
|
/// </summary>
|
|
/// <param name="alpha">The float containing the value to pack.</param>
|
|
/// <returns>The <see cref="byte"/> containing the packed values.</returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private static byte Pack(float alpha)
|
|
{
|
|
return (byte)Math.Round(alpha.Clamp(0, 1) * 255F);
|
|
}
|
|
}
|
|
}
|