IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
DownsamplingModule.cs
Go to the documentation of this file.
1using System.Runtime.InteropServices;
7using Microsoft.Win32.SafeHandles;
8
10
17{
21 BinningAverage = Backend.Types.peak_icv_downsampling_mode.PEAK_ICV_BINNING_AVERAGE,
22
26 BinningSum = Backend.Types.peak_icv_downsampling_mode.PEAK_ICV_BINNING_SUM,
27
31 Decimation = Backend.Types.peak_icv_downsampling_mode.PEAK_ICV_DECIMATION
32}
33
66public class DownsamplingModule : ModuleBase, IDisposable, IHasSafeHandle
67{
68 private const string _modeKey = "Mode";
69 private const string _xKey = "X";
70 private const string _yKey = "Y";
71 private const string _defaultModeKey = "DefaultMode";
72
73 internal const uint DefaultX = 1;
74 internal const uint DefaultY = 1;
75
76 private readonly Handle _handle;
77
78
79 private DownsamplingMode _defaultMode = DownsamplingMode.BinningAverage;
80
86 {
87 var handle = new Handle();
88 DllHelper.ExecuteAndMapReturnCodes(() =>
89 CInterface.peak_icv_Preprocessing_Downsampling_Create(out handle,
90 new Backend.Types.peak_common_point_u(1, 1),
91 (Backend.Types.peak_icv_downsampling_mode) _defaultMode));
92 _handle = handle;
93 }
94
99 public DownsamplingModule(DownsamplingMode defaultMode, string customName)
100 {
101 _defaultMode = defaultMode;
102 CustomId = customName;
103
104 var handle = new Handle();
105 DllHelper.ExecuteAndMapReturnCodes(() =>
106 CInterface.peak_icv_Preprocessing_Downsampling_Create(out handle,
107 new Backend.Types.peak_common_point_u(1, 1),
108 (Backend.Types.peak_icv_downsampling_mode) _defaultMode));
109 _handle = handle;
110 }
111
115 public string? CustomId { get; set; }
116
118 public override string TypeId => CustomId ?? "Downsampling";
119
123 public uint X
124 {
125 get => Factor.X;
126
127 set
128 {
129 DllHelper.ExecuteAndMapReturnCodes(() =>
130 CInterface.peak_icv_Preprocessing_Downsampling_SetFactor(_handle,
131 new Backend.Types.peak_common_point_u(value, Y)));
132 }
133 }
134
138 public uint Y
139 {
140 get => Factor.Y;
141
142 set
143 {
144 DllHelper.ExecuteAndMapReturnCodes(() =>
145 CInterface.peak_icv_Preprocessing_Downsampling_SetFactor(_handle,
146 new Backend.Types.peak_common_point_u(X, value)));
147 }
148 }
149
154 {
155 get
156 {
157 var range = new Backend.Types.peak_icv_interval_u();
158 DllHelper.ExecuteAndMapReturnCodes(() =>
159 CInterface.peak_icv_Preprocessing_Downsampling_GetRange(_handle,
160 out range));
161 return new IntervalU(range.minimum, range.maximum);
162 }
163 }
164
169 {
170 get
171 {
172 Backend.Types.peak_icv_downsampling_mode mode = Backend.Types.peak_icv_downsampling_mode
173 .PEAK_ICV_BINNING_AVERAGE;
174 DllHelper.ExecuteAndMapReturnCodes(() =>
175 CInterface.peak_icv_Preprocessing_Downsampling_GetMode(_handle,
176 out mode));
177 return (DownsamplingMode) mode;
178 }
179 set
180 {
181 // Note: No checks done in C!
182 if (!Enum.IsDefined(typeof(DownsamplingMode), value))
183 {
184 throw new IcvNotSupportedException("Given DownsamplingMode mode is not supported.");
185 }
186
187 DllHelper.ExecuteAndMapReturnCodes(() =>
188 CInterface.peak_icv_Preprocessing_Downsampling_SetMode(_handle,
189 (Backend.Types.peak_icv_downsampling_mode) value));
190 }
191 }
192
193 internal PointU Factor
194 {
195 get
196 {
197 var factor = new Backend.Types.peak_common_point_u();
198 DllHelper.ExecuteAndMapReturnCodes(() =>
199 CInterface.peak_icv_Preprocessing_Downsampling_GetFactor(_handle,
200 out factor));
201 return new PointU(factor.x, factor.y);
202 }
203 set
204 {
205 DllHelper.ExecuteAndMapReturnCodes(() =>
206 CInterface.peak_icv_Preprocessing_Downsampling_SetFactor(_handle,
207 new Backend.Types.peak_common_point_u(value.X, value.Y)));
208 }
209 }
210
214 public void Dispose()
215 {
216 _handle.Dispose();
217 GC.SuppressFinalize(this);
218 }
219
220 SafeHandle IHasSafeHandle.Handle => _handle;
221
252 public override Image Process(Image inputImage)
253 {
254 if (!IsEnabled || !NeedsProcessing())
255 {
256 return inputImage;
257 }
258
259 var outputImageSize = new Backend.Types.peak_common_size();
260 DllHelper.ExecuteAndMapReturnCodes(() =>
261 CInterface.peak_icv_Preprocessing_Downsampling_GetOutputImageSize(_handle,
262 inputImage._handle, out outputImageSize));
263
264 var outputImage = new Image(inputImage.PixelFormat, new Size(outputImageSize.width, outputImageSize.height));
265
266 DllHelper.ExecuteAndMapReturnCodes(() =>
267 CInterface.peak_icv_Preprocessing_Downsampling_Process(_handle,
268 inputImage._handle, outputImage._handle));
269
270 return outputImage;
271
272 bool NeedsProcessing()
273 {
274 var shouldProcess = false;
275 DllHelper.ExecuteAndMapReturnCodes(() =>
276 CInterface.peak_icv_Preprocessing_Downsampling_NeedsProcessing(_handle, out shouldProcess));
277 return shouldProcess;
278 }
279 }
280
311 public override object Process(object input)
312 {
313 return base.Process(input);
314 }
315
327 public override void Serialize(IArchive archive)
328 {
329 base.Serialize(archive);
330
331 PointU factor = Factor;
332 archive.SetInt64(_xKey, factor.X);
333 archive.SetInt64(_yKey, factor.Y);
334
335 archive.SetString(_modeKey, Mode.ToString());
336 archive.SetString(_defaultModeKey, _defaultMode.ToString());
337 }
338
366 public override void Deserialize(IArchive archive)
367 {
368 base.Deserialize(archive);
369
370 var x = (uint) archive.GetInt64(_xKey);
371 var y = (uint) archive.GetInt64(_yKey);
372 Factor = new PointU(x, y);
373
374 var modeStr = archive.GetString(_modeKey);
375 var defaultModeStr = archive.GetString(_defaultModeKey);
376
377 if (!Enum.TryParse(modeStr, out DownsamplingMode mode))
378 {
379 throw new IcvCorruptedException($"The given downsampling mode \"{modeStr}\" is unknown!");
380 }
381
382 if (!Enum.TryParse(defaultModeStr, out DownsamplingMode defaultMode))
383 {
384 throw new IcvCorruptedException(
385 $"The given default downsampling mode \"{defaultModeStr}\" is unknown!");
386 }
387
388 Mode = mode;
389 _defaultMode = defaultMode;
390 }
391
415 public override void ResetToDefault()
416 {
417 base.ResetToDefault();
418
419 X = 1;
420 Y = 1;
421 Mode = _defaultMode;
422 }
423
424 internal sealed class Handle : SafeHandleZeroOrMinusOneIsInvalid
425 {
426 public Handle() : base(true)
427 {
428 }
429
430 protected override bool ReleaseHandle()
431 {
432 return CInterface.peak_icv_Preprocessing_Downsampling_Destroy(handle) ==
433 Backend.Types.peak_icv_status.PEAK_ICV_STATUS_SUCCESS;
434 }
435 }
436
437 private static class CInterface
438 {
439 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
440 public static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_Downsampling_Create(
441 out Handle handle, Backend.Types.peak_common_point_u factor,
442 Backend.Types.peak_icv_downsampling_mode mode);
443
444 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
445 public static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_Downsampling_Destroy(
446 IntPtr handle);
447
448 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
449 public static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_Downsampling_SetFactor(
450 Handle handle, Backend.Types.peak_common_point_u factor);
451
452 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
453 public static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_Downsampling_GetFactor(
454 Handle handle, out Backend.Types.peak_common_point_u factor);
455
456 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
457 public static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_Downsampling_GetRange(
458 Handle handle, out Backend.Types.peak_icv_interval_u range);
459
460 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
461 public static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_Downsampling_SetMode(
462 Handle handle, Backend.Types.peak_icv_downsampling_mode mode);
463
464 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
465 public static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_Downsampling_GetMode(
466 Handle handle, out Backend.Types.peak_icv_downsampling_mode mode);
467
468 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
469 public static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_Downsampling_GetOutputImageSize(
470 Handle handle, Image.Handle imageHandle, out Backend.Types.peak_common_size outputImageSize);
471
472 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
473 public static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_Downsampling_NeedsProcessing(
474 Handle handle, [MarshalAs(UnmanagedType.I1)] out bool needsProcessing);
475
476 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
477 public static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_Downsampling_Process(
478 Handle handle, Image.Handle inputImageHandle, Image.Handle outputImageHandle);
479 }
480}
T Y
Gets the Y coordinate of the point.
Definition Point.cs:34
T X
Gets the X coordinate of the point.
Definition Point.cs:29
Represents a 2D point with unsigned integer coordinates.
Definition Point.cs:154
Represents a size with integer width and height values.
Definition Size.cs:107
Represents an interval with unsigned integer boundaries.
Definition Interval.cs:137
Represents a range with signed integer boundaries.
Definition Range.cs:139
Occurs when reading from a file or stream that is incomplete, damaged, or in an unexpected format.
Definition Exception.cs:75
Occurs when a requested operation is not implemented or supported (e.g., exporting to an unsupported ...
Definition Exception.cs:62
Downsampling is used to decrease the image size.
override void Deserialize(IArchive archive)
Restores the object's state from the provided archive.
DownsamplingModule()
Creates an instance of class DownsamplingModule with factors X = 1, Y = 1 and DownsamplingMode = Down...
override Image Process(Image inputImage)
Applies the downsampling factors and mode to the input image and returns a downsampled copy based on ...
uint Y
The current number of rows used for downsampling.
DownsamplingMode Mode
The current T:DownsamplingMode.
override void ResetToDefault()
Resets all parameters to their default values.
override object Process(object input)
Applies the downsampling factors and mode to the input image and returns a downsampled copy based on ...
void Dispose()
Releases all resources used by this class.
string? CustomId
The custom TypeId of the module for serialization purposes.
override string TypeId
The unique type identifier for this module implementation.This should be a unique string identifying ...
DownsamplingModule(DownsamplingMode defaultMode, string customName)
Creates an instance of class DownsamplingModule with factors X = 1, Y = 1 and the given T:Downsamplin...
override void Serialize(IArchive archive)
Serializes the object's internal state into the provided archive.
uint X
The current number of columns used for downsampling.
Base class for all pipeline modules.
Definition ModuleBase.cs:13
virtual bool IsEnabled
Get whether this module is enabled.
Definition ModuleBase.cs:28
Constructs an Image from an IImageView Instance.
Definition Image.cs:18
PixelFormat PixelFormat
Gets the pixel format from the image.
Definition Image.cs:217
DownsamplingMode
Mode parameter for the downsampling algorithm.
@ BinningAverage
The averaged pixel values of neighboring rows and/or columns are computed during binning.
@ BinningSum
The pixel values of neighboring rows and/or columns are summed during binning.
@ Decimation
The additional pixel values of neighboring rows and/or columns are skipped during decimation.
Represents a hierarchical, strongly-typed key-value archive.
Definition IArchive.cs:79
long GetInt64(string key)
Retrieves a 64-bit integer value by key.
void SetInt64(string key, long value)
Stores a 64-bit integer value under the specified key.
string GetString(string key)
Retrieves a string value by key.
void SetString(string key, string value)
Stores a string value under the specified key.