diff --git a/src/ImageSharp/Formats/Pbm/BinaryDecoder.cs b/src/ImageSharp/Formats/Pbm/BinaryDecoder.cs index c7a09a613e..8b6df295b2 100644 --- a/src/ImageSharp/Formats/Pbm/BinaryDecoder.cs +++ b/src/ImageSharp/Formats/Pbm/BinaryDecoder.cs @@ -171,7 +171,7 @@ namespace SixLabors.ImageSharp.Formats.Pbm x++; if (x == width) { - startBit = (bit + 1) % 8; + startBit = (bit + 1) & 7; // Round off to below 8. if (startBit != 0) { stream.Seek(-1, System.IO.SeekOrigin.Current); diff --git a/src/ImageSharp/Formats/Pbm/BinaryEncoder.cs b/src/ImageSharp/Formats/Pbm/BinaryEncoder.cs index 1233c87fcb..2bcbaeef7c 100644 --- a/src/ImageSharp/Formats/Pbm/BinaryEncoder.cs +++ b/src/ImageSharp/Formats/Pbm/BinaryEncoder.cs @@ -60,8 +60,8 @@ namespace SixLabors.ImageSharp.Formats.Pbm private static void WriteGrayscale(Configuration configuration, Stream stream, ImageFrame image) where TPixel : unmanaged, IPixel { - int width = image.Size().Width; - int height = image.Size().Height; + int width = image.Width; + int height = image.Height; MemoryAllocator allocator = configuration.MemoryAllocator; using IMemoryOwner row = allocator.Allocate(width); Span rowSpan = row.GetSpan(); @@ -83,8 +83,8 @@ namespace SixLabors.ImageSharp.Formats.Pbm private static void WriteWideGrayscale(Configuration configuration, Stream stream, ImageFrame image) where TPixel : unmanaged, IPixel { - int width = image.Size().Width; - int height = image.Size().Height; + int width = image.Width; + int height = image.Height; int bytesPerPixel = 2; MemoryAllocator allocator = configuration.MemoryAllocator; using IMemoryOwner row = allocator.Allocate(width * bytesPerPixel); @@ -107,8 +107,8 @@ namespace SixLabors.ImageSharp.Formats.Pbm private static void WriteRgb(Configuration configuration, Stream stream, ImageFrame image) where TPixel : unmanaged, IPixel { - int width = image.Size().Width; - int height = image.Size().Height; + int width = image.Width; + int height = image.Height; int bytesPerPixel = 3; MemoryAllocator allocator = configuration.MemoryAllocator; using IMemoryOwner row = allocator.Allocate(width * bytesPerPixel); @@ -131,8 +131,8 @@ namespace SixLabors.ImageSharp.Formats.Pbm private static void WriteWideRgb(Configuration configuration, Stream stream, ImageFrame image) where TPixel : unmanaged, IPixel { - int width = image.Size().Width; - int height = image.Size().Height; + int width = image.Width; + int height = image.Height; int bytesPerPixel = 6; MemoryAllocator allocator = configuration.MemoryAllocator; using IMemoryOwner row = allocator.Allocate(width * bytesPerPixel); @@ -155,8 +155,8 @@ namespace SixLabors.ImageSharp.Formats.Pbm private static void WriteBlackAndWhite(Configuration configuration, Stream stream, ImageFrame image) where TPixel : unmanaged, IPixel { - int width = image.Size().Width; - int height = image.Size().Height; + int width = image.Width; + int height = image.Height; MemoryAllocator allocator = configuration.MemoryAllocator; using IMemoryOwner row = allocator.Allocate(width); Span rowSpan = row.GetSpan(); @@ -186,7 +186,7 @@ namespace SixLabors.ImageSharp.Formats.Pbm if (x == width) { previousValue = value; - startBit = (i + 1) % 8; + startBit = (i + 1) & 7; // Round off to below 8. break; } } diff --git a/src/ImageSharp/Formats/Pbm/BufferedReadStreamExtensions.cs b/src/ImageSharp/Formats/Pbm/BufferedReadStreamExtensions.cs index 054731b483..581d3e592b 100644 --- a/src/ImageSharp/Formats/Pbm/BufferedReadStreamExtensions.cs +++ b/src/ImageSharp/Formats/Pbm/BufferedReadStreamExtensions.cs @@ -51,7 +51,7 @@ namespace SixLabors.ImageSharp.Formats.Pbm while (true) { int current = stream.ReadByte() - 0x30; - if (current < 0 || current > 9) + if ((uint)current > 9) { break; } diff --git a/src/ImageSharp/Formats/Pbm/PbmConstants.cs b/src/ImageSharp/Formats/Pbm/PbmConstants.cs index 0aa9b706ae..912ffaf856 100644 --- a/src/ImageSharp/Formats/Pbm/PbmConstants.cs +++ b/src/ImageSharp/Formats/Pbm/PbmConstants.cs @@ -18,11 +18,11 @@ namespace SixLabors.ImageSharp.Formats.Pbm /// /// The list of mimetypes that equate to a ppm. /// - public static readonly IEnumerable MimeTypes = new[] { "image/x-portable-pixmap", "image/x-portable-anymap", "image/x-portable-arbitrarymap" }; + public static readonly IEnumerable MimeTypes = new[] { "image/x-portable-pixmap", "image/x-portable-anymap" }; /// /// The list of file extensions that equate to a ppm. /// - public static readonly IEnumerable FileExtensions = new[] { "ppm", "pbm", "pgm", "pam" }; + public static readonly IEnumerable FileExtensions = new[] { "ppm", "pbm", "pgm" }; } } diff --git a/src/ImageSharp/Formats/Pbm/PbmDecoder.cs b/src/ImageSharp/Formats/Pbm/PbmDecoder.cs index 640ec38234..62cef176de 100644 --- a/src/ImageSharp/Formats/Pbm/PbmDecoder.cs +++ b/src/ImageSharp/Formats/Pbm/PbmDecoder.cs @@ -9,7 +9,23 @@ using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Pbm { /// - /// Image decoder for generating an image out of a ppm stream. + /// Image decoder for reading PGM, PBM or PPM bitmaps from a stream. These images are from + /// the family of PNM images. + /// + /// + /// PBM + /// Black and white images. + /// + /// + /// PGM + /// Grayscale images. + /// + /// + /// PPM + /// Color images, with RGB pixels. + /// + /// + /// The specification of these images is found at . /// public sealed class PbmDecoder : IImageDecoder, IImageInfoDetector { diff --git a/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs b/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs index 31969af477..bd99a578aa 100644 --- a/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs +++ b/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs @@ -82,9 +82,9 @@ namespace SixLabors.ImageSharp.Formats.Pbm /// The input stream. private void ProcessHeader(BufferedReadStream stream) { - byte[] buffer = new byte[2]; + Span buffer = stackalloc byte[2]; - int bytesRead = stream.Read(buffer, 0, 2); + int bytesRead = stream.Read(buffer); if (bytesRead != 2 || buffer[0] != 'P') { // Empty or not an PPM image. diff --git a/src/ImageSharp/Formats/Pbm/PbmEncoder.cs b/src/ImageSharp/Formats/Pbm/PbmEncoder.cs index 21565d1610..fe0f7f9f16 100644 --- a/src/ImageSharp/Formats/Pbm/PbmEncoder.cs +++ b/src/ImageSharp/Formats/Pbm/PbmEncoder.cs @@ -10,7 +10,28 @@ using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Pbm { /// - /// Image encoder for writing an image to a stream as PGM, PBM, PPM or PAM bitmap. + /// Image encoder for writing an image to a stream as PGM, PBM or PPM bitmap. These images are from + /// the family of PNM images. + /// + /// The PNM formats are a faily simple image format. They share a plain text header, consisting of: + /// signature, width, height and max_pixel_value only. The pixels follow thereafter and can be in + /// plain text decimals seperated by spaces, or binary encoded. + /// + /// + /// PBM + /// Black and white images, with 1 representing black and 0 representing white. + /// + /// + /// PGM + /// Grayscale images, scaling from 0 to max_pixel_value, 0 representing black and max_pixel_value representing white. + /// + /// + /// PPM + /// Color images, with RGB pixels (in that order), with 0 representing black and 2 representing full color. + /// + /// + /// + /// The specification of these images is found at . /// public sealed class PbmEncoder : IImageEncoder, IPbmEncoderOptions { diff --git a/src/ImageSharp/Formats/Pbm/PbmEncoderCore.cs b/src/ImageSharp/Formats/Pbm/PbmEncoderCore.cs index 527ceb8eed..eb1ba81401 100644 --- a/src/ImageSharp/Formats/Pbm/PbmEncoderCore.cs +++ b/src/ImageSharp/Formats/Pbm/PbmEncoderCore.cs @@ -2,12 +2,10 @@ // Licensed under the Apache License, Version 2.0. using System; -using System.Buffers; +using System.Buffers.Text; using System.IO; -using System.Text; using System.Threading; using SixLabors.ImageSharp.Advanced; -using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Pbm @@ -17,7 +15,9 @@ namespace SixLabors.ImageSharp.Formats.Pbm /// internal sealed class PbmEncoderCore : IImageEncoderInternals { - private const char NewLine = '\n'; + private const byte NewLine = (byte)'\n'; + private const byte Space = (byte)' '; + private const byte P = (byte)'P'; /// /// The global configuration. @@ -70,7 +70,7 @@ namespace SixLabors.ImageSharp.Formats.Pbm this.DeduceOptions(image); - string signature = this.DeduceSignature(); + byte signature = this.DeduceSignature(); this.WriteHeader(stream, signature, image.Size()); this.WritePixels(stream, image.Frames.RootFrame); @@ -91,29 +91,29 @@ namespace SixLabors.ImageSharp.Formats.Pbm } } - private string DeduceSignature() + private byte DeduceSignature() { - string signature; + byte signature; if (this.colorType == PbmColorType.BlackAndWhite) { if (this.encoding == PbmEncoding.Plain) { - signature = "P1"; + signature = (byte)'1'; } else { - signature = "P4"; + signature = (byte)'4'; } } else if (this.colorType == PbmColorType.Grayscale) { if (this.encoding == PbmEncoding.Plain) { - signature = "P2"; + signature = (byte)'2'; } else { - signature = "P5"; + signature = (byte)'5'; } } else @@ -121,35 +121,41 @@ namespace SixLabors.ImageSharp.Formats.Pbm // RGB ColorType if (this.encoding == PbmEncoding.Plain) { - signature = "P3"; + signature = (byte)'3'; } else { - signature = "P6"; + signature = (byte)'6'; } } return signature; } - private void WriteHeader(Stream stream, string signature, Size pixelSize) + private void WriteHeader(Stream stream, byte signature, Size pixelSize) { - var builder = new StringBuilder(20); - builder.Append(signature); - builder.Append(NewLine); - builder.Append(pixelSize.Width.ToString()); - builder.Append(NewLine); - builder.Append(pixelSize.Height.ToString()); - builder.Append(NewLine); + Span buffer = stackalloc byte[128]; + + int written = 3; + buffer[0] = P; + buffer[1] = signature; + buffer[2] = NewLine; + + Utf8Formatter.TryFormat(pixelSize.Width, buffer.Slice(written), out int bytesWritten); + written += bytesWritten; + buffer[written++] = Space; + Utf8Formatter.TryFormat(pixelSize.Height, buffer.Slice(written), out bytesWritten); + written += bytesWritten; + buffer[written++] = NewLine; + if (this.colorType != PbmColorType.BlackAndWhite) { - builder.Append(this.maxPixelValue.ToString()); - builder.Append(NewLine); + Utf8Formatter.TryFormat(this.maxPixelValue, buffer.Slice(written), out bytesWritten); + written += bytesWritten; + buffer[written++] = NewLine; } - string headerStr = builder.ToString(); - byte[] headerBytes = Encoding.ASCII.GetBytes(headerStr); - stream.Write(headerBytes, 0, headerBytes.Length); + stream.Write(buffer, 0, written); } /// diff --git a/src/ImageSharp/Formats/Pbm/PbmImageFormatDetector.cs b/src/ImageSharp/Formats/Pbm/PbmImageFormatDetector.cs index 943424dc9c..15bacc4de7 100644 --- a/src/ImageSharp/Formats/Pbm/PbmImageFormatDetector.cs +++ b/src/ImageSharp/Formats/Pbm/PbmImageFormatDetector.cs @@ -22,9 +22,12 @@ namespace SixLabors.ImageSharp.Formats.Pbm private bool IsSupportedFileFormat(ReadOnlySpan header) { - if (header.Length >= this.HeaderSize) +#pragma warning disable SA1131 // Use readable conditions + if (1 < (uint)header.Length) +#pragma warning restore SA1131 // Use readable conditions { - return header[0] == P && header[1] > Zero && header[1] < Seven; + // Signature should be between P1 and P6. + return header[0] == P && (uint)(header[1] - Zero - 1) < (Seven - Zero - 1); } return false; diff --git a/src/ImageSharp/Formats/Pbm/PlainEncoder.cs b/src/ImageSharp/Formats/Pbm/PlainEncoder.cs index d90eaf73f1..e362f8680f 100644 --- a/src/ImageSharp/Formats/Pbm/PlainEncoder.cs +++ b/src/ImageSharp/Formats/Pbm/PlainEncoder.cs @@ -3,6 +3,7 @@ using System; using System.Buffers; +using System.Buffers.Text; using System.IO; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -20,6 +21,14 @@ namespace SixLabors.ImageSharp.Formats.Pbm private const byte Zero = 0x30; private const byte One = 0x31; + private const int MaxCharsPerPixelBlackAndWhite = 2; + private const int MaxCharsPerPixelGrayscale = 4; + private const int MaxCharsPerPixelGrayscaleWide = 6; + private const int MaxCharsPerPixelRgb = 4 * 3; + private const int MaxCharsPerPixelRgbWide = 6 * 3; + + private static readonly StandardFormat DecimalFormat = StandardFormat.Parse("D"); + /// /// Decode pixels into the PBM plain encoding. /// @@ -63,12 +72,12 @@ namespace SixLabors.ImageSharp.Formats.Pbm private static void WriteGrayscale(Configuration configuration, Stream stream, ImageFrame image) where TPixel : unmanaged, IPixel { - int width = image.Size().Width; - int height = image.Size().Height; - int bytesWritten = -1; + int width = image.Width; + int height = image.Height; MemoryAllocator allocator = configuration.MemoryAllocator; using IMemoryOwner row = allocator.Allocate(width); Span rowSpan = row.GetSpan(); + Span plainSpan = stackalloc byte[width * MaxCharsPerPixelGrayscale]; for (int y = 0; y < height; y++) { @@ -78,23 +87,28 @@ namespace SixLabors.ImageSharp.Formats.Pbm pixelSpan, rowSpan); + int written = 0; for (int x = 0; x < width; x++) { - WriteWhitespace(stream, ref bytesWritten); - bytesWritten += stream.WriteDecimal(rowSpan[x].PackedValue); + Utf8Formatter.TryFormat(rowSpan[x].PackedValue, plainSpan.Slice(written), out int bytesWritten, DecimalFormat); + written += bytesWritten; + plainSpan[written++] = Space; } + + plainSpan[written - 1] = NewLine; + stream.Write(plainSpan, 0, written); } } private static void WriteWideGrayscale(Configuration configuration, Stream stream, ImageFrame image) where TPixel : unmanaged, IPixel { - int width = image.Size().Width; - int height = image.Size().Height; - int bytesWritten = -1; + int width = image.Width; + int height = image.Height; MemoryAllocator allocator = configuration.MemoryAllocator; using IMemoryOwner row = allocator.Allocate(width); Span rowSpan = row.GetSpan(); + Span plainSpan = stackalloc byte[width * MaxCharsPerPixelGrayscaleWide]; for (int y = 0; y < height; y++) { @@ -104,23 +118,28 @@ namespace SixLabors.ImageSharp.Formats.Pbm pixelSpan, rowSpan); + int written = 0; for (int x = 0; x < width; x++) { - WriteWhitespace(stream, ref bytesWritten); - bytesWritten += stream.WriteDecimal(rowSpan[x].PackedValue); + Utf8Formatter.TryFormat(rowSpan[x].PackedValue, plainSpan.Slice(written), out int bytesWritten, DecimalFormat); + written += bytesWritten; + plainSpan[written++] = Space; } + + plainSpan[written - 1] = NewLine; + stream.Write(plainSpan, 0, written); } } private static void WriteRgb(Configuration configuration, Stream stream, ImageFrame image) where TPixel : unmanaged, IPixel { - int width = image.Size().Width; - int height = image.Size().Height; - int bytesWritten = -1; + int width = image.Width; + int height = image.Height; MemoryAllocator allocator = configuration.MemoryAllocator; using IMemoryOwner row = allocator.Allocate(width); Span rowSpan = row.GetSpan(); + Span plainSpan = stackalloc byte[width * MaxCharsPerPixelRgb]; for (int y = 0; y < height; y++) { @@ -130,27 +149,34 @@ namespace SixLabors.ImageSharp.Formats.Pbm pixelSpan, rowSpan); + int written = 0; for (int x = 0; x < width; x++) { - WriteWhitespace(stream, ref bytesWritten); - bytesWritten += stream.WriteDecimal(rowSpan[x].R); - WriteWhitespace(stream, ref bytesWritten); - bytesWritten += stream.WriteDecimal(rowSpan[x].G); - WriteWhitespace(stream, ref bytesWritten); - bytesWritten += stream.WriteDecimal(rowSpan[x].B); + Utf8Formatter.TryFormat(rowSpan[x].R, plainSpan.Slice(written), out int bytesWritten, DecimalFormat); + written += bytesWritten; + plainSpan[written++] = Space; + Utf8Formatter.TryFormat(rowSpan[x].G, plainSpan.Slice(written), out bytesWritten, DecimalFormat); + written += bytesWritten; + plainSpan[written++] = Space; + Utf8Formatter.TryFormat(rowSpan[x].B, plainSpan.Slice(written), out bytesWritten, DecimalFormat); + written += bytesWritten; + plainSpan[written++] = Space; } + + plainSpan[written - 1] = NewLine; + stream.Write(plainSpan, 0, written); } } private static void WriteWideRgb(Configuration configuration, Stream stream, ImageFrame image) where TPixel : unmanaged, IPixel { - int width = image.Size().Width; - int height = image.Size().Height; - int bytesWritten = -1; + int width = image.Width; + int height = image.Height; MemoryAllocator allocator = configuration.MemoryAllocator; using IMemoryOwner row = allocator.Allocate(width); Span rowSpan = row.GetSpan(); + Span plainSpan = stackalloc byte[width * MaxCharsPerPixelRgbWide]; for (int y = 0; y < height; y++) { @@ -160,27 +186,34 @@ namespace SixLabors.ImageSharp.Formats.Pbm pixelSpan, rowSpan); + int written = 0; for (int x = 0; x < width; x++) { - WriteWhitespace(stream, ref bytesWritten); - bytesWritten += stream.WriteDecimal(rowSpan[x].R); - WriteWhitespace(stream, ref bytesWritten); - bytesWritten += stream.WriteDecimal(rowSpan[x].G); - WriteWhitespace(stream, ref bytesWritten); - bytesWritten += stream.WriteDecimal(rowSpan[x].B); + Utf8Formatter.TryFormat(rowSpan[x].R, plainSpan.Slice(written), out int bytesWritten, DecimalFormat); + written += bytesWritten; + plainSpan[written++] = Space; + Utf8Formatter.TryFormat(rowSpan[x].G, plainSpan.Slice(written), out bytesWritten, DecimalFormat); + written += bytesWritten; + plainSpan[written++] = Space; + Utf8Formatter.TryFormat(rowSpan[x].B, plainSpan.Slice(written), out bytesWritten, DecimalFormat); + written += bytesWritten; + plainSpan[written++] = Space; } + + plainSpan[written - 1] = NewLine; + stream.Write(plainSpan, 0, written); } } private static void WriteBlackAndWhite(Configuration configuration, Stream stream, ImageFrame image) where TPixel : unmanaged, IPixel { - int width = image.Size().Width; - int height = image.Size().Height; - int bytesWritten = -1; + int width = image.Width; + int height = image.Height; MemoryAllocator allocator = configuration.MemoryAllocator; using IMemoryOwner row = allocator.Allocate(width); Span rowSpan = row.GetSpan(); + Span plainSpan = stackalloc byte[width * MaxCharsPerPixelBlackAndWhite]; for (int y = 0; y < height; y++) { @@ -190,38 +223,16 @@ namespace SixLabors.ImageSharp.Formats.Pbm pixelSpan, rowSpan); + int written = 0; for (int x = 0; x < width; x++) { - WriteWhitespace(stream, ref bytesWritten); - if (rowSpan[x].PackedValue > 127) - { - stream.WriteByte(Zero); - } - else - { - stream.WriteByte(One); - } - - bytesWritten++; + byte value = (rowSpan[x].PackedValue > 127) ? Zero : One; + plainSpan[written++] = value; + plainSpan[written++] = Space; } - } - } - private static void WriteWhitespace(Stream stream, ref int bytesWritten) - { - if (bytesWritten > MaxLineLength) - { - stream.WriteByte(NewLine); - bytesWritten = 1; - } - else if (bytesWritten == -1) - { - bytesWritten = 0; - } - else - { - stream.WriteByte(Space); - bytesWritten++; + plainSpan[written - 1] = NewLine; + stream.Write(plainSpan, 0, written); } } } diff --git a/src/ImageSharp/Formats/Pbm/StreamExtensions.cs b/src/ImageSharp/Formats/Pbm/StreamExtensions.cs deleted file mode 100644 index 9851afee0a..0000000000 --- a/src/ImageSharp/Formats/Pbm/StreamExtensions.cs +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) Six Labors. -// Licensed under the Apache License, Version 2.0. - -using System.IO; -using System.Text; - -namespace SixLabors.ImageSharp.Formats.Pbm -{ - internal static class StreamExtensions - { - public static int WriteDecimal(this Stream stream, int value) - { - string str = value.ToString(); - byte[] bytes = Encoding.ASCII.GetBytes(str); - stream.Write(bytes); - return bytes.Length; - } - } -} diff --git a/tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs index 4ff3593877..6c84fba9ee 100644 --- a/tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs @@ -3,7 +3,8 @@ using System.IO; using SixLabors.ImageSharp.Formats.Pbm; - +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; using Xunit; using static SixLabors.ImageSharp.Tests.TestImages.Pbm; @@ -21,7 +22,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Pbm [InlineData(GrayscaleBinaryWide, PbmColorType.Grayscale)] [InlineData(RgbPlain, PbmColorType.Rgb)] [InlineData(RgbBinary, PbmColorType.Rgb)] - public void PpmDecoder_CanDecode(string imagePath, PbmColorType expectedColorType) + public void ImageLoadCanDecode(string imagePath, PbmColorType expectedColorType) { // Arrange var testFile = TestFile.Create(imagePath); @@ -36,5 +37,55 @@ namespace SixLabors.ImageSharp.Tests.Formats.Pbm Assert.NotNull(bitmapMetadata); Assert.Equal(expectedColorType, bitmapMetadata.ColorType); } + + [Theory] + [InlineData(BlackAndWhitePlain)] + [InlineData(BlackAndWhiteBinary)] + [InlineData(GrayscalePlain)] + [InlineData(GrayscaleBinary)] + [InlineData(GrayscaleBinaryWide)] + public void ImageLoadL8CanDecode(string imagePath) + { + // Arrange + var testFile = TestFile.Create(imagePath); + using var stream = new MemoryStream(testFile.Bytes, false); + + // Act + using var image = Image.Load(stream); + + // Assert + Assert.NotNull(image); + } + + [Theory] + [InlineData(RgbPlain)] + [InlineData(RgbBinary)] + public void ImageLoadRgb24CanDecode(string imagePath) + { + // Arrange + var testFile = TestFile.Create(imagePath); + using var stream = new MemoryStream(testFile.Bytes, false); + + // Act + using var image = Image.Load(stream); + + // Assert + Assert.NotNull(image); + } + + [Theory] + [WithFile(BlackAndWhiteBinary, PixelTypes.L8, true)] + [WithFile(GrayscalePlainNormalized, PixelTypes.L8, true)] + [WithFile(GrayscaleBinary, PixelTypes.L8, true)] + [WithFile(GrayscaleBinaryWide, PixelTypes.L16, true)] + [WithFile(RgbPlainNormalized, PixelTypes.Rgb24, false)] + [WithFile(RgbBinary, PixelTypes.Rgb24, false)] + public void DecodeReferenceImage(TestImageProvider provider, bool isGrayscale) + where TPixel : unmanaged, IPixel + { + using Image image = provider.GetImage(); + + image.CompareToReferenceOutput(provider, grayscale: isGrayscale); + } } } diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 67f947ff55..444be63a24 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -874,8 +874,10 @@ namespace SixLabors.ImageSharp.Tests public const string GrayscaleBinary = "Pbm/rings.pgm"; public const string GrayscaleBinaryWide = "Pbm/Gene-UP WebSocket RunImageMask.pgm"; public const string GrayscalePlain = "Pbm/grayscale_plain.pgm"; + public const string GrayscalePlainNormalized = "Pbm/grayscale_plain_normalized.pgm"; public const string RgbBinary = "Pbm/00000_00000.ppm"; public const string RgbPlain = "Pbm/rgb_plain.ppm"; + public const string RgbPlainNormalized = "Pbm/rgb_plain_normalized.ppm"; } } } diff --git a/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L16_Gene-UP WebSocket RunImageMask.png b/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L16_Gene-UP WebSocket RunImageMask.png new file mode 100644 index 0000000000..09bb074a3b --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L16_Gene-UP WebSocket RunImageMask.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:78fc668be9f82c01c277cb2560253b04a1ff74a5af4daaf19327591420a71fec +size 4521 diff --git a/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L8_blackandwhite_binary.png b/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L8_blackandwhite_binary.png new file mode 100644 index 0000000000..d1f1515bb0 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L8_blackandwhite_binary.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1339a8170408a7bcde261617cc599587c8f25c4dc94f780976ee1638879888e9 +size 147 diff --git a/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L8_blackandwhite_plain.png b/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L8_blackandwhite_plain.png new file mode 100644 index 0000000000..3722619230 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L8_blackandwhite_plain.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:82d0397f38971cf90d7c064db332093e686196e244ece1196cca2071d27f0a6f +size 147 diff --git a/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L8_grayscale_plain_normalized.png b/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L8_grayscale_plain_normalized.png new file mode 100644 index 0000000000..9c86c2fc10 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L8_grayscale_plain_normalized.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f8e8b8a1a05e76b1eeb577373c3a6f492e356f0dd58489afded248415cec4a07 +size 145 diff --git a/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L8_rings.png b/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L8_rings.png new file mode 100644 index 0000000000..acf751c28e --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_L8_rings.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:388c86b3dd472ef17fb911ae424b81baeeeff74c4161cf5825eab50698d54348 +size 27884 diff --git a/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_Rgb24_00000_00000.png b/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_Rgb24_00000_00000.png new file mode 100644 index 0000000000..49cc74f3ff --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_Rgb24_00000_00000.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2e3fc46b9f0546941ef95be7b750fb29376a679a921f2581403882b0e76e9caf +size 2250 diff --git a/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_Rgb24_rgb_plain_normalized.png b/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_Rgb24_rgb_plain_normalized.png new file mode 100644 index 0000000000..421a598493 --- /dev/null +++ b/tests/Images/External/ReferenceOutput/PbmDecoderTests/DecodeReferenceImage_Rgb24_rgb_plain_normalized.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c44322c4bf461acea27053057f5241afb029d9a1e66e94dcf1be6f86f7f97727 +size 152 diff --git a/tests/Images/Input/Pbm/grayscale_plain_normalized.pgm b/tests/Images/Input/Pbm/grayscale_plain_normalized.pgm new file mode 100644 index 0000000000..fe03296296 --- /dev/null +++ b/tests/Images/Input/Pbm/grayscale_plain_normalized.pgm @@ -0,0 +1,10 @@ +P2 +24 7 +255 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 51 51 51 51 0 0 119 119 119 119 0 0 187 187 187 187 0 0 255 255 255 255 0 +0 51 0 0 0 0 0 119 0 0 0 0 0 187 0 0 0 0 0 255 0 0 255 0 +0 51 51 51 0 0 0 119 119 119 0 0 0 187 187 187 0 0 0 255 255 255 255 0 +0 51 0 0 0 0 0 119 0 0 0 0 0 187 0 0 0 0 0 255 0 0 0 0 +0 51 0 0 0 0 0 119 119 119 119 0 0 187 187 187 187 0 0 255 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \ No newline at end of file diff --git a/tests/Images/Input/Pbm/rgb_plain_normalized.ppm b/tests/Images/Input/Pbm/rgb_plain_normalized.ppm new file mode 100644 index 0000000000..6289315793 --- /dev/null +++ b/tests/Images/Input/Pbm/rgb_plain_normalized.ppm @@ -0,0 +1,8 @@ +P3 +# example from the man page +4 4 +255 + 0 0 0 0 0 0 0 0 0 255 0 255 + 0 0 0 0 255 119 0 0 0 0 0 0 + 0 0 0 0 0 0 0 255 119 0 0 0 +255 0 255 0 0 0 0 0 0 0 0 0 \ No newline at end of file