From 855ef8cf5de5927a92660ae3d964faa2dc00f019 Mon Sep 17 00:00:00 2001 From: Nikita Balabaev Date: Thu, 10 Aug 2017 15:08:14 +0700 Subject: [PATCH] Add an equivalent for System.Drawing.Image.GetPixelFormatSize() (#258) --- src/ImageSharp/Formats/Bmp/BmpDecoder.cs | 11 +++ src/ImageSharp/Formats/Gif/GifDecoder.cs | 11 +++ src/ImageSharp/Formats/IImageDecoder.cs | 8 ++ src/ImageSharp/Formats/Jpeg/JpegDecoder.cs | 11 +++ .../Formats/Jpeg/JpegDecoderCore.cs | 22 +++++- src/ImageSharp/Formats/Png/PngDecoder.cs | 12 +++ src/ImageSharp/Formats/Png/PngDecoderCore.cs | 74 +++++++++++++++++++ src/ImageSharp/Image/Image.Decode.cs | 14 ++++ src/ImageSharp/Image/Image.FromStream.cs | 27 +++++++ .../Formats/Bmp/BmpDecoderTests.cs | 29 ++++++++ .../Formats/Gif/GifDecoderTests.cs | 15 ++++ .../Formats/Jpg/JpegDecoderTests.cs | 16 ++++ .../Formats/Png/PngDecoderTests.cs | 18 +++++ .../Image/ImageDiscoverMimeType.cs | 7 +- .../ImageSharp.Tests/Image/ImageLoadTests.cs | 26 +++---- tests/ImageSharp.Tests/TestFileSystem.cs | 2 +- tests/ImageSharp.Tests/TestFormat.cs | 15 ++-- tests/ImageSharp.Tests/TestImages.cs | 6 +- .../TestImages/Formats/Bmp/bpp8.bmp | 3 + .../TestImages/Formats/Png/bpp1.png | 3 + .../TestImages/Formats/Png/gray_4bpp.png | 3 + .../TestImages/Formats/Png/palette-8bpp.png | 3 + 22 files changed, 309 insertions(+), 27 deletions(-) create mode 100644 tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs create mode 100644 tests/ImageSharp.Tests/TestImages/Formats/Bmp/bpp8.bmp create mode 100644 tests/ImageSharp.Tests/TestImages/Formats/Png/bpp1.png create mode 100644 tests/ImageSharp.Tests/TestImages/Formats/Png/gray_4bpp.png create mode 100644 tests/ImageSharp.Tests/TestImages/Formats/Png/palette-8bpp.png diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoder.cs b/src/ImageSharp/Formats/Bmp/BmpDecoder.cs index 5baf1b1a5..bdd15c2d7 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoder.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoder.cs @@ -37,5 +37,16 @@ namespace ImageSharp.Formats return new BmpDecoderCore(configuration, this).Decode(stream); } + + /// + public int DetectPixelSize(Configuration configuration, Stream stream) + { + Guard.NotNull(stream, "stream"); + + byte[] buffer = new byte[2]; + stream.Skip(28); + stream.Read(buffer, 0, 2); + return BitConverter.ToInt16(buffer, 0); + } } } diff --git a/src/ImageSharp/Formats/Gif/GifDecoder.cs b/src/ImageSharp/Formats/Gif/GifDecoder.cs index 927289094..4d847c9fe 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoder.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoder.cs @@ -33,5 +33,16 @@ namespace ImageSharp.Formats var decoder = new GifDecoderCore(configuration, this); return decoder.Decode(stream); } + + /// + public int DetectPixelSize(Configuration configuration, Stream stream) + { + Guard.NotNull(stream, "stream"); + + byte[] buffer = new byte[1]; + stream.Skip(10); // Skip the identifier and size + stream.Read(buffer, 0, 1); // Skip the identifier and size + return (buffer[0] & 0x07) + 1; // The lowest 3 bits represent the bit depth minus 1 + } } } diff --git a/src/ImageSharp/Formats/IImageDecoder.cs b/src/ImageSharp/Formats/IImageDecoder.cs index 66eabb1b8..a16ef2612 100644 --- a/src/ImageSharp/Formats/IImageDecoder.cs +++ b/src/ImageSharp/Formats/IImageDecoder.cs @@ -25,5 +25,13 @@ namespace ImageSharp.Formats /// The decoded image Image Decode(Configuration configuration, Stream stream) where TPixel : struct, IPixel; + + /// + /// Detects the image pixel size from the specified stream. + /// + /// The configuration for the image. + /// The containing image data. + /// The color depth, in number of bits per pixel + int DetectPixelSize(Configuration configuration, Stream stream); } } diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs index b3caddeca..8bdbdfe0c 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs @@ -32,5 +32,16 @@ namespace ImageSharp.Formats return decoder.Decode(stream); } } + + /// + public int DetectPixelSize(Configuration configuration, Stream stream) + { + Guard.NotNull(stream, "stream"); + + using (JpegDecoderCore decoder = new JpegDecoderCore(configuration, this)) + { + return decoder.DetectPixelSize(stream); + } + } } } diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 0ce927e51..60c9f1a1d 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -30,6 +30,11 @@ namespace ImageSharp.Formats /// public const int MaxTq = 3; + /// + /// The only supported precision + /// + public const int SupportedPrecision = 8; + // Complex value type field + mutable + available to other classes = the field MUST NOT be private :P #pragma warning disable SA1401 // FieldsMustBePrivate @@ -191,7 +196,7 @@ namespace ImageSharp.Formats public bool IgnoreMetadata { get; private set; } /// - /// Decodes the image from the specified and sets + /// Decodes the image from the specified and sets /// the data to image. /// /// The pixel format. @@ -208,6 +213,17 @@ namespace ImageSharp.Formats return image; } + /// + /// Detects the image pixel size from the specified stream. + /// + /// The containing image data. + /// The color depth, in number of bits per pixel + public int DetectPixelSize(Stream stream) + { + this.ProcessStream(new ImageMetaData(), stream, true); + return this.ComponentCount * SupportedPrecision; + } + /// /// Dispose /// @@ -1196,7 +1212,7 @@ namespace ImageSharp.Formats this.InputProcessor.ReadFull(this.Temp, 0, remaining); // We only support 8-bit precision. - if (this.Temp[0] != 8) + if (this.Temp[0] != SupportedPrecision) { throw new ImageFormatException("Only 8-Bit precision supported."); } @@ -1238,7 +1254,7 @@ namespace ImageSharp.Formats if (h == 3 || v == 3) { - throw new ImageFormatException("Lnsupported subsampling ratio"); + throw new ImageFormatException("Unsupported subsampling ratio"); } switch (this.ComponentCount) diff --git a/src/ImageSharp/Formats/Png/PngDecoder.cs b/src/ImageSharp/Formats/Png/PngDecoder.cs index 61a8cb212..dd71b70cc 100644 --- a/src/ImageSharp/Formats/Png/PngDecoder.cs +++ b/src/ImageSharp/Formats/Png/PngDecoder.cs @@ -56,5 +56,17 @@ namespace ImageSharp.Formats var decoder = new PngDecoderCore(configuration, this); return decoder.Decode(stream); } + + /// + /// Detects the image pixel size from the specified stream. + /// + /// The configuration for the image. + /// The containing image data. + /// The color depth, in number of bits per pixel + public int DetectPixelSize(Configuration configuration, Stream stream) + { + var decoder = new PngDecoderCore(configuration, this); + return decoder.DetectPixelSize(stream); + } } } diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 467d41ff4..7baf35295 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -261,6 +261,58 @@ namespace ImageSharp.Formats } } + /// + /// Detects the image pixel size from the specified stream. + /// + /// The containing image data. + /// The color depth, in number of bits per pixel + public int DetectPixelSize(Stream stream) + { + this.currentStream = stream; + this.currentStream.Skip(8); + try + { + PngChunk currentChunk; + while (!this.isEndChunkReached && (currentChunk = this.ReadChunk()) != null) + { + try + { + switch (currentChunk.Type) + { + case PngChunkTypes.Header: + this.ReadHeaderChunk(currentChunk.Data); + this.ValidateHeader(); + this.isEndChunkReached = true; + break; + case PngChunkTypes.End: + this.isEndChunkReached = true; + break; + } + } + finally + { + // Data is rented in ReadChunkData() + if (currentChunk.Data != null) + { + ArrayPool.Shared.Return(currentChunk.Data); + } + } + } + } + finally + { + this.scanline?.Dispose(); + this.previousScanline?.Dispose(); + } + + if (this.header == null) + { + throw new ImageFormatException("PNG Image hasn't header chunk"); + } + + return this.CalculateBitsPerPixel(); + } + /// /// Converts a byte array to a new array where each value in the original array is represented by the specified number of bits. /// @@ -343,6 +395,28 @@ namespace ImageSharp.Formats this.scanline = Buffer.CreateClean(this.bytesPerScanline); } + /// + /// Calculates the correct number of bits per pixel for the given color type. + /// + /// The + private int CalculateBitsPerPixel() + { + switch (this.pngColorType) + { + case PngColorType.Grayscale: + case PngColorType.Palette: + return this.header.BitDepth; + case PngColorType.GrayscaleWithAlpha: + return this.header.BitDepth * 2; + case PngColorType.Rgb: + return this.header.BitDepth * 3; + case PngColorType.RgbWithAlpha: + return this.header.BitDepth * 4; + default: + throw new NotSupportedException("Unsupported PNG color type"); + } + } + /// /// Calculates the correct number of bytes per pixel for the given color type. /// diff --git a/src/ImageSharp/Image/Image.Decode.cs b/src/ImageSharp/Image/Image.Decode.cs index 101310706..05a01d825 100644 --- a/src/ImageSharp/Image/Image.Decode.cs +++ b/src/ImageSharp/Image/Image.Decode.cs @@ -81,5 +81,19 @@ namespace ImageSharp Image img = decoder.Decode(config, stream); return (img, format); } + + /// + /// Detects the image pixel size. + /// + /// The stream. + /// the configuration. + /// + /// The color depth, in number of bits per pixel or null if suitable decoder not found. + /// + private static int? InternalDetectPixelSize(Stream stream, Configuration config) + { + IImageDecoder decoder = DiscoverDecoder(stream, config, out IImageFormat _); + return decoder?.DetectPixelSize(config, stream); + } } } \ No newline at end of file diff --git a/src/ImageSharp/Image/Image.FromStream.cs b/src/ImageSharp/Image/Image.FromStream.cs index 29d93ae85..032c81c33 100644 --- a/src/ImageSharp/Image/Image.FromStream.cs +++ b/src/ImageSharp/Image/Image.FromStream.cs @@ -39,6 +39,33 @@ namespace ImageSharp return WithSeekableStream(stream, s => InternalDetectFormat(s, config ?? Configuration.Default)); } + /// + /// By reading the header on the provided stream this calculates the images color depth. + /// + /// The image stream to read the header from. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// The color depth, in number of bits per pixel or null if suitable decoder not found + public static int? DetectPixelSize(Stream stream) + { + return DetectPixelSize(null, stream); + } + + /// + /// By reading the header on the provided stream this calculates the images color depth. + /// + /// The configuration. + /// The image stream to read the header from. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// The color depth, in number of bits per pixel or null if suitable decoder not found + public static int? DetectPixelSize(Configuration config, Stream stream) + { + return WithSeekableStream(stream, s => InternalDetectPixelSize(s, config ?? Configuration.Default)); + } + /// /// Create a new instance of the class from the given stream. /// diff --git a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs new file mode 100644 index 000000000..a2eaf6df6 --- /dev/null +++ b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs @@ -0,0 +1,29 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +// ReSharper disable InconsistentNaming +namespace ImageSharp.Tests +{ + using System.IO; + + using Xunit; + + public class BmpDecoderTests + { + [Theory] + [InlineData(TestImages.Bmp.Car, 24)] + [InlineData(TestImages.Bmp.F, 24)] + [InlineData(TestImages.Bmp.NegHeight, 24)] + [InlineData(TestImages.Bmp.Bpp8, 8)] + public void DetectPixelSize(string imagePath, int expectedPixelSize) + { + TestFile testFile = TestFile.Create(imagePath); + using (var stream = new MemoryStream(testFile.Bytes, false)) + { + Assert.Equal(expectedPixelSize, Image.DetectPixelSize(stream)); + } + } + } +} diff --git a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs index 06bfd8990..c952cd799 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs @@ -6,6 +6,7 @@ // ReSharper disable InconsistentNaming namespace ImageSharp.Tests { + using System.IO; using System.Text; using Xunit; @@ -80,5 +81,19 @@ namespace ImageSharp.Tests Assert.Equal("浉条卥慨灲", image.MetaData.Properties[0].Value); } } + + [Theory] + [InlineData(TestImages.Gif.Cheers, 8)] + [InlineData(TestImages.Gif.Giphy, 8)] + [InlineData(TestImages.Gif.Rings, 8)] + [InlineData(TestImages.Gif.Trans, 8)] + public void DetectPixelSize(string imagePath, int expectedPixelSize) + { + TestFile testFile = TestFile.Create(imagePath); + using (var stream = new MemoryStream(testFile.Bytes, false)) + { + Assert.Equal(expectedPixelSize, Image.DetectPixelSize(stream)); + } + } } } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs index 9401d098d..4a9eb4325 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs @@ -152,5 +152,21 @@ namespace ImageSharp.Tests Assert.Null(image.MetaData.ExifProfile); } } + + [Theory] + [InlineData(TestImages.Jpeg.Progressive.Progress, 24)] + [InlineData(TestImages.Jpeg.Progressive.Fb, 24)] + [InlineData(TestImages.Jpeg.Baseline.Cmyk, 32)] + [InlineData(TestImages.Jpeg.Baseline.Ycck, 32)] + [InlineData(TestImages.Jpeg.Baseline.Jpeg400, 8)] + [InlineData(TestImages.Jpeg.Baseline.Snake, 24)] + public void DetectPixelSize(string imagePath, int expectedPixelSize) + { + TestFile testFile = TestFile.Create(imagePath); + using (var stream = new MemoryStream(testFile.Bytes, false)) + { + Assert.Equal(expectedPixelSize, Image.DetectPixelSize(stream)); + } + } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs index ee8a2ee55..fcc0010ec 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs @@ -5,6 +5,7 @@ namespace ImageSharp.Tests { + using System.IO; using System.Text; using Xunit; @@ -82,5 +83,22 @@ namespace ImageSharp.Tests Assert.Equal("潓瑦慷敲", image.MetaData.Properties[0].Name); } } + + [Theory] + [InlineData(TestImages.Png.Bpp1, 1)] + [InlineData(TestImages.Png.Gray4Bpp, 4)] + [InlineData(TestImages.Png.Palette8Bpp, 8)] + [InlineData(TestImages.Png.Pd, 24)] + [InlineData(TestImages.Png.Blur, 32)] + [InlineData(TestImages.Png.Rgb48Bpp, 48)] + [InlineData(TestImages.Png.Rgb48BppInterlaced, 48)] + public void DetectPixelSize(string imagePath, int expectedPixelSize) + { + TestFile testFile = TestFile.Create(imagePath); + using (var stream = new MemoryStream(testFile.Bytes, false)) + { + Assert.Equal(expectedPixelSize, Image.DetectPixelSize(stream)); + } + } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs b/tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs index 59a39c454..0d0d00957 100644 --- a/tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs +++ b/tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs @@ -41,20 +41,20 @@ namespace ImageSharp.Tests this.fileSystem = new Mock(); - this.LocalConfiguration = new Configuration() + this.LocalConfiguration = new Configuration { FileSystem = this.fileSystem.Object }; this.LocalConfiguration.AddImageFormatDetector(this.localMimeTypeDetector.Object); - TestFormat.RegisterGloablTestFormat(); + TestFormat.RegisterGlobalTestFormat(); this.Marker = Guid.NewGuid().ToByteArray(); this.DataStream = TestFormat.GlobalTestFormat.CreateStream(this.Marker); this.FilePath = Guid.NewGuid().ToString(); this.fileSystem.Setup(x => x.OpenRead(this.FilePath)).Returns(this.DataStream); - TestFileSystem.RegisterGloablTestFormat(); + TestFileSystem.RegisterGlobalTestFormat(); TestFileSystem.Global.AddFile(this.FilePath, this.DataStream); } @@ -86,7 +86,6 @@ namespace ImageSharp.Tests Assert.Equal(localImageFormat, type); } - [Fact] public void DiscoverImageFormatStream() { diff --git a/tests/ImageSharp.Tests/Image/ImageLoadTests.cs b/tests/ImageSharp.Tests/Image/ImageLoadTests.cs index bb64ceda3..bfbdd93f1 100644 --- a/tests/ImageSharp.Tests/Image/ImageLoadTests.cs +++ b/tests/ImageSharp.Tests/Image/ImageLoadTests.cs @@ -55,28 +55,28 @@ namespace ImageSharp.Tests this.fileSystem = new Mock(); - this.LocalConfiguration = new Configuration() + this.LocalConfiguration = new Configuration { FileSystem = this.fileSystem.Object }; this.LocalConfiguration.AddImageFormatDetector(this.localMimeTypeDetector.Object); this.LocalConfiguration.SetDecoder(localImageFormatMock.Object, this.localDecoder.Object); - TestFormat.RegisterGloablTestFormat(); + TestFormat.RegisterGlobalTestFormat(); this.Marker = Guid.NewGuid().ToByteArray(); this.DataStream = TestFormat.GlobalTestFormat.CreateStream(this.Marker); this.FilePath = Guid.NewGuid().ToString(); this.fileSystem.Setup(x => x.OpenRead(this.FilePath)).Returns(this.DataStream); - TestFileSystem.RegisterGloablTestFormat(); + TestFileSystem.RegisterGlobalTestFormat(); TestFileSystem.Global.AddFile(this.FilePath, this.DataStream); } [Fact] public void LoadFromStream() { - Image img = Image.Load(this.DataStream); + Image img = Image.Load(this.DataStream); Assert.NotNull(img); @@ -87,7 +87,7 @@ namespace ImageSharp.Tests public void LoadFromNoneSeekableStream() { NoneSeekableStream stream = new NoneSeekableStream(this.DataStream); - Image img = Image.Load(stream); + Image img = Image.Load(stream); Assert.NotNull(img); @@ -112,7 +112,7 @@ namespace ImageSharp.Tests public void LoadFromStreamWithConfig() { Stream stream = new MemoryStream(); - Image img = Image.Load(this.LocalConfiguration, stream); + Image img = Image.Load(this.LocalConfiguration, stream); Assert.NotNull(img); @@ -138,7 +138,7 @@ namespace ImageSharp.Tests public void LoadFromStreamWithDecoder() { Stream stream = new MemoryStream(); - Image img = Image.Load(stream, this.localDecoder.Object); + Image img = Image.Load(stream, this.localDecoder.Object); Assert.NotNull(img); this.localDecoder.Verify(x => x.Decode(Configuration.Default, stream)); @@ -158,7 +158,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytes() { - Image img = Image.Load(this.DataStream.ToArray()); + Image img = Image.Load(this.DataStream.ToArray()); Assert.NotNull(img); @@ -182,7 +182,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithConfig() { - Image img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray()); + Image img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray()); Assert.NotNull(img); @@ -207,7 +207,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithDecoder() { - Image img = Image.Load(this.DataStream.ToArray(), this.localDecoder.Object); + Image img = Image.Load(this.DataStream.ToArray(), this.localDecoder.Object); Assert.NotNull(img); this.localDecoder.Verify(x => x.Decode(Configuration.Default, It.IsAny())); @@ -228,7 +228,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFile() { - Image img = Image.Load(this.DataStream); + Image img = Image.Load(this.DataStream); Assert.NotNull(img); @@ -251,7 +251,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithConfig() { - Image img = Image.Load(this.LocalConfiguration, this.FilePath); + Image img = Image.Load(this.LocalConfiguration, this.FilePath); Assert.NotNull(img); @@ -273,7 +273,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithDecoder() { - Image img = Image.Load(this.FilePath, this.localDecoder.Object); + Image img = Image.Load(this.FilePath, this.localDecoder.Object); Assert.NotNull(img); this.localDecoder.Verify(x => x.Decode(Configuration.Default, this.DataStream)); diff --git a/tests/ImageSharp.Tests/TestFileSystem.cs b/tests/ImageSharp.Tests/TestFileSystem.cs index d43b989f1..875970441 100644 --- a/tests/ImageSharp.Tests/TestFileSystem.cs +++ b/tests/ImageSharp.Tests/TestFileSystem.cs @@ -22,7 +22,7 @@ namespace ImageSharp.Tests public static TestFileSystem Global { get; } = new TestFileSystem(); - public static void RegisterGloablTestFormat() + public static void RegisterGlobalTestFormat() { Configuration.Default.FileSystem = Global; } diff --git a/tests/ImageSharp.Tests/TestFormat.cs b/tests/ImageSharp.Tests/TestFormat.cs index 5a3cd102e..6c2bca367 100644 --- a/tests/ImageSharp.Tests/TestFormat.cs +++ b/tests/ImageSharp.Tests/TestFormat.cs @@ -23,15 +23,15 @@ namespace ImageSharp.Tests { public static TestFormat GlobalTestFormat { get; } = new TestFormat(); - public static void RegisterGloablTestFormat() + public static void RegisterGlobalTestFormat() { Configuration.Default.Configure(GlobalTestFormat); } public TestFormat() { - this.Encoder = new TestEncoder(this); ; - this.Decoder = new TestDecoder(this); ; + this.Encoder = new TestEncoder(this); + this.Decoder = new TestDecoder(this); } public List DecodeCalls { get; } = new List(); @@ -200,10 +200,15 @@ namespace ImageSharp.Tests config = config }); - // TODO record this happend so we an verify it. + // TODO record this happend so we can verify it. return this.testFormat.Sample(); } + public int DetectPixelSize(Configuration configuration, Stream stream) + { + throw new NotImplementedException(); + } + public bool IsSupportedFileFormat(Span header) => testFormat.IsSupportedFileFormat(header); } @@ -222,7 +227,7 @@ namespace ImageSharp.Tests public void Encode(Image image, Stream stream) where TPixel : struct, IPixel { - // TODO record this happend so we an verify it. + // TODO record this happend so we can verify it. } } } diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 3479457cf..53e6ae1ef 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -25,6 +25,9 @@ namespace ImageSharp.Tests public const string Powerpoint = "Png/pp.png"; public const string SplashInterlaced = "Png/splash-interlaced.png"; public const string Interlaced = "Png/interlaced.png"; + public const string Palette8Bpp = "Png/Palette-8bpp.png"; + public const string Bpp1 = "Png/bpp1.png"; + public const string Gray4Bpp = "Png/gray_4bpp.png"; public const string Rgb48Bpp = "Png/rgb-48bpp.png"; public const string Rgb48BppInterlaced = "Png/rgb-48bpp-interlaced.png"; @@ -108,8 +111,9 @@ namespace ImageSharp.Tests { public const string Car = "Bmp/Car.bmp"; public const string F = "Bmp/F.bmp"; + public const string Bpp8 = "Bmp/bpp8.bmp"; public const string NegHeight = "Bmp/neg_height.bmp"; - public static readonly string[] All = { Car, F, NegHeight }; + public static readonly string[] All = { Car, F, NegHeight, Bpp8 }; } public static class Gif diff --git a/tests/ImageSharp.Tests/TestImages/Formats/Bmp/bpp8.bmp b/tests/ImageSharp.Tests/TestImages/Formats/Bmp/bpp8.bmp new file mode 100644 index 000000000..ff422e447 --- /dev/null +++ b/tests/ImageSharp.Tests/TestImages/Formats/Bmp/bpp8.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bf34f4498dfe2a983771d0ff07ef87230de59fea3db8096212b5ad562dfb1011 +size 65002 diff --git a/tests/ImageSharp.Tests/TestImages/Formats/Png/bpp1.png b/tests/ImageSharp.Tests/TestImages/Formats/Png/bpp1.png new file mode 100644 index 000000000..cbfb46bda --- /dev/null +++ b/tests/ImageSharp.Tests/TestImages/Formats/Png/bpp1.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:364e9fac6467570afe2f23e432d4f7df10dd2dd53920d4f2c743ac0f8519f1e0 +size 4403 diff --git a/tests/ImageSharp.Tests/TestImages/Formats/Png/gray_4bpp.png b/tests/ImageSharp.Tests/TestImages/Formats/Png/gray_4bpp.png new file mode 100644 index 000000000..ff4f77fe3 --- /dev/null +++ b/tests/ImageSharp.Tests/TestImages/Formats/Png/gray_4bpp.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e7c6d6cbd959e84001e559702c31e313d065c9cc25808c190c4d4a1f564d4357 +size 7396 diff --git a/tests/ImageSharp.Tests/TestImages/Formats/Png/palette-8bpp.png b/tests/ImageSharp.Tests/TestImages/Formats/Png/palette-8bpp.png new file mode 100644 index 000000000..8943fdeb3 --- /dev/null +++ b/tests/ImageSharp.Tests/TestImages/Formats/Png/palette-8bpp.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7bfbc244f4b0672d6a12a1f73ec062bcf762f229268b99aa4b9ffd8447512471 +size 9171