21 private const string _valueKey =
"Value";
22 private const string _gammaKey =
"Gamma";
23 private const string _digitalBlackKey =
"DigitalBlack";
25 internal const float DefaultGamma = 1.0f;
26 internal const float DefaultDigitalBlack = 0.0f;
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;
39 var handle =
new Handle();
40 DllHelper.ExecuteAndMapReturnCodes(() =>
41 CInterface.peak_icv_Preprocessing_ToneCurveCorrection_Create(
49 public override string TypeId =>
"ToneCurveCorrection";
54 get => IsGammaEnabled || IsDigitalBlackEnabled;
57 IsGammaEnabled = value;
58 IsDigitalBlackEnabled = value;
100 var interval =
new Backend.Types.peak_icv_interval_f();
101 DllHelper.ExecuteAndMapReturnCodes(() =>
102 CInterface.peak_icv_Preprocessing_ToneCurveCorrection_GetGammaRange(
108 return new IntervalF(interval.minimum, interval.maximum);
127 get => _digitalBlack;
130 _digitalBlack = value;
132 if (IsDigitalBlackEnabled)
149 var interval =
new Backend.Types.peak_icv_interval_f();
150 DllHelper.ExecuteAndMapReturnCodes(() =>
151 CInterface.peak_icv_Preprocessing_ToneCurveCorrection_GetDigitalBlackRange(
157 return new IntervalF(interval.minimum, interval.maximum);
161 internal bool IsGammaEnabled
163 get => _isGammaEnabled;
166 _isGammaEnabled = value;
172 internal bool IsDigitalBlackEnabled
174 get => _isDigitalBlackEnabled;
177 _isDigitalBlackEnabled = value;
189 GC.SuppressFinalize(
this);
192 SafeHandle IHasSafeHandle.Handle => _handle;
210 gammaArchive.
SetBool(EnabledKey, IsGammaEnabled);
215 digitalBlackArchive.
SetBool(EnabledKey, IsDigitalBlackEnabled);
217 archive.
SetArchive(_digitalBlackKey, digitalBlackArchive);
249 var version = archive.
GetInt64(VersionKey);
250 ValidateVersion(version,
TypeId);
253 _gamma = (float) gammaArchive.
GetDouble(_valueKey);
254 IsGammaEnabled = gammaArchive.
GetBool(EnabledKey);
257 _digitalBlack = (float) digitalBlackArchive.
GetDouble(_valueKey);
258 IsDigitalBlackEnabled = digitalBlackArchive.
GetBool(EnabledKey);
283 Gamma = DefaultGamma;
320 Image.Handle inputImageHandle = input._handle;
322 DllHelper.ExecuteAndMapReturnCodes(() =>
323 CInterface.peak_icv_Preprocessing_ToneCurveCorrection_ProcessInplace(
324 _handle, inputImageHandle));
352 return base.Process(input);
355 private void ApplyGamma()
357 var newGamma = _isGammaEnabled ? _gamma : DefaultGamma;
358 DllHelper.ExecuteAndMapReturnCodes(() =>
359 CInterface.peak_icv_Preprocessing_ToneCurveCorrection_SetGamma(
366 private void ApplyDigitalBlack()
368 var newDigitalBlack = _isDigitalBlackEnabled ? _digitalBlack : DefaultDigitalBlack;
369 DllHelper.ExecuteAndMapReturnCodes(() =>
370 CInterface.peak_icv_Preprocessing_ToneCurveCorrection_SetDigitalBlack(
377 private bool NeedsProcessing()
379 var needsProcessing =
false;
380 DllHelper.ExecuteAndMapReturnCodes(() =>
381 CInterface.peak_icv_Preprocessing_ToneCurveCorrection_NeedsProcessing(
382 _handle, out needsProcessing));
384 return needsProcessing;
387 private bool IsSupportedPixelFormat(
PixelFormat format)
391 var isSupportedMonoImage =
392 format == PixelFormat.Mono8
393 || format == PixelFormat.Mono10
394 || format == PixelFormat.Mono12
397 var isSupportedColorImage =
399 && info.HasChannel(
Channel.Green)
400 && info.HasChannel(
Channel.Blue)
403 return isSupportedMonoImage || isSupportedColorImage;
406 internal sealed class Handle : SafeHandleZeroOrMinusOneIsInvalid
408 internal Handle() : base(true)
412 internal Handle(IntPtr handle) : base(true)
417 protected override bool ReleaseHandle()
419 return CInterface.peak_icv_Preprocessing_ToneCurveCorrection_Destroy(handle) == 0;
423 private static class CInterface
425 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
426 internal static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_ToneCurveCorrection_Create(
429 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
430 internal static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_ToneCurveCorrection_Destroy(
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);
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);
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);
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);
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);
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);
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);
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);