From ec8163a3fabe4880347286b6fb7edd7c7c972335 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Fri, 23 Sep 2022 19:49:00 +0200 Subject: [PATCH] Fix warnings --- .../Formats/OpenExr/ExrAttribute.cs | 3 +- .../Formats/OpenExr/ExrDecoderCore.cs | 87 ++--- .../PixelImplementations/Rgb96.cs | 324 +++++++++-------- .../PixelImplementations/Rgba128.cs | 340 +++++++++--------- .../Formats/Exr/ImageExtensionsTest.cs | 3 +- 5 files changed, 377 insertions(+), 380 deletions(-) diff --git a/src/ImageSharp/Formats/OpenExr/ExrAttribute.cs b/src/ImageSharp/Formats/OpenExr/ExrAttribute.cs index ad5a8290dc..0d6ca86cc9 100644 --- a/src/ImageSharp/Formats/OpenExr/ExrAttribute.cs +++ b/src/ImageSharp/Formats/OpenExr/ExrAttribute.cs @@ -1,5 +1,5 @@ // Copyright (c) Six Labors. -// Licensed under the Six Labors Split License.. +// Licensed under the Six Labors Split License. using System.Diagnostics; @@ -23,4 +23,3 @@ internal class ExrAttribute public int Length { get; } } - diff --git a/src/ImageSharp/Formats/OpenExr/ExrDecoderCore.cs b/src/ImageSharp/Formats/OpenExr/ExrDecoderCore.cs index 2304f3f203..3e554fb74d 100644 --- a/src/ImageSharp/Formats/OpenExr/ExrDecoderCore.cs +++ b/src/ImageSharp/Formats/OpenExr/ExrDecoderCore.cs @@ -96,7 +96,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals ExrPixelType pixelType = this.ValidateChannels(); this.ReadImageDataType(); - Image image = new (this.configuration, this.Width, this.Height, this.metadata); + Image image = new(this.configuration, this.Width, this.Height, this.metadata); Buffer2D pixels = image.GetRootFramePixelBuffer(); switch (pixelType) @@ -156,16 +156,15 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals for (int channelIdx = 0; channelIdx < this.Channels.Count; channelIdx++) { ExrChannelInfo channel = this.Channels[channelIdx]; - offset += this.ReadFloatChannelData(stream, channel, decompressedPixelData.Slice(offset), redPixelData, greenPixelData, bluePixelData, alphaPixelData, width); + offset += ReadFloatChannelData(stream, channel, decompressedPixelData.Slice(offset), redPixelData, greenPixelData, bluePixelData, alphaPixelData, width); } for (int x = 0; x < width; x++) { - var pixelValue = new HalfVector4(redPixelData[x], greenPixelData[x], bluePixelData[x], hasAlpha ? alphaPixelData[x] : 1.0f); + HalfVector4 pixelValue = new(redPixelData[x], greenPixelData[x], bluePixelData[x], hasAlpha ? alphaPixelData[x] : 1.0f); color.FromVector4(pixelValue.ToVector4()); pixelRow[x] = color; } - } stream.Position = nextRowOffsetPosition; @@ -219,7 +218,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals for (int x = 0; x < width; x++) { - var pixelValue = new Rgba128(redPixelData[x], greenPixelData[x], bluePixelData[x], hasAlpha ? alphaPixelData[x] : uint.MaxValue); + Rgba128 pixelValue = new(redPixelData[x], greenPixelData[x], bluePixelData[x], hasAlpha ? alphaPixelData[x] : uint.MaxValue); color.FromVector4(pixelValue.ToVector4()); pixelRow[x] = color; } @@ -227,7 +226,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals } } - private int ReadFloatChannelData( + private static int ReadFloatChannelData( BufferedReadStream stream, ExrChannelInfo channel, Span decompressedPixelData, @@ -240,19 +239,19 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals switch (channel.ChannelName) { case ExrConstants.ChannelNames.Red: - return this.ReadChannelData(channel, decompressedPixelData, redPixelData, width); + return ReadChannelData(channel, decompressedPixelData, redPixelData, width); case ExrConstants.ChannelNames.Blue: - return this.ReadChannelData(channel, decompressedPixelData, bluePixelData, width); + return ReadChannelData(channel, decompressedPixelData, bluePixelData, width); case ExrConstants.ChannelNames.Green: - return this.ReadChannelData(channel, decompressedPixelData, greenPixelData, width); + return ReadChannelData(channel, decompressedPixelData, greenPixelData, width); case ExrConstants.ChannelNames.Alpha: - return this.ReadChannelData(channel, decompressedPixelData, alphaPixelData, width); + return ReadChannelData(channel, decompressedPixelData, alphaPixelData, width); case ExrConstants.ChannelNames.Luminance: - int bytesRead = this.ReadChannelData(channel, decompressedPixelData, redPixelData, width); + int bytesRead = ReadChannelData(channel, decompressedPixelData, redPixelData, width); redPixelData.CopyTo(bluePixelData); redPixelData.CopyTo(greenPixelData); @@ -279,19 +278,19 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals switch (channel.ChannelName) { case ExrConstants.ChannelNames.Red: - return this.ReadChannelData(channel, decompressedPixelData, redPixelData, width); + return ReadChannelData(channel, decompressedPixelData, redPixelData, width); case ExrConstants.ChannelNames.Blue: - return this.ReadChannelData(channel, decompressedPixelData, bluePixelData, width); + return ReadChannelData(channel, decompressedPixelData, bluePixelData, width); case ExrConstants.ChannelNames.Green: - return this.ReadChannelData(channel, decompressedPixelData, greenPixelData, width); + return ReadChannelData(channel, decompressedPixelData, greenPixelData, width); case ExrConstants.ChannelNames.Alpha: - return this.ReadChannelData(channel, decompressedPixelData, alphaPixelData, width); + return ReadChannelData(channel, decompressedPixelData, alphaPixelData, width); case ExrConstants.ChannelNames.Luminance: - int bytesRead = this.ReadChannelData(channel, decompressedPixelData, redPixelData, width); + int bytesRead = ReadChannelData(channel, decompressedPixelData, redPixelData, width); redPixelData.CopyTo(bluePixelData); redPixelData.CopyTo(greenPixelData); return bytesRead; @@ -304,31 +303,31 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals } } - private int ReadChannelData(ExrChannelInfo channel, Span decompressedPixelData, Span pixelData, int width) + private static int ReadChannelData(ExrChannelInfo channel, Span decompressedPixelData, Span pixelData, int width) { switch (channel.PixelType) { case ExrPixelType.Half: - return this.ReadPixelRowChannelHalfSingle(decompressedPixelData, pixelData, width); + return ReadPixelRowChannelHalfSingle(decompressedPixelData, pixelData, width); case ExrPixelType.Float: - return this.ReadPixelRowChannelSingle(decompressedPixelData, pixelData, width); + return ReadPixelRowChannelSingle(decompressedPixelData, pixelData, width); } return 0; } - private int ReadChannelData(ExrChannelInfo channel, Span decompressedPixelData, Span pixelData, int width) + private static int ReadChannelData(ExrChannelInfo channel, Span decompressedPixelData, Span pixelData, int width) { switch (channel.PixelType) { case ExrPixelType.UnsignedInt: - return this.ReadPixelRowChannelUnsignedInt(decompressedPixelData, pixelData, width); + return ReadPixelRowChannelUnsignedInt(decompressedPixelData, pixelData, width); } return 0; } - private int ReadPixelRowChannelHalfSingle(Span decompressedPixelData, Span channelData, int width) + private static int ReadPixelRowChannelHalfSingle(Span decompressedPixelData, Span channelData, int width) { int offset = 0; for (int x = 0; x < width; x++) @@ -341,7 +340,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals return offset; } - private int ReadPixelRowChannelSingle(Span decompressedPixelData, Span channelData, int width) + private static int ReadPixelRowChannelSingle(Span decompressedPixelData, Span channelData, int width) { int offset = 0; for (int x = 0; x < width; x++) @@ -354,7 +353,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals return offset; } - private int ReadPixelRowChannelUnsignedInt(Span decompressedPixelData, Span channelData, int width) + private static int ReadPixelRowChannelUnsignedInt(Span decompressedPixelData, Span channelData, int width) { int offset = 0; for (int x = 0; x < width; x++) @@ -449,7 +448,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals private ExrHeaderAttributes ParseHeaderAttributes(BufferedReadStream stream) { ExrAttribute attribute = this.ReadAttribute(stream); - var header = new ExrHeaderAttributes(); + ExrHeaderAttributes header = new(); while (!attribute.Equals(ExrAttribute.EmptyAttribute)) { @@ -471,7 +470,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals header.DisplayWindow = displayWindow; break; case ExrConstants.AttributeNames.LineOrder: - var lineOrder = (ExrLineOrder)stream.ReadByte(); + ExrLineOrder lineOrder = (ExrLineOrder)stream.ReadByte(); header.LineOrder = lineOrder; break; case ExrConstants.AttributeNames.PixelAspectRatio: @@ -509,7 +508,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals private ExrAttribute ReadAttribute(BufferedReadStream stream) { string attributeName = ReadString(stream); - if (attributeName.Equals(string.Empty)) + if (attributeName.Equals(string.Empty, StringComparison.Ordinal)) { return ExrAttribute.EmptyAttribute; } @@ -533,7 +532,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals private List ReadChannelList(BufferedReadStream stream, int attributeSize) { - var channels = new List(); + List channels = new(); while (attributeSize > 1) { ExrChannelInfo channelInfo = this.ReadChannelInfo(stream, out int bytesRead); @@ -552,7 +551,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals string channelName = ReadString(stream); bytesRead = channelName.Length + 1; - var pixelType = (ExrPixelType)this.ReadSignedInteger(stream); + ExrPixelType pixelType = (ExrPixelType)this.ReadSignedInteger(stream); bytesRead += 4; byte pLinear = (byte)stream.ReadByte(); @@ -572,7 +571,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals private static string ReadString(BufferedReadStream stream) { - var str = new StringBuilder(); + StringBuilder str = new(); int character = stream.ReadByte(); if (character == 0) { @@ -599,11 +598,11 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals ExrPixelType? pixelType = null; for (int i = 0; i < this.Channels.Count; i++) { - if (this.Channels[i].ChannelName.Equals(ExrConstants.ChannelNames.Blue) || - this.Channels[i].ChannelName.Equals(ExrConstants.ChannelNames.Green) || - this.Channels[i].ChannelName.Equals(ExrConstants.ChannelNames.Red) || - this.Channels[i].ChannelName.Equals(ExrConstants.ChannelNames.Alpha) || - this.Channels[i].ChannelName.Equals(ExrConstants.ChannelNames.Luminance)) + if (this.Channels[i].ChannelName.Equals(ExrConstants.ChannelNames.Blue, StringComparison.Ordinal) || + this.Channels[i].ChannelName.Equals(ExrConstants.ChannelNames.Green, StringComparison.Ordinal) || + this.Channels[i].ChannelName.Equals(ExrConstants.ChannelNames.Red, StringComparison.Ordinal) || + this.Channels[i].ChannelName.Equals(ExrConstants.ChannelNames.Alpha, StringComparison.Ordinal) || + this.Channels[i].ChannelName.Equals(ExrConstants.ChannelNames.Luminance, StringComparison.Ordinal)) { if (!pixelType.HasValue) { @@ -651,27 +650,27 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals bool hasLuminance = false; foreach (ExrChannelInfo channelInfo in this.Channels) { - if (channelInfo.ChannelName.Equals("A")) + if (channelInfo.ChannelName.Equals("A", StringComparison.Ordinal)) { hasAlphaChannel = true; } - if (channelInfo.ChannelName.Equals("R")) + if (channelInfo.ChannelName.Equals("R", StringComparison.Ordinal)) { hasRedChannel = true; } - if (channelInfo.ChannelName.Equals("G")) + if (channelInfo.ChannelName.Equals("G", StringComparison.Ordinal)) { hasGreenChannel = true; } - if (channelInfo.ChannelName.Equals("B")) + if (channelInfo.ChannelName.Equals("B", StringComparison.Ordinal)) { hasBlueChannel = true; } - if (channelInfo.ChannelName.Equals("Y")) + if (channelInfo.ChannelName.Equals("Y", StringComparison.Ordinal)) { hasLuminance = true; } @@ -702,7 +701,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals { foreach (ExrChannelInfo channelInfo in this.Channels) { - if (channelInfo.ChannelName.Equals("A")) + if (channelInfo.ChannelName.Equals("A", StringComparison.Ordinal)) { return true; } @@ -716,7 +715,11 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals uint bytesPerRow = 0; foreach (ExrChannelInfo channelInfo in this.Channels) { - if (channelInfo.ChannelName.Equals("A") || channelInfo.ChannelName.Equals("R") || channelInfo.ChannelName.Equals("G") || channelInfo.ChannelName.Equals("B") || channelInfo.ChannelName.Equals("Y")) + if (channelInfo.ChannelName.Equals("A", StringComparison.Ordinal) + || channelInfo.ChannelName.Equals("R", StringComparison.Ordinal) + || channelInfo.ChannelName.Equals("G", StringComparison.Ordinal) + || channelInfo.ChannelName.Equals("B", StringComparison.Ordinal) + || channelInfo.ChannelName.Equals("Y", StringComparison.Ordinal)) { if (channelInfo.PixelType == ExrPixelType.Half) { diff --git a/src/ImageSharp/PixelFormats/PixelImplementations/Rgb96.cs b/src/ImageSharp/PixelFormats/PixelImplementations/Rgb96.cs index 2809a4c319..620e305a21 100644 --- a/src/ImageSharp/PixelFormats/PixelImplementations/Rgb96.cs +++ b/src/ImageSharp/PixelFormats/PixelImplementations/Rgb96.cs @@ -1,175 +1,173 @@ // Copyright (c) Six Labors. -// Licensed under the Apache License, Version 2.0. +// Licensed under the Six Labors Split License. -using System; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -namespace SixLabors.ImageSharp.PixelFormats +namespace SixLabors.ImageSharp.PixelFormats; + +/// +/// Pixel type containing three 32-bit unsigned normalized values ranging from 0 to 4294967295. +/// The color components are stored in red, green, blue. +/// +/// Ranges from [0, 0, 0] to [1, 1, 1] in vector form. +/// +/// +[StructLayout(LayoutKind.Sequential)] +public partial struct Rgb96 : IPixel { + private const float InvMax = 1.0f / uint.MaxValue; + + private const double Max = uint.MaxValue; + + /// + /// Gets the red component. + /// + public uint R; + + /// + /// Gets the green component. + /// + public uint G; + + /// + /// Gets the blue component. + /// + public uint B; + + /// + /// Initializes a new instance of the struct. + /// + /// The red component. + /// The green component. + /// The blue component. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Rgb96(uint r, uint g, uint b) + { + this.R = r; + this.G = g; + this.B = b; + } + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// + /// True if the parameter is equal to the parameter; otherwise, false. + /// + /// The on the right side of the operand. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(Rgb96 left, Rgb96 right) => left.Equals(right); + /// - /// Pixel type containing three 32-bit unsigned normalized values ranging from 0 to 4294967295. - /// The color components are stored in red, green, blue. - /// - /// Ranges from [0, 0, 0] to [1, 1, 1] in vector form. - /// + /// Compares two objects for equality. /// - [StructLayout(LayoutKind.Sequential)] - public partial struct Rgb96 : IPixel + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is not equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(Rgb96 left, Rgb96 right) => !left.Equals(right); + + /// + public PixelOperations CreatePixelOperations() => new(); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromScaledVector4(Vector4 vector) => this.FromVector4(vector); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Vector4 ToScaledVector4() => this.ToVector4(); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromVector4(Vector4 vector) { - private const float InvMax = 1.0f / uint.MaxValue; - - private const double Max = uint.MaxValue; - - /// - /// Gets the red component. - /// - public uint R; - - /// - /// Gets the green component. - /// - public uint G; - - /// - /// Gets the blue component. - /// - public uint B; - - /// - /// Initializes a new instance of the struct. - /// - /// The red component. - /// The green component. - /// The blue component. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Rgb96(uint r, uint g, uint b) - { - this.R = r; - this.G = g; - this.B = b; - } - - /// - /// Compares two objects for equality. - /// - /// The on the left side of the operand. - /// - /// True if the parameter is equal to the parameter; otherwise, false. - /// - /// The on the right side of the operand. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool operator ==(Rgb96 left, Rgb96 right) => left.Equals(right); - - /// - /// Compares two objects for equality. - /// - /// The on the left side of the operand. - /// The on the right side of the operand. - /// - /// True if the parameter is not equal to the parameter; otherwise, false. - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool operator !=(Rgb96 left, Rgb96 right) => !left.Equals(right); - - /// - public PixelOperations CreatePixelOperations() => new(); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromScaledVector4(Vector4 vector) => this.FromVector4(vector); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Vector4 ToScaledVector4() => this.ToVector4(); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromVector4(Vector4 vector) - { - vector = Numerics.Clamp(vector, Vector4.Zero, Vector4.One); - this.R = (uint)(vector.X * Max); - this.G = (uint)(vector.Y * Max); - this.B = (uint)(vector.Z * Max); - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Vector4 ToVector4() => new( - this.R * InvMax, - this.G * InvMax, - this.B * InvMax, - 1.0f); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromBgra5551(Bgra5551 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromArgb32(Argb32 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromBgr24(Bgr24 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromBgra32(Bgra32 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromL8(L8 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromL16(L16 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromLa16(La16 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromLa32(La32 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromRgb24(Rgb24 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromRgba32(Rgba32 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromAbgr32(Abgr32 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToRgba32(ref Rgba32 dest) => dest.FromScaledVector4(this.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromRgb48(Rgb48 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromRgba64(Rgba64 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - public override bool Equals(object obj) => obj is Rgb96 other && this.Equals(other); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public override int GetHashCode() => HashCode.Combine(this.R, this.G, this.B); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Equals(Rgb96 other) => this.R.Equals(other.R) && this.G.Equals(other.G) && this.B.Equals(other.B); - - /// - public override string ToString() => FormattableString.Invariant($"Rgb96({this.R}, {this.G}, {this.B})"); + vector = Numerics.Clamp(vector, Vector4.Zero, Vector4.One); + this.R = (uint)(vector.X * Max); + this.G = (uint)(vector.Y * Max); + this.B = (uint)(vector.Z * Max); } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Vector4 ToVector4() => new( + this.R * InvMax, + this.G * InvMax, + this.B * InvMax, + 1.0f); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromBgra5551(Bgra5551 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromArgb32(Argb32 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromBgr24(Bgr24 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromBgra32(Bgra32 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromL8(L8 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromL16(L16 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromLa16(La16 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromLa32(La32 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromRgb24(Rgb24 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromRgba32(Rgba32 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromAbgr32(Abgr32 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void ToRgba32(ref Rgba32 dest) => dest.FromScaledVector4(this.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromRgb48(Rgb48 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromRgba64(Rgba64 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + public override bool Equals(object obj) => obj is Rgb96 other && this.Equals(other); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override int GetHashCode() => HashCode.Combine(this.R, this.G, this.B); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool Equals(Rgb96 other) => this.R.Equals(other.R) && this.G.Equals(other.G) && this.B.Equals(other.B); + + /// + public override string ToString() => FormattableString.Invariant($"Rgb96({this.R}, {this.G}, {this.B})"); } diff --git a/src/ImageSharp/PixelFormats/PixelImplementations/Rgba128.cs b/src/ImageSharp/PixelFormats/PixelImplementations/Rgba128.cs index 01fc16d45a..912a62db0c 100644 --- a/src/ImageSharp/PixelFormats/PixelImplementations/Rgba128.cs +++ b/src/ImageSharp/PixelFormats/PixelImplementations/Rgba128.cs @@ -1,183 +1,181 @@ // Copyright (c) Six Labors. -// Licensed under the Apache License, Version 2.0. +// Licensed under the Six Labors Split License. -using System; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -namespace SixLabors.ImageSharp.PixelFormats +namespace SixLabors.ImageSharp.PixelFormats; + +/// +/// Pixel type containing four 32-bit unsigned normalized values ranging from 0 to 4294967295. +/// The color components are stored in red, green, blue and alpha. +/// +/// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form. +/// +/// +[StructLayout(LayoutKind.Sequential)] +public partial struct Rgba128 : IPixel { + private const float InvMax = 1.0f / uint.MaxValue; + + private const double Max = uint.MaxValue; + + /// + /// Gets the red component. + /// + public uint R; + + /// + /// Gets the green component. + /// + public uint G; + + /// + /// Gets the blue component. + /// + public uint B; + /// - /// Pixel type containing four 32-bit unsigned normalized values ranging from 0 to 4294967295. - /// The color components are stored in red, green, blue and alpha. - /// - /// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form. - /// + /// Gets the alpha channel. /// - [StructLayout(LayoutKind.Sequential)] - public partial struct Rgba128 : IPixel + public uint A; + + /// + /// Initializes a new instance of the struct. + /// + /// The red component. + /// The green component. + /// The blue component. + /// The alpha component. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Rgba128(uint r, uint g, uint b, uint a) { - private const float InvMax = 1.0f / uint.MaxValue; - - private const double Max = uint.MaxValue; - - /// - /// Gets the red component. - /// - public uint R; - - /// - /// Gets the green component. - /// - public uint G; - - /// - /// Gets the blue component. - /// - public uint B; - - /// - /// Gets the alpha channel. - /// - public uint A; - - /// - /// Initializes a new instance of the struct. - /// - /// The red component. - /// The green component. - /// The blue component. - /// The alpha component. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Rgba128(uint r, uint g, uint b, uint a) - { - this.R = r; - this.G = g; - this.B = b; - this.A = a; - } - - /// - /// Compares two objects for equality. - /// - /// The on the left side of the operand. - /// - /// True if the parameter is equal to the parameter; otherwise, false. - /// - /// The on the right side of the operand. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool operator ==(Rgba128 left, Rgba128 right) => left.Equals(right); - - /// - /// Compares two objects for equality. - /// - /// The on the left side of the operand. - /// The on the right side of the operand. - /// - /// True if the parameter is not equal to the parameter; otherwise, false. - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool operator !=(Rgba128 left, Rgba128 right) => !left.Equals(right); - - /// - public PixelOperations CreatePixelOperations() => new(); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromScaledVector4(Vector4 vector) => this.FromVector4(vector); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Vector4 ToScaledVector4() => this.ToVector4(); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromVector4(Vector4 vector) - { - vector = Numerics.Clamp(vector, Vector4.Zero, Vector4.One); - this.R = (uint)(vector.X * Max); - this.G = (uint)(vector.Y * Max); - this.B = (uint)(vector.Z * Max); - this.A = (uint)(vector.W * Max); - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Vector4 ToVector4() => new( - this.R * InvMax, - this.G * InvMax, - this.B * InvMax, - this.A * InvMax); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromBgra5551(Bgra5551 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromArgb32(Argb32 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromBgr24(Bgr24 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromBgra32(Bgra32 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromL8(L8 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromL16(L16 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromLa16(La16 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromLa32(La32 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromRgb24(Rgb24 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromRgba32(Rgba32 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromAbgr32(Abgr32 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToRgba32(ref Rgba32 dest) => dest.FromScaledVector4(this.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromRgb48(Rgb48 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void FromRgba64(Rgba64 source) => this.FromScaledVector4(source.ToScaledVector4()); - - /// - public override bool Equals(object obj) => obj is Rgba128 other && this.Equals(other); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public override int GetHashCode() => HashCode.Combine(this.R, this.G, this.B, this.A); - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Equals(Rgba128 other) => this.R.Equals(other.R) && this.G.Equals(other.G) && this.B.Equals(other.B) && this.A.Equals(other.A); - - /// - public override string ToString() => FormattableString.Invariant($"Rgba128({this.R}, {this.G}, {this.B}, {this.A})"); + this.R = r; + this.G = g; + this.B = b; + this.A = a; } + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// + /// True if the parameter is equal to the parameter; otherwise, false. + /// + /// The on the right side of the operand. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(Rgba128 left, Rgba128 right) => left.Equals(right); + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is not equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(Rgba128 left, Rgba128 right) => !left.Equals(right); + + /// + public PixelOperations CreatePixelOperations() => new(); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromScaledVector4(Vector4 vector) => this.FromVector4(vector); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Vector4 ToScaledVector4() => this.ToVector4(); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromVector4(Vector4 vector) + { + vector = Numerics.Clamp(vector, Vector4.Zero, Vector4.One); + this.R = (uint)(vector.X * Max); + this.G = (uint)(vector.Y * Max); + this.B = (uint)(vector.Z * Max); + this.A = (uint)(vector.W * Max); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Vector4 ToVector4() => new( + this.R * InvMax, + this.G * InvMax, + this.B * InvMax, + this.A * InvMax); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromBgra5551(Bgra5551 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromArgb32(Argb32 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromBgr24(Bgr24 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromBgra32(Bgra32 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromL8(L8 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromL16(L16 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromLa16(La16 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromLa32(La32 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromRgb24(Rgb24 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromRgba32(Rgba32 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromAbgr32(Abgr32 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void ToRgba32(ref Rgba32 dest) => dest.FromScaledVector4(this.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromRgb48(Rgb48 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void FromRgba64(Rgba64 source) => this.FromScaledVector4(source.ToScaledVector4()); + + /// + public override bool Equals(object obj) => obj is Rgba128 other && this.Equals(other); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override int GetHashCode() => HashCode.Combine(this.R, this.G, this.B, this.A); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool Equals(Rgba128 other) => this.R.Equals(other.R) && this.G.Equals(other.G) && this.B.Equals(other.B) && this.A.Equals(other.A); + + /// + public override string ToString() => FormattableString.Invariant($"Rgba128({this.R}, {this.G}, {this.B}, {this.A})"); } diff --git a/tests/ImageSharp.Tests/Formats/Exr/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Exr/ImageExtensionsTest.cs index 323000bc59..926f5ca909 100644 --- a/tests/ImageSharp.Tests/Formats/Exr/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Exr/ImageExtensionsTest.cs @@ -1,5 +1,5 @@ // Copyright (c) Six Labors. -// Licensed under the Apache License, Version 2.0. +// Licensed under the Six Labors Split License. using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.OpenExr; @@ -64,4 +64,3 @@ public class ImageExtensionsTest } } } -