IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
GainModule.cs
Go to the documentation of this file.
1using System.Runtime.InteropServices;
7using Microsoft.Win32.SafeHandles;
8
10
20public class GainModule : ModuleBase, IGain, IDisposable, IHasSafeHandle
21{
22 private const string _masterGainKey = "MasterGain";
23 private const string _redGainKey = "RedGain";
24 private const string _greenGainKey = "GreenGain";
25 private const string _blueGainKey = "BlueGain";
26 internal const float DefaultGain = 1.0f;
27
28 private readonly Handle _handle;
29 private float _blueGain = DefaultGain;
30 private float _greenGain = DefaultGain;
31 private bool _isEnabled = true;
32 private float _masterGain = DefaultGain;
33 private float _redGain = DefaultGain;
34
38 public GainModule()
39 {
40 var handle = new Handle();
41 DllHelper.ExecuteAndMapReturnCodes(() =>
42 CInterface.peak_icv_Preprocessing_Gain_Create(out handle)
43 );
44 _handle = handle;
45 }
46
55 {
56 get
57 {
58 var range = new Backend.Types.peak_icv_interval_f();
59 DllHelper.ExecuteAndMapReturnCodes(() =>
60 CInterface.peak_icv_Preprocessing_Gain_GetRange(
61 _handle, out range)
62 );
63
64 return new IntervalF(range.minimum, range.maximum);
65 }
66 }
67
71 public void Dispose()
72 {
73 _handle.Dispose();
74 GC.SuppressFinalize(this);
75 }
76
78 public override string TypeId => "Gain";
79
81 public override bool IsEnabled
82 {
83 get => _isEnabled;
84 set
85 {
86 _isEnabled = value;
87 ApplyGain();
88 }
89 }
90
102 public override void Serialize(IArchive archive)
103 {
104 base.Serialize(archive);
105
106 archive.SetDouble(_masterGainKey, Master);
107 archive.SetDouble(_redGainKey, Red);
108 archive.SetDouble(_greenGainKey, Green);
109 archive.SetDouble(_blueGainKey, Blue);
110 }
111
139 public override void Deserialize(IArchive archive)
140 {
141 base.Deserialize(archive);
142
143 Master = (float) archive.GetDouble(_masterGainKey);
144 Red = (float) archive.GetDouble(_redGainKey);
145 Green = (float) archive.GetDouble(_greenGainKey);
146 Blue = (float) archive.GetDouble(_blueGainKey);
147 }
148
159 public float Master
160 {
161 get => _masterGain;
162 set
163 {
164 _masterGain = value;
165
166 ApplyGain();
167 }
168 }
169
180 public float Red
181 {
182 get => _redGain;
183 set
184 {
185 _redGain = value;
186
187 ApplyGain();
188 }
189 }
190
201 public float Green
202 {
203 get => _greenGain;
204 set
205 {
206 _greenGain = value;
207
208 ApplyGain();
209 }
210 }
211
222 public float Blue
223 {
224 get => _blueGain;
225 set
226 {
227 _blueGain = value;
228
229 ApplyGain();
230 }
231 }
232
261 public override object Process(object input)
262 {
263 return base.Process(input);
264 }
265
292 public override void ResetToDefault()
293 {
294 Master = DefaultGain;
295 Red = DefaultGain;
296 Green = DefaultGain;
297 Blue = DefaultGain;
298 }
299
300 SafeHandle IHasSafeHandle.Handle => _handle;
301
329 public override Image Process(Image input)
330 {
331 if (!IsEnabled || !NeedsProcessing())
332 {
333 return input;
334 }
335
336 Image.Handle inputImageHandle = input._handle;
337 DllHelper.ExecuteAndMapReturnCodes(() =>
338 CInterface.peak_icv_Preprocessing_Gain_ProcessInplace(
339 _handle, inputImageHandle)
340 );
341
342 return input;
343 }
344
345 private void ApplyGain()
346 {
347 SetGain(Backend.Types.peak_icv_gain_type.PEAK_ICV_GAIN_TYPE_MASTER, _masterGain);
348 SetGain(Backend.Types.peak_icv_gain_type.PEAK_ICV_GAIN_TYPE_RED, _redGain);
349 SetGain(Backend.Types.peak_icv_gain_type.PEAK_ICV_GAIN_TYPE_GREEN, _greenGain);
350 SetGain(Backend.Types.peak_icv_gain_type.PEAK_ICV_GAIN_TYPE_BLUE, _blueGain);
351 }
352
353 private void SetGain(Backend.Types.peak_icv_gain_type type, float value)
354 {
355 DllHelper.ExecuteAndMapReturnCodes(() =>
356 CInterface.peak_icv_Preprocessing_Gain_SetValue(_handle, type, value)
357 );
358 }
359
360 private bool NeedsProcessing()
361 {
362 var needsProcessing = false;
363 DllHelper.ExecuteAndMapReturnCodes(() =>
364 CInterface.peak_icv_Preprocessing_Gain_NeedsProcessing(
365 _handle, out needsProcessing)
366 );
367
368 return needsProcessing;
369 }
370
371 internal sealed class Handle : SafeHandleZeroOrMinusOneIsInvalid
372 {
373 internal Handle() : base(true)
374 {
375 }
376
377 internal Handle(IntPtr handle) : base(true)
378 {
379 SetHandle(handle);
380 }
381
382 protected override bool ReleaseHandle()
383 {
384 return CInterface.peak_icv_Preprocessing_Gain_Destroy(handle) == 0;
385 }
386 }
387
388 private static class CInterface
389 {
390 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
391 internal static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_Gain_Create(out Handle handle);
392
393 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
394 internal static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_Gain_Destroy(IntPtr handle);
395
396 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
397 internal static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_Gain_NeedsProcessing(
398 Handle handle, [MarshalAs(UnmanagedType.I1)] out bool needsProcessing);
399
400 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
401 internal static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_Gain_ProcessInplace(
402 Handle handle, Image.Handle imageHandle);
403
404 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
405 internal static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_Gain_GetValue(
406 Handle handle, Backend.Types.peak_icv_gain_type type, out float value);
407
408 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
409 internal static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_Gain_SetValue(
410 Handle handle, Backend.Types.peak_icv_gain_type type, float value);
411
412 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
413 internal static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_Gain_GetRange(
414 Handle handle, out Backend.Types.peak_icv_interval_f range);
415 }
416}
Represents an interval with floating-point boundaries.
Definition Interval.cs:157
Represents a range with signed integer boundaries.
Definition Range.cs:139
Image pipeline module for applying gain to the image.
Definition GainModule.cs:21
float Blue
The current blue gain value.
override void Deserialize(IArchive archive)
Restores the object's state from the provided archive.
override Image Process(Image input)
Applies the gains to the input image in place and returns a reference to the modified image.
GainModule()
Creates an instance of class GainModule.
Definition GainModule.cs:38
override void ResetToDefault()
Resets all parameters to their default values.
override object Process(object input)
Applies the gains to the input image in place and returns a reference to the modified image.
void Dispose()
Releases all resources used by this class.
Definition GainModule.cs:71
override string TypeId
The unique type identifier for this module implementation.This should be a unique string identifying ...
Definition GainModule.cs:78
float Green
The current green gain value.
float Red
The current red gain value.
override bool IsEnabled
Gets or sets whether the module is enabled.
Definition GainModule.cs:82
override void Serialize(IArchive archive)
Serializes the object's internal state into the provided archive.
float Master
The current master gain value.
Base class for all pipeline modules.
Definition ModuleBase.cs:13
Constructs an Image from an IImageView Instance.
Definition Image.cs:18
Interface for modules that provide gain control functionality.
Definition IGain.cs:17
Represents a hierarchical, strongly-typed key-value archive.
Definition IArchive.cs:79
double GetDouble(string key)
Retrieves a double-precision floating-point value by key.
void SetDouble(string key, double value)
Stores a double-precision floating-point value under the specified key.