diff --git a/src/ImageSharp.ruleset b/src/ImageSharp.ruleset
index f29278c95..72e0cd1b0 100644
--- a/src/ImageSharp.ruleset
+++ b/src/ImageSharp.ruleset
@@ -1,6 +1,7 @@
-
+
+
-
+
\ No newline at end of file
diff --git a/src/ImageSharp/Color/Color.cs b/src/ImageSharp/Color/Color.cs
index 3b91a78d1..82ecab390 100644
--- a/src/ImageSharp/Color/Color.cs
+++ b/src/ImageSharp/Color/Color.cs
@@ -248,15 +248,12 @@ public readonly partial struct Color : IEquatable
[MethodImpl(InliningOptions.ShortMethod)]
public string ToHex()
{
- Rgba32 rgba = default;
if (this.boxedHighPrecisionPixel is not null)
{
- this.boxedHighPrecisionPixel.ToRgba32(ref rgba);
- return rgba.ToHex();
+ return this.boxedHighPrecisionPixel.ToRgba32().ToHex();
}
- rgba.FromScaledVector4(this.data);
- return rgba.ToHex();
+ return Rgba32.FromScaledVector4(this.data).ToHex();
}
///
@@ -278,14 +275,10 @@ public readonly partial struct Color : IEquatable
if (this.boxedHighPrecisionPixel is null)
{
- pixel = default;
- pixel.FromScaledVector4(this.data);
- return pixel;
+ return TPixel.FromScaledVector4(this.data);
}
- pixel = default;
- pixel.FromScaledVector4(this.boxedHighPrecisionPixel.ToScaledVector4());
- return pixel;
+ return TPixel.FromScaledVector4(this.boxedHighPrecisionPixel.ToScaledVector4());
}
///
diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
index 863fed359..bed489752 100644
--- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
+++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
@@ -294,72 +294,60 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
private void ReadRle(BufferedReadStream stream, BmpCompression compression, Buffer2D pixels, byte[] colors, int width, int height, bool inverted)
where TPixel : unmanaged, IPixel
{
- TPixel color = default;
- using (IMemoryOwner buffer = this.memoryAllocator.Allocate(width * height, AllocationOptions.Clean))
- using (IMemoryOwner undefinedPixels = this.memoryAllocator.Allocate(width * height, AllocationOptions.Clean))
- using (IMemoryOwner rowsWithUndefinedPixels = this.memoryAllocator.Allocate(height, AllocationOptions.Clean))
+ using IMemoryOwner buffer = this.memoryAllocator.Allocate(width * height, AllocationOptions.Clean);
+ using IMemoryOwner undefinedPixels = this.memoryAllocator.Allocate(width * height, AllocationOptions.Clean);
+ using IMemoryOwner rowsWithUndefinedPixels = this.memoryAllocator.Allocate(height, AllocationOptions.Clean);
+ Span rowsWithUndefinedPixelsSpan = rowsWithUndefinedPixels.Memory.Span;
+ Span undefinedPixelsSpan = undefinedPixels.Memory.Span;
+ Span bufferSpan = buffer.Memory.Span;
+ if (compression is BmpCompression.RLE8)
{
- Span rowsWithUndefinedPixelsSpan = rowsWithUndefinedPixels.Memory.Span;
- Span undefinedPixelsSpan = undefinedPixels.Memory.Span;
- Span bufferSpan = buffer.Memory.Span;
- if (compression is BmpCompression.RLE8)
- {
- this.UncompressRle8(stream, width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan);
- }
- else
- {
- this.UncompressRle4(stream, width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan);
- }
+ this.UncompressRle8(stream, width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan);
+ }
+ else
+ {
+ this.UncompressRle4(stream, width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan);
+ }
- for (int y = 0; y < height; y++)
- {
- int newY = Invert(y, height, inverted);
- int rowStartIdx = y * width;
- Span bufferRow = bufferSpan.Slice(rowStartIdx, width);
- Span pixelRow = pixels.DangerousGetRowSpan(newY);
+ for (int y = 0; y < height; y++)
+ {
+ int newY = Invert(y, height, inverted);
+ int rowStartIdx = y * width;
+ Span bufferRow = bufferSpan.Slice(rowStartIdx, width);
+ Span pixelRow = pixels.DangerousGetRowSpan(newY);
- bool rowHasUndefinedPixels = rowsWithUndefinedPixelsSpan[y];
- if (rowHasUndefinedPixels)
+ bool rowHasUndefinedPixels = rowsWithUndefinedPixelsSpan[y];
+ if (rowHasUndefinedPixels)
+ {
+ // Slow path with undefined pixels.
+ for (int x = 0; x < width; x++)
{
- // Slow path with undefined pixels.
- for (int x = 0; x < width; x++)
+ byte colorIdx = bufferRow[x];
+ if (undefinedPixelsSpan[rowStartIdx + x])
{
- byte colorIdx = bufferRow[x];
- if (undefinedPixelsSpan[rowStartIdx + x])
- {
- switch (this.rleSkippedPixelHandling)
- {
- case RleSkippedPixelHandling.FirstColorOfPalette:
- color.FromBgr24(Unsafe.As(ref colors[colorIdx * 4]));
- break;
- case RleSkippedPixelHandling.Transparent:
- color.FromScaledVector4(Vector4.Zero);
- break;
-
- // Default handling for skipped pixels is black (which is what System.Drawing is also doing).
- default:
- color.FromScaledVector4(new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
- break;
- }
- }
- else
+ pixelRow[x] = this.rleSkippedPixelHandling switch
{
- color.FromBgr24(Unsafe.As(ref colors[colorIdx * 4]));
- }
+ RleSkippedPixelHandling.FirstColorOfPalette => TPixel.FromBgr24(Unsafe.As(ref colors[colorIdx * 4])),
+ RleSkippedPixelHandling.Transparent => TPixel.FromScaledVector4(Vector4.Zero),
- pixelRow[x] = color;
+ // Default handling for skipped pixels is black (which is what System.Drawing is also doing).
+ _ => TPixel.FromScaledVector4(new Vector4(0.0f, 0.0f, 0.0f, 1.0f)),
+ };
}
- }
- else
- {
- // Fast path without any undefined pixels.
- for (int x = 0; x < width; x++)
+ else
{
- color.FromBgr24(Unsafe.As(ref colors[bufferRow[x] * 4]));
- pixelRow[x] = color;
+ pixelRow[x] = TPixel.FromBgr24(Unsafe.As(ref colors[colorIdx * 4]));
}
}
}
+ else
+ {
+ // Fast path without any undefined pixels.
+ for (int x = 0; x < width; x++)
+ {
+ pixelRow[x] = TPixel.FromBgr24(Unsafe.As(ref colors[bufferRow[x] * 4]));
+ }
+ }
}
}
@@ -375,66 +363,54 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
private void ReadRle24(BufferedReadStream stream, Buffer2D pixels, int width, int height, bool inverted)
where TPixel : unmanaged, IPixel
{
- TPixel color = default;
- using (IMemoryOwner buffer = this.memoryAllocator.Allocate(width * height * 3, AllocationOptions.Clean))
- using (IMemoryOwner undefinedPixels = this.memoryAllocator.Allocate(width * height, AllocationOptions.Clean))
- using (IMemoryOwner rowsWithUndefinedPixels = this.memoryAllocator.Allocate(height, AllocationOptions.Clean))
- {
- Span rowsWithUndefinedPixelsSpan = rowsWithUndefinedPixels.Memory.Span;
- Span undefinedPixelsSpan = undefinedPixels.Memory.Span;
- Span bufferSpan = buffer.GetSpan();
+ using IMemoryOwner buffer = this.memoryAllocator.Allocate(width * height * 3, AllocationOptions.Clean);
+ using IMemoryOwner undefinedPixels = this.memoryAllocator.Allocate(width * height, AllocationOptions.Clean);
+ using IMemoryOwner rowsWithUndefinedPixels = this.memoryAllocator.Allocate(height, AllocationOptions.Clean);
+ Span rowsWithUndefinedPixelsSpan = rowsWithUndefinedPixels.Memory.Span;
+ Span undefinedPixelsSpan = undefinedPixels.Memory.Span;
+ Span bufferSpan = buffer.GetSpan();
- this.UncompressRle24(stream, width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan);
- for (int y = 0; y < height; y++)
+ this.UncompressRle24(stream, width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan);
+ for (int y = 0; y < height; y++)
+ {
+ int newY = Invert(y, height, inverted);
+ Span pixelRow = pixels.DangerousGetRowSpan(newY);
+ bool rowHasUndefinedPixels = rowsWithUndefinedPixelsSpan[y];
+ if (rowHasUndefinedPixels)
{
- int newY = Invert(y, height, inverted);
- Span pixelRow = pixels.DangerousGetRowSpan(newY);
- bool rowHasUndefinedPixels = rowsWithUndefinedPixelsSpan[y];
- if (rowHasUndefinedPixels)
+ // Slow path with undefined pixels.
+ int yMulWidth = y * width;
+ int rowStartIdx = yMulWidth * 3;
+ for (int x = 0; x < width; x++)
{
- // Slow path with undefined pixels.
- int yMulWidth = y * width;
- int rowStartIdx = yMulWidth * 3;
- for (int x = 0; x < width; x++)
+ int idx = rowStartIdx + (x * 3);
+ if (undefinedPixelsSpan[yMulWidth + x])
{
- int idx = rowStartIdx + (x * 3);
- if (undefinedPixelsSpan[yMulWidth + x])
- {
- switch (this.rleSkippedPixelHandling)
- {
- case RleSkippedPixelHandling.FirstColorOfPalette:
- color.FromBgr24(Unsafe.As(ref bufferSpan[idx]));
- break;
- case RleSkippedPixelHandling.Transparent:
- color.FromScaledVector4(Vector4.Zero);
- break;
-
- // Default handling for skipped pixels is black (which is what System.Drawing is also doing).
- default:
- color.FromScaledVector4(new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
- break;
- }
- }
- else
+ pixelRow[x] = this.rleSkippedPixelHandling switch
{
- color.FromBgr24(Unsafe.As(ref bufferSpan[idx]));
- }
+ RleSkippedPixelHandling.FirstColorOfPalette => TPixel.FromBgr24(Unsafe.As(ref bufferSpan[idx])),
+ RleSkippedPixelHandling.Transparent => TPixel.FromScaledVector4(Vector4.Zero),
- pixelRow[x] = color;
+ // Default handling for skipped pixels is black (which is what System.Drawing is also doing).
+ _ => TPixel.FromScaledVector4(new Vector4(0.0f, 0.0f, 0.0f, 1.0f)),
+ };
}
- }
- else
- {
- // Fast path without any undefined pixels.
- int rowStartIdx = y * width * 3;
- for (int x = 0; x < width; x++)
+ else
{
- int idx = rowStartIdx + (x * 3);
- color.FromBgr24(Unsafe.As(ref bufferSpan[idx]));
- pixelRow[x] = color;
+ pixelRow[x] = TPixel.FromBgr24(Unsafe.As(ref bufferSpan[idx]));
}
}
}
+ else
+ {
+ // Fast path without any undefined pixels.
+ int rowStartIdx = y * width * 3;
+ for (int x = 0; x < width; x++)
+ {
+ int idx = rowStartIdx + (x * 3);
+ pixelRow[x] = TPixel.FromBgr24(Unsafe.As(ref bufferSpan[idx]));
+ }
+ }
}
}
@@ -492,7 +468,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
int max = cmd[1];
int bytesToRead = (int)(((uint)max + 1) / 2);
- Span run = bytesToRead <= 128 ? scratchBuffer.Slice(0, bytesToRead) : new byte[bytesToRead];
+ Span run = bytesToRead <= 128 ? scratchBuffer[..bytesToRead] : new byte[bytesToRead];
stream.Read(run);
@@ -598,7 +574,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
// Take this number of bytes from the stream as uncompressed data.
int length = cmd[1];
- Span run = length <= 128 ? scratchBuffer.Slice(0, length) : new byte[length];
+ Span run = length <= 128 ? scratchBuffer[..length] : new byte[length];
stream.Read(run);
@@ -680,7 +656,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
int length = cmd[1];
int length3 = length * 3;
- Span run = length3 <= 128 ? scratchBuffer.Slice(0, length3) : new byte[length3];
+ Span run = length3 <= 128 ? scratchBuffer[..length3] : new byte[length3];
stream.Read(run);
@@ -835,7 +811,6 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
}
using IMemoryOwner row = this.memoryAllocator.Allocate(arrayWidth + padding, AllocationOptions.Clean);
- TPixel color = default;
Span rowSpan = row.GetSpan();
for (int y = 0; y < height; y++)
@@ -856,8 +831,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
{
int colorIndex = ((rowSpan[offset] >> (8 - bitsPerPixel - (shift * bitsPerPixel))) & mask) * bytesPerColorMapEntry;
- color.FromBgr24(Unsafe.As(ref colors[colorIndex]));
- pixelRow[newX] = color;
+ pixelRow[newX] = TPixel.FromBgr24(Unsafe.As(ref colors[colorIndex]));
}
offset++;
@@ -882,8 +856,6 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
{
int padding = CalculatePadding(width, 2);
int stride = (width * 2) + padding;
- TPixel color = default;
-
int rightShiftRedMask = CalculateRightShift((uint)redMask);
int rightShiftGreenMask = CalculateRightShift((uint)greenMask);
int rightShiftBlueMask = CalculateRightShift((uint)blueMask);
@@ -917,8 +889,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
int b = (blueMaskBits == 5) ? GetBytesFrom5BitValue((temp & blueMask) >> rightShiftBlueMask) : GetBytesFrom6BitValue((temp & blueMask) >> rightShiftBlueMask);
Rgb24 rgb = new((byte)r, (byte)g, (byte)b);
- color.FromRgb24(rgb);
- pixelRow[x] = color;
+ pixelRow[x] = TPixel.FromRgb24(rgb);
offset += 2;
}
}
@@ -1107,8 +1078,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
{
Bgra32 bgra = bgraRowSpan[x];
bgra.A = byte.MaxValue;
- ref TPixel pixel = ref pixelSpan[x];
- pixel.FromBgra32(bgra);
+ pixelSpan[x] = TPixel.FromBgra32(bgra);
}
}
}
@@ -1129,7 +1099,6 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
private void ReadRgb32BitFields(BufferedReadStream stream, Buffer2D pixels, int width, int height, bool inverted, int redMask, int greenMask, int blueMask, int alphaMask)
where TPixel : unmanaged, IPixel
{
- TPixel color = default;
int padding = CalculatePadding(width, 4);
int stride = (width * 4) + padding;
@@ -1179,18 +1148,17 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
g * invMaxValueGreen,
b * invMaxValueBlue,
alpha);
- color.FromScaledVector4(vector4);
+ pixelRow[x] = TPixel.FromScaledVector4(vector4);
}
else
{
byte r = (byte)((temp & redMask) >> rightShiftRedMask);
byte g = (byte)((temp & greenMask) >> rightShiftGreenMask);
byte b = (byte)((temp & blueMask) >> rightShiftBlueMask);
- byte a = alphaMask != 0 ? (byte)((temp & alphaMask) >> rightShiftAlphaMask) : (byte)255;
- color.FromRgba32(new Rgba32(r, g, b, a));
+ byte a = alphaMask != 0 ? (byte)((temp & alphaMask) >> rightShiftAlphaMask) : byte.MaxValue;
+ pixelRow[x] = TPixel.FromRgba32(new(r, g, b, a));
}
- pixelRow[x] = color;
offset += 4;
}
}
@@ -1468,7 +1436,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals
colorMapSizeBytes = this.infoHeader.ClrUsed * bytesPerColorMapEntry;
}
- palette = Array.Empty();
+ palette = [];
if (colorMapSizeBytes > 0)
{
diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs
index 8916da6e0..d64792eba 100644
--- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs
+++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs
@@ -185,7 +185,7 @@ internal sealed class GifDecoderCore : IImageDecoderInternals
{
uint frameCount = 0;
ImageFrameMetadata? previousFrame = null;
- List framesMetadata = new();
+ List framesMetadata = [];
try
{
this.ReadLogicalScreenDescriptorAndGlobalColorTable(stream);
@@ -595,9 +595,7 @@ internal sealed class GifDecoderCore : IImageDecoderInternals
for (int x = descriptorLeft; x < descriptorRight && x < imageWidth; x++)
{
int index = Numerics.Clamp(Unsafe.Add(ref indicesRowRef, (uint)(x - descriptorLeft)), 0, colorTableMaxIdx);
- ref TPixel pixel = ref Unsafe.Add(ref rowRef, (uint)x);
- Rgb24 rgb = colorTable[index];
- pixel.FromRgb24(rgb);
+ Unsafe.Add(ref rowRef, (uint)x) = TPixel.FromRgb24(colorTable[index]);
}
}
else
@@ -613,9 +611,7 @@ internal sealed class GifDecoderCore : IImageDecoderInternals
}
int index = Numerics.Clamp(rawIndex, 0, colorTableMaxIdx);
- ref TPixel pixel = ref Unsafe.Add(ref rowRef, (uint)x);
- Rgb24 rgb = colorTable[index];
- pixel.FromRgb24(rgb);
+ Unsafe.Add(ref rowRef, (uint)x) = TPixel.FromRgb24(colorTable[index]);
}
}
}
diff --git a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs
index 1215768e4..95429e606 100644
--- a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs
+++ b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs
@@ -169,7 +169,7 @@ internal sealed class GifEncoderCore : IImageEncoderInternals
this.EncodeFirstFrame(stream, frameMetadata, quantized);
// Capture the global palette for reuse on subsequent frames and cleanup the quantized frame.
- TPixel[] globalPalette = image.Frames.Count == 1 ? Array.Empty() : quantized.Palette.ToArray();
+ TPixel[] globalPalette = image.Frames.Count == 1 ? [] : quantized.Palette.ToArray();
this.EncodeAdditionalFrames(stream, image, globalPalette, derivedTransparencyIndex, frameMetadata.DisposalMethod);
@@ -488,8 +488,7 @@ internal sealed class GifEncoderCore : IImageEncoderInternals
int index = -1;
if (quantized != null)
{
- TPixel transparentPixel = default;
- transparentPixel.FromScaledVector4(Vector4.Zero);
+ TPixel transparentPixel = TPixel.FromScaledVector4(Vector4.Zero);
ReadOnlySpan palette = quantized.Palette.Span;
// Transparent pixels are much more likely to be found at the end of a palette.
@@ -693,7 +692,7 @@ internal sealed class GifEncoderCore : IImageEncoderInternals
}
IMemoryOwner? owner = null;
- Span extensionBuffer = stackalloc byte[0]; // workaround compiler limitation
+ scoped Span extensionBuffer = []; // workaround compiler limitation
if (extensionSize > 128)
{
owner = this.memoryAllocator.Allocate(extensionSize + 3);
diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs
index aa3603cfb..993f53269 100644
--- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs
+++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs
@@ -328,18 +328,17 @@ internal sealed class PngEncoderCore : IImageEncoderInternals, IDisposable
=> clone.ProcessPixelRows(accessor =>
{
// TODO: We should be able to speed this up with SIMD and masking.
- Rgba32 rgba32 = default;
Rgba32 transparent = Color.Transparent.ToPixel();
for (int y = 0; y < accessor.Height; y++)
{
Span span = accessor.GetRowSpan(y);
for (int x = 0; x < accessor.Width; x++)
{
- span[x].ToRgba32(ref rgba32);
-
- if (rgba32.A is 0)
+ ref TPixel pixel = ref span[x];
+ Rgba32 rgba = pixel.ToRgba32();
+ if (rgba.A is 0)
{
- span[x].FromRgba32(transparent);
+ pixel = TPixel.FromRgba32(transparent);
}
}
}
diff --git a/src/ImageSharp/Formats/Png/PngScanlineProcessor.cs b/src/ImageSharp/Formats/Png/PngScanlineProcessor.cs
index 39b3fff27..aa937a8e2 100644
--- a/src/ImageSharp/Formats/Png/PngScanlineProcessor.cs
+++ b/src/ImageSharp/Formats/Png/PngScanlineProcessor.cs
@@ -42,7 +42,6 @@ internal static class PngScanlineProcessor
where TPixel : unmanaged, IPixel
{
uint offset = pixelOffset + frameControl.XOffset;
- TPixel pixel = default;
ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan);
ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan);
int scaleFactor = 255 / (ColorNumerics.GetColorCountForBitDepth(bitDepth) - 1);
@@ -55,8 +54,7 @@ internal static class PngScanlineProcessor
for (nuint x = offset; x < frameControl.XMax; x += increment, o += 2)
{
ushort luminance = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2));
- pixel.FromL16(Unsafe.As(ref luminance));
- Unsafe.Add(ref rowSpanRef, x) = pixel;
+ Unsafe.Add(ref rowSpanRef, x) = TPixel.FromL16(Unsafe.As(ref luminance));
}
}
else
@@ -64,8 +62,7 @@ internal static class PngScanlineProcessor
for (nuint x = offset, o = 0; x < frameControl.XMax; x += increment, o++)
{
byte luminance = (byte)(Unsafe.Add(ref scanlineSpanRef, o) * scaleFactor);
- pixel.FromL8(Unsafe.As(ref luminance));
- Unsafe.Add(ref rowSpanRef, x) = pixel;
+ Unsafe.Add(ref rowSpanRef, x) = TPixel.FromL8(Unsafe.As(ref luminance));
}
}
@@ -75,30 +72,22 @@ internal static class PngScanlineProcessor
if (bitDepth == 16)
{
L16 transparent = transparentColor.Value.ToPixel();
- La32 source = default;
int o = 0;
for (nuint x = offset; x < frameControl.XMax; x += increment, o += 2)
{
ushort luminance = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2));
- source.L = luminance;
- source.A = luminance.Equals(transparent.PackedValue) ? ushort.MinValue : ushort.MaxValue;
-
- pixel.FromLa32(source);
- Unsafe.Add(ref rowSpanRef, x) = pixel;
+ La32 source = new(luminance, luminance.Equals(transparent.PackedValue) ? ushort.MinValue : ushort.MaxValue);
+ Unsafe.Add(ref rowSpanRef, x) = TPixel.FromLa32(source);
}
}
else
{
byte transparent = (byte)(transparentColor.Value.ToPixel().PackedValue * scaleFactor);
- La16 source = default;
for (nuint x = offset, o = 0; x < frameControl.XMax; x += increment, o++)
{
byte luminance = (byte)(Unsafe.Add(ref scanlineSpanRef, o) * scaleFactor);
- source.L = luminance;
- source.A = luminance.Equals(transparent) ? byte.MinValue : byte.MaxValue;
-
- pixel.FromLa16(source);
- Unsafe.Add(ref rowSpanRef, x) = pixel;
+ La16 source = new(luminance, luminance.Equals(transparent) ? byte.MinValue : byte.MaxValue);
+ Unsafe.Add(ref rowSpanRef, x) = TPixel.FromLa16(source);
}
}
}
@@ -133,34 +122,28 @@ internal static class PngScanlineProcessor
where TPixel : unmanaged, IPixel
{
uint offset = pixelOffset + frameControl.XOffset;
- TPixel pixel = default;
ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan);
ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan);
if (bitDepth == 16)
{
- La32 source = default;
int o = 0;
for (nuint x = offset; x < frameControl.XMax; x += increment, o += 4)
{
- source.L = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2));
- source.A = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + 2, 2));
+ ushort l = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2));
+ ushort a = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + 2, 2));
- pixel.FromLa32(source);
- Unsafe.Add(ref rowSpanRef, (uint)x) = pixel;
+ Unsafe.Add(ref rowSpanRef, (uint)x) = TPixel.FromLa32(new(l, a));
}
}
else
{
- La16 source = default;
nuint offset2 = 0;
for (nuint x = offset; x < frameControl.XMax; x += increment)
{
- source.L = Unsafe.Add(ref scanlineSpanRef, offset2);
- source.A = Unsafe.Add(ref scanlineSpanRef, offset2 + bytesPerSample);
-
- pixel.FromLa16(source);
- Unsafe.Add(ref rowSpanRef, x) = pixel;
+ byte l = Unsafe.Add(ref scanlineSpanRef, offset2);
+ byte a = Unsafe.Add(ref scanlineSpanRef, offset2 + bytesPerSample);
+ Unsafe.Add(ref rowSpanRef, x) = TPixel.FromLa16(new(l, a));
offset2 += bytesPerPixel;
}
}
@@ -194,7 +177,6 @@ internal static class PngScanlineProcessor
PngThrowHelper.ThrowMissingPalette();
}
- TPixel pixel = default;
ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan);
ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan);
ref Color paletteBase = ref MemoryMarshal.GetReference(palette.Value.Span);
@@ -202,8 +184,7 @@ internal static class PngScanlineProcessor
for (nuint x = pixelOffset, o = 0; x < frameControl.XMax; x += increment, o++)
{
uint index = Unsafe.Add(ref scanlineSpanRef, o);
- pixel.FromRgba32(Unsafe.Add(ref paletteBase, index).ToPixel());
- Unsafe.Add(ref rowSpanRef, x) = pixel;
+ Unsafe.Add(ref rowSpanRef, x) = TPixel.FromRgba32(Unsafe.Add(ref paletteBase, index).ToPixel());
}
}
@@ -243,8 +224,6 @@ internal static class PngScanlineProcessor
where TPixel : unmanaged, IPixel
{
uint offset = pixelOffset + frameControl.XOffset;
-
- TPixel pixel = default;
ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan);
ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan);
@@ -252,16 +231,13 @@ internal static class PngScanlineProcessor
{
if (bitDepth == 16)
{
- Rgb48 rgb48 = default;
int o = 0;
for (nuint x = offset; x < frameControl.XMax; x += increment, o += bytesPerPixel)
{
- rgb48.R = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample));
- rgb48.G = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample));
- rgb48.B = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + (2 * bytesPerSample), bytesPerSample));
-
- pixel.FromRgb48(rgb48);
- Unsafe.Add(ref rowSpanRef, x) = pixel;
+ ushort r = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample));
+ ushort g = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample));
+ ushort b = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + (2 * bytesPerSample), bytesPerSample));
+ Unsafe.Add(ref rowSpanRef, x) = TPixel.FromRgb48(new(r, g, b));
}
}
else if (pixelOffset == 0 && increment == 1)
@@ -274,16 +250,13 @@ internal static class PngScanlineProcessor
}
else
{
- Rgb24 rgb = default;
int o = 0;
for (nuint x = offset; x < frameControl.XMax; x += increment, o += bytesPerPixel)
{
- rgb.R = Unsafe.Add(ref scanlineSpanRef, (uint)o);
- rgb.G = Unsafe.Add(ref scanlineSpanRef, (uint)(o + bytesPerSample));
- rgb.B = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (2 * bytesPerSample)));
-
- pixel.FromRgb24(rgb);
- Unsafe.Add(ref rowSpanRef, x) = pixel;
+ byte r = Unsafe.Add(ref scanlineSpanRef, (uint)o);
+ byte g = Unsafe.Add(ref scanlineSpanRef, (uint)(o + bytesPerSample));
+ byte b = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (2 * bytesPerSample)));
+ Unsafe.Add(ref rowSpanRef, x) = TPixel.FromRgb24(new(r, g, b));
}
}
@@ -293,27 +266,20 @@ internal static class PngScanlineProcessor
if (bitDepth == 16)
{
Rgb48 transparent = transparentColor.Value.ToPixel();
-
- Rgb48 rgb48 = default;
- Rgba64 rgba64 = default;
+ Rgba64 rgba = default;
int o = 0;
for (nuint x = offset; x < frameControl.XMax; x += increment, o += bytesPerPixel)
{
- rgb48.R = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample));
- rgb48.G = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample));
- rgb48.B = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + (2 * bytesPerSample), bytesPerSample));
-
- rgba64.Rgb = rgb48;
- rgba64.A = rgb48.Equals(transparent) ? ushort.MinValue : ushort.MaxValue;
-
- pixel.FromRgba64(rgba64);
- Unsafe.Add(ref rowSpanRef, x) = pixel;
+ rgba.R = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample));
+ rgba.G = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample));
+ rgba.B = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + (2 * bytesPerSample), bytesPerSample));
+ rgba.A = rgba.Rgb.Equals(transparent) ? ushort.MinValue : ushort.MaxValue;
+ Unsafe.Add(ref rowSpanRef, x) = TPixel.FromRgba64(rgba);
}
}
else
{
Rgb24 transparent = transparentColor.Value.ToPixel();
-
Rgba32 rgba = default;
int o = 0;
for (nuint x = offset; x < frameControl.XMax; x += increment, o += bytesPerPixel)
@@ -322,9 +288,7 @@ internal static class PngScanlineProcessor
rgba.G = Unsafe.Add(ref scanlineSpanRef, (uint)(o + bytesPerSample));
rgba.B = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (2 * bytesPerSample)));
rgba.A = transparent.Equals(rgba.Rgb) ? byte.MinValue : byte.MaxValue;
-
- pixel.FromRgba32(rgba);
- Unsafe.Add(ref rowSpanRef, x) = pixel;
+ Unsafe.Add(ref rowSpanRef, x) = TPixel.FromRgba32(rgba);
}
}
}
@@ -362,22 +326,18 @@ internal static class PngScanlineProcessor
where TPixel : unmanaged, IPixel
{
uint offset = pixelOffset + frameControl.XOffset;
- TPixel pixel = default;
ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan);
if (bitDepth == 16)
{
- Rgba64 rgba64 = default;
int o = 0;
for (nuint x = offset; x < frameControl.XMax; x += increment, o += bytesPerPixel)
{
- rgba64.R = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample));
- rgba64.G = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample));
- rgba64.B = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + (2 * bytesPerSample), bytesPerSample));
- rgba64.A = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + (3 * bytesPerSample), bytesPerSample));
-
- pixel.FromRgba64(rgba64);
- Unsafe.Add(ref rowSpanRef, x) = pixel;
+ ushort r = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, bytesPerSample));
+ ushort g = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + bytesPerSample, bytesPerSample));
+ ushort b = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + (2 * bytesPerSample), bytesPerSample));
+ ushort a = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + (3 * bytesPerSample), bytesPerSample));
+ Unsafe.Add(ref rowSpanRef, x) = TPixel.FromRgba64(new(r, g, b, a));
}
}
else if (pixelOffset == 0 && increment == 1)
@@ -391,17 +351,14 @@ internal static class PngScanlineProcessor
else
{
ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan);
- Rgba32 rgba = default;
int o = 0;
for (nuint x = offset; x < frameControl.XMax; x += increment, o += bytesPerPixel)
{
- rgba.R = Unsafe.Add(ref scanlineSpanRef, (uint)o);
- rgba.G = Unsafe.Add(ref scanlineSpanRef, (uint)(o + bytesPerSample));
- rgba.B = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (2 * bytesPerSample)));
- rgba.A = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (3 * bytesPerSample)));
-
- pixel.FromRgba32(rgba);
- Unsafe.Add(ref rowSpanRef, x) = pixel;
+ byte r = Unsafe.Add(ref scanlineSpanRef, (uint)o);
+ byte g = Unsafe.Add(ref scanlineSpanRef, (uint)(o + bytesPerSample));
+ byte b = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (2 * bytesPerSample)));
+ byte a = Unsafe.Add(ref scanlineSpanRef, (uint)(o + (3 * bytesPerSample)));
+ Unsafe.Add(ref rowSpanRef, x) = TPixel.FromRgba32(new(r, g, b, a));
}
}
}
diff --git a/src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs b/src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs
index deb0a37f0..86d81d834 100644
--- a/src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs
+++ b/src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs
@@ -149,7 +149,7 @@ internal class QoiDecoderCore : IImageDecoderInternals
Span previouslySeenPixels = previouslySeenPixelsBuffer.GetSpan();
Rgba32 previousPixel = new(0, 0, 0, 255);
- // We save the pixel to avoid loosing the fully opaque black pixel
+ // We save the pixel to avoid losing the fully opaque black pixel
// See https://github.com/phoboslab/qoi/issues/258
int pixelArrayPosition = GetArrayPosition(previousPixel);
previouslySeenPixels[pixelArrayPosition] = previousPixel;
@@ -174,7 +174,7 @@ internal class QoiDecoderCore : IImageDecoderInternals
}
readPixel.A = previousPixel.A;
- pixel.FromRgba32(readPixel);
+ pixel = TPixel.FromRgba32(readPixel);
pixelArrayPosition = GetArrayPosition(readPixel);
previouslySeenPixels[pixelArrayPosition] = readPixel;
break;
@@ -186,7 +186,7 @@ internal class QoiDecoderCore : IImageDecoderInternals
ThrowInvalidImageContentException();
}
- pixel.FromRgba32(readPixel);
+ pixel = TPixel.FromRgba32(readPixel);
pixelArrayPosition = GetArrayPosition(readPixel);
previouslySeenPixels[pixelArrayPosition] = readPixel;
break;
@@ -197,7 +197,7 @@ internal class QoiDecoderCore : IImageDecoderInternals
// Getting one pixel from previously seen pixels
case QoiChunk.QoiOpIndex:
readPixel = previouslySeenPixels[operationByte];
- pixel.FromRgba32(readPixel);
+ pixel = TPixel.FromRgba32(readPixel);
break;
// Get one pixel from the difference (-2..1) of the previous pixel
@@ -211,7 +211,7 @@ internal class QoiDecoderCore : IImageDecoderInternals
G = (byte)Numerics.Modulo256(previousPixel.G + (greenDifference - 2)),
B = (byte)Numerics.Modulo256(previousPixel.B + (blueDifference - 2))
};
- pixel.FromRgba32(readPixel);
+ pixel = TPixel.FromRgba32(readPixel);
pixelArrayPosition = GetArrayPosition(readPixel);
previouslySeenPixels[pixelArrayPosition] = readPixel;
break;
@@ -227,7 +227,7 @@ internal class QoiDecoderCore : IImageDecoderInternals
int currentRed = Numerics.Modulo256(diffRedDG - 8 + (diffGreen - 32) + previousPixel.R);
int currentBlue = Numerics.Modulo256(diffBlueDG - 8 + (diffGreen - 32) + previousPixel.B);
readPixel = previousPixel with { R = (byte)currentRed, B = (byte)currentBlue, G = (byte)currentGreen };
- pixel.FromRgba32(readPixel);
+ pixel = TPixel.FromRgba32(readPixel);
pixelArrayPosition = GetArrayPosition(readPixel);
previouslySeenPixels[pixelArrayPosition] = readPixel;
break;
@@ -241,7 +241,7 @@ internal class QoiDecoderCore : IImageDecoderInternals
}
readPixel = previousPixel;
- pixel.FromRgba32(readPixel);
+ pixel = TPixel.FromRgba32(readPixel);
for (int k = -1; k < repetitions; k++, j++)
{
if (j == row.Length)
diff --git a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs
index 26e057bff..de99c475b 100644
--- a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs
+++ b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs
@@ -222,7 +222,6 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
private void ReadPaletted(BufferedReadStream stream, int width, int height, Buffer2D pixels, Span palette, int colorMapPixelSizeInBytes, TgaImageOrigin origin)
where TPixel : unmanaged, IPixel
{
- TPixel color = default;
bool invertX = InvertX(origin);
for (int y = 0; y < height; y++)
@@ -237,14 +236,14 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
{
for (int x = width - 1; x >= 0; x--)
{
- this.ReadPalettedBgra16Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow);
+ this.ReadPalettedBgra16Pixel(stream, palette, colorMapPixelSizeInBytes, x, pixelRow);
}
}
else
{
for (int x = 0; x < width; x++)
{
- this.ReadPalettedBgra16Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow);
+ this.ReadPalettedBgra16Pixel(stream, palette, colorMapPixelSizeInBytes, x, pixelRow);
}
}
@@ -255,14 +254,14 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
{
for (int x = width - 1; x >= 0; x--)
{
- ReadPalettedBgr24Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow);
+ ReadPalettedBgr24Pixel(stream, palette, colorMapPixelSizeInBytes, x, pixelRow);
}
}
else
{
for (int x = 0; x < width; x++)
{
- ReadPalettedBgr24Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow);
+ ReadPalettedBgr24Pixel(stream, palette, colorMapPixelSizeInBytes, x, pixelRow);
}
}
@@ -273,14 +272,14 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
{
for (int x = width - 1; x >= 0; x--)
{
- ReadPalettedBgra32Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow);
+ ReadPalettedBgra32Pixel(stream, palette, colorMapPixelSizeInBytes, x, pixelRow);
}
}
else
{
for (int x = 0; x < width; x++)
{
- ReadPalettedBgra32Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow);
+ ReadPalettedBgra32Pixel(stream, palette, colorMapPixelSizeInBytes, x, pixelRow);
}
}
@@ -319,16 +318,16 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
switch (colorMapPixelSizeInBytes)
{
case 1:
- color.FromL8(Unsafe.As(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
+ color = TPixel.FromL8(Unsafe.As(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
break;
case 2:
- this.ReadPalettedBgra16Pixel(palette, bufferSpan[idx], colorMapPixelSizeInBytes, ref color);
+ color = this.ReadPalettedBgra16Pixel(palette, bufferSpan[idx], colorMapPixelSizeInBytes);
break;
case 3:
- color.FromBgr24(Unsafe.As(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
+ color = TPixel.FromBgr24(Unsafe.As(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
break;
case 4:
- color.FromBgra32(Unsafe.As(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
+ color = TPixel.FromBgra32(Unsafe.As(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
break;
}
@@ -350,17 +349,15 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
private void ReadMonoChrome(BufferedReadStream stream, int width, int height, Buffer2D pixels, TgaImageOrigin origin)
where TPixel : unmanaged, IPixel
{
- bool invertX = InvertX(origin);
- if (invertX)
+ if (InvertX(origin))
{
- TPixel color = default;
for (int y = 0; y < height; y++)
{
int newY = InvertY(y, height, origin);
Span pixelSpan = pixels.DangerousGetRowSpan(newY);
for (int x = width - 1; x >= 0; x--)
{
- ReadL8Pixel(stream, color, x, pixelSpan);
+ ReadL8Pixel(stream, x, pixelSpan);
}
}
@@ -369,8 +366,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
using IMemoryOwner row = this.memoryAllocator.AllocatePaddedPixelRowBuffer(width, 1, 0);
Span rowSpan = row.GetSpan();
- bool invertY = InvertY(origin);
- if (invertY)
+ if (InvertY(origin))
{
for (int y = height - 1; y >= 0; y--)
{
@@ -398,7 +394,6 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
private void ReadBgra16(BufferedReadStream stream, int width, int height, Buffer2D pixels, TgaImageOrigin origin)
where TPixel : unmanaged, IPixel
{
- TPixel color = default;
bool invertX = InvertX(origin);
using IMemoryOwner row = this.memoryAllocator.AllocatePaddedPixelRowBuffer(width, 2, 0);
Span rowSpan = row.GetSpan();
@@ -426,14 +421,12 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
if (this.fileHeader.ImageType == TgaImageType.BlackAndWhite)
{
- color.FromLa16(Unsafe.As(ref MemoryMarshal.GetReference(scratchBuffer)));
+ pixelSpan[x] = TPixel.FromLa16(Unsafe.As(ref MemoryMarshal.GetReference(scratchBuffer)));
}
else
{
- color.FromBgra5551(Unsafe.As(ref MemoryMarshal.GetReference(scratchBuffer)));
+ pixelSpan[x] = TPixel.FromBgra5551(Unsafe.As(ref MemoryMarshal.GetReference(scratchBuffer)));
}
-
- pixelSpan[x] = color;
}
}
else
@@ -477,18 +470,16 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
private void ReadBgr24(BufferedReadStream stream, int width, int height, Buffer2D pixels, TgaImageOrigin origin)
where TPixel : unmanaged, IPixel
{
- bool invertX = InvertX(origin);
- if (invertX)
+ if (InvertX(origin))
{
Span scratchBuffer = stackalloc byte[4];
- TPixel color = default;
for (int y = 0; y < height; y++)
{
int newY = InvertY(y, height, origin);
Span pixelSpan = pixels.DangerousGetRowSpan(newY);
for (int x = width - 1; x >= 0; x--)
{
- ReadBgr24Pixel(stream, color, x, pixelSpan, scratchBuffer);
+ ReadBgr24Pixel(stream, x, pixelSpan, scratchBuffer);
}
}
@@ -497,9 +488,8 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
using IMemoryOwner row = this.memoryAllocator.AllocatePaddedPixelRowBuffer(width, 3, 0);
Span rowSpan = row.GetSpan();
- bool invertY = InvertY(origin);
- if (invertY)
+ if (InvertY(origin))
{
for (int y = height - 1; y >= 0; y--)
{
@@ -527,7 +517,6 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
private void ReadBgra32(BufferedReadStream stream, int width, int height, Buffer2D pixels, TgaImageOrigin origin)
where TPixel : unmanaged, IPixel
{
- TPixel color = default;
bool invertX = InvertX(origin);
Guard.NotNull(this.tgaMetadata);
@@ -565,14 +554,14 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
{
for (int x = width - 1; x >= 0; x--)
{
- this.ReadBgra32Pixel(stream, x, color, pixelRow, scratchBuffer);
+ this.ReadBgra32Pixel(stream, x, pixelRow, scratchBuffer);
}
}
else
{
for (int x = 0; x < width; x++)
{
- this.ReadBgra32Pixel(stream, x, color, pixelRow, scratchBuffer);
+ this.ReadBgra32Pixel(stream, x, pixelRow, scratchBuffer);
}
}
}
@@ -610,7 +599,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
switch (bytesPerPixel)
{
case 1:
- color.FromL8(Unsafe.As(ref bufferSpan[idx]));
+ color = TPixel.FromL8(Unsafe.As(ref bufferSpan[idx]));
break;
case 2:
if (!this.hasAlpha)
@@ -621,26 +610,26 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
if (this.fileHeader.ImageType == TgaImageType.RleBlackAndWhite)
{
- color.FromLa16(Unsafe.As(ref bufferSpan[idx]));
+ color = TPixel.FromLa16(Unsafe.As(ref bufferSpan[idx]));
}
else
{
- color.FromBgra5551(Unsafe.As(ref bufferSpan[idx]));
+ color = TPixel.FromBgra5551(Unsafe.As(ref bufferSpan[idx]));
}
break;
case 3:
- color.FromBgr24(Unsafe.As(ref bufferSpan[idx]));
+ color = TPixel.FromBgr24(Unsafe.As(ref bufferSpan[idx]));
break;
case 4:
if (this.hasAlpha)
{
- color.FromBgra32(Unsafe.As(ref bufferSpan[idx]));
+ color = TPixel.FromBgra32(Unsafe.As(ref bufferSpan[idx]));
}
else
{
byte alpha = alphaBits == 0 ? byte.MaxValue : bufferSpan[idx + 3];
- color.FromBgra32(new Bgra32(bufferSpan[idx + 2], bufferSpan[idx + 1], bufferSpan[idx], alpha));
+ color = TPixel.FromBgra32(new Bgra32(bufferSpan[idx + 2], bufferSpan[idx + 1], bufferSpan[idx], alpha));
}
break;
@@ -677,16 +666,15 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static void ReadL8Pixel(BufferedReadStream stream, TPixel color, int x, Span pixelSpan)
+ private static void ReadL8Pixel(BufferedReadStream stream, int x, Span pixelSpan)
where TPixel : unmanaged, IPixel
{
byte pixelValue = (byte)stream.ReadByte();
- color.FromL8(Unsafe.As(ref pixelValue));
- pixelSpan[x] = color;
+ pixelSpan[x] = TPixel.FromL8(Unsafe.As(ref pixelValue));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static void ReadBgr24Pixel(BufferedReadStream stream, TPixel color, int x, Span pixelSpan, Span scratchBuffer)
+ private static void ReadBgr24Pixel(BufferedReadStream stream, int x, Span pixelSpan, Span scratchBuffer)
where TPixel : unmanaged, IPixel
{
int bytesRead = stream.Read(scratchBuffer, 0, 3);
@@ -695,8 +683,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read a bgr pixel");
}
- color.FromBgr24(Unsafe.As(ref MemoryMarshal.GetReference(scratchBuffer)));
- pixelSpan[x] = color;
+ pixelSpan[x] = TPixel.FromBgr24(Unsafe.As(ref MemoryMarshal.GetReference(scratchBuffer)));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -714,7 +701,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void ReadBgra32Pixel(BufferedReadStream stream, int x, TPixel color, Span pixelRow, Span scratchBuffer)
+ private void ReadBgra32Pixel(BufferedReadStream stream, int x, Span pixelRow, Span scratchBuffer)
where TPixel : unmanaged, IPixel
{
int bytesRead = stream.Read(scratchBuffer, 0, 4);
@@ -726,8 +713,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
Guard.NotNull(this.tgaMetadata);
byte alpha = this.tgaMetadata.AlphaChannelBits == 0 ? byte.MaxValue : scratchBuffer[3];
- color.FromBgra32(new Bgra32(scratchBuffer[2], scratchBuffer[1], scratchBuffer[0], alpha));
- pixelRow[x] = color;
+ pixelRow[x] = TPixel.FromBgra32(new Bgra32(scratchBuffer[2], scratchBuffer[1], scratchBuffer[0], alpha));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -745,7 +731,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void ReadPalettedBgra16Pixel(BufferedReadStream stream, Span palette, int colorMapPixelSizeInBytes, int x, TPixel color, Span pixelRow)
+ private void ReadPalettedBgra16Pixel(BufferedReadStream stream, Span palette, int colorMapPixelSizeInBytes, int x, Span pixelRow)
where TPixel : unmanaged, IPixel
{
int colorIndex = stream.ReadByte();
@@ -754,16 +740,14 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read color index");
}
- this.ReadPalettedBgra16Pixel(palette, colorIndex, colorMapPixelSizeInBytes, ref color);
- pixelRow[x] = color;
+ pixelRow[x] = this.ReadPalettedBgra16Pixel(palette, colorIndex, colorMapPixelSizeInBytes);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void ReadPalettedBgra16Pixel(Span palette, int index, int colorMapPixelSizeInBytes, ref TPixel color)
+ private TPixel ReadPalettedBgra16Pixel(Span palette, int index, int colorMapPixelSizeInBytes)
where TPixel : unmanaged, IPixel
{
- Bgra5551 bgra = default;
- bgra.FromBgra5551(Unsafe.As(ref palette[index * colorMapPixelSizeInBytes]));
+ Bgra5551 bgra = Unsafe.As(ref palette[index * colorMapPixelSizeInBytes]);
if (!this.hasAlpha)
{
@@ -771,11 +755,11 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
bgra.PackedValue = (ushort)(bgra.PackedValue | 0x8000);
}
- color.FromBgra5551(bgra);
+ return TPixel.FromBgra5551(bgra);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static void ReadPalettedBgr24Pixel(BufferedReadStream stream, Span palette, int colorMapPixelSizeInBytes, int x, TPixel color, Span pixelRow)
+ private static void ReadPalettedBgr24Pixel(BufferedReadStream stream, Span palette, int colorMapPixelSizeInBytes, int x, Span pixelRow)
where TPixel : unmanaged, IPixel
{
int colorIndex = stream.ReadByte();
@@ -784,12 +768,11 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read color index");
}
- color.FromBgr24(Unsafe.As(ref palette[colorIndex * colorMapPixelSizeInBytes]));
- pixelRow[x] = color;
+ pixelRow[x] = TPixel.FromBgr24(Unsafe.As(ref palette[colorIndex * colorMapPixelSizeInBytes]));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static void ReadPalettedBgra32Pixel(BufferedReadStream stream, Span palette, int colorMapPixelSizeInBytes, int x, TPixel color, Span pixelRow)
+ private static void ReadPalettedBgra32Pixel(BufferedReadStream stream, Span palette, int colorMapPixelSizeInBytes, int x, Span pixelRow)
where TPixel : unmanaged, IPixel
{
int colorIndex = stream.ReadByte();
@@ -798,8 +781,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals
TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read color index");
}
- color.FromBgra32(Unsafe.As(ref palette[colorIndex * colorMapPixelSizeInBytes]));
- pixelRow[x] = color;
+ pixelRow[x] = TPixel.FromBgra32(Unsafe.As(ref palette[colorIndex * colorMapPixelSizeInBytes]));
}
///
diff --git a/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs b/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs
index bbb476c01..bb13798c5 100644
--- a/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs
+++ b/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs
@@ -5,7 +5,6 @@ using System.Buffers;
using System.Buffers.Binary;
using System.Numerics;
using System.Runtime.CompilerServices;
-using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Metadata;
using SixLabors.ImageSharp.PixelFormats;
@@ -160,7 +159,6 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals
private void WriteRunLengthEncodedImage(Stream stream, ImageFrame image)
where TPixel : unmanaged, IPixel
{
- Rgba32 color = default;
Buffer2D pixels = image.PixelBuffer;
for (int y = 0; y < image.Height; y++)
{
@@ -168,14 +166,13 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals
for (int x = 0; x < image.Width;)
{
TPixel currentPixel = pixelRow[x];
- currentPixel.ToRgba32(ref color);
byte equalPixelCount = FindEqualPixels(pixelRow, x);
if (equalPixelCount > 0)
{
// Write the number of equal pixels, with the high bit set, indicating ist a compressed pixel run.
stream.WriteByte((byte)(equalPixelCount | 128));
- this.WritePixel(stream, currentPixel, color);
+ this.WritePixel(stream, currentPixel, currentPixel.ToRgba32());
x += equalPixelCount + 1;
}
else
@@ -183,13 +180,12 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals
// Write Raw Packet (i.e., Non-Run-Length Encoded):
byte unEqualPixelCount = FindUnEqualPixels(pixelRow, x);
stream.WriteByte(unEqualPixelCount);
- this.WritePixel(stream, currentPixel, color);
+ this.WritePixel(stream, currentPixel, currentPixel.ToRgba32());
x++;
for (int i = 0; i < unEqualPixelCount; i++)
{
currentPixel = pixelRow[x];
- currentPixel.ToRgba32(ref color);
- this.WritePixel(stream, currentPixel, color);
+ this.WritePixel(stream, currentPixel, currentPixel.ToRgba32());
x++;
}
}
diff --git a/src/ImageSharp/Formats/Tiff/Compression/HorizontalPredictor.cs b/src/ImageSharp/Formats/Tiff/Compression/HorizontalPredictor.cs
index 1c838b0b7..30a933528 100644
--- a/src/ImageSharp/Formats/Tiff/Compression/HorizontalPredictor.cs
+++ b/src/ImageSharp/Formats/Tiff/Compression/HorizontalPredictor.cs
@@ -102,8 +102,7 @@ internal static class HorizontalPredictor
byte r = (byte)(rowRgb[x].R - rowRgb[x - 1].R);
byte g = (byte)(rowRgb[x].G - rowRgb[x - 1].G);
byte b = (byte)(rowRgb[x].B - rowRgb[x - 1].B);
- var rgb = new Rgb24(r, g, b);
- rowRgb[x].FromRgb24(rgb);
+ rowRgb[x] = new Rgb24(r, g, b);
}
}
}
@@ -128,8 +127,7 @@ internal static class HorizontalPredictor
for (int x = rowL16.Length - 1; x >= 1; x--)
{
- ushort val = (ushort)(rowL16[x].PackedValue - rowL16[x - 1].PackedValue);
- rowL16[x].PackedValue = val;
+ rowL16[x].PackedValue = (ushort)(rowL16[x].PackedValue - rowL16[x - 1].PackedValue);
}
}
}
@@ -181,13 +179,13 @@ internal static class HorizontalPredictor
{
int offset = 0;
Span rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
- ushort pixelValue = TiffUtils.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
+ ushort pixelValue = TiffUtilities.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
offset += 2;
for (int x = 1; x < width; x++)
{
Span rowSpan = rowBytes.Slice(offset, 2);
- ushort diff = TiffUtils.ConvertToUShortBigEndian(rowSpan);
+ ushort diff = TiffUtilities.ConvertToUShortBigEndian(rowSpan);
pixelValue += diff;
BinaryPrimitives.WriteUInt16BigEndian(rowSpan, pixelValue);
offset += 2;
@@ -200,13 +198,13 @@ internal static class HorizontalPredictor
{
int offset = 0;
Span rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
- ushort pixelValue = TiffUtils.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
+ ushort pixelValue = TiffUtilities.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
offset += 2;
for (int x = 1; x < width; x++)
{
Span rowSpan = rowBytes.Slice(offset, 2);
- ushort diff = TiffUtils.ConvertToUShortLittleEndian(rowSpan);
+ ushort diff = TiffUtilities.ConvertToUShortLittleEndian(rowSpan);
pixelValue += diff;
BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, pixelValue);
offset += 2;
@@ -225,13 +223,13 @@ internal static class HorizontalPredictor
{
int offset = 0;
Span rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
- uint pixelValue = TiffUtils.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
+ uint pixelValue = TiffUtilities.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
offset += 4;
for (int x = 1; x < width; x++)
{
Span rowSpan = rowBytes.Slice(offset, 4);
- uint diff = TiffUtils.ConvertToUIntBigEndian(rowSpan);
+ uint diff = TiffUtilities.ConvertToUIntBigEndian(rowSpan);
pixelValue += diff;
BinaryPrimitives.WriteUInt32BigEndian(rowSpan, pixelValue);
offset += 4;
@@ -244,13 +242,13 @@ internal static class HorizontalPredictor
{
int offset = 0;
Span rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
- uint pixelValue = TiffUtils.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
+ uint pixelValue = TiffUtilities.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
offset += 4;
for (int x = 1; x < width; x++)
{
Span rowSpan = rowBytes.Slice(offset, 4);
- uint diff = TiffUtils.ConvertToUIntLittleEndian(rowSpan);
+ uint diff = TiffUtilities.ConvertToUIntLittleEndian(rowSpan);
pixelValue += diff;
BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, pixelValue);
offset += 4;
@@ -278,8 +276,7 @@ internal static class HorizontalPredictor
r += pixel.R;
g += pixel.G;
b += pixel.B;
- var rgb = new Rgb24(r, g, b);
- pixel.FromRgb24(rgb);
+ pixel = new Rgb24(r, g, b);
}
}
}
@@ -305,8 +302,7 @@ internal static class HorizontalPredictor
g += pixel.G;
b += pixel.B;
a += pixel.A;
- var rgb = new Rgba32(r, g, b, a);
- pixel.FromRgba32(rgb);
+ pixel = new Rgba32(r, g, b, a);
}
}
}
@@ -321,29 +317,29 @@ internal static class HorizontalPredictor
{
int offset = 0;
Span rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
- ushort r = TiffUtils.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
+ ushort r = TiffUtilities.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
offset += 2;
- ushort g = TiffUtils.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
+ ushort g = TiffUtilities.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
offset += 2;
- ushort b = TiffUtils.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
+ ushort b = TiffUtilities.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
offset += 2;
for (int x = 1; x < width; x++)
{
Span rowSpan = rowBytes.Slice(offset, 2);
- ushort deltaR = TiffUtils.ConvertToUShortBigEndian(rowSpan);
+ ushort deltaR = TiffUtilities.ConvertToUShortBigEndian(rowSpan);
r += deltaR;
BinaryPrimitives.WriteUInt16BigEndian(rowSpan, r);
offset += 2;
rowSpan = rowBytes.Slice(offset, 2);
- ushort deltaG = TiffUtils.ConvertToUShortBigEndian(rowSpan);
+ ushort deltaG = TiffUtilities.ConvertToUShortBigEndian(rowSpan);
g += deltaG;
BinaryPrimitives.WriteUInt16BigEndian(rowSpan, g);
offset += 2;
rowSpan = rowBytes.Slice(offset, 2);
- ushort deltaB = TiffUtils.ConvertToUShortBigEndian(rowSpan);
+ ushort deltaB = TiffUtilities.ConvertToUShortBigEndian(rowSpan);
b += deltaB;
BinaryPrimitives.WriteUInt16BigEndian(rowSpan, b);
offset += 2;
@@ -356,29 +352,29 @@ internal static class HorizontalPredictor
{
int offset = 0;
Span rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
- ushort r = TiffUtils.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
+ ushort r = TiffUtilities.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
offset += 2;
- ushort g = TiffUtils.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
+ ushort g = TiffUtilities.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
offset += 2;
- ushort b = TiffUtils.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
+ ushort b = TiffUtilities.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
offset += 2;
for (int x = 1; x < width; x++)
{
Span rowSpan = rowBytes.Slice(offset, 2);
- ushort deltaR = TiffUtils.ConvertToUShortLittleEndian(rowSpan);
+ ushort deltaR = TiffUtilities.ConvertToUShortLittleEndian(rowSpan);
r += deltaR;
BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, r);
offset += 2;
rowSpan = rowBytes.Slice(offset, 2);
- ushort deltaG = TiffUtils.ConvertToUShortLittleEndian(rowSpan);
+ ushort deltaG = TiffUtilities.ConvertToUShortLittleEndian(rowSpan);
g += deltaG;
BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, g);
offset += 2;
rowSpan = rowBytes.Slice(offset, 2);
- ushort deltaB = TiffUtils.ConvertToUShortLittleEndian(rowSpan);
+ ushort deltaB = TiffUtilities.ConvertToUShortLittleEndian(rowSpan);
b += deltaB;
BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, b);
offset += 2;
@@ -397,37 +393,37 @@ internal static class HorizontalPredictor
{
int offset = 0;
Span rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
- ushort r = TiffUtils.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
+ ushort r = TiffUtilities.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
offset += 2;
- ushort g = TiffUtils.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
+ ushort g = TiffUtilities.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
offset += 2;
- ushort b = TiffUtils.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
+ ushort b = TiffUtilities.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
offset += 2;
- ushort a = TiffUtils.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
+ ushort a = TiffUtilities.ConvertToUShortBigEndian(rowBytes.Slice(offset, 2));
offset += 2;
for (int x = 1; x < width; x++)
{
Span rowSpan = rowBytes.Slice(offset, 2);
- ushort deltaR = TiffUtils.ConvertToUShortBigEndian(rowSpan);
+ ushort deltaR = TiffUtilities.ConvertToUShortBigEndian(rowSpan);
r += deltaR;
BinaryPrimitives.WriteUInt16BigEndian(rowSpan, r);
offset += 2;
rowSpan = rowBytes.Slice(offset, 2);
- ushort deltaG = TiffUtils.ConvertToUShortBigEndian(rowSpan);
+ ushort deltaG = TiffUtilities.ConvertToUShortBigEndian(rowSpan);
g += deltaG;
BinaryPrimitives.WriteUInt16BigEndian(rowSpan, g);
offset += 2;
rowSpan = rowBytes.Slice(offset, 2);
- ushort deltaB = TiffUtils.ConvertToUShortBigEndian(rowSpan);
+ ushort deltaB = TiffUtilities.ConvertToUShortBigEndian(rowSpan);
b += deltaB;
BinaryPrimitives.WriteUInt16BigEndian(rowSpan, b);
offset += 2;
rowSpan = rowBytes.Slice(offset, 2);
- ushort deltaA = TiffUtils.ConvertToUShortBigEndian(rowSpan);
+ ushort deltaA = TiffUtilities.ConvertToUShortBigEndian(rowSpan);
a += deltaA;
BinaryPrimitives.WriteUInt16BigEndian(rowSpan, a);
offset += 2;
@@ -440,37 +436,37 @@ internal static class HorizontalPredictor
{
int offset = 0;
Span rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
- ushort r = TiffUtils.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
+ ushort r = TiffUtilities.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
offset += 2;
- ushort g = TiffUtils.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
+ ushort g = TiffUtilities.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
offset += 2;
- ushort b = TiffUtils.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
+ ushort b = TiffUtilities.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
offset += 2;
- ushort a = TiffUtils.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
+ ushort a = TiffUtilities.ConvertToUShortLittleEndian(rowBytes.Slice(offset, 2));
offset += 2;
for (int x = 1; x < width; x++)
{
Span rowSpan = rowBytes.Slice(offset, 2);
- ushort deltaR = TiffUtils.ConvertToUShortLittleEndian(rowSpan);
+ ushort deltaR = TiffUtilities.ConvertToUShortLittleEndian(rowSpan);
r += deltaR;
BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, r);
offset += 2;
rowSpan = rowBytes.Slice(offset, 2);
- ushort deltaG = TiffUtils.ConvertToUShortLittleEndian(rowSpan);
+ ushort deltaG = TiffUtilities.ConvertToUShortLittleEndian(rowSpan);
g += deltaG;
BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, g);
offset += 2;
rowSpan = rowBytes.Slice(offset, 2);
- ushort deltaB = TiffUtils.ConvertToUShortLittleEndian(rowSpan);
+ ushort deltaB = TiffUtilities.ConvertToUShortLittleEndian(rowSpan);
b += deltaB;
BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, b);
offset += 2;
rowSpan = rowBytes.Slice(offset, 2);
- ushort deltaA = TiffUtils.ConvertToUShortLittleEndian(rowSpan);
+ ushort deltaA = TiffUtilities.ConvertToUShortLittleEndian(rowSpan);
a += deltaA;
BinaryPrimitives.WriteUInt16LittleEndian(rowSpan, a);
offset += 2;
@@ -489,29 +485,29 @@ internal static class HorizontalPredictor
{
int offset = 0;
Span rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
- uint r = TiffUtils.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
+ uint r = TiffUtilities.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
offset += 4;
- uint g = TiffUtils.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
+ uint g = TiffUtilities.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
offset += 4;
- uint b = TiffUtils.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
+ uint b = TiffUtilities.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
offset += 4;
for (int x = 1; x < width; x++)
{
Span rowSpan = rowBytes.Slice(offset, 4);
- uint deltaR = TiffUtils.ConvertToUIntBigEndian(rowSpan);
+ uint deltaR = TiffUtilities.ConvertToUIntBigEndian(rowSpan);
r += deltaR;
BinaryPrimitives.WriteUInt32BigEndian(rowSpan, r);
offset += 4;
rowSpan = rowBytes.Slice(offset, 4);
- uint deltaG = TiffUtils.ConvertToUIntBigEndian(rowSpan);
+ uint deltaG = TiffUtilities.ConvertToUIntBigEndian(rowSpan);
g += deltaG;
BinaryPrimitives.WriteUInt32BigEndian(rowSpan, g);
offset += 4;
rowSpan = rowBytes.Slice(offset, 4);
- uint deltaB = TiffUtils.ConvertToUIntBigEndian(rowSpan);
+ uint deltaB = TiffUtilities.ConvertToUIntBigEndian(rowSpan);
b += deltaB;
BinaryPrimitives.WriteUInt32BigEndian(rowSpan, b);
offset += 4;
@@ -524,29 +520,29 @@ internal static class HorizontalPredictor
{
int offset = 0;
Span rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
- uint r = TiffUtils.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
+ uint r = TiffUtilities.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
offset += 4;
- uint g = TiffUtils.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
+ uint g = TiffUtilities.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
offset += 4;
- uint b = TiffUtils.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
+ uint b = TiffUtilities.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
offset += 4;
for (int x = 1; x < width; x++)
{
Span rowSpan = rowBytes.Slice(offset, 4);
- uint deltaR = TiffUtils.ConvertToUIntLittleEndian(rowSpan);
+ uint deltaR = TiffUtilities.ConvertToUIntLittleEndian(rowSpan);
r += deltaR;
BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, r);
offset += 4;
rowSpan = rowBytes.Slice(offset, 4);
- uint deltaG = TiffUtils.ConvertToUIntLittleEndian(rowSpan);
+ uint deltaG = TiffUtilities.ConvertToUIntLittleEndian(rowSpan);
g += deltaG;
BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, g);
offset += 4;
rowSpan = rowBytes.Slice(offset, 4);
- uint deltaB = TiffUtils.ConvertToUIntLittleEndian(rowSpan);
+ uint deltaB = TiffUtilities.ConvertToUIntLittleEndian(rowSpan);
b += deltaB;
BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, b);
offset += 4;
@@ -565,37 +561,37 @@ internal static class HorizontalPredictor
{
int offset = 0;
Span rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
- uint r = TiffUtils.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
+ uint r = TiffUtilities.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
offset += 4;
- uint g = TiffUtils.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
+ uint g = TiffUtilities.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
offset += 4;
- uint b = TiffUtils.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
+ uint b = TiffUtilities.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
offset += 4;
- uint a = TiffUtils.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
+ uint a = TiffUtilities.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
offset += 4;
for (int x = 1; x < width; x++)
{
Span rowSpan = rowBytes.Slice(offset, 4);
- uint deltaR = TiffUtils.ConvertToUIntBigEndian(rowSpan);
+ uint deltaR = TiffUtilities.ConvertToUIntBigEndian(rowSpan);
r += deltaR;
BinaryPrimitives.WriteUInt32BigEndian(rowSpan, r);
offset += 4;
rowSpan = rowBytes.Slice(offset, 4);
- uint deltaG = TiffUtils.ConvertToUIntBigEndian(rowSpan);
+ uint deltaG = TiffUtilities.ConvertToUIntBigEndian(rowSpan);
g += deltaG;
BinaryPrimitives.WriteUInt32BigEndian(rowSpan, g);
offset += 4;
rowSpan = rowBytes.Slice(offset, 4);
- uint deltaB = TiffUtils.ConvertToUIntBigEndian(rowSpan);
+ uint deltaB = TiffUtilities.ConvertToUIntBigEndian(rowSpan);
b += deltaB;
BinaryPrimitives.WriteUInt32BigEndian(rowSpan, b);
offset += 4;
rowSpan = rowBytes.Slice(offset, 4);
- uint deltaA = TiffUtils.ConvertToUIntBigEndian(rowSpan);
+ uint deltaA = TiffUtilities.ConvertToUIntBigEndian(rowSpan);
a += deltaA;
BinaryPrimitives.WriteUInt32BigEndian(rowSpan, a);
offset += 4;
@@ -608,37 +604,37 @@ internal static class HorizontalPredictor
{
int offset = 0;
Span rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
- uint r = TiffUtils.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
+ uint r = TiffUtilities.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
offset += 4;
- uint g = TiffUtils.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
+ uint g = TiffUtilities.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
offset += 4;
- uint b = TiffUtils.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
+ uint b = TiffUtilities.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
offset += 4;
- uint a = TiffUtils.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
+ uint a = TiffUtilities.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
offset += 4;
for (int x = 1; x < width; x++)
{
Span rowSpan = rowBytes.Slice(offset, 4);
- uint deltaR = TiffUtils.ConvertToUIntLittleEndian(rowSpan);
+ uint deltaR = TiffUtilities.ConvertToUIntLittleEndian(rowSpan);
r += deltaR;
BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, r);
offset += 4;
rowSpan = rowBytes.Slice(offset, 4);
- uint deltaG = TiffUtils.ConvertToUIntLittleEndian(rowSpan);
+ uint deltaG = TiffUtilities.ConvertToUIntLittleEndian(rowSpan);
g += deltaG;
BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, g);
offset += 4;
rowSpan = rowBytes.Slice(offset, 4);
- uint deltaB = TiffUtils.ConvertToUIntLittleEndian(rowSpan);
+ uint deltaB = TiffUtilities.ConvertToUIntLittleEndian(rowSpan);
b += deltaB;
BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, b);
offset += 4;
rowSpan = rowBytes.Slice(offset, 4);
- uint deltaA = TiffUtils.ConvertToUIntLittleEndian(rowSpan);
+ uint deltaA = TiffUtilities.ConvertToUIntLittleEndian(rowSpan);
a += deltaA;
BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, a);
offset += 4;
diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero16TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero16TiffColor{TPixel}.cs
index 8763f9957..2ef261811 100644
--- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero16TiffColor{TPixel}.cs
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero16TiffColor{TPixel}.cs
@@ -11,6 +11,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
///
/// Implements the 'BlackIsZero' photometric interpretation for 16-bit grayscale images.
///
+/// The type of pixel format.
internal class BlackIsZero16TiffColor : TiffBaseColorDecoder
where TPixel : unmanaged, IPixel
{
@@ -32,9 +33,8 @@ internal class BlackIsZero16TiffColor : TiffBaseColorDecoder
///
public override void Decode(ReadOnlySpan data, Buffer2D pixels, int left, int top, int width, int height)
{
- L16 l16 = TiffUtils.L16Default;
- var color = default(TPixel);
- color.FromScaledVector4(Vector4.Zero);
+ L16 l16 = TiffUtilities.L16Default;
+ TPixel color = TPixel.FromScaledVector4(Vector4.Zero);
int offset = 0;
for (int y = top; y < top + height; y++)
@@ -44,10 +44,10 @@ internal class BlackIsZero16TiffColor : TiffBaseColorDecoder
{
for (int x = 0; x < pixelRow.Length; x++)
{
- ushort intensity = TiffUtils.ConvertToUShortBigEndian(data.Slice(offset, 2));
+ ushort intensity = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset, 2));
offset += 2;
- pixelRow[x] = TiffUtils.ColorFromL16(l16, intensity, color);
+ pixelRow[x] = TPixel.FromL16(new(intensity));
}
}
else
diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor{TPixel}.cs
index 89d1b9d20..c9c0ee581 100644
--- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor{TPixel}.cs
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor{TPixel}.cs
@@ -19,11 +19,9 @@ internal class BlackIsZero1TiffColor : TiffBaseColorDecoder
public override void Decode(ReadOnlySpan data, Buffer2D pixels, int left, int top, int width, int height)
{
nuint offset = 0;
- TPixel colorBlack = default;
- TPixel colorWhite = default;
+ TPixel colorBlack = TPixel.FromRgba32(Color.Black.ToPixel());
+ TPixel colorWhite = TPixel.FromRgba32(Color.White.ToPixel());
- colorBlack.FromRgba32(Color.Black.ToPixel());
- colorWhite.FromRgba32(Color.White.ToPixel());
ref byte dataRef = ref MemoryMarshal.GetReference(data);
for (nuint y = (uint)top; y < (uint)(top + height); y++)
{
diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero24TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero24TiffColor{TPixel}.cs
index d57130a5f..07bf3d1bd 100644
--- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero24TiffColor{TPixel}.cs
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero24TiffColor{TPixel}.cs
@@ -1,7 +1,6 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
-using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
@@ -11,6 +10,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
///
/// Implements the 'BlackIsZero' photometric interpretation for 24-bit grayscale images.
///
+/// The type of pixel format.
internal class BlackIsZero24TiffColor : TiffBaseColorDecoder
where TPixel : unmanaged, IPixel
{
@@ -25,8 +25,6 @@ internal class BlackIsZero24TiffColor