From 24bc9756666f8f7c70740a6d30a764ba7bc460df Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 5 Jun 2018 13:16:06 -0700 Subject: [PATCH 01/31] Cross target netcoreapp2.1 --- src/ImageSharp/ImageSharp.csproj | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index b1934faa6..390c65506 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -5,7 +5,7 @@ $(packageversion) 0.0.1 Six Labors and contributors - netstandard1.1;netstandard1.3;netstandard2.0 + netstandard1.1;netstandard1.3;netstandard2.0;netcoreapp2.1 true true SixLabors.ImageSharp @@ -29,7 +29,7 @@ portable True IOperation - 7.2 + 7.3 @@ -40,10 +40,14 @@ All + + + + - + From 3ea3c093ce33c182e2917a12e3d169e08e560775 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 5 Jun 2018 13:27:31 -0700 Subject: [PATCH 02/31] Use Encoding.UTF8 overload accepting span --- .../MetaData/Profiles/Exif/ExifReader.cs | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs b/src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs index 4f28449d6..d6f38882b 100644 --- a/src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs +++ b/src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs @@ -140,26 +140,27 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif private unsafe string ConvertToString(ReadOnlySpan buffer) { -#if NETSTANDARD1_1 - byte[] bytes = buffer.ToArray(); + Span nullChar = stackalloc byte[1] { 0 }; - string result = Encoding.UTF8.GetString(bytes, 0, buffer.Length); + int nullCharIndex = buffer.IndexOf(nullChar); + if (nullCharIndex > -1) + { + buffer = buffer.Slice(0, nullCharIndex); + } + +#if NETSTANDARD1_1 + return Encoding.UTF8.GetString(buffer.ToArray(), 0, buffer.Length); +#elif NETCOREAPP2_1 + return Encoding.UTF8.GetString(buffer); #else string result; fixed (byte* pointer = &MemoryMarshal.GetReference(buffer)) { - result = Encoding.UTF8.GetString(pointer, buffer.Length); + return Encoding.UTF8.GetString(pointer, buffer.Length); } #endif - int nullCharIndex = result.IndexOf('\0'); - if (nullCharIndex != -1) - { - result = result.Substring(0, nullCharIndex); - } - - return result; } /// From 40ea87482bfbf96f4808eadd459f557454d16f4e Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 5 Jun 2018 13:46:42 -0700 Subject: [PATCH 03/31] Remove unused variable --- src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs b/src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs index d6f38882b..d3ea9743f 100644 --- a/src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs +++ b/src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs @@ -154,8 +154,6 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif #elif NETCOREAPP2_1 return Encoding.UTF8.GetString(buffer); #else - string result; - fixed (byte* pointer = &MemoryMarshal.GetReference(buffer)) { return Encoding.UTF8.GetString(pointer, buffer.Length); From 23c1f8efd729903676bae6eef5e64605482872b7 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 5 Jun 2018 13:59:18 -0700 Subject: [PATCH 04/31] Use vectorized Span methods in DenseMatrix --- src/ImageSharp/Primitives/DenseMatrix{T}.cs | 39 +++++---------------- 1 file changed, 9 insertions(+), 30 deletions(-) diff --git a/src/ImageSharp/Primitives/DenseMatrix{T}.cs b/src/ImageSharp/Primitives/DenseMatrix{T}.cs index 5f3defd11..97fbdb2b3 100644 --- a/src/ImageSharp/Primitives/DenseMatrix{T}.cs +++ b/src/ImageSharp/Primitives/DenseMatrix{T}.cs @@ -105,6 +105,9 @@ namespace SixLabors.ImageSharp.Primitives } } + + public Span Span => new Span(Data); + /// /// Performs an implicit conversion from a to a . /// @@ -146,19 +149,13 @@ namespace SixLabors.ImageSharp.Primitives /// /// The value to fill each item with [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Fill(T value) - { - for (int i = 0; i < this.Data.Length; i++) - { - this.Data[i] = value; - } - } + public void Fill(T value) => this.Span.Fill(value); /// /// Clears the matrix setting each value to the default value for the element type /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Clear() => Array.Clear(this.Data, 0, this.Data.Length); + public void Clear() => this.Span.Clear(); /// /// Checks the coordinates to ensure they are within bounds. @@ -183,28 +180,10 @@ namespace SixLabors.ImageSharp.Primitives } /// - public bool Equals(DenseMatrix other) - { - if (this.Columns != other.Columns) - { - return false; - } - - if (this.Rows != other.Rows) - { - return false; - } - - for (int i = 0; i < this.Data.Length; i++) - { - if (!this.Data[i].Equals(other.Data[i])) - { - return false; - } - } - - return true; - } + public bool Equals(DenseMatrix other) => + this.Columns == other.Columns && + this.Rows == other.Rows && + this.Span.SequenceEqual(other.Span); /// public override bool Equals(object obj) => obj is DenseMatrix other && this.Equals(other); From b5ec02eb40a3c6b8847b0a647c366c5b238fae14 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 5 Jun 2018 14:12:27 -0700 Subject: [PATCH 05/31] Make Exif header a constant --- .../MetaData/Profiles/Exif/ExifConstants.cs | 21 +++++++++++++++++++ .../MetaData/Profiles/Exif/ExifWriter.cs | 19 +++++------------ 2 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 src/ImageSharp/MetaData/Profiles/Exif/ExifConstants.cs diff --git a/src/ImageSharp/MetaData/Profiles/Exif/ExifConstants.cs b/src/ImageSharp/MetaData/Profiles/Exif/ExifConstants.cs new file mode 100644 index 000000000..cca53ba43 --- /dev/null +++ b/src/ImageSharp/MetaData/Profiles/Exif/ExifConstants.cs @@ -0,0 +1,21 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +namespace SixLabors.ImageSharp.MetaData.Profiles.Exif +{ + internal static class ExifConstants + { + public static readonly byte[] Header = { + (byte)'E', + (byte)'x', + (byte)'i', + (byte)'f', + 0x00, + 0x00, + (byte)'I', + (byte)'I', + 0x2A, + 0x00, + }; + } +} \ No newline at end of file diff --git a/src/ImageSharp/MetaData/Profiles/Exif/ExifWriter.cs b/src/ImageSharp/MetaData/Profiles/Exif/ExifWriter.cs index f7363ef31..8749c0755 100644 --- a/src/ImageSharp/MetaData/Profiles/Exif/ExifWriter.cs +++ b/src/ImageSharp/MetaData/Profiles/Exif/ExifWriter.cs @@ -90,16 +90,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif byte[] result = new byte[length]; - result[0] = (byte)'E'; - result[1] = (byte)'x'; - result[2] = (byte)'i'; - result[3] = (byte)'f'; - result[4] = 0x00; - result[5] = 0x00; - result[6] = (byte)'I'; - result[7] = (byte)'I'; - result[8] = 0x2A; - result[9] = 0x00; + ExifConstants.Header.AsSpan().CopyTo(result); // 0-9 int i = 10; uint ifdOffset = ((uint)i - StartIndex) + 4; @@ -250,7 +241,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif return length; } - private int WriteArray(ExifValue value, byte[] destination, int offset) + private int WriteArray(ExifValue value, Span destination, int offset) { if (value.DataType == ExifDataType.Ascii) { @@ -266,7 +257,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif return newOffset; } - private int WriteData(List indexes, byte[] destination, int offset) + private int WriteData(List indexes, Span destination, int offset) { if (this.dataOffsets.Count == 0) { @@ -289,7 +280,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif return newOffset; } - private int WriteHeaders(List indexes, byte[] destination, int offset) + private int WriteHeaders(List indexes, Span destination, int offset) { this.dataOffsets = new List(); @@ -370,7 +361,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif } } - private int WriteValue(ExifValue value, byte[] destination, int offset) + private int WriteValue(ExifValue value, Span destination, int offset) { if (value.IsArray && value.DataType != ExifDataType.Ascii) { From e0612ad200986645fffa20fa763c22d1ebf4e679 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 5 Jun 2018 14:13:41 -0700 Subject: [PATCH 06/31] Use pattern matching --- src/ImageSharp/PixelFormats/Rgba1010102.cs | 2 +- src/ImageSharp/PixelFormats/Rgba32.cs | 2 +- src/ImageSharp/PixelFormats/Rgba64.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/PixelFormats/Rgba1010102.cs b/src/ImageSharp/PixelFormats/Rgba1010102.cs index 166936d5e..ee4865e4e 100644 --- a/src/ImageSharp/PixelFormats/Rgba1010102.cs +++ b/src/ImageSharp/PixelFormats/Rgba1010102.cs @@ -188,7 +188,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// public override bool Equals(object obj) { - return (obj is Rgba1010102) && this.Equals((Rgba1010102)obj); + return obj is Rgba1010102 other && this.Equals(other); } /// diff --git a/src/ImageSharp/PixelFormats/Rgba32.cs b/src/ImageSharp/PixelFormats/Rgba32.cs index f6979aad8..bd014a58f 100644 --- a/src/ImageSharp/PixelFormats/Rgba32.cs +++ b/src/ImageSharp/PixelFormats/Rgba32.cs @@ -387,7 +387,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// public override bool Equals(object obj) { - return (obj is Rgba32) && this.Equals((Rgba32)obj); + return obj is Rgba32 other && this.Equals(other); } /// diff --git a/src/ImageSharp/PixelFormats/Rgba64.cs b/src/ImageSharp/PixelFormats/Rgba64.cs index 1507a258c..5454e0fc1 100644 --- a/src/ImageSharp/PixelFormats/Rgba64.cs +++ b/src/ImageSharp/PixelFormats/Rgba64.cs @@ -187,7 +187,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// public override bool Equals(object obj) { - return (obj is Rgba64) && this.Equals((Rgba64)obj); + return obj is Rgba64 other && this.Equals(other); } /// From 369deb3cf6b29db27df28202d452c48eac4a0cad Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 5 Jun 2018 14:14:12 -0700 Subject: [PATCH 07/31] Remove trailing whitespace --- src/ImageSharp/Primitives/DenseMatrix{T}.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Primitives/DenseMatrix{T}.cs b/src/ImageSharp/Primitives/DenseMatrix{T}.cs index 97fbdb2b3..828aeadfa 100644 --- a/src/ImageSharp/Primitives/DenseMatrix{T}.cs +++ b/src/ImageSharp/Primitives/DenseMatrix{T}.cs @@ -180,9 +180,9 @@ namespace SixLabors.ImageSharp.Primitives } /// - public bool Equals(DenseMatrix other) => - this.Columns == other.Columns && - this.Rows == other.Rows && + public bool Equals(DenseMatrix other) => + this.Columns == other.Columns && + this.Rows == other.Rows && this.Span.SequenceEqual(other.Span); /// From 2f68ee011cf1e08c4d4cc18faceef22383d3f9a5 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 5 Jun 2018 14:27:06 -0700 Subject: [PATCH 08/31] Update dotnet to RTM --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index deb862197..5a146cea6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,7 @@ matrix: - os: linux # Ubuntu 14.04 dist: trusty sudo: required - dotnet: 2.1.300-rc1-008673 + dotnet: 2.1.300 mono: latest # - os: osx # OSX 10.11 # osx_image: xcode7.3.1 From 128dd1a14741c9ff4172a1a2901bc7221957de09 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 5 Jun 2018 14:49:41 -0700 Subject: [PATCH 09/31] =?UTF-8?q?=F0=9F=91=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ImageSharp/Primitives/DenseMatrix{T}.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Primitives/DenseMatrix{T}.cs b/src/ImageSharp/Primitives/DenseMatrix{T}.cs index 828aeadfa..c890a4129 100644 --- a/src/ImageSharp/Primitives/DenseMatrix{T}.cs +++ b/src/ImageSharp/Primitives/DenseMatrix{T}.cs @@ -89,6 +89,11 @@ namespace SixLabors.ImageSharp.Primitives } } + /// + /// Gets a Span wrapping the Data. + /// + public Span Span => new Span(this.Data); + /// /// Gets or sets the item at the specified position. /// @@ -105,9 +110,6 @@ namespace SixLabors.ImageSharp.Primitives } } - - public Span Span => new Span(Data); - /// /// Performs an implicit conversion from a to a . /// From 5ad7ae5089551f60df6fcdc78b5fdf2686b1c01b Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Fri, 8 Jun 2018 11:50:16 -0700 Subject: [PATCH 10/31] Stackalloc bmp header --- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index 2b0c90733..e5bf6d9cb 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -66,16 +66,25 @@ namespace SixLabors.ImageSharp.Formats.Bmp reserved: 0, fileSize: 54 + infoHeader.ImageSize); - byte[] buffer = new byte[40]; // TODO: stackalloc - +#if NETCOREAPP2_1 + Span buffer = stackalloc byte[40]; +#else + byte[] buffer = new byte[40]; +#endif fileHeader.WriteTo(buffer); +#if NETCOREAPP2_1 + stream.Write(buffer.Slice(0, BmpFileHeader.Size)); +#else stream.Write(buffer, 0, BmpFileHeader.Size); - +#endif infoHeader.WriteTo(buffer); +#if NETCOREAPP2_1 + stream.Write(buffer.Slice(0, 40)); +#else stream.Write(buffer, 0, 40); - +#endif this.WriteImage(stream, image.Frames.RootFrame); stream.Flush(); From 0a16995fe2f90574965d5a09e877095e7f00be1c Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Fri, 8 Jun 2018 11:50:31 -0700 Subject: [PATCH 11/31] Stackalloc LzwDecoder buffer --- src/ImageSharp/Formats/Gif/LzwDecoder.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/ImageSharp/Formats/Gif/LzwDecoder.cs b/src/ImageSharp/Formats/Gif/LzwDecoder.cs index 9f9e070e2..446ebde9a 100644 --- a/src/ImageSharp/Formats/Gif/LzwDecoder.cs +++ b/src/ImageSharp/Formats/Gif/LzwDecoder.cs @@ -112,7 +112,12 @@ namespace SixLabors.ImageSharp.Formats.Gif Unsafe.Add(ref suffixRef, code) = (byte)code; } +#if NETCOREAPP2_1 + Span buffer = stackalloc byte[255]; +#else byte[] buffer = new byte[255]; +#endif + while (xyz < length) { if (top == 0) @@ -221,15 +226,24 @@ namespace SixLabors.ImageSharp.Formats.Gif /// The . /// [MethodImpl(MethodImplOptions.AggressiveInlining)] +#if NETCOREAPP2_1 + private int ReadBlock(Span buffer) +#else private int ReadBlock(byte[] buffer) +#endif { int bufferSize = this.stream.ReadByte(); + if (bufferSize < 1) { return 0; } +#if NETCOREAPP2_1 + int count = this.stream.Read(buffer.Slice(0, bufferSize)); +#else int count = this.stream.Read(buffer, 0, bufferSize); +#endif return count != bufferSize ? 0 : bufferSize; } From 4f80c17297c282ccbc555422c8c10cdb0d3e474a Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Mon, 11 Jun 2018 12:00:25 -0700 Subject: [PATCH 12/31] Add stream extensions for Span --- .../Common/Extensions/StreamExtensions.cs | 29 ++++++++++++++++++- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 10 ++----- src/ImageSharp/Formats/Gif/LzwDecoder.cs | 5 +--- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/ImageSharp/Common/Extensions/StreamExtensions.cs b/src/ImageSharp/Common/Extensions/StreamExtensions.cs index 7a9a34ac1..d11ba8ca5 100644 --- a/src/ImageSharp/Common/Extensions/StreamExtensions.cs +++ b/src/ImageSharp/Common/Extensions/StreamExtensions.cs @@ -1,7 +1,7 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Buffers; +using System; using System.IO; namespace SixLabors.ImageSharp @@ -11,6 +11,33 @@ namespace SixLabors.ImageSharp /// internal static class StreamExtensions { +#if NETCOREAPP2_1 + /// + /// Writes data from a stream into the provided buffer. + /// + /// The stream. + /// The buffer. + /// The offset within the buffer to begin writing. + /// The number of bytes to write to the stream. + public static void Write(this Stream stream, Span buffer, int offset, int count) + { + stream.Write(buffer.Slice(offset, count)); + } + + /// + /// Reads data from a stream into the provided buffer. + /// + /// The stream. + /// The buffer.. + /// The offset within the buffer where the bytes are read into. + /// The number of bytes, if available, to read. + /// The actual number of bytes read. + public static int Read(this Stream stream, Span buffer, int offset, int count) + { + return stream.Read(buffer.Slice(offset, count)); + } +#endif + /// /// Skips the number of bytes in the given stream. /// diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index e5bf6d9cb..ed71119d7 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -73,18 +73,12 @@ namespace SixLabors.ImageSharp.Formats.Bmp #endif fileHeader.WriteTo(buffer); -#if NETCOREAPP2_1 - stream.Write(buffer.Slice(0, BmpFileHeader.Size)); -#else stream.Write(buffer, 0, BmpFileHeader.Size); -#endif + infoHeader.WriteTo(buffer); -#if NETCOREAPP2_1 - stream.Write(buffer.Slice(0, 40)); -#else stream.Write(buffer, 0, 40); -#endif + this.WriteImage(stream, image.Frames.RootFrame); stream.Flush(); diff --git a/src/ImageSharp/Formats/Gif/LzwDecoder.cs b/src/ImageSharp/Formats/Gif/LzwDecoder.cs index 446ebde9a..6953e8fcd 100644 --- a/src/ImageSharp/Formats/Gif/LzwDecoder.cs +++ b/src/ImageSharp/Formats/Gif/LzwDecoder.cs @@ -239,11 +239,8 @@ namespace SixLabors.ImageSharp.Formats.Gif return 0; } -#if NETCOREAPP2_1 - int count = this.stream.Read(buffer.Slice(0, bufferSize)); -#else int count = this.stream.Read(buffer, 0, bufferSize); -#endif + return count != bufferSize ? 0 : bufferSize; } From 32cd3f280945e714a85d3123e7d66dc0cdf039bb Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Mon, 11 Jun 2018 12:03:09 -0700 Subject: [PATCH 13/31] Eliminate subsample allocation in JpegEncoder --- src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs index 37279d526..aa624838c 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System; using System.IO; using System.Runtime.CompilerServices; @@ -736,16 +737,16 @@ namespace SixLabors.ImageSharp.Formats.Jpeg private void WriteStartOfFrame(int width, int height, int componentCount) { // "default" to 4:2:0 - byte[] subsamples = { 0x22, 0x11, 0x11 }; + Span subsamples = stackalloc byte[] { 0x22, 0x11, 0x11 }; byte[] chroma = { 0x00, 0x01, 0x01 }; switch (this.subsample) { case JpegSubsample.Ratio444: - subsamples = new byte[] { 0x11, 0x11, 0x11 }; + subsamples = stackalloc byte[] { 0x11, 0x11, 0x11 }; break; case JpegSubsample.Ratio420: - subsamples = new byte[] { 0x22, 0x11, 0x11 }; + subsamples = stackalloc byte[] { 0x22, 0x11, 0x11 }; break; } From 5eaa411166553603928e544b0868ffa83e49f272 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Mon, 11 Jun 2018 12:03:29 -0700 Subject: [PATCH 14/31] Eliminate cmd allocation in BmpDecoder --- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index 26bd97b81..d10b05ce7 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -245,7 +245,11 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// Buffer for uncompressed data. private void UncompressRle8(int w, Span buffer) { +#if NETCOREAPP2_1 + Span cmd = stackalloc byte[2]; +#else byte[] cmd = new byte[2]; +#endif int count = 0; while (count < buffer.Length) From 337aa3e8c5bf9974fc35e30df04af86ed7930dcc Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Mon, 11 Jun 2018 12:12:24 -0700 Subject: [PATCH 15/31] Eliminate allocation reading BMP header --- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 13 ++++++++----- src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs | 5 +++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index d10b05ce7..85461c0d2 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; +using System.Buffers.Binary; using System.IO; using System.Runtime.CompilerServices; using SixLabors.ImageSharp.Memory; @@ -473,12 +474,14 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// private void ReadInfoHeader() { +#if NETCOREAPP2_1 + Span buffer = stackalloc byte[BmpInfoHeader.MaxHeaderSize]; +#else byte[] buffer = new byte[BmpInfoHeader.MaxHeaderSize]; +#endif + this.stream.Read(buffer, 0, BmpInfoHeader.HeaderSizeSize); // read the header size - // read header size - this.stream.Read(buffer, 0, BmpInfoHeader.HeaderSizeSize); - - int headerSize = BitConverter.ToInt32(buffer, 0); + int headerSize = BinaryPrimitives.ReadInt32LittleEndian(buffer); if (headerSize < BmpInfoHeader.CoreSize) { throw new NotSupportedException($"ImageSharp does not support this BMP file. HeaderSize: {headerSize}."); @@ -502,7 +505,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp else if (headerSize >= BmpInfoHeader.Size) { // >= 40 bytes - this.infoHeader = BmpInfoHeader.Parse(buffer.AsSpan(0, 40)); + this.infoHeader = BmpInfoHeader.Parse(buffer); } else { diff --git a/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs b/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs index a088a9b13..872c24286 100644 --- a/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs +++ b/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs @@ -132,6 +132,11 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// public static BmpInfoHeader Parse(ReadOnlySpan data) { + if (data.Length != Size) + { + throw new ArgumentException(nameof(data), $"Must be 40 bytes. Was {data.Length} bytes."); + } + return MemoryMarshal.Cast(data)[0]; } From 2a4ea4aa923eaf47c996dbd14f29cf86a933e566 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Mon, 11 Jun 2018 12:18:42 -0700 Subject: [PATCH 16/31] Eliminate allocation processing the identifier in ProcessApp2Marker --- .../Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs index 752e72dd2..cd893900e 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs @@ -482,16 +482,20 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort private void ProcessApp2Marker(int remaining) { // Length is 14 though we only need to check 12. - const int Icclength = 14; - if (remaining < Icclength || this.IgnoreMetadata) + const int IccLength = 14; + if (remaining < IccLength || this.IgnoreMetadata) { this.InputStream.Skip(remaining); return; } - byte[] identifier = new byte[Icclength]; - this.InputStream.Read(identifier, 0, Icclength); - remaining -= Icclength; // We have read it by this point +#if NETCOREAPP2_1 + byte[] identifier = new byte[IccLength]; // 14 bytes +#else + Span identifer = stackalloc byte[IccLength]; +#endif + this.InputStream.Read(identifier, 0, IccLength); + remaining -= IccLength; // We have read it by this point if (ProfileResolver.IsProfile(identifier, ProfileResolver.IccMarker)) { From 0115a0ab30e3e264c766097322d90336aa382794 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Mon, 11 Jun 2018 12:31:50 -0700 Subject: [PATCH 17/31] Eliminate allocation reading BMP file header --- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index 85461c0d2..7528f36bf 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -521,8 +521,11 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// private void ReadFileHeader() { +#if NETCOREAPP2_1 + Span buffer = stackalloc byte[BmpFileHeader.Size]; +#else byte[] buffer = new byte[BmpFileHeader.Size]; - +#endif this.stream.Read(buffer, 0, BmpFileHeader.Size); this.fileHeader = BmpFileHeader.Parse(buffer); From 069653d61e218450ae285ec21e0e1e5d4c33b526 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Mon, 11 Jun 2018 12:39:45 -0700 Subject: [PATCH 18/31] Revert "Eliminate allocation processing the identifier in ProcessApp2Marker" This reverts commit e4bddc786f044eb60aacff5fb88bd292ade677e4. --- .../Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs index cd893900e..752e72dd2 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs @@ -482,20 +482,16 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort private void ProcessApp2Marker(int remaining) { // Length is 14 though we only need to check 12. - const int IccLength = 14; - if (remaining < IccLength || this.IgnoreMetadata) + const int Icclength = 14; + if (remaining < Icclength || this.IgnoreMetadata) { this.InputStream.Skip(remaining); return; } -#if NETCOREAPP2_1 - byte[] identifier = new byte[IccLength]; // 14 bytes -#else - Span identifer = stackalloc byte[IccLength]; -#endif - this.InputStream.Read(identifier, 0, IccLength); - remaining -= IccLength; // We have read it by this point + byte[] identifier = new byte[Icclength]; + this.InputStream.Read(identifier, 0, Icclength); + remaining -= Icclength; // We have read it by this point if (ProfileResolver.IsProfile(identifier, ProfileResolver.IccMarker)) { From b1ea9077de715dcdc4a65e0506bfa3110bd76ba8 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Mon, 11 Jun 2018 15:40:04 -0700 Subject: [PATCH 19/31] Make new Span field on DenseMatrix internal This may need to be a method. --- src/ImageSharp/Primitives/DenseMatrix{T}.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Primitives/DenseMatrix{T}.cs b/src/ImageSharp/Primitives/DenseMatrix{T}.cs index c890a4129..ef1abc897 100644 --- a/src/ImageSharp/Primitives/DenseMatrix{T}.cs +++ b/src/ImageSharp/Primitives/DenseMatrix{T}.cs @@ -92,7 +92,7 @@ namespace SixLabors.ImageSharp.Primitives /// /// Gets a Span wrapping the Data. /// - public Span Span => new Span(this.Data); + internal Span Span => new Span(this.Data); /// /// Gets or sets the item at the specified position. From 154391f97328aac114578a1c07f418ae9a970d3f Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Mon, 11 Jun 2018 15:40:41 -0700 Subject: [PATCH 20/31] Enable netcoreapp2.1 tests on AppVeyor --- appveyor.yml | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index d98fa9c6a..83ab8e4c7 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -6,13 +6,11 @@ skip_branch_with_pr: true environment: matrix: - ### TODO: Enable the netcoreapp2.1 target when RC2 has been released! - - #- target_framework: netcoreapp2.1 - # is_32bit: False + - target_framework: netcoreapp2.1 + is_32bit: False - #- target_framework: netcoreapp2.1 - # is_32bit: True + - target_framework: netcoreapp2.1 + is_32bit: True - target_framework: netcoreapp2.0 is_32bit: False From 400b9e4366ed8f3a5cc590faf4137b6a5c321d01 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Thu, 14 Jun 2018 07:52:37 -0700 Subject: [PATCH 21/31] Interpolate Size --- src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs b/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs index 872c24286..b5b4d39b1 100644 --- a/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs +++ b/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs @@ -134,7 +134,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp { if (data.Length != Size) { - throw new ArgumentException(nameof(data), $"Must be 40 bytes. Was {data.Length} bytes."); + throw new ArgumentException(nameof(data), $"Must be {Size} bytes. Was {data.Length} bytes."); } return MemoryMarshal.Cast(data)[0]; From 1b38d48c4f0a3b3569674c1d01c902fdd77ca455 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Thu, 14 Jun 2018 07:52:50 -0700 Subject: [PATCH 22/31] stackalloc chroma --- src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs index aa624838c..9ffd40c93 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs @@ -738,7 +738,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg { // "default" to 4:2:0 Span subsamples = stackalloc byte[] { 0x22, 0x11, 0x11 }; - byte[] chroma = { 0x00, 0x01, 0x01 }; + Span chroma = stackalloc byte[] { 0x00, 0x01, 0x01 }; switch (this.subsample) { From a573ee0a6e8947c3caf4dba6ec041a00cc4e009c Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Thu, 14 Jun 2018 08:29:21 -0700 Subject: [PATCH 23/31] Manually reference System.Runtime.CompilerServices.Unsafe in tests --- tests/ImageSharp.Tests/ImageSharp.Tests.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj index 139df3972..09e915ce8 100644 --- a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj +++ b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj @@ -28,6 +28,7 @@ + From 4e43db783a41d637c937f5351816bab6a9c56594 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Thu, 14 Jun 2018 10:47:04 -0700 Subject: [PATCH 24/31] Enable AutoGenerateBindingRedirects for tests --- tests/ImageSharp.Tests/ImageSharp.Tests.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj index 09e915ce8..7ecf1baca 100644 --- a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj +++ b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj @@ -9,6 +9,7 @@ SixLabors.ImageSharp.Tests SixLabors.ImageSharp.Tests AnyCPU;x64;x86 + true true From f1d931a67ca265ecf58ae12b9a444b934e5f2a87 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Fri, 15 Jun 2018 08:07:35 -0700 Subject: [PATCH 25/31] Specify the runtime when running tests for netcoreapp2.1 [take 1] --- run-tests.ps1 | 5 +++++ tests/ImageSharp.Tests/ImageSharp.Tests.csproj | 2 -- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/run-tests.ps1 b/run-tests.ps1 index d774f7a61..3218d9d67 100644 --- a/run-tests.ps1 +++ b/run-tests.ps1 @@ -75,6 +75,11 @@ else { $xunitArgs += " --fx-version 2.0.0" } + if ($targetFramework -eq "netcoreapp2.1") { + # There were issues matching the correct installed runtime if we do not specify it explicitly: + $xunitArgs += " --fx-version 2.1.0" + } + if ($is32Bit -eq "True") { $xunitArgs += " -x86" } diff --git a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj index 7ecf1baca..139df3972 100644 --- a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj +++ b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj @@ -9,7 +9,6 @@ SixLabors.ImageSharp.Tests SixLabors.ImageSharp.Tests AnyCPU;x64;x86 - true true @@ -29,7 +28,6 @@ - From c3bf1664ab701e2ceaa3b06f1ee83a81b66fd2ef Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Fri, 15 Jun 2018 16:21:54 -0700 Subject: [PATCH 26/31] Eliminate reference to System.Runtime.CompilerServices.Unsafe on .NETCOREAPP2.1 --- src/ImageSharp/ImageSharp.csproj | 2 +- tests/ImageSharp.Tests/ImageSharp.Tests.csproj | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index 390c65506..baf0551f1 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -40,12 +40,12 @@ All - + diff --git a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj index 139df3972..e0f13fb21 100644 --- a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj +++ b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj @@ -25,6 +25,11 @@ + + + + + From 8b856093200b7976eb8bbd47fdb3b01f1782bdb2 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Fri, 15 Jun 2018 16:37:24 -0700 Subject: [PATCH 27/31] Downgrade Unsafe package [test] --- src/ImageSharp/ImageSharp.csproj | 4 ++++ .../Drawing/Text/DrawTextOnImageTests.cs | 13 ++++--------- tests/ImageSharp.Tests/ImageSharp.Tests.csproj | 4 ---- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index baf0551f1..299586abc 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -47,6 +47,10 @@ + + + + diff --git a/tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs b/tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs index a9c7a6ebb..c3a2bf17f 100644 --- a/tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs +++ b/tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs @@ -1,25 +1,20 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -using System.Numerics; - +using System; +using System.Linq; +using System.Text; using SixLabors.Fonts; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; -using SixLabors.ImageSharp.Processing.Drawing; using SixLabors.ImageSharp.Processing.Text; +using SixLabors.Primitives; using Xunit; // ReSharper disable InconsistentNaming namespace SixLabors.ImageSharp.Tests.Drawing.Text { - using System; - using System.Linq; - using System.Text; - - using SixLabors.Primitives; - [GroupOutput("Drawing/Text")] public class DrawTextOnImageTests { diff --git a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj index e0f13fb21..6add99c05 100644 --- a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj +++ b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj @@ -25,10 +25,6 @@ - - - - From 0eb76940dee88772bdad9788f06d1e61cef6beae Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 19 Jun 2018 07:58:11 -0700 Subject: [PATCH 28/31] Use non-preview appveyor image The main image was updated to 15.7.3 --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index 83ab8e4c7..3e6b79bfc 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,5 +1,5 @@ version: 1.0.0.{build} -image: Visual Studio 2017 Preview +image: Visual Studio 2017 # prevent the double build when a branch has an active PR skip_branch_with_pr: true From e306f914be42083845b3464c0ce0c248a2e86599 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Tue, 19 Jun 2018 13:30:19 -0700 Subject: [PATCH 29/31] Fix merge conflict --- tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs b/tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs index ea97dd76d..7c04d090b 100644 --- a/tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs +++ b/tests/ImageSharp.Tests/Drawing/Text/DrawTextOnImageTests.cs @@ -7,7 +7,9 @@ using System.Text; using SixLabors.Fonts; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; +using SixLabors.ImageSharp.Processing.Drawing.Pens; using SixLabors.ImageSharp.Processing.Text; +using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; using SixLabors.Primitives; using Xunit; From 85a16c0bf84a9c8e1fb01953b58c9a539db633c3 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Wed, 20 Jun 2018 01:25:00 +0200 Subject: [PATCH 30/31] Make sure that netcoreapp2.1 test execution is testing the netcoreapp2.1 build of ImageSharp --- src/ImageSharp/Common/Helpers/TestHelpers.cs | 24 +++++++++++++++++ .../TestUtilities/TestEnvironment.cs | 21 +++++++++++++++ .../Tests/TestEnvironmentTests.cs | 26 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 src/ImageSharp/Common/Helpers/TestHelpers.cs diff --git a/src/ImageSharp/Common/Helpers/TestHelpers.cs b/src/ImageSharp/Common/Helpers/TestHelpers.cs new file mode 100644 index 000000000..14e5835b4 --- /dev/null +++ b/src/ImageSharp/Common/Helpers/TestHelpers.cs @@ -0,0 +1,24 @@ +// Copyright(c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +namespace SixLabors.ImageSharp.Common.Helpers +{ + /// + /// Internal utilities intended to be only used in tests. + /// + internal static class TestHelpers + { + /// + /// This constant is useful to verify the target framework ImageSharp has been built against. + /// Only intended to be used in tests! + /// + internal const string ImageSharpBuiltAgainst = +#if NETSTANDARD1_1 + "netstandard1.1"; +#elif NETCOREAPP2_1 + "netcoreapp2.1"; +#else + "netstandard2.0"; +#endif + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs index 4cee650e8..e9f519aba 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs @@ -30,6 +30,13 @@ namespace SixLabors.ImageSharp.Tests return Boolean.TryParse(Environment.GetEnvironmentVariable("CI"), out isCi) && isCi; }); + private static readonly Lazy NetCoreVersionLazy = new Lazy(GetNetCoreVersion); + + /// + /// Gets the .NET Core version, if running on .NET Core, otherwise returns null. + /// + internal static string NetCoreVersion => NetCoreVersionLazy.Value; + // ReSharper disable once InconsistentNaming /// /// Gets a value indicating whether test execution runs on CI. @@ -123,5 +130,19 @@ namespace SixLabors.ImageSharp.Tests return path; } + + /// + /// Solution borrowed from: + /// https://github.com/dotnet/BenchmarkDotNet/issues/448#issuecomment-308424100 + /// + private static string GetNetCoreVersion() + { + Assembly assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly; + string[] assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries); + int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App"); + if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2) + return assemblyPath[netCoreAppIndex + 1]; + return ""; + } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs index 9db55281e..4a2c63beb 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestEnvironmentTests.cs @@ -4,6 +4,7 @@ using System; using System.IO; +using SixLabors.ImageSharp.Common.Helpers; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Bmp; using SixLabors.ImageSharp.Formats.Gif; @@ -13,9 +14,11 @@ using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs; using Xunit; using Xunit.Abstractions; +// ReSharper disable InconsistentNaming namespace SixLabors.ImageSharp.Tests { + public class TestEnvironmentTests { public TestEnvironmentTests(ITestOutputHelper output) @@ -31,6 +34,29 @@ namespace SixLabors.ImageSharp.Tests Assert.True(Directory.Exists(path)); } + /// + /// We need this test to make sure that the netcoreapp2.1 test execution actually covers the netcoreapp2.1 build configuration of ImageSharp. + /// + [Fact] + public void ImageSharpAssemblyUnderTest_MatchesExpectedTargetFramework() + { + this.Output.WriteLine("NetCoreVersion: " + TestEnvironment.NetCoreVersion); + this.Output.WriteLine("ImageSharpBuiltAgainst: " + TestHelpers.ImageSharpBuiltAgainst); + + if (string.IsNullOrEmpty(TestEnvironment.NetCoreVersion)) + { + this.Output.WriteLine("Not running under .NET Core!"); + } + else if (TestEnvironment.NetCoreVersion.StartsWith("2.1")) + { + Assert.Equal("netcoreapp2.1", TestHelpers.ImageSharpBuiltAgainst); + } + else + { + Assert.Equal("netstandard2.0", TestHelpers.ImageSharpBuiltAgainst); + } + } + [Fact] public void SolutionDirectoryFullPath() { From c41fb827826e2d31417aecf986560e9775415867 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Wed, 20 Jun 2018 01:32:59 +0200 Subject: [PATCH 31/31] correct doc comment --- tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs index e9f519aba..b07a383b7 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs @@ -33,7 +33,7 @@ namespace SixLabors.ImageSharp.Tests private static readonly Lazy NetCoreVersionLazy = new Lazy(GetNetCoreVersion); /// - /// Gets the .NET Core version, if running on .NET Core, otherwise returns null. + /// Gets the .NET Core version, if running on .NET Core, otherwise returns an empty string. /// internal static string NetCoreVersion => NetCoreVersionLazy.Value;