Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interop package to multi-target different operating systems. Eliminate native stub usage. #35

Merged
merged 11 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
os: linux
dist: trusty
dist: bionic

before_install:
- curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
- sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
- sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-trusty-prod trusty main" > /etc/apt/sources.list.d/dotnetdev.list'
- sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-bionic-prod bionic main" > /etc/apt/sources.list.d/dotnetdev.list'
- sudo apt-get install apt-transport-https
- sudo apt-get update -qq && sudo apt-get install -y dotnet-sdk-2.1.4 libopenslide0
- sudo apt-get update -qq && sudo apt-get install -y dotnet-sdk-2.1 libopenslide0

script:
- chmod +x test.sh
Expand Down
19 changes: 19 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Product>OpenSlideNET</Product>
<Authors>YiGolden</Authors>
<Copyright>Copyright (c) 2018-2019 yigolden</Copyright>
<Version>1.0.0-preview5-20022501</Version>
<InteropPackageVersion>1.0.0-preview5-20022501</InteropPackageVersion>
<PackageTags>openslide</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/yigolden/OpenSlideNET</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/yigolden/OpenSlideNET.git</RepositoryUrl>

<!-- Some dependencies havs not been strong signed -->
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)build\key.snk</AssemblyOriginatorKeyFile>
<!-- <SignAssembly>false</SignAssembly> -->
</PropertyGroup>
</Project>
File renamed without changes.
18 changes: 0 additions & 18 deletions dir.props

This file was deleted.

5 changes: 5 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"msbuild-sdks": {
"MSBuild.Sdk.Extras": "2.0.54"
}
}
11 changes: 11 additions & 0 deletions ref/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />

<PropertyGroup>
<LangVersion>7.3</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
</Project>
15 changes: 15 additions & 0 deletions ref/OpenSlideNET.Interop/OpenSlideNET.Interop.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="MSBuild.Sdk.Extras">

<PropertyGroup>
<TargetFrameworks>net46;netstandard2.0;netstandard2.1</TargetFrameworks>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.1'">
<PackageReference Include="System.Memory" Version="4.5.3" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\..\src\OpenSlideNET.Interop\**\*.cs" Exclude="..\..\src\OpenSlideNET.Interop\obj\**" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />

<PropertyGroup>
<LangVersion>7.3</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Buffers;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using static OpenSlideNET.DeepZoomGenerator;

Expand All @@ -21,10 +22,9 @@ private static int EnsureMinimumSize(int size)

private static void WriteImage(DeepZoomGenerator src, Image<Bgra32> dest, TileInfo tileInfo)
{
var frame = dest.Frames.RootFrame;
src.Image.DangerousReadRegion(
src.Image.ReadRegion(
tileInfo.SlideLevel, tileInfo.X, tileInfo.Y, tileInfo.Width, tileInfo.Height,
ref Unsafe.As<Bgra32, byte>(ref frame.DangerousGetPinnableReferenceToPixelBuffer()));
ref Unsafe.As<Bgra32, byte>(ref MemoryMarshal.GetReference(dest.GetPixelSpan())));

dest.Mutate(ctx =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;

namespace OpenSlideNET
Expand All @@ -19,13 +20,12 @@ private static int EnsureMinimumSize(int size)

private static Image<Bgra32> WriteImage(OpenSlideImage image, string name)
{
if (!image.TryGetAssociatedImageDimemsions(name, out var dims))
if (!image.TryGetAssociatedImageDimensions(name, out var dims))
{
throw new KeyNotFoundException();
}
var dest = new Image<Bgra32>((int)dims.Width, (int)dims.Height);
var frame = dest.Frames.RootFrame;
image.DangerousReadAssociatedImage(name, ref Unsafe.As<Bgra32, byte>(ref frame.DangerousGetPinnableReferenceToPixelBuffer()));
image.ReadAssociatedImage(name, ref Unsafe.As<Bgra32, byte>(ref MemoryMarshal.GetReference(dest.GetPixelSpan())));
return dest;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Buffers;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;

namespace OpenSlideNET
Expand All @@ -25,14 +26,14 @@ private static Image<Bgra32> GenerateThumbnail(OpenSlideImage image, int maxWidt
throw new ArgumentNullException(nameof(image));
}

(long width, long height) = image.Dimemsions;
(long width, long height) = image.Dimensions;

// 决定合适的level
double downsampleWidth = width / (double)maxWidth;
double downsampleHeight = height / (double)maxHeight;
double downsample = Math.Max(downsampleWidth, downsampleHeight);
int level = image.GetBestLevelForDownsample(downsample);
(long levelWidth, long levelHeight) = image.GetLevelDimemsions(level);
(long levelWidth, long levelHeight) = image.GetLevelDimensions(level);

// 计算目标大小
int targetWidth, targetHeight;
Expand All @@ -54,16 +55,11 @@ private static Image<Bgra32> GenerateThumbnail(OpenSlideImage image, int maxWidt
{
output = new Image<Bgra32>((int)levelWidth, (int)levelHeight);
}
image.ReadRegion(
level, 0, 0, levelWidth, levelHeight,
ref Unsafe.As<Bgra32, byte>(ref MemoryMarshal.GetReference(output.GetPixelSpan())));
output.Mutate(ctx =>
{
ctx.Apply(img =>
{
var frame = img.Frames.RootFrame;

image.DangerousReadRegion(
level, 0, 0, levelWidth, levelHeight,
ref Unsafe.As<Bgra32, byte>(ref frame.DangerousGetPinnableReferenceToPixelBuffer()));
});
ctx.Resize(targetWidth, targetHeight);
});
return output;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup>
<PackageId>OpenSlideNET.ImageExtensions</PackageId>
<Title>OpenSlideNET.ImageExtensions</Title>
<TargetFrameworks>netstandard1.3;netstandard2.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Description>.NET bindings for OpenSlide with Deep Zoom support.</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0005" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0007" />
<PackageReference Include="System.Buffers" Version="4.5.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.1" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.7.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace OpenSlideNET
namespace OpenSlideNET.Interop
{
internal static partial class Interop
public static partial class OpenSlideInterop
{
[DllImport(LibOpenSlide, EntryPoint = "openslide_get_associated_image_names", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr GetAssociatedImageNames_Internal(IntPtr osr);
private static extern IntPtr GetAssociatedImageNames_Internal(OpenSlideImageSafeHandle osr);

/// <summary>
/// Get the NULL-terminated array of associated image names.
/// Certain vendor-specific associated images may exist within a whole slide image. They are encoded as key-value pairs. This call provides a list of names as strings that can be used to read associated images with openslide_get_associated_image_dimensions() and openslide_read_associated_image().
/// </summary>
/// <param name="osr">The OpenSlide object. </param>
/// <returns>A NULL-terminated string array of associated image names, or an empty array if an error occurred. </returns>
public static unsafe string[] GetAssociatedImageNames(IntPtr osr)
public static unsafe string[] GetAssociatedImageNames(OpenSlideImageSafeHandle osr)
{
var list = new List<string>();
IntPtr* pCurrent = (IntPtr*)GetAssociatedImageNames_Internal(osr);
Expand All @@ -29,7 +29,7 @@ public static unsafe string[] GetAssociatedImageNames(IntPtr osr)
}

[DllImport(LibOpenSlide, EntryPoint = "openslide_get_associated_image_dimensions", CallingConvention = CallingConvention.Cdecl)]
private static extern void GetAssociatedImageDimemsions_Internal(IntPtr osr, IntPtr name, out long w, out long h);
private static extern void GetAssociatedImageDimensions_Internal(OpenSlideImageSafeHandle osr, IntPtr name, out long w, out long h);

/// <summary>
/// Get the dimensions of an associated image.
Expand All @@ -39,13 +39,13 @@ public static unsafe string[] GetAssociatedImageNames(IntPtr osr)
/// <param name="name">The name of the desired associated image. Must be a valid name as given by openslide_get_associated_image_names(). </param>
/// <param name="w">The width of the associated image, or -1 if an error occurred. </param>
/// <param name="h">The height of the associated image, or -1 if an error occurred. </param>
internal static unsafe void GetAssociatedImageDimemsions(IntPtr osr, string name, out long w, out long h)
public static unsafe void GetAssociatedImageDimensions(OpenSlideImageSafeHandle osr, string name, out long w, out long h)
{
byte* pointer = stackalloc byte[64];
UnsafeUtf8Encoder utf8Encoder = new UnsafeUtf8Encoder(pointer, 64);
try
{
GetAssociatedImageDimemsions_Internal(osr, utf8Encoder.Encode(name), out w, out h);
GetAssociatedImageDimensions_Internal(osr, utf8Encoder.Encode(name), out w, out h);
}
finally
{
Expand All @@ -54,7 +54,7 @@ internal static unsafe void GetAssociatedImageDimemsions(IntPtr osr, string name
}

[DllImport(LibOpenSlide, EntryPoint = "openslide_read_associated_image", CallingConvention = CallingConvention.Cdecl)]
private static unsafe extern void ReadAssociatedImage_Internal(IntPtr osr, IntPtr name, void* dest);
private static unsafe extern void ReadAssociatedImage_Internal(OpenSlideImageSafeHandle osr, IntPtr name, void* dest);

/// <summary>
/// Copy pre-multiplied ARGB data from an associated image.
Expand All @@ -63,7 +63,7 @@ internal static unsafe void GetAssociatedImageDimemsions(IntPtr osr, string name
/// <param name="osr">The OpenSlide object. </param>
/// <param name="name">The name of the desired associated image. Must be a valid name as given by openslide_get_associated_image_names(). </param>
/// <param name="dest">The destination buffer for the ARGB data. </param>
internal static unsafe void ReadAssociatedImage(IntPtr osr, string name, void* dest)
public static unsafe void ReadAssociatedImage(OpenSlideImageSafeHandle osr, string name, void* dest)
{
byte* pointer = stackalloc byte[64];
UnsafeUtf8Encoder utf8Encoder = new UnsafeUtf8Encoder(pointer, 64);
Expand Down
Loading