IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
ToneCurveCorrectionModule.cs
Go to the documentation of this file.
1using System.Runtime.InteropServices;
7using Microsoft.Win32.SafeHandles;
8
10
19public class ToneCurveCorrectionModule : ModuleBase, IDisposable, IHasSafeHandle
20{
21 private const string _valueKey = "Value";
22 private const string _gammaKey = "Gamma";
23 private const string _digitalBlackKey = "DigitalBlack";
24
25 internal const float DefaultGamma = 1.0f;
26 internal const float DefaultDigitalBlack = 0.0f;
27
28 private readonly Handle _handle;
29 private float _digitalBlack = DefaultDigitalBlack;
30 private float _gamma = DefaultGamma;
31 private bool _isDigitalBlackEnabled = true;
32 private bool _isGammaEnabled = true;
33
38 {
39 var handle = new Handle();
40 DllHelper.ExecuteAndMapReturnCodes(() =>
41 CInterface.peak_icv_Preprocessing_ToneCurveCorrection_Create(
42 out handle
43 )
44 );
45 _handle = handle;
46 }
47
49 public override string TypeId => "ToneCurveCorrection";
50
52 public override bool IsEnabled
53 {
54 get => IsGammaEnabled || IsDigitalBlackEnabled;
55 set
56 {
57 IsGammaEnabled = value;
58 IsDigitalBlackEnabled = value;
59 }
60 }
61
76 public float Gamma
77 {
78 get => _gamma;
79 set
80 {
81 _gamma = value;
82
83 if (IsGammaEnabled)
84 {
85 ApplyGamma();
86 }
87 }
88 }
89
97 {
98 get
99 {
100 var interval = new Backend.Types.peak_icv_interval_f();
101 DllHelper.ExecuteAndMapReturnCodes(() =>
102 CInterface.peak_icv_Preprocessing_ToneCurveCorrection_GetGammaRange(
103 _handle,
104 out interval
105 )
106 );
107
108 return new IntervalF(interval.minimum, interval.maximum);
109 }
110 }
111
125 public float DigitalBlack
126 {
127 get => _digitalBlack;
128 set
129 {
130 _digitalBlack = value;
131
132 if (IsDigitalBlackEnabled)
133 {
134 ApplyDigitalBlack();
135 }
136 }
137 }
138
146 {
147 get
148 {
149 var interval = new Backend.Types.peak_icv_interval_f();
150 DllHelper.ExecuteAndMapReturnCodes(() =>
151 CInterface.peak_icv_Preprocessing_ToneCurveCorrection_GetDigitalBlackRange(
152 _handle,
153 out interval
154 )
155 );
156
157 return new IntervalF(interval.minimum, interval.maximum);
158 }
159 }
160
161 internal bool IsGammaEnabled
162 {
163 get => _isGammaEnabled;
164 set
165 {
166 _isGammaEnabled = value;
167
168 ApplyGamma();
169 }
170 }
171
172 internal bool IsDigitalBlackEnabled
173 {
174 get => _isDigitalBlackEnabled;
175 set
176 {
177 _isDigitalBlackEnabled = value;
178
179 ApplyDigitalBlack();
180 }
181 }
182
186 public void Dispose()
187 {
188 _handle.Dispose();
189 GC.SuppressFinalize(this);
190 }
191
192 SafeHandle IHasSafeHandle.Handle => _handle;
193
205 public override void Serialize(IArchive archive)
206 {
207 archive.SetInt64(VersionKey, 1);
208
209 IArchive gammaArchive = archive.CreateArchive();
210 gammaArchive.SetBool(EnabledKey, IsGammaEnabled);
211 gammaArchive.SetDouble(_valueKey, Gamma);
212 archive.SetArchive(_gammaKey, gammaArchive);
213
214 IArchive digitalBlackArchive = archive.CreateArchive();
215 digitalBlackArchive.SetBool(EnabledKey, IsDigitalBlackEnabled);
216 digitalBlackArchive.SetDouble(_valueKey, DigitalBlack);
217 archive.SetArchive(_digitalBlackKey, digitalBlackArchive);
218 }
219
247 public override void Deserialize(IArchive archive)
248 {
249 var version = archive.GetInt64(VersionKey);
250 ValidateVersion(version, TypeId);
251
252 IArchive gammaArchive = archive.GetArchive(_gammaKey);
253 _gamma = (float) gammaArchive.GetDouble(_valueKey);
254 IsGammaEnabled = gammaArchive.GetBool(EnabledKey);
255
256 IArchive digitalBlackArchive = archive.GetArchive(_digitalBlackKey);
257 _digitalBlack = (float) digitalBlackArchive.GetDouble(_valueKey);
258 IsDigitalBlackEnabled = digitalBlackArchive.GetBool(EnabledKey);
259 }
260
281 public override void ResetToDefault()
282 {
283 Gamma = DefaultGamma;
284 DigitalBlack = DefaultDigitalBlack;
285 }
286
308 public override Image Process(Image input)
309 {
310 if (!IsEnabled || !NeedsProcessing())
311 {
312 return input;
313 }
314
315 if (!IsSupportedPixelFormat(input.PixelFormat))
316 {
317 return input;
318 }
319
320 Image.Handle inputImageHandle = input._handle;
321
322 DllHelper.ExecuteAndMapReturnCodes(() =>
323 CInterface.peak_icv_Preprocessing_ToneCurveCorrection_ProcessInplace(
324 _handle, inputImageHandle));
325
326 return input;
327 }
328
350 public override object Process(object input)
351 {
352 return base.Process(input);
353 }
354
355 private void ApplyGamma()
356 {
357 var newGamma = _isGammaEnabled ? _gamma : DefaultGamma;
358 DllHelper.ExecuteAndMapReturnCodes(() =>
359 CInterface.peak_icv_Preprocessing_ToneCurveCorrection_SetGamma(
360 _handle,
361 newGamma
362 )
363 );
364 }
365
366 private void ApplyDigitalBlack()
367 {
368 var newDigitalBlack = _isDigitalBlackEnabled ? _digitalBlack : DefaultDigitalBlack;
369 DllHelper.ExecuteAndMapReturnCodes(() =>
370 CInterface.peak_icv_Preprocessing_ToneCurveCorrection_SetDigitalBlack(
371 _handle,
372 newDigitalBlack
373 )
374 );
375 }
376
377 private bool NeedsProcessing()
378 {
379 var needsProcessing = false;
380 DllHelper.ExecuteAndMapReturnCodes(() =>
381 CInterface.peak_icv_Preprocessing_ToneCurveCorrection_NeedsProcessing(
382 _handle, out needsProcessing));
383
384 return needsProcessing;
385 }
386
387 private bool IsSupportedPixelFormat(PixelFormat format)
388 {
389 var info = new PixelFormatInfo(format);
390
391 var isSupportedMonoImage =
392 format == PixelFormat.Mono8
393 || format == PixelFormat.Mono10
394 || format == PixelFormat.Mono12
395 || format == PixelFormat.Mono16;
396
397 var isSupportedColorImage =
398 info.HasChannel(Channel.Red)
399 && info.HasChannel(Channel.Green)
400 && info.HasChannel(Channel.Blue)
401 && !info.IsPacked;
402
403 return isSupportedMonoImage || isSupportedColorImage;
404 }
405
406 internal sealed class Handle : SafeHandleZeroOrMinusOneIsInvalid
407 {
408 internal Handle() : base(true)
409 {
410 }
411
412 internal Handle(IntPtr handle) : base(true)
413 {
414 SetHandle(handle);
415 }
416
417 protected override bool ReleaseHandle()
418 {
419 return CInterface.peak_icv_Preprocessing_ToneCurveCorrection_Destroy(handle) == 0;
420 }
421 }
422
423 private static class CInterface
424 {
425 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
426 internal static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_ToneCurveCorrection_Create(
427 out Handle handle);
428
429 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
430 internal static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_ToneCurveCorrection_Destroy(
431 IntPtr handle);
432
433 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
434 internal static extern Backend.Types.peak_icv_status
435 peak_icv_Preprocessing_ToneCurveCorrection_NeedsProcessing(
436 Handle handle, [MarshalAs(UnmanagedType.I1)] out bool needsProcessing);
437
438 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
439 internal static extern Backend.Types.peak_icv_status
440 peak_icv_Preprocessing_ToneCurveCorrection_ProcessInplace(
441 Handle handle, Image.Handle imageHandle);
442
443 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
444 internal static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_ToneCurveCorrection_SetGamma(
445 Handle handle, float gamma);
446
447 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
448 internal static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_ToneCurveCorrection_GetGamma(
449 Handle handle, out float gamma);
450
451 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
452 internal static extern Backend.Types.peak_icv_status
453 peak_icv_Preprocessing_ToneCurveCorrection_GetGammaRange(
454 Handle handle, out Backend.Types.peak_icv_interval_f range);
455
456 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
457 internal static extern Backend.Types.peak_icv_status
458 peak_icv_Preprocessing_ToneCurveCorrection_SetDigitalBlack(
459 Handle handle, float digitalBlack);
460
461 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
462 internal static extern Backend.Types.peak_icv_status
463 peak_icv_Preprocessing_ToneCurveCorrection_GetDigitalBlack(
464 Handle handle, out float digitalBlack);
465
466 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
467 internal static extern Backend.Types.peak_icv_status
468 peak_icv_Preprocessing_ToneCurveCorrection_GetDigitalBlackRange(
469 Handle handle, out Backend.Types.peak_icv_interval_f range);
470 }
471}
Represents an interval with floating-point boundaries.
Definition Interval.cs:157
Provides information and utility functions for pixel formats. The PixelFormatInfo class offers method...
Base class for all pipeline modules.
Definition ModuleBase.cs:13
Tone curve correction is an image pipeline module that applies digital black correction and inverse g...
override void Deserialize(IArchive archive)
Restores the object's state from the provided archive.
override Image Process(Image input)
Processes the input image and returns a tone curve corrected output image.
IntervalF DigitalBlackRange
Gets the valid range for the digital black correction value.
override void ResetToDefault()
Resets all parameters to their default values.
override object Process(object input)
Processes the input image and returns a tone curve corrected output image.
IntervalF GammaRange
Gets the valid range for the gamma correction exponent.
override string TypeId
The unique type identifier for this module implementation.This should be a unique string identifying ...
float DigitalBlack
The current digital black used for digital black correction.
override bool IsEnabled
Gets or sets whether the module is enabled.
ToneCurveCorrectionModule()
Creates an instance of class ToneCurveCorrectionModule.
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
Channel
Represents the various data channels in image or spatial data formats. /// This enumeration defines s...
Definition Channel.cs:13
PixelFormat
Each enumerator represents a specific pixel layout and bit-depth, used for interpreting raw image dat...
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.
double GetDouble(string key)
Retrieves a double-precision floating-point value by key.
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.
void SetDouble(string key, double value)
Stores a double-precision floating-point value under the specified key.