Skip to content

Commit 7332e98

Browse files
Merge pull request #1075 from SixLabors/js/normalize-ifdefs
Simplify and cleanup build constants
2 parents 06bf7ac + b8306a3 commit 7332e98

File tree

12 files changed

+82
-89
lines changed

12 files changed

+82
-89
lines changed

.gitattributes

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,22 @@
8989
###############################################################################
9090
*.bmp binary
9191
*.dll binary
92+
*.eot binary
9293
*.exe binary
9394
*.gif binary
9495
*.jpg binary
96+
*.pdf binary
9597
*.png binary
96-
*.tga binary
98+
*.ppt binary
99+
*.pptx binary
97100
*.ttf binary
98101
*.snk binary
102+
*.ttf binary
103+
*.woff binary
104+
*.woff2 binary
105+
*.xls binary
106+
*.xlsx binary
107+
99108

100109
###############################################################################
101110
# Set explicit file behavior to:
@@ -107,3 +116,4 @@
107116
*.pdf diff=astextplain
108117
*.pptx diff=astextplain
109118
*.rtf diff=astextplain
119+
*.svg diff=astextplain

Directory.Build.props

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,42 @@
2929
<SuppressNETCoreSdkPreviewMessage>true</SuppressNETCoreSdkPreviewMessage>
3030
</PropertyGroup>
3131

32+
<!--
33+
https://apisof.net/
34+
+===================+================+===================+==============================+======================+==========================+
35+
| Target Framework | SUPPORTS_MATHF | SUPPORTS_HASHCODE | SUPPORTS_EXTENDED_INTRINSICS | SUPPORTS_SPAN_STREAM | SUPPORTS_ENCODING_STRING |
36+
+===================+================+===================+==============================+======================+==========================+
37+
| netcoreapp3.1 | Y | Y | Y | Y | Y |
38+
| netcoreapp2.1 | Y | Y | Y | Y | Y |
39+
| netcoreapp2.0 | Y | N | N | N | N |
40+
| netstandard2.1 | Y | N | N | Y | Y |
41+
| netstandard2.0 | N | N | N | N | N |
42+
| netstandard1.3 | N | N | N | N | N |
43+
| net472 | N | N | Y | N | N |
44+
+===================+================+===================+==============================+======================+==========================+
45+
-->
46+
47+
<!-- TODO: Include additional targets to TargetFrameworks -->
48+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1'">
49+
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF;SUPPORTS_HASHCODE;SUPPORTS_EXTENDED_INTRINSICS;SUPPORTS_SPAN_STREAM;SUPPORTS_ENCODING_STRING</DefineConstants>
50+
</PropertyGroup>
51+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1'">
52+
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF;SUPPORTS_HASHCODE;SUPPORTS_EXTENDED_INTRINSICS;SUPPORTS_SPAN_STREAM;SUPPORTS_ENCODING_STRING</DefineConstants>
53+
</PropertyGroup>
54+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0'">
55+
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF;</DefineConstants>
56+
</PropertyGroup>
57+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.1'">
58+
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF;SUPPORTS_SPAN_STREAM;SUPPORTS_ENCODING_STRING</DefineConstants>
59+
</PropertyGroup>
60+
<PropertyGroup Condition=" '$(TargetFramework)' == 'net472'">
61+
<DefineConstants>$(DefineConstants);SUPPORTS_EXTENDED_INTRINSICS</DefineConstants>
62+
</PropertyGroup>
63+
3264
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
3365
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
3466
</PropertyGroup>
35-
67+
3668
<!--TODO: Check what this is testing for and why does it fail?-->
3769
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
3870
<CheckForOverflowUnderflow>false</CheckForOverflowUnderflow>

shared-infrastructure

src/ImageSharp.Drawing/ImageSharp.Drawing.csproj

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,10 @@
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>
21-
</PropertyGroup>
22-
2315
<ItemGroup>
2416
<PackageReference Include="SixLabors.Fonts" />
2517
<PackageReference Include="SixLabors.Shapes" />

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();

0 commit comments

Comments
 (0)