IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
TransformationModule.cs
Go to the documentation of this file.
1using System.Runtime.InteropServices;
7
9
18{
19 internal const bool DefaultMirrorLeftRight = false;
20 internal const bool DefaultMirrorUpDown = false;
21 internal const Rotation DefaultRotationAngle = Rotation.None;
22 internal const bool DefaultIsMirrorEnabled = true;
23 internal const bool DefaultIsRotationEnabled = true;
24
25 private const string _leftRightKey = "LeftRight";
26 private const string _upDownKey = "UpDown";
27 private const string _mirrorKey = "Mirror";
28 private const string _angleKey = "Angle";
29 private const string _rotationKey = "Rotation";
30
32 public override string TypeId => "TransformationModule";
33
35 public override bool IsEnabled
36 {
37 get => IsMirrorEnabled || IsRotationEnabled;
38 set
39 {
40 IsMirrorEnabled = value;
41 IsRotationEnabled = value;
42 }
43 }
44
48 public bool MirrorLeftRight { get; set; } = DefaultMirrorLeftRight;
49
53 public bool MirrorUpDown { get; set; } = DefaultMirrorUpDown;
54
58 public Rotation RotationAngle { get; set; } = DefaultRotationAngle;
59
60 internal bool IsMirrorEnabled { get; set; } = DefaultIsMirrorEnabled;
61 internal bool IsRotationEnabled { get; set; } = DefaultIsRotationEnabled;
62
93 public override Image Process(Image inputImage)
94 {
95 var mirrorNeedsProcessing = IsMirrorEnabled && (MirrorLeftRight || MirrorUpDown);
96 var rotationNeedsProcessing = IsRotationEnabled && RotationAngle != Rotation.None;
97
98 if (!mirrorNeedsProcessing && !rotationNeedsProcessing)
99 {
100 return inputImage;
101 }
102
103 var parameters = new Backend.Types.peak_icv_preprocessing_transformation_parameters(
104 IsMirrorEnabled && MirrorLeftRight,
105 IsMirrorEnabled && MirrorUpDown,
106 CInterface.RotationAngleToCType(IsRotationEnabled
108 : Rotation.None));
109
110 var outputFormat = new PixelFormat();
111 DllHelper.ExecuteAndMapReturnCodes(() =>
112 CInterface.peak_icv_Preprocessing_Transformation_GetOutputPixelFormat(inputImage.PixelFormat,
113 parameters, out outputFormat));
114
115 var transpose = IsRotationEnabled && (RotationAngle == Rotation.Degree90Clockwise ||
116 RotationAngle == Rotation.Degree90Counterclockwise);
117 Size outputSize = transpose ? inputImage.Size.Transposed() : inputImage.Size;
118
119 var outputImage = new Image(outputFormat, outputSize);
120 DllHelper.ExecuteAndMapReturnCodes(() =>
121 CInterface.peak_icv_Preprocessing_Transformation_Process(inputImage._handle, parameters,
122 outputImage._handle));
123
124 return outputImage;
125 }
126
157 public override object Process(object input)
158 {
159 return base.Process(input);
160 }
161
173 public override void Serialize(IArchive archive)
174 {
175 archive.SetInt64(VersionKey, Version);
176
177 IArchive mirrorArchive = archive.CreateArchive();
178 mirrorArchive.SetBool(EnabledKey, IsMirrorEnabled);
179 mirrorArchive.SetBool(_leftRightKey, MirrorLeftRight);
180 mirrorArchive.SetBool(_upDownKey, MirrorUpDown);
181 archive.SetArchive(_mirrorKey, mirrorArchive);
182
183 IArchive rotationArchive = archive.CreateArchive();
184 rotationArchive.SetBool(EnabledKey, IsRotationEnabled);
185 rotationArchive.SetInt64(_angleKey, (int) RotationAngle);
186 archive.SetArchive(_rotationKey, rotationArchive);
187 }
188
213 public override void Deserialize(IArchive archive)
214 {
215 var version = archive.GetInt64(VersionKey);
216 ValidateVersion(version, TypeId);
217
218 IArchive rotationArchive = archive.GetArchive(_rotationKey);
219 IsRotationEnabled = rotationArchive.GetBool(EnabledKey);
220
221 var angle = (int) rotationArchive.GetInt64(_angleKey);
222 if (!Enum.IsDefined(typeof(Rotation), angle))
223 {
224 throw new IcvNotSupportedException($"The rotation angle {angle} is invalid!");
225 }
226
227 RotationAngle = (Rotation) angle;
228
229 IArchive mirrorArchive = archive.GetArchive(_mirrorKey);
230 IsMirrorEnabled = mirrorArchive.GetBool(EnabledKey);
231 MirrorLeftRight = mirrorArchive.GetBool(_leftRightKey);
232 MirrorUpDown = mirrorArchive.GetBool(_upDownKey);
233 }
234
258 public override void ResetToDefault()
259 {
260 MirrorLeftRight = DefaultMirrorLeftRight;
261 MirrorUpDown = DefaultMirrorUpDown;
262 RotationAngle = DefaultRotationAngle;
263 }
264
265 private static class CInterface
266 {
267 public static Backend.Types.peak_icv_preprocessing_transformation_rotation_angle RotationAngleToCType(
268 Rotation rotation)
269 {
270 return rotation switch
271 {
272 Rotation.None => Backend.Types.peak_icv_preprocessing_transformation_rotation_angle
273 .PEAK_ICV_PREPROCESSING_TRANSFORMATION_ROTATION_NONE,
274 Rotation.Degree90Counterclockwise => Backend.Types
275 .peak_icv_preprocessing_transformation_rotation_angle
276 .PEAK_ICV_PREPROCESSING_TRANSFORMATION_ROTATION_90_DEGREE_COUNTERCLOCKWISE,
277 Rotation.Degree180 => Backend.Types.peak_icv_preprocessing_transformation_rotation_angle
278 .PEAK_ICV_PREPROCESSING_TRANSFORMATION_ROTATION_180_DEGREE,
279 Rotation.Degree90Clockwise => Backend.Types.peak_icv_preprocessing_transformation_rotation_angle
280 .PEAK_ICV_PREPROCESSING_TRANSFORMATION_ROTATION_90_DEGREE_CLOCKWISE,
281 _ => throw new IcvNotSupportedException("Unsupported rotation angle.")
282 };
283 }
284
285 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
286 public static extern Backend.Types.peak_icv_status
287 peak_icv_Preprocessing_Transformation_GetOutputPixelFormat(
288 PixelFormat inputPixelFormat,
289 Backend.Types.peak_icv_preprocessing_transformation_parameters parameters,
290 out PixelFormat outputPixelFormat);
291
292 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
293 public static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_Transformation_Process(
294 Image.Handle inputImageHandle, Backend.Types.peak_icv_preprocessing_transformation_parameters parameters,
295 Image.Handle outputImageHandle);
296 }
297}
Represents a size with integer width and height values.
Definition Size.cs:107
Occurs when a requested operation is not implemented or supported (e.g., exporting to an unsupported ...
Definition Exception.cs:62
Base class for all pipeline modules.
Definition ModuleBase.cs:13
Transformation is an image pipeline module that provides mirror and rotation functionality.
override void Deserialize(IArchive archive)
Restores the object's state from the provided archive.
bool MirrorUpDown
Sets whether the image should be mirrored up-down (along the horizontal axis).
bool MirrorLeftRight
Sets whether the image should be mirrored left-right (along the vertical axis).
override Image Process(Image inputImage)
Processes the input image and returns a mirrored and/or rotated copy based on current settings.
override void ResetToDefault()
Resets all parameters to their default values.
override object Process(object input)
Processes the input image and returns a mirrored and/or rotated copy based on current settings.
override string TypeId
The unique type identifier for this module implementation.This should be a unique string identifying ...
override bool IsEnabled
Gets or sets whether the module is enabled.
override void Serialize(IArchive archive)
Serializes the object's internal state into the provided archive.
Constructs an Image from an IImageView Instance.
Definition Image.cs:18
PixelFormat PixelFormat
Gets the pixel format from the image.
Definition Image.cs:217
Size Size
Gets the image size in pixels from the image.
Definition Image.cs:223
PixelFormat
Each enumerator represents a specific pixel layout and bit-depth, used for interpreting raw image dat...
Rotation
Angle parameter for the rotation algorithm.
Definition Rotation.cs:13
Represents a hierarchical, strongly-typed key-value archive.
Definition IArchive.cs:79
IArchive GetArchive(string key)
Retrieves a nested archive by key.
IArchive CreateArchive()
Creates a new, empty archive instance of the same concrete type.
void SetBool(string key, bool value)
Stores a boolean value under the specified key.
long GetInt64(string key)
Retrieves a 64-bit integer value by key.
void SetArchive(string key, IArchive value)
Stores a nested archive under the specified key.
void SetInt64(string key, long value)
Stores a 64-bit integer value under the specified key.
bool GetBool(string key)
Retrieves a boolean value by key.