48 private const string _saturationKey =
"Saturation";
49 private const string _matrixKey =
"ColorCorrection";
50 private const string _chromaticAdaptionKey =
"ChromaticAdaption";
51 private const string _valueKey =
"Value";
52 private const string _algorithmKey =
"Algorithm";
53 private const string _targetColorSpaceKey =
"TargetColorSpace";
54 private const string _colorTemperatureKey =
"ColorTemperature";
59 internal const float DefaultSaturation = 1.0f;
71 private readonly Handle _handle;
80 var handle =
new Handle();
81 DllHelper.ExecuteAndMapReturnCodes(() =>
82 CInterface.peak_icv_Preprocessing_ColorMatrixTransformation_Create(out handle));
92 public override string TypeId =>
"ColorMatrixTransformation";
97 get => IsColorCorrectionEnabled || IsSaturationEnabled || IsChromaticAdaptionEnabled;
100 IsColorCorrectionEnabled = value;
101 IsSaturationEnabled = value;
102 IsChromaticAdaptionEnabled = value;
103 UpdateMatrixInAutoFeatureModule();
116 DllHelper.ExecuteAndMapReturnCodes(() =>
118 .peak_icv_Preprocessing_ColorMatrixTransformation_GetColorCorrectionMatrix(
119 _handle, out backendMatrix));
125 DllHelper.ExecuteAndMapReturnCodes(() =>
127 .peak_icv_Preprocessing_ColorMatrixTransformation_SetColorCorrectionMatrix(
128 _handle, backendMatrix));
129 UpdateMatrixInAutoFeatureModule();
143 var saturation = 1.0f;
144 DllHelper.ExecuteAndMapReturnCodes(() =>
145 CInterface.peak_icv_Preprocessing_ColorMatrixTransformation_GetSaturation(
146 _handle, out saturation));
151 DllHelper.ExecuteAndMapReturnCodes(() =>
152 CInterface.peak_icv_Preprocessing_ColorMatrixTransformation_SetSaturation(
154 UpdateMatrixInAutoFeatureModule();
165 var interval =
new Backend.Types.peak_icv_interval_f();
166 DllHelper.ExecuteAndMapReturnCodes(() =>
167 CInterface.peak_icv_Preprocessing_ColorMatrixTransformation_GetSaturationRange(
168 _handle, out interval));
169 return new IntervalF(interval.minimum, interval.maximum);
180 Backend.Types.peak_icv_chromatic_adaption_algorithm algorithm = Backend.Types
181 .peak_icv_chromatic_adaption_algorithm.PEAK_ICV_CHROMATIC_ADAPTION_ALGORITHM_BRADFORD;
182 DllHelper.ExecuteAndMapReturnCodes(() =>
184 .peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_GetAlgorithm(
185 _handle, out algorithm));
190 var algorithm = (Backend.Types.peak_icv_chromatic_adaption_algorithm) value;
191 DllHelper.ExecuteAndMapReturnCodes(() =>
193 .peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_SetAlgorithm(
194 _handle, algorithm));
195 UpdateMatrixInAutoFeatureModule();
206 Backend.Types.peak_icv_color_space colorSpace =
207 Backend.Types.peak_icv_color_space.PEAK_ICV_COLOR_SPACE_SRGB_D65;
208 DllHelper.ExecuteAndMapReturnCodes(() =>
210 .peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_GetTargetColorSpace(
211 _handle, out colorSpace));
216 var colorSpace = (Backend.Types.peak_icv_color_space) value;
217 DllHelper.ExecuteAndMapReturnCodes(() =>
219 .peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_SetTargetColorSpace(
220 _handle, colorSpace));
221 UpdateMatrixInAutoFeatureModule();
243 var colorTemperature = 0u;
244 DllHelper.ExecuteAndMapReturnCodes(() =>
246 .peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_GetColorTemperature(
247 _handle, out colorTemperature));
248 return colorTemperature;
254 ResetColorTemperature();
258 var colorTemperature = (uint) value;
259 DllHelper.ExecuteAndMapReturnCodes(() =>
261 .peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_SetColorTemperature(
262 _handle, colorTemperature));
264 UpdateMatrixInAutoFeatureModule();
275 var range =
new Backend.Types.peak_icv_range_u();
276 DllHelper.ExecuteAndMapReturnCodes(() =>
278 .peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_GetColorTemperatureRange(
279 _handle, out range));
280 return new RangeU(range.minimum, range.maximum, range.increment);
284 internal bool IsColorCorrectionEnabled
289 DllHelper.ExecuteAndMapReturnCodes(() =>
291 .peak_icv_Preprocessing_ColorMatrixTransformation_ColorCorrection_IsEnabled(
292 _handle, out enabled));
297 DllHelper.ExecuteAndMapReturnCodes(() =>
299 .peak_icv_Preprocessing_ColorMatrixTransformation_ColorCorrection_SetEnabled(
301 UpdateMatrixInAutoFeatureModule();
305 internal bool IsSaturationEnabled
310 DllHelper.ExecuteAndMapReturnCodes(() =>
311 CInterface.peak_icv_Preprocessing_ColorMatrixTransformation_Saturation_IsEnabled(
312 _handle, out enabled));
317 DllHelper.ExecuteAndMapReturnCodes(() =>
319 .peak_icv_Preprocessing_ColorMatrixTransformation_Saturation_SetEnabled(
321 UpdateMatrixInAutoFeatureModule();
325 internal bool IsChromaticAdaptionEnabled
330 DllHelper.ExecuteAndMapReturnCodes(() =>
332 .peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_IsEnabled(
333 _handle, out enabled));
338 DllHelper.ExecuteAndMapReturnCodes(() =>
340 .peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_SetEnabled(
342 UpdateMatrixInAutoFeatureModule();
352 GC.SuppressFinalize(
this);
355 SafeHandle IHasSafeHandle.Handle => _handle;
373 saturationArchive.
SetBool(EnabledKey, IsSaturationEnabled);
375 archive.
SetArchive(_saturationKey, saturationArchive);
378 matrixArchive.
SetBool(EnabledKey, IsColorCorrectionEnabled);
379 Matrix.Serialize(matrixArchive);
380 archive.
SetArchive(_matrixKey, matrixArchive);
383 chromaticAdaptionArchive.
SetBool(EnabledKey, IsChromaticAdaptionEnabled);
384 chromaticAdaptionArchive.
SetString(_algorithmKey,
389 if (colorTemperature is not
null)
391 chromaticAdaptionArchive.
SetInt64(_colorTemperatureKey, (
long) colorTemperature);
394 archive.
SetArchive(_chromaticAdaptionKey, chromaticAdaptionArchive);
426 var version = archive.
GetInt64(VersionKey);
427 ValidateVersion(version,
TypeId);
432 IsColorCorrectionEnabled = matrixArchive.
GetBool(EnabledKey);
433 IsSaturationEnabled = saturationArchive.
GetBool(EnabledKey);
443 if (chromaticAdaptionArchive.
HasKey(_colorTemperatureKey))
449 ResetColorTemperature();
455 chromaticAdaptionArchive.
GetString(_targetColorSpaceKey));
457 chromaticAdaptionArchive.
GetString(_algorithmKey));
464 IsChromaticAdaptionEnabled = chromaticAdaptionArchive.
GetBool(EnabledKey);
468 ResetChromaticAdaption();
471 UpdateMatrixInAutoFeatureModule();
503 Matrix = DefaultColorCorrectionMatrix;
505 ResetChromaticAdaption();
537 var isColorImage = info.HasChannel(
Channel.Red) && info.HasChannel(
Channel.Green) &&
539 if (!isColorImage || info.IsPacked)
544 DllHelper.ExecuteAndMapReturnCodes(() =>
545 CInterface.peak_icv_Preprocessing_ColorMatrixTransformation_ProcessInplace(
546 _handle, inputImage._handle));
573 return base.Process(input);
582 _autoFeatureModule = module;
583 UpdateMatrixInAutoFeatureModule();
586 internal void ResetChromaticAdaption()
590 ResetColorTemperature();
592 UpdateMatrixInAutoFeatureModule();
595 private void ResetColorTemperature()
597 DllHelper.ExecuteAndMapReturnCodes(() =>
599 .peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_ResetColorTemperature(
602 UpdateMatrixInAutoFeatureModule();
612 DllHelper.ExecuteAndMapReturnCodes(() =>
614 .peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_HasColorTemperature(
615 _handle, out value));
620 private bool NeedsProcessing()
624 DllHelper.ExecuteAndMapReturnCodes(() =>
625 CInterface.peak_icv_Preprocessing_ColorMatrixTransformation_NeedsProcessing(
626 _handle, out value));
631 private void UpdateMatrixInAutoFeatureModule()
633 if (_autoFeatureModule ==
null)
645 DllHelper.ExecuteAndMapReturnCodes(() =>
646 CInterface.peak_icv_Preprocessing_ColorMatrixTransformation_GetEffectiveMatrix(
655 internal sealed class Handle : SafeHandleZeroOrMinusOneIsInvalid
657 public Handle() : base(true)
661 protected override bool ReleaseHandle()
663 return CInterface.peak_icv_Preprocessing_ColorMatrixTransformation_Destroy(handle) ==
664 Backend.Types.peak_icv_status.PEAK_ICV_STATUS_SUCCESS;
668 private static class CInterface
670 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
671 public static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_ColorMatrixTransformation_Create(
674 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
675 public static extern Backend.Types.peak_icv_status peak_icv_Preprocessing_ColorMatrixTransformation_Destroy(
678 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
679 public static extern Backend.Types.peak_icv_status
680 peak_icv_Preprocessing_ColorMatrixTransformation_NeedsProcessing(
681 Handle handle, [MarshalAs(UnmanagedType.I1)] out
bool needsProcessing);
683 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
684 public static extern Backend.Types.peak_icv_status
685 peak_icv_Preprocessing_ColorMatrixTransformation_ProcessInplace(
686 Handle handle,
Image.Handle imageHandle);
688 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
689 public static extern Backend.Types.peak_icv_status
690 peak_icv_Preprocessing_ColorMatrixTransformation_SetColorCorrectionMatrix(
691 Handle handle, Backend.Types.peak_icv_color_correction_matrix matrix);
693 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
694 public static extern Backend.Types.peak_icv_status
695 peak_icv_Preprocessing_ColorMatrixTransformation_GetColorCorrectionMatrix(
696 Handle handle, out Backend.Types.peak_icv_color_correction_matrix matrix);
698 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
699 public static extern Backend.Types.peak_icv_status
700 peak_icv_Preprocessing_ColorMatrixTransformation_GetEffectiveMatrix(
701 Handle handle, out Backend.Types.peak_icv_color_correction_matrix matrix);
703 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
704 public static extern Backend.Types.peak_icv_status
705 peak_icv_Preprocessing_ColorMatrixTransformation_SetSaturation(
706 Handle handle,
float saturation);
708 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
709 public static extern Backend.Types.peak_icv_status
710 peak_icv_Preprocessing_ColorMatrixTransformation_GetSaturation(
711 Handle handle, out
float saturation);
713 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
714 public static extern Backend.Types.peak_icv_status
715 peak_icv_Preprocessing_ColorMatrixTransformation_GetSaturationRange(
716 Handle handle, out Backend.Types.peak_icv_interval_f range);
718 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
719 public static extern Backend.Types.peak_icv_status
720 peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_GetTargetColorSpace(
721 Handle handle, out Backend.Types.peak_icv_color_space colorSpace);
723 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
724 public static extern Backend.Types.peak_icv_status
725 peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_SetTargetColorSpace(
726 Handle handle, Backend.Types.peak_icv_color_space colorSpace);
728 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
729 public static extern Backend.Types.peak_icv_status
730 peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_GetAlgorithm(
731 Handle handle, out Backend.Types.peak_icv_chromatic_adaption_algorithm algorithm);
733 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
734 public static extern Backend.Types.peak_icv_status
735 peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_SetAlgorithm(
736 Handle handle, Backend.Types.peak_icv_chromatic_adaption_algorithm algorithm);
738 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
739 public static extern Backend.Types.peak_icv_status
740 peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_SetColorTemperature(
741 Handle handle, uint colorTemperature);
743 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
744 public static extern Backend.Types.peak_icv_status
745 peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_GetColorTemperature(
746 Handle handle, out uint colorTemperature);
748 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
749 public static extern Backend.Types.peak_icv_status
750 peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_ResetColorTemperature(
753 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
754 public static extern Backend.Types.peak_icv_status
755 peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_GetColorTemperatureRange(
756 Handle handle, out Backend.Types.peak_icv_range_u range);
758 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
759 public static extern Backend.Types.peak_icv_status
760 peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_HasColorTemperature(
761 Handle handle, [MarshalAs(UnmanagedType.I1)] out
bool hasColorTemperature);
763 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
764 public static extern Backend.Types.peak_icv_status
765 peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_SetEnabled(
766 Handle handle, [MarshalAs(UnmanagedType.I1)]
bool enabled);
768 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
769 public static extern Backend.Types.peak_icv_status
770 peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_IsEnabled(
771 Handle handle, [MarshalAs(UnmanagedType.I1)] out
bool enabled);
773 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
774 public static extern Backend.Types.peak_icv_status
775 peak_icv_Preprocessing_ColorMatrixTransformation_Saturation_SetEnabled(
776 Handle handle, [MarshalAs(UnmanagedType.I1)]
bool enabled);
778 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
779 public static extern Backend.Types.peak_icv_status
780 peak_icv_Preprocessing_ColorMatrixTransformation_Saturation_IsEnabled(
781 Handle handle, [MarshalAs(UnmanagedType.I1)] out
bool enabled);
783 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
784 public static extern Backend.Types.peak_icv_status
785 peak_icv_Preprocessing_ColorMatrixTransformation_ColorCorrection_SetEnabled(
786 Handle handle, [MarshalAs(UnmanagedType.I1)]
bool enabled);
788 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
789 public static extern Backend.Types.peak_icv_status
790 peak_icv_Preprocessing_ColorMatrixTransformation_ColorCorrection_IsEnabled(
791 Handle handle, [MarshalAs(UnmanagedType.I1)] out
bool enabled);