|
|
|
@ -22,67 +22,16 @@ internal static class PngScanlineProcessor |
|
|
|
bool hasTrans, |
|
|
|
L16 luminance16Trans, |
|
|
|
L8 luminanceTrans) |
|
|
|
where TPixel : unmanaged, IPixel<TPixel> |
|
|
|
{ |
|
|
|
TPixel pixel = default; |
|
|
|
ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan); |
|
|
|
ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan); |
|
|
|
int scaleFactor = 255 / (ColorNumerics.GetColorCountForBitDepth(header.BitDepth) - 1); |
|
|
|
|
|
|
|
if (!hasTrans) |
|
|
|
{ |
|
|
|
if (header.BitDepth == 16) |
|
|
|
{ |
|
|
|
int o = 0; |
|
|
|
for (nuint x = 0; x < (uint)header.Width; x++, o += 2) |
|
|
|
{ |
|
|
|
ushort luminance = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2)); |
|
|
|
pixel.FromL16(Unsafe.As<ushort, L16>(ref luminance)); |
|
|
|
Unsafe.Add(ref rowSpanRef, x) = pixel; |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
for (nuint x = 0; x < (uint)header.Width; x++) |
|
|
|
{ |
|
|
|
byte luminance = (byte)(Unsafe.Add(ref scanlineSpanRef, x) * scaleFactor); |
|
|
|
pixel.FromL8(Unsafe.As<byte, L8>(ref luminance)); |
|
|
|
Unsafe.Add(ref rowSpanRef, x) = pixel; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if (header.BitDepth == 16) |
|
|
|
{ |
|
|
|
La32 source = default; |
|
|
|
int o = 0; |
|
|
|
for (nuint x = 0; x < (uint)header.Width; x++, o += 2) |
|
|
|
{ |
|
|
|
ushort luminance = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2)); |
|
|
|
source.L = luminance; |
|
|
|
source.A = luminance.Equals(luminance16Trans.PackedValue) ? ushort.MinValue : ushort.MaxValue; |
|
|
|
|
|
|
|
pixel.FromLa32(source); |
|
|
|
Unsafe.Add(ref rowSpanRef, x) = pixel; |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
La16 source = default; |
|
|
|
byte scaledLuminanceTrans = (byte)(luminanceTrans.PackedValue * scaleFactor); |
|
|
|
for (nuint x = 0; x < (uint)header.Width; x++) |
|
|
|
{ |
|
|
|
byte luminance = (byte)(Unsafe.Add(ref scanlineSpanRef, x) * scaleFactor); |
|
|
|
source.L = luminance; |
|
|
|
source.A = luminance.Equals(scaledLuminanceTrans) ? byte.MinValue : byte.MaxValue; |
|
|
|
|
|
|
|
pixel.FromLa16(source); |
|
|
|
Unsafe.Add(ref rowSpanRef, x) = pixel; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
where TPixel : unmanaged, IPixel<TPixel> => |
|
|
|
ProcessInterlacedGrayscaleScanline( |
|
|
|
header, |
|
|
|
scanlineSpan, |
|
|
|
rowSpan, |
|
|
|
0, |
|
|
|
1, |
|
|
|
hasTrans, |
|
|
|
luminance16Trans, |
|
|
|
luminanceTrans); |
|
|
|
|
|
|
|
public static void ProcessInterlacedGrayscaleScanline<TPixel>( |
|
|
|
in PngHeader header, |
|
|
|
@ -161,39 +110,15 @@ internal static class PngScanlineProcessor |
|
|
|
Span<TPixel> rowSpan, |
|
|
|
uint bytesPerPixel, |
|
|
|
uint bytesPerSample) |
|
|
|
where TPixel : unmanaged, IPixel<TPixel> |
|
|
|
{ |
|
|
|
TPixel pixel = default; |
|
|
|
ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan); |
|
|
|
ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan); |
|
|
|
|
|
|
|
if (header.BitDepth == 16) |
|
|
|
{ |
|
|
|
La32 source = default; |
|
|
|
int o = 0; |
|
|
|
for (nuint x = 0; x < (uint)header.Width; x++, o += 4) |
|
|
|
{ |
|
|
|
source.L = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o, 2)); |
|
|
|
source.A = BinaryPrimitives.ReadUInt16BigEndian(scanlineSpan.Slice(o + 2, 2)); |
|
|
|
|
|
|
|
pixel.FromLa32(source); |
|
|
|
Unsafe.Add(ref rowSpanRef, x) = pixel; |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
La16 source = default; |
|
|
|
for (nuint x = 0; x < (uint)header.Width; x++) |
|
|
|
{ |
|
|
|
nuint offset = x * bytesPerPixel; |
|
|
|
source.L = Unsafe.Add(ref scanlineSpanRef, offset); |
|
|
|
source.A = Unsafe.Add(ref scanlineSpanRef, offset + bytesPerSample); |
|
|
|
|
|
|
|
pixel.FromLa16(source); |
|
|
|
Unsafe.Add(ref rowSpanRef, x) = pixel; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
where TPixel : unmanaged, IPixel<TPixel> => |
|
|
|
ProcessInterlacedGrayscaleWithAlphaScanline( |
|
|
|
header, |
|
|
|
scanlineSpan, |
|
|
|
rowSpan, |
|
|
|
0, |
|
|
|
1, |
|
|
|
bytesPerPixel, |
|
|
|
bytesPerSample); |
|
|
|
|
|
|
|
public static void ProcessInterlacedGrayscaleWithAlphaScanline<TPixel>( |
|
|
|
in PngHeader header, |
|
|
|
@ -244,48 +169,14 @@ internal static class PngScanlineProcessor |
|
|
|
Span<TPixel> rowSpan, |
|
|
|
ReadOnlySpan<byte> palette, |
|
|
|
byte[] paletteAlpha) |
|
|
|
where TPixel : unmanaged, IPixel<TPixel> |
|
|
|
{ |
|
|
|
if (palette.IsEmpty) |
|
|
|
{ |
|
|
|
PngThrowHelper.ThrowMissingPalette(); |
|
|
|
} |
|
|
|
|
|
|
|
TPixel pixel = default; |
|
|
|
ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan); |
|
|
|
ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan); |
|
|
|
ReadOnlySpan<Rgb24> palettePixels = MemoryMarshal.Cast<byte, Rgb24>(palette); |
|
|
|
ref Rgb24 palettePixelsRef = ref MemoryMarshal.GetReference(palettePixels); |
|
|
|
|
|
|
|
if (paletteAlpha?.Length > 0) |
|
|
|
{ |
|
|
|
// If the alpha palette is not null and has one or more entries, this means, that the image contains an alpha
|
|
|
|
// channel and we should try to read it.
|
|
|
|
Rgba32 rgba = default; |
|
|
|
ref byte paletteAlphaRef = ref MemoryMarshal.GetArrayDataReference(paletteAlpha); |
|
|
|
|
|
|
|
for (nuint x = 0; x < (uint)header.Width; x++) |
|
|
|
{ |
|
|
|
uint index = Unsafe.Add(ref scanlineSpanRef, x); |
|
|
|
rgba.Rgb = Unsafe.Add(ref palettePixelsRef, index); |
|
|
|
rgba.A = paletteAlpha.Length > index ? Unsafe.Add(ref paletteAlphaRef, index) : byte.MaxValue; |
|
|
|
|
|
|
|
pixel.FromRgba32(rgba); |
|
|
|
Unsafe.Add(ref rowSpanRef, x) = pixel; |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
for (nuint x = 0; x < (uint)header.Width; x++) |
|
|
|
{ |
|
|
|
int index = Unsafe.Add(ref scanlineSpanRef, x); |
|
|
|
Rgb24 rgb = Unsafe.Add(ref palettePixelsRef, index); |
|
|
|
|
|
|
|
pixel.FromRgb24(rgb); |
|
|
|
Unsafe.Add(ref rowSpanRef, x) = pixel; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
where TPixel : unmanaged, IPixel<TPixel> => |
|
|
|
ProcessInterlacedPaletteScanline(header, |
|
|
|
scanlineSpan, |
|
|
|
rowSpan, |
|
|
|
0, |
|
|
|
1, |
|
|
|
palette, |
|
|
|
paletteAlpha); |
|
|
|
|
|
|
|
public static void ProcessInterlacedPaletteScanline<TPixel>( |
|
|
|
in PngHeader header, |
|
|
|
@ -297,6 +188,11 @@ internal static class PngScanlineProcessor |
|
|
|
byte[] paletteAlpha) |
|
|
|
where TPixel : unmanaged, IPixel<TPixel> |
|
|
|
{ |
|
|
|
if (palette.IsEmpty) |
|
|
|
{ |
|
|
|
PngThrowHelper.ThrowMissingPalette(); |
|
|
|
} |
|
|
|
|
|
|
|
TPixel pixel = default; |
|
|
|
ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan); |
|
|
|
ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan); |
|
|
|
@ -347,9 +243,9 @@ internal static class PngScanlineProcessor |
|
|
|
TPixel pixel = default; |
|
|
|
ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan); |
|
|
|
|
|
|
|
if (!hasTrans) |
|
|
|
if (header.BitDepth == 16) |
|
|
|
{ |
|
|
|
if (header.BitDepth == 16) |
|
|
|
if (!hasTrans) |
|
|
|
{ |
|
|
|
Rgb48 rgb48 = default; |
|
|
|
int o = 0; |
|
|
|
@ -365,31 +261,27 @@ internal static class PngScanlineProcessor |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
PixelOperations<TPixel>.Instance.FromRgb24Bytes(configuration, scanlineSpan, rowSpan, header.Width); |
|
|
|
Rgb48 rgb48 = default; |
|
|
|
Rgba64 rgba64 = default; |
|
|
|
int o = 0; |
|
|
|
for (nuint x = 0; x < (uint)header.Width; x++, 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(rgb48Trans) ? ushort.MinValue : ushort.MaxValue; |
|
|
|
|
|
|
|
pixel.FromRgba64(rgba64); |
|
|
|
Unsafe.Add(ref rowSpanRef, x) = pixel; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if (header.BitDepth == 16) |
|
|
|
{ |
|
|
|
Rgb48 rgb48 = default; |
|
|
|
Rgba64 rgba64 = default; |
|
|
|
int o = 0; |
|
|
|
for (nuint x = 0; x < (uint)header.Width; x++, 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(rgb48Trans) ? ushort.MinValue : ushort.MaxValue; |
|
|
|
|
|
|
|
pixel.FromRgba64(rgba64); |
|
|
|
Unsafe.Add(ref rowSpanRef, x) = pixel; |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
if (hasTrans) |
|
|
|
{ |
|
|
|
Rgba32 rgba32 = default; |
|
|
|
ReadOnlySpan<Rgb24> rgb24Span = MemoryMarshal.Cast<byte, Rgb24>(scanlineSpan); |
|
|
|
@ -404,6 +296,10 @@ internal static class PngScanlineProcessor |
|
|
|
Unsafe.Add(ref rowSpanRef, x) = pixel; |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
PixelOperations<TPixel>.Instance.FromRgb24Bytes(configuration, scanlineSpan, rowSpan, header.Width); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static void ProcessInterlacedRgbScanline<TPixel>( |
|
|
|
|