Skip to content

Commit 2519ec3

Browse files
committed
fix code issues by ReSharper
1 parent 352c778 commit 2519ec3

File tree

192 files changed

+731
-1614
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

192 files changed

+731
-1614
lines changed

OpenCvSharp.sln.DotSettings

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
1313
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
1414
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
15+
<s:Boolean x:Key="/Default/GrammarAndSpelling/GrammarChecking/Exceptions/=multi_002Dchannel/@EntryIndexedValue">True</s:Boolean>
16+
<s:Boolean x:Key="/Default/GrammarAndSpelling/GrammarChecking/Exceptions/=multi_002Ddimensional/@EntryIndexedValue">True</s:Boolean>
1517
<s:Boolean x:Key="/Default/UserDictionary/Words/=AGAST/@EntryIndexedValue">True</s:Boolean>
1618
<s:Boolean x:Key="/Default/UserDictionary/Words/=AKAZE/@EntryIndexedValue">True</s:Boolean>
1719
<s:Boolean x:Key="/Default/UserDictionary/Words/=Argb/@EntryIndexedValue">True</s:Boolean>
@@ -105,6 +107,7 @@
105107
<s:Boolean x:Key="/Default/UserDictionary/Words/=ONNX/@EntryIndexedValue">True</s:Boolean>
106108
<s:Boolean x:Key="/Default/UserDictionary/Words/=opencv/@EntryIndexedValue">True</s:Boolean>
107109
<s:Boolean x:Key="/Default/UserDictionary/Words/=Otsu/@EntryIndexedValue">True</s:Boolean>
110+
<s:Boolean x:Key="/Default/UserDictionary/Words/=prms/@EntryIndexedValue">True</s:Boolean>
108111
<s:Boolean x:Key="/Default/UserDictionary/Words/=PSNR/@EntryIndexedValue">True</s:Boolean>
109112
<s:Boolean x:Key="/Default/UserDictionary/Words/=Ptrs/@EntryIndexedValue">True</s:Boolean>
110113
<s:Boolean x:Key="/Default/UserDictionary/Words/=RANSAC/@EntryIndexedValue">True</s:Boolean>
@@ -143,6 +146,7 @@
143146
<s:Boolean x:Key="/Default/UserDictionary/Words/=Vecs/@EntryIndexedValue">True</s:Boolean>
144147
<s:Boolean x:Key="/Default/UserDictionary/Words/=videoio/@EntryIndexedValue">True</s:Boolean>
145148
<s:Boolean x:Key="/Default/UserDictionary/Words/=Voronoi/@EntryIndexedValue">True</s:Boolean>
149+
146150
<s:Boolean x:Key="/Default/UserDictionary/Words/=Writeable/@EntryIndexedValue">True</s:Boolean>
147151
<s:Boolean x:Key="/Default/UserDictionary/Words/=ximgproc/@EntryIndexedValue">True</s:Boolean>
148152
<s:Boolean x:Key="/Default/UserDictionary/Words/=xphoto/@EntryIndexedValue">True</s:Boolean>

src/OpenCvSharp/Cv2/Cv2_imgcodecs.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ public static bool ImWrite(string fileName, Mat img, int[]? prms = null)
5555
throw new ArgumentNullException(nameof(fileName));
5656
if (img is null)
5757
throw new ArgumentNullException(nameof(img));
58-
if (prms is null)
59-
prms = Array.Empty<int>();
58+
prms ??= [];
6059

6160
NativeMethods.HandleException(
6261
NativeMethods.imgcodecs_imwrite(fileName, img.CvPtr, prms, prms.Length, out var ret));
@@ -100,7 +99,7 @@ public static bool ImWrite(string fileName, IEnumerable<Mat> img, int[]? prms =
10099
throw new ArgumentNullException(nameof(fileName));
101100
if (img is null)
102101
throw new ArgumentNullException(nameof(img));
103-
prms ??= Array.Empty<int>();
102+
prms ??= [];
104103

105104
using var imgVec = new VectorOfMat(img);
106105
NativeMethods.HandleException(
@@ -218,8 +217,7 @@ public static bool ImEncode(string ext, InputArray img, out byte[] buf, int[]? p
218217
throw new ArgumentNullException(nameof(ext));
219218
if (img is null)
220219
throw new ArgumentNullException(nameof(img));
221-
if (prms is null)
222-
prms = Array.Empty<int>();
220+
prms ??= [];
223221
img.ThrowIfDisposed();
224222

225223
using var bufVec = new VectorOfByte();

src/OpenCvSharp/Fundamentals/CvObject.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,5 @@ protected CvObject(IntPtr ptr)
3232
/// <summary>
3333
/// Native pointer of OpenCV structure
3434
/// </summary>
35-
public IntPtr CvPtr
36-
{
37-
get { return ptr; }
38-
}
35+
public IntPtr CvPtr => ptr;
3936
}
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,79 @@
11
#if DOTNETCORE
2-
using System;
3-
using System.Threading;
42

5-
namespace OpenCvSharp.Internal
3+
namespace OpenCvSharp.Internal;
4+
5+
/// <summary>
6+
/// This static class defines one instance which than can be used by multiple threads to gather exception information from OpenCV
7+
/// Implemented as a singleton
8+
/// </summary>
9+
public static class ExceptionHandler
610
{
11+
// ThreadLocal variables to save the exception for the current thread
12+
private static readonly ThreadLocal<bool> exceptionHappened = new(false);
13+
private static readonly ThreadLocal<ErrorCode> localStatus = new();
14+
private static readonly ThreadLocal<string> localFuncName = new();
15+
private static readonly ThreadLocal<string> localErrMsg = new();
16+
private static readonly ThreadLocal<string> localFileName = new();
17+
private static readonly ThreadLocal<int> localLine = new();
18+
719
/// <summary>
8-
/// This static class defines one instance which than can be used by multiple threads to gather exception information from OpenCV
9-
/// Implemented as a singleton
20+
/// Callback function invoked by OpenCV when exception occurs
21+
/// Stores the information locally for every thread
1022
/// </summary>
11-
public static class ExceptionHandler
12-
{
13-
// ThreadLocal variables to save the exception for the current thread
14-
private static readonly ThreadLocal<bool> exceptionHappened = new ThreadLocal<bool>(false);
15-
private static readonly ThreadLocal<ErrorCode> localStatus = new ThreadLocal<ErrorCode>();
16-
private static readonly ThreadLocal<string> localFuncName = new ThreadLocal<string>();
17-
private static readonly ThreadLocal<string> localErrMsg = new ThreadLocal<string>();
18-
private static readonly ThreadLocal<string> localFileName = new ThreadLocal<string>();
19-
private static readonly ThreadLocal<int> localLine = new ThreadLocal<int>();
20-
21-
/// <summary>
22-
/// Callback function invoked by OpenCV when exception occurs
23-
/// Stores the information locally for every thread
24-
/// </summary>
25-
public static readonly CvErrorCallback ErrorHandlerCallback =
26-
delegate (ErrorCode status, string funcName, string errMsg, string fileName, int line, IntPtr userData)
27-
{
28-
try
29-
{
30-
return 0;
31-
}
32-
finally
33-
{
34-
exceptionHappened.Value = true;
35-
localStatus.Value = status;
36-
localErrMsg.Value = errMsg;
37-
localFileName.Value = fileName;
38-
localLine.Value = line;
39-
localFuncName.Value = funcName;
40-
}
41-
};
42-
43-
/// <summary>
44-
/// Registers the callback function to OpenCV, so exception caught before the p/invoke boundary
45-
/// </summary>
46-
public static void RegisterExceptionCallback()
23+
public static readonly CvErrorCallback ErrorHandlerCallback =
24+
delegate (ErrorCode status, string funcName, string errMsg, string fileName, int line, IntPtr userData)
4725
{
48-
IntPtr zero = IntPtr.Zero;
49-
IntPtr ret = NativeMethods.redirectError(ErrorHandlerCallback, zero, ref zero);
50-
}
51-
52-
/// <summary>
53-
/// Throws appropriate exception if one happened
54-
/// </summary>
55-
public static void ThrowPossibleException()
56-
{
57-
if (CheckForException())
26+
try
5827
{
59-
throw new OpenCVException(
60-
localStatus.Value,
61-
localFuncName.Value ?? "",
62-
localErrMsg.Value ?? "",
63-
localFileName.Value ?? "",
64-
localLine.Value);
28+
return 0;
6529
}
66-
}
30+
finally
31+
{
32+
exceptionHappened.Value = true;
33+
localStatus.Value = status;
34+
localErrMsg.Value = errMsg;
35+
localFileName.Value = fileName;
36+
localLine.Value = line;
37+
localFuncName.Value = funcName;
38+
}
39+
};
6740

68-
/// <summary>
69-
/// Returns a boolean which indicates if an exception occured for the current thread
70-
/// Reading this value changes its state, so an exception is handled only once
71-
/// </summary>
72-
private static bool CheckForException()
41+
/// <summary>
42+
/// Registers the callback function to OpenCV, so exception caught before the p/invoke boundary
43+
/// </summary>
44+
public static void RegisterExceptionCallback()
45+
{
46+
IntPtr zero = IntPtr.Zero;
47+
IntPtr ret = NativeMethods.redirectError(ErrorHandlerCallback, zero, ref zero);
48+
}
49+
50+
/// <summary>
51+
/// Throws appropriate exception if one happened
52+
/// </summary>
53+
public static void ThrowPossibleException()
54+
{
55+
if (CheckForException())
7356
{
74-
var value = exceptionHappened.Value;
75-
// reset exception value
76-
exceptionHappened.Value = false;
77-
return value;
57+
throw new OpenCVException(
58+
localStatus.Value,
59+
localFuncName.Value ?? "",
60+
localErrMsg.Value ?? "",
61+
localFileName.Value ?? "",
62+
localLine.Value);
7863
}
7964
}
65+
66+
/// <summary>
67+
/// Returns a boolean which indicates if an exception occured for the current thread
68+
/// Reading this value changes its state, so an exception is handled only once
69+
/// </summary>
70+
private static bool CheckForException()
71+
{
72+
var value = exceptionHappened.Value;
73+
// reset exception value
74+
exceptionHappened.Value = false;
75+
return value;
76+
}
8077
}
81-
#endif
78+
79+
#endif

src/OpenCvSharp/Internal/PInvoke/NativeMethods/NativeMethods.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public static void LoadLibraries(IEnumerable<string>? additionalPaths = null)
9393
return;
9494
}
9595

96-
var ap = (additionalPaths is null) ? Array.Empty<string>() : additionalPaths.ToArray();
96+
var ap = (additionalPaths is null) ? [] : additionalPaths.ToArray();
9797

9898
/*
9999
if (Environment.Is64BitProcess)

src/OpenCvSharp/Internal/PInvoke/NativeMethods/core/NativeMethods_core.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static partial class NativeMethods
3434
public static extern ExceptionStatus core_getBuildInformation(IntPtr buf);
3535

3636
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true)]
37-
public static unsafe extern ExceptionStatus core_getVersionString(byte* buf, int maxLength);
37+
public static extern unsafe ExceptionStatus core_getVersionString(byte* buf, int maxLength);
3838
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
3939
public static extern ExceptionStatus core_getVersionMajor(out int returnValue);
4040
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]

src/OpenCvSharp/Internal/PInvoke/NativeMethods/imgproc/NativeMethods_imgproc_LineSegmentDetector.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ namespace OpenCvSharp.Internal;
1010

1111
static partial class NativeMethods
1212
{
13-
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
13+
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
1414
public static extern void imgproc_LineSegmentDetector_detect_OutputArray(IntPtr obj, IntPtr image, IntPtr lines,
1515
IntPtr width, IntPtr prec, IntPtr nfa);
1616

17-
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
17+
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
1818
public static extern void imgproc_LineSegmentDetector_detect_vector(IntPtr obj, IntPtr image, IntPtr lines,
1919
IntPtr width, IntPtr prec, IntPtr nfa);
2020

21-
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
21+
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
2222
public static extern void imgproc_LineSegmentDetector_drawSegments(IntPtr obj, IntPtr image, IntPtr lines);
2323

2424
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
@@ -30,7 +30,7 @@ public static extern IntPtr imgproc_createLineSegmentDetector(
3030
int refine, double scale, double sigma_scale, double quant, double ang_th,
3131
double log_eps, double density_th, int n_bins);
3232

33-
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
33+
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
3434
public static extern void imgproc_Ptr_LineSegmentDetector_delete(IntPtr obj);
3535

3636
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]

src/OpenCvSharp/Internal/PInvoke/WindowsLibraryLoader.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public sealed class WindowsLibraryLoader
2323
private const string DllFileExtension = ".dll";
2424
private const string DllDirectory = "dll";
2525

26-
private readonly List<string> loadedAssemblies = new();
26+
private readonly List<string> loadedAssemblies = [];
2727

2828
/// <summary>
2929
/// Map processor
@@ -63,7 +63,7 @@ public sealed class WindowsLibraryLoader
6363
/// </summary>
6464
private WindowsLibraryLoader()
6565
{
66-
AdditionalPaths = new List<string>();
66+
AdditionalPaths = [];
6767
}
6868

6969
/// <summary>
@@ -114,7 +114,7 @@ public void LoadLibrary(string dllName, IEnumerable<string>? additionalPaths = n
114114
if (!IsCurrentPlatformSupported())
115115
return;
116116

117-
var additionalPathsArray = additionalPaths?.ToArray() ?? Array.Empty<string>();
117+
var additionalPathsArray = additionalPaths?.ToArray() ?? [];
118118

119119
// In .NET Core, process only when additional paths are specified.
120120
if (IsDotNetCore() && additionalPathsArray.Length == 0)
@@ -187,7 +187,7 @@ public void LoadLibrary(string dllName, IEnumerable<string>? additionalPaths = n
187187
if (processArch.HasWarnings)
188188
{
189189
// include process detection warnings
190-
errorMessage.AppendLine().Append($"Warnings: ").AppendLine().Append("{processArch.WarningText()}");
190+
errorMessage.AppendLine().Append("Warnings: ").AppendLine().Append("{processArch.WarningText()}");
191191
}
192192

193193
throw new OpenCvSharpException(errorMessage.ToString());
@@ -202,7 +202,7 @@ public void LoadLibrary(string dllName, IEnumerable<string>? additionalPaths = n
202202
}
203203

204204
/// <summary>
205-
/// Get's the current process architecture while keeping track of any assumptions or possible errors.
205+
/// Gets the current process architecture while keeping track of any assumptions or possible errors.
206206
/// </summary>
207207
/// <returns></returns>
208208
private ProcessArchitectureInfo GetProcessArchitecture()
@@ -214,7 +214,7 @@ private ProcessArchitectureInfo GetProcessArchitecture()
214214
if (!string.IsNullOrEmpty(processArchitecture))
215215
{
216216
// Sanity check
217-
processInfo.Architecture = processArchitecture!;
217+
processInfo.Architecture = processArchitecture;
218218
}
219219
else
220220
{
@@ -360,7 +360,7 @@ private class ProcessArchitectureInfo
360360
public ProcessArchitectureInfo()
361361
{
362362
Architecture = "";
363-
Warnings = new List<string>();
363+
Warnings = [];
364364
}
365365

366366
public bool HasWarnings => Warnings.Count > 0;

src/OpenCvSharp/Internal/Vectors/VectorOfByte.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public byte[] ToArray()
8181
var size = Size;
8282
if (size == 0)
8383
{
84-
return Array.Empty<byte>();
84+
return [];
8585
}
8686
var dst = new byte[size];
8787
Marshal.Copy(ElemPtr, dst, 0, dst.Length);

src/OpenCvSharp/Internal/Vectors/VectorOfDMatch.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public DMatch[] ToArray()
8282
var size = Size;
8383
if (size == 0)
8484
{
85-
return Array.Empty<DMatch>();
85+
return [];
8686
}
8787
var dst = new DMatch[size];
8888
using (var dstPtr = new ArrayAddress1<DMatch>(dst))

src/OpenCvSharp/Internal/Vectors/VectorOfDTreesNode.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public DTrees.Node[] ToArray()
6060
var size = Size;
6161
if (size == 0)
6262
{
63-
return Array.Empty<DTrees.Node>();
63+
return [];
6464
}
6565
var dst = new DTrees.Node[size];
6666
using (var dstPtr = new ArrayAddress1<DTrees.Node>(dst))

src/OpenCvSharp/Internal/Vectors/VectorOfDTreesSplit.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public DTrees.Split[] ToArray()
6060
var size = Size;
6161
if (size == 0)
6262
{
63-
return Array.Empty<DTrees.Split>();
63+
return [];
6464
}
6565
var dst = new DTrees.Split[size];
6666
using (var dstPtr = new ArrayAddress1<DTrees.Split>(dst))

src/OpenCvSharp/Internal/Vectors/VectorOfDouble.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public double[] ToArray()
8181
var size = Size;
8282
if (size == 0)
8383
{
84-
return Array.Empty<double>();
84+
return [];
8585
}
8686
var dst = new double[size];
8787
Marshal.Copy(ElemPtr, dst, 0, dst.Length);

src/OpenCvSharp/Internal/Vectors/VectorOfFloat.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public float[] ToArray()
8181
var size = Size;
8282
if (size == 0)
8383
{
84-
return Array.Empty<float>();
84+
return [];
8585
}
8686
var dst = new float[size];
8787
Marshal.Copy(ElemPtr, dst, 0, dst.Length);

src/OpenCvSharp/Internal/Vectors/VectorOfImageFeatures.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public ImageFeatures[] ToArray()
4444
{
4545
var size = Size;
4646
if (size == 0)
47-
return Array.Empty<ImageFeatures>();
47+
return [];
4848

4949
VectorOfKeyPoint[]? keypointsVecs = null;
5050
Mat[]? descriptors = null;
@@ -53,7 +53,7 @@ public ImageFeatures[] ToArray()
5353
var nativeResult = new WImageFeatures[size];
5454
keypointsVecs = new VectorOfKeyPoint[size];
5555
descriptors = new Mat[size];
56-
for (int i = 0; i < size; i++)
56+
for (var i = 0; i < size; i++)
5757
{
5858
keypointsVecs[i] = new VectorOfKeyPoint();
5959
descriptors[i] = new Mat();

0 commit comments

Comments
 (0)