Browse Source

Fix warnings

pull/3096/head
Brian Popow 4 years ago
parent
commit
ec8163a3fa
  1. 3
      src/ImageSharp/Formats/OpenExr/ExrAttribute.cs
  2. 87
      src/ImageSharp/Formats/OpenExr/ExrDecoderCore.cs
  3. 324
      src/ImageSharp/PixelFormats/PixelImplementations/Rgb96.cs
  4. 340
      src/ImageSharp/PixelFormats/PixelImplementations/Rgba128.cs
  5. 3
      tests/ImageSharp.Tests/Formats/Exr/ImageExtensionsTest.cs

3
src/ImageSharp/Formats/OpenExr/ExrAttribute.cs

@ -1,5 +1,5 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.. // Licensed under the Six Labors Split License.
using System.Diagnostics; using System.Diagnostics;
@ -23,4 +23,3 @@ internal class ExrAttribute
public int Length { get; } public int Length { get; }
} }

87
src/ImageSharp/Formats/OpenExr/ExrDecoderCore.cs

@ -96,7 +96,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals
ExrPixelType pixelType = this.ValidateChannels(); ExrPixelType pixelType = this.ValidateChannels();
this.ReadImageDataType(); this.ReadImageDataType();
Image<TPixel> image = new (this.configuration, this.Width, this.Height, this.metadata); Image<TPixel> image = new(this.configuration, this.Width, this.Height, this.metadata);
Buffer2D<TPixel> pixels = image.GetRootFramePixelBuffer(); Buffer2D<TPixel> pixels = image.GetRootFramePixelBuffer();
switch (pixelType) switch (pixelType)
@ -156,16 +156,15 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals
for (int channelIdx = 0; channelIdx < this.Channels.Count; channelIdx++) for (int channelIdx = 0; channelIdx < this.Channels.Count; channelIdx++)
{ {
ExrChannelInfo channel = this.Channels[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++) 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()); color.FromVector4(pixelValue.ToVector4());
pixelRow[x] = color; pixelRow[x] = color;
} }
} }
stream.Position = nextRowOffsetPosition; stream.Position = nextRowOffsetPosition;
@ -219,7 +218,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals
for (int x = 0; x < width; x++) 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()); color.FromVector4(pixelValue.ToVector4());
pixelRow[x] = color; pixelRow[x] = color;
} }
@ -227,7 +226,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals
} }
} }
private int ReadFloatChannelData( private static int ReadFloatChannelData(
BufferedReadStream stream, BufferedReadStream stream,
ExrChannelInfo channel, ExrChannelInfo channel,
Span<byte> decompressedPixelData, Span<byte> decompressedPixelData,
@ -240,19 +239,19 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals
switch (channel.ChannelName) switch (channel.ChannelName)
{ {
case ExrConstants.ChannelNames.Red: case ExrConstants.ChannelNames.Red:
return this.ReadChannelData(channel, decompressedPixelData, redPixelData, width); return ReadChannelData(channel, decompressedPixelData, redPixelData, width);
case ExrConstants.ChannelNames.Blue: case ExrConstants.ChannelNames.Blue:
return this.ReadChannelData(channel, decompressedPixelData, bluePixelData, width); return ReadChannelData(channel, decompressedPixelData, bluePixelData, width);
case ExrConstants.ChannelNames.Green: case ExrConstants.ChannelNames.Green:
return this.ReadChannelData(channel, decompressedPixelData, greenPixelData, width); return ReadChannelData(channel, decompressedPixelData, greenPixelData, width);
case ExrConstants.ChannelNames.Alpha: case ExrConstants.ChannelNames.Alpha:
return this.ReadChannelData(channel, decompressedPixelData, alphaPixelData, width); return ReadChannelData(channel, decompressedPixelData, alphaPixelData, width);
case ExrConstants.ChannelNames.Luminance: case ExrConstants.ChannelNames.Luminance:
int bytesRead = this.ReadChannelData(channel, decompressedPixelData, redPixelData, width); int bytesRead = ReadChannelData(channel, decompressedPixelData, redPixelData, width);
redPixelData.CopyTo(bluePixelData); redPixelData.CopyTo(bluePixelData);
redPixelData.CopyTo(greenPixelData); redPixelData.CopyTo(greenPixelData);
@ -279,19 +278,19 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals
switch (channel.ChannelName) switch (channel.ChannelName)
{ {
case ExrConstants.ChannelNames.Red: case ExrConstants.ChannelNames.Red:
return this.ReadChannelData(channel, decompressedPixelData, redPixelData, width); return ReadChannelData(channel, decompressedPixelData, redPixelData, width);
case ExrConstants.ChannelNames.Blue: case ExrConstants.ChannelNames.Blue:
return this.ReadChannelData(channel, decompressedPixelData, bluePixelData, width); return ReadChannelData(channel, decompressedPixelData, bluePixelData, width);
case ExrConstants.ChannelNames.Green: case ExrConstants.ChannelNames.Green:
return this.ReadChannelData(channel, decompressedPixelData, greenPixelData, width); return ReadChannelData(channel, decompressedPixelData, greenPixelData, width);
case ExrConstants.ChannelNames.Alpha: case ExrConstants.ChannelNames.Alpha:
return this.ReadChannelData(channel, decompressedPixelData, alphaPixelData, width); return ReadChannelData(channel, decompressedPixelData, alphaPixelData, width);
case ExrConstants.ChannelNames.Luminance: case ExrConstants.ChannelNames.Luminance:
int bytesRead = this.ReadChannelData(channel, decompressedPixelData, redPixelData, width); int bytesRead = ReadChannelData(channel, decompressedPixelData, redPixelData, width);
redPixelData.CopyTo(bluePixelData); redPixelData.CopyTo(bluePixelData);
redPixelData.CopyTo(greenPixelData); redPixelData.CopyTo(greenPixelData);
return bytesRead; return bytesRead;
@ -304,31 +303,31 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals
} }
} }
private int ReadChannelData(ExrChannelInfo channel, Span<byte> decompressedPixelData, Span<float> pixelData, int width) private static int ReadChannelData(ExrChannelInfo channel, Span<byte> decompressedPixelData, Span<float> pixelData, int width)
{ {
switch (channel.PixelType) switch (channel.PixelType)
{ {
case ExrPixelType.Half: case ExrPixelType.Half:
return this.ReadPixelRowChannelHalfSingle(decompressedPixelData, pixelData, width); return ReadPixelRowChannelHalfSingle(decompressedPixelData, pixelData, width);
case ExrPixelType.Float: case ExrPixelType.Float:
return this.ReadPixelRowChannelSingle(decompressedPixelData, pixelData, width); return ReadPixelRowChannelSingle(decompressedPixelData, pixelData, width);
} }
return 0; return 0;
} }
private int ReadChannelData(ExrChannelInfo channel, Span<byte> decompressedPixelData, Span<uint> pixelData, int width) private static int ReadChannelData(ExrChannelInfo channel, Span<byte> decompressedPixelData, Span<uint> pixelData, int width)
{ {
switch (channel.PixelType) switch (channel.PixelType)
{ {
case ExrPixelType.UnsignedInt: case ExrPixelType.UnsignedInt:
return this.ReadPixelRowChannelUnsignedInt(decompressedPixelData, pixelData, width); return ReadPixelRowChannelUnsignedInt(decompressedPixelData, pixelData, width);
} }
return 0; return 0;
} }
private int ReadPixelRowChannelHalfSingle(Span<byte> decompressedPixelData, Span<float> channelData, int width) private static int ReadPixelRowChannelHalfSingle(Span<byte> decompressedPixelData, Span<float> channelData, int width)
{ {
int offset = 0; int offset = 0;
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
@ -341,7 +340,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals
return offset; return offset;
} }
private int ReadPixelRowChannelSingle(Span<byte> decompressedPixelData, Span<float> channelData, int width) private static int ReadPixelRowChannelSingle(Span<byte> decompressedPixelData, Span<float> channelData, int width)
{ {
int offset = 0; int offset = 0;
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
@ -354,7 +353,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals
return offset; return offset;
} }
private int ReadPixelRowChannelUnsignedInt(Span<byte> decompressedPixelData, Span<uint> channelData, int width) private static int ReadPixelRowChannelUnsignedInt(Span<byte> decompressedPixelData, Span<uint> channelData, int width)
{ {
int offset = 0; int offset = 0;
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
@ -449,7 +448,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals
private ExrHeaderAttributes ParseHeaderAttributes(BufferedReadStream stream) private ExrHeaderAttributes ParseHeaderAttributes(BufferedReadStream stream)
{ {
ExrAttribute attribute = this.ReadAttribute(stream); ExrAttribute attribute = this.ReadAttribute(stream);
var header = new ExrHeaderAttributes(); ExrHeaderAttributes header = new();
while (!attribute.Equals(ExrAttribute.EmptyAttribute)) while (!attribute.Equals(ExrAttribute.EmptyAttribute))
{ {
@ -471,7 +470,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals
header.DisplayWindow = displayWindow; header.DisplayWindow = displayWindow;
break; break;
case ExrConstants.AttributeNames.LineOrder: case ExrConstants.AttributeNames.LineOrder:
var lineOrder = (ExrLineOrder)stream.ReadByte(); ExrLineOrder lineOrder = (ExrLineOrder)stream.ReadByte();
header.LineOrder = lineOrder; header.LineOrder = lineOrder;
break; break;
case ExrConstants.AttributeNames.PixelAspectRatio: case ExrConstants.AttributeNames.PixelAspectRatio:
@ -509,7 +508,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals
private ExrAttribute ReadAttribute(BufferedReadStream stream) private ExrAttribute ReadAttribute(BufferedReadStream stream)
{ {
string attributeName = ReadString(stream); string attributeName = ReadString(stream);
if (attributeName.Equals(string.Empty)) if (attributeName.Equals(string.Empty, StringComparison.Ordinal))
{ {
return ExrAttribute.EmptyAttribute; return ExrAttribute.EmptyAttribute;
} }
@ -533,7 +532,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals
private List<ExrChannelInfo> ReadChannelList(BufferedReadStream stream, int attributeSize) private List<ExrChannelInfo> ReadChannelList(BufferedReadStream stream, int attributeSize)
{ {
var channels = new List<ExrChannelInfo>(); List<ExrChannelInfo> channels = new();
while (attributeSize > 1) while (attributeSize > 1)
{ {
ExrChannelInfo channelInfo = this.ReadChannelInfo(stream, out int bytesRead); ExrChannelInfo channelInfo = this.ReadChannelInfo(stream, out int bytesRead);
@ -552,7 +551,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals
string channelName = ReadString(stream); string channelName = ReadString(stream);
bytesRead = channelName.Length + 1; bytesRead = channelName.Length + 1;
var pixelType = (ExrPixelType)this.ReadSignedInteger(stream); ExrPixelType pixelType = (ExrPixelType)this.ReadSignedInteger(stream);
bytesRead += 4; bytesRead += 4;
byte pLinear = (byte)stream.ReadByte(); byte pLinear = (byte)stream.ReadByte();
@ -572,7 +571,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals
private static string ReadString(BufferedReadStream stream) private static string ReadString(BufferedReadStream stream)
{ {
var str = new StringBuilder(); StringBuilder str = new();
int character = stream.ReadByte(); int character = stream.ReadByte();
if (character == 0) if (character == 0)
{ {
@ -599,11 +598,11 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals
ExrPixelType? pixelType = null; ExrPixelType? pixelType = null;
for (int i = 0; i < this.Channels.Count; i++) for (int i = 0; i < this.Channels.Count; i++)
{ {
if (this.Channels[i].ChannelName.Equals(ExrConstants.ChannelNames.Blue) || if (this.Channels[i].ChannelName.Equals(ExrConstants.ChannelNames.Blue, StringComparison.Ordinal) ||
this.Channels[i].ChannelName.Equals(ExrConstants.ChannelNames.Green) || this.Channels[i].ChannelName.Equals(ExrConstants.ChannelNames.Green, StringComparison.Ordinal) ||
this.Channels[i].ChannelName.Equals(ExrConstants.ChannelNames.Red) || this.Channels[i].ChannelName.Equals(ExrConstants.ChannelNames.Red, StringComparison.Ordinal) ||
this.Channels[i].ChannelName.Equals(ExrConstants.ChannelNames.Alpha) || this.Channels[i].ChannelName.Equals(ExrConstants.ChannelNames.Alpha, StringComparison.Ordinal) ||
this.Channels[i].ChannelName.Equals(ExrConstants.ChannelNames.Luminance)) this.Channels[i].ChannelName.Equals(ExrConstants.ChannelNames.Luminance, StringComparison.Ordinal))
{ {
if (!pixelType.HasValue) if (!pixelType.HasValue)
{ {
@ -651,27 +650,27 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals
bool hasLuminance = false; bool hasLuminance = false;
foreach (ExrChannelInfo channelInfo in this.Channels) foreach (ExrChannelInfo channelInfo in this.Channels)
{ {
if (channelInfo.ChannelName.Equals("A")) if (channelInfo.ChannelName.Equals("A", StringComparison.Ordinal))
{ {
hasAlphaChannel = true; hasAlphaChannel = true;
} }
if (channelInfo.ChannelName.Equals("R")) if (channelInfo.ChannelName.Equals("R", StringComparison.Ordinal))
{ {
hasRedChannel = true; hasRedChannel = true;
} }
if (channelInfo.ChannelName.Equals("G")) if (channelInfo.ChannelName.Equals("G", StringComparison.Ordinal))
{ {
hasGreenChannel = true; hasGreenChannel = true;
} }
if (channelInfo.ChannelName.Equals("B")) if (channelInfo.ChannelName.Equals("B", StringComparison.Ordinal))
{ {
hasBlueChannel = true; hasBlueChannel = true;
} }
if (channelInfo.ChannelName.Equals("Y")) if (channelInfo.ChannelName.Equals("Y", StringComparison.Ordinal))
{ {
hasLuminance = true; hasLuminance = true;
} }
@ -702,7 +701,7 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals
{ {
foreach (ExrChannelInfo channelInfo in this.Channels) foreach (ExrChannelInfo channelInfo in this.Channels)
{ {
if (channelInfo.ChannelName.Equals("A")) if (channelInfo.ChannelName.Equals("A", StringComparison.Ordinal))
{ {
return true; return true;
} }
@ -716,7 +715,11 @@ internal sealed class ExrDecoderCore : IImageDecoderInternals
uint bytesPerRow = 0; uint bytesPerRow = 0;
foreach (ExrChannelInfo channelInfo in this.Channels) 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) if (channelInfo.PixelType == ExrPixelType.Half)
{ {

324
src/ImageSharp/PixelFormats/PixelImplementations/Rgb96.cs

@ -1,175 +1,173 @@
// Copyright (c) Six Labors. // 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.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace SixLabors.ImageSharp.PixelFormats namespace SixLabors.ImageSharp.PixelFormats;
/// <summary>
/// Pixel type containing three 32-bit unsigned normalized values ranging from 0 to 4294967295.
/// The color components are stored in red, green, blue.
/// <para>
/// Ranges from [0, 0, 0] to [1, 1, 1] in vector form.
/// </para>
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public partial struct Rgb96 : IPixel<Rgb96>
{ {
private const float InvMax = 1.0f / uint.MaxValue;
private const double Max = uint.MaxValue;
/// <summary>
/// Gets the red component.
/// </summary>
public uint R;
/// <summary>
/// Gets the green component.
/// </summary>
public uint G;
/// <summary>
/// Gets the blue component.
/// </summary>
public uint B;
/// <summary>
/// Initializes a new instance of the <see cref="Rgb96"/> struct.
/// </summary>
/// <param name="r">The red component.</param>
/// <param name="g">The green component.</param>
/// <param name="b">The blue component.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Rgb96(uint r, uint g, uint b)
{
this.R = r;
this.G = g;
this.B = b;
}
/// <summary>
/// Compares two <see cref="Rgb96"/> objects for equality.
/// </summary>
/// <param name="left">The <see cref="Rgb96"/> on the left side of the operand.</param>
/// <returns>
/// True if the <paramref name="left"/> parameter is equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
/// <param name="right">The <see cref="Rgb96"/> on the right side of the operand.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Rgb96 left, Rgb96 right) => left.Equals(right);
/// <summary> /// <summary>
/// Pixel type containing three 32-bit unsigned normalized values ranging from 0 to 4294967295. /// Compares two <see cref="Rgb96"/> objects for equality.
/// The color components are stored in red, green, blue.
/// <para>
/// Ranges from [0, 0, 0] to [1, 1, 1] in vector form.
/// </para>
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential)] /// <param name="left">The <see cref="Rgb96"/> on the left side of the operand.</param>
public partial struct Rgb96 : IPixel<Rgb96> /// <param name="right">The <see cref="Rgb96"/> 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 !=(Rgb96 left, Rgb96 right) => !left.Equals(right);
/// <inheritdoc />
public PixelOperations<Rgb96> CreatePixelOperations() => new();
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromScaledVector4(Vector4 vector) => this.FromVector4(vector);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector4 ToScaledVector4() => this.ToVector4();
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromVector4(Vector4 vector)
{ {
private const float InvMax = 1.0f / uint.MaxValue; vector = Numerics.Clamp(vector, Vector4.Zero, Vector4.One);
this.R = (uint)(vector.X * Max);
private const double Max = uint.MaxValue; this.G = (uint)(vector.Y * Max);
this.B = (uint)(vector.Z * Max);
/// <summary>
/// Gets the red component.
/// </summary>
public uint R;
/// <summary>
/// Gets the green component.
/// </summary>
public uint G;
/// <summary>
/// Gets the blue component.
/// </summary>
public uint B;
/// <summary>
/// Initializes a new instance of the <see cref="Rgb96"/> struct.
/// </summary>
/// <param name="r">The red component.</param>
/// <param name="g">The green component.</param>
/// <param name="b">The blue component.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Rgb96(uint r, uint g, uint b)
{
this.R = r;
this.G = g;
this.B = b;
}
/// <summary>
/// Compares two <see cref="Rgb96"/> objects for equality.
/// </summary>
/// <param name="left">The <see cref="Rgb96"/> on the left side of the operand.</param>
/// <returns>
/// True if the <paramref name="left"/> parameter is equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
/// <param name="right">The <see cref="Rgb96"/> on the right side of the operand.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Rgb96 left, Rgb96 right) => left.Equals(right);
/// <summary>
/// Compares two <see cref="Rgb96"/> objects for equality.
/// </summary>
/// <param name="left">The <see cref="Rgb96"/> on the left side of the operand.</param>
/// <param name="right">The <see cref="Rgb96"/> 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 !=(Rgb96 left, Rgb96 right) => !left.Equals(right);
/// <inheritdoc />
public PixelOperations<Rgb96> CreatePixelOperations() => new();
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromScaledVector4(Vector4 vector) => this.FromVector4(vector);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector4 ToScaledVector4() => this.ToVector4();
/// <inheritdoc />
[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);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector4 ToVector4() => new(
this.R * InvMax,
this.G * InvMax,
this.B * InvMax,
1.0f);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromBgra5551(Bgra5551 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromArgb32(Argb32 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromBgr24(Bgr24 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromBgra32(Bgra32 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromL8(L8 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromL16(L16 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromLa16(La16 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromLa32(La32 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromRgb24(Rgb24 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromRgba32(Rgba32 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromAbgr32(Abgr32 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgba32(ref Rgba32 dest) => dest.FromScaledVector4(this.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromRgb48(Rgb48 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromRgba64(Rgba64 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
public override bool Equals(object obj) => obj is Rgb96 other && this.Equals(other);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode() => HashCode.Combine(this.R, this.G, this.B);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Rgb96 other) => this.R.Equals(other.R) && this.G.Equals(other.G) && this.B.Equals(other.B);
/// <inheritdoc />
public override string ToString() => FormattableString.Invariant($"Rgb96({this.R}, {this.G}, {this.B})");
} }
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector4 ToVector4() => new(
this.R * InvMax,
this.G * InvMax,
this.B * InvMax,
1.0f);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromBgra5551(Bgra5551 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromArgb32(Argb32 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromBgr24(Bgr24 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromBgra32(Bgra32 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromL8(L8 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromL16(L16 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromLa16(La16 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromLa32(La32 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromRgb24(Rgb24 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromRgba32(Rgba32 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromAbgr32(Abgr32 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgba32(ref Rgba32 dest) => dest.FromScaledVector4(this.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromRgb48(Rgb48 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromRgba64(Rgba64 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
public override bool Equals(object obj) => obj is Rgb96 other && this.Equals(other);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode() => HashCode.Combine(this.R, this.G, this.B);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Rgb96 other) => this.R.Equals(other.R) && this.G.Equals(other.G) && this.B.Equals(other.B);
/// <inheritdoc />
public override string ToString() => FormattableString.Invariant($"Rgb96({this.R}, {this.G}, {this.B})");
} }

340
src/ImageSharp/PixelFormats/PixelImplementations/Rgba128.cs

@ -1,183 +1,181 @@
// Copyright (c) Six Labors. // 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.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace SixLabors.ImageSharp.PixelFormats namespace SixLabors.ImageSharp.PixelFormats;
/// <summary>
/// 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.
/// <para>
/// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form.
/// </para>
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public partial struct Rgba128 : IPixel<Rgba128>
{ {
private const float InvMax = 1.0f / uint.MaxValue;
private const double Max = uint.MaxValue;
/// <summary>
/// Gets the red component.
/// </summary>
public uint R;
/// <summary>
/// Gets the green component.
/// </summary>
public uint G;
/// <summary>
/// Gets the blue component.
/// </summary>
public uint B;
/// <summary> /// <summary>
/// Pixel type containing four 32-bit unsigned normalized values ranging from 0 to 4294967295. /// Gets the alpha channel.
/// The color components are stored in red, green, blue and alpha.
/// <para>
/// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form.
/// </para>
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential)] public uint A;
public partial struct Rgba128 : IPixel<Rgba128>
/// <summary>
/// Initializes a new instance of the <see cref="Rgba128"/> struct.
/// </summary>
/// <param name="r">The red component.</param>
/// <param name="g">The green component.</param>
/// <param name="b">The blue component.</param>
/// <param name="a">The alpha component.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Rgba128(uint r, uint g, uint b, uint a)
{ {
private const float InvMax = 1.0f / uint.MaxValue; this.R = r;
this.G = g;
private const double Max = uint.MaxValue; this.B = b;
this.A = a;
/// <summary>
/// Gets the red component.
/// </summary>
public uint R;
/// <summary>
/// Gets the green component.
/// </summary>
public uint G;
/// <summary>
/// Gets the blue component.
/// </summary>
public uint B;
/// <summary>
/// Gets the alpha channel.
/// </summary>
public uint A;
/// <summary>
/// Initializes a new instance of the <see cref="Rgba128"/> struct.
/// </summary>
/// <param name="r">The red component.</param>
/// <param name="g">The green component.</param>
/// <param name="b">The blue component.</param>
/// <param name="a">The alpha component.</param>
[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;
}
/// <summary>
/// Compares two <see cref="Rgba128"/> objects for equality.
/// </summary>
/// <param name="left">The <see cref="Rgba128"/> on the left side of the operand.</param>
/// <returns>
/// True if the <paramref name="left"/> parameter is equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
/// <param name="right">The <see cref="Rgba128"/> on the right side of the operand.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Rgba128 left, Rgba128 right) => left.Equals(right);
/// <summary>
/// Compares two <see cref="Rgba128"/> objects for equality.
/// </summary>
/// <param name="left">The <see cref="Rgba128"/> on the left side of the operand.</param>
/// <param name="right">The <see cref="Rgba128"/> 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 !=(Rgba128 left, Rgba128 right) => !left.Equals(right);
/// <inheritdoc />
public PixelOperations<Rgba128> CreatePixelOperations() => new();
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromScaledVector4(Vector4 vector) => this.FromVector4(vector);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector4 ToScaledVector4() => this.ToVector4();
/// <inheritdoc />
[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);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector4 ToVector4() => new(
this.R * InvMax,
this.G * InvMax,
this.B * InvMax,
this.A * InvMax);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromBgra5551(Bgra5551 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromArgb32(Argb32 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromBgr24(Bgr24 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromBgra32(Bgra32 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromL8(L8 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromL16(L16 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromLa16(La16 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromLa32(La32 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromRgb24(Rgb24 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromRgba32(Rgba32 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromAbgr32(Abgr32 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgba32(ref Rgba32 dest) => dest.FromScaledVector4(this.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromRgb48(Rgb48 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromRgba64(Rgba64 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
public override bool Equals(object obj) => obj is Rgba128 other && this.Equals(other);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode() => HashCode.Combine(this.R, this.G, this.B, this.A);
/// <inheritdoc />
[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);
/// <inheritdoc />
public override string ToString() => FormattableString.Invariant($"Rgba128({this.R}, {this.G}, {this.B}, {this.A})");
} }
/// <summary>
/// Compares two <see cref="Rgba128"/> objects for equality.
/// </summary>
/// <param name="left">The <see cref="Rgba128"/> on the left side of the operand.</param>
/// <returns>
/// True if the <paramref name="left"/> parameter is equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
/// <param name="right">The <see cref="Rgba128"/> on the right side of the operand.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Rgba128 left, Rgba128 right) => left.Equals(right);
/// <summary>
/// Compares two <see cref="Rgba128"/> objects for equality.
/// </summary>
/// <param name="left">The <see cref="Rgba128"/> on the left side of the operand.</param>
/// <param name="right">The <see cref="Rgba128"/> 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 !=(Rgba128 left, Rgba128 right) => !left.Equals(right);
/// <inheritdoc />
public PixelOperations<Rgba128> CreatePixelOperations() => new();
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromScaledVector4(Vector4 vector) => this.FromVector4(vector);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector4 ToScaledVector4() => this.ToVector4();
/// <inheritdoc />
[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);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector4 ToVector4() => new(
this.R * InvMax,
this.G * InvMax,
this.B * InvMax,
this.A * InvMax);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromBgra5551(Bgra5551 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromArgb32(Argb32 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromBgr24(Bgr24 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromBgra32(Bgra32 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromL8(L8 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromL16(L16 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromLa16(La16 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromLa32(La32 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromRgb24(Rgb24 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromRgba32(Rgba32 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromAbgr32(Abgr32 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgba32(ref Rgba32 dest) => dest.FromScaledVector4(this.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromRgb48(Rgb48 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FromRgba64(Rgba64 source) => this.FromScaledVector4(source.ToScaledVector4());
/// <inheritdoc />
public override bool Equals(object obj) => obj is Rgba128 other && this.Equals(other);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode() => HashCode.Combine(this.R, this.G, this.B, this.A);
/// <inheritdoc />
[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);
/// <inheritdoc />
public override string ToString() => FormattableString.Invariant($"Rgba128({this.R}, {this.G}, {this.B}, {this.A})");
} }

3
tests/ImageSharp.Tests/Formats/Exr/ImageExtensionsTest.cs

@ -1,5 +1,5 @@
// Copyright (c) Six Labors. // 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;
using SixLabors.ImageSharp.Formats.OpenExr; using SixLabors.ImageSharp.Formats.OpenExr;
@ -64,4 +64,3 @@ public class ImageExtensionsTest
} }
} }
} }

Loading…
Cancel
Save