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.
198 lines
6.8 KiB
198 lines
6.8 KiB
// Copyright (c) Six Labors and contributors.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
|
using System.Numerics;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace SixLabors.ImageSharp.PixelFormats
|
|
{
|
|
/// <summary>
|
|
/// Pixel type containing three 8-bit unsigned normalized values ranging from 0 to 255.
|
|
/// The color components are stored in blue, green, red order (least significant to most significant byte).
|
|
/// <para>
|
|
/// Ranges from [0, 0, 0, 1] to [1, 1, 1, 1] in vector form.
|
|
/// </para>
|
|
/// </summary>
|
|
[StructLayout(LayoutKind.Explicit)]
|
|
public partial struct Bgr24 : IPixel<Bgr24>
|
|
{
|
|
/// <summary>
|
|
/// The blue component.
|
|
/// </summary>
|
|
[FieldOffset(0)]
|
|
public byte B;
|
|
|
|
/// <summary>
|
|
/// The green component.
|
|
/// </summary>
|
|
[FieldOffset(1)]
|
|
public byte G;
|
|
|
|
/// <summary>
|
|
/// The red component.
|
|
/// </summary>
|
|
[FieldOffset(2)]
|
|
public byte R;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="Bgr24"/> struct.
|
|
/// </summary>
|
|
/// <param name="r">The red component.</param>
|
|
/// <param name="g">The green component.</param>
|
|
/// <param name="b">The blue component.</param>
|
|
[MethodImpl(InliningOptions.ShortMethod)]
|
|
public Bgr24(byte r, byte g, byte b)
|
|
{
|
|
this.R = r;
|
|
this.G = g;
|
|
this.B = b;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Compares two <see cref="Bgr24"/> objects for equality.
|
|
/// </summary>
|
|
/// <param name="left">The <see cref="Bgr24"/> on the left side of the operand.</param>
|
|
/// <param name="right">The <see cref="Bgr24"/> 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(InliningOptions.ShortMethod)]
|
|
public static bool operator ==(Bgr24 left, Bgr24 right) => left.Equals(right);
|
|
|
|
/// <summary>
|
|
/// Compares two <see cref="Bgr24"/> objects for equality.
|
|
/// </summary>
|
|
/// <param name="left">The <see cref="Bgr24"/> on the left side of the operand.</param>
|
|
/// <param name="right">The <see cref="Bgr24"/> 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(InliningOptions.ShortMethod)]
|
|
public static bool operator !=(Bgr24 left, Bgr24 right) => !left.Equals(right);
|
|
|
|
/// <inheritdoc/>
|
|
public PixelOperations<Bgr24> CreatePixelOperations() => new PixelOperations();
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(InliningOptions.ShortMethod)]
|
|
public void PackFromScaledVector4(Vector4 vector) => this.PackFromVector4(vector);
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(InliningOptions.ShortMethod)]
|
|
public Vector4 ToScaledVector4() => this.ToVector4();
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(InliningOptions.ShortMethod)]
|
|
public void PackFromVector4(Vector4 vector)
|
|
{
|
|
Rgba32 rgba = default;
|
|
rgba.PackFromVector4(vector);
|
|
this.PackFromRgba32(rgba);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(InliningOptions.ShortMethod)]
|
|
public Vector4 ToVector4() => new Rgba32(this.R, this.G, this.B, byte.MaxValue).ToVector4();
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(InliningOptions.ShortMethod)]
|
|
public void PackFromArgb32(Argb32 source)
|
|
{
|
|
this.R = source.R;
|
|
this.G = source.G;
|
|
this.B = source.B;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(InliningOptions.ShortMethod)]
|
|
public void PackFromBgr24(Bgr24 source) => this = source;
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(InliningOptions.ShortMethod)]
|
|
public void PackFromBgra32(Bgra32 source)
|
|
{
|
|
this.R = source.R;
|
|
this.G = source.G;
|
|
this.B = source.B;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(InliningOptions.ShortMethod)]
|
|
public void PackFromGray8(Gray8 source)
|
|
{
|
|
this.R = source.PackedValue;
|
|
this.G = source.PackedValue;
|
|
this.B = source.PackedValue;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(InliningOptions.ShortMethod)]
|
|
public void PackFromGray16(Gray16 source)
|
|
{
|
|
byte rgb = ImageMaths.DownScaleFrom16BitTo8Bit(source.PackedValue);
|
|
this.R = rgb;
|
|
this.G = rgb;
|
|
this.B = rgb;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(InliningOptions.ShortMethod)]
|
|
public void PackFromRgb24(Rgb24 source)
|
|
{
|
|
this.R = source.R;
|
|
this.G = source.G;
|
|
this.B = source.B;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(InliningOptions.ShortMethod)]
|
|
public void PackFromRgba32(Rgba32 source) => this = source.Bgr;
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(InliningOptions.ShortMethod)]
|
|
public void ToRgba32(ref Rgba32 dest)
|
|
{
|
|
dest.R = this.R;
|
|
dest.G = this.G;
|
|
dest.B = this.B;
|
|
dest.A = byte.MaxValue;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(InliningOptions.ShortMethod)]
|
|
public void PackFromRgb48(Rgb48 source)
|
|
{
|
|
this.R = ImageMaths.DownScaleFrom16BitTo8Bit(source.R);
|
|
this.G = ImageMaths.DownScaleFrom16BitTo8Bit(source.G);
|
|
this.B = ImageMaths.DownScaleFrom16BitTo8Bit(source.B);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(InliningOptions.ShortMethod)]
|
|
public void PackFromRgba64(Rgba64 source)
|
|
{
|
|
this.R = ImageMaths.DownScaleFrom16BitTo8Bit(source.R);
|
|
this.G = ImageMaths.DownScaleFrom16BitTo8Bit(source.G);
|
|
this.B = ImageMaths.DownScaleFrom16BitTo8Bit(source.B);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(InliningOptions.ShortMethod)]
|
|
public bool Equals(Bgr24 other) => this.R.Equals(other.R) && this.G.Equals(other.G) && this.B.Equals(other.B);
|
|
|
|
/// <inheritdoc/>
|
|
public override bool Equals(object obj) => obj is Bgr24 other && this.Equals(other);
|
|
|
|
/// <inheritdoc />
|
|
public override string ToString() => $"Bgra({this.B}, {this.G}, {this.R})";
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(InliningOptions.ShortMethod)]
|
|
public override int GetHashCode()
|
|
{
|
|
int hash = HashHelpers.Combine(this.R.GetHashCode(), this.G.GetHashCode());
|
|
return HashHelpers.Combine(hash, this.B.GetHashCode());
|
|
}
|
|
}
|
|
}
|