Skip to content

Commit

Permalink
add net7.0 target c# 11 goodies
Browse files Browse the repository at this point in the history
  • Loading branch information
saucecontrol committed May 27, 2022
1 parent 32195e6 commit 067d63c
Show file tree
Hide file tree
Showing 27 changed files with 66 additions and 70 deletions.
6 changes: 4 additions & 2 deletions azure-pipelines.yml
Expand Up @@ -5,6 +5,7 @@ trigger:
- master
paths:
exclude:
- doc
- readme.md

variables:
Expand Down Expand Up @@ -110,9 +111,10 @@ jobs:
targetFolder: out/vcpkg/install

- task: UseDotNet@2
displayName: Ensure 6.0 SDK
displayName: Ensure 7.0 SDK
inputs:
version: 6.0.x
version: 7.0.x
includePreviewVersions: true

- script: |
dotnet build -c Dist --version-suffix ci$(Build.BuildNumber) src/MagicScaler
Expand Down
5 changes: 5 additions & 0 deletions build/Common.props
Expand Up @@ -18,6 +18,7 @@
<PackageReleaseNotes>See $(RepositoryUrl)/releases for release-specific notes.</PackageReleaseNotes>

<LangVersion>preview</LangVersion>
<WarningLevel>7</WarningLevel>
<Features>strict</Features>
<Nullable>annotations</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down Expand Up @@ -54,6 +55,10 @@
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="4.3.0-2.22270.4" PrivateAssets="all" />
</ItemGroup>

<ItemGroup Condition="'$(Configuration)'=='Dist' Or '$(Configuration)'=='Coverage'">
<None Include="$(MSBuildThisFileDirectory)$(Company).png" Pack="true" PackagePath="/" />
<None Include="$(ProjectRoot)readme.md" Pack="true" PackagePath="/" />
Expand Down
1 change: 1 addition & 0 deletions nuget.config
Expand Up @@ -2,5 +2,6 @@
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="dotnet-tools" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" />
</packageSources>
</configuration>
7 changes: 6 additions & 1 deletion src/Directory.Build.props
Expand Up @@ -4,8 +4,13 @@
<Import Project="$(MSBuildThisFileDirectory)..\build\Branding.props" />
<Import Project="$(MSBuildThisFileDirectory)..\build\FrameworkVersions.props" />

<PropertyGroup>
<DefineConstants Condition="$([MSBuild]::VersionGreaterThanOrEquals($(NetStandardVersion), '2.1'))">$(DefineConstants);BUILTIN_NULLABLE</DefineConstants>
</PropertyGroup>

<PropertyGroup>
<Nullable>annotations</Nullable>
<Nullable Condition="$(DefineConstants.Contains('BUILTIN_NULLABLE'))">enable</Nullable>
<SignAssembly>true</SignAssembly>
</PropertyGroup>

Expand All @@ -28,7 +33,7 @@
</ItemGroup>

<ItemGroup Condition="'$(Configuration)'=='Dist'">
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="all" />
<PackageReference Include="SauceControl.InheritDoc" Version="1.3.0" PrivateAssets="all" />
</ItemGroup>

Expand Down
1 change: 1 addition & 0 deletions src/Directory.Build.targets
Expand Up @@ -5,6 +5,7 @@
<ItemGroup>
<AssemblyInfoLines Include="[assembly:System.CLSCompliant(true)]" />
<AssemblyInfoLines Include="[assembly:System.Runtime.InteropServices.ComVisible(false)]" />
<AssemblyInfoLines Include="[assembly:System.Runtime.CompilerServices.DisableRuntimeMarshalling]" Condition="$([MSBuild]::VersionGreaterThanOrEquals($(NetCoreVersion), '7.0'))" />
<AssemblyInfoLines Include="[module:System.Runtime.CompilerServices.SkipLocalsInit]" />
</ItemGroup>

Expand Down
18 changes: 9 additions & 9 deletions src/MagicScaler/Core/PixelBuffer.cs
Expand Up @@ -17,6 +17,8 @@ internal sealed class PixelBuffer<T> : IDisposable where T : struct, BufferType
private byte[]? buffArray;
private byte buffOffset;

public readonly int Stride;

private int buffLength
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -50,13 +52,6 @@ private int consumed
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private (int, int) getValidRange() => typeof(T) == typeof(BufferType.Windowed) && loaded > read
? (start + loaded - read, read)
: (start, loaded);

public readonly int Stride;

public PixelBuffer(int minLines, int stride, T param = default)
{
capacity = minLines;
Expand All @@ -65,14 +60,19 @@ public PixelBuffer(int minLines, int stride, T param = default)
read = typeof(T) == typeof(BufferType.Windowed) ? minLines : 0;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private (int, int) getValidRange() => typeof(T) == typeof(BufferType.Windowed) && loaded > read
? (start + loaded - read, read)
: (start, loaded);

private Span<byte> init(int first, int lines)
{
if (buffArray is null)
{
if (capacity == 0)
throw new ObjectDisposedException(nameof(PixelBuffer<T>));

var buff = BufferPool.RentRawAligned(window + Math.Max(capacity, lines) * Stride);
var buff = BufferPool.RentRawAligned(checked(window + Math.Max(capacity, lines) * Stride));

buffArray = buff.Array!;
buffOffset = (byte)buff.Offset;
Expand All @@ -95,7 +95,7 @@ private Span<byte> init(int first, int lines)

private void grow(int cbKeep, int cbKill)
{
var tbuff = BufferPool.RentRawAligned(window + capacity * Stride * 2);
var tbuff = BufferPool.RentRawAligned(checked(window + capacity * Stride * 2));

if (cbKeep > 0)
Unsafe.CopyBlockUnaligned(ref tbuff.Array![tbuff.Offset], ref buffArray![buffOffset + cbKill], (uint)cbKeep);
Expand Down
2 changes: 1 addition & 1 deletion src/MagicScaler/Magic/HybridScaleTransform.cs
Expand Up @@ -69,7 +69,7 @@ protected override unsafe void CopyPixelsInternal(in PixelArea prc, int cbStride
new Span<byte>(bp, iw - iiw).Fill(bp[-1]);
break;
case 3:
new Span<triple>(bp, iw - iiw).Fill(((triple*)bp)[-1]);
new Span<Triple>(bp, iw - iiw).Fill(((Triple*)bp)[-1]);
break;
case 4:
new Span<uint>(bp, iw - iiw).Fill(((uint*)bp)[-1]);
Expand Down
2 changes: 1 addition & 1 deletion src/MagicScaler/Magic/PadTransform.cs
Expand Up @@ -45,7 +45,7 @@ protected override unsafe void CopyPixelsInternal(in PixelArea prc, int cbStride
new Span<byte>(pbBuffer, prc.Width).Fill((byte)fill);
break;
case 3:
new Span<triple>(pbBuffer, prc.Width).Fill((triple)fill);
new Span<Triple>(pbBuffer, prc.Width).Fill((Triple)fill);
break;
case 4:
new Span<uint>(pbBuffer, prc.Width).Fill(fill);
Expand Down
2 changes: 1 addition & 1 deletion src/MagicScaler/Magic/PixelSource.cs
Expand Up @@ -181,7 +181,7 @@ public FrameBufferSource(int width, int height, PixelFormat format, bool multidi

Stride = MathUtil.PowerOfTwoCeiling(width * Format.BytesPerPixel, HWIntrinsics.VectorCount<byte>());

frameBuff = BufferPool.RentAligned<byte>(Stride * height);
frameBuff = BufferPool.RentAligned<byte>(checked(Stride * height));
}

protected override void Dispose(bool disposing)
Expand Down
2 changes: 1 addition & 1 deletion src/MagicScaler/Magic/TestPatternPixelSource.cs
Expand Up @@ -82,7 +82,7 @@ private unsafe byte[] getPattern()
new Span<byte>(bp, barWidth).Fill((byte)barVal);
break;
case 3:
new Span<triple>(bp, barWidth).Fill((triple)barVal);
new Span<Triple>(bp, barWidth).Fill((Triple)barVal);
break;
case 4:
new Span<uint>(bp, barWidth).Fill((uint)byte.MaxValue << 24 | barVal);
Expand Down
10 changes: 3 additions & 7 deletions src/MagicScaler/MagicScaler.csproj
Expand Up @@ -2,24 +2,20 @@

<PropertyGroup>
<VersionPrefix>0.13.1</VersionPrefix>
<TargetFrameworks>net6.0;net472</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net472</TargetFrameworks>
<TargetFrameworks Condition="'$(Configuration)'=='Dist' Or '$(Configuration)'=='Coverage'">$(TargetFrameworks);net461;netcoreapp3.1;netstandard2.0;netstandard2.1</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
<DefineConstants Condition="$([MSBuild]::VersionGreaterThanOrEquals($(NetStandardVersion), '2.1'))">$(DefineConstants);BUILTIN_MATHF;BUILTIN_SPAN;BUILTIN_NULLABLE</DefineConstants>
<DefineConstants Condition="$([MSBuild]::VersionGreaterThanOrEquals($(NetStandardVersion), '2.1'))">$(DefineConstants);BUILTIN_MATHF;BUILTIN_SPAN</DefineConstants>
<DefineConstants Condition="$([MSBuild]::VersionGreaterThanOrEquals($(NetStandardVersion), '2.1')) Or $([MSBuild]::VersionGreaterThanOrEquals($(NetFrameworkVersion), '4.7.2'))">$(DefineConstants);VECTOR_CONVERT</DefineConstants>
<DefineConstants Condition="$([MSBuild]::VersionGreaterThanOrEquals($(NetCoreVersion), '3.0'))">$(DefineConstants);HWINTRINSICS</DefineConstants>
<DefineConstants Condition="'$(Configuration)'!='Dist'">$(DefineConstants);GUARDRAILS;WICPROCESSOR;GDIPROCESSOR</DefineConstants>
</PropertyGroup>

<PropertyGroup>
<Nullable Condition="$(DefineConstants.Contains('BUILTIN_NULLABLE'))">enable</Nullable>
</PropertyGroup>

<ItemGroup Condition="!$(DefineConstants.Contains('BUILTIN_SPAN'))">
<PackageReference Include="System.Buffers" Version="4.5.1" />
<PackageReference Include="System.Memory" Version="4.5.4" />
<PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.3" />
<PackageReference Include="IndexRange" Version="1.0.2" />
Expand Down
7 changes: 3 additions & 4 deletions src/MagicScaler/Metadata/ExifReader.cs
Expand Up @@ -19,7 +19,6 @@ namespace PhotoSauce.MagicScaler;

private ExifReader(ReadOnlySpan<byte> src, int offset, bool bswap)
{
this = default;
ushort cnt = MemoryMarshal.Read<ushort>(src[offset..]);
if (bswap)
cnt = BufferUtil.ReverseEndianness(cnt);
Expand Down Expand Up @@ -74,11 +73,11 @@ public static ExifReader Create(ReadOnlySpan<byte> span)

public readonly T CoerceValue<T>(in ExifItem item) where T : unmanaged
{
if (!UnsafeUtil.IsIntegerPrimitive<T>() && !UnsafeUtil.IsFloatingPrimitive<T>())
if (!UnsafeUtil.IsIntegerPrimitive<T>() && !UnsafeUtil.IsFloatPrimitive<T>())
throw new ArgumentException($"Coercion not implemented for type {typeof(T).Name}.", nameof(T));

int dlen = item.Type.GetElementSize();
if (dlen == sizeof(T) && item.Type.IsFloating() == UnsafeUtil.IsFloatingPrimitive<T>())
if (dlen == sizeof(T) && item.Type.IsFloating() == UnsafeUtil.IsFloatPrimitive<T>())
return GetValue<T>(item);

if (typeof(T) == typeof(float))
Expand Down Expand Up @@ -174,7 +173,7 @@ public bool MoveNext()
return true;
}

public unsafe readonly ref readonly ExifItem Current
public readonly ref readonly ExifItem Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#if BUILTIN_SPAN
Expand Down
1 change: 0 additions & 1 deletion src/MagicScaler/Metadata/ExifWriter.cs
Expand Up @@ -32,7 +32,6 @@ private ExifWriter(int tagCount, int dataLength)
dataOffset = writer.Position + tagCount * ExifConstants.MinTagLength + nextIfdLength;
tagWriter = buffer.Span.AsWriter(writer.Position..dataOffset);
dataWriter = buffer.Span.AsWriter(dataOffset..);
length = 0;
}

public static ExifWriter Create(int tagCount, int dataLength) => new(tagCount, dataLength);
Expand Down
12 changes: 2 additions & 10 deletions src/MagicScaler/Utilities/BufferUtil.cs
Expand Up @@ -15,11 +15,7 @@ namespace PhotoSauce.MagicScaler;

public readonly int Position => (int)pos;

public SpanBufferReader(ReadOnlySpan<byte> buff)
{
span = buff;
pos = default;
}
public SpanBufferReader(ReadOnlySpan<byte> buff) => span = buff;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Read<T>() where T : unmanaged
Expand Down Expand Up @@ -50,11 +46,7 @@ public SpanBufferReader(ReadOnlySpan<byte> buff)

public readonly int Position => (int)pos;

public SpanBufferWriter(Span<byte> buff)
{
span = buff;
pos = default;
}
public SpanBufferWriter(Span<byte> buff) => span = buff;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Write<T>(T val) where T : unmanaged
Expand Down
7 changes: 1 addition & 6 deletions src/MagicScaler/Utilities/CacheHash.cs
Expand Up @@ -14,12 +14,7 @@ internal static class CacheHash
{
public const int DigestLength = 5;

private static ReadOnlySpan<byte> base32Table => new[] {
(byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G', (byte)'H',
(byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N', (byte)'O', (byte)'P',
(byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U', (byte)'V', (byte)'W', (byte)'X',
(byte)'Y', (byte)'Z', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7'
};
private static ReadOnlySpan<byte> base32Table => "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"u8;

// first 40 bits from the crypto hash, base32 encoded
// https://tools.ietf.org/html/rfc4648#section-6
Expand Down
8 changes: 4 additions & 4 deletions src/MagicScaler/Utilities/MathUtil.cs
Expand Up @@ -15,18 +15,18 @@
namespace PhotoSauce.MagicScaler
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal readonly struct triple
internal readonly struct Triple
{
public readonly ushort v1;
public readonly byte v2;

public triple(uint v)
public Triple(uint v)
{
v1 = (ushort)v;
v2 = (byte)(v >> 16);
}

public static explicit operator triple(uint v) => new(v);
public static explicit operator Triple(uint v) => new(v);
}

internal static class MathUtil
Expand Down Expand Up @@ -138,7 +138,7 @@ internal static class MathUtil
public static byte UnFix22ToByte(uint x) => ClampToByte(UnFix22(x));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int DivCeiling(int x, int y) => (x + (y - 1)) / y;
public static int DivCeiling(int x, int y) => (int)(((uint)x + ((uint)y - 1)) / (uint)y);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int PowerOfTwoFloor(int x, int powerOfTwo) => x & ~(powerOfTwo - 1);
Expand Down
8 changes: 8 additions & 0 deletions src/MagicScaler/Utilities/MiscExtensions.cs
Expand Up @@ -43,13 +43,20 @@ public static void TryReturn<T>(this ArrayPool<T> pool, T[]? buff)

public static void FillBuffer(this Stream stm, Span<byte> buff)
{
#if NET7_0_OR_GREATER
stm.ReadExactly(buff);
#else
int rem = buff.Length;
while (rem > 0)
rem -= stm.Read(buff[^rem..]);
#endif
}

public static int TryFillBuffer(this Stream stm, Span<byte> buff)
{
#if NET7_0_OR_GREATER
return stm.ReadAtLeast(buff, buff.Length, false);
#else
int cb = buff.Length;
int rb;
do
Expand All @@ -60,6 +67,7 @@ public static int TryFillBuffer(this Stream stm, Span<byte> buff)
while (rb != 0 && buff.Length != 0);

return cb - buff.Length;
#endif
}

public static Guid FinalizeToGuid<T>(this T hasher) where T : IBlake2Incremental
Expand Down
2 changes: 1 addition & 1 deletion src/MagicScaler/Utilities/SimpleLruCache.cs
Expand Up @@ -117,7 +117,7 @@ public TValue GetOrAdd(TKey key, TValue value)
count++;
}

for (; count > maxItems; --count)
for (; count > maxItems; count--)
{
var last = tail!.Prev!;
last.Next = null;
Expand Down
2 changes: 1 addition & 1 deletion src/MagicScaler/Utilities/UnsafeUtil.cs
Expand Up @@ -60,7 +60,7 @@ internal static unsafe class UnsafeUtil
typeof(T) == typeof(nuint);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsFloatingPrimitive<T>() where T : unmanaged =>
public static bool IsFloatPrimitive<T>() where T : unmanaged =>
typeof(T) == typeof(float) ||
typeof(T) == typeof(double);

Expand Down
1 change: 0 additions & 1 deletion src/MagicScaler/WIC/IStreamImpl.cs
Expand Up @@ -30,7 +30,6 @@ private IStreamImpl(Stream managedSource, uint offs = 0)
lpVtbl = vtblStatic;
source = GCHandle.Alloc(managedSource, GCHandleType.Weak);
offset = offs;
refCount = 0;
}

public static IStream* Wrap(Stream managedSource, uint offs = 0)
Expand Down
1 change: 0 additions & 1 deletion src/MagicScaler/WIC/IWICBitmapSourceImpl.cs
Expand Up @@ -26,7 +26,6 @@ private IWICBitmapSourceImpl(PixelSource managedSource)
{
lpVtbl = vtblStatic;
source = GCHandle.Alloc(managedSource, GCHandleType.Weak);
refCount = 0;
}

public static IWICBitmapSource* Wrap(PixelSource managedSource)
Expand Down
8 changes: 2 additions & 6 deletions src/MagicScaler/WIC/WicImageContainer.cs
Expand Up @@ -67,12 +67,8 @@ protected virtual void Dispose(bool disposing)

internal sealed unsafe class WicGifContainer : WicImageContainer, IMetadataSource
{
public static ReadOnlySpan<byte> Animexts1_0 => new[] {
(byte)'A', (byte)'N', (byte)'I', (byte)'M', (byte)'E', (byte)'X', (byte)'T', (byte)'S', (byte)'1', (byte)'.', (byte)'0'
};
public static ReadOnlySpan<byte> Netscape2_0 => new[] {
(byte)'N', (byte)'E', (byte)'T', (byte)'S', (byte)'C', (byte)'A', (byte)'P', (byte)'E', (byte)'2', (byte)'.', (byte)'0'
};
public static ReadOnlySpan<byte> Animexts1_0 => "ANIMEXTS1.0"u8;
public static ReadOnlySpan<byte> Netscape2_0 => "NETSCAPE2.0"u8;

public readonly AnimationContainer AnimationMetadata;

Expand Down
3 changes: 1 addition & 2 deletions src/MagicScaler/WIC/WicImageFrame.cs
Expand Up @@ -257,8 +257,7 @@ private WicColorProfile matchProfile(ReadOnlySpan<IntPtr> profiles, PixelFormat

if (ecs == (uint)ExifColorSpace.Uncalibrated)
{
var r03 = (ReadOnlySpan<byte>)(new[] { (byte)'R', (byte)'0', (byte)'3' });
if (WicMetadataReader->GetValueOrDefault(Container.MimeType == ImageMimeTypes.Jpeg ? Wic.Metadata.InteropIndexJpeg : Wic.Metadata.InteropIndexExif, buff).SequenceEqual(r03))
if (WicMetadataReader->GetValueOrDefault(Container.MimeType == ImageMimeTypes.Jpeg ? Wic.Metadata.InteropIndexJpeg : Wic.Metadata.InteropIndexExif, buff).SequenceEqual("R03"u8))
return WicColorProfile.AdobeRgb.Value;
}
}
Expand Down

0 comments on commit 067d63c

Please sign in to comment.