From 3182f935dc84b4ebfce2b506ab0d5eb83ca8aa5a Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Sun, 5 Feb 2023 15:25:49 +0100 Subject: [PATCH 01/22] Remove nullable disable from Formats.BMP * Removed nullable disable * refactored stream usage. It is now always passed as a parameter and not stored a a instance variable --- src/ImageSharp/Color/Color.cs | 3 +- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 179 ++++++++++-------- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 5 +- .../PaletteDitherProcessor{TPixel}.cs | 2 +- .../Processors/Quantization/IQuantizer.cs | 4 +- .../Quantization/OctreeQuantizer.cs | 4 +- .../Quantization/OctreeQuantizer{TPixel}.cs | 2 +- .../Quantization/PaletteQuantizer.cs | 6 +- .../Quantization/PaletteQuantizer{TPixel}.cs | 2 +- .../Processors/Quantization/WuQuantizer.cs | 4 +- .../Quantization/WuQuantizer{TPixel}.cs | 2 +- 11 files changed, 112 insertions(+), 101 deletions(-) diff --git a/src/ImageSharp/Color/Color.cs b/src/ImageSharp/Color/Color.cs index 99a271bcf7..590a3ed311 100644 --- a/src/ImageSharp/Color/Color.cs +++ b/src/ImageSharp/Color/Color.cs @@ -286,12 +286,11 @@ public readonly partial struct Color : IEquatable /// Bulk converts a span of to a span of a specified type. /// /// The pixel type to convert to. - /// The configuration. /// The source color span. /// The destination pixel span. [MethodImpl(InliningOptions.ShortMethod)] #pragma warning disable RCS1163 // Unused parameter. - public static void ToPixel(Configuration configuration, ReadOnlySpan source, Span destination) + public static void ToPixel(ReadOnlySpan source, Span destination) #pragma warning restore RCS1163 // Unused parameter. where TPixel : unmanaged, IPixel { diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index fd0497d9ac..cb1c7a2511 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -1,9 +1,9 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Buffers; using System.Buffers.Binary; +using System.Diagnostics.CodeAnalysis; using System.Numerics; using System.Runtime.CompilerServices; using SixLabors.ImageSharp.Common.Helpers; @@ -58,20 +58,15 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// private const int RleDelta = 0x02; - /// - /// The stream to decode from. - /// - private BufferedReadStream stream; - /// /// The metadata. /// - private ImageMetadata metadata; + private ImageMetadata? metadata; /// /// The bitmap specific metadata. /// - private BmpMetadata bmpMetadata; + private BmpMetadata? bmpMetadata; /// /// The file header containing general information. @@ -126,7 +121,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals public Image Decode(BufferedReadStream stream, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { - Image image = null; + Image? image = null; try { int bytesPerColorMapEntry = this.ReadImageHeaders(stream, out bool inverted, out byte[] palette); @@ -142,24 +137,25 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals { if (this.bmpMetadata.InfoHeaderType == BmpInfoHeaderType.WinVersion3) { - this.ReadRgb32Slow(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); + this.ReadRgb32Slow(stream, pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); } else { - this.ReadRgb32Fast(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); + this.ReadRgb32Fast(stream, pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); } } else if (this.infoHeader.BitsPerPixel == 24) { - this.ReadRgb24(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); + this.ReadRgb24(stream, pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); } else if (this.infoHeader.BitsPerPixel == 16) { - this.ReadRgb16(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); + this.ReadRgb16(stream, pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); } else if (this.infoHeader.BitsPerPixel <= 8) { this.ReadRgbPalette( + stream, pixels, palette, this.infoHeader.Width, @@ -172,19 +168,19 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals break; case BmpCompression.RLE24: - this.ReadRle24(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); + this.ReadRle24(stream, pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); break; case BmpCompression.RLE8: case BmpCompression.RLE4: - this.ReadRle(this.infoHeader.Compression, pixels, palette, this.infoHeader.Width, this.infoHeader.Height, inverted); + this.ReadRle(stream, this.infoHeader.Compression, pixels, palette, this.infoHeader.Width, this.infoHeader.Height, inverted); break; case BmpCompression.BitFields: case BmpCompression.BI_ALPHABITFIELDS: - this.ReadBitFields(pixels, inverted); + this.ReadBitFields(stream, pixels, inverted); break; @@ -250,14 +246,16 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// which will be used to determine which bits belong to that channel. /// /// The pixel format. + /// The containing image data. /// The output pixel buffer containing the decoded image. /// Whether the bitmap is inverted. - private void ReadBitFields(Buffer2D pixels, bool inverted) + private void ReadBitFields(BufferedReadStream stream, Buffer2D pixels, bool inverted) where TPixel : unmanaged, IPixel { if (this.infoHeader.BitsPerPixel == 16) { this.ReadRgb16( + stream, pixels, this.infoHeader.Width, this.infoHeader.Height, @@ -269,6 +267,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals else { this.ReadRgb32BitFields( + stream, pixels, this.infoHeader.Width, this.infoHeader.Height, @@ -282,17 +281,17 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// /// Looks up color values and builds the image from de-compressed RLE8 or RLE4 data. - /// Compressed RLE8 stream is uncompressed by - /// Compressed RLE4 stream is uncompressed by + /// Compressed RLE4 stream is uncompressed by /// /// The pixel format. + /// The containing image data. /// The compression type. Either RLE4 or RLE8. /// The to assign the palette to. /// The containing the colors. /// The width of the bitmap. /// The height of the bitmap. /// Whether the bitmap is inverted. - private void ReadRle(BmpCompression compression, Buffer2D pixels, byte[] colors, int width, int height, bool inverted) + private void ReadRle(BufferedReadStream stream, BmpCompression compression, Buffer2D pixels, byte[] colors, int width, int height, bool inverted) where TPixel : unmanaged, IPixel { TPixel color = default; @@ -305,11 +304,11 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals Span bufferSpan = buffer.Memory.Span; if (compression is BmpCompression.RLE8) { - this.UncompressRle8(width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan); + this.UncompressRle8(stream, width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan); } else { - this.UncompressRle4(width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan); + this.UncompressRle4(stream, width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan); } for (int y = 0; y < height; y++) @@ -368,11 +367,12 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// Looks up color values and builds the image from de-compressed RLE24. /// /// The pixel format. + /// The containing image data. /// The to assign the palette to. /// The width of the bitmap. /// The height of the bitmap. /// Whether the bitmap is inverted. - private void ReadRle24(Buffer2D pixels, int width, int height, bool inverted) + private void ReadRle24(BufferedReadStream stream, Buffer2D pixels, int width, int height, bool inverted) where TPixel : unmanaged, IPixel { TPixel color = default; @@ -384,7 +384,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals Span undefinedPixelsSpan = undefinedPixels.Memory.Span; Span bufferSpan = buffer.GetSpan(); - this.UncompressRle24(width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan); + this.UncompressRle24(stream, width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan); for (int y = 0; y < height; y++) { int newY = Invert(y, height, inverted); @@ -446,18 +446,19 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals ///
If first byte is 0, the second byte may have special meaning. ///
Otherwise, the first byte is the length of the run and second byte contains two color indexes. /// + /// The containing image data. /// The width of the bitmap. /// Buffer for uncompressed data. /// Keeps track over skipped and therefore undefined pixels. /// Keeps track of rows, which have undefined pixels. - private void UncompressRle4(int w, Span buffer, Span undefinedPixels, Span rowsWithUndefinedPixels) + private void UncompressRle4(BufferedReadStream stream, int w, Span buffer, Span undefinedPixels, Span rowsWithUndefinedPixels) { Span cmd = stackalloc byte[2]; int count = 0; while (count < buffer.Length) { - if (this.stream.Read(cmd, 0, cmd.Length) != 2) + if (stream.Read(cmd, 0, cmd.Length) != 2) { BmpThrowHelper.ThrowInvalidImageContentException("Failed to read 2 bytes from the stream while uncompressing RLE4 bitmap."); } @@ -478,8 +479,8 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals break; case RleDelta: - int dx = this.stream.ReadByte(); - int dy = this.stream.ReadByte(); + int dx = stream.ReadByte(); + int dy = stream.ReadByte(); count += RleSkipDelta(count, w, dx, dy, undefinedPixels, rowsWithUndefinedPixels); break; @@ -492,7 +493,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals byte[] run = new byte[bytesToRead]; - this.stream.Read(run, 0, run.Length); + stream.Read(run, 0, run.Length); int idx = 0; for (int i = 0; i < max; i++) @@ -512,7 +513,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals // Absolute mode data is aligned to two-byte word-boundary. int padding = bytesToRead & 1; - this.stream.Skip(padding); + stream.Skip(padding); break; } @@ -551,18 +552,19 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals ///
If first byte is 0, the second byte may have special meaning. ///
Otherwise, the first byte is the length of the run and second byte is the color for the run. /// + /// The containing image data. /// The width of the bitmap. /// Buffer for uncompressed data. /// Keeps track of skipped and therefore undefined pixels. /// Keeps track of rows, which have undefined pixels. - private void UncompressRle8(int w, Span buffer, Span undefinedPixels, Span rowsWithUndefinedPixels) + private void UncompressRle8(BufferedReadStream stream, int w, Span buffer, Span undefinedPixels, Span rowsWithUndefinedPixels) { Span cmd = stackalloc byte[2]; int count = 0; while (count < buffer.Length) { - if (this.stream.Read(cmd, 0, cmd.Length) != 2) + if (stream.Read(cmd, 0, cmd.Length) != 2) { BmpThrowHelper.ThrowInvalidImageContentException("Failed to read 2 bytes from stream while uncompressing RLE8 bitmap."); } @@ -583,8 +585,8 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals break; case RleDelta: - int dx = this.stream.ReadByte(); - int dy = this.stream.ReadByte(); + int dx = stream.ReadByte(); + int dy = stream.ReadByte(); count += RleSkipDelta(count, w, dx, dy, undefinedPixels, rowsWithUndefinedPixels); break; @@ -596,7 +598,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals byte[] run = new byte[length]; - this.stream.Read(run, 0, run.Length); + stream.Read(run, 0, run.Length); run.AsSpan().CopyTo(buffer[count..]); @@ -605,7 +607,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals // Absolute mode data is aligned to two-byte word-boundary. int padding = length & 1; - this.stream.Skip(padding); + stream.Skip(padding); break; } @@ -630,18 +632,19 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals ///
If first byte is 0, the second byte may have special meaning. ///
Otherwise, the first byte is the length of the run and following three bytes are the color for the run. /// + /// The containing image data. /// The width of the bitmap. /// Buffer for uncompressed data. /// Keeps track of skipped and therefore undefined pixels. /// Keeps track of rows, which have undefined pixels. - private void UncompressRle24(int w, Span buffer, Span undefinedPixels, Span rowsWithUndefinedPixels) + private void UncompressRle24(BufferedReadStream stream, int w, Span buffer, Span undefinedPixels, Span rowsWithUndefinedPixels) { Span cmd = stackalloc byte[2]; int uncompressedPixels = 0; while (uncompressedPixels < buffer.Length) { - if (this.stream.Read(cmd, 0, cmd.Length) != 2) + if (stream.Read(cmd, 0, cmd.Length) != 2) { BmpThrowHelper.ThrowInvalidImageContentException("Failed to read 2 bytes from stream while uncompressing RLE24 bitmap."); } @@ -662,8 +665,8 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals break; case RleDelta: - int dx = this.stream.ReadByte(); - int dy = this.stream.ReadByte(); + int dx = stream.ReadByte(); + int dy = stream.ReadByte(); uncompressedPixels += RleSkipDelta(uncompressedPixels, w, dx, dy, undefinedPixels, rowsWithUndefinedPixels); break; @@ -675,7 +678,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals byte[] run = new byte[length * 3]; - this.stream.Read(run, 0, run.Length); + stream.Read(run, 0, run.Length); run.AsSpan().CopyTo(buffer[(uncompressedPixels * 3)..]); @@ -684,7 +687,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals // Absolute mode data is aligned to two-byte word-boundary. int padding = run.Length & 1; - this.stream.Skip(padding); + stream.Skip(padding); break; } @@ -693,8 +696,8 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals { int max = uncompressedPixels + cmd[0]; byte blueIdx = cmd[1]; - byte greenIdx = (byte)this.stream.ReadByte(); - byte redIdx = (byte)this.stream.ReadByte(); + byte greenIdx = (byte)stream.ReadByte(); + byte redIdx = (byte)stream.ReadByte(); int bufferIdx = uncompressedPixels * 3; for (; uncompressedPixels < max; uncompressedPixels++) @@ -800,6 +803,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// Reads the color palette from the stream. /// /// The pixel format. + /// The containing image data. /// The to assign the palette to. /// The containing the colors. /// The width of the bitmap. @@ -808,7 +812,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// Usually 4 bytes, but in case of Windows 2.x bitmaps or OS/2 1.x bitmaps /// the bytes per color palette entry's can be 3 bytes instead of 4. /// Whether the bitmap is inverted. - private void ReadRgbPalette(Buffer2D pixels, byte[] colors, int width, int height, int bitsPerPixel, int bytesPerColorMapEntry, bool inverted) + private void ReadRgbPalette(BufferedReadStream stream, Buffer2D pixels, byte[] colors, int width, int height, int bitsPerPixel, int bytesPerColorMapEntry, bool inverted) where TPixel : unmanaged, IPixel { // Pixels per byte (bits per pixel). @@ -833,7 +837,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals for (int y = 0; y < height; y++) { int newY = Invert(y, height, inverted); - if (this.stream.Read(rowSpan) == 0) + if (stream.Read(rowSpan) == 0) { BmpThrowHelper.ThrowInvalidImageContentException("Could not read enough data for a pixel row!"); } @@ -861,6 +865,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// Reads the 16 bit color palette from the stream. /// /// The pixel format. + /// The containing image data. /// The to assign the palette to. /// The width of the bitmap. /// The height of the bitmap. @@ -868,7 +873,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// The bitmask for the red channel. /// The bitmask for the green channel. /// The bitmask for the blue channel. - private void ReadRgb16(Buffer2D pixels, int width, int height, bool inverted, int redMask = DefaultRgb16RMask, int greenMask = DefaultRgb16GMask, int blueMask = DefaultRgb16BMask) + private void ReadRgb16(BufferedReadStream stream, Buffer2D pixels, int width, int height, bool inverted, int redMask = DefaultRgb16RMask, int greenMask = DefaultRgb16GMask, int blueMask = DefaultRgb16BMask) where TPixel : unmanaged, IPixel { int padding = CalculatePadding(width, 2); @@ -889,7 +894,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals for (int y = 0; y < height; y++) { - if (this.stream.Read(bufferSpan) == 0) + if (stream.Read(bufferSpan) == 0) { BmpThrowHelper.ThrowInvalidImageContentException("Could not read enough data for a pixel row!"); } @@ -935,11 +940,12 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// Reads the 24 bit color palette from the stream. /// /// The pixel format. + /// The containing image data. /// The to assign the palette to. /// The width of the bitmap. /// The height of the bitmap. /// Whether the bitmap is inverted. - private void ReadRgb24(Buffer2D pixels, int width, int height, bool inverted) + private void ReadRgb24(BufferedReadStream stream, Buffer2D pixels, int width, int height, bool inverted) where TPixel : unmanaged, IPixel { int padding = CalculatePadding(width, 3); @@ -948,7 +954,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals for (int y = 0; y < height; y++) { - if (this.stream.Read(rowSpan) == 0) + if (stream.Read(rowSpan) == 0) { BmpThrowHelper.ThrowInvalidImageContentException("Could not read enough data for a pixel row!"); } @@ -967,11 +973,12 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// Reads the 32 bit color palette from the stream. /// /// The pixel format. + /// The containing image data. /// The to assign the palette to. /// The width of the bitmap. /// The height of the bitmap. /// Whether the bitmap is inverted. - private void ReadRgb32Fast(Buffer2D pixels, int width, int height, bool inverted) + private void ReadRgb32Fast(BufferedReadStream stream, Buffer2D pixels, int width, int height, bool inverted) where TPixel : unmanaged, IPixel { int padding = CalculatePadding(width, 4); @@ -980,7 +987,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals for (int y = 0; y < height; y++) { - if (this.stream.Read(rowSpan) == 0) + if (stream.Read(rowSpan) == 0) { BmpThrowHelper.ThrowInvalidImageContentException("Could not read enough data for a pixel row!"); } @@ -1000,11 +1007,12 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// This is a special case only used for 32bpp WinBMPv3 files, which could be in either BGR0 or BGRA format. /// /// The pixel format. + /// The containing image data. /// The to assign the palette to. /// The width of the bitmap. /// The height of the bitmap. /// Whether the bitmap is inverted. - private void ReadRgb32Slow(Buffer2D pixels, int width, int height, bool inverted) + private void ReadRgb32Slow(BufferedReadStream stream, Buffer2D pixels, int width, int height, bool inverted) where TPixel : unmanaged, IPixel { int padding = CalculatePadding(width, 4); @@ -1012,7 +1020,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals using IMemoryOwner bgraRow = this.memoryAllocator.Allocate(width); Span rowSpan = row.GetSpan(); Span bgraRowSpan = bgraRow.GetSpan(); - long currentPosition = this.stream.Position; + long currentPosition = stream.Position; bool hasAlpha = false; // Loop though the rows checking each pixel. We start by assuming it's @@ -1020,7 +1028,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals // actually a BGRA image, and change tactics accordingly. for (int y = 0; y < height; y++) { - if (this.stream.Read(rowSpan) == 0) + if (stream.Read(rowSpan) == 0) { BmpThrowHelper.ThrowInvalidImageContentException("Could not read enough data for a pixel row!"); } @@ -1049,14 +1057,14 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals } // Reset our stream for a second pass. - this.stream.Position = currentPosition; + stream.Position = currentPosition; // Process the pixels in bulk taking the raw alpha component value. if (hasAlpha) { for (int y = 0; y < height; y++) { - if (this.stream.Read(rowSpan) == 0) + if (stream.Read(rowSpan) == 0) { BmpThrowHelper.ThrowInvalidImageContentException("Could not read enough data for a pixel row!"); } @@ -1077,7 +1085,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals // Slow path. We need to set each alpha component value to fully opaque. for (int y = 0; y < height; y++) { - if (this.stream.Read(rowSpan) == 0) + if (stream.Read(rowSpan) == 0) { BmpThrowHelper.ThrowInvalidImageContentException("Could not read enough data for a pixel row!"); } @@ -1105,6 +1113,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// Decode an 32 Bit Bitmap containing a bitmask for each color channel. /// /// The pixel format. + /// The containing image data. /// The output pixel buffer containing the decoded image. /// The width of the image. /// The height of the image. @@ -1113,7 +1122,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// The bitmask for the green channel. /// The bitmask for the blue channel. /// The bitmask for the alpha channel. - private void ReadRgb32BitFields(Buffer2D pixels, int width, int height, bool inverted, int redMask, int greenMask, int blueMask, int alphaMask) + 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; @@ -1142,7 +1151,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals for (int y = 0; y < height; y++) { - if (this.stream.Read(bufferSpan) == 0) + if (stream.Read(bufferSpan) == 0) { BmpThrowHelper.ThrowInvalidImageContentException("Could not read enough data for a pixel row!"); } @@ -1228,10 +1237,13 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// /// Reads the from the stream. /// - private void ReadInfoHeader() + /// The containing image data. + [MemberNotNull(nameof(metadata))] + [MemberNotNull(nameof(bmpMetadata))] + private void ReadInfoHeader(BufferedReadStream stream) { Span buffer = stackalloc byte[BmpInfoHeader.MaxHeaderSize]; - long infoHeaderStart = this.stream.Position; + long infoHeaderStart = stream.Position; // Resolution is stored in PPM. this.metadata = new ImageMetadata @@ -1240,7 +1252,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals }; // Read the header size. - this.stream.Read(buffer, 0, BmpInfoHeader.HeaderSizeSize); + stream.Read(buffer, 0, BmpInfoHeader.HeaderSizeSize); int headerSize = BinaryPrimitives.ReadInt32LittleEndian(buffer); if (headerSize is < BmpInfoHeader.CoreSize or > BmpInfoHeader.MaxHeaderSize) @@ -1249,7 +1261,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals } // Read the rest of the header. - this.stream.Read(buffer, BmpInfoHeader.HeaderSizeSize, headerSize - BmpInfoHeader.HeaderSizeSize); + stream.Read(buffer, BmpInfoHeader.HeaderSizeSize, headerSize - BmpInfoHeader.HeaderSizeSize); BmpInfoHeaderType infoHeaderType = BmpInfoHeaderType.WinVersion2; if (headerSize == BmpInfoHeader.CoreSize) @@ -1275,7 +1287,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals if (this.infoHeader.Compression == BmpCompression.BitFields) { byte[] bitfieldsBuffer = new byte[12]; - this.stream.Read(bitfieldsBuffer, 0, 12); + stream.Read(bitfieldsBuffer, 0, 12); Span data = bitfieldsBuffer.AsSpan(); this.infoHeader.RedMask = BinaryPrimitives.ReadInt32LittleEndian(data[..4]); this.infoHeader.GreenMask = BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)); @@ -1284,7 +1296,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals else if (this.infoHeader.Compression == BmpCompression.BI_ALPHABITFIELDS) { byte[] bitfieldsBuffer = new byte[16]; - this.stream.Read(bitfieldsBuffer, 0, 16); + stream.Read(bitfieldsBuffer, 0, 16); Span data = bitfieldsBuffer.AsSpan(); this.infoHeader.RedMask = BinaryPrimitives.ReadInt32LittleEndian(data[..4]); this.infoHeader.GreenMask = BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)); @@ -1324,12 +1336,12 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals if (this.infoHeader.ProfileData != 0 && this.infoHeader.ProfileSize != 0) { // Read color profile. - long streamPosition = this.stream.Position; + long streamPosition = stream.Position; byte[] iccProfileData = new byte[this.infoHeader.ProfileSize]; - this.stream.Position = infoHeaderStart + this.infoHeader.ProfileData; - this.stream.Read(iccProfileData); + stream.Position = infoHeaderStart + this.infoHeader.ProfileData; + stream.Read(iccProfileData); this.metadata.IccProfile = new IccProfile(iccProfileData); - this.stream.Position = streamPosition; + stream.Position = streamPosition; } } else @@ -1358,10 +1370,11 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// /// Reads the from the stream. /// - private void ReadFileHeader() + /// The containing image data. + private void ReadFileHeader(BufferedReadStream stream) { Span buffer = stackalloc byte[BmpFileHeader.Size]; - this.stream.Read(buffer, 0, BmpFileHeader.Size); + stream.Read(buffer, 0, BmpFileHeader.Size); short fileTypeMarker = BinaryPrimitives.ReadInt16LittleEndian(buffer); switch (fileTypeMarker) @@ -1375,7 +1388,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals // Because we only decode the first bitmap in the array, the array header will be ignored. // The bitmap file header of the first image follows the array header. - this.stream.Read(buffer, 0, BmpFileHeader.Size); + stream.Read(buffer, 0, BmpFileHeader.Size); this.fileHeader = BmpFileHeader.Parse(buffer); if (this.fileHeader.Type != BmpConstants.TypeMarkers.Bitmap) { @@ -1398,12 +1411,12 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// The color palette. /// Bytes per color palette entry. Usually 4 bytes, but in case of Windows 2.x bitmaps or OS/2 1.x bitmaps /// the bytes per color palette entry's can be 3 bytes instead of 4. + [MemberNotNull(nameof(metadata))] + [MemberNotNull(nameof(bmpMetadata))] private int ReadImageHeaders(BufferedReadStream stream, out bool inverted, out byte[] palette) { - this.stream = stream; - - this.ReadFileHeader(); - this.ReadInfoHeader(); + this.ReadFileHeader(stream); + this.ReadInfoHeader(stream); // see http://www.drdobbs.com/architecture-and-design/the-bmp-file-format-part-1/184409517 // If the height is negative, then this is a Windows bitmap whose origin @@ -1451,13 +1464,13 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals colorMapSizeBytes = this.infoHeader.ClrUsed * bytesPerColorMapEntry; } - palette = null; + palette = Array.Empty(); if (colorMapSizeBytes > 0) { // Usually the color palette is 1024 byte (256 colors * 4), but the documentation does not mention a size limit. // Make sure, that we will not read pass the bitmap offset (starting position of image data). - if ((this.stream.Position + colorMapSizeBytes) > this.fileHeader.Offset) + if ((stream.Position + colorMapSizeBytes) > this.fileHeader.Offset) { BmpThrowHelper.ThrowInvalidImageContentException( $"Reading the color map would read beyond the bitmap offset. Either the color map size of '{colorMapSizeBytes}' is invalid or the bitmap offset."); @@ -1465,21 +1478,21 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals palette = new byte[colorMapSizeBytes]; - if (this.stream.Read(palette, 0, colorMapSizeBytes) == 0) + if (stream.Read(palette, 0, colorMapSizeBytes) == 0) { BmpThrowHelper.ThrowInvalidImageContentException("Could not read enough data for the palette!"); } } - int skipAmount = this.fileHeader.Offset - (int)this.stream.Position; - if ((skipAmount + (int)this.stream.Position) > this.stream.Length) + int skipAmount = this.fileHeader.Offset - (int)stream.Position; + if ((skipAmount + (int)stream.Position) > stream.Length) { BmpThrowHelper.ThrowInvalidImageContentException("Invalid file header offset found. Offset is greater than the stream length."); } if (skipAmount > 0) { - this.stream.Skip(skipAmount); + stream.Skip(skipAmount); } return bytesPerColorMapEntry; diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index 9f1e856393..0f432fbda9 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Buffers; using System.Buffers.Binary; @@ -72,7 +71,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// /// The global configuration. /// - private Configuration configuration; + private Configuration? configuration; /// /// The color depth, in number of bits per pixel. @@ -142,7 +141,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals _ => 0 }; - byte[] iccProfileData = null; + byte[] iccProfileData = Array.Empty(); int iccProfileSize = 0; if (metadata.IccProfile != null) { diff --git a/src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor{TPixel}.cs index abf283e6c1..982cc7d46c 100644 --- a/src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor{TPixel}.cs @@ -35,7 +35,7 @@ internal sealed class PaletteDitherProcessor : ImageProcessor ReadOnlySpan sourcePalette = definition.Palette.Span; this.paletteOwner = this.Configuration.MemoryAllocator.Allocate(sourcePalette.Length); - Color.ToPixel(this.Configuration, sourcePalette, this.paletteOwner.Memory.Span); + Color.ToPixel(sourcePalette, this.paletteOwner.Memory.Span); this.ditherProcessor = new DitherProcessor( this.Configuration, diff --git a/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs index 9d5b606040..f46cea2e0b 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs @@ -21,7 +21,7 @@ public interface IQuantizer /// The to configure internal operations. /// The pixel format. /// The . - IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) + IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration) where TPixel : unmanaged, IPixel; /// @@ -31,6 +31,6 @@ public interface IQuantizer /// The to configure internal operations. /// The options to create the quantizer with. /// The . - IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) + IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel; } diff --git a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs index 0a1032bf0d..0d2450e221 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs @@ -34,12 +34,12 @@ public class OctreeQuantizer : IQuantizer public QuantizerOptions Options { get; } /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) + public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration) where TPixel : unmanaged, IPixel => this.CreatePixelSpecificQuantizer(configuration, this.Options); /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) + public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel => new OctreeQuantizer(configuration, options); } diff --git a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs index d66a38d80d..37ebc39bac 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs @@ -38,7 +38,7 @@ public struct OctreeQuantizer : IQuantizer /// The configuration which allows altering default behaviour or extending the library. /// The quantizer options defining quantization rules. [MethodImpl(InliningOptions.ShortMethod)] - public OctreeQuantizer(Configuration configuration, QuantizerOptions options) + public OctreeQuantizer(Configuration? configuration, QuantizerOptions options) { Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(options, nameof(options)); diff --git a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs index f3ed39c957..c5617fe862 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs @@ -39,19 +39,19 @@ public class PaletteQuantizer : IQuantizer public QuantizerOptions Options { get; } /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) + public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration) where TPixel : unmanaged, IPixel => this.CreatePixelSpecificQuantizer(configuration, this.Options); /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) + public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel { Guard.NotNull(options, nameof(options)); // Always use the palette length over options since the palette cannot be reduced. TPixel[] palette = new TPixel[this.colorPalette.Length]; - Color.ToPixel(configuration, this.colorPalette.Span, palette.AsSpan()); + Color.ToPixel(this.colorPalette.Span, palette.AsSpan()); return new PaletteQuantizer(configuration, options, palette); } } diff --git a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs index ea7413aab4..182bed2382 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs @@ -29,7 +29,7 @@ internal struct PaletteQuantizer : IQuantizer /// The quantizer options defining quantization rules. /// The palette to use. [MethodImpl(InliningOptions.ShortMethod)] - public PaletteQuantizer(Configuration configuration, QuantizerOptions options, ReadOnlyMemory palette) + public PaletteQuantizer(Configuration? configuration, QuantizerOptions options, ReadOnlyMemory palette) { Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(options, nameof(options)); diff --git a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs index 86d798d965..dff9155401 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs @@ -33,12 +33,12 @@ public class WuQuantizer : IQuantizer public QuantizerOptions Options { get; } /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) + public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration) where TPixel : unmanaged, IPixel => this.CreatePixelSpecificQuantizer(configuration, this.Options); /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) + public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel => new WuQuantizer(configuration, options); } diff --git a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs index 0119558bff..c04dbe1918 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs @@ -85,7 +85,7 @@ internal struct WuQuantizer : IQuantizer /// The configuration which allows altering default behaviour or extending the library. /// The quantizer options defining quantization rules. [MethodImpl(InliningOptions.ShortMethod)] - public WuQuantizer(Configuration configuration, QuantizerOptions options) + public WuQuantizer(Configuration? configuration, QuantizerOptions options) { Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(options, nameof(options)); From cd9a06b4cb6d011b3ebd3e73cfa5944483f6f775 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Sun, 5 Feb 2023 17:54:39 +0100 Subject: [PATCH 02/22] Fix unit test. --- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index 0f432fbda9..e1611b059b 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -141,7 +141,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals _ => 0 }; - byte[] iccProfileData = Array.Empty(); + byte[]? iccProfileData = null; int iccProfileSize = 0; if (metadata.IccProfile != null) { @@ -181,7 +181,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The metadata. /// The icc profile data. /// The bitmap information header. - private BmpInfoHeader CreateBmpInfoHeader(int width, int height, int infoHeaderSize, short bpp, int bytesPerLine, ImageMetadata metadata, byte[] iccProfileData) + private BmpInfoHeader CreateBmpInfoHeader(int width, int height, int infoHeaderSize, short bpp, int bytesPerLine, ImageMetadata metadata, byte[]? iccProfileData) { int hResolution = 0; int vResolution = 0; @@ -235,7 +235,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals if (this.infoHeaderType is BmpInfoHeaderType.WinVersion5 && metadata.IccProfile != null) { - infoHeader.ProfileSize = iccProfileData.Length; + infoHeader.ProfileSize = iccProfileData!.Length; infoHeader.CsType = BmpColorSpace.PROFILE_EMBEDDED; infoHeader.Intent = BmpRenderingIntent.LCS_GM_IMAGES; } @@ -249,7 +249,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The stream to write to. /// The color profile data. /// The buffer. - private static void WriteColorProfile(Stream stream, byte[] iccProfileData, Span buffer) + private static void WriteColorProfile(Stream stream, byte[]? iccProfileData, Span buffer) { if (iccProfileData != null) { From 94fd43f71ff34fb714e32f892bd5c37a8928b442 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Sun, 5 Feb 2023 18:09:53 +0100 Subject: [PATCH 03/22] remove nullable disable from format gif --- src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 139 ++++++++++--------- src/ImageSharp/Formats/Gif/GifEncoderCore.cs | 24 ++-- src/ImageSharp/Image.Decode.cs | 2 +- src/ImageSharp/Image.cs | 4 +- src/ImageSharp/ImageInfo.cs | 4 +- src/ImageSharp/Image{TPixel}.cs | 4 +- 6 files changed, 90 insertions(+), 87 deletions(-) diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index 8c59158000..0ccbe3db54 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -1,8 +1,8 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Buffers; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; @@ -24,15 +24,10 @@ internal sealed class GifDecoderCore : IImageDecoderInternals /// private readonly byte[] buffer = new byte[16]; - /// - /// The currently loaded stream. - /// - private BufferedReadStream stream; - /// /// The global color table. /// - private IMemoryOwner globalColorTable; + private IMemoryOwner? globalColorTable; /// /// The area to restore. @@ -77,12 +72,12 @@ internal sealed class GifDecoderCore : IImageDecoderInternals /// /// The abstract metadata. /// - private ImageMetadata metadata; + private ImageMetadata? metadata; /// /// The gif specific metadata. /// - private GifMetadata gifMetadata; + private GifMetadata? gifMetadata; /// /// Initializes a new instance of the class. @@ -108,8 +103,8 @@ internal sealed class GifDecoderCore : IImageDecoderInternals where TPixel : unmanaged, IPixel { uint frameCount = 0; - Image image = null; - ImageFrame previousFrame = null; + Image? image = null; + ImageFrame? previousFrame = null; try { this.ReadLogicalScreenDescriptorAndGlobalColorTable(stream); @@ -125,7 +120,7 @@ internal sealed class GifDecoderCore : IImageDecoderInternals break; } - this.ReadFrame(ref image, ref previousFrame); + this.ReadFrame(stream, ref image, ref previousFrame); // Reset per-frame state. this.imageDescriptor = default; @@ -136,16 +131,16 @@ internal sealed class GifDecoderCore : IImageDecoderInternals switch (stream.ReadByte()) { case GifConstants.GraphicControlLabel: - this.ReadGraphicalControlExtension(); + this.ReadGraphicalControlExtension(stream); break; case GifConstants.CommentLabel: - this.ReadComments(); + this.ReadComments(stream); break; case GifConstants.ApplicationExtensionLabel: - this.ReadApplicationExtension(); + this.ReadApplicationExtension(stream); break; case GifConstants.PlainTextLabel: - this.SkipBlock(); // Not supported by any known decoder. + this.SkipBlock(stream); // Not supported by any known decoder. break; } } @@ -187,23 +182,23 @@ internal sealed class GifDecoderCore : IImageDecoderInternals { if (nextFlag == GifConstants.ImageLabel) { - this.ReadImageDescriptor(); + this.ReadImageDescriptor(stream); } else if (nextFlag == GifConstants.ExtensionIntroducer) { switch (stream.ReadByte()) { case GifConstants.GraphicControlLabel: - this.SkipBlock(); // Skip graphic control extension block + this.SkipBlock(stream); // Skip graphic control extension block break; case GifConstants.CommentLabel: - this.ReadComments(); + this.ReadComments(stream); break; case GifConstants.ApplicationExtensionLabel: - this.ReadApplicationExtension(); + this.ReadApplicationExtension(stream); break; case GifConstants.PlainTextLabel: - this.SkipBlock(); // Not supported by any known decoder. + this.SkipBlock(stream); // Not supported by any known decoder. break; } } @@ -239,9 +234,10 @@ internal sealed class GifDecoderCore : IImageDecoderInternals /// /// Reads the graphic control extension. /// - private void ReadGraphicalControlExtension() + /// The containing image data. + private void ReadGraphicalControlExtension(BufferedReadStream stream) { - int bytesRead = this.stream.Read(this.buffer, 0, 6); + int bytesRead = stream.Read(this.buffer, 0, 6); if (bytesRead != 6) { GifThrowHelper.ThrowInvalidImageContentException("Not enough data to read the graphic control extension"); @@ -253,9 +249,10 @@ internal sealed class GifDecoderCore : IImageDecoderInternals /// /// Reads the image descriptor. /// - private void ReadImageDescriptor() + /// The containing image data. + private void ReadImageDescriptor(BufferedReadStream stream) { - int bytesRead = this.stream.Read(this.buffer, 0, 9); + int bytesRead = stream.Read(this.buffer, 0, 9); if (bytesRead != 9) { GifThrowHelper.ThrowInvalidImageContentException("Not enough data to read the image descriptor"); @@ -271,9 +268,10 @@ internal sealed class GifDecoderCore : IImageDecoderInternals /// /// Reads the logical screen descriptor. /// - private void ReadLogicalScreenDescriptor() + /// The containing image data. + private void ReadLogicalScreenDescriptor(BufferedReadStream stream) { - int bytesRead = this.stream.Read(this.buffer, 0, 7); + int bytesRead = stream.Read(this.buffer, 0, 7); if (bytesRead != 7) { GifThrowHelper.ThrowInvalidImageContentException("Not enough data to read the logical screen descriptor"); @@ -286,84 +284,87 @@ internal sealed class GifDecoderCore : IImageDecoderInternals /// Reads the application extension block parsing any animation or XMP information /// if present. /// - private void ReadApplicationExtension() + /// The containing image data. + private void ReadApplicationExtension(BufferedReadStream stream) { - int appLength = this.stream.ReadByte(); + int appLength = stream.ReadByte(); // If the length is 11 then it's a valid extension and most likely // a NETSCAPE, XMP or ANIMEXTS extension. We want the loop count from this. - long position = this.stream.Position; + long position = stream.Position; if (appLength == GifConstants.ApplicationBlockSize) { - this.stream.Read(this.buffer, 0, GifConstants.ApplicationBlockSize); + stream.Read(this.buffer, 0, GifConstants.ApplicationBlockSize); bool isXmp = this.buffer.AsSpan().StartsWith(GifConstants.XmpApplicationIdentificationBytes); if (isXmp && !this.skipMetadata) { - GifXmpApplicationExtension extension = GifXmpApplicationExtension.Read(this.stream, this.memoryAllocator); + GifXmpApplicationExtension extension = GifXmpApplicationExtension.Read(stream, this.memoryAllocator); if (extension.Data.Length > 0) { - this.metadata.XmpProfile = new XmpProfile(extension.Data); + this.metadata!.XmpProfile = new XmpProfile(extension.Data); } else { // Reset the stream position and continue. - this.stream.Position = position; - this.SkipBlock(appLength); + stream.Position = position; + this.SkipBlock(stream, appLength); } return; } - int subBlockSize = this.stream.ReadByte(); + int subBlockSize = stream.ReadByte(); // TODO: There's also a NETSCAPE buffer extension. // http://www.vurdalakov.net/misc/gif/netscape-buffering-application-extension if (subBlockSize == GifConstants.NetscapeLoopingSubBlockSize) { - this.stream.Read(this.buffer, 0, GifConstants.NetscapeLoopingSubBlockSize); - this.gifMetadata.RepeatCount = GifNetscapeLoopingApplicationExtension.Parse(this.buffer.AsSpan(1)).RepeatCount; - this.stream.Skip(1); // Skip the terminator. + stream.Read(this.buffer, 0, GifConstants.NetscapeLoopingSubBlockSize); + this.gifMetadata!.RepeatCount = GifNetscapeLoopingApplicationExtension.Parse(this.buffer.AsSpan(1)).RepeatCount; + stream.Skip(1); // Skip the terminator. return; } // Could be something else not supported yet. // Skip the subblock and terminator. - this.SkipBlock(subBlockSize); + this.SkipBlock(stream, subBlockSize); return; } - this.SkipBlock(appLength); // Not supported by any known decoder. + this.SkipBlock(stream, appLength); // Not supported by any known decoder. } /// /// Skips over a block or reads its terminator. /// The length of the block to skip. + /// The containing image data. /// - private void SkipBlock(int blockSize = 0) + private void SkipBlock(BufferedReadStream stream, int blockSize = 0) { if (blockSize > 0) { - this.stream.Skip(blockSize); + stream.Skip(blockSize); } int flag; - while ((flag = this.stream.ReadByte()) > 0) + while ((flag = stream.ReadByte()) > 0) { - this.stream.Skip(flag); + stream.Skip(flag); } } /// /// Reads the gif comments. /// - private void ReadComments() + /// The containing image data. + private void ReadComments(BufferedReadStream stream) { int length; var stringBuilder = new StringBuilder(); - while ((length = this.stream.ReadByte()) != 0) + while ((length = stream.ReadByte()) != 0) { if (length > GifConstants.MaxCommentSubBlockLength) { @@ -372,21 +373,21 @@ internal sealed class GifDecoderCore : IImageDecoderInternals if (this.skipMetadata) { - this.stream.Seek(length, SeekOrigin.Current); + stream.Seek(length, SeekOrigin.Current); continue; } using IMemoryOwner commentsBuffer = this.memoryAllocator.Allocate(length); Span commentsSpan = commentsBuffer.GetSpan(); - this.stream.Read(commentsSpan); + stream.Read(commentsSpan); string commentPart = GifConstants.Encoding.GetString(commentsSpan); stringBuilder.Append(commentPart); } if (stringBuilder.Length > 0) { - this.gifMetadata.Comments.Add(stringBuilder.ToString()); + this.gifMetadata!.Comments.Add(stringBuilder.ToString()); } } @@ -394,15 +395,16 @@ internal sealed class GifDecoderCore : IImageDecoderInternals /// Reads an individual gif frame. /// /// The pixel format. + /// The containing image data. /// The image to decode the information to. /// The previous frame. - private void ReadFrame(ref Image image, ref ImageFrame previousFrame) + private void ReadFrame(BufferedReadStream stream, ref Image? image, ref ImageFrame? previousFrame) where TPixel : unmanaged, IPixel { - this.ReadImageDescriptor(); + this.ReadImageDescriptor(stream); - IMemoryOwner localColorTable = null; - Buffer2D indices = null; + IMemoryOwner? localColorTable = null; + Buffer2D? indices = null; try { // Determine the color table for this frame. If there is a local one, use it otherwise use the global color table. @@ -410,11 +412,11 @@ internal sealed class GifDecoderCore : IImageDecoderInternals { int length = this.imageDescriptor.LocalColorTableSize * 3; localColorTable = this.configuration.MemoryAllocator.Allocate(length, AllocationOptions.Clean); - this.stream.Read(localColorTable.GetSpan()); + stream.Read(localColorTable.GetSpan()); } indices = this.configuration.MemoryAllocator.Allocate2D(this.imageDescriptor.Width, this.imageDescriptor.Height, AllocationOptions.Clean); - this.ReadFrameIndices(indices); + this.ReadFrameIndices(stream, indices); Span rawColorTable = default; if (localColorTable != null) @@ -430,7 +432,7 @@ internal sealed class GifDecoderCore : IImageDecoderInternals this.ReadFrameColors(ref image, ref previousFrame, indices, colorTable, this.imageDescriptor); // Skip any remaining blocks - this.SkipBlock(); + this.SkipBlock(stream); } finally { @@ -442,12 +444,13 @@ internal sealed class GifDecoderCore : IImageDecoderInternals /// /// Reads the frame indices marking the color to use for each pixel. /// + /// The containing image data. /// The 2D pixel buffer to write to. [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ReadFrameIndices(Buffer2D indices) + private void ReadFrameIndices(BufferedReadStream stream, Buffer2D indices) { - int minCodeSize = this.stream.ReadByte(); - using var lzwDecoder = new LzwDecoder(this.configuration.MemoryAllocator, this.stream); + int minCodeSize = stream.ReadByte(); + using LzwDecoder lzwDecoder = new LzwDecoder(this.configuration.MemoryAllocator, stream); lzwDecoder.DecodePixels(minCodeSize, indices); } @@ -460,15 +463,15 @@ internal sealed class GifDecoderCore : IImageDecoderInternals /// The indexed pixels. /// The color table containing the available colors. /// The - private void ReadFrameColors(ref Image image, ref ImageFrame previousFrame, Buffer2D indices, ReadOnlySpan colorTable, in GifImageDescriptor descriptor) + private void ReadFrameColors(ref Image? image, ref ImageFrame? previousFrame, Buffer2D indices, ReadOnlySpan colorTable, in GifImageDescriptor descriptor) where TPixel : unmanaged, IPixel { int imageWidth = this.logicalScreenDescriptor.Width; int imageHeight = this.logicalScreenDescriptor.Height; bool transFlag = this.graphicsControlExtension.TransparencyFlag; - ImageFrame prevFrame = null; - ImageFrame currentFrame = null; + ImageFrame? prevFrame = null; + ImageFrame? currentFrame = null; ImageFrame imageFrame; if (previousFrame is null) @@ -494,7 +497,7 @@ internal sealed class GifDecoderCore : IImageDecoderInternals prevFrame = previousFrame; } - currentFrame = image.Frames.CreateFrame(); + currentFrame = image!.Frames.CreateFrame(); this.SetFrameMetadata(currentFrame.Metadata, false); @@ -661,13 +664,13 @@ internal sealed class GifDecoderCore : IImageDecoderInternals /// Reads the logical screen descriptor and global color table blocks /// /// The stream containing image data. + [MemberNotNull(nameof(metadata))] + [MemberNotNull(nameof(gifMetadata))] private void ReadLogicalScreenDescriptorAndGlobalColorTable(BufferedReadStream stream) { - this.stream = stream; - // Skip the identifier - this.stream.Skip(6); - this.ReadLogicalScreenDescriptor(); + stream.Skip(6); + this.ReadLogicalScreenDescriptor(stream); ImageMetadata meta = new(); diff --git a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs index eaa6852937..0e59a0f05e 100644 --- a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs @@ -1,8 +1,8 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Buffers; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using SixLabors.ImageSharp.Advanced; @@ -93,7 +93,7 @@ internal sealed class GifEncoderCore : IImageEncoderInternals bool useGlobalTable = this.colorTableMode == GifColorTableMode.Global; // Quantize the image returning a palette. - IndexedImageFrame quantized; + IndexedImageFrame? quantized; using (IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(this.configuration)) { if (useGlobalTable) @@ -129,7 +129,7 @@ internal sealed class GifEncoderCore : IImageEncoderInternals this.WriteComments(gifMetadata, stream); // Write application extensions. - XmpProfile xmpProfile = image.Metadata.XmpProfile ?? image.Frames.RootFrame.Metadata.XmpProfile; + XmpProfile? xmpProfile = image.Metadata.XmpProfile ?? image.Frames.RootFrame.Metadata.XmpProfile; this.WriteApplicationExtensions(stream, image.Frames.Count, gifMetadata.RepeatCount, xmpProfile); } @@ -141,7 +141,7 @@ internal sealed class GifEncoderCore : IImageEncoderInternals private void EncodeFrames( Stream stream, Image image, - IndexedImageFrame quantized, + IndexedImageFrame? quantized, ReadOnlyMemory palette) where TPixel : unmanaged, IPixel { @@ -152,8 +152,8 @@ internal sealed class GifEncoderCore : IImageEncoderInternals // Gather the metadata for this frame. ImageFrame frame = image.Frames[i]; ImageFrameMetadata metadata = frame.Metadata; - bool hasMetadata = metadata.TryGetGifMetadata(out GifFrameMetadata frameMetadata); - bool useLocal = this.colorTableMode == GifColorTableMode.Local || (hasMetadata && frameMetadata.ColorTableMode == GifColorTableMode.Local); + bool hasMetadata = metadata.TryGetGifMetadata(out GifFrameMetadata? frameMetadata); + bool useLocal = this.colorTableMode == GifColorTableMode.Local || (hasMetadata && frameMetadata!.ColorTableMode == GifColorTableMode.Local); if (!useLocal && !hasPaletteQuantizer && i > 0) { @@ -164,7 +164,7 @@ internal sealed class GifEncoderCore : IImageEncoderInternals paletteQuantizer = new(this.configuration, this.quantizer.Options, palette); } - this.EncodeFrame(stream, frame, i, useLocal, frameMetadata, ref quantized, ref paletteQuantizer); + this.EncodeFrame(stream, frame, i, useLocal, frameMetadata, ref quantized!, ref paletteQuantizer); // Clean up for the next run. quantized.Dispose(); @@ -182,7 +182,7 @@ internal sealed class GifEncoderCore : IImageEncoderInternals ImageFrame frame, int frameIndex, bool useLocal, - GifFrameMetadata metadata, + GifFrameMetadata? metadata, ref IndexedImageFrame quantized, ref PaletteQuantizer paletteQuantizer) where TPixel : unmanaged, IPixel @@ -193,7 +193,7 @@ internal sealed class GifEncoderCore : IImageEncoderInternals if (useLocal) { // Reassign using the current frame and details. - QuantizerOptions options = null; + QuantizerOptions? options = null; int colorTableLength = metadata?.ColorTableLength ?? 0; if (colorTableLength > 0) { @@ -338,7 +338,7 @@ internal sealed class GifEncoderCore : IImageEncoderInternals /// The frame count fo this image. /// The animated image repeat count. /// The XMP metadata profile. Null if profile is not to be written. - private void WriteApplicationExtensions(Stream stream, int frameCount, ushort repeatCount, XmpProfile xmpProfile) + private void WriteApplicationExtensions(Stream stream, int frameCount, ushort repeatCount, XmpProfile? xmpProfile) { // Application Extension: Loop repeat count. if (frameCount > 1 && repeatCount != 1) @@ -350,7 +350,7 @@ internal sealed class GifEncoderCore : IImageEncoderInternals // Application Extension: XMP Profile. if (xmpProfile != null) { - GifXmpApplicationExtension xmpExtension = new(xmpProfile.Data); + GifXmpApplicationExtension xmpExtension = new(xmpProfile.Data!); this.WriteExtension(xmpExtension, stream); } } @@ -439,7 +439,7 @@ internal sealed class GifEncoderCore : IImageEncoderInternals private void WriteExtension(TGifExtension extension, Stream stream) where TGifExtension : struct, IGifExtension { - IMemoryOwner owner = null; + IMemoryOwner? owner = null; Span extensionBuffer; int extensionSize = extension.ContentLength; diff --git a/src/ImageSharp/Image.Decode.cs b/src/ImageSharp/Image.Decode.cs index ae38144c02..887cb23ca4 100644 --- a/src/ImageSharp/Image.Decode.cs +++ b/src/ImageSharp/Image.Decode.cs @@ -91,7 +91,7 @@ public abstract partial class Image ImageFormatManager.ThrowInvalidDecoder(configuration.ImageFormatsManager); } - return format!; + return format; } /// diff --git a/src/ImageSharp/Image.cs b/src/ImageSharp/Image.cs index 65b2d68b0a..af1b1916f9 100644 --- a/src/ImageSharp/Image.cs +++ b/src/ImageSharp/Image.cs @@ -28,7 +28,7 @@ public abstract partial class Image : ImageInfo, IDisposable, IConfigurationProv /// The pixel type information. /// The image metadata. /// The size in px units. - protected Image(Configuration configuration, PixelTypeInfo pixelType, ImageMetadata metadata, Size size) + protected Image(Configuration configuration, PixelTypeInfo pixelType, ImageMetadata? metadata, Size size) : base(pixelType, size, metadata) => this.configuration = configuration ?? Configuration.Default; @@ -43,7 +43,7 @@ public abstract partial class Image : ImageInfo, IDisposable, IConfigurationProv internal Image( Configuration configuration, PixelTypeInfo pixelType, - ImageMetadata metadata, + ImageMetadata? metadata, int width, int height) : this(configuration, pixelType, metadata, new Size(width, height)) diff --git a/src/ImageSharp/ImageInfo.cs b/src/ImageSharp/ImageInfo.cs index 9a6452ab89..fdc15a8127 100644 --- a/src/ImageSharp/ImageInfo.cs +++ b/src/ImageSharp/ImageInfo.cs @@ -18,7 +18,7 @@ public class ImageInfo /// The width of the image in px units. /// The height of the image in px units. /// The image metadata. - public ImageInfo(PixelTypeInfo pixelType, int width, int height, ImageMetadata metadata) + public ImageInfo(PixelTypeInfo pixelType, int width, int height, ImageMetadata? metadata) : this(pixelType, new(width, height), metadata) { } @@ -29,7 +29,7 @@ public class ImageInfo /// The pixel type information. /// The size of the image in px units. /// The image metadata. - public ImageInfo(PixelTypeInfo pixelType, Size size, ImageMetadata metadata) + public ImageInfo(PixelTypeInfo pixelType, Size size, ImageMetadata? metadata) { this.PixelType = pixelType; this.Metadata = metadata ?? new ImageMetadata(); diff --git a/src/ImageSharp/Image{TPixel}.cs b/src/ImageSharp/Image{TPixel}.cs index 9e3cc065c0..ed0db9b85b 100644 --- a/src/ImageSharp/Image{TPixel}.cs +++ b/src/ImageSharp/Image{TPixel}.cs @@ -77,7 +77,7 @@ public sealed class Image : Image /// The width of the image in pixels. /// The height of the image in pixels. /// The images metadata. - internal Image(Configuration configuration, int width, int height, ImageMetadata metadata) + internal Image(Configuration configuration, int width, int height, ImageMetadata? metadata) : base(configuration, PixelTypeInfo.Create(), metadata, width, height) => this.frames = new ImageFrameCollection(this, width, height, default(TPixel)); @@ -128,7 +128,7 @@ public sealed class Image : Image int width, int height, TPixel backgroundColor, - ImageMetadata metadata) + ImageMetadata? metadata) : base(configuration, PixelTypeInfo.Create(), metadata, width, height) => this.frames = new ImageFrameCollection(this, width, height, backgroundColor); From 90ac3a29c9292d14ec217033c75140238262ba2e Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Sun, 5 Feb 2023 18:40:45 +0100 Subject: [PATCH 04/22] Make skipblock static to fix build --- src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index 0ccbe3db54..6c82d61e1d 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -140,7 +140,7 @@ internal sealed class GifDecoderCore : IImageDecoderInternals this.ReadApplicationExtension(stream); break; case GifConstants.PlainTextLabel: - this.SkipBlock(stream); // Not supported by any known decoder. + SkipBlock(stream); // Not supported by any known decoder. break; } } @@ -189,7 +189,7 @@ internal sealed class GifDecoderCore : IImageDecoderInternals switch (stream.ReadByte()) { case GifConstants.GraphicControlLabel: - this.SkipBlock(stream); // Skip graphic control extension block + SkipBlock(stream); // Skip graphic control extension block break; case GifConstants.CommentLabel: this.ReadComments(stream); @@ -198,7 +198,7 @@ internal sealed class GifDecoderCore : IImageDecoderInternals this.ReadApplicationExtension(stream); break; case GifConstants.PlainTextLabel: - this.SkipBlock(stream); // Not supported by any known decoder. + SkipBlock(stream); // Not supported by any known decoder. break; } } @@ -307,7 +307,7 @@ internal sealed class GifDecoderCore : IImageDecoderInternals { // Reset the stream position and continue. stream.Position = position; - this.SkipBlock(stream, appLength); + SkipBlock(stream, appLength); } return; @@ -327,12 +327,12 @@ internal sealed class GifDecoderCore : IImageDecoderInternals // Could be something else not supported yet. // Skip the subblock and terminator. - this.SkipBlock(stream, subBlockSize); + SkipBlock(stream, subBlockSize); return; } - this.SkipBlock(stream, appLength); // Not supported by any known decoder. + SkipBlock(stream, appLength); // Not supported by any known decoder. } /// @@ -340,7 +340,7 @@ internal sealed class GifDecoderCore : IImageDecoderInternals /// The length of the block to skip. /// The containing image data. /// - private void SkipBlock(BufferedReadStream stream, int blockSize = 0) + private static void SkipBlock(BufferedReadStream stream, int blockSize = 0) { if (blockSize > 0) { @@ -363,7 +363,7 @@ internal sealed class GifDecoderCore : IImageDecoderInternals { int length; - var stringBuilder = new StringBuilder(); + StringBuilder stringBuilder = new StringBuilder(); while ((length = stream.ReadByte()) != 0) { if (length > GifConstants.MaxCommentSubBlockLength) @@ -432,7 +432,7 @@ internal sealed class GifDecoderCore : IImageDecoderInternals this.ReadFrameColors(ref image, ref previousFrame, indices, colorTable, this.imageDescriptor); // Skip any remaining blocks - this.SkipBlock(stream); + SkipBlock(stream); } finally { From f9ac6624b0759977a7daa90a35a42fdfff7b2bfe Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Sun, 5 Feb 2023 19:18:34 +0100 Subject: [PATCH 05/22] remove nullable disable from Format Pbm --- src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs b/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs index c5e0b00718..80db009a44 100644 --- a/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs +++ b/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.Memory; @@ -45,7 +44,7 @@ internal sealed class PbmDecoderCore : IImageDecoderInternals /// /// The decoded by this decoder instance. /// - private ImageMetadata metadata; + private ImageMetadata? metadata; /// /// Initializes a new instance of the class. From 94ebf3627d018cb8bd74a2602ee7c50d9f8aca54 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Sun, 5 Feb 2023 19:43:59 +0100 Subject: [PATCH 06/22] Remove nullable disable from format tga --- src/ImageSharp/Formats/Tga/TgaDecoderCore.cs | 160 +++++++++---------- src/ImageSharp/Formats/Tga/TgaEncoderCore.cs | 3 +- 2 files changed, 76 insertions(+), 87 deletions(-) diff --git a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs index 9c32471fd2..292ffccd96 100644 --- a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs @@ -1,8 +1,8 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Buffers; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.Memory; @@ -29,12 +29,12 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals /// /// The metadata. /// - private ImageMetadata metadata; + private ImageMetadata metadata = null!; /// /// The tga specific metadata. /// - private TgaMetadata tgaMetadata; + private TgaMetadata tgaMetadata = null!; /// /// The file header containing general information about the image. @@ -46,11 +46,6 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals /// private readonly MemoryAllocator memoryAllocator; - /// - /// The stream to decode from. - /// - private BufferedReadStream currentStream; - /// /// Indicates whether there is a alpha channel present. /// @@ -80,7 +75,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals try { TgaImageOrigin origin = this.ReadFileHeader(stream); - this.currentStream.Skip(this.fileHeader.IdLength); + stream.Skip(this.fileHeader.IdLength); // Parse the color map, if present. if (this.fileHeader.ColorMapType is not 0 and not 1) @@ -93,7 +88,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals throw new UnknownImageFormatException("Width or height cannot be 0"); } - var image = Image.CreateUninitialized(this.configuration, this.fileHeader.Width, this.fileHeader.Height, this.metadata); + Image image = Image.CreateUninitialized(this.configuration, this.fileHeader.Width, this.fileHeader.Height, this.metadata); Buffer2D pixels = image.GetRootFramePixelBuffer(); if (this.fileHeader.ColorMapType == 1) @@ -113,7 +108,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals using (IMemoryOwner palette = this.memoryAllocator.Allocate(colorMapSizeInBytes, AllocationOptions.Clean)) { Span paletteSpan = palette.GetSpan(); - int bytesRead = this.currentStream.Read(paletteSpan, this.fileHeader.CMapStart, colorMapSizeInBytes); + int bytesRead = stream.Read(paletteSpan, this.fileHeader.CMapStart, colorMapSizeInBytes); if (bytesRead != colorMapSizeInBytes) { TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read the color map"); @@ -122,6 +117,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals if (this.fileHeader.ImageType == TgaImageType.RleColorMapped) { this.ReadPalettedRle( + stream, this.fileHeader.Width, this.fileHeader.Height, pixels, @@ -132,6 +128,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals else { this.ReadPaletted( + stream, this.fileHeader.Width, this.fileHeader.Height, pixels, @@ -148,7 +145,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals if (this.fileHeader.CMapLength > 0) { int colorMapPixelSizeInBytes = this.fileHeader.CMapDepth / 8; - this.currentStream.Skip(this.fileHeader.CMapLength * colorMapPixelSizeInBytes); + stream.Skip(this.fileHeader.CMapLength * colorMapPixelSizeInBytes); } switch (this.fileHeader.PixelDepth) @@ -156,11 +153,11 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals case 8: if (this.fileHeader.ImageType.IsRunLengthEncoded()) { - this.ReadRle(this.fileHeader.Width, this.fileHeader.Height, pixels, 1, origin); + this.ReadRle(stream, this.fileHeader.Width, this.fileHeader.Height, pixels, 1, origin); } else { - this.ReadMonoChrome(this.fileHeader.Width, this.fileHeader.Height, pixels, origin); + this.ReadMonoChrome(stream, this.fileHeader.Width, this.fileHeader.Height, pixels, origin); } break; @@ -169,11 +166,11 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals case 16: if (this.fileHeader.ImageType.IsRunLengthEncoded()) { - this.ReadRle(this.fileHeader.Width, this.fileHeader.Height, pixels, 2, origin); + this.ReadRle(stream, this.fileHeader.Width, this.fileHeader.Height, pixels, 2, origin); } else { - this.ReadBgra16(this.fileHeader.Width, this.fileHeader.Height, pixels, origin); + this.ReadBgra16(stream, this.fileHeader.Width, this.fileHeader.Height, pixels, origin); } break; @@ -181,11 +178,11 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals case 24: if (this.fileHeader.ImageType.IsRunLengthEncoded()) { - this.ReadRle(this.fileHeader.Width, this.fileHeader.Height, pixels, 3, origin); + this.ReadRle(stream, this.fileHeader.Width, this.fileHeader.Height, pixels, 3, origin); } else { - this.ReadBgr24(this.fileHeader.Width, this.fileHeader.Height, pixels, origin); + this.ReadBgr24(stream, this.fileHeader.Width, this.fileHeader.Height, pixels, origin); } break; @@ -193,11 +190,11 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals case 32: if (this.fileHeader.ImageType.IsRunLengthEncoded()) { - this.ReadRle(this.fileHeader.Width, this.fileHeader.Height, pixels, 4, origin); + this.ReadRle(stream, this.fileHeader.Width, this.fileHeader.Height, pixels, 4, origin); } else { - this.ReadBgra32(this.fileHeader.Width, this.fileHeader.Height, pixels, origin); + this.ReadBgra32(stream, this.fileHeader.Width, this.fileHeader.Height, pixels, origin); } break; @@ -219,13 +216,14 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals /// Reads a uncompressed TGA image with a palette. /// /// The pixel type. + /// The containing image data. /// The width of the image. /// The height of the image. /// The to assign the palette to. /// The color palette. /// Color map size of one entry in bytes. /// The image origin. - private void ReadPaletted(int width, int height, Buffer2D pixels, Span palette, int colorMapPixelSizeInBytes, TgaImageOrigin origin) + private void ReadPaletted(BufferedReadStream stream, int width, int height, Buffer2D pixels, Span palette, int colorMapPixelSizeInBytes, TgaImageOrigin origin) where TPixel : unmanaged, IPixel { TPixel color = default; @@ -243,14 +241,14 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals { for (int x = width - 1; x >= 0; x--) { - this.ReadPalettedBgra16Pixel(palette, colorMapPixelSizeInBytes, x, color, pixelRow); + this.ReadPalettedBgra16Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow); } } else { for (int x = 0; x < width; x++) { - this.ReadPalettedBgra16Pixel(palette, colorMapPixelSizeInBytes, x, color, pixelRow); + this.ReadPalettedBgra16Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow); } } @@ -261,14 +259,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals { for (int x = width - 1; x >= 0; x--) { - this.ReadPalettedBgr24Pixel(palette, colorMapPixelSizeInBytes, x, color, pixelRow); - } - } - else - { - for (int x = 0; x < width; x++) - { - this.ReadPalettedBgr24Pixel(palette, colorMapPixelSizeInBytes, x, color, pixelRow); + this.ReadPalettedBgr24Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow); } } @@ -279,14 +270,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals { for (int x = width - 1; x >= 0; x--) { - this.ReadPalettedBgra32Pixel(palette, colorMapPixelSizeInBytes, x, color, pixelRow); - } - } - else - { - for (int x = 0; x < width; x++) - { - this.ReadPalettedBgra32Pixel(palette, colorMapPixelSizeInBytes, x, color, pixelRow); + this.ReadPalettedBgra32Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow); } } @@ -299,20 +283,21 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals /// Reads a run length encoded TGA image with a palette. /// /// The pixel type. + /// The containing image data. /// The width of the image. /// The height of the image. /// The to assign the palette to. /// The color palette. /// Color map size of one entry in bytes. /// The image origin. - private void ReadPalettedRle(int width, int height, Buffer2D pixels, Span palette, int colorMapPixelSizeInBytes, TgaImageOrigin origin) + private void ReadPalettedRle(BufferedReadStream stream, int width, int height, Buffer2D pixels, Span palette, int colorMapPixelSizeInBytes, TgaImageOrigin origin) where TPixel : unmanaged, IPixel { using (IMemoryOwner buffer = this.memoryAllocator.Allocate(width * height, AllocationOptions.Clean)) { TPixel color = default; Span bufferSpan = buffer.GetSpan(); - this.UncompressRle(width, height, bufferSpan, bytesPerPixel: 1); + this.UncompressRle(stream, width, height, bufferSpan, bytesPerPixel: 1); for (int y = 0; y < height; y++) { @@ -349,11 +334,12 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals /// Reads a uncompressed monochrome TGA image. /// /// The pixel type. + /// The containing image data. /// The width of the image. /// The height of the image. /// The to assign the palette to. /// the image origin. - private void ReadMonoChrome(int width, int height, Buffer2D pixels, TgaImageOrigin origin) + private void ReadMonoChrome(BufferedReadStream stream, int width, int height, Buffer2D pixels, TgaImageOrigin origin) where TPixel : unmanaged, IPixel { bool invertX = InvertX(origin); @@ -366,7 +352,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals Span pixelSpan = pixels.DangerousGetRowSpan(newY); for (int x = width - 1; x >= 0; x--) { - this.ReadL8Pixel(color, x, pixelSpan); + this.ReadL8Pixel(stream, color, x, pixelSpan); } } @@ -380,14 +366,14 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals { for (int y = height - 1; y >= 0; y--) { - this.ReadL8Row(width, pixels, rowSpan, y); + this.ReadL8Row(stream, width, pixels, rowSpan, y); } } else { for (int y = 0; y < height; y++) { - this.ReadL8Row(width, pixels, rowSpan, y); + this.ReadL8Row(stream, width, pixels, rowSpan, y); } } } @@ -396,11 +382,12 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals /// Reads a uncompressed TGA image where each pixels has 16 bit. /// /// The pixel type. + /// The containing image data. /// The width of the image. /// The height of the image. /// The to assign the palette to. /// The image origin. - private void ReadBgra16(int width, int height, Buffer2D pixels, TgaImageOrigin origin) + private void ReadBgra16(BufferedReadStream stream, int width, int height, Buffer2D pixels, TgaImageOrigin origin) where TPixel : unmanaged, IPixel { TPixel color = default; @@ -417,7 +404,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals { for (int x = width - 1; x >= 0; x--) { - int bytesRead = this.currentStream.Read(this.scratchBuffer, 0, 2); + int bytesRead = stream.Read(this.scratchBuffer, 0, 2); if (bytesRead != 2) { TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read a pixel row"); @@ -442,7 +429,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals } else { - int bytesRead = this.currentStream.Read(rowSpan); + int bytesRead = stream.Read(rowSpan); if (bytesRead != rowSpan.Length) { TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read a pixel row"); @@ -473,11 +460,12 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals /// Reads a uncompressed TGA image where each pixels has 24 bit. /// /// The pixel type. + /// The containing image data. /// The width of the image. /// The height of the image. /// The to assign the palette to. /// The image origin. - private void ReadBgr24(int width, int height, Buffer2D pixels, TgaImageOrigin origin) + private void ReadBgr24(BufferedReadStream stream, int width, int height, Buffer2D pixels, TgaImageOrigin origin) where TPixel : unmanaged, IPixel { bool invertX = InvertX(origin); @@ -490,7 +478,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals Span pixelSpan = pixels.DangerousGetRowSpan(newY); for (int x = width - 1; x >= 0; x--) { - this.ReadBgr24Pixel(color, x, pixelSpan); + this.ReadBgr24Pixel(stream, color, x, pixelSpan); } } @@ -505,14 +493,14 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals { for (int y = height - 1; y >= 0; y--) { - this.ReadBgr24Row(width, pixels, rowSpan, y); + this.ReadBgr24Row(stream, width, pixels, rowSpan, y); } } else { for (int y = 0; y < height; y++) { - this.ReadBgr24Row(width, pixels, rowSpan, y); + this.ReadBgr24Row(stream, width, pixels, rowSpan, y); } } } @@ -521,11 +509,12 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals /// Reads a uncompressed TGA image where each pixels has 32 bit. /// /// The pixel type. + /// The containing image data. /// The width of the image. /// The height of the image. /// The to assign the palette to. /// The image origin. - private void ReadBgra32(int width, int height, Buffer2D pixels, TgaImageOrigin origin) + private void ReadBgra32(BufferedReadStream stream, int width, int height, Buffer2D pixels, TgaImageOrigin origin) where TPixel : unmanaged, IPixel { TPixel color = default; @@ -539,14 +528,14 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals { for (int y = height - 1; y >= 0; y--) { - this.ReadBgra32Row(width, pixels, rowSpan, y); + this.ReadBgra32Row(stream, width, pixels, rowSpan, y); } } else { for (int y = 0; y < height; y++) { - this.ReadBgra32Row(width, pixels, rowSpan, y); + this.ReadBgra32Row(stream, width, pixels, rowSpan, y); } } @@ -561,14 +550,14 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals { for (int x = width - 1; x >= 0; x--) { - this.ReadBgra32Pixel(x, color, pixelRow); + this.ReadBgra32Pixel(stream, x, color, pixelRow); } } else { for (int x = 0; x < width; x++) { - this.ReadBgra32Pixel(x, color, pixelRow); + this.ReadBgra32Pixel(stream, x, color, pixelRow); } } } @@ -578,12 +567,13 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals /// Reads a run length encoded TGA image. /// /// The pixel type. + /// The containing image data. /// The width of the image. /// The height of the image. /// The to assign the palette to. /// The bytes per pixel. /// The image origin. - private void ReadRle(int width, int height, Buffer2D pixels, int bytesPerPixel, TgaImageOrigin origin) + private void ReadRle(BufferedReadStream stream, int width, int height, Buffer2D pixels, int bytesPerPixel, TgaImageOrigin origin) where TPixel : unmanaged, IPixel { TPixel color = default; @@ -591,7 +581,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals using (IMemoryOwner buffer = this.memoryAllocator.Allocate(width * height * bytesPerPixel, AllocationOptions.Clean)) { Span bufferSpan = buffer.GetSpan(); - this.UncompressRle(width, height, bufferSpan, bytesPerPixel); + this.UncompressRle(stream, width, height, bufferSpan, bytesPerPixel); for (int y = 0; y < height; y++) { int newY = InvertY(y, height, origin); @@ -658,10 +648,10 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ReadL8Row(int width, Buffer2D pixels, Span row, int y) + private void ReadL8Row(BufferedReadStream stream, int width, Buffer2D pixels, Span row, int y) where TPixel : unmanaged, IPixel { - int bytesRead = this.currentStream.Read(row); + int bytesRead = stream.Read(row); if (bytesRead != row.Length) { TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read a pixel row"); @@ -672,19 +662,19 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ReadL8Pixel(TPixel color, int x, Span pixelSpan) + private void ReadL8Pixel(BufferedReadStream stream, TPixel color, int x, Span pixelSpan) where TPixel : unmanaged, IPixel { - byte pixelValue = (byte)this.currentStream.ReadByte(); + byte pixelValue = (byte)stream.ReadByte(); color.FromL8(Unsafe.As(ref pixelValue)); pixelSpan[x] = color; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ReadBgr24Pixel(TPixel color, int x, Span pixelSpan) + private void ReadBgr24Pixel(BufferedReadStream stream, TPixel color, int x, Span pixelSpan) where TPixel : unmanaged, IPixel { - int bytesRead = this.currentStream.Read(this.scratchBuffer, 0, 3); + int bytesRead = stream.Read(this.scratchBuffer, 0, 3); if (bytesRead != 3) { TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read a bgr pixel"); @@ -695,10 +685,10 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ReadBgr24Row(int width, Buffer2D pixels, Span row, int y) + private void ReadBgr24Row(BufferedReadStream stream, int width, Buffer2D pixels, Span row, int y) where TPixel : unmanaged, IPixel { - int bytesRead = this.currentStream.Read(row); + int bytesRead = stream.Read(row); if (bytesRead != row.Length) { TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read a pixel row"); @@ -709,10 +699,10 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ReadBgra32Pixel(int x, TPixel color, Span pixelRow) + private void ReadBgra32Pixel(BufferedReadStream stream, int x, TPixel color, Span pixelRow) where TPixel : unmanaged, IPixel { - int bytesRead = this.currentStream.Read(this.scratchBuffer, 0, 4); + int bytesRead = stream.Read(this.scratchBuffer, 0, 4); if (bytesRead != 4) { TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read a bgra pixel"); @@ -724,10 +714,10 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ReadBgra32Row(int width, Buffer2D pixels, Span row, int y) + private void ReadBgra32Row(BufferedReadStream stream, int width, Buffer2D pixels, Span row, int y) where TPixel : unmanaged, IPixel { - int bytesRead = this.currentStream.Read(row); + int bytesRead = stream.Read(row); if (bytesRead != row.Length) { TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read a pixel row"); @@ -738,10 +728,10 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ReadPalettedBgra16Pixel(Span palette, int colorMapPixelSizeInBytes, int x, TPixel color, Span pixelRow) + private void ReadPalettedBgra16Pixel(BufferedReadStream stream, Span palette, int colorMapPixelSizeInBytes, int x, TPixel color, Span pixelRow) where TPixel : unmanaged, IPixel { - int colorIndex = this.currentStream.ReadByte(); + int colorIndex = stream.ReadByte(); if (colorIndex == -1) { TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read color index"); @@ -768,10 +758,10 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ReadPalettedBgr24Pixel(Span palette, int colorMapPixelSizeInBytes, int x, TPixel color, Span pixelRow) + private void ReadPalettedBgr24Pixel(BufferedReadStream stream, Span palette, int colorMapPixelSizeInBytes, int x, TPixel color, Span pixelRow) where TPixel : unmanaged, IPixel { - int colorIndex = this.currentStream.ReadByte(); + int colorIndex = stream.ReadByte(); if (colorIndex == -1) { TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read color index"); @@ -782,10 +772,10 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ReadPalettedBgra32Pixel(Span palette, int colorMapPixelSizeInBytes, int x, TPixel color, Span pixelRow) + private void ReadPalettedBgra32Pixel(BufferedReadStream stream, Span palette, int colorMapPixelSizeInBytes, int x, TPixel color, Span pixelRow) where TPixel : unmanaged, IPixel { - int colorIndex = this.currentStream.ReadByte(); + int colorIndex = stream.ReadByte(); if (colorIndex == -1) { TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read color index"); @@ -802,21 +792,21 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals /// The height of the image. /// Buffer for uncompressed data. /// The bytes used per pixel. - private void UncompressRle(int width, int height, Span buffer, int bytesPerPixel) + private void UncompressRle(BufferedReadStream stream, int width, int height, Span buffer, int bytesPerPixel) { int uncompressedPixels = 0; Span pixel = this.scratchBuffer.AsSpan(0, bytesPerPixel); int totalPixels = width * height; while (uncompressedPixels < totalPixels) { - byte runLengthByte = (byte)this.currentStream.ReadByte(); + byte runLengthByte = (byte)stream.ReadByte(); // The high bit of a run length packet is set to 1. int highBit = runLengthByte >> 7; if (highBit == 1) { int runLength = runLengthByte & 127; - int bytesRead = this.currentStream.Read(pixel, 0, bytesPerPixel); + int bytesRead = stream.Read(pixel, 0, bytesPerPixel); if (bytesRead != bytesPerPixel) { TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read a pixel from the stream"); @@ -836,7 +826,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals int bufferIdx = uncompressedPixels * bytesPerPixel; for (int i = 0; i < runLength + 1; i++, uncompressedPixels++) { - int bytesRead = this.currentStream.Read(pixel, 0, bytesPerPixel); + int bytesRead = stream.Read(pixel, 0, bytesPerPixel); if (bytesRead != bytesPerPixel) { TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read a pixel from the stream"); @@ -917,13 +907,13 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals /// /// The containing image data. /// The image origin. + [MemberNotNull(nameof(metadata))] + [MemberNotNull(nameof(tgaMetadata))] private TgaImageOrigin ReadFileHeader(BufferedReadStream stream) { - this.currentStream = stream; - Span buffer = stackalloc byte[TgaFileHeader.Size]; - this.currentStream.Read(buffer, 0, TgaFileHeader.Size); + stream.Read(buffer, 0, TgaFileHeader.Size); this.fileHeader = TgaFileHeader.Parse(buffer); this.metadata = new ImageMetadata(); this.tgaMetadata = this.metadata.GetTgaMetadata(); @@ -939,7 +929,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals this.hasAlpha = alphaBits > 0; // Bits 4 and 5 describe the image origin. - var origin = (TgaImageOrigin)((this.fileHeader.ImageDescriptor & 0x30) >> 4); + TgaImageOrigin origin = (TgaImageOrigin)((this.fileHeader.ImageDescriptor & 0x30) >> 4); return origin; } } diff --git a/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs b/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs index 44799c894b..0b987a3d6c 100644 --- a/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Buffers; using System.Buffers.Binary; @@ -26,7 +25,7 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals /// /// The global configuration. /// - private Configuration configuration; + private Configuration? configuration; /// /// Reusable buffer for writing data. From 7be55a1917d1f8a46836c56c850986973d0183ba Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Sun, 5 Feb 2023 21:53:59 +0100 Subject: [PATCH 07/22] Fix build --- src/ImageSharp/Formats/Tga/TgaDecoderCore.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs index 292ffccd96..7ca5fa9e66 100644 --- a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs @@ -259,7 +259,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals { for (int x = width - 1; x >= 0; x--) { - this.ReadPalettedBgr24Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow); + ReadPalettedBgr24Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow); } } @@ -270,7 +270,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals { for (int x = width - 1; x >= 0; x--) { - this.ReadPalettedBgra32Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow); + ReadPalettedBgra32Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow); } } @@ -352,7 +352,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals Span pixelSpan = pixels.DangerousGetRowSpan(newY); for (int x = width - 1; x >= 0; x--) { - this.ReadL8Pixel(stream, color, x, pixelSpan); + ReadL8Pixel(stream, color, x, pixelSpan); } } @@ -662,7 +662,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ReadL8Pixel(BufferedReadStream stream, TPixel color, int x, Span pixelSpan) + private static void ReadL8Pixel(BufferedReadStream stream, TPixel color, int x, Span pixelSpan) where TPixel : unmanaged, IPixel { byte pixelValue = (byte)stream.ReadByte(); @@ -758,7 +758,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private 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, TPixel color, Span pixelRow) where TPixel : unmanaged, IPixel { int colorIndex = stream.ReadByte(); @@ -772,7 +772,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private 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, TPixel color, Span pixelRow) where TPixel : unmanaged, IPixel { int colorIndex = stream.ReadByte(); @@ -788,6 +788,7 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals /// /// Produce uncompressed tga data from a run length encoded stream. /// + /// The containing image data. /// The width of the image. /// The height of the image. /// Buffer for uncompressed data. From 72cee824a0b3a140944a97048b76c298572896e0 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Sun, 5 Feb 2023 22:41:16 +0100 Subject: [PATCH 08/22] Readd deleted code... --- src/ImageSharp/Formats/Tga/TgaDecoderCore.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs index 7ca5fa9e66..65ab8dfc15 100644 --- a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs @@ -262,6 +262,13 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals ReadPalettedBgr24Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow); } } + else + { + for (int x = 0; x < width; x++) + { + ReadPalettedBgr24Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow); + } + } break; @@ -273,6 +280,13 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals ReadPalettedBgra32Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow); } } + else + { + for (int x = 0; x < width; x++) + { + ReadPalettedBgra32Pixel(stream, palette, colorMapPixelSizeInBytes, x, color, pixelRow); + } + } break; } From 7b46791c41f425a1b2a0c24be6d424b53c3943c1 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Mon, 6 Feb 2023 09:18:44 +0100 Subject: [PATCH 09/22] Remove pragma --- src/ImageSharp/Color/Color.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ImageSharp/Color/Color.cs b/src/ImageSharp/Color/Color.cs index 590a3ed311..13af25f6c7 100644 --- a/src/ImageSharp/Color/Color.cs +++ b/src/ImageSharp/Color/Color.cs @@ -289,9 +289,7 @@ public readonly partial struct Color : IEquatable /// The source color span. /// The destination pixel span. [MethodImpl(InliningOptions.ShortMethod)] -#pragma warning disable RCS1163 // Unused parameter. public static void ToPixel(ReadOnlySpan source, Span destination) -#pragma warning restore RCS1163 // Unused parameter. where TPixel : unmanaged, IPixel { // TODO: Investigate bulk operations utilizing configuration parameter here. From cc4647741ae58a17280ef35d51c5e22c056dd156 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Mon, 6 Feb 2023 10:43:26 +0100 Subject: [PATCH 10/22] Pass configuration as parameter --- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 79 ++++++++++--------- .../Processors/Quantization/IQuantizer.cs | 4 +- .../Quantization/OctreeQuantizer.cs | 4 +- .../Quantization/PaletteQuantizer.cs | 4 +- .../Processors/Quantization/WuQuantizer.cs | 4 +- 5 files changed, 50 insertions(+), 45 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index e1611b059b..eaf6b4df7f 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -68,11 +68,6 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// private readonly MemoryAllocator memoryAllocator; - /// - /// The global configuration. - /// - private Configuration? configuration; - /// /// The color depth, in number of bits per pixel. /// @@ -123,7 +118,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Guard.NotNull(image, nameof(image)); Guard.NotNull(stream, nameof(stream)); - this.configuration = image.GetConfiguration(); + Configuration configuration = image.GetConfiguration(); ImageMetadata metadata = image.Metadata; BmpMetadata bmpMetadata = metadata.GetBmpMetadata(); this.bitsPerPixel ??= bmpMetadata.BitsPerPixel; @@ -164,7 +159,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals WriteBitmapFileHeader(stream, infoHeaderSize, colorPaletteSize, iccProfileSize, infoHeader, buffer); this.WriteBitmapInfoHeader(stream, infoHeader, buffer, infoHeaderSize); - this.WriteImage(stream, image); + this.WriteImage(stream, image, configuration); WriteColorProfile(stream, iccProfileData, buffer); stream.Flush(); @@ -316,38 +311,39 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// /// The containing pixel data. /// - private void WriteImage(Stream stream, Image image) + /// The global configuration. + private void WriteImage(Stream stream, Image image, Configuration configuration) where TPixel : unmanaged, IPixel { Buffer2D pixels = image.Frames.RootFrame.PixelBuffer; switch (this.bitsPerPixel) { case BmpBitsPerPixel.Pixel32: - this.Write32BitPixelData(stream, pixels); + this.Write32BitPixelData(stream, pixels, configuration); break; case BmpBitsPerPixel.Pixel24: - this.Write24BitPixelData(stream, pixels); + this.Write24BitPixelData(stream, pixels, configuration); break; case BmpBitsPerPixel.Pixel16: - this.Write16BitPixelData(stream, pixels); + this.Write16BitPixelData(stream, pixels, configuration); break; case BmpBitsPerPixel.Pixel8: - this.Write8BitPixelData(stream, image); + this.Write8BitPixelData(stream, image, configuration); break; case BmpBitsPerPixel.Pixel4: - this.Write4BitPixelData(stream, image); + this.Write4BitPixelData(stream, image, configuration); break; case BmpBitsPerPixel.Pixel2: - this.Write2BitPixelData(stream, image); + this.Write2BitPixelData(stream, image, configuration); break; case BmpBitsPerPixel.Pixel1: - this.Write1BitPixelData(stream, image); + this.Write1BitPixelData(stream, image, configuration); break; } } @@ -361,7 +357,8 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The pixel format. /// The to write to. /// The containing pixel data. - private void Write32BitPixelData(Stream stream, Buffer2D pixels) + /// The global configuration. + private void Write32BitPixelData(Stream stream, Buffer2D pixels, Configuration configuration) where TPixel : unmanaged, IPixel { using IMemoryOwner row = this.AllocateRow(pixels.Width, 4); @@ -371,7 +368,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals { Span pixelSpan = pixels.DangerousGetRowSpan(y); PixelOperations.Instance.ToBgra32Bytes( - this.configuration, + configuration, pixelSpan, rowSpan, pixelSpan.Length); @@ -385,7 +382,8 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The pixel format. /// The to write to. /// The containing pixel data. - private void Write24BitPixelData(Stream stream, Buffer2D pixels) + /// The global configuration. + private void Write24BitPixelData(Stream stream, Buffer2D pixels, Configuration configuration) where TPixel : unmanaged, IPixel { int width = pixels.Width; @@ -397,7 +395,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals { Span pixelSpan = pixels.DangerousGetRowSpan(y); PixelOperations.Instance.ToBgr24Bytes( - this.configuration, + configuration, pixelSpan, row.Slice(0, rowBytesWithoutPadding), width); @@ -411,7 +409,8 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The type of the pixel. /// The to write to. /// The containing pixel data. - private void Write16BitPixelData(Stream stream, Buffer2D pixels) + /// The global configuration. + private void Write16BitPixelData(Stream stream, Buffer2D pixels, Configuration configuration) where TPixel : unmanaged, IPixel { int width = pixels.Width; @@ -424,7 +423,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Span pixelSpan = pixels.DangerousGetRowSpan(y); PixelOperations.Instance.ToBgra5551Bytes( - this.configuration, + configuration, pixelSpan, row.Slice(0, rowBytesWithoutPadding), pixelSpan.Length); @@ -439,7 +438,8 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The type of the pixel. /// The to write to. /// The containing pixel data. - private void Write8BitPixelData(Stream stream, Image image) + /// The global configuration. + private void Write8BitPixelData(Stream stream, Image image, Configuration configuration) where TPixel : unmanaged, IPixel { bool isL8 = typeof(TPixel) == typeof(L8); @@ -452,7 +452,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals } else { - this.Write8BitColor(stream, image, colorPalette); + this.Write8BitColor(stream, image, colorPalette, configuration); } } @@ -463,16 +463,17 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The to write to. /// The containing pixel data. /// A byte span of size 1024 for the color palette. - private void Write8BitColor(Stream stream, Image image, Span colorPalette) + /// The global configuration + private void Write8BitColor(Stream stream, Image image, Span colorPalette, Configuration configuration) where TPixel : unmanaged, IPixel { - using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(this.configuration); + using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration); frameQuantizer.BuildPalette(this.pixelSamplingStrategy, image); using IndexedImageFrame quantized = frameQuantizer.QuantizeFrame(image.Frames.RootFrame, image.Bounds); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - this.WriteColorPalette(stream, quantizedColorPalette, colorPalette); + this.WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); for (int y = image.Height - 1; y >= 0; y--) { @@ -530,10 +531,11 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The type of the pixel. /// The to write to. /// The containing pixel data. - private void Write4BitPixelData(Stream stream, Image image) + /// The global configuration. + private void Write4BitPixelData(Stream stream, Image image, Configuration configuration) where TPixel : unmanaged, IPixel { - using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(this.configuration, new QuantizerOptions() + using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration, new QuantizerOptions() { MaxColors = 16 }); @@ -545,7 +547,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Span colorPalette = colorPaletteBuffer.GetSpan(); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - this.WriteColorPalette(stream, quantizedColorPalette, colorPalette); + this.WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); ReadOnlySpan pixelRowSpan = quantized.DangerousGetRowSpan(0); int rowPadding = pixelRowSpan.Length % 2 != 0 ? this.padding - 1 : this.padding; @@ -577,10 +579,11 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The type of the pixel. /// The to write to. /// The containing pixel data. - private void Write2BitPixelData(Stream stream, Image image) + /// The global configuration + private void Write2BitPixelData(Stream stream, Image image, Configuration configuration) where TPixel : unmanaged, IPixel { - using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(this.configuration, new QuantizerOptions() + using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration, new QuantizerOptions() { MaxColors = 4 }); @@ -592,7 +595,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Span colorPalette = colorPaletteBuffer.GetSpan(); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - this.WriteColorPalette(stream, quantizedColorPalette, colorPalette); + this.WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); ReadOnlySpan pixelRowSpan = quantized.DangerousGetRowSpan(0); int rowPadding = pixelRowSpan.Length % 4 != 0 ? this.padding - 1 : this.padding; @@ -633,10 +636,11 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The type of the pixel. /// The to write to. /// The containing pixel data. - private void Write1BitPixelData(Stream stream, Image image) + /// The global configuration + private void Write1BitPixelData(Stream stream, Image image, Configuration configuration) where TPixel : unmanaged, IPixel { - using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(this.configuration, new QuantizerOptions() + using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration, new QuantizerOptions() { MaxColors = 2 }); @@ -648,7 +652,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Span colorPalette = colorPaletteBuffer.GetSpan(); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - this.WriteColorPalette(stream, quantizedColorPalette, colorPalette); + this.WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); ReadOnlySpan quantizedPixelRow = quantized.DangerousGetRowSpan(0); int rowPadding = quantizedPixelRow.Length % 8 != 0 ? this.padding - 1 : this.padding; @@ -683,11 +687,12 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The to write to. /// The color palette from the quantized image. /// A temporary byte span to write the color palette to. - private void WriteColorPalette(Stream stream, ReadOnlySpan quantizedColorPalette, Span colorPalette) + /// The global configuration + private void WriteColorPalette(Stream stream, ReadOnlySpan quantizedColorPalette, Span colorPalette, Configuration configuration) where TPixel : unmanaged, IPixel { int quantizedColorBytes = quantizedColorPalette.Length * 4; - PixelOperations.Instance.ToBgra32(this.configuration, quantizedColorPalette, MemoryMarshal.Cast(colorPalette[..quantizedColorBytes])); + PixelOperations.Instance.ToBgra32(configuration, quantizedColorPalette, MemoryMarshal.Cast(colorPalette[..quantizedColorBytes])); Span colorPaletteAsUInt = MemoryMarshal.Cast(colorPalette); for (int i = 0; i < colorPaletteAsUInt.Length; i++) { diff --git a/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs index f46cea2e0b..9d5b606040 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs @@ -21,7 +21,7 @@ public interface IQuantizer /// The to configure internal operations. /// The pixel format. /// The . - IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration) + IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) where TPixel : unmanaged, IPixel; /// @@ -31,6 +31,6 @@ public interface IQuantizer /// The to configure internal operations. /// The options to create the quantizer with. /// The . - IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration, QuantizerOptions options) + IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel; } diff --git a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs index 0d2450e221..0a1032bf0d 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs @@ -34,12 +34,12 @@ public class OctreeQuantizer : IQuantizer public QuantizerOptions Options { get; } /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration) + public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) where TPixel : unmanaged, IPixel => this.CreatePixelSpecificQuantizer(configuration, this.Options); /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration, QuantizerOptions options) + public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel => new OctreeQuantizer(configuration, options); } diff --git a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs index c5617fe862..fe4af9005a 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs @@ -39,12 +39,12 @@ public class PaletteQuantizer : IQuantizer public QuantizerOptions Options { get; } /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration) + public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) where TPixel : unmanaged, IPixel => this.CreatePixelSpecificQuantizer(configuration, this.Options); /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration, QuantizerOptions options) + public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel { Guard.NotNull(options, nameof(options)); diff --git a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs index dff9155401..86d798d965 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs @@ -33,12 +33,12 @@ public class WuQuantizer : IQuantizer public QuantizerOptions Options { get; } /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration) + public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) where TPixel : unmanaged, IPixel => this.CreatePixelSpecificQuantizer(configuration, this.Options); /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration, QuantizerOptions options) + public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel => new WuQuantizer(configuration, options); } From 4a82d27303519c1da92f6432fcaef7bd8ef96603 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Mon, 6 Feb 2023 10:50:12 +0100 Subject: [PATCH 11/22] Check iccProfileData for null not the profile --- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index eaf6b4df7f..1479d31e64 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -228,9 +228,9 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals infoHeader.Compression = BmpCompression.BitFields; } - if (this.infoHeaderType is BmpInfoHeaderType.WinVersion5 && metadata.IccProfile != null) + if (this.infoHeaderType is BmpInfoHeaderType.WinVersion5 && iccProfileData != null) { - infoHeader.ProfileSize = iccProfileData!.Length; + infoHeader.ProfileSize = iccProfileData.Length; infoHeader.CsType = BmpColorSpace.PROFILE_EMBEDDED; infoHeader.Intent = BmpRenderingIntent.LCS_GM_IMAGES; } From 1c773661b4c19881cf844ebf19cf2ab3c93c1b1e Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Mon, 6 Feb 2023 10:54:56 +0100 Subject: [PATCH 12/22] Make method static --- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index 1479d31e64..620e0499f4 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -473,7 +473,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals using IndexedImageFrame quantized = frameQuantizer.QuantizeFrame(image.Frames.RootFrame, image.Bounds); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - this.WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); + WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); for (int y = image.Height - 1; y >= 0; y--) { @@ -547,7 +547,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Span colorPalette = colorPaletteBuffer.GetSpan(); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - this.WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); + WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); ReadOnlySpan pixelRowSpan = quantized.DangerousGetRowSpan(0); int rowPadding = pixelRowSpan.Length % 2 != 0 ? this.padding - 1 : this.padding; @@ -595,7 +595,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Span colorPalette = colorPaletteBuffer.GetSpan(); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - this.WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); + WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); ReadOnlySpan pixelRowSpan = quantized.DangerousGetRowSpan(0); int rowPadding = pixelRowSpan.Length % 4 != 0 ? this.padding - 1 : this.padding; @@ -652,7 +652,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Span colorPalette = colorPaletteBuffer.GetSpan(); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - this.WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); + WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); ReadOnlySpan quantizedPixelRow = quantized.DangerousGetRowSpan(0); int rowPadding = quantizedPixelRow.Length % 8 != 0 ? this.padding - 1 : this.padding; @@ -688,7 +688,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The color palette from the quantized image. /// A temporary byte span to write the color palette to. /// The global configuration - private void WriteColorPalette(Stream stream, ReadOnlySpan quantizedColorPalette, Span colorPalette, Configuration configuration) + private static void WriteColorPalette(Stream stream, ReadOnlySpan quantizedColorPalette, Span colorPalette, Configuration configuration) where TPixel : unmanaged, IPixel { int quantizedColorBytes = quantizedColorPalette.Length * 4; From 66d494e2505454320ab6052966eb91366f8ca09d Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 7 Feb 2023 09:17:16 +1000 Subject: [PATCH 13/22] Reorder params, fix other warnings. --- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 68 +++++++++---------- .../Quantization/OctreeQuantizer{TPixel}.cs | 7 +- .../Quantization/PaletteQuantizer{TPixel}.cs | 11 ++- .../Quantization/WuQuantizer{TPixel}.cs | 8 +-- 4 files changed, 44 insertions(+), 50 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index 620e0499f4..f30369d19b 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -159,7 +159,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals WriteBitmapFileHeader(stream, infoHeaderSize, colorPaletteSize, iccProfileSize, infoHeader, buffer); this.WriteBitmapInfoHeader(stream, infoHeader, buffer, infoHeaderSize); - this.WriteImage(stream, image, configuration); + this.WriteImage(configuration, stream, image); WriteColorProfile(stream, iccProfileData, buffer); stream.Flush(); @@ -307,43 +307,43 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// Writes the pixel data to the binary stream. /// /// The pixel format. + /// The global configuration. /// The to write to. /// /// The containing pixel data. /// - /// The global configuration. - private void WriteImage(Stream stream, Image image, Configuration configuration) + private void WriteImage(Configuration configuration, Stream stream, Image image) where TPixel : unmanaged, IPixel { Buffer2D pixels = image.Frames.RootFrame.PixelBuffer; switch (this.bitsPerPixel) { case BmpBitsPerPixel.Pixel32: - this.Write32BitPixelData(stream, pixels, configuration); + this.Write32BitPixelData(configuration, stream, pixels); break; case BmpBitsPerPixel.Pixel24: - this.Write24BitPixelData(stream, pixels, configuration); + this.Write24BitPixelData(configuration, stream, pixels); break; case BmpBitsPerPixel.Pixel16: - this.Write16BitPixelData(stream, pixels, configuration); + this.Write16BitPixelData(configuration, stream, pixels); break; case BmpBitsPerPixel.Pixel8: - this.Write8BitPixelData(stream, image, configuration); + this.Write8BitPixelData(configuration, stream, image); break; case BmpBitsPerPixel.Pixel4: - this.Write4BitPixelData(stream, image, configuration); + this.Write4BitPixelData(configuration, stream, image); break; case BmpBitsPerPixel.Pixel2: - this.Write2BitPixelData(stream, image, configuration); + this.Write2BitPixelData(configuration, stream, image); break; case BmpBitsPerPixel.Pixel1: - this.Write1BitPixelData(stream, image, configuration); + this.Write1BitPixelData(configuration, stream, image); break; } } @@ -355,10 +355,10 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// Writes 32-bit data with a color palette to the stream. /// /// The pixel format. + /// The global configuration. /// The to write to. /// The containing pixel data. - /// The global configuration. - private void Write32BitPixelData(Stream stream, Buffer2D pixels, Configuration configuration) + private void Write32BitPixelData(Configuration configuration, Stream stream, Buffer2D pixels) where TPixel : unmanaged, IPixel { using IMemoryOwner row = this.AllocateRow(pixels.Width, 4); @@ -380,10 +380,10 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// Writes 24-bit pixel data with a color palette to the stream. /// /// The pixel format. + /// The global configuration. /// The to write to. /// The containing pixel data. - /// The global configuration. - private void Write24BitPixelData(Stream stream, Buffer2D pixels, Configuration configuration) + private void Write24BitPixelData(Configuration configuration, Stream stream, Buffer2D pixels) where TPixel : unmanaged, IPixel { int width = pixels.Width; @@ -407,11 +407,11 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// Writes 16-bit pixel data with a color palette to the stream. /// /// The type of the pixel. + /// The global configuration. /// The to write to. /// The containing pixel data. - /// The global configuration. - private void Write16BitPixelData(Stream stream, Buffer2D pixels, Configuration configuration) - where TPixel : unmanaged, IPixel + private void Write16BitPixelData(Configuration configuration, Stream stream, Buffer2D pixels) + where TPixel : unmanaged, IPixel { int width = pixels.Width; int rowBytesWithoutPadding = width * 2; @@ -436,10 +436,10 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// Writes 8 bit pixel data with a color palette. The color palette has 256 entry's with 4 bytes for each entry. /// /// The type of the pixel. + /// The global configuration. /// The to write to. /// The containing pixel data. - /// The global configuration. - private void Write8BitPixelData(Stream stream, Image image, Configuration configuration) + private void Write8BitPixelData(Configuration configuration, Stream stream, Image image) where TPixel : unmanaged, IPixel { bool isL8 = typeof(TPixel) == typeof(L8); @@ -452,7 +452,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals } else { - this.Write8BitColor(stream, image, colorPalette, configuration); + this.Write8BitColor(configuration, stream, image, colorPalette); } } @@ -460,11 +460,11 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// Writes an 8 bit color image with a color palette. The color palette has 256 entry's with 4 bytes for each entry. /// /// The type of the pixel. + /// The global configuration. /// The to write to. /// The containing pixel data. /// A byte span of size 1024 for the color palette. - /// The global configuration - private void Write8BitColor(Stream stream, Image image, Span colorPalette, Configuration configuration) + private void Write8BitColor(Configuration configuration, Stream stream, Image image, Span colorPalette) where TPixel : unmanaged, IPixel { using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration); @@ -473,7 +473,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals using IndexedImageFrame quantized = frameQuantizer.QuantizeFrame(image.Frames.RootFrame, image.Bounds); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); + WriteColorPalette(configuration, stream, quantizedColorPalette, colorPalette); for (int y = image.Height - 1; y >= 0; y--) { @@ -529,10 +529,10 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// Writes 4 bit pixel data with a color palette. The color palette has 16 entry's with 4 bytes for each entry. /// /// The type of the pixel. + /// The global configuration. /// The to write to. /// The containing pixel data. - /// The global configuration. - private void Write4BitPixelData(Stream stream, Image image, Configuration configuration) + private void Write4BitPixelData(Configuration configuration, Stream stream, Image image) where TPixel : unmanaged, IPixel { using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration, new QuantizerOptions() @@ -547,7 +547,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Span colorPalette = colorPaletteBuffer.GetSpan(); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); + WriteColorPalette(configuration, stream, quantizedColorPalette, colorPalette); ReadOnlySpan pixelRowSpan = quantized.DangerousGetRowSpan(0); int rowPadding = pixelRowSpan.Length % 2 != 0 ? this.padding - 1 : this.padding; @@ -577,10 +577,10 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// Writes 2 bit pixel data with a color palette. The color palette has 4 entry's with 4 bytes for each entry. /// /// The type of the pixel. + /// The global configuration. /// The to write to. /// The containing pixel data. - /// The global configuration - private void Write2BitPixelData(Stream stream, Image image, Configuration configuration) + private void Write2BitPixelData(Configuration configuration, Stream stream, Image image) where TPixel : unmanaged, IPixel { using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration, new QuantizerOptions() @@ -595,7 +595,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Span colorPalette = colorPaletteBuffer.GetSpan(); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); + WriteColorPalette(configuration, stream, quantizedColorPalette, colorPalette); ReadOnlySpan pixelRowSpan = quantized.DangerousGetRowSpan(0); int rowPadding = pixelRowSpan.Length % 4 != 0 ? this.padding - 1 : this.padding; @@ -634,10 +634,10 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// Writes 1 bit pixel data with a color palette. The color palette has 2 entry's with 4 bytes for each entry. /// /// The type of the pixel. + /// The global configuration. /// The to write to. /// The containing pixel data. - /// The global configuration - private void Write1BitPixelData(Stream stream, Image image, Configuration configuration) + private void Write1BitPixelData(Configuration configuration, Stream stream, Image image) where TPixel : unmanaged, IPixel { using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration, new QuantizerOptions() @@ -652,7 +652,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Span colorPalette = colorPaletteBuffer.GetSpan(); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); + WriteColorPalette(configuration, stream, quantizedColorPalette, colorPalette); ReadOnlySpan quantizedPixelRow = quantized.DangerousGetRowSpan(0); int rowPadding = quantizedPixelRow.Length % 8 != 0 ? this.padding - 1 : this.padding; @@ -684,11 +684,11 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// Writes the color palette to the stream. The color palette has 4 bytes for each entry. /// /// The type of the pixel. + /// The global configuration. /// The to write to. /// The color palette from the quantized image. /// A temporary byte span to write the color palette to. - /// The global configuration - private static void WriteColorPalette(Stream stream, ReadOnlySpan quantizedColorPalette, Span colorPalette, Configuration configuration) + private static void WriteColorPalette(Configuration configuration, Stream stream, ReadOnlySpan quantizedColorPalette, Span colorPalette) where TPixel : unmanaged, IPixel { int quantizedColorBytes = quantizedColorPalette.Length * 4; diff --git a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs index 37ebc39bac..1136fbc9da 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs @@ -26,7 +26,7 @@ public struct OctreeQuantizer : IQuantizer private readonly int maxColors; private readonly int bitDepth; private readonly Octree octree; - private IMemoryOwner paletteOwner; + private readonly IMemoryOwner paletteOwner; private ReadOnlyMemory palette; private EuclideanPixelMap? pixelMap; private readonly bool isDithering; @@ -38,11 +38,8 @@ public struct OctreeQuantizer : IQuantizer /// The configuration which allows altering default behaviour or extending the library. /// The quantizer options defining quantization rules. [MethodImpl(InliningOptions.ShortMethod)] - public OctreeQuantizer(Configuration? configuration, QuantizerOptions options) + public OctreeQuantizer(Configuration configuration, QuantizerOptions options) { - Guard.NotNull(configuration, nameof(configuration)); - Guard.NotNull(options, nameof(options)); - this.Configuration = configuration; this.Options = options; diff --git a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs index 182bed2382..86db9f6f01 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs @@ -17,10 +17,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization; "Design", "CA1001:Types that own disposable fields should be disposable", Justification = "https://github.com/dotnet/roslyn-analyzers/issues/6151")] -internal struct PaletteQuantizer : IQuantizer +internal readonly struct PaletteQuantizer : IQuantizer where TPixel : unmanaged, IPixel { - private EuclideanPixelMap pixelMap; + private readonly EuclideanPixelMap pixelMap; /// /// Initializes a new instance of the struct. @@ -29,7 +29,7 @@ internal struct PaletteQuantizer : IQuantizer /// The quantizer options defining quantization rules. /// The palette to use. [MethodImpl(InliningOptions.ShortMethod)] - public PaletteQuantizer(Configuration? configuration, QuantizerOptions options, ReadOnlyMemory palette) + public PaletteQuantizer(Configuration configuration, QuantizerOptions options, ReadOnlyMemory palette) { Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(options, nameof(options)); @@ -65,8 +65,5 @@ internal struct PaletteQuantizer : IQuantizer => (byte)this.pixelMap.GetClosestColor(color, out match); /// - public void Dispose() - { - this.pixelMap.Dispose(); - } + public void Dispose() => this.pixelMap.Dispose(); } diff --git a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs index c04dbe1918..edf293d39e 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs @@ -69,9 +69,9 @@ internal struct WuQuantizer : IQuantizer /// private const int TableLength = IndexCount * IndexCount * IndexCount * IndexAlphaCount; - private IMemoryOwner momentsOwner; - private IMemoryOwner tagsOwner; - private IMemoryOwner paletteOwner; + private readonly IMemoryOwner momentsOwner; + private readonly IMemoryOwner tagsOwner; + private readonly IMemoryOwner paletteOwner; private ReadOnlyMemory palette; private int maxColors; private readonly Box[] colorCube; @@ -85,7 +85,7 @@ internal struct WuQuantizer : IQuantizer /// The configuration which allows altering default behaviour or extending the library. /// The quantizer options defining quantization rules. [MethodImpl(InliningOptions.ShortMethod)] - public WuQuantizer(Configuration? configuration, QuantizerOptions options) + public WuQuantizer(Configuration configuration, QuantizerOptions options) { Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(options, nameof(options)); From c6e583991c1916b7f39adf45da20a1510cd31e35 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 7 Feb 2023 21:56:08 +1000 Subject: [PATCH 14/22] Minor cleanup --- src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 8 ++++---- src/ImageSharp/Formats/Gif/GifEncoderCore.cs | 4 +--- src/ImageSharp/Image.cs | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index 6c82d61e1d..3ebd65a67f 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -337,9 +337,9 @@ internal sealed class GifDecoderCore : IImageDecoderInternals /// /// Skips over a block or reads its terminator. - /// The length of the block to skip. - /// The containing image data. /// + /// The containing image data. + /// The length of the block to skip. private static void SkipBlock(BufferedReadStream stream, int blockSize = 0) { if (blockSize > 0) @@ -363,7 +363,7 @@ internal sealed class GifDecoderCore : IImageDecoderInternals { int length; - StringBuilder stringBuilder = new StringBuilder(); + StringBuilder stringBuilder = new(); while ((length = stream.ReadByte()) != 0) { if (length > GifConstants.MaxCommentSubBlockLength) @@ -450,7 +450,7 @@ internal sealed class GifDecoderCore : IImageDecoderInternals private void ReadFrameIndices(BufferedReadStream stream, Buffer2D indices) { int minCodeSize = stream.ReadByte(); - using LzwDecoder lzwDecoder = new LzwDecoder(this.configuration.MemoryAllocator, stream); + using LzwDecoder lzwDecoder = new(this.configuration.MemoryAllocator, stream); lzwDecoder.DecodePixels(minCodeSize, indices); } diff --git a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs index 0e59a0f05e..75c9b11860 100644 --- a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs @@ -2,7 +2,6 @@ // Licensed under the Six Labors Split License. using System.Buffers; -using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using SixLabors.ImageSharp.Advanced; @@ -141,7 +140,7 @@ internal sealed class GifEncoderCore : IImageEncoderInternals private void EncodeFrames( Stream stream, Image image, - IndexedImageFrame? quantized, + IndexedImageFrame quantized, ReadOnlyMemory palette) where TPixel : unmanaged, IPixel { @@ -168,7 +167,6 @@ internal sealed class GifEncoderCore : IImageEncoderInternals // Clean up for the next run. quantized.Dispose(); - quantized = null; } if (hasPaletteQuantizer) diff --git a/src/ImageSharp/Image.cs b/src/ImageSharp/Image.cs index af1b1916f9..02fa014781 100644 --- a/src/ImageSharp/Image.cs +++ b/src/ImageSharp/Image.cs @@ -30,7 +30,7 @@ public abstract partial class Image : ImageInfo, IDisposable, IConfigurationProv /// The size in px units. protected Image(Configuration configuration, PixelTypeInfo pixelType, ImageMetadata? metadata, Size size) : base(pixelType, size, metadata) - => this.configuration = configuration ?? Configuration.Default; + => this.configuration = configuration; /// /// Initializes a new instance of the class. From b560918106aa378b633ec0746e7dc1814ce9ac88 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Wed, 8 Feb 2023 18:10:11 +0100 Subject: [PATCH 15/22] do not use null!. --- src/ImageSharp/Formats/Tga/TgaDecoderCore.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs index 65ab8dfc15..f04ba25ff3 100644 --- a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs @@ -29,12 +29,12 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals /// /// The metadata. /// - private ImageMetadata metadata = null!; + private ImageMetadata? metadata; /// /// The tga specific metadata. /// - private TgaMetadata tgaMetadata = null!; + private TgaMetadata? tgaMetadata; /// /// The file header containing general information about the image. @@ -533,6 +533,9 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals { TPixel color = default; bool invertX = InvertX(origin); + + Guard.NotNull(this.tgaMetadata); + if (this.tgaMetadata.AlphaChannelBits == 8 && !invertX) { using IMemoryOwner row = this.memoryAllocator.AllocatePaddedPixelRowBuffer(width, 4, 0); @@ -591,6 +594,9 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals where TPixel : unmanaged, IPixel { TPixel color = default; + + Guard.NotNull(this.tgaMetadata); + byte alphaBits = this.tgaMetadata.AlphaChannelBits; using (IMemoryOwner buffer = this.memoryAllocator.Allocate(width * height * bytesPerPixel, AllocationOptions.Clean)) { @@ -722,6 +728,8 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals TgaThrowHelper.ThrowInvalidImageContentException("Not enough data to read a bgra pixel"); } + Guard.NotNull(this.tgaMetadata); + byte alpha = this.tgaMetadata.AlphaChannelBits == 0 ? byte.MaxValue : this.scratchBuffer[3]; color.FromBgra32(new Bgra32(this.scratchBuffer[2], this.scratchBuffer[1], this.scratchBuffer[0], alpha)); pixelRow[x] = color; From b79e61fbd9306d422c5611f5ac2c29cb9ec7efc5 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Wed, 8 Feb 2023 20:08:43 +0100 Subject: [PATCH 16/22] Remove nullable disable from ChunkedMemoryStream --- src/ImageSharp/IO/ChunkedMemoryStream.cs | 39 +++++++++++++----------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/ImageSharp/IO/ChunkedMemoryStream.cs b/src/ImageSharp/IO/ChunkedMemoryStream.cs index 5f388d24f1..2e088397a0 100644 --- a/src/ImageSharp/IO/ChunkedMemoryStream.cs +++ b/src/ImageSharp/IO/ChunkedMemoryStream.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Buffers; using System.Runtime.CompilerServices; @@ -19,7 +18,7 @@ internal sealed class ChunkedMemoryStream : Stream private readonly MemoryAllocator allocator; // Data - private MemoryChunk memoryChunk; + private MemoryChunk? memoryChunk; // The total number of allocated chunks private int chunkCount; @@ -31,13 +30,13 @@ internal sealed class ChunkedMemoryStream : Stream private bool isDisposed; // Current chunk to write to - private MemoryChunk writeChunk; + private MemoryChunk? writeChunk; // Offset into chunk to write to private int writeOffset; // Current chunk to read from - private MemoryChunk readChunk; + private MemoryChunk? readChunk; // Offset into chunk to read from private int readOffset; @@ -71,10 +70,10 @@ internal sealed class ChunkedMemoryStream : Stream this.EnsureNotDisposed(); int length = 0; - MemoryChunk chunk = this.memoryChunk; + MemoryChunk? chunk = this.memoryChunk; while (chunk != null) { - MemoryChunk next = chunk.Next; + MemoryChunk? next = chunk.Next; if (next != null) { length += chunk.Length; @@ -104,8 +103,8 @@ internal sealed class ChunkedMemoryStream : Stream } int pos = 0; - MemoryChunk chunk = this.memoryChunk; - while (chunk != this.readChunk) + MemoryChunk? chunk = this.memoryChunk; + while (chunk != this.readChunk && chunk is not null) { pos += chunk.Length; chunk = chunk.Next; @@ -126,14 +125,14 @@ internal sealed class ChunkedMemoryStream : Stream } // Back up current position in case new position is out of range - MemoryChunk backupReadChunk = this.readChunk; + MemoryChunk? backupReadChunk = this.readChunk; int backupReadOffset = this.readOffset; this.readChunk = null; this.readOffset = 0; int leftUntilAtPos = (int)value; - MemoryChunk chunk = this.memoryChunk; + MemoryChunk? chunk = this.memoryChunk; while (chunk != null) { if ((leftUntilAtPos < chunk.Length) @@ -365,6 +364,8 @@ internal sealed class ChunkedMemoryStream : Stream this.writeOffset = 0; } + Guard.NotNull(this.writeChunk); + Span chunkBuffer = this.writeChunk.Buffer.GetSpan(); int chunkSize = this.writeChunk.Length; int count = buffer.Length; @@ -402,6 +403,8 @@ internal sealed class ChunkedMemoryStream : Stream this.writeOffset = 0; } + Guard.NotNull(this.writeChunk); + IMemoryOwner chunkBuffer = this.writeChunk.Buffer; int chunkSize = this.writeChunk.Length; @@ -426,7 +429,7 @@ internal sealed class ChunkedMemoryStream : Stream int length = (int)this.Length; // This will throw if stream is closed byte[] copy = new byte[this.Length]; - MemoryChunk backupReadChunk = this.readChunk; + MemoryChunk? backupReadChunk = this.readChunk; int backupReadOffset = this.readOffset; this.readChunk = this.memoryChunk; @@ -522,15 +525,14 @@ internal sealed class ChunkedMemoryStream : Stream // available contiguous buffer size. IMemoryOwner buffer = this.allocator.Allocate(Math.Min(this.allocatorCapacity, GetChunkSize(this.chunkCount++))); - return new MemoryChunk + return new MemoryChunk(buffer) { - Buffer = buffer, Next = null, Length = buffer.Length() }; } - private static void ReleaseMemoryChunks(MemoryChunk chunk) + private static void ReleaseMemoryChunks(MemoryChunk? chunk) { while (chunk != null) { @@ -555,11 +557,13 @@ internal sealed class ChunkedMemoryStream : Stream { private bool isDisposed; - public IMemoryOwner Buffer { get; set; } + public MemoryChunk(IMemoryOwner buffer) => this.Buffer = buffer; + + public IMemoryOwner Buffer { get; } - public MemoryChunk Next { get; set; } + public MemoryChunk? Next { get; set; } - public int Length { get; set; } + public int Length { get; init; } private void Dispose(bool disposing) { @@ -570,7 +574,6 @@ internal sealed class ChunkedMemoryStream : Stream this.Buffer.Dispose(); } - this.Buffer = null; this.isDisposed = true; } } From c8fd4a321dec6257f2fe8bf5881c62bfe30251e3 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 9 Feb 2023 20:30:08 +1000 Subject: [PATCH 17/22] Match other format configuration handling --- src/ImageSharp/Formats/Tga/TgaEncoderCore.cs | 41 ++++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs b/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs index 0b987a3d6c..f468ab9ae7 100644 --- a/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaEncoderCore.cs @@ -22,11 +22,6 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals /// private readonly MemoryAllocator memoryAllocator; - /// - /// The global configuration. - /// - private Configuration? configuration; - /// /// Reusable buffer for writing data. /// @@ -67,7 +62,6 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals Guard.NotNull(image, nameof(image)); Guard.NotNull(stream, nameof(stream)); - this.configuration = image.GetConfiguration(); ImageMetadata metadata = image.Metadata; TgaMetadata tgaMetadata = metadata.GetTgaMetadata(); this.bitsPerPixel ??= tgaMetadata.BitsPerPixel; @@ -123,7 +117,7 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals } else { - this.WriteImage(stream, image.Frames.RootFrame); + this.WriteImage(image.GetConfiguration(), stream, image.Frames.RootFrame); } stream.Flush(); @@ -133,30 +127,31 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals /// Writes the pixel data to the binary stream. /// /// The pixel format. + /// The global configuration. /// The to write to. /// /// The containing pixel data. /// - private void WriteImage(Stream stream, ImageFrame image) + private void WriteImage(Configuration configuration, Stream stream, ImageFrame image) where TPixel : unmanaged, IPixel { Buffer2D pixels = image.PixelBuffer; switch (this.bitsPerPixel) { case TgaBitsPerPixel.Pixel8: - this.Write8Bit(stream, pixels); + this.Write8Bit(configuration, stream, pixels); break; case TgaBitsPerPixel.Pixel16: - this.Write16Bit(stream, pixels); + this.Write16Bit(configuration, stream, pixels); break; case TgaBitsPerPixel.Pixel24: - this.Write24Bit(stream, pixels); + this.Write24Bit(configuration, stream, pixels); break; case TgaBitsPerPixel.Pixel32: - this.Write32Bit(stream, pixels); + this.Write32Bit(configuration, stream, pixels); break; } } @@ -226,7 +221,7 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals case TgaBitsPerPixel.Pixel16: Bgra5551 bgra5551 = new(color.ToVector4()); - BinaryPrimitives.TryWriteInt16LittleEndian(this.buffer, (short)bgra5551.PackedValue); + BinaryPrimitives.WriteInt16LittleEndian(this.buffer, (short)bgra5551.PackedValue); stream.WriteByte(this.buffer[0]); stream.WriteByte(this.buffer[1]); @@ -320,9 +315,10 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals /// Writes the 8bit pixels uncompressed to the stream. /// /// The pixel format. + /// The global configuration. /// The to write to. /// The containing pixel data. - private void Write8Bit(Stream stream, Buffer2D pixels) + private void Write8Bit(Configuration configuration, Stream stream, Buffer2D pixels) where TPixel : unmanaged, IPixel { using IMemoryOwner row = this.AllocateRow(pixels.Width, 1); @@ -332,7 +328,7 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals { Span pixelSpan = pixels.DangerousGetRowSpan(y); PixelOperations.Instance.ToL8Bytes( - this.configuration, + configuration, pixelSpan, rowSpan, pixelSpan.Length); @@ -344,9 +340,10 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals /// Writes the 16bit pixels uncompressed to the stream. /// /// The pixel format. + /// The global configuration. /// The to write to. /// The containing pixel data. - private void Write16Bit(Stream stream, Buffer2D pixels) + private void Write16Bit(Configuration configuration, Stream stream, Buffer2D pixels) where TPixel : unmanaged, IPixel { using IMemoryOwner row = this.AllocateRow(pixels.Width, 2); @@ -356,7 +353,7 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals { Span pixelSpan = pixels.DangerousGetRowSpan(y); PixelOperations.Instance.ToBgra5551Bytes( - this.configuration, + configuration, pixelSpan, rowSpan, pixelSpan.Length); @@ -368,9 +365,10 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals /// Writes the 24bit pixels uncompressed to the stream. /// /// The pixel format. + /// The global configuration. /// The to write to. /// The containing pixel data. - private void Write24Bit(Stream stream, Buffer2D pixels) + private void Write24Bit(Configuration configuration, Stream stream, Buffer2D pixels) where TPixel : unmanaged, IPixel { using IMemoryOwner row = this.AllocateRow(pixels.Width, 3); @@ -380,7 +378,7 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals { Span pixelSpan = pixels.DangerousGetRowSpan(y); PixelOperations.Instance.ToBgr24Bytes( - this.configuration, + configuration, pixelSpan, rowSpan, pixelSpan.Length); @@ -392,9 +390,10 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals /// Writes the 32bit pixels uncompressed to the stream. /// /// The pixel format. + /// The global configuration. /// The to write to. /// The containing pixel data. - private void Write32Bit(Stream stream, Buffer2D pixels) + private void Write32Bit(Configuration configuration, Stream stream, Buffer2D pixels) where TPixel : unmanaged, IPixel { using IMemoryOwner row = this.AllocateRow(pixels.Width, 4); @@ -404,7 +403,7 @@ internal sealed class TgaEncoderCore : IImageEncoderInternals { Span pixelSpan = pixels.DangerousGetRowSpan(y); PixelOperations.Instance.ToBgra32Bytes( - this.configuration, + configuration, pixelSpan, rowSpan, pixelSpan.Length); From 829be7ac1bd4c3ff6143c8a5c831f21cc9531056 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 9 Feb 2023 20:31:44 +1000 Subject: [PATCH 18/22] Style cleanup --- src/ImageSharp/Formats/Tga/TgaDecoderCore.cs | 155 +++++++++---------- 1 file changed, 75 insertions(+), 80 deletions(-) diff --git a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs index f04ba25ff3..2428763432 100644 --- a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs @@ -307,39 +307,37 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals private void ReadPalettedRle(BufferedReadStream stream, int width, int height, Buffer2D pixels, Span palette, int colorMapPixelSizeInBytes, TgaImageOrigin origin) where TPixel : unmanaged, IPixel { - using (IMemoryOwner buffer = this.memoryAllocator.Allocate(width * height, AllocationOptions.Clean)) - { - TPixel color = default; - Span bufferSpan = buffer.GetSpan(); - this.UncompressRle(stream, width, height, bufferSpan, bytesPerPixel: 1); + using IMemoryOwner buffer = this.memoryAllocator.Allocate(width * height, AllocationOptions.Clean); + TPixel color = default; + Span bufferSpan = buffer.GetSpan(); + this.UncompressRle(stream, width, height, bufferSpan, bytesPerPixel: 1); - for (int y = 0; y < height; y++) + for (int y = 0; y < height; y++) + { + int newY = InvertY(y, height, origin); + Span pixelRow = pixels.DangerousGetRowSpan(newY); + int rowStartIdx = y * width; + for (int x = 0; x < width; x++) { - int newY = InvertY(y, height, origin); - Span pixelRow = pixels.DangerousGetRowSpan(newY); - int rowStartIdx = y * width; - for (int x = 0; x < width; x++) + int idx = rowStartIdx + x; + switch (colorMapPixelSizeInBytes) { - int idx = rowStartIdx + x; - switch (colorMapPixelSizeInBytes) - { - case 1: - color.FromL8(Unsafe.As(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes])); - break; - case 2: - this.ReadPalettedBgra16Pixel(palette, bufferSpan[idx], colorMapPixelSizeInBytes, ref color); - break; - case 3: - color.FromBgr24(Unsafe.As(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes])); - break; - case 4: - color.FromBgra32(Unsafe.As(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes])); - break; - } - - int newX = InvertX(x, width, origin); - pixelRow[newX] = color; + case 1: + color.FromL8(Unsafe.As(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes])); + break; + case 2: + this.ReadPalettedBgra16Pixel(palette, bufferSpan[idx], colorMapPixelSizeInBytes, ref color); + break; + case 3: + color.FromBgr24(Unsafe.As(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes])); + break; + case 4: + color.FromBgra32(Unsafe.As(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes])); + break; } + + int newX = InvertX(x, width, origin); + pixelRow[newX] = color; } } } @@ -598,60 +596,58 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals Guard.NotNull(this.tgaMetadata); byte alphaBits = this.tgaMetadata.AlphaChannelBits; - using (IMemoryOwner buffer = this.memoryAllocator.Allocate(width * height * bytesPerPixel, AllocationOptions.Clean)) + using IMemoryOwner buffer = this.memoryAllocator.Allocate(width * height * bytesPerPixel, AllocationOptions.Clean); + Span bufferSpan = buffer.GetSpan(); + this.UncompressRle(stream, width, height, bufferSpan, bytesPerPixel); + for (int y = 0; y < height; y++) { - Span bufferSpan = buffer.GetSpan(); - this.UncompressRle(stream, width, height, bufferSpan, bytesPerPixel); - for (int y = 0; y < height; y++) + int newY = InvertY(y, height, origin); + Span pixelRow = pixels.DangerousGetRowSpan(newY); + int rowStartIdx = y * width * bytesPerPixel; + for (int x = 0; x < width; x++) { - int newY = InvertY(y, height, origin); - Span pixelRow = pixels.DangerousGetRowSpan(newY); - int rowStartIdx = y * width * bytesPerPixel; - for (int x = 0; x < width; x++) + int idx = rowStartIdx + (x * bytesPerPixel); + switch (bytesPerPixel) { - int idx = rowStartIdx + (x * bytesPerPixel); - switch (bytesPerPixel) - { - case 1: - color.FromL8(Unsafe.As(ref bufferSpan[idx])); - break; - case 2: - if (!this.hasAlpha) - { - // Set alpha value to 1, to treat it as opaque for Bgra5551. - bufferSpan[idx + 1] = (byte)(bufferSpan[idx + 1] | 128); - } - - if (this.fileHeader.ImageType == TgaImageType.RleBlackAndWhite) - { - color.FromLa16(Unsafe.As(ref bufferSpan[idx])); - } - else - { - color.FromBgra5551(Unsafe.As(ref bufferSpan[idx])); - } - - break; - case 3: - color.FromBgr24(Unsafe.As(ref bufferSpan[idx])); - break; - case 4: - if (this.hasAlpha) - { - color.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)); - } - - break; - } + case 1: + color.FromL8(Unsafe.As(ref bufferSpan[idx])); + break; + case 2: + if (!this.hasAlpha) + { + // Set alpha value to 1, to treat it as opaque for Bgra5551. + bufferSpan[idx + 1] = (byte)(bufferSpan[idx + 1] | 128); + } - int newX = InvertX(x, width, origin); - pixelRow[newX] = color; + if (this.fileHeader.ImageType == TgaImageType.RleBlackAndWhite) + { + color.FromLa16(Unsafe.As(ref bufferSpan[idx])); + } + else + { + color.FromBgra5551(Unsafe.As(ref bufferSpan[idx])); + } + + break; + case 3: + color.FromBgr24(Unsafe.As(ref bufferSpan[idx])); + break; + case 4: + if (this.hasAlpha) + { + color.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)); + } + + break; } + + int newX = InvertX(x, width, origin); + pixelRow[newX] = color; } } } @@ -952,7 +948,6 @@ internal sealed class TgaDecoderCore : IImageDecoderInternals this.hasAlpha = alphaBits > 0; // Bits 4 and 5 describe the image origin. - TgaImageOrigin origin = (TgaImageOrigin)((this.fileHeader.ImageDescriptor & 0x30) >> 4); - return origin; + return (TgaImageOrigin)((this.fileHeader.ImageDescriptor & 0x30) >> 4); } } From ffd5c360a4e466b98d5dfb8198c45648ca060916 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 9 Feb 2023 20:57:58 +1000 Subject: [PATCH 19/22] Remove unneccessary guard --- src/ImageSharp/IO/ChunkedMemoryStream.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ImageSharp/IO/ChunkedMemoryStream.cs b/src/ImageSharp/IO/ChunkedMemoryStream.cs index 2e088397a0..da52f7ca85 100644 --- a/src/ImageSharp/IO/ChunkedMemoryStream.cs +++ b/src/ImageSharp/IO/ChunkedMemoryStream.cs @@ -47,8 +47,6 @@ internal sealed class ChunkedMemoryStream : Stream /// The memory allocator. public ChunkedMemoryStream(MemoryAllocator allocator) { - Guard.NotNull(allocator, nameof(allocator)); - this.allocatorCapacity = allocator.GetBufferCapacityInBytes(); this.allocator = allocator; } From 280e3762d25b2866456c5f96c7da32eaf7801cde Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sat, 11 Feb 2023 17:41:10 +0100 Subject: [PATCH 20/22] Fix calculation of mode score for SSE2/AVX2 version --- .../Formats/Webp/Lossy/LossyUtils.cs | 18 +++++++++--------- src/ImageSharp/Formats/Webp/Lossy/QuantEnc.cs | 12 ++++++------ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/Lossy/LossyUtils.cs b/src/ImageSharp/Formats/Webp/Lossy/LossyUtils.cs index 4756dea867..354581e929 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/LossyUtils.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/LossyUtils.cs @@ -14,7 +14,7 @@ internal static class LossyUtils { // Note: method name in libwebp reference implementation is called VP8SSE16x16. [MethodImpl(InliningOptions.ShortMethod)] - public static int Vp8_Sse16X16(Span a, Span b) + public static int Vp8_Sse16x16(Span a, Span b) { if (Avx2.IsSupported) { @@ -31,7 +31,7 @@ internal static class LossyUtils // Note: method name in libwebp reference implementation is called VP8SSE16x8. [MethodImpl(InliningOptions.ShortMethod)] - public static int Vp8_Sse16X8(Span a, Span b) + public static int Vp8_Sse16x8(Span a, Span b) { if (Avx2.IsSupported) { @@ -48,7 +48,7 @@ internal static class LossyUtils // Note: method name in libwebp reference implementation is called VP8SSE4x4. [MethodImpl(InliningOptions.ShortMethod)] - public static int Vp8_Sse4X4(Span a, Span b) + public static int Vp8_Sse4x4(Span a, Span b) { if (Avx2.IsSupported) { @@ -77,8 +77,8 @@ internal static class LossyUtils Vector256 b01s = Avx2.UnpackLow(b01.AsByte(), Vector256.Zero); // subtract, square and accumulate. - Vector256 d0 = Avx2.SubtractSaturate(a01s, b01s); - Vector256 e0 = Avx2.MultiplyAddAdjacent(d0.AsInt16(), d0.AsInt16()); + Vector256 d0 = Avx2.SubtractSaturate(a01s.AsInt16(), b01s.AsInt16()); + Vector256 e0 = Avx2.MultiplyAddAdjacent(d0, d0); return Numerics.ReduceSum(e0); } @@ -110,10 +110,10 @@ internal static class LossyUtils Vector128 b23s = Sse2.UnpackLow(b23.AsByte(), Vector128.Zero); // subtract, square and accumulate. - Vector128 d0 = Sse2.SubtractSaturate(a01s, b01s); - Vector128 d1 = Sse2.SubtractSaturate(a23s, b23s); - Vector128 e0 = Sse2.MultiplyAddAdjacent(d0.AsInt16(), d0.AsInt16()); - Vector128 e1 = Sse2.MultiplyAddAdjacent(d1.AsInt16(), d1.AsInt16()); + Vector128 d0 = Sse2.SubtractSaturate(a01s.AsInt16(), b01s.AsInt16()); + Vector128 d1 = Sse2.SubtractSaturate(a23s.AsInt16(), b23s.AsInt16()); + Vector128 e0 = Sse2.MultiplyAddAdjacent(d0, d0); + Vector128 e1 = Sse2.MultiplyAddAdjacent(d1, d1); Vector128 sum = Sse2.Add(e0, e1); return Numerics.ReduceSum(sum); diff --git a/src/ImageSharp/Formats/Webp/Lossy/QuantEnc.cs b/src/ImageSharp/Formats/Webp/Lossy/QuantEnc.cs index fed9c16d4d..fca0d03f23 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/QuantEnc.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/QuantEnc.cs @@ -53,7 +53,7 @@ internal static unsafe class QuantEnc rdCur.Nz = (uint)ReconstructIntra16(it, dqm, rdCur, tmpDst, mode); // Measure RD-score. - rdCur.D = LossyUtils.Vp8_Sse16X16(src, tmpDst); + rdCur.D = LossyUtils.Vp8_Sse16x16(src, tmpDst); rdCur.SD = tlambda != 0 ? Mult8B(tlambda, LossyUtils.Vp8Disto16X16(src, tmpDst, WeightY, scratch)) : 0; rdCur.H = WebpConstants.Vp8FixedCostsI16[mode]; rdCur.R = it.GetCostLuma16(rdCur, proba, res); @@ -145,7 +145,7 @@ internal static unsafe class QuantEnc rdTmp.Nz = (uint)ReconstructIntra4(it, dqm, tmpLevels, src, tmpDst, mode); // Compute RD-score. - rdTmp.D = LossyUtils.Vp8_Sse4X4(src, tmpDst); + rdTmp.D = LossyUtils.Vp8_Sse4x4(src, tmpDst); rdTmp.SD = tlambda != 0 ? Mult8B(tlambda, LossyUtils.Vp8Disto4X4(src, tmpDst, WeightY, scratch)) : 0; rdTmp.H = modeCosts[mode]; @@ -235,7 +235,7 @@ internal static unsafe class QuantEnc rdUv.Nz = (uint)ReconstructUv(it, dqm, rdUv, tmpDst, mode); // Compute RD-score - rdUv.D = LossyUtils.Vp8_Sse16X8(src, tmpDst); + rdUv.D = LossyUtils.Vp8_Sse16x8(src, tmpDst); rdUv.SD = 0; // not calling TDisto here: it tends to flatten areas. rdUv.H = WebpConstants.Vp8FixedCostsUv[mode]; rdUv.R = it.GetCostUv(rdUv, proba, res); @@ -389,7 +389,7 @@ internal static unsafe class QuantEnc for (mode = 0; mode < WebpConstants.NumPredModes; ++mode) { Span reference = it.YuvP.AsSpan(Vp8Encoding.Vp8I16ModeOffsets[mode]); - long score = (LossyUtils.Vp8_Sse16X16(src, reference) * WebpConstants.RdDistoMult) + (WebpConstants.Vp8FixedCostsI16[mode] * lambdaDi16); + long score = (LossyUtils.Vp8_Sse16x16(src, reference) * WebpConstants.RdDistoMult) + (WebpConstants.Vp8FixedCostsI16[mode] * lambdaDi16); if (mode > 0 && WebpConstants.Vp8FixedCostsI16[mode] > bitLimit) { @@ -436,7 +436,7 @@ internal static unsafe class QuantEnc for (mode = 0; mode < WebpConstants.NumBModes; ++mode) { Span reference = it.YuvP.AsSpan(Vp8Encoding.Vp8I4ModeOffsets[mode]); - long score = (LossyUtils.Vp8_Sse4X4(src, reference) * WebpConstants.RdDistoMult) + (modeCosts[mode] * lambdaDi4); + long score = (LossyUtils.Vp8_Sse4x4(src, reference) * WebpConstants.RdDistoMult) + (modeCosts[mode] * lambdaDi4); if (score < bestI4Score) { bestI4Mode = mode; @@ -485,7 +485,7 @@ internal static unsafe class QuantEnc for (mode = 0; mode < WebpConstants.NumPredModes; ++mode) { Span reference = it.YuvP.AsSpan(Vp8Encoding.Vp8UvModeOffsets[mode]); - long score = (LossyUtils.Vp8_Sse16X8(src, reference) * WebpConstants.RdDistoMult) + (WebpConstants.Vp8FixedCostsUv[mode] * lambdaDuv); + long score = (LossyUtils.Vp8_Sse16x8(src, reference) * WebpConstants.RdDistoMult) + (WebpConstants.Vp8FixedCostsUv[mode] * lambdaDuv); if (score < bestUvScore) { bestMode = mode; From 4407990cfdbebd11860358d06b931557d45a67cf Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sat, 11 Feb 2023 17:42:50 +0100 Subject: [PATCH 21/22] Better test for mode score calculation 4X4 --- .../Formats/WebP/LossyUtilsTests.cs | 49 +++++++++---------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/WebP/LossyUtilsTests.cs b/tests/ImageSharp.Tests/Formats/WebP/LossyUtilsTests.cs index 69b503b5e5..e5928f2a61 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/LossyUtilsTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/LossyUtilsTests.cs @@ -140,7 +140,7 @@ public class LossyUtilsTests int expected = 2063; // act - int actual = LossyUtils.Vp8_Sse16X16(a, b); + int actual = LossyUtils.Vp8_Sse16x16(a, b); // assert Assert.Equal(expected, actual); @@ -186,7 +186,7 @@ public class LossyUtilsTests int expected = 749; // act - int actual = LossyUtils.Vp8_Sse16X8(a, b); + int actual = LossyUtils.Vp8_Sse16x8(a, b); // assert Assert.Equal(expected, actual); @@ -195,33 +195,25 @@ public class LossyUtilsTests private static void RunVp8Sse4X4Test() { // arrange - byte[] a = + Random rand = new(1234); + byte[] a = new byte[128 * 10]; + byte[] b = new byte[128 * 10]; + for (int i = 0; i < a.Length; i++) { - 27, 27, 28, 29, 29, 28, 27, 27, 27, 28, 28, 29, 29, 28, 28, 27, 129, 129, 129, 129, 129, 129, 129, - 129, 128, 128, 128, 128, 128, 128, 128, 128, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 29, 29, 28, - 28, 27, 129, 129, 129, 129, 129, 129, 129, 129, 128, 128, 128, 128, 128, 128, 128, 128, 27, 27, 26, - 26, 26, 26, 27, 27, 27, 28, 28, 29, 29, 28, 28, 27, 129, 129, 129, 129, 129, 129, 129, 129, 128, - 128, 128, 128, 128, 128, 128, 128, 28, 27, 27, 26, 26, 27, 27, 28, 27, 28, 28, 29, 29, 28, 28, 27, - 129, 129, 129, 129, 129, 129, 129, 129, 128, 128, 128, 128, 128, 128, 128, 128 - }; - - byte[] b = + a[i] = (byte)rand.Next(byte.MaxValue); + b[i] = (byte)rand.Next(byte.MaxValue); + } + int[] expected = { 194133, 125861, 165966, 195688, 106491, 173015, 266960, 200272, 311224, 122545 }; + + // act + assert + int offset = 0; + for (int i = 0; i < expected.Length; i++) { - 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 204, 204, 204, 204, 204, 204, 204, - 204, 204, 204, 204, 204, 204, 204, 204, 204, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, - 28, 28, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 26, 26, 26, - 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 204, 204, 204, 204, 204, 204, 204, 204, 204, - 204, 204, 204, 204, 204, 204, 204, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, - 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204 - }; + int actual = LossyUtils.Vp8_Sse4x4(a.AsSpan(offset), b.AsSpan(offset)); + Assert.Equal(expected[i], actual); - int expected = 27; - - // act - int actual = LossyUtils.Vp8_Sse4X4(a, b); - - // assert - Assert.Equal(expected, actual); + offset += 128; + } } private static void RunMean16x4Test() @@ -329,12 +321,15 @@ public class LossyUtilsTests [Fact] public void Vp8Sse16X8_WithoutAVX2_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunVp8Sse16X8Test, HwIntrinsics.DisableAVX2); + // This will test the AVX2 version. [Fact] public void Vp8Sse4X4_WithHardwareIntrinsics_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunVp8Sse4X4Test, HwIntrinsics.AllowAll); + // This will test the fallback scalar version. [Fact] - public void Vp8Sse4X4_WithoutSSE2_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunVp8Sse4X4Test, HwIntrinsics.DisableSSE2); + public void Vp8Sse4X4_WithoutHardwareIntrinsics_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunVp8Sse4X4Test, HwIntrinsics.DisableSSE2 | HwIntrinsics.DisableAVX); + // This will test the SSE2 version. [Fact] public void Vp8Sse4X4_WithoutAVX2_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunVp8Sse4X4Test, HwIntrinsics.DisableAVX2); From d0369fb115e3989f5377e783149b8786c95cb77c Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sat, 11 Feb 2023 18:07:36 +0100 Subject: [PATCH 22/22] Better tests for mode score calculation for 16x8 and 16x16 --- .../Formats/Webp/Lossy/LossyUtils.cs | 8 +- .../Formats/WebP/LossyUtilsTests.cs | 152 +++++------------- 2 files changed, 47 insertions(+), 113 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/Lossy/LossyUtils.cs b/src/ImageSharp/Formats/Webp/Lossy/LossyUtils.cs index 354581e929..322a5f6434 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/LossyUtils.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/LossyUtils.cs @@ -126,18 +126,16 @@ internal static class LossyUtils public static int Vp8_SseNxN(Span a, Span b, int w, int h) { int count = 0; - int aOffset = 0; - int bOffset = 0; + int offset = 0; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { - int diff = a[aOffset + x] - b[bOffset + x]; + int diff = a[offset + x] - b[offset + x]; count += diff * diff; } - aOffset += WebpConstants.Bps; - bOffset += WebpConstants.Bps; + offset += WebpConstants.Bps; } return count; diff --git a/tests/ImageSharp.Tests/Formats/WebP/LossyUtilsTests.cs b/tests/ImageSharp.Tests/Formats/WebP/LossyUtilsTests.cs index e5928f2a61..e7f9ade36c 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/LossyUtilsTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/LossyUtilsTests.cs @@ -77,119 +77,49 @@ public class LossyUtilsTests private static void RunVp8Sse16X16Test() { // arrange - byte[] a = + Random rand = new(1234); + byte[] a = new byte[512 * 10]; + byte[] b = new byte[512 * 10]; + for (int i = 0; i < a.Length; i++) { - 154, 154, 151, 151, 149, 148, 151, 157, 163, 163, 154, 132, 102, 98, 104, 108, 107, 104, 104, 103, - 101, 106, 123, 119, 170, 171, 172, 171, 168, 175, 171, 173, 151, 151, 149, 150, 147, 147, 146, 159, - 164, 165, 154, 129, 92, 90, 101, 105, 104, 103, 104, 101, 100, 105, 123, 117, 172, 172, 172, 168, - 170, 177, 170, 175, 151, 149, 150, 150, 147, 147, 156, 161, 161, 161, 151, 126, 93, 90, 102, 107, - 104, 103, 104, 101, 104, 104, 122, 117, 172, 172, 170, 168, 170, 177, 172, 175, 150, 149, 152, 151, - 148, 151, 160, 159, 157, 157, 148, 133, 96, 90, 103, 107, 104, 104, 101, 100, 102, 102, 121, 117, - 170, 170, 169, 171, 171, 179, 173, 175, 149, 151, 152, 151, 148, 154, 162, 157, 154, 154, 151, 132, - 92, 89, 101, 108, 104, 102, 101, 101, 103, 103, 123, 118, 171, 168, 177, 173, 171, 178, 172, 176, - 152, 152, 152, 151, 154, 162, 161, 155, 149, 157, 156, 129, 92, 87, 101, 107, 102, 100, 107, 100, - 101, 102, 123, 118, 170, 175, 182, 172, 171, 179, 173, 175, 152, 151, 154, 155, 160, 162, 161, 153, - 150, 156, 153, 129, 92, 91, 102, 106, 100, 109, 115, 99, 101, 102, 124, 120, 171, 179, 178, 172, - 171, 181, 171, 173, 154, 154, 154, 162, 160, 158, 156, 152, 153, 157, 151, 128, 86, 86, 102, 105, - 102, 122, 114, 99, 101, 102, 125, 120, 178, 173, 177, 172, 171, 180, 172, 173, 154, 152, 158, 163, - 150, 148, 148, 156, 151, 158, 152, 129, 87, 87, 101, 105, 204, 204, 204, 204, 204, 204, 204, 204, - 204, 204, 204, 204, 204, 204, 204, 204, 154, 151, 165, 156, 141, 137, 146, 158, 152, 159, 152, 133, - 90, 88, 99, 106, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, - 154, 160, 164, 150, 126, 127, 149, 159, 155, 161, 153, 131, 84, 86, 97, 103, 204, 204, 204, 204, - 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 157, 167, 157, 137, 102, 128, 155, 161, - 157, 159, 154, 134, 84, 82, 97, 102, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, - 204, 204, 204, 204, 163, 163, 150, 113, 78, 132, 156, 162, 159, 160, 154, 132, 83, 78, 91, 97, 204, - 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 163, 157, 137, 80, 78, - 131, 154, 163, 157, 159, 149, 131, 82, 77, 94, 100, 204, 204, 204, 204, 204, 204, 204, 204, 204, - 204, 204, 204, 204, 204, 204, 204, 159, 151, 108, 72, 88, 132, 156, 162, 159, 157, 151, 130, 79, 78, - 95, 102, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 151, 130, - 82, 82, 89, 134, 154, 161, 161, 157, 152, 129, 81, 77, 95, 102, 204, 204, 204, 204, 204, 204, 204, - 204, 204, 204, 204, 204, 204, 204, 204, 204 - }; + a[i] = (byte)rand.Next(byte.MaxValue); + b[i] = (byte)rand.Next(byte.MaxValue); + } + int[] expected = { 2533110, 2818581, 2984663, 2891188, 2855134, 2634604, 2466504, 3061747, 2626010, 2640965 }; - byte[] b = + // act + assert + int offset = 0; + for (int i = 0; i < expected.Length; i++) { - 150, 150, 150, 150, 146, 149, 152, 154, 164, 166, 154, 132, 99, 92, 106, 112, 204, 204, 204, 204, - 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 150, 150, 150, 150, 146, 149, 152, 154, - 161, 164, 151, 130, 93, 86, 100, 106, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, - 204, 204, 204, 204, 150, 150, 150, 150, 146, 149, 152, 154, 158, 161, 148, 127, 93, 86, 100, 106, - 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 150, 150, 150, 150, - 146, 149, 152, 154, 156, 159, 146, 125, 99, 92, 106, 112, 204, 204, 204, 204, 204, 204, 204, 204, - 204, 204, 204, 204, 204, 204, 204, 204, 148, 148, 148, 148, 149, 158, 162, 159, 155, 155, 153, 129, - 94, 87, 101, 106, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, - 151, 151, 151, 151, 152, 159, 161, 156, 155, 155, 153, 129, 94, 87, 101, 106, 204, 204, 204, 204, - 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 154, 154, 154, 154, 156, 161, 159, 152, - 155, 155, 153, 129, 94, 87, 101, 106, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, - 204, 204, 204, 204, 156, 156, 156, 156, 159, 162, 158, 149, 155, 155, 153, 129, 94, 87, 101, 106, - 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 152, 153, 157, 162, - 150, 149, 149, 151, 155, 160, 150, 131, 91, 90, 104, 104, 204, 204, 204, 204, 204, 204, 204, 204, - 204, 204, 204, 204, 204, 204, 204, 204, 152, 156, 158, 157, 140, 137, 145, 159, 155, 160, 150, 131, - 89, 88, 102, 101, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, - 153, 161, 160, 149, 118, 128, 147, 162, 155, 160, 150, 131, 86, 85, 99, 98, 204, 204, 204, 204, 204, - 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 154, 165, 161, 144, 96, 128, 154, 159, 155, - 160, 150, 131, 83, 82, 97, 96, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, - 204, 204, 161, 160, 149, 105, 78, 127, 156, 170, 156, 156, 154, 130, 81, 77, 95, 102, 204, 204, 204, - 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 160, 160, 133, 85, 81, 129, 155, - 167, 156, 156, 154, 130, 81, 77, 95, 102, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, - 204, 204, 204, 204, 204, 156, 147, 109, 76, 85, 130, 153, 163, 156, 156, 154, 130, 81, 77, 95, 102, - 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 152, 128, 87, 83, - 88, 132, 152, 159, 156, 156, 154, 130, 81, 77, 95, 102, 204, 204, 204, 204, 204, 204, 204, 204, 204, - 204, 204, 204, 204, 204, 204, 204 - }; - - int expected = 2063; - - // act - int actual = LossyUtils.Vp8_Sse16x16(a, b); + int actual = LossyUtils.Vp8_Sse16x16(a.AsSpan(offset), b.AsSpan(offset)); + Assert.Equal(expected[i], actual); - // assert - Assert.Equal(expected, actual); + offset += 512; + } } private static void RunVp8Sse16X8Test() { // arrange - byte[] a = + Random rand = new(1234); + byte[] a = new byte[256 * 10]; + byte[] b = new byte[256 * 10]; + for (int i = 0; i < a.Length; i++) { - 107, 104, 104, 103, 101, 106, 123, 119, 170, 171, 172, 171, 168, 175, 171, 173, 151, 151, 149, 150, - 147, 147, 146, 159, 164, 165, 154, 129, 92, 90, 101, 105, 104, 103, 104, 101, 100, 105, 123, 117, - 172, 172, 172, 168, 170, 177, 170, 175, 151, 149, 150, 150, 147, 147, 156, 161, 161, 161, 151, 126, - 93, 90, 102, 107, 104, 103, 104, 101, 104, 104, 122, 117, 172, 172, 170, 168, 170, 177, 172, 175, - 150, 149, 152, 151, 148, 151, 160, 159, 157, 157, 148, 133, 96, 90, 103, 107, 104, 104, 101, 100, - 102, 102, 121, 117, 170, 170, 169, 171, 171, 179, 173, 175, 149, 151, 152, 151, 148, 154, 162, 157, - 154, 154, 151, 132, 92, 89, 101, 108, 104, 102, 101, 101, 103, 103, 123, 118, 171, 168, 177, 173, - 171, 178, 172, 176, 152, 152, 152, 151, 154, 162, 161, 155, 149, 157, 156, 129, 92, 87, 101, 107, - 102, 100, 107, 100, 101, 102, 123, 118, 170, 175, 182, 172, 171, 179, 173, 175, 152, 151, 154, 155, - 160, 162, 161, 153, 150, 156, 153, 129, 92, 91, 102, 106, 100, 109, 115, 99, 101, 102, 124, 120, - 171, 179, 178, 172, 171, 181, 171, 173, 154, 154, 154, 162, 160, 158, 156, 152, 153, 157, 151, 128, - 86, 86, 102, 105, 102, 122, 114, 99, 101, 102, 125, 120, 178, 173, 177, 172, 171, 180, 172, 173, - 154, 152, 158, 163, 150, 148, 148, 156, 151, 158, 152, 129, 87, 87, 101, 105 - }; + a[i] = (byte)rand.Next(byte.MaxValue); + b[i] = (byte)rand.Next(byte.MaxValue); + } + int[] expected = { 1298274, 1234836, 1325264, 1493317, 1551995, 1432668, 1407891, 1483297, 1537930, 1317204 }; - byte[] b = + // act + assert + int offset = 0; + for (int i = 0; i < expected.Length; i++) { - 103, 103, 103, 103, 101, 106, 122, 114, 171, 171, 171, 171, 171, 177, 169, 175, 150, 150, 150, 150, - 146, 149, 152, 154, 161, 164, 151, 130, 93, 86, 100, 106, 103, 103, 103, 103, 101, 106, 122, 114, - 171, 171, 171, 171, 171, 177, 169, 175, 150, 150, 150, 150, 146, 149, 152, 154, 158, 161, 148, 127, - 93, 86, 100, 106, 103, 103, 103, 103, 101, 106, 122, 114, 171, 171, 171, 171, 171, 177, 169, 175, - 150, 150, 150, 150, 146, 149, 152, 154, 156, 159, 146, 125, 99, 92, 106, 112, 103, 103, 103, 103, - 101, 106, 122, 114, 171, 171, 171, 171, 171, 177, 169, 175, 148, 148, 148, 148, 149, 158, 162, 159, - 155, 155, 153, 129, 94, 87, 101, 106, 102, 100, 100, 102, 100, 101, 120, 122, 170, 176, 176, 170, - 174, 180, 171, 177, 151, 151, 151, 151, 152, 159, 161, 156, 155, 155, 153, 129, 94, 87, 101, 106, - 102, 105, 105, 102, 100, 101, 120, 122, 170, 176, 176, 170, 174, 180, 171, 177, 154, 154, 154, 154, - 156, 161, 159, 152, 155, 155, 153, 129, 94, 87, 101, 106, 102, 112, 112, 102, 100, 101, 120, 122, - 170, 176, 176, 170, 174, 180, 171, 177, 156, 156, 156, 156, 159, 162, 158, 149, 155, 155, 153, 129, - 94, 87, 101, 106, 102, 117, 117, 102, 100, 101, 120, 122, 170, 176, 176, 170, 174, 180, 171, 177, - 152, 153, 157, 162, 150, 149, 149, 151, 155, 160, 150, 131, 91, 90, 104, 104 - }; - - int expected = 749; - - // act - int actual = LossyUtils.Vp8_Sse16x8(a, b); + int actual = LossyUtils.Vp8_Sse16x8(a.AsSpan(offset), b.AsSpan(offset)); + Assert.Equal(expected[i], actual); - // assert - Assert.Equal(expected, actual); + offset += 256; + } } private static void RunVp8Sse4X4Test() @@ -303,36 +233,42 @@ public class LossyUtilsTests [Fact] public void TransformOne_WithoutHardwareIntrinsics_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunTransformOneTest, HwIntrinsics.DisableHWIntrinsic); + // This will test the AVX2 version. [Fact] public void Vp8Sse16X16_WithHardwareIntrinsics_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunVp8Sse16X16Test, HwIntrinsics.AllowAll); + // This will test the SSE2 version. [Fact] - public void Vp8Sse16X16_WithoutSSE2_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunVp8Sse16X16Test, HwIntrinsics.DisableSSE2); + public void Vp8Sse16X16_WithoutAVX2_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunVp8Sse16X16Test, HwIntrinsics.DisableAVX2); + // This will test the fallback scalar version. [Fact] - public void Vp8Sse16X16_WithoutAVX2_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunVp8Sse16X16Test, HwIntrinsics.DisableAVX2); + public void Vp8Sse16X16_WithoutSSE2_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunVp8Sse16X16Test, HwIntrinsics.DisableSSE2 | HwIntrinsics.DisableAVX); + // This will test the AVX2 version. [Fact] public void Vp8Sse16X8_WithHardwareIntrinsics_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunVp8Sse16X8Test, HwIntrinsics.AllowAll); + // This will test the SSE2 version. [Fact] - public void Vp8Sse16X8_WithoutSSE2_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunVp8Sse16X8Test, HwIntrinsics.DisableSSE2); + public void Vp8Sse16X8_WithoutAVX2_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunVp8Sse16X8Test, HwIntrinsics.DisableAVX2); + // This will test the fallback scalar version. [Fact] - public void Vp8Sse16X8_WithoutAVX2_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunVp8Sse16X8Test, HwIntrinsics.DisableAVX2); + public void Vp8Sse16X8_WithoutHardwareIntrinsics_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunVp8Sse16X8Test, HwIntrinsics.DisableSSE2 | HwIntrinsics.DisableAVX); // This will test the AVX2 version. [Fact] public void Vp8Sse4X4_WithHardwareIntrinsics_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunVp8Sse4X4Test, HwIntrinsics.AllowAll); - // This will test the fallback scalar version. - [Fact] - public void Vp8Sse4X4_WithoutHardwareIntrinsics_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunVp8Sse4X4Test, HwIntrinsics.DisableSSE2 | HwIntrinsics.DisableAVX); - // This will test the SSE2 version. [Fact] public void Vp8Sse4X4_WithoutAVX2_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunVp8Sse4X4Test, HwIntrinsics.DisableAVX2); + // This will test the fallback scalar version. + [Fact] + public void Vp8Sse4X4_WithoutHardwareIntrinsics_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunVp8Sse4X4Test, HwIntrinsics.DisableSSE2 | HwIntrinsics.DisableAVX); + [Fact] public void Mean16x4_WithHardwareIntrinsics_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunMean16x4Test, HwIntrinsics.AllowAll);