Skip to content

Commit

Permalink
Merge pull request #957 from shimat/UnmanagedType_LPStr
Browse files Browse the repository at this point in the history
enable UnmanagedType.LPUTF8Str for non-Windows platforms
  • Loading branch information
shimat authored May 27, 2020
2 parents 834856e + 870e55e commit f13c326
Show file tree
Hide file tree
Showing 20 changed files with 736 additions and 134 deletions.
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ init:
Write-Host "PLATFORM = "$env:PLATFORM
Write-Host "CONFIGURATION = "$env:CONFIGURATION
Write-Host "APPVEYOR_SAVE_CACHE_ON_ERROR = "$env:APPVEYOR_SAVE_CACHE_ON_ERROR
#- ps: $blockRdp = $true; iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1'))

#on_finish:
# - ps: $blockRdp = $true; iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1'))
Expand Down
2 changes: 1 addition & 1 deletion samples
Submodule samples updated 0 files
4 changes: 2 additions & 2 deletions src/OpenCvSharp/Modules/core/FileNodeIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace OpenCvSharp
/// <summary>
/// File Storage Node class
/// </summary>
public class FileNodeIterator : DisposableCvObject, IEquatable<FileNodeIterator>, IEnumerator<FileNode>
public class FileNodeIterator : DisposableCvObject, IEquatable<FileNodeIterator?>, IEnumerator<FileNode>
{
/// <summary>
/// The default constructor
Expand Down Expand Up @@ -141,7 +141,7 @@ public FileNodeIterator ReadRaw(string fmt, byte[] vec, long maxCount = int.MaxV
/// </summary>
/// <param name="it"></param>
/// <returns></returns>
public bool Equals(FileNodeIterator it)
public bool Equals(FileNodeIterator? it)
{
if (it is null)
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/OpenCvSharp/Modules/core/RNG.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public double Gaussian(double sigma)
}

/// <inheritdoc />
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (obj is RNG rng)
return Equals(rng);
Expand Down
2 changes: 1 addition & 1 deletion src/OpenCvSharp/Modules/core/Struct/Point3i.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public readonly bool Equals(Point3i other)
}

/// <inheritdoc />
public override readonly bool Equals(object obj)
public override readonly bool Equals(object? obj)
{
return obj is Point3i other && Equals(other);
}
Expand Down
2 changes: 1 addition & 1 deletion src/OpenCvSharp/Modules/core/Struct/Size2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public readonly bool Equals(Size2d other)
}

/// <inheritdoc />
public override readonly bool Equals(object obj)
public override readonly bool Equals(object? obj)
{
return obj is Size2d other && Equals(other);
}
Expand Down
2 changes: 1 addition & 1 deletion src/OpenCvSharp/Modules/videoio/FourCC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static FourCC FromInt32(int code)
}

/// <inheritdoc />
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (obj is FourCC enumValue)
return Equals(enumValue);
Expand Down
2 changes: 1 addition & 1 deletion src/OpenCvSharp/OpenCvSharp.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp2.1;net48;net461</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp2.1;netcoreapp3.1;net48;net461</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>OpenCvSharp</AssemblyName>
Expand Down
32 changes: 24 additions & 8 deletions src/OpenCvSharp/PInvoke/NativeMethods/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ public static partial class NativeMethods
//public const string DllFfmpegX86 = "opencv_videoio_ffmpeg430";
//public const string DllFfmpegX64 = "opencv_videoio_ffmpeg430_64";

private const UnmanagedType StringUnmanagedType =
#if NETSTANDARD2_1 || NETCOREAPP2_1 || NET48
UnmanagedType.LPUTF8Str;
#else
//private const UnmanagedType StringUnmanagedType = UnmanagedType.LPStr;

private const UnmanagedType StringUnmanagedTypeWindows = UnmanagedType.LPStr;

private const UnmanagedType StringUnmanagedTypeNotWindows =
#if NET461 || NETSTANDARD2_0
UnmanagedType.LPStr;
#else
UnmanagedType.LPUTF8Str;
#endif

/// <summary>
Expand All @@ -53,8 +57,7 @@ static NativeMethods()
TryPInvoke();
}

// ReSharper disable once StringLiteralTypo
//[Conditional("DOTNETCORE")]
#pragma warning disable CA1801
public static void HandleException(ExceptionStatus status)
{
#if DOTNETCORE
Expand All @@ -63,9 +66,10 @@ public static void HandleException(ExceptionStatus status)
{
ExceptionHandler.ThrowPossibleException();
}
#else
#else
#endif
}
#pragma warning restore CA1801

/// <summary>
/// Load DLL files dynamically using Win32 LoadLibrary
Expand Down Expand Up @@ -102,6 +106,7 @@ public static void LoadLibraries(IEnumerable<string>? additionalPaths = null)
/// </summary>
public static void TryPInvoke()
{
#pragma warning disable CA1031
if (tried)
return;
tried = true;
Expand Down Expand Up @@ -144,6 +149,7 @@ public static void TryPInvoke()
catch { }
throw;
}
#pragma warning restore CA1031
}

/// <summary>
Expand All @@ -152,7 +158,11 @@ public static void TryPInvoke()
/// <returns></returns>
public static bool IsWindows()
{
#if NET461
return !IsUnix();
#else
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
#endif
}

/// <summary>
Expand All @@ -161,11 +171,15 @@ public static bool IsWindows()
/// <returns></returns>
public static bool IsUnix()
{
#if DOTNET_FRAMEWORK
#if NET461
var p = Environment.OSVersion.Platform;
return (p == PlatformID.Unix ||
p == PlatformID.MacOSX ||
(int)p == 128);
#elif NETCOREAPP3_1
return RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ||
RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD);
#else
return RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
Expand Down Expand Up @@ -193,9 +207,11 @@ public static bool IsMono()
public static readonly CvErrorCallback ErrorHandlerIgnorance =
(status, funcName, errMsg, fileName, line, userData) => 0;

#pragma warning disable CA2211
/// <summary>
/// Default error handler
/// </summary>
public static CvErrorCallback? ErrorHandlerDefault = null;
#pragma warning restore CA2211
}
}
121 changes: 103 additions & 18 deletions src/OpenCvSharp/PInvoke/NativeMethods/NativeMethods_imgcodecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,87 @@ namespace OpenCvSharp
{
static partial class NativeMethods
{
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true)]
public static extern ExceptionStatus imgcodecs_imread(
[MarshalAs(StringUnmanagedType)] string filename, int flags, out IntPtr returnValue);
// imread

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true)]
public static extern ExceptionStatus imgcodecs_imreadmulti(
[MarshalAs(StringUnmanagedType)] string filename, IntPtr mats, int flags, out int returnValue);
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true,
EntryPoint = "imgcodecs_imread")]
public static extern ExceptionStatus imgcodecs_imread_NotWindows(
[MarshalAs(StringUnmanagedTypeNotWindows)] string fileName, int flags, out IntPtr returnValue);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true)]
public static extern ExceptionStatus imgcodecs_imwrite(
[MarshalAs(StringUnmanagedType)] string filename, IntPtr img, [In] int[] @params, int paramsLength, out int returnValue);
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true,
EntryPoint = "imgcodecs_imread")]
public static extern ExceptionStatus imgcodecs_imread_Windows(
[MarshalAs(StringUnmanagedTypeWindows)] string fileName, int flags, out IntPtr returnValue);

[Pure]
public static ExceptionStatus imgcodecs_imread(string fileName, int flags, out IntPtr returnValue)
{
if (IsWindows())
return imgcodecs_imread_Windows(fileName, flags, out returnValue);
return imgcodecs_imread_NotWindows(fileName, flags, out returnValue);
}

// imreadmulti

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true,
EntryPoint = "imgcodecs_imreadmulti")]
public static extern ExceptionStatus imgcodecs_imreadmulti_NotWindows(
[MarshalAs(StringUnmanagedTypeNotWindows)] string fileName, IntPtr mats, int flags, out int returnValue);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true,
EntryPoint = "imgcodecs_imreadmulti")]
public static extern ExceptionStatus imgcodecs_imreadmulti_Windows(
[MarshalAs(StringUnmanagedTypeWindows)] string fileName, IntPtr mats, int flags, out int returnValue);

[Pure]
public static ExceptionStatus imgcodecs_imreadmulti(string fileName, IntPtr mats, int flags, out int returnValue)
{
if (IsWindows())
return imgcodecs_imreadmulti_Windows(fileName, mats, flags, out returnValue);
return imgcodecs_imreadmulti_NotWindows(fileName, mats, flags, out returnValue);
}

// imwrite

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true,
EntryPoint = "imgcodecs_imwrite")]
public static extern ExceptionStatus imgcodecs_imwrite_NotWindows(
[MarshalAs(StringUnmanagedTypeNotWindows)] string fileName, IntPtr img, [In] int[] @params, int paramsLength, out int returnValue);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true,
EntryPoint = "imgcodecs_imwrite")]
public static extern ExceptionStatus imgcodecs_imwrite_Windows(
[MarshalAs(StringUnmanagedTypeWindows)] string fileName, IntPtr img, [In] int[] @params, int paramsLength, out int returnValue);

[Pure]
public static ExceptionStatus imgcodecs_imwrite(string fileName, IntPtr img, int[] @params, int paramsLength, out int returnValue)
{
if (IsWindows())
return imgcodecs_imwrite_Windows(fileName, img, @params, paramsLength, out returnValue);
return imgcodecs_imwrite_NotWindows(fileName, img, @params, paramsLength, out returnValue);
}

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true, CharSet = CharSet.Ansi)]
public static extern ExceptionStatus imgcodecs_imwrite_multi(
[MarshalAs(StringUnmanagedType)] string filename, IntPtr img, [In] int[] @params, int paramsLength, out int returnValue);
// imwrite_multi

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true,
EntryPoint = "imgcodecs_imwrite_multi")]
public static extern ExceptionStatus imgcodecs_imwrite_multi_NotWindows(
[MarshalAs(StringUnmanagedTypeNotWindows)] string fileName, IntPtr img, [In] int[] @params, int paramsLength, out int returnValue);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true,
EntryPoint = "imgcodecs_imwrite_multi")]
public static extern ExceptionStatus imgcodecs_imwrite_multi_Windows(
[MarshalAs(StringUnmanagedTypeWindows)] string fileName, IntPtr img, [In] int[] @params, int paramsLength, out int returnValue);

[Pure]
public static ExceptionStatus imgcodecs_imwrite_multi(string fileName, IntPtr img, int[] @params, int paramsLength, out int returnValue)
{
if (IsWindows())
return imgcodecs_imwrite_multi_Windows(fileName, img, @params, paramsLength, out returnValue);
return imgcodecs_imwrite_multi_NotWindows(fileName, img, @params, paramsLength, out returnValue);
}

//

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus imgcodecs_imdecode_Mat(
Expand All @@ -35,17 +101,36 @@ public static extern ExceptionStatus imgcodecs_imdecode_vector(
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus imgcodecs_imdecode_InputArray(
IntPtr buf, int flags, out IntPtr returnValue);


// Do not consider that "ext" may not be ASCII characters
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true)]
public static extern ExceptionStatus imgcodecs_imencode_vector(
[MarshalAs(StringUnmanagedType)] string ext, IntPtr img, IntPtr buf, [In] int[] @params, int paramsLength, out int returnValue);
[MarshalAs(UnmanagedType.LPStr)] string ext, IntPtr img, IntPtr buf, [In] int[] @params, int paramsLength, out int returnValue);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true)]
public static extern ExceptionStatus imgcodecs_haveImageReader(
[MarshalAs(StringUnmanagedType)] string fileName, out int returnValue);
// haveImageReader

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true,
EntryPoint = "imgcodecs_imwrite")]
public static extern ExceptionStatus imgcodecs_haveImageReader_NotWindows(
[MarshalAs(StringUnmanagedTypeNotWindows)] string fileName, out int returnValue);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true,
EntryPoint = "imgcodecs_imwrite")]
public static extern ExceptionStatus imgcodecs_haveImageReader_Windows(
[MarshalAs(StringUnmanagedTypeWindows)] string fileName, out int returnValue);

[Pure]
public static ExceptionStatus imgcodecs_haveImageReader(string fileName, out int returnValue)
{
if (IsWindows())
return imgcodecs_haveImageReader_Windows(fileName, out returnValue);
return imgcodecs_haveImageReader_NotWindows(fileName, out returnValue);
}

// haveImageWriter

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true)]
public static extern ExceptionStatus imgcodecs_haveImageWriter(
[MarshalAs(StringUnmanagedType)] string fileName, out int returnValue);
[MarshalAs(UnmanagedType.LPStr)] string fileName, out int returnValue);
}
}
Loading

0 comments on commit f13c326

Please sign in to comment.