Skip to content

Commit 4bdfcbe

Browse files
Simplify and cleanup build constants
1 parent 06bf7ac commit 4bdfcbe

File tree

9 files changed

+94
-79
lines changed

9 files changed

+94
-79
lines changed

src/ImageSharp.Drawing/ImageSharp.Drawing.csproj

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,40 @@
88
<PackageId>SixLabors.ImageSharp.Drawing</PackageId>
99
<PackageTags>Image Draw Shape Path Font</PackageTags>
1010
<RootNamespace>SixLabors.ImageSharp</RootNamespace>
11-
<TargetFrameworks>netcoreapp2.1;netstandard1.3;netstandard2.0</TargetFrameworks>
12-
</PropertyGroup>
1311

14-
<!-- TODO: Include .NETSTANDARD2.1 when released-->
15-
<PropertyGroup Condition=" $(TargetFramework.StartsWith('netcoreapp2')) ">
16-
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF</DefineConstants>
12+
<TargetFrameworks>netcoreapp2.1;netstandard2.0;netstandard1.3</TargetFrameworks>
1713
</PropertyGroup>
1814

19-
<PropertyGroup Condition=" $(TargetFramework.StartsWith('netcoreapp2.1')) ">
20-
<DefineConstants>$(DefineConstants);SUPPORTS_HASHCODE</DefineConstants>
15+
<!--
16+
https://apisof.net/
17+
+===================+================+===================+==============================+======================+==========================+
18+
| Target Framework | SUPPORTS_MATHF | SUPPORTS_HASHCODE | SUPPORTS_EXTENDED_INTRINSICS | SUPPORTS_SPAN_STREAM | SUPPORTS_ENCODING_STRING |
19+
+===================+================+===================+==============================+======================+==========================+
20+
| netcoreapp3.1 | Y | Y | Y | Y | Y |
21+
| netcoreapp2.1 | Y | Y | Y | Y | Y |
22+
| netcoreapp2.0 | Y | N | N | N | N |
23+
| netstandard2.1 | Y | N | N | Y | Y |
24+
| netstandard2.0 | N | N | N | N | N |
25+
| netstandard1.3 | N | N | N | N | N |
26+
| net472 | N | N | Y | N | N |
27+
+===================+================+===================+==============================+======================+==========================+
28+
-->
29+
30+
<!-- TODO: Include additional targets to TargetFrameworks -->
31+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1'">
32+
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF;SUPPORTS_HASHCODE;SUPPORTS_EXTENDED_INTRINSICS;SUPPORTS_SPAN_STREAM;SUPPORTS_ENCODING_STRING</DefineConstants>
33+
</PropertyGroup>
34+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1'">
35+
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF;SUPPORTS_HASHCODE;SUPPORTS_EXTENDED_INTRINSICS;SUPPORTS_SPAN_STREAM;SUPPORTS_ENCODING_STRING</DefineConstants>
36+
</PropertyGroup>
37+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0'">
38+
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF;</DefineConstants>
39+
</PropertyGroup>
40+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.1'">
41+
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF;SUPPORTS_SPAN_STREAM;SUPPORTS_ENCODING_STRING</DefineConstants>
42+
</PropertyGroup>
43+
<PropertyGroup Condition=" '$(TargetFramework)' == 'net472'">
44+
<DefineConstants>$(DefineConstants);SUPPORTS_EXTENDED_INTRINSICS</DefineConstants>
2145
</PropertyGroup>
2246

2347
<ItemGroup>

src/ImageSharp/Common/Extensions/EncoderExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// Copyright (c) Six Labors and contributors.
1+
// Copyright (c) Six Labors and contributors.
22
// Licensed under the Apache License, Version 2.0.
33

4-
#if !NETCOREAPP2_1
4+
#if !SUPPORTS_ENCODING_STRING
55
using System;
66
using System.Text;
77

@@ -32,4 +32,4 @@ public static string GetString(this Encoding encoding, ReadOnlySpan<byte> buffer
3232
}
3333
}
3434
}
35-
#endif
35+
#endif

src/ImageSharp/Common/Extensions/StreamExtensions.cs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Buffers;
66
using System.IO;
7-
87
using SixLabors.ImageSharp.Memory;
98
using SixLabors.Memory;
109

@@ -15,7 +14,6 @@ namespace SixLabors.ImageSharp
1514
/// </summary>
1615
internal static class StreamExtensions
1716
{
18-
#if NETCOREAPP2_1
1917
/// <summary>
2018
/// Writes data from a stream into the provided buffer.
2119
/// </summary>
@@ -24,23 +22,18 @@ internal static class StreamExtensions
2422
/// <param name="offset">The offset within the buffer to begin writing.</param>
2523
/// <param name="count">The number of bytes to write to the stream.</param>
2624
public static void Write(this Stream stream, Span<byte> buffer, int offset, int count)
27-
{
28-
stream.Write(buffer.Slice(offset, count));
29-
}
25+
=> stream.Write(buffer.Slice(offset, count));
3026

3127
/// <summary>
3228
/// Reads data from a stream into the provided buffer.
3329
/// </summary>
3430
/// <param name="stream">The stream.</param>
35-
/// <param name="buffer">The buffer..</param>
31+
/// <param name="buffer">The buffer.</param>
3632
/// <param name="offset">The offset within the buffer where the bytes are read into.</param>
3733
/// <param name="count">The number of bytes, if available, to read.</param>
3834
/// <returns>The actual number of bytes read.</returns>
3935
public static int Read(this Stream stream, Span<byte> buffer, int offset, int count)
40-
{
41-
return stream.Read(buffer.Slice(offset, count));
42-
}
43-
#endif
36+
=> stream.Read(buffer.Slice(offset, count));
4437

4538
/// <summary>
4639
/// Skips the number of bytes in the given stream.
@@ -75,17 +68,39 @@ public static void Skip(this Stream stream, int count)
7568
}
7669

7770
public static void Read(this Stream stream, IManagedByteBuffer buffer)
78-
{
79-
stream.Read(buffer.Array, 0, buffer.Length());
80-
}
71+
=> stream.Read(buffer.Array, 0, buffer.Length());
8172

8273
public static void Write(this Stream stream, IManagedByteBuffer buffer)
74+
=> stream.Write(buffer.Array, 0, buffer.Length());
75+
76+
#if !SUPPORTS_SPAN_STREAM
77+
// This is a port of the CoreFX implementation and is MIT Licensed:
78+
// https://github.com/dotnet/corefx/blob/17300169760c61a90cab8d913636c1058a30a8c1/src/Common/src/CoreLib/System/IO/Stream.cs#L742
79+
public static int Read(this Stream stream, Span<byte> buffer)
8380
{
84-
stream.Write(buffer.Array, 0, buffer.Length());
81+
// This uses ArrayPool<byte>.Shared, rather than taking a MemoryAllocator,
82+
// in order to match the signature of the framework method that exists in
83+
// .NET Core.
84+
byte[] sharedBuffer = ArrayPool<byte>.Shared.Rent(buffer.Length);
85+
try
86+
{
87+
int numRead = stream.Read(sharedBuffer, 0, buffer.Length);
88+
if ((uint)numRead > (uint)buffer.Length)
89+
{
90+
throw new IOException("Stream was too long.");
91+
}
92+
93+
new Span<byte>(sharedBuffer, 0, numRead).CopyTo(buffer);
94+
return numRead;
95+
}
96+
finally
97+
{
98+
ArrayPool<byte>.Shared.Return(sharedBuffer);
99+
}
85100
}
86101

87-
#if NET472 || NETSTANDARD1_3 || NETSTANDARD2_0
88-
// This is a port of the CoreFX implementation and is MIT Licensed: https://github.com/dotnet/coreclr/blob/c4dca1072d15bdda64c754ad1ea474b1580fa554/src/System.Private.CoreLib/shared/System/IO/Stream.cs#L770
102+
// This is a port of the CoreFX implementation and is MIT Licensed:
103+
// https://github.com/dotnet/corefx/blob/17300169760c61a90cab8d913636c1058a30a8c1/src/Common/src/CoreLib/System/IO/Stream.cs#L775
89104
public static void Write(this Stream stream, ReadOnlySpan<byte> buffer)
90105
{
91106
// This uses ArrayPool<byte>.Shared, rather than taking a MemoryAllocator,

src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,7 @@ private void ReadRle24<TPixel>(Buffer2D<TPixel> pixels, int width, int height, b
445445
/// <param name="rowsWithUndefinedPixels">Keeps track of rows, which have undefined pixels.</param>
446446
private void UncompressRle4(int w, Span<byte> buffer, Span<bool> undefinedPixels, Span<bool> rowsWithUndefinedPixels)
447447
{
448-
#if NETCOREAPP2_1
449448
Span<byte> cmd = stackalloc byte[2];
450-
#else
451-
var cmd = new byte[2];
452-
#endif
453449
int count = 0;
454450

455451
while (count < buffer.Length)
@@ -556,11 +552,7 @@ private void UncompressRle4(int w, Span<byte> buffer, Span<bool> undefinedPixels
556552
/// <param name="rowsWithUndefinedPixels">Keeps track of rows, which have undefined pixels.</param>
557553
private void UncompressRle8(int w, Span<byte> buffer, Span<bool> undefinedPixels, Span<bool> rowsWithUndefinedPixels)
558554
{
559-
#if NETCOREAPP2_1
560555
Span<byte> cmd = stackalloc byte[2];
561-
#else
562-
var cmd = new byte[2];
563-
#endif
564556
int count = 0;
565557

566558
while (count < buffer.Length)
@@ -639,11 +631,7 @@ private void UncompressRle8(int w, Span<byte> buffer, Span<bool> undefinedPixels
639631
/// <param name="rowsWithUndefinedPixels">Keeps track of rows, which have undefined pixels.</param>
640632
private void UncompressRle24(int w, Span<byte> buffer, Span<bool> undefinedPixels, Span<bool> rowsWithUndefinedPixels)
641633
{
642-
#if NETCOREAPP2_1
643634
Span<byte> cmd = stackalloc byte[2];
644-
#else
645-
var cmd = new byte[2];
646-
#endif
647635
int uncompressedPixels = 0;
648636

649637
while (uncompressedPixels < buffer.Length)
@@ -1213,11 +1201,7 @@ private static int CountBits(uint n)
12131201
/// </summary>
12141202
private void ReadInfoHeader()
12151203
{
1216-
#if NETCOREAPP2_1
12171204
Span<byte> buffer = stackalloc byte[BmpInfoHeader.MaxHeaderSize];
1218-
#else
1219-
var buffer = new byte[BmpInfoHeader.MaxHeaderSize];
1220-
#endif
12211205

12221206
// Read the header size.
12231207
this.stream.Read(buffer, 0, BmpInfoHeader.HeaderSizeSize);
@@ -1339,11 +1323,7 @@ private void ReadInfoHeader()
13391323
/// </summary>
13401324
private void ReadFileHeader()
13411325
{
1342-
#if NETCOREAPP2_1
13431326
Span<byte> buffer = stackalloc byte[BmpFileHeader.Size];
1344-
#else
1345-
var buffer = new byte[BmpFileHeader.Size];
1346-
#endif
13471327
this.stream.Read(buffer, 0, BmpFileHeader.Size);
13481328

13491329
short fileTypeMarker = BinaryPrimitives.ReadInt16LittleEndian(buffer);

src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
173173
reserved: 0,
174174
offset: BmpFileHeader.Size + infoHeaderSize + colorPaletteSize);
175175

176-
#if NETCOREAPP2_1
177176
Span<byte> buffer = stackalloc byte[infoHeaderSize];
178-
#else
179-
var buffer = new byte[infoHeaderSize];
180-
#endif
181177
fileHeader.WriteTo(buffer);
182178

183179
stream.Write(buffer, 0, BmpFileHeader.Size);

src/ImageSharp/Formats/Gif/LzwDecoder.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,7 @@ public void DecodePixels(int width, int height, int dataSize, Span<byte> pixels)
113113
Unsafe.Add(ref suffixRef, code) = (byte)code;
114114
}
115115

116-
#if NETCOREAPP2_1
117116
Span<byte> buffer = stackalloc byte[255];
118-
#else
119-
var buffer = new byte[255];
120-
#endif
121117

122118
while (xyz < length)
123119
{
@@ -227,11 +223,7 @@ public void DecodePixels(int width, int height, int dataSize, Span<byte> pixels)
227223
/// The <see cref="int"/>.
228224
/// </returns>
229225
[MethodImpl(MethodImplOptions.AggressiveInlining)]
230-
#if NETCOREAPP2_1
231226
private int ReadBlock(Span<byte> buffer)
232-
#else
233-
private int ReadBlock(byte[] buffer)
234-
#endif
235227
{
236228
int bufferSize = this.stream.ReadByte();
237229

src/ImageSharp/Formats/Tga/TgaDecoderCore.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -565,11 +565,8 @@ private bool ReadFileHeader(Stream stream)
565565
{
566566
this.currentStream = stream;
567567

568-
#if NETCOREAPP2_1
569568
Span<byte> buffer = stackalloc byte[TgaFileHeader.Size];
570-
#else
571-
var buffer = new byte[TgaFileHeader.Size];
572-
#endif
569+
573570
this.currentStream.Read(buffer, 0, TgaFileHeader.Size);
574571
this.fileHeader = TgaFileHeader.Parse(buffer);
575572
this.metadata = new ImageMetadata();

src/ImageSharp/Formats/Tga/TgaEncoderCore.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
9797
pixelDepth: (byte)this.bitsPerPixel.Value,
9898
imageDescriptor: imageDescriptor);
9999

100-
#if NETCOREAPP2_1
101100
Span<byte> buffer = stackalloc byte[TgaFileHeader.Size];
102-
#else
103-
byte[] buffer = new byte[TgaFileHeader.Size];
104-
#endif
105101
fileHeader.WriteTo(buffer);
106102

107103
stream.Write(buffer, 0, TgaFileHeader.Size);

src/ImageSharp/ImageSharp.csproj

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<VersionPrefix Condition="$(packageversion) != ''">$(packageversion)</VersionPrefix>
1111
<VersionPrefix Condition="$(packageversion) == ''">0.0.1</VersionPrefix>
1212

13-
<TargetFrameworks>netcoreapp2.1;netstandard1.3;netstandard2.0;net472</TargetFrameworks>
13+
<TargetFrameworks>netcoreapp2.1;netstandard2.0;netstandard1.3;net472</TargetFrameworks>
1414

1515
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1616
<GenerateDocumentationFile>true</GenerateDocumentationFile>
@@ -19,23 +19,38 @@
1919
<RootNamespace>SixLabors.ImageSharp</RootNamespace>
2020
</PropertyGroup>
2121

22-
<!-- TODO: Include .NETSTANDARD2.1 when released-->
23-
<PropertyGroup Condition=" $(TargetFramework.StartsWith('netcoreapp2')) ">
24-
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF</DefineConstants>
22+
<!--
23+
https://apisof.net/
24+
+===================+================+===================+==============================+======================+==========================+
25+
| Target Framework | SUPPORTS_MATHF | SUPPORTS_HASHCODE | SUPPORTS_EXTENDED_INTRINSICS | SUPPORTS_SPAN_STREAM | SUPPORTS_ENCODING_STRING |
26+
+===================+================+===================+==============================+======================+==========================+
27+
| netcoreapp3.1 | Y | Y | Y | Y | Y |
28+
| netcoreapp2.1 | Y | Y | Y | Y | Y |
29+
| netcoreapp2.0 | Y | N | N | N | N |
30+
| netstandard2.1 | Y | N | N | Y | Y |
31+
| netstandard2.0 | N | N | N | N | N |
32+
| netstandard1.3 | N | N | N | N | N |
33+
| net472 | N | N | Y | N | N |
34+
+===================+================+===================+==============================+======================+==========================+
35+
-->
36+
37+
<!-- TODO: Include additional targets to TargetFrameworks -->
38+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1'">
39+
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF;SUPPORTS_HASHCODE;SUPPORTS_EXTENDED_INTRINSICS;SUPPORTS_SPAN_STREAM;SUPPORTS_ENCODING_STRING</DefineConstants>
2540
</PropertyGroup>
26-
27-
<PropertyGroup Condition=" $(TargetFramework.StartsWith('netcoreapp2.1')) ">
28-
<DefineConstants>$(DefineConstants);SUPPORTS_HASHCODE</DefineConstants>
41+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1'">
42+
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF;SUPPORTS_HASHCODE;SUPPORTS_EXTENDED_INTRINSICS;SUPPORTS_SPAN_STREAM;SUPPORTS_ENCODING_STRING</DefineConstants>
2943
</PropertyGroup>
30-
31-
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1' OR '$(TargetFramework)' == 'net472' ">
44+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0'">
45+
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF;</DefineConstants>
46+
</PropertyGroup>
47+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.1'">
48+
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF;SUPPORTS_SPAN_STREAM;SUPPORTS_ENCODING_STRING</DefineConstants>
49+
</PropertyGroup>
50+
<PropertyGroup Condition=" '$(TargetFramework)' == 'net472'">
3251
<DefineConstants>$(DefineConstants);SUPPORTS_EXTENDED_INTRINSICS</DefineConstants>
3352
</PropertyGroup>
3453

35-
<ItemGroup>
36-
<Compile Include="..\Shared\*.cs" />
37-
</ItemGroup>
38-
3954
<ItemGroup>
4055
<Compile Include="..\..\shared-infrastructure\**\*.cs" />
4156
</ItemGroup>

0 commit comments

Comments
 (0)