From 855ef8cf5de5927a92660ae3d964faa2dc00f019 Mon Sep 17 00:00:00 2001 From: Nikita Balabaev Date: Thu, 10 Aug 2017 15:08:14 +0700 Subject: [PATCH 01/14] 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 5baf1b1a5a..bdd15c2d71 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 927289094f..4d847c9fe7 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 66eabb1b82..a16ef2612c 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 b3caddeca7..8bdbdfe0cf 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 0ce927e516..60c9f1a1d5 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 61a8cb2127..dd71b70cc9 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 467d41ff41..7baf35295f 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 1013107062..05a01d825c 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 29d93ae859..032c81c33d 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 0000000000..a2eaf6df62 --- /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 06bfd8990d..c952cd799c 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 9401d098de..4a9eb43252 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 ee8a2ee55b..fcc0010ec7 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 59a39c4542..0d0d00957e 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 bb64ceda34..bfbdd93f17 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 d43b989f10..8759704415 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 5a3cd102e7..6c2bca3678 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 3479457cf8..53e6ae1eff 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 0000000000..ff422e447e --- /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 0000000000..cbfb46bda7 --- /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 0000000000..ff4f77fe39 --- /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 0000000000..8943fdeb37 --- /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 From 53d69674a24d542da696494502fbe550ade7f758 Mon Sep 17 00:00:00 2001 From: Nikita Balabaev Date: Fri, 18 Aug 2017 09:45:02 +0700 Subject: [PATCH 02/14] fixes on code review --- src/ImageSharp/Formats/Bmp/BmpDecoder.cs | 4 ++-- src/ImageSharp/Formats/Gif/GifDecoder.cs | 5 +++-- src/ImageSharp/Formats/IImageDecoder.cs | 4 ++-- src/ImageSharp/Formats/Jpeg/JpegDecoder.cs | 4 ++-- src/ImageSharp/Formats/PixelTypeInfo.cs | 22 +++++++++++++++++++ src/ImageSharp/Formats/Png/PngDecoder.cs | 4 ++-- src/ImageSharp/Image/Image.Decode.cs | 6 ++--- src/ImageSharp/Image/Image.FromStream.cs | 16 +++++++++----- .../Formats/Bmp/BmpDecoderTests.cs | 2 +- .../Formats/Gif/GifDecoderTests.cs | 2 +- .../Formats/Jpg/JpegDecoderTests.cs | 2 +- .../Formats/Png/PngDecoderTests.cs | 2 +- tests/ImageSharp.Tests/TestFormat.cs | 2 +- 13 files changed, 51 insertions(+), 24 deletions(-) create mode 100644 src/ImageSharp/Formats/PixelTypeInfo.cs diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoder.cs b/src/ImageSharp/Formats/Bmp/BmpDecoder.cs index bdd15c2d71..ee6676f5a1 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoder.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoder.cs @@ -39,14 +39,14 @@ namespace ImageSharp.Formats } /// - public int DetectPixelSize(Configuration configuration, Stream stream) + public PixelTypeInfo DetectPixelType(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); + return new PixelTypeInfo(BitConverter.ToInt16(buffer, 0)); } } } diff --git a/src/ImageSharp/Formats/Gif/GifDecoder.cs b/src/ImageSharp/Formats/Gif/GifDecoder.cs index 4d847c9fe7..63eb2eaf42 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoder.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoder.cs @@ -35,14 +35,15 @@ namespace ImageSharp.Formats } /// - public int DetectPixelSize(Configuration configuration, Stream stream) + public PixelTypeInfo DetectPixelType(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 + int bitsPerPixel = (buffer[0] & 0x07) + 1; // The lowest 3 bits represent the bit depth minus 1 + return new PixelTypeInfo(bitsPerPixel); } } } diff --git a/src/ImageSharp/Formats/IImageDecoder.cs b/src/ImageSharp/Formats/IImageDecoder.cs index a16ef2612c..6befafb39e 100644 --- a/src/ImageSharp/Formats/IImageDecoder.cs +++ b/src/ImageSharp/Formats/IImageDecoder.cs @@ -31,7 +31,7 @@ namespace ImageSharp.Formats /// /// The configuration for the image. /// The containing image data. - /// The color depth, in number of bits per pixel - int DetectPixelSize(Configuration configuration, Stream stream); + /// The object + PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream); } } diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs index 8bdbdfe0cf..66e303c01c 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs @@ -34,13 +34,13 @@ namespace ImageSharp.Formats } /// - public int DetectPixelSize(Configuration configuration, Stream stream) + public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream) { Guard.NotNull(stream, "stream"); using (JpegDecoderCore decoder = new JpegDecoderCore(configuration, this)) { - return decoder.DetectPixelSize(stream); + return new PixelTypeInfo(decoder.DetectPixelSize(stream)); } } } diff --git a/src/ImageSharp/Formats/PixelTypeInfo.cs b/src/ImageSharp/Formats/PixelTypeInfo.cs new file mode 100644 index 0000000000..c331543515 --- /dev/null +++ b/src/ImageSharp/Formats/PixelTypeInfo.cs @@ -0,0 +1,22 @@ +namespace ImageSharp.Formats +{ + /// + /// Stores information about pixel + /// + public class PixelTypeInfo + { + /// + /// Initializes a new instance of the class. + /// + /// color depth, in number of bits per pixel + internal PixelTypeInfo(int bitsPerPixel) + { + this.BitsPerPixel = bitsPerPixel; + } + + /// + /// Gets color depth, in number of bits per pixel + /// + public int BitsPerPixel { get; } + } +} diff --git a/src/ImageSharp/Formats/Png/PngDecoder.cs b/src/ImageSharp/Formats/Png/PngDecoder.cs index dd71b70cc9..b7ffe8e72d 100644 --- a/src/ImageSharp/Formats/Png/PngDecoder.cs +++ b/src/ImageSharp/Formats/Png/PngDecoder.cs @@ -63,10 +63,10 @@ namespace ImageSharp.Formats /// 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) + public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream) { var decoder = new PngDecoderCore(configuration, this); - return decoder.DetectPixelSize(stream); + return new PixelTypeInfo(decoder.DetectPixelSize(stream)); } } } diff --git a/src/ImageSharp/Image/Image.Decode.cs b/src/ImageSharp/Image/Image.Decode.cs index 05a01d825c..313224c5fa 100644 --- a/src/ImageSharp/Image/Image.Decode.cs +++ b/src/ImageSharp/Image/Image.Decode.cs @@ -88,12 +88,12 @@ namespace ImageSharp /// The stream. /// the configuration. /// - /// The color depth, in number of bits per pixel or null if suitable decoder not found. + /// The or null if suitable decoder not found. /// - private static int? InternalDetectPixelSize(Stream stream, Configuration config) + private static PixelTypeInfo InternalDetectPixelType(Stream stream, Configuration config) { IImageDecoder decoder = DiscoverDecoder(stream, config, out IImageFormat _); - return decoder?.DetectPixelSize(config, stream); + return decoder?.DetectPixelType(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 032c81c33d..e84d89a912 100644 --- a/src/ImageSharp/Image/Image.FromStream.cs +++ b/src/ImageSharp/Image/Image.FromStream.cs @@ -46,10 +46,12 @@ namespace ImageSharp /// /// 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) + /// + /// The or null if suitable decoder not found. + /// + public static PixelTypeInfo DetectPixelType(Stream stream) { - return DetectPixelSize(null, stream); + return DetectPixelType(null, stream); } /// @@ -60,10 +62,12 @@ namespace ImageSharp /// /// 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) + /// + /// The or null if suitable decoder not found. + /// + public static PixelTypeInfo DetectPixelType(Configuration config, Stream stream) { - return WithSeekableStream(stream, s => InternalDetectPixelSize(s, config ?? Configuration.Default)); + return WithSeekableStream(stream, s => InternalDetectPixelType(s, config ?? Configuration.Default)); } /// diff --git a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs index a2eaf6df62..84edca0014 100644 --- a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs @@ -22,7 +22,7 @@ namespace ImageSharp.Tests TestFile testFile = TestFile.Create(imagePath); using (var stream = new MemoryStream(testFile.Bytes, false)) { - Assert.Equal(expectedPixelSize, Image.DetectPixelSize(stream)); + Assert.Equal(expectedPixelSize, Image.DetectPixelType(stream)?.BitsPerPixel); } } } diff --git a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs index c952cd799c..b7af4525ec 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs @@ -92,7 +92,7 @@ namespace ImageSharp.Tests TestFile testFile = TestFile.Create(imagePath); using (var stream = new MemoryStream(testFile.Bytes, false)) { - Assert.Equal(expectedPixelSize, Image.DetectPixelSize(stream)); + Assert.Equal(expectedPixelSize, Image.DetectPixelType(stream)?.BitsPerPixel); } } } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs index 4a9eb43252..4ac7c5c5b5 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs @@ -165,7 +165,7 @@ namespace ImageSharp.Tests TestFile testFile = TestFile.Create(imagePath); using (var stream = new MemoryStream(testFile.Bytes, false)) { - Assert.Equal(expectedPixelSize, Image.DetectPixelSize(stream)); + Assert.Equal(expectedPixelSize, Image.DetectPixelType(stream)?.BitsPerPixel); } } } diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs index c7147b2261..f9b34e7c7f 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs @@ -97,7 +97,7 @@ namespace ImageSharp.Tests TestFile testFile = TestFile.Create(imagePath); using (var stream = new MemoryStream(testFile.Bytes, false)) { - Assert.Equal(expectedPixelSize, Image.DetectPixelSize(stream)); + Assert.Equal(expectedPixelSize, Image.DetectPixelType(stream)?.BitsPerPixel); } } diff --git a/tests/ImageSharp.Tests/TestFormat.cs b/tests/ImageSharp.Tests/TestFormat.cs index 6c2bca3678..c760b9b98f 100644 --- a/tests/ImageSharp.Tests/TestFormat.cs +++ b/tests/ImageSharp.Tests/TestFormat.cs @@ -204,7 +204,7 @@ namespace ImageSharp.Tests return this.testFormat.Sample(); } - public int DetectPixelSize(Configuration configuration, Stream stream) + public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream) { throw new NotImplementedException(); } From e4a684d6d82b2cc8607dc7fb50a2e5dcaa06b69d Mon Sep 17 00:00:00 2001 From: Nikita Balabaev Date: Thu, 24 Aug 2017 16:16:23 +0700 Subject: [PATCH 03/14] update packages --- src/ImageSharp/ImageSharp.csproj | 8 ++++---- tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj | 2 +- tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs | 6 +++--- tests/ImageSharp.Tests/ImageSharp.Tests.csproj | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index 8733131a74..2748275556 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -38,13 +38,13 @@ All - + - - + + - + ..\..\ImageSharp.ruleset diff --git a/tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj b/tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj index 72593a0da4..6228afd9ea 100644 --- a/tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj +++ b/tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj @@ -10,7 +10,7 @@ - + diff --git a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs index 0537ffda0e..7b9d28f039 100644 --- a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs @@ -3,10 +3,10 @@ // Licensed under the Apache License, Version 2.0. // -using ImageSharp.Formats; - namespace ImageSharp.Tests { + using System.IO; + using ImageSharp.PixelFormats; using Xunit; @@ -38,4 +38,4 @@ namespace ImageSharp.Tests } } } -} \ No newline at end of file +} diff --git a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj index b0429d9ede..bd20ba7c1c 100644 --- a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj +++ b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj @@ -10,7 +10,7 @@ - + From f8a490c716b1208efee6c41a2646797b91ecb103 Mon Sep 17 00:00:00 2001 From: Nikita Balabaev Date: Thu, 24 Aug 2017 17:36:40 +0700 Subject: [PATCH 04/14] fix test image filename --- tests/ImageSharp.Tests/TestImages.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index c80fde6d07..8960de8868 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -25,7 +25,7 @@ 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 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"; From 4b8e4d4c41d4741b8d20e0807d5b3d9e105945e1 Mon Sep 17 00:00:00 2001 From: Nikita Balabaev Date: Tue, 26 Sep 2017 16:58:11 +0700 Subject: [PATCH 05/14] fix bugs after merge --- .../Formats/Jpeg/GolangPort/OrigJpegDecoder.cs | 11 +++++++++++ src/ImageSharp/Formats/Jpeg/JpegDecoder.cs | 3 +-- .../Formats/Jpeg/PdfJsPort/PdfJsJpegDecoder.cs | 6 ++++++ src/ImageSharp/Formats/PixelTypeInfo.cs | 2 +- tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs | 2 ++ tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs | 2 ++ .../ReferenceCodecs/SystemDrawingReferenceDecoder.cs | 8 ++++++++ .../TestUtilities/Tests/TestImageProviderTests.cs | 10 ++++++++++ .../TestImages/Formats => Images/Input}/Bmp/bpp8.bmp | 0 .../TestImages/Formats => Images/Input}/Png/bpp1.png | 0 .../Formats => Images/Input}/Png/gray_4bpp.png | 0 .../Formats => Images/Input}/Png/palette-8bpp.png | 0 12 files changed, 41 insertions(+), 3 deletions(-) rename tests/{ImageSharp.Tests/TestImages/Formats => Images/Input}/Bmp/bpp8.bmp (100%) rename tests/{ImageSharp.Tests/TestImages/Formats => Images/Input}/Png/bpp1.png (100%) rename tests/{ImageSharp.Tests/TestImages/Formats => Images/Input}/Png/gray_4bpp.png (100%) rename tests/{ImageSharp.Tests/TestImages/Formats => Images/Input}/Png/palette-8bpp.png (100%) diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoder.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoder.cs index 13be70e30b..97a424c415 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoder.cs @@ -27,5 +27,16 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort return decoder.Decode(stream); } } + + /// + public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream) + { + Guard.NotNull(stream, "stream"); + + using (var decoder = new OrigJpegDecoderCore(configuration, this)) + { + return new PixelTypeInfo(decoder.DetectPixelSize(stream)); + } + } } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs index d4ec048d32..6845314c1c 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs @@ -4,7 +4,6 @@ using System.IO; using SixLabors.ImageSharp.Formats.Jpeg.GolangPort; -using SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Jpeg @@ -36,7 +35,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg { Guard.NotNull(stream, "stream"); - using (JpegDecoderCore decoder = new JpegDecoderCore(configuration, this)) + using (var decoder = new OrigJpegDecoderCore(configuration, this)) { return new PixelTypeInfo(decoder.DetectPixelSize(stream)); } diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoder.cs index 37ce0151f3..1458d54e5f 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoder.cs @@ -27,5 +27,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort return decoder.Decode(stream); } } + + /// + public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream) + { + throw new System.NotImplementedException(); + } } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/PixelTypeInfo.cs b/src/ImageSharp/Formats/PixelTypeInfo.cs index c331543515..d4b72a00b4 100644 --- a/src/ImageSharp/Formats/PixelTypeInfo.cs +++ b/src/ImageSharp/Formats/PixelTypeInfo.cs @@ -1,4 +1,4 @@ -namespace ImageSharp.Formats +namespace SixLabors.ImageSharp.Formats { /// /// Stores information about pixel diff --git a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs index c6598fab81..b6f894386b 100644 --- a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs @@ -8,6 +8,8 @@ using Xunit; namespace SixLabors.ImageSharp.Tests { + using System.IO; + using SixLabors.ImageSharp.Formats.Bmp; public class BmpDecoderTests : FileTestBase diff --git a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs index f57b0f128d..a13730eb6c 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs @@ -11,6 +11,8 @@ using Xunit; // ReSharper disable InconsistentNaming namespace SixLabors.ImageSharp.Tests { + using System.IO; + public class GifDecoderTests { private const PixelTypes PixelTypes = Tests.PixelTypes.Rgba32 | Tests.PixelTypes.RgbaVector | Tests.PixelTypes.Argb32; diff --git a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceDecoder.cs b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceDecoder.cs index 23ff61eb3d..d36fd2a14d 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceDecoder.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceDecoder.cs @@ -45,5 +45,13 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs } } } + + public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream) + { + using (var sourceBitmap = new System.Drawing.Bitmap(stream)) + { + return new PixelTypeInfo(System.Drawing.Image.GetPixelFormatSize(sourceBitmap.PixelFormat)); + } + } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs index 88d7fa2b86..24becce2ed 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs @@ -83,6 +83,11 @@ namespace SixLabors.ImageSharp.Tests return new Image(42, 42); } + public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream) + { + throw new NotImplementedException(); + } + // Couldn't make xUnit happy without this hackery: private static ConcurrentDictionary invocationCounts = new ConcurrentDictionary(); @@ -145,6 +150,11 @@ namespace SixLabors.ImageSharp.Tests return new Image(42, 42); } + public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream) + { + throw new NotImplementedException(); + } + private static ConcurrentDictionary invocationCounts = new ConcurrentDictionary(); private string callerName = null; diff --git a/tests/ImageSharp.Tests/TestImages/Formats/Bmp/bpp8.bmp b/tests/Images/Input/Bmp/bpp8.bmp similarity index 100% rename from tests/ImageSharp.Tests/TestImages/Formats/Bmp/bpp8.bmp rename to tests/Images/Input/Bmp/bpp8.bmp diff --git a/tests/ImageSharp.Tests/TestImages/Formats/Png/bpp1.png b/tests/Images/Input/Png/bpp1.png similarity index 100% rename from tests/ImageSharp.Tests/TestImages/Formats/Png/bpp1.png rename to tests/Images/Input/Png/bpp1.png diff --git a/tests/ImageSharp.Tests/TestImages/Formats/Png/gray_4bpp.png b/tests/Images/Input/Png/gray_4bpp.png similarity index 100% rename from tests/ImageSharp.Tests/TestImages/Formats/Png/gray_4bpp.png rename to tests/Images/Input/Png/gray_4bpp.png diff --git a/tests/ImageSharp.Tests/TestImages/Formats/Png/palette-8bpp.png b/tests/Images/Input/Png/palette-8bpp.png similarity index 100% rename from tests/ImageSharp.Tests/TestImages/Formats/Png/palette-8bpp.png rename to tests/Images/Input/Png/palette-8bpp.png From 7c566eafc4b2c84c88e34a610186c94dee619efc Mon Sep 17 00:00:00 2001 From: Nikita Balabaev Date: Tue, 26 Sep 2017 17:05:38 +0700 Subject: [PATCH 06/14] remove duplicates in .csproj files --- src/ImageSharp/ImageSharp.csproj | 1 - tests/ImageSharp.Tests/ImageSharp.Tests.csproj | 4 ---- 2 files changed, 5 deletions(-) diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index 509270cccb..45d0a70b81 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -40,7 +40,6 @@ All - diff --git a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj index b016fb3bae..e8a6e8c596 100644 --- a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj +++ b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj @@ -16,10 +16,6 @@ - - - - From df7f5de6bf6a3fe13826e66820699c443a7d3c35 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Tue, 16 Jan 2018 19:04:56 +0000 Subject: [PATCH 07/14] Create LICENSE --- LICENSE | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000..2eeb57968e --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2018 Six Labors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. From 51959f760d034ab7807e04f7f2f54bac1c4ad669 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Tue, 16 Jan 2018 19:05:41 +0000 Subject: [PATCH 08/14] Delete APACHE-2.0-LICENSE.txt --- APACHE-2.0-LICENSE.txt | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 APACHE-2.0-LICENSE.txt diff --git a/APACHE-2.0-LICENSE.txt b/APACHE-2.0-LICENSE.txt deleted file mode 100644 index a666c6e078..0000000000 --- a/APACHE-2.0-LICENSE.txt +++ /dev/null @@ -1,13 +0,0 @@ -Copyright 2012 James South - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. \ No newline at end of file From 15a8bf20ffb7f9644ec3996e3b11e9c104b0a3de Mon Sep 17 00:00:00 2001 From: denisivan0v Date: Wed, 17 Jan 2018 16:55:53 +0700 Subject: [PATCH 09/14] Added an API to read base image information without decoding it - intoduced base IImage interface - introduced IImageInfoDetector interface - Image.DetectPixelType method expanded to Identify method that returns IImage --- src/ImageSharp/Formats/Bmp/BmpDecoder.cs | 11 +- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 134 +++++++------ src/ImageSharp/Formats/Gif/GifDecoder.cs | 17 +- src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 182 +++++++++++++----- .../Sections/GifLogicalScreenDescriptor.cs | 5 + src/ImageSharp/Formats/IImageDecoder.cs | 10 - src/ImageSharp/Formats/IImageInfoDetector.cs | 21 ++ .../Jpeg/GolangPort/OrigJpegDecoder.cs | 6 +- .../Jpeg/GolangPort/OrigJpegDecoderCore.cs | 14 +- src/ImageSharp/Formats/Jpeg/JpegDecoder.cs | 6 +- .../Jpeg/PdfJsPort/PdfJsJpegDecoder.cs | 6 - .../Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs | 2 +- src/ImageSharp/Formats/PixelTypeInfo.cs | 6 +- src/ImageSharp/Formats/Png/PngDecoder.cs | 15 +- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 69 ++++--- src/ImageSharp/Image/IImage.cs | 31 +++ src/ImageSharp/Image/Image.Decode.cs | 10 +- src/ImageSharp/Image/Image.FromStream.cs | 16 +- src/ImageSharp/Image/ImageFrameCollection.cs | 4 +- src/ImageSharp/Image/ImageInfo.cs | 24 +++ src/ImageSharp/Image/Image{TPixel}.cs | 31 +-- .../Processors/Transforms/ResizeProcessor.cs | 2 +- .../Formats/Bmp/BmpDecoderTests.cs | 2 +- .../Formats/Gif/GifDecoderTests.cs | 2 +- .../Formats/Jpg/JpegDecoderTests.cs | 2 +- .../Formats/Png/PngDecoderTests.cs | 2 +- tests/ImageSharp.Tests/TestFormat.cs | 5 - .../SystemDrawingReferenceDecoder.cs | 21 +- .../Tests/TestImageProviderTests.cs | 10 - 29 files changed, 418 insertions(+), 248 deletions(-) create mode 100644 src/ImageSharp/Formats/IImageInfoDetector.cs create mode 100644 src/ImageSharp/Image/IImage.cs create mode 100644 src/ImageSharp/Image/ImageInfo.cs diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoder.cs b/src/ImageSharp/Formats/Bmp/BmpDecoder.cs index b976d66797..e252e63406 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoder.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoder.cs @@ -1,8 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System; -using System.Collections.Generic; using System.IO; using SixLabors.ImageSharp.PixelFormats; @@ -23,7 +21,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// Formats will be supported in a later releases. We advise always /// to use only 24 Bit Windows bitmaps. /// - public sealed class BmpDecoder : IImageDecoder, IBmpDecoderOptions + public sealed class BmpDecoder : IImageDecoder, IBmpDecoderOptions, IImageInfoDetector { /// public Image Decode(Configuration configuration, Stream stream) @@ -36,14 +34,11 @@ namespace SixLabors.ImageSharp.Formats.Bmp } /// - public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream) + public IImage Identify(Configuration configuration, Stream stream) { Guard.NotNull(stream, "stream"); - byte[] buffer = new byte[2]; - stream.Skip(28); - stream.Read(buffer, 0, 2); - return new PixelTypeInfo(BitConverter.ToInt16(buffer, 0)); + return new BmpDecoderCore(configuration, this).Identify(stream); } } } diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index 04176e0333..ef9e761643 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -5,6 +5,7 @@ using System; using System.IO; using System.Runtime.CompilerServices; using SixLabors.ImageSharp.Memory; +using SixLabors.ImageSharp.MetaData; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Formats.Bmp @@ -94,62 +95,9 @@ namespace SixLabors.ImageSharp.Formats.Bmp public Image Decode(Stream stream) where TPixel : struct, IPixel { - this.currentStream = stream; - try { - this.ReadFileHeader(); - this.ReadInfoHeader(); - - // 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 - // is the upper-left corner and not the lower-left.The inverted flag - // indicates a lower-left origin.Our code will be outputting an - // upper-left origin pixel array. - bool inverted = false; - if (this.infoHeader.Height < 0) - { - inverted = true; - this.infoHeader.Height = -this.infoHeader.Height; - } - - int colorMapSize = -1; - - if (this.infoHeader.ClrUsed == 0) - { - if (this.infoHeader.BitsPerPixel == 1 || - this.infoHeader.BitsPerPixel == 4 || - this.infoHeader.BitsPerPixel == 8) - { - colorMapSize = (int)Math.Pow(2, this.infoHeader.BitsPerPixel) * 4; - } - } - else - { - colorMapSize = this.infoHeader.ClrUsed * 4; - } - - byte[] palette = null; - - if (colorMapSize > 0) - { - // 256 * 4 - if (colorMapSize > 1024) - { - throw new ImageFormatException($"Invalid bmp colormap size '{colorMapSize}'"); - } - - palette = new byte[colorMapSize]; - - this.currentStream.Read(palette, 0, colorMapSize); - } - - if (this.infoHeader.Width > int.MaxValue || this.infoHeader.Height > int.MaxValue) - { - throw new ArgumentOutOfRangeException( - $"The input bitmap '{this.infoHeader.Width}x{this.infoHeader.Height}' is " - + $"bigger then the max allowed size '{int.MaxValue}x{int.MaxValue}'"); - } + this.ReadImageHeaders(stream, out bool inverted, out byte[] palette); var image = new Image(this.configuration, this.infoHeader.Width, this.infoHeader.Height); using (PixelAccessor pixels = image.Lock()) @@ -192,6 +140,16 @@ namespace SixLabors.ImageSharp.Formats.Bmp } } + /// + /// Reads the image base information from the specified stream. + /// + /// The containing image data. + public IImage Identify(Stream stream) + { + this.ReadImageHeaders(stream, out _, out _); + return new ImageInfo(new PixelTypeInfo(this.infoHeader.BitsPerPixel), this.infoHeader.Width, this.infoHeader.Height, new ImageMetaData()); + } + /// /// Returns the y- value based on the given height. /// @@ -624,5 +582,73 @@ namespace SixLabors.ImageSharp.Formats.Bmp Offset = BitConverter.ToInt32(data, 10) }; } + + /// + /// Reads the and from the stream and sets the corresponding fields. + /// + private void ReadImageHeaders(Stream stream, out bool inverted, out byte[] palette) + { + this.currentStream = stream; + + try + { + this.ReadFileHeader(); + this.ReadInfoHeader(); + + // 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 + // is the upper-left corner and not the lower-left.The inverted flag + // indicates a lower-left origin.Our code will be outputting an + // upper-left origin pixel array. + inverted = false; + if (this.infoHeader.Height < 0) + { + inverted = true; + this.infoHeader.Height = -this.infoHeader.Height; + } + + int colorMapSize = -1; + + if (this.infoHeader.ClrUsed == 0) + { + if (this.infoHeader.BitsPerPixel == 1 || + this.infoHeader.BitsPerPixel == 4 || + this.infoHeader.BitsPerPixel == 8) + { + colorMapSize = (int)Math.Pow(2, this.infoHeader.BitsPerPixel) * 4; + } + } + else + { + colorMapSize = this.infoHeader.ClrUsed * 4; + } + + palette = null; + + if (colorMapSize > 0) + { + // 256 * 4 + if (colorMapSize > 1024) + { + throw new ImageFormatException($"Invalid bmp colormap size '{colorMapSize}'"); + } + + palette = new byte[colorMapSize]; + + this.currentStream.Read(palette, 0, colorMapSize); + } + + if (this.infoHeader.Width > int.MaxValue || this.infoHeader.Height > int.MaxValue) + { + throw new ArgumentOutOfRangeException( + $"The input bitmap '{this.infoHeader.Width}x{this.infoHeader.Height}' is " + + $"bigger then the max allowed size '{int.MaxValue}x{int.MaxValue}'"); + } + } + catch (IndexOutOfRangeException e) + { + throw new ImageFormatException("Bitmap does not have a valid format.", e); + } + } } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/Gif/GifDecoder.cs b/src/ImageSharp/Formats/Gif/GifDecoder.cs index 20bebb1d34..ccb6cf92c5 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoder.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoder.cs @@ -1,8 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System; -using System.Collections.Generic; using System.IO; using System.Text; using SixLabors.ImageSharp.PixelFormats; @@ -12,7 +10,7 @@ namespace SixLabors.ImageSharp.Formats.Gif /// /// Decoder for generating an image out of a gif encoded stream. /// - public sealed class GifDecoder : IImageDecoder, IGifDecoderOptions + public sealed class GifDecoder : IImageDecoder, IGifDecoderOptions, IImageInfoDetector { /// /// Gets or sets a value indicating whether the metadata should be ignored when the image is being decoded. @@ -33,20 +31,17 @@ namespace SixLabors.ImageSharp.Formats.Gif public Image Decode(Configuration configuration, Stream stream) where TPixel : struct, IPixel { - var decoder = new GifDecoderCore(configuration, this); - return decoder.Decode(stream); + var decoder = new GifDecoderCore(configuration, this); + return decoder.Decode(stream); } /// - public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream) + public IImage Identify(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 - int bitsPerPixel = (buffer[0] & 0x07) + 1; // The lowest 3 bits represent the bit depth minus 1 - return new PixelTypeInfo(bitsPerPixel); + var decoder = new GifDecoderCore(configuration, this); + return decoder.Identify(stream); } } } diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index ae20be7d5d..dbca49f068 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -17,9 +17,7 @@ namespace SixLabors.ImageSharp.Formats.Gif /// /// Performs the gif decoding operation. /// - /// The pixel format. - internal sealed class GifDecoderCore - where TPixel : struct, IPixel + internal sealed class GifDecoderCore { /// /// The temp buffer used to reduce allocations. @@ -46,11 +44,6 @@ namespace SixLabors.ImageSharp.Formats.Gif /// private int globalColorTableLength; - /// - /// The previous frame. - /// - private ImageFrame previousFrame; - /// /// The area to restore. /// @@ -72,12 +65,7 @@ namespace SixLabors.ImageSharp.Formats.Gif private ImageMetaData metaData; /// - /// The image to decode the information to. - /// - private Image image; - - /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The configuration. /// The decoder options. @@ -107,28 +95,84 @@ namespace SixLabors.ImageSharp.Formats.Gif /// /// Decodes the stream to the image. /// + /// The pixel format. /// The stream containing image data. /// The decoded image - public Image Decode(Stream stream) + public Image Decode(Stream stream) + where TPixel : struct, IPixel { + Image image = null; + ImageFrame previousFrame = null; try { - this.metaData = new ImageMetaData(); + this.ReadLogicalScreenDescriptorAndGlobalColorTable(stream); - this.currentStream = stream; + // Loop though the respective gif parts and read the data. + int nextFlag = stream.ReadByte(); + while (nextFlag != GifConstants.Terminator) + { + if (nextFlag == GifConstants.ImageLabel) + { + if (previousFrame != null && this.DecodingMode == FrameDecodingMode.First) + { + break; + } - // Skip the identifier - this.currentStream.Skip(6); - this.ReadLogicalScreenDescriptor(); + this.ReadFrame(ref image, ref previousFrame); + } + else if (nextFlag == GifConstants.ExtensionIntroducer) + { + int label = stream.ReadByte(); + switch (label) + { + case GifConstants.GraphicControlLabel: + this.ReadGraphicalControlExtension(); + break; + case GifConstants.CommentLabel: + this.ReadComments(); + break; + case GifConstants.ApplicationExtensionLabel: - if (this.logicalScreenDescriptor.GlobalColorTableFlag) - { - this.globalColorTableLength = this.logicalScreenDescriptor.GlobalColorTableSize * 3; - this.globalColorTable = Buffer.CreateClean(this.globalColorTableLength); + // The application extension length should be 11 but we've got test images that incorrectly + // set this to 252. + int appLength = stream.ReadByte(); + this.Skip(appLength); // No need to read. + break; + case GifConstants.PlainTextLabel: + int plainLength = stream.ReadByte(); + this.Skip(plainLength); // Not supported by any known decoder. + break; + } + } + else if (nextFlag == GifConstants.EndIntroducer) + { + break; + } - // Read the global color table from the stream - stream.Read(this.globalColorTable.Array, 0, this.globalColorTableLength); + nextFlag = stream.ReadByte(); + if (nextFlag == -1) + { + break; + } } + } + finally + { + this.globalColorTable?.Dispose(); + } + + return image; + } + + /// + /// Reads the image base information from the specified stream. + /// + /// The containing image data. + public IImage Identify(Stream stream) + { + try + { + this.ReadLogicalScreenDescriptorAndGlobalColorTable(stream); // Loop though the respective gif parts and read the data. int nextFlag = stream.ReadByte(); @@ -136,12 +180,19 @@ namespace SixLabors.ImageSharp.Formats.Gif { if (nextFlag == GifConstants.ImageLabel) { - if (this.previousFrame != null && this.DecodingMode == FrameDecodingMode.First) + GifImageDescriptor imageDescriptor = this.ReadImageDescriptor(); + + // Determine the color table for this frame. If there is a local one, use it otherwise use the global color table. + if (imageDescriptor.LocalColorTableFlag) { - break; + int length = imageDescriptor.LocalColorTableSize * 3; + + // Skip local color table block + this.Skip(length); } - this.ReadFrame(); + // Skip image block + this.Skip(0); } else if (nextFlag == GifConstants.ExtensionIntroducer) { @@ -149,7 +200,9 @@ namespace SixLabors.ImageSharp.Formats.Gif switch (label) { case GifConstants.GraphicControlLabel: - this.ReadGraphicalControlExtension(); + + // Skip graphic control extension block + this.Skip(0); break; case GifConstants.CommentLabel: this.ReadComments(); @@ -184,7 +237,7 @@ namespace SixLabors.ImageSharp.Formats.Gif this.globalColorTable?.Dispose(); } - return this.image; + return new ImageInfo(new PixelTypeInfo(this.logicalScreenDescriptor.BitsPerPixel), this.logicalScreenDescriptor.Width, this.logicalScreenDescriptor.Height, this.metaData); } /// @@ -242,6 +295,7 @@ namespace SixLabors.ImageSharp.Formats.Gif { Width = BitConverter.ToInt16(this.buffer, 0), Height = BitConverter.ToInt16(this.buffer, 2), + BitsPerPixel = (this.buffer[4] & 0x07) + 1, // The lowest 3 bits represent the bit depth minus 1 BackgroundColorIndex = this.buffer[5], PixelAspectRatio = this.buffer[6], GlobalColorTableFlag = ((packed & 0x80) >> 7) == 1, @@ -308,7 +362,11 @@ namespace SixLabors.ImageSharp.Formats.Gif /// /// Reads an individual gif frame. /// - private void ReadFrame() + /// The pixel format. + /// The image to decode the information to. + /// The previous frame. + private void ReadFrame(ref Image image, ref ImageFrame previousFrame) + where TPixel : struct, IPixel { GifImageDescriptor imageDescriptor = this.ReadImageDescriptor(); @@ -327,7 +385,7 @@ namespace SixLabors.ImageSharp.Formats.Gif indices = Buffer.CreateClean(imageDescriptor.Width * imageDescriptor.Height); this.ReadFrameIndices(imageDescriptor, indices); - this.ReadFrameColors(indices, localColorTable ?? this.globalColorTable, imageDescriptor); + this.ReadFrameColors(ref image, ref previousFrame, indices, localColorTable ?? this.globalColorTable, imageDescriptor); // Skip any remaining blocks this.Skip(0); @@ -357,10 +415,14 @@ namespace SixLabors.ImageSharp.Formats.Gif /// /// Reads the frames colors, mapping indices to colors. /// + /// The pixel format. + /// The image to decode the information to. + /// The previous frame. /// The indexed pixels. /// The color table containing the available colors. /// The - private void ReadFrameColors(Span indices, Span colorTable, GifImageDescriptor descriptor) + private void ReadFrameColors(ref Image image, ref ImageFrame previousFrame, Span indices, Span colorTable, GifImageDescriptor descriptor) + where TPixel : struct, IPixel { int imageWidth = this.logicalScreenDescriptor.Width; int imageHeight = this.logicalScreenDescriptor.Height; @@ -371,30 +433,30 @@ namespace SixLabors.ImageSharp.Formats.Gif ImageFrame imageFrame; - if (this.previousFrame == null) + if (previousFrame == null) { // This initializes the image to become fully transparent because the alpha channel is zero. - this.image = new Image(this.configuration, imageWidth, imageHeight, this.metaData); + image = new Image(this.configuration, new PixelTypeInfo(this.logicalScreenDescriptor.BitsPerPixel), imageWidth, imageHeight, this.metaData); - this.SetFrameMetaData(this.image.Frames.RootFrame.MetaData); + this.SetFrameMetaData(image.Frames.RootFrame.MetaData); - imageFrame = this.image.Frames.RootFrame; + imageFrame = image.Frames.RootFrame; } else { if (this.graphicsControlExtension != null && this.graphicsControlExtension.DisposalMethod == DisposalMethod.RestoreToPrevious) { - prevFrame = this.previousFrame; + prevFrame = previousFrame; } - currentFrame = this.image.Frames.AddFrame(this.previousFrame); // This clones the frame and adds it the collection + currentFrame = image.Frames.AddFrame(previousFrame); // This clones the frame and adds it the collection this.SetFrameMetaData(currentFrame.MetaData); imageFrame = currentFrame; - this.RestoreToBackground(imageFrame); + this.RestoreToBackground(imageFrame, image.Width, image.Height); } int i = 0; @@ -466,11 +528,11 @@ namespace SixLabors.ImageSharp.Formats.Gif if (prevFrame != null) { - this.previousFrame = prevFrame; + previousFrame = prevFrame; return; } - this.previousFrame = currentFrame ?? this.image.Frames.RootFrame; + previousFrame = currentFrame ?? image.Frames.RootFrame; if (this.graphicsControlExtension != null && this.graphicsControlExtension.DisposalMethod == DisposalMethod.RestoreToBackground) @@ -482,8 +544,12 @@ namespace SixLabors.ImageSharp.Formats.Gif /// /// Restores the current frame area to the background. /// + /// The pixel format. /// The frame. - private void RestoreToBackground(ImageFrame frame) + /// Width of the image. + /// Height of the image. + private void RestoreToBackground(ImageFrame frame, int imageWidth, int imageHeight) + where TPixel : struct, IPixel { if (this.restoreArea == null) { @@ -491,8 +557,8 @@ namespace SixLabors.ImageSharp.Formats.Gif } // Optimization for when the size of the frame is the same as the image size. - if (this.restoreArea.Value.Width == this.image.Width && - this.restoreArea.Value.Height == this.image.Height) + if (this.restoreArea.Value.Width == imageWidth && + this.restoreArea.Value.Height == imageHeight) { using (PixelAccessor pixelAccessor = frame.Lock()) { @@ -533,5 +599,29 @@ namespace SixLabors.ImageSharp.Formats.Gif meta.DisposalMethod = this.graphicsControlExtension.DisposalMethod; } } + + /// + /// Reads the logical screen descriptor and global color table blocks + /// + /// The stream containing image data. + private void ReadLogicalScreenDescriptorAndGlobalColorTable(Stream stream) + { + this.metaData = new ImageMetaData(); + + this.currentStream = stream; + + // Skip the identifier + this.currentStream.Skip(6); + this.ReadLogicalScreenDescriptor(); + + if (this.logicalScreenDescriptor.GlobalColorTableFlag) + { + this.globalColorTableLength = this.logicalScreenDescriptor.GlobalColorTableSize * 3; + this.globalColorTable = Buffer.CreateClean(this.globalColorTableLength); + + // Read the global color table from the stream + stream.Read(this.globalColorTable.Array, 0, this.globalColorTableLength); + } + } } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/Gif/Sections/GifLogicalScreenDescriptor.cs b/src/ImageSharp/Formats/Gif/Sections/GifLogicalScreenDescriptor.cs index b1109c3e25..9fea5b1126 100644 --- a/src/ImageSharp/Formats/Gif/Sections/GifLogicalScreenDescriptor.cs +++ b/src/ImageSharp/Formats/Gif/Sections/GifLogicalScreenDescriptor.cs @@ -21,6 +21,11 @@ namespace SixLabors.ImageSharp.Formats.Gif /// rendered in the displaying device. /// public short Height { get; set; } + + /// + /// Gets or sets the color depth, in number of bits per pixel. + /// + public int BitsPerPixel { get; set; } /// /// Gets or sets the index at the Global Color Table for the Background Color. diff --git a/src/ImageSharp/Formats/IImageDecoder.cs b/src/ImageSharp/Formats/IImageDecoder.cs index 0d16c7db22..ffc40314d8 100644 --- a/src/ImageSharp/Formats/IImageDecoder.cs +++ b/src/ImageSharp/Formats/IImageDecoder.cs @@ -1,8 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System; -using System.Collections.Generic; using System.IO; using SixLabors.ImageSharp.PixelFormats; @@ -22,13 +20,5 @@ namespace SixLabors.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 object - PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream); } } diff --git a/src/ImageSharp/Formats/IImageInfoDetector.cs b/src/ImageSharp/Formats/IImageInfoDetector.cs new file mode 100644 index 0000000000..4e1dfc84f6 --- /dev/null +++ b/src/ImageSharp/Formats/IImageInfoDetector.cs @@ -0,0 +1,21 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.IO; + +namespace SixLabors.ImageSharp.Formats +{ + /// + /// Used for detecting the image base information without decoding it. + /// + public interface IImageInfoDetector + { + /// + /// Reads the image base information from the specified stream. + /// + /// The configuration for the image. + /// The containing image data. + /// The object + IImage Identify(Configuration configuration, Stream stream); + } +} \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoder.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoder.cs index 97a424c415..7b082a3d6a 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoder.cs @@ -9,7 +9,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort /// /// Image decoder for generating an image out of a jpg stream. /// - internal sealed class OrigJpegDecoder : IImageDecoder, IJpegDecoderOptions + internal sealed class OrigJpegDecoder : IImageDecoder, IJpegDecoderOptions, IImageInfoDetector { /// /// Gets or sets a value indicating whether the metadata should be ignored when the image is being decoded. @@ -29,13 +29,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort } /// - public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream) + public IImage Identify(Configuration configuration, Stream stream) { Guard.NotNull(stream, "stream"); using (var decoder = new OrigJpegDecoderCore(configuration, this)) { - return new PixelTypeInfo(decoder.DetectPixelSize(stream)); + return decoder.Identify(stream); } } } diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs index 92e8557286..b7cad3281e 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs @@ -127,6 +127,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort IEnumerable IRawJpegData.Components => this.Components; + /// + /// Gets the color depth, in number of bits per pixel. + /// + public int BitsPerPixel => this.ComponentCount * SupportedPrecision; + /// /// Gets the image height /// @@ -193,14 +198,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort } /// - /// Detects the image pixel size from the specified stream. + /// Reads the image base information from the specified stream. /// /// The containing image data. - /// The color depth, in number of bits per pixel - public int DetectPixelSize(Stream stream) + public IImage Identify(Stream stream) { this.ParseStream(stream, true); - return this.ComponentCount * SupportedPrecision; + return new ImageInfo(new PixelTypeInfo(this.BitsPerPixel), this.ImageWidth, this.ImageHeight, this.MetaData); } /// @@ -785,7 +789,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort { using (var postProcessor = new JpegImagePostProcessor(this)) { - var image = new Image(this.configuration, this.ImageWidth, this.ImageHeight, this.MetaData); + var image = new Image(this.configuration, new PixelTypeInfo(this.BitsPerPixel), this.ImageWidth, this.ImageHeight, this.MetaData); postProcessor.PostProcess(image.Frames.RootFrame); return image; } diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs index 6845314c1c..ee7d7e6996 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs @@ -11,7 +11,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg /// /// Image decoder for generating an image out of a jpg stream. /// - public sealed class JpegDecoder : IImageDecoder, IJpegDecoderOptions + public sealed class JpegDecoder : IImageDecoder, IJpegDecoderOptions, IImageInfoDetector { /// /// Gets or sets a value indicating whether the metadata should be ignored when the image is being decoded. @@ -31,13 +31,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg } /// - public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream) + public IImage Identify(Configuration configuration, Stream stream) { Guard.NotNull(stream, "stream"); using (var decoder = new OrigJpegDecoderCore(configuration, this)) { - return new PixelTypeInfo(decoder.DetectPixelSize(stream)); + return decoder.Identify(stream); } } } diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoder.cs index 1458d54e5f..37ce0151f3 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoder.cs @@ -27,11 +27,5 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort return decoder.Decode(stream); } } - - /// - public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream) - { - throw new System.NotImplementedException(); - } } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs index 211c24d208..c93adac2d4 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs @@ -159,7 +159,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort this.QuantizeAndInverseAllComponents(); - var image = new Image(this.configuration, this.ImageWidth, this.ImageHeight, metadata); + var image = new Image(this.configuration, null, this.ImageWidth, this.ImageHeight, metadata); this.FillPixelData(image.Frames.RootFrame); this.AssignResolution(image); return image; diff --git a/src/ImageSharp/Formats/PixelTypeInfo.cs b/src/ImageSharp/Formats/PixelTypeInfo.cs index d4b72a00b4..a2a2ce9ce2 100644 --- a/src/ImageSharp/Formats/PixelTypeInfo.cs +++ b/src/ImageSharp/Formats/PixelTypeInfo.cs @@ -1,21 +1,21 @@ namespace SixLabors.ImageSharp.Formats { /// - /// Stores information about pixel + /// Stores information about pixel. /// public class PixelTypeInfo { /// /// Initializes a new instance of the class. /// - /// color depth, in number of bits per pixel + /// Color depth, in number of bits per pixel. internal PixelTypeInfo(int bitsPerPixel) { this.BitsPerPixel = bitsPerPixel; } /// - /// Gets color depth, in number of bits per pixel + /// Gets color depth, in number of bits per pixel. /// public int BitsPerPixel { get; } } diff --git a/src/ImageSharp/Formats/Png/PngDecoder.cs b/src/ImageSharp/Formats/Png/PngDecoder.cs index abb41770f9..8e110732ee 100644 --- a/src/ImageSharp/Formats/Png/PngDecoder.cs +++ b/src/ImageSharp/Formats/Png/PngDecoder.cs @@ -1,8 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System; -using System.Collections.Generic; using System.IO; using System.Text; using SixLabors.ImageSharp.PixelFormats; @@ -29,7 +27,7 @@ namespace SixLabors.ImageSharp.Formats.Png /// /// /// - public sealed class PngDecoder : IImageDecoder, IPngDecoderOptions + public sealed class PngDecoder : IImageDecoder, IPngDecoderOptions, IImageInfoDetector { /// /// Gets or sets a value indicating whether the metadata should be ignored when the image is being decoded. @@ -55,16 +53,11 @@ namespace SixLabors.ImageSharp.Formats.Png 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 PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream) + /// + public IImage Identify(Configuration configuration, Stream stream) { var decoder = new PngDecoderCore(configuration, this); - return new PixelTypeInfo(decoder.DetectPixelSize(stream)); + return decoder.Identify(stream); } } } diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 52b571761b..e4c554437e 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -237,7 +237,7 @@ namespace SixLabors.ImageSharp.Formats.Png deframeStream.AllocateNewBytes(currentChunk.Length); this.ReadScanlines(deframeStream.CompressedStream, image.Frames.RootFrame); - stream.Read(this.crcBuffer, 0, 4); + this.currentStream.Read(this.crcBuffer, 0, 4); break; case PngChunkTypes.Palette: byte[] pal = new byte[currentChunk.Length]; @@ -279,42 +279,50 @@ namespace SixLabors.ImageSharp.Formats.Png } /// - /// Detects the image pixel size from the specified stream. + /// Reads the image base information from the specified stream. /// /// The containing image data. - /// The color depth, in number of bits per pixel - public int DetectPixelSize(Stream stream) + public IImage Identify(Stream stream) { + var metadata = new ImageMetaData(); this.currentStream = stream; this.currentStream.Skip(8); try { - PngChunk currentChunk; - while (!this.isEndChunkReached && (currentChunk = this.ReadChunk()) != null) + PngChunk currentChunk; + while (!this.isEndChunkReached && (currentChunk = this.ReadChunk()) != null) + { + try { - try + switch (currentChunk.Type) { - switch (currentChunk.Type) - { - case PngChunkTypes.Header: - this.ReadHeaderChunk(currentChunk.Data); - this.ValidateHeader(); - this.isEndChunkReached = true; - break; - case PngChunkTypes.End: - this.isEndChunkReached = true; - break; - } + case PngChunkTypes.Header: + this.ReadHeaderChunk(currentChunk.Data); + this.ValidateHeader(); + break; + case PngChunkTypes.Physical: + this.ReadPhysicalChunk(metadata, currentChunk.Data); + break; + case PngChunkTypes.Data: + this.SkipChunkDataAndCrc(currentChunk); + break; + case PngChunkTypes.Text: + this.ReadTextChunk(metadata, currentChunk.Data, currentChunk.Length); + break; + case PngChunkTypes.End: + this.isEndChunkReached = true; + break; } - finally + } + finally + { + // Data is rented in ReadChunkData() + if (currentChunk.Data != null) { - // Data is rented in ReadChunkData() - if (currentChunk.Data != null) - { - ArrayPool.Shared.Return(currentChunk.Data); - } + ArrayPool.Shared.Return(currentChunk.Data); } } + } } finally { @@ -327,7 +335,7 @@ namespace SixLabors.ImageSharp.Formats.Png throw new ImageFormatException("PNG Image hasn't header chunk"); } - return this.CalculateBitsPerPixel(); + return new ImageInfo(new PixelTypeInfo(this.CalculateBitsPerPixel()), this.header.Width, this.header.Height, metadata); } /// @@ -418,7 +426,7 @@ namespace SixLabors.ImageSharp.Formats.Png private void InitializeImage(ImageMetaData metadata, out Image image) where TPixel : struct, IPixel { - image = new Image(this.configuration, this.header.Width, this.header.Height, metadata); + image = new Image(this.configuration, new PixelTypeInfo(this.CalculateBitsPerPixel()), this.header.Width, this.header.Height, metadata); this.bytesPerPixel = this.CalculateBytesPerPixel(); this.bytesPerScanline = this.CalculateScanlineLength(this.header.Width) + 1; this.bytesPerSample = 1; @@ -1255,6 +1263,15 @@ namespace SixLabors.ImageSharp.Formats.Png } } + /// + /// Skips the chunk data and the cycle redundancy chunk read from the data. + /// + private void SkipChunkDataAndCrc(PngChunk chunk) + { + this.currentStream.Skip(chunk.Length); + this.currentStream.Skip(4); + } + /// /// Reads the chunk data from the stream. /// diff --git a/src/ImageSharp/Image/IImage.cs b/src/ImageSharp/Image/IImage.cs new file mode 100644 index 0000000000..f840c78f00 --- /dev/null +++ b/src/ImageSharp/Image/IImage.cs @@ -0,0 +1,31 @@ +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.MetaData; + +namespace SixLabors.ImageSharp +{ + /// + /// Represents the base image abstraction. + /// + public interface IImage + { + /// + /// Gets information about pixel. + /// + PixelTypeInfo PixelType { get; } + + /// + /// Gets the width. + /// + int Width { get; } + + /// + /// Gets the height. + /// + int Height { get; } + + /// + /// Gets the meta data of the image. + /// + ImageMetaData MetaData { get; } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Image/Image.Decode.cs b/src/ImageSharp/Image/Image.Decode.cs index da2e036057..8f379cb109 100644 --- a/src/ImageSharp/Image/Image.Decode.cs +++ b/src/ImageSharp/Image/Image.Decode.cs @@ -81,17 +81,17 @@ namespace SixLabors.ImageSharp } /// - /// Detects the image pixel size. + /// Reads the image base information. /// /// The stream. /// the configuration. /// - /// The or null if suitable decoder not found. + /// The or null if suitable info detector not found. /// - private static PixelTypeInfo InternalDetectPixelType(Stream stream, Configuration config) + private static IImage InternalIdentity(Stream stream, Configuration config) { - IImageDecoder decoder = DiscoverDecoder(stream, config, out IImageFormat _); - return decoder?.DetectPixelType(config, stream); + var detector = DiscoverDecoder(stream, config, out IImageFormat _) as IImageInfoDetector; + return detector?.Identify(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 2e0832b81f..0233efb208 100644 --- a/src/ImageSharp/Image/Image.FromStream.cs +++ b/src/ImageSharp/Image/Image.FromStream.cs @@ -37,22 +37,22 @@ namespace SixLabors.ImageSharp } /// - /// By reading the header on the provided stream this calculates the images color depth. + /// By reading the header on the provided stream this reads the image base information. /// /// The image stream to read the header from. /// /// Thrown if the stream is not readable nor seekable. /// /// - /// The or null if suitable decoder not found. + /// The or null if suitable info detector not found. /// - public static PixelTypeInfo DetectPixelType(Stream stream) + public static IImage Identify(Stream stream) { - return DetectPixelType(null, stream); + return Identify(null, stream); } /// - /// By reading the header on the provided stream this calculates the images color depth. + /// By reading the header on the provided stream this reads the image base information. /// /// The configuration. /// The image stream to read the header from. @@ -60,11 +60,11 @@ namespace SixLabors.ImageSharp /// Thrown if the stream is not readable nor seekable. /// /// - /// The or null if suitable decoder not found. + /// The or null if suitable info detector not found. /// - public static PixelTypeInfo DetectPixelType(Configuration config, Stream stream) + public static IImage Identify(Configuration config, Stream stream) { - return WithSeekableStream(stream, s => InternalDetectPixelType(s, config ?? Configuration.Default)); + return WithSeekableStream(stream, s => InternalIdentity(s, config ?? Configuration.Default)); } /// diff --git a/src/ImageSharp/Image/ImageFrameCollection.cs b/src/ImageSharp/Image/ImageFrameCollection.cs index 3e9bb03435..ececcea895 100644 --- a/src/ImageSharp/Image/ImageFrameCollection.cs +++ b/src/ImageSharp/Image/ImageFrameCollection.cs @@ -129,7 +129,7 @@ namespace SixLabors.ImageSharp this.frames.Remove(frame); - return new Image(this.parent.GetConfiguration(), this.parent.MetaData.Clone(), new[] { frame }); + return new Image(this.parent.GetConfiguration(), this.parent.PixelType, this.parent.MetaData.Clone(), new[] { frame }); } /// @@ -137,7 +137,7 @@ namespace SixLabors.ImageSharp { ImageFrame frame = this[index]; ImageFrame clonedFrame = frame.Clone(); - return new Image(this.parent.GetConfiguration(), this.parent.MetaData.Clone(), new[] { clonedFrame }); + return new Image(this.parent.GetConfiguration(), this.parent.PixelType, this.parent.MetaData.Clone(), new[] { clonedFrame }); } /// diff --git a/src/ImageSharp/Image/ImageInfo.cs b/src/ImageSharp/Image/ImageInfo.cs new file mode 100644 index 0000000000..11dcc3e752 --- /dev/null +++ b/src/ImageSharp/Image/ImageInfo.cs @@ -0,0 +1,24 @@ +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.MetaData; + +namespace SixLabors.ImageSharp +{ + internal sealed class ImageInfo : IImage + { + public ImageInfo(PixelTypeInfo pixelType, int width, int height, ImageMetaData metaData) + { + this.PixelType = pixelType; + this.Width = width; + this.Height = height; + this.MetaData = metaData; + } + + public PixelTypeInfo PixelType { get; } + + public int Width { get; } + + public int Height { get; } + + public ImageMetaData MetaData { get; } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Image/Image{TPixel}.cs b/src/ImageSharp/Image/Image{TPixel}.cs index 482971e540..2d0448d08f 100644 --- a/src/ImageSharp/Image/Image{TPixel}.cs +++ b/src/ImageSharp/Image/Image{TPixel}.cs @@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp /// Encapsulates an image, which consists of the pixel data for a graphics image and its attributes. /// /// The pixel format. - public sealed partial class Image : IDisposable, IConfigurable + public sealed partial class Image : IImage, IDisposable, IConfigurable where TPixel : struct, IPixel { private Configuration configuration; @@ -33,7 +33,7 @@ namespace SixLabors.ImageSharp /// The width of the image in pixels. /// The height of the image in pixels. public Image(Configuration configuration, int width, int height) - : this(configuration, width, height, new ImageMetaData()) + : this(configuration, null, width, height, new ImageMetaData()) { } @@ -55,12 +55,14 @@ namespace SixLabors.ImageSharp /// /// The configuration providing initialization code which allows extending the library. /// + /// The information about pixel of the 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, PixelTypeInfo pixelType, int width, int height, ImageMetaData metadata) { this.configuration = configuration ?? Configuration.Default; + this.PixelType = pixelType; this.MetaData = metadata ?? new ImageMetaData(); this.frames = new ImageFrameCollection(this, width, height); } @@ -70,11 +72,13 @@ namespace SixLabors.ImageSharp /// with the height and the width of the image. /// /// The configuration providing initialization code which allows extending the library. + /// The information about pixel of the image. /// The images metadata. /// The frames that will be owned by this image instance. - internal Image(Configuration configuration, ImageMetaData metadata, IEnumerable> frames) + internal Image(Configuration configuration, PixelTypeInfo pixelType, ImageMetaData metadata, IEnumerable> frames) { this.configuration = configuration ?? Configuration.Default; + this.PixelType = pixelType; this.MetaData = metadata ?? new ImageMetaData(); this.frames = new ImageFrameCollection(this, frames); @@ -84,20 +88,17 @@ namespace SixLabors.ImageSharp /// Gets the pixel buffer. /// Configuration IConfigurable.Configuration => this.configuration; + + /// + public PixelTypeInfo PixelType { get; } - /// - /// Gets the width. - /// + /// public int Width => this.frames.RootFrame.Width; - /// - /// Gets the height. - /// + /// public int Height => this.frames.RootFrame.Height; - /// - /// Gets the meta data of the image. - /// + /// public ImageMetaData MetaData { get; private set; } = new ImageMetaData(); /// @@ -144,7 +145,7 @@ namespace SixLabors.ImageSharp public Image Clone() { IEnumerable> clonedFrames = this.frames.Select(x => x.Clone()); - return new Image(this.configuration, this.MetaData.Clone(), clonedFrames); + return new Image(this.configuration, this.PixelType, this.MetaData.Clone(), clonedFrames); } /// @@ -162,7 +163,7 @@ namespace SixLabors.ImageSharp where TPixel2 : struct, IPixel { IEnumerable> clonedFrames = this.frames.Select(x => x.CloneAs()); - var target = new Image(this.configuration, this.MetaData.Clone(), clonedFrames); + var target = new Image(this.configuration, this.PixelType, this.MetaData.Clone(), clonedFrames); return target; } diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs index 17b42c5040..f1fb0086ff 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs @@ -60,7 +60,7 @@ namespace SixLabors.ImageSharp.Processing.Processors // For resize we know we are going to populate every pixel with fresh data and we want a different target size so // let's manually clone an empty set of images at the correct target and then have the base class process them in turn. IEnumerable> frames = source.Frames.Select(x => new ImageFrame(this.Width, this.Height, x.MetaData.Clone())); // this will create places holders - var image = new Image(config, source.MetaData.Clone(), frames); // base the place holder images in to prevent a extra frame being added + var image = new Image(config, source.PixelType, source.MetaData.Clone(), frames); // base the place holder images in to prevent a extra frame being added return image; } diff --git a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs index 17a559a94f..2c0121803b 100644 --- a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs @@ -49,7 +49,7 @@ namespace SixLabors.ImageSharp.Tests TestFile testFile = TestFile.Create(imagePath); using (var stream = new MemoryStream(testFile.Bytes, false)) { - Assert.Equal(expectedPixelSize, Image.DetectPixelType(stream)?.BitsPerPixel); + Assert.Equal(expectedPixelSize, Image.Identify(stream)?.PixelType?.BitsPerPixel); } } } diff --git a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs index 8838e4f711..9a095548a7 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs @@ -130,7 +130,7 @@ namespace SixLabors.ImageSharp.Tests TestFile testFile = TestFile.Create(imagePath); using (var stream = new MemoryStream(testFile.Bytes, false)) { - Assert.Equal(expectedPixelSize, Image.DetectPixelType(stream)?.BitsPerPixel); + Assert.Equal(expectedPixelSize, Image.Identify(stream)?.PixelType?.BitsPerPixel); } } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs index f912a90ae6..cb1987aef4 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs @@ -398,7 +398,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg TestFile testFile = TestFile.Create(imagePath); using (var stream = new MemoryStream(testFile.Bytes, false)) { - Assert.Equal(expectedPixelSize, Image.DetectPixelType(stream)?.BitsPerPixel); + Assert.Equal(expectedPixelSize, Image.Identify(stream)?.PixelType?.BitsPerPixel); } } } diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs index b2b4b2f030..248e0a5eea 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs @@ -197,7 +197,7 @@ namespace SixLabors.ImageSharp.Tests TestFile testFile = TestFile.Create(imagePath); using (var stream = new MemoryStream(testFile.Bytes, false)) { - Assert.Equal(expectedPixelSize, Image.DetectPixelType(stream)?.BitsPerPixel); + Assert.Equal(expectedPixelSize, Image.Identify(stream)?.PixelType?.BitsPerPixel); } } diff --git a/tests/ImageSharp.Tests/TestFormat.cs b/tests/ImageSharp.Tests/TestFormat.cs index c12ae5aca4..078b708dfd 100644 --- a/tests/ImageSharp.Tests/TestFormat.cs +++ b/tests/ImageSharp.Tests/TestFormat.cs @@ -201,11 +201,6 @@ namespace SixLabors.ImageSharp.Tests return this.testFormat.Sample(); } - public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream) - { - throw new NotImplementedException(); - } - public bool IsSupportedFileFormat(Span header) => testFormat.IsSupportedFileFormat(header); } diff --git a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceDecoder.cs b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceDecoder.cs index d36fd2a14d..719d1b7583 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceDecoder.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceDecoder.cs @@ -1,19 +1,17 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System; -using System.Drawing; -using System.Drawing.Drawing2D; + using System.IO; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.PixelFormats; -using PixelFormat = System.Drawing.Imaging.PixelFormat; - namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs { - public class SystemDrawingReferenceDecoder : IImageDecoder + using SixLabors.ImageSharp.MetaData; + + public class SystemDrawingReferenceDecoder : IImageDecoder, IImageInfoDetector { public static SystemDrawingReferenceDecoder Instance { get; } = new SystemDrawingReferenceDecoder(); @@ -22,7 +20,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs { using (var sourceBitmap = new System.Drawing.Bitmap(stream)) { - if (sourceBitmap.PixelFormat == PixelFormat.Format32bppArgb) + if (sourceBitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb) { return SystemDrawingBridge.FromFromArgb32SystemDrawingBitmap(sourceBitmap); } @@ -32,12 +30,12 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs sourceBitmap.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { - using (var g = Graphics.FromImage(convertedBitmap)) + using (var g = System.Drawing.Graphics.FromImage(convertedBitmap)) { g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; - g.PixelOffsetMode = PixelOffsetMode.HighQuality; + g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; g.DrawImage(sourceBitmap, 0, 0, sourceBitmap.Width, sourceBitmap.Height); } @@ -46,11 +44,12 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs } } - public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream) + public IImage Identify(Configuration configuration, Stream stream) { using (var sourceBitmap = new System.Drawing.Bitmap(stream)) { - return new PixelTypeInfo(System.Drawing.Image.GetPixelFormatSize(sourceBitmap.PixelFormat)); + var pixelType = new PixelTypeInfo(System.Drawing.Image.GetPixelFormatSize(sourceBitmap.PixelFormat)); + return new ImageInfo(pixelType, sourceBitmap.Width, sourceBitmap.Height, new ImageMetaData()); } } } diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs index 0c54f8a3c5..e3249fae9f 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs @@ -83,11 +83,6 @@ namespace SixLabors.ImageSharp.Tests return new Image(42, 42); } - public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream) - { - throw new NotImplementedException(); - } - // Couldn't make xUnit happy without this hackery: private static ConcurrentDictionary invocationCounts = new ConcurrentDictionary(); @@ -150,11 +145,6 @@ namespace SixLabors.ImageSharp.Tests return new Image(42, 42); } - public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream) - { - throw new NotImplementedException(); - } - private static ConcurrentDictionary invocationCounts = new ConcurrentDictionary(); private string callerName = null; From d31a5224937c892fb57124e015d059e777351022 Mon Sep 17 00:00:00 2001 From: denisivan0v Date: Wed, 17 Jan 2018 17:07:48 +0700 Subject: [PATCH 10/14] codestyle fixes --- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 2 +- src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 4 ++-- .../Formats/Gif/Sections/GifLogicalScreenDescriptor.cs | 2 +- src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs | 2 +- src/ImageSharp/Image/ImageInfo.cs | 2 +- src/ImageSharp/Image/Image{TPixel}.cs | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index ef9e761643..cb510ac05a 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -149,7 +149,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp this.ReadImageHeaders(stream, out _, out _); return new ImageInfo(new PixelTypeInfo(this.infoHeader.BitsPerPixel), this.infoHeader.Width, this.infoHeader.Height, new ImageMetaData()); } - + /// /// Returns the y- value based on the given height. /// diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index dbca49f068..257274c92c 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -186,7 +186,7 @@ namespace SixLabors.ImageSharp.Formats.Gif if (imageDescriptor.LocalColorTableFlag) { int length = imageDescriptor.LocalColorTableSize * 3; - + // Skip local color table block this.Skip(length); } @@ -200,7 +200,7 @@ namespace SixLabors.ImageSharp.Formats.Gif switch (label) { case GifConstants.GraphicControlLabel: - + // Skip graphic control extension block this.Skip(0); break; diff --git a/src/ImageSharp/Formats/Gif/Sections/GifLogicalScreenDescriptor.cs b/src/ImageSharp/Formats/Gif/Sections/GifLogicalScreenDescriptor.cs index 9fea5b1126..05f232a4be 100644 --- a/src/ImageSharp/Formats/Gif/Sections/GifLogicalScreenDescriptor.cs +++ b/src/ImageSharp/Formats/Gif/Sections/GifLogicalScreenDescriptor.cs @@ -21,7 +21,7 @@ namespace SixLabors.ImageSharp.Formats.Gif /// rendered in the displaying device. /// public short Height { get; set; } - + /// /// Gets or sets the color depth, in number of bits per pixel. /// diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs index b7cad3281e..ef7a4234f8 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs @@ -131,7 +131,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort /// Gets the color depth, in number of bits per pixel. /// public int BitsPerPixel => this.ComponentCount * SupportedPrecision; - + /// /// Gets the image height /// diff --git a/src/ImageSharp/Image/ImageInfo.cs b/src/ImageSharp/Image/ImageInfo.cs index 11dcc3e752..0d8daaf1a8 100644 --- a/src/ImageSharp/Image/ImageInfo.cs +++ b/src/ImageSharp/Image/ImageInfo.cs @@ -12,7 +12,7 @@ namespace SixLabors.ImageSharp this.Height = height; this.MetaData = metaData; } - + public PixelTypeInfo PixelType { get; } public int Width { get; } diff --git a/src/ImageSharp/Image/Image{TPixel}.cs b/src/ImageSharp/Image/Image{TPixel}.cs index 2d0448d08f..9b20e3dfab 100644 --- a/src/ImageSharp/Image/Image{TPixel}.cs +++ b/src/ImageSharp/Image/Image{TPixel}.cs @@ -88,7 +88,7 @@ namespace SixLabors.ImageSharp /// Gets the pixel buffer. /// Configuration IConfigurable.Configuration => this.configuration; - + /// public PixelTypeInfo PixelType { get; } From 1fc1a5d9d985309c9658b142bafbb6da3468a366 Mon Sep 17 00:00:00 2001 From: denisivan0v Date: Wed, 17 Jan 2018 17:11:13 +0700 Subject: [PATCH 11/14] codestyle fix --- src/ImageSharp/Image/IImage.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Image/IImage.cs b/src/ImageSharp/Image/IImage.cs index f840c78f00..58e5d50e51 100644 --- a/src/ImageSharp/Image/IImage.cs +++ b/src/ImageSharp/Image/IImage.cs @@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp /// Gets the width. /// int Width { get; } - + /// /// Gets the height. /// From 35e2a08348865fcb6f0315879d4ea40153502ef0 Mon Sep 17 00:00:00 2001 From: denisivan0v Date: Thu, 18 Jan 2018 17:13:54 +0700 Subject: [PATCH 12/14] bugfix in GifDecoderCore.Identity --- src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 11 ---- .../Formats/GeneralFormatTests.cs | 59 +++++++++++++++++++ .../ImageSharp.Tests/ImageSharp.Tests.csproj | 1 + 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index 257274c92c..7a08b4194e 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -180,17 +180,6 @@ namespace SixLabors.ImageSharp.Formats.Gif { if (nextFlag == GifConstants.ImageLabel) { - GifImageDescriptor imageDescriptor = this.ReadImageDescriptor(); - - // Determine the color table for this frame. If there is a local one, use it otherwise use the global color table. - if (imageDescriptor.LocalColorTableFlag) - { - int length = imageDescriptor.LocalColorTableSize * 3; - - // Skip local color table block - this.Skip(length); - } - // Skip image block this.Skip(0); } diff --git a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs index 473bc2b523..97128e2c93 100644 --- a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs +++ b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs @@ -3,11 +3,19 @@ using System.IO; using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.Formats.Bmp; +using SixLabors.ImageSharp.Formats.Gif; +using SixLabors.ImageSharp.Formats.Jpeg; +using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.PixelFormats; using Xunit; namespace SixLabors.ImageSharp.Tests { + using System; + + + public class GeneralFormatTests : FileTestBase { [Theory] @@ -146,5 +154,56 @@ namespace SixLabors.ImageSharp.Tests } } } + + [Theory] + [InlineData(10, 10, "png")] + [InlineData(100, 100, "png")] + [InlineData(100, 10, "png")] + [InlineData(10, 100, "png")] + [InlineData(10, 10, "gif")] + [InlineData(100, 100, "gif")] + [InlineData(100, 10, "gif")] + [InlineData(10, 100, "gif")] + [InlineData(10, 10, "bmp")] + [InlineData(100, 100, "bmp")] + [InlineData(100, 10, "bmp")] + [InlineData(10, 100, "bmp")] + [InlineData(10, 10, "jpg")] + [InlineData(100, 100, "jpg")] + [InlineData(100, 10, "jpg")] + [InlineData(10, 100, "jpg")] + public void CanIdentifyImageLoadedFromBytes(int width, int height, string format) + { + using (Image image = Image.LoadPixelData(new Rgba32[width * height], width, height)) + { + using (var memoryStream = new MemoryStream()) + { + image.Save(memoryStream, GetEncoder(format)); + memoryStream.Position = 0; + + var imageInfo = Image.Identify(memoryStream); + + Assert.Equal(imageInfo.Width, width); + Assert.Equal(imageInfo.Height, height); + } + } + } + + private static IImageEncoder GetEncoder(string format) + { + switch (format) + { + case "png": + return new PngEncoder(); + case "gif": + return new GifEncoder(); + case "bmp": + return new BmpEncoder(); + case "jpg": + return new JpegEncoder(); + default: + throw new ArgumentOutOfRangeException(nameof(format), format, null); + } + } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj index 2f45e4c83a..7e0329ef46 100644 --- a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj +++ b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj @@ -17,6 +17,7 @@ + From abaa16799944a7267cc732c3215809e335dff02a Mon Sep 17 00:00:00 2001 From: denisivan0v Date: Fri, 19 Jan 2018 14:18:01 +0700 Subject: [PATCH 13/14] changes on code review --- src/ImageSharp/Formats/Bmp/BmpDecoder.cs | 2 +- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 4 +- src/ImageSharp/Formats/Gif/GifDecoder.cs | 2 +- src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 6 +- src/ImageSharp/Formats/IImageInfoDetector.cs | 6 +- .../Jpeg/GolangPort/OrigJpegDecoder.cs | 2 +- .../Jpeg/GolangPort/OrigJpegDecoderCore.cs | 6 +- src/ImageSharp/Formats/Jpeg/JpegDecoder.cs | 2 +- .../Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs | 2 +- src/ImageSharp/Formats/PixelTypeInfo.cs | 2 +- src/ImageSharp/Formats/Png/PngDecoder.cs | 2 +- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 6 +- src/ImageSharp/Image/IImage.cs | 26 +- src/ImageSharp/Image/IImageInfo.cs | 31 +++ src/ImageSharp/Image/Image.Decode.cs | 4 +- src/ImageSharp/Image/Image.FromStream.cs | 12 +- src/ImageSharp/Image/ImageFrameCollection.cs | 4 +- src/ImageSharp/Image/ImageInfo.cs | 16 +- src/ImageSharp/Image/Image{TPixel}.cs | 18 +- src/ImageSharp/ImageSharp.csproj | 236 +++++++++--------- .../Processors/Transforms/ResizeProcessor.cs | 2 +- .../SystemDrawingReferenceDecoder.cs | 5 +- 22 files changed, 208 insertions(+), 188 deletions(-) create mode 100644 src/ImageSharp/Image/IImageInfo.cs diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoder.cs b/src/ImageSharp/Formats/Bmp/BmpDecoder.cs index e252e63406..78a9de6c45 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoder.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoder.cs @@ -34,7 +34,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp } /// - public IImage Identify(Configuration configuration, Stream stream) + public IImageInfo Identify(Configuration configuration, Stream stream) { Guard.NotNull(stream, "stream"); diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index cb510ac05a..e552ac1042 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -141,10 +141,10 @@ namespace SixLabors.ImageSharp.Formats.Bmp } /// - /// Reads the image base information from the specified stream. + /// Reads the raw image information from the specified stream. /// /// The containing image data. - public IImage Identify(Stream stream) + public IImageInfo Identify(Stream stream) { this.ReadImageHeaders(stream, out _, out _); return new ImageInfo(new PixelTypeInfo(this.infoHeader.BitsPerPixel), this.infoHeader.Width, this.infoHeader.Height, new ImageMetaData()); diff --git a/src/ImageSharp/Formats/Gif/GifDecoder.cs b/src/ImageSharp/Formats/Gif/GifDecoder.cs index ccb6cf92c5..c81c51e8b4 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoder.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoder.cs @@ -36,7 +36,7 @@ namespace SixLabors.ImageSharp.Formats.Gif } /// - public IImage Identify(Configuration configuration, Stream stream) + public IImageInfo Identify(Configuration configuration, Stream stream) { Guard.NotNull(stream, "stream"); diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index 7a08b4194e..3c22518057 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -165,10 +165,10 @@ namespace SixLabors.ImageSharp.Formats.Gif } /// - /// Reads the image base information from the specified stream. + /// Reads the raw image information from the specified stream. /// /// The containing image data. - public IImage Identify(Stream stream) + public IImageInfo Identify(Stream stream) { try { @@ -425,7 +425,7 @@ namespace SixLabors.ImageSharp.Formats.Gif if (previousFrame == null) { // This initializes the image to become fully transparent because the alpha channel is zero. - image = new Image(this.configuration, new PixelTypeInfo(this.logicalScreenDescriptor.BitsPerPixel), imageWidth, imageHeight, this.metaData); + image = new Image(this.configuration, imageWidth, imageHeight, this.metaData); this.SetFrameMetaData(image.Frames.RootFrame.MetaData); diff --git a/src/ImageSharp/Formats/IImageInfoDetector.cs b/src/ImageSharp/Formats/IImageInfoDetector.cs index 4e1dfc84f6..37bc0866fa 100644 --- a/src/ImageSharp/Formats/IImageInfoDetector.cs +++ b/src/ImageSharp/Formats/IImageInfoDetector.cs @@ -6,16 +6,16 @@ using System.IO; namespace SixLabors.ImageSharp.Formats { /// - /// Used for detecting the image base information without decoding it. + /// Used for detecting the raw image information without decoding it. /// public interface IImageInfoDetector { /// - /// Reads the image base information from the specified stream. + /// Reads the raw image information from the specified stream. /// /// The configuration for the image. /// The containing image data. /// The object - IImage Identify(Configuration configuration, Stream stream); + IImageInfo Identify(Configuration configuration, Stream stream); } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoder.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoder.cs index 7b082a3d6a..ecebe9480d 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoder.cs @@ -29,7 +29,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort } /// - public IImage Identify(Configuration configuration, Stream stream) + public IImageInfo Identify(Configuration configuration, Stream stream) { Guard.NotNull(stream, "stream"); diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs index ef7a4234f8..d788b65c4b 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs @@ -198,10 +198,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort } /// - /// Reads the image base information from the specified stream. + /// Reads the raw image information from the specified stream. /// /// The containing image data. - public IImage Identify(Stream stream) + public IImageInfo Identify(Stream stream) { this.ParseStream(stream, true); return new ImageInfo(new PixelTypeInfo(this.BitsPerPixel), this.ImageWidth, this.ImageHeight, this.MetaData); @@ -789,7 +789,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort { using (var postProcessor = new JpegImagePostProcessor(this)) { - var image = new Image(this.configuration, new PixelTypeInfo(this.BitsPerPixel), this.ImageWidth, this.ImageHeight, this.MetaData); + var image = new Image(this.configuration, this.ImageWidth, this.ImageHeight, this.MetaData); postProcessor.PostProcess(image.Frames.RootFrame); return image; } diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs index ee7d7e6996..91835b5d71 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs @@ -31,7 +31,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg } /// - public IImage Identify(Configuration configuration, Stream stream) + public IImageInfo Identify(Configuration configuration, Stream stream) { Guard.NotNull(stream, "stream"); diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs index c93adac2d4..211c24d208 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs @@ -159,7 +159,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort this.QuantizeAndInverseAllComponents(); - var image = new Image(this.configuration, null, this.ImageWidth, this.ImageHeight, metadata); + var image = new Image(this.configuration, this.ImageWidth, this.ImageHeight, metadata); this.FillPixelData(image.Frames.RootFrame); this.AssignResolution(image); return image; diff --git a/src/ImageSharp/Formats/PixelTypeInfo.cs b/src/ImageSharp/Formats/PixelTypeInfo.cs index a2a2ce9ce2..cdb6db8d9f 100644 --- a/src/ImageSharp/Formats/PixelTypeInfo.cs +++ b/src/ImageSharp/Formats/PixelTypeInfo.cs @@ -1,7 +1,7 @@ namespace SixLabors.ImageSharp.Formats { /// - /// Stores information about pixel. + /// Stores the raw image pixel type information. /// public class PixelTypeInfo { diff --git a/src/ImageSharp/Formats/Png/PngDecoder.cs b/src/ImageSharp/Formats/Png/PngDecoder.cs index 8e110732ee..9bde4f8cc3 100644 --- a/src/ImageSharp/Formats/Png/PngDecoder.cs +++ b/src/ImageSharp/Formats/Png/PngDecoder.cs @@ -54,7 +54,7 @@ namespace SixLabors.ImageSharp.Formats.Png } /// - public IImage Identify(Configuration configuration, Stream stream) + public IImageInfo Identify(Configuration configuration, Stream stream) { var decoder = new PngDecoderCore(configuration, this); return decoder.Identify(stream); diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index e4c554437e..5c9b753e58 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -279,10 +279,10 @@ namespace SixLabors.ImageSharp.Formats.Png } /// - /// Reads the image base information from the specified stream. + /// Reads the raw image information from the specified stream. /// /// The containing image data. - public IImage Identify(Stream stream) + public IImageInfo Identify(Stream stream) { var metadata = new ImageMetaData(); this.currentStream = stream; @@ -426,7 +426,7 @@ namespace SixLabors.ImageSharp.Formats.Png private void InitializeImage(ImageMetaData metadata, out Image image) where TPixel : struct, IPixel { - image = new Image(this.configuration, new PixelTypeInfo(this.CalculateBitsPerPixel()), this.header.Width, this.header.Height, metadata); + image = new Image(this.configuration, this.header.Width, this.header.Height, metadata); this.bytesPerPixel = this.CalculateBytesPerPixel(); this.bytesPerScanline = this.CalculateScanlineLength(this.header.Width) + 1; this.bytesPerSample = 1; diff --git a/src/ImageSharp/Image/IImage.cs b/src/ImageSharp/Image/IImage.cs index 58e5d50e51..afd00105e6 100644 --- a/src/ImageSharp/Image/IImage.cs +++ b/src/ImageSharp/Image/IImage.cs @@ -1,31 +1,9 @@ -using SixLabors.ImageSharp.Formats; -using SixLabors.ImageSharp.MetaData; - -namespace SixLabors.ImageSharp +namespace SixLabors.ImageSharp { /// /// Represents the base image abstraction. /// - public interface IImage + public interface IImage : IImageInfo { - /// - /// Gets information about pixel. - /// - PixelTypeInfo PixelType { get; } - - /// - /// Gets the width. - /// - int Width { get; } - - /// - /// Gets the height. - /// - int Height { get; } - - /// - /// Gets the meta data of the image. - /// - ImageMetaData MetaData { get; } } } \ No newline at end of file diff --git a/src/ImageSharp/Image/IImageInfo.cs b/src/ImageSharp/Image/IImageInfo.cs new file mode 100644 index 0000000000..b8dd59cc72 --- /dev/null +++ b/src/ImageSharp/Image/IImageInfo.cs @@ -0,0 +1,31 @@ +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.MetaData; + +namespace SixLabors.ImageSharp +{ + /// + /// Represents raw image information. + /// + public interface IImageInfo + { + /// + /// Gets the raw image pixel type information. + /// + PixelTypeInfo PixelType { get; } + + /// + /// Gets the width. + /// + int Width { get; } + + /// + /// Gets the height. + /// + int Height { get; } + + /// + /// Gets the meta data of the image. + /// + ImageMetaData MetaData { get; } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Image/Image.Decode.cs b/src/ImageSharp/Image/Image.Decode.cs index 8f379cb109..6af61f9f4a 100644 --- a/src/ImageSharp/Image/Image.Decode.cs +++ b/src/ImageSharp/Image/Image.Decode.cs @@ -86,9 +86,9 @@ namespace SixLabors.ImageSharp /// The stream. /// the configuration. /// - /// The or null if suitable info detector not found. + /// The or null if suitable info detector not found. /// - private static IImage InternalIdentity(Stream stream, Configuration config) + private static IImageInfo InternalIdentity(Stream stream, Configuration config) { var detector = DiscoverDecoder(stream, config, out IImageFormat _) as IImageInfoDetector; return detector?.Identify(config, stream); diff --git a/src/ImageSharp/Image/Image.FromStream.cs b/src/ImageSharp/Image/Image.FromStream.cs index 0233efb208..680e15aa7d 100644 --- a/src/ImageSharp/Image/Image.FromStream.cs +++ b/src/ImageSharp/Image/Image.FromStream.cs @@ -37,22 +37,22 @@ namespace SixLabors.ImageSharp } /// - /// By reading the header on the provided stream this reads the image base information. + /// By reading the header on the provided stream this reads the raw image information. /// /// The image stream to read the header from. /// /// Thrown if the stream is not readable nor seekable. /// /// - /// The or null if suitable info detector not found. + /// The or null if suitable info detector not found. /// - public static IImage Identify(Stream stream) + public static IImageInfo Identify(Stream stream) { return Identify(null, stream); } /// - /// By reading the header on the provided stream this reads the image base information. + /// By reading the header on the provided stream this reads the raw image information. /// /// The configuration. /// The image stream to read the header from. @@ -60,9 +60,9 @@ namespace SixLabors.ImageSharp /// Thrown if the stream is not readable nor seekable. /// /// - /// The or null if suitable info detector not found. + /// The or null if suitable info detector not found. /// - public static IImage Identify(Configuration config, Stream stream) + public static IImageInfo Identify(Configuration config, Stream stream) { return WithSeekableStream(stream, s => InternalIdentity(s, config ?? Configuration.Default)); } diff --git a/src/ImageSharp/Image/ImageFrameCollection.cs b/src/ImageSharp/Image/ImageFrameCollection.cs index ececcea895..3e9bb03435 100644 --- a/src/ImageSharp/Image/ImageFrameCollection.cs +++ b/src/ImageSharp/Image/ImageFrameCollection.cs @@ -129,7 +129,7 @@ namespace SixLabors.ImageSharp this.frames.Remove(frame); - return new Image(this.parent.GetConfiguration(), this.parent.PixelType, this.parent.MetaData.Clone(), new[] { frame }); + return new Image(this.parent.GetConfiguration(), this.parent.MetaData.Clone(), new[] { frame }); } /// @@ -137,7 +137,7 @@ namespace SixLabors.ImageSharp { ImageFrame frame = this[index]; ImageFrame clonedFrame = frame.Clone(); - return new Image(this.parent.GetConfiguration(), this.parent.PixelType, this.parent.MetaData.Clone(), new[] { clonedFrame }); + return new Image(this.parent.GetConfiguration(), this.parent.MetaData.Clone(), new[] { clonedFrame }); } /// diff --git a/src/ImageSharp/Image/ImageInfo.cs b/src/ImageSharp/Image/ImageInfo.cs index 0d8daaf1a8..a315ee0ed5 100644 --- a/src/ImageSharp/Image/ImageInfo.cs +++ b/src/ImageSharp/Image/ImageInfo.cs @@ -3,8 +3,18 @@ using SixLabors.ImageSharp.MetaData; namespace SixLabors.ImageSharp { - internal sealed class ImageInfo : IImage + /// + /// Stores the raw image information. + /// + internal sealed class ImageInfo : IImageInfo { + /// + /// Initializes a new instance of the class. + /// + /// The raw image pixel type information. + /// The width of the image in pixels. + /// The height of the image in pixels. + /// The images metadata. public ImageInfo(PixelTypeInfo pixelType, int width, int height, ImageMetaData metaData) { this.PixelType = pixelType; @@ -13,12 +23,16 @@ namespace SixLabors.ImageSharp this.MetaData = metaData; } + /// public PixelTypeInfo PixelType { get; } + /// public int Width { get; } + /// public int Height { get; } + /// public ImageMetaData MetaData { get; } } } \ No newline at end of file diff --git a/src/ImageSharp/Image/Image{TPixel}.cs b/src/ImageSharp/Image/Image{TPixel}.cs index 9b20e3dfab..be38b41f24 100644 --- a/src/ImageSharp/Image/Image{TPixel}.cs +++ b/src/ImageSharp/Image/Image{TPixel}.cs @@ -5,9 +5,9 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Runtime.CompilerServices; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Formats; -using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.MetaData; using SixLabors.ImageSharp.PixelFormats; @@ -33,7 +33,7 @@ namespace SixLabors.ImageSharp /// The width of the image in pixels. /// The height of the image in pixels. public Image(Configuration configuration, int width, int height) - : this(configuration, null, width, height, new ImageMetaData()) + : this(configuration, width, height, new ImageMetaData()) { } @@ -55,14 +55,13 @@ namespace SixLabors.ImageSharp /// /// The configuration providing initialization code which allows extending the library. /// - /// The information about pixel of the image. /// The width of the image in pixels. /// The height of the image in pixels. /// The images metadata. - internal Image(Configuration configuration, PixelTypeInfo pixelType, int width, int height, ImageMetaData metadata) + internal Image(Configuration configuration, int width, int height, ImageMetaData metadata) { this.configuration = configuration ?? Configuration.Default; - this.PixelType = pixelType; + this.PixelType = new PixelTypeInfo(Unsafe.SizeOf() * 8); this.MetaData = metadata ?? new ImageMetaData(); this.frames = new ImageFrameCollection(this, width, height); } @@ -72,13 +71,12 @@ namespace SixLabors.ImageSharp /// with the height and the width of the image. /// /// The configuration providing initialization code which allows extending the library. - /// The information about pixel of the image. /// The images metadata. /// The frames that will be owned by this image instance. - internal Image(Configuration configuration, PixelTypeInfo pixelType, ImageMetaData metadata, IEnumerable> frames) + internal Image(Configuration configuration, ImageMetaData metadata, IEnumerable> frames) { this.configuration = configuration ?? Configuration.Default; - this.PixelType = pixelType; + this.PixelType = new PixelTypeInfo(Unsafe.SizeOf() * 8); this.MetaData = metadata ?? new ImageMetaData(); this.frames = new ImageFrameCollection(this, frames); @@ -145,7 +143,7 @@ namespace SixLabors.ImageSharp public Image Clone() { IEnumerable> clonedFrames = this.frames.Select(x => x.Clone()); - return new Image(this.configuration, this.PixelType, this.MetaData.Clone(), clonedFrames); + return new Image(this.configuration, this.MetaData.Clone(), clonedFrames); } /// @@ -163,7 +161,7 @@ namespace SixLabors.ImageSharp where TPixel2 : struct, IPixel { IEnumerable> clonedFrames = this.frames.Select(x => x.CloneAs()); - var target = new Image(this.configuration, this.PixelType, this.MetaData.Clone(), clonedFrames); + var target = new Image(this.configuration, this.MetaData.Clone(), clonedFrames); return target; } diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index 8c22237cf7..1d22e59cb2 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -1,120 +1,120 @@  - - A cross-platform library for the processing of image files; written in C# - SixLabors.ImageSharp - $(packageversion) - 0.0.1 - Six Labors and contributors - netstandard1.1;netstandard1.3;netstandard2.0 - true - true - SixLabors.ImageSharp - SixLabors.ImageSharp - Image Resize Crop Gif Jpg Jpeg Bitmap Png Core - https://raw.githubusercontent.com/SixLabors/ImageSharp/master/build/icons/imagesharp-logo-128.png - https://github.com/SixLabors/ImageSharp - http://www.apache.org/licenses/LICENSE-2.0 - git - https://github.com/SixLabors/ImageSharp - false - false - false - false - false - false - false - false - false - full - portable - True - IOperation - - - - - - - - - All - - - - - - - - - - - - - ..\..\ImageSharp.ruleset - SixLabors.ImageSharp - - - true - - - - TextTemplatingFileGenerator - Block8x8F.Generated.cs - - - TextTemplatingFileGenerator - Block8x8F.Generated.cs - - - TextTemplatingFileGenerator - PixelOperations{TPixel}.Generated.cs - - - TextTemplatingFileGenerator - Rgba32.PixelOperations.Generated.cs - - - PorterDuffFunctions.Generated.cs - TextTemplatingFileGenerator - - - DefaultPixelBlenders.Generated.cs - TextTemplatingFileGenerator - - - - - - - - True - True - Block8x8F.Generated.tt - - - True - True - Block8x8F.Generated.tt - - - True - True - PixelOperations{TPixel}.Generated.tt - - - True - True - Rgba32.PixelOperations.Generated.tt - - - True - True - DefaultPixelBlenders.Generated.tt - - - True - True - PorterDuffFunctions.Generated.tt - - + + A cross-platform library for the processing of image files; written in C# + SixLabors.ImageSharp + $(packageversion) + 0.0.1 + Six Labors and contributors + netstandard1.1;netstandard1.3;netstandard2.0 + true + true + SixLabors.ImageSharp + SixLabors.ImageSharp + Image Resize Crop Gif Jpg Jpeg Bitmap Png Core + https://raw.githubusercontent.com/SixLabors/ImageSharp/master/build/icons/imagesharp-logo-128.png + https://github.com/SixLabors/ImageSharp + http://www.apache.org/licenses/LICENSE-2.0 + git + https://github.com/SixLabors/ImageSharp + false + false + false + false + false + false + false + false + false + full + portable + True + IOperation + + + + + + + + + All + + + + + + + + + + + + + ..\..\ImageSharp.ruleset + SixLabors.ImageSharp + + + true + + + + TextTemplatingFileGenerator + Block8x8F.Generated.cs + + + TextTemplatingFileGenerator + Block8x8F.Generated.cs + + + TextTemplatingFileGenerator + PixelOperations{TPixel}.Generated.cs + + + TextTemplatingFileGenerator + Rgba32.PixelOperations.Generated.cs + + + PorterDuffFunctions.Generated.cs + TextTemplatingFileGenerator + + + DefaultPixelBlenders.Generated.cs + TextTemplatingFileGenerator + + + + + + + + True + True + Block8x8F.Generated.tt + + + True + True + Block8x8F.Generated.tt + + + True + True + PixelOperations{TPixel}.Generated.tt + + + True + True + Rgba32.PixelOperations.Generated.tt + + + True + True + DefaultPixelBlenders.Generated.tt + + + True + True + PorterDuffFunctions.Generated.tt + + \ No newline at end of file diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs index f1fb0086ff..17b42c5040 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs @@ -60,7 +60,7 @@ namespace SixLabors.ImageSharp.Processing.Processors // For resize we know we are going to populate every pixel with fresh data and we want a different target size so // let's manually clone an empty set of images at the correct target and then have the base class process them in turn. IEnumerable> frames = source.Frames.Select(x => new ImageFrame(this.Width, this.Height, x.MetaData.Clone())); // this will create places holders - var image = new Image(config, source.PixelType, source.MetaData.Clone(), frames); // base the place holder images in to prevent a extra frame being added + var image = new Image(config, source.MetaData.Clone(), frames); // base the place holder images in to prevent a extra frame being added return image; } diff --git a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceDecoder.cs b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceDecoder.cs index 719d1b7583..0e967e9278 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceDecoder.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingReferenceDecoder.cs @@ -5,12 +5,11 @@ using System.IO; using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.MetaData; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs { - using SixLabors.ImageSharp.MetaData; - public class SystemDrawingReferenceDecoder : IImageDecoder, IImageInfoDetector { public static SystemDrawingReferenceDecoder Instance { get; } = new SystemDrawingReferenceDecoder(); @@ -44,7 +43,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs } } - public IImage Identify(Configuration configuration, Stream stream) + public IImageInfo Identify(Configuration configuration, Stream stream) { using (var sourceBitmap = new System.Drawing.Bitmap(stream)) { From fea91bd1b739fe948c3763b483e80613e115346c Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 19 Jan 2018 23:12:14 +1100 Subject: [PATCH 14/14] Update intellisens docs + copyright --- ImageSharp.sln.DotSettings | 10 ++++++++++ src/ImageSharp/Formats/IImageInfoDetector.cs | 2 +- .../Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs | 1 + src/ImageSharp/Formats/PixelTypeInfo.cs | 7 +++++-- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 4 ++-- src/ImageSharp/Image/IImage.cs | 7 +++++-- src/ImageSharp/Image/IImageInfo.cs | 12 ++++++++---- src/ImageSharp/Image/Image.Decode.cs | 2 +- src/ImageSharp/Image/Image.FromStream.cs | 6 +++--- src/ImageSharp/Image/ImageInfo.cs | 9 ++++++--- 10 files changed, 42 insertions(+), 18 deletions(-) diff --git a/ImageSharp.sln.DotSettings b/ImageSharp.sln.DotSettings index 1839bf2f8d..435aad73bf 100644 --- a/ImageSharp.sln.DotSettings +++ b/ImageSharp.sln.DotSettings @@ -38,10 +38,15 @@ NEXT_LINE_SHIFTED_2 1 1 + False + False False + NEVER False False + NEVER False + ALWAYS False True ON_SINGLE_LINE @@ -370,8 +375,13 @@ <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> <Policy Inspect="True" Prefix="T" Suffix="" Style="AaBb" /> <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + True + True + True + True True True + True True True \ No newline at end of file diff --git a/src/ImageSharp/Formats/IImageInfoDetector.cs b/src/ImageSharp/Formats/IImageInfoDetector.cs index 37bc0866fa..b7769e8955 100644 --- a/src/ImageSharp/Formats/IImageInfoDetector.cs +++ b/src/ImageSharp/Formats/IImageInfoDetector.cs @@ -6,7 +6,7 @@ using System.IO; namespace SixLabors.ImageSharp.Formats { /// - /// Used for detecting the raw image information without decoding it. + /// Encapsulates methods used for detecting the raw image information without fully decoding it. /// public interface IImageInfoDetector { diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs index d788b65c4b..cdc2a91972 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs @@ -204,6 +204,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort public IImageInfo Identify(Stream stream) { this.ParseStream(stream, true); + return new ImageInfo(new PixelTypeInfo(this.BitsPerPixel), this.ImageWidth, this.ImageHeight, this.MetaData); } diff --git a/src/ImageSharp/Formats/PixelTypeInfo.cs b/src/ImageSharp/Formats/PixelTypeInfo.cs index cdb6db8d9f..ed21b91bfc 100644 --- a/src/ImageSharp/Formats/PixelTypeInfo.cs +++ b/src/ImageSharp/Formats/PixelTypeInfo.cs @@ -1,7 +1,10 @@ -namespace SixLabors.ImageSharp.Formats +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +namespace SixLabors.ImageSharp.Formats { /// - /// Stores the raw image pixel type information. + /// Contains information about the pixels that make up an images visual data. /// public class PixelTypeInfo { diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 5c9b753e58..5cdf80289c 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -332,10 +332,10 @@ namespace SixLabors.ImageSharp.Formats.Png if (this.header == null) { - throw new ImageFormatException("PNG Image hasn't header chunk"); + throw new ImageFormatException("PNG Image does not contain a header chunk"); } - return new ImageInfo(new PixelTypeInfo(this.CalculateBitsPerPixel()), this.header.Width, this.header.Height, metadata); + return new ImageInfo(new PixelTypeInfo(this.CalculateBitsPerPixel()), this.header.Width, this.header.Height, metadata); } /// diff --git a/src/ImageSharp/Image/IImage.cs b/src/ImageSharp/Image/IImage.cs index afd00105e6..7355dc1fec 100644 --- a/src/ImageSharp/Image/IImage.cs +++ b/src/ImageSharp/Image/IImage.cs @@ -1,7 +1,10 @@ -namespace SixLabors.ImageSharp +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +namespace SixLabors.ImageSharp { /// - /// Represents the base image abstraction. + /// Encapsulates the properties and methods that describe an image. /// public interface IImage : IImageInfo { diff --git a/src/ImageSharp/Image/IImageInfo.cs b/src/ImageSharp/Image/IImageInfo.cs index b8dd59cc72..25d5ec7cab 100644 --- a/src/ImageSharp/Image/IImageInfo.cs +++ b/src/ImageSharp/Image/IImageInfo.cs @@ -1,15 +1,19 @@ -using SixLabors.ImageSharp.Formats; +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.MetaData; namespace SixLabors.ImageSharp { /// - /// Represents raw image information. + /// Encapsulates properties that descibe basic image information including dimensions, pixel type information + /// and additional metadata /// public interface IImageInfo { /// - /// Gets the raw image pixel type information. + /// Gets information about the image pixels. /// PixelTypeInfo PixelType { get; } @@ -24,7 +28,7 @@ namespace SixLabors.ImageSharp int Height { get; } /// - /// Gets the meta data of the image. + /// Gets the metadata of the image. /// ImageMetaData MetaData { get; } } diff --git a/src/ImageSharp/Image/Image.Decode.cs b/src/ImageSharp/Image/Image.Decode.cs index 6af61f9f4a..ef00ff15fd 100644 --- a/src/ImageSharp/Image/Image.Decode.cs +++ b/src/ImageSharp/Image/Image.Decode.cs @@ -81,7 +81,7 @@ namespace SixLabors.ImageSharp } /// - /// Reads the image base information. + /// Reads the raw image information from the specified stream. /// /// The stream. /// the configuration. diff --git a/src/ImageSharp/Image/Image.FromStream.cs b/src/ImageSharp/Image/Image.FromStream.cs index 680e15aa7d..62668dd023 100644 --- a/src/ImageSharp/Image/Image.FromStream.cs +++ b/src/ImageSharp/Image/Image.FromStream.cs @@ -52,15 +52,15 @@ namespace SixLabors.ImageSharp } /// - /// By reading the header on the provided stream this reads the raw image information. + /// Reads the raw image information from the specified stream without fully decoding it. /// /// The configuration. - /// The image stream to read the header from. + /// The image stream to read the information from. /// /// Thrown if the stream is not readable nor seekable. /// /// - /// The or null if suitable info detector not found. + /// The or null if suitable info detector is not found. /// public static IImageInfo Identify(Configuration config, Stream stream) { diff --git a/src/ImageSharp/Image/ImageInfo.cs b/src/ImageSharp/Image/ImageInfo.cs index a315ee0ed5..6f894cb599 100644 --- a/src/ImageSharp/Image/ImageInfo.cs +++ b/src/ImageSharp/Image/ImageInfo.cs @@ -1,17 +1,20 @@ -using SixLabors.ImageSharp.Formats; +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.MetaData; namespace SixLabors.ImageSharp { /// - /// Stores the raw image information. + /// Contains information about the image including dimensions, pixel type information and additional metadata /// internal sealed class ImageInfo : IImageInfo { /// /// Initializes a new instance of the class. /// - /// The raw image pixel type information. + /// The image pixel type information. /// The width of the image in pixels. /// The height of the image in pixels. /// The images metadata.