19#include <peak_icv_c/algorithms/preprocessing/peak_icv_color_matrix_transformation.h>
140 PEAK_COMMON_NO_DISCARD
bool IsEnabled()
const override;
149 PEAK_COMMON_NO_DISCARD
const char*
GetType()
const override;
248#ifndef DOXYGEN_SHOULD_SKIP_THIS
254 void ApplyMatrix()
const;
255 void UpdateCCMInAutoFeatureModule()
const;
257 void SetSaturationEnabled(
bool enabled);
258 void SetColorCorrectionEnabled(
bool enabled);
259 PEAK_COMMON_NO_DISCARD
bool IsSaturationEnabled()
const;
260 PEAK_COMMON_NO_DISCARD
bool IsColorCorrectionEnabled()
const;
262 ColorCorrectionMatrix m_colorCorrectionMatrix{
"Matrix", { ColorCorrectionMatrix::Matrix::Identity() } };
263 static constexpr auto saturationDefault = 1.0F;
265 static constexpr auto saturationKey =
"Saturation";
266 static constexpr auto matrixKey =
"ColorCorrection";
268 peak_icv_color_matrix_transformation_handle m_handle{};
270 bool m_saturationEnabled{
true };
271 bool m_colorCorrectionEnabled{
true };
273 float m_saturation{ saturationDefault };
274 std::shared_ptr<modules::IAutoFeature> m_autoFeatureModule;
280 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_Create(&m_handle);
283 m_colorCorrectionMatrix = FetchMatrix();
288 if (m_handle !=
nullptr)
290 std::ignore = PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_Destroy(m_handle);
295 : m_colorCorrectionMatrix(std::move(other.m_colorCorrectionMatrix))
296 , m_saturationEnabled(other.m_saturationEnabled)
297 , m_colorCorrectionEnabled(other.m_colorCorrectionEnabled)
299 m_handle = std::exchange(other.m_handle,
nullptr);
306 if (m_handle !=
nullptr)
309 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_Destroy(m_handle);
313 m_handle = std::exchange(other.m_handle,
nullptr);
314 m_colorCorrectionEnabled = other.m_colorCorrectionEnabled;
315 m_saturationEnabled = other.m_saturationEnabled;
316 m_colorCorrectionMatrix = std::move(other.m_colorCorrectionMatrix);
324 archive.
SetInt(
"Version", 1);
327 saturationArchive->SetBool(
"Enabled", IsColorCorrectionEnabled());
329 archive.
SetArchive(saturationKey, saturationArchive);
332 matrixArchive->SetBool(
"Enabled", IsColorCorrectionEnabled());
334 m_colorCorrectionMatrix.
Serialize(*matrixArchive);
340 const auto version = archive.
GetInt(
"Version");
344 const auto saturationArchive = archive.
GetArchive(saturationKey);
345 const auto matrixArchive = archive.
GetArchive(matrixKey);
347 SetColorCorrectionEnabled(saturationArchive->GetBool(
"Enabled"));
348 SetSaturationEnabled(matrixArchive->GetBool(
"Enabled"));
350 m_colorCorrectionMatrix.
Deserialize(*matrixArchive);
351 SetSaturation(
static_cast<float>(saturationArchive->GetDouble(
"Value")));
358 SetColorCorrectionEnabled(enabled);
359 SetSaturationEnabled(enabled);
361 UpdateCCMInAutoFeatureModule();
366 return m_colorCorrectionEnabled || m_saturationEnabled;
371 return "ColorMatrixTransformation";
377 std::copy(identity.cbegin(), identity.cend(), m_colorCorrectionMatrix.
begin());
379 if (m_colorCorrectionEnabled)
389 return m_colorCorrectionMatrix;
394 std::copy(matrix.
cbegin(), matrix.
cend(), m_colorCorrectionMatrix.
begin());
396 if (m_colorCorrectionEnabled)
404 if (m_saturationEnabled)
407 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_SetSaturation(m_handle, saturation);
409 m_saturation = saturation;
410 UpdateCCMInAutoFeatureModule();
414 m_saturation = saturation;
425 peak_common_interval_f intervalF;
427 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_GetSaturationRange(m_handle, &intervalF);
430 return { intervalF.minimum, intervalF.maximum };
440 auto needsProcessing = [&] {
443 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_NeedsProcessing(m_handle, &value);
464 auto*
const inputImageHandle = peak::common::detail::BackendAccessor<peak::icv::Image>::BackendHandle(inputImage);
467 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_ProcessInplace(m_handle, inputImageHandle);
473inline void ColorMatrixTransformationModule::UpdateCCMInAutoFeatureModule()
const
475 if (m_autoFeatureModule !=
nullptr)
477 std::array<float, 9> ccm{};
479 const auto matrix = (m_saturationEnabled || m_colorCorrectionEnabled) ? GetEffectiveMatrix() :
481 std::copy(matrix.cbegin(), matrix.cend(), ccm.begin());
483 m_autoFeatureModule->SetColorCorrectionMatrix(ccm);
489 m_autoFeatureModule =
module;
491 UpdateCCMInAutoFeatureModule();
498 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_GetColorCorrectionMatrix(
499 m_handle,
reinterpret_cast<peak_icv_color_correction_matrix*
>(&colorCorrectionMatrix.At(0, 0)));
502 return colorCorrectionMatrix;
505inline void ColorMatrixTransformationModule::ApplyMatrix()
const
509 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_SetColorCorrectionMatrix(
510 m_handle, *
reinterpret_cast<peak_icv_color_correction_matrix*
>(&matrix.At(0, 0)));
513 UpdateCCMInAutoFeatureModule();
516inline ColorCorrectionMatrix ColorMatrixTransformationModule::GetEffectiveMatrix()
const
518 ColorCorrectionMatrix matrix{};
520 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_GetEffectiveMatrix(
521 m_handle,
reinterpret_cast<peak_icv_color_correction_matrix*
>(&matrix.At(0, 0)));
526inline void ColorMatrixTransformationModule::SetSaturationEnabled(
bool enabled)
528 m_saturationEnabled = enabled;
531 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_SetSaturation(
532 m_handle, m_saturationEnabled ? m_saturation : saturationDefault);
536inline void ColorMatrixTransformationModule::SetColorCorrectionEnabled(
bool enabled)
538 m_colorCorrectionEnabled = enabled;
542inline bool ColorMatrixTransformationModule::IsSaturationEnabled()
const
544 return m_saturationEnabled;
547inline bool ColorMatrixTransformationModule::IsColorCorrectionEnabled()
const
549 return m_colorCorrectionEnabled;
A type-erased container for any copyable value.
T & AnyCast()
Casts the stored value to the specified type.
Represents a continuous numeric interval with inclusive lower and upper bounds.
Interface for a hierarchical key-value archive system.
virtual std::shared_ptr< IArchive > GetArchive(const std::string &key) const =0
Retrieves a nested archive by key.
virtual void SetInt(const std::string &key, int64_t value)=0
Stores a 64-bit signed integer.
virtual int64_t GetInt(const std::string &key) const =0
Retrieves a 64-bit signed integer by key.
virtual std::shared_ptr< IArchive > CreateArchive() const =0
Creates a new, empty archive instance.
virtual void SetArchive(const std::string &key, const std::shared_ptr< IArchive > &value)=0
Stores a nested archive object.
auto cbegin() const
Returns a const iterator to the beginning of the array.
auto begin()
Returns an iterator to the beginning of the array.
auto cend() const
Returns a const iterator to the end of the array.
For detailed information about the use of ids_peak_icv Image refer to the Image Guide.
peak::common::PixelFormat GetPixelFormat() const
Provides the pixel format of the image.
void Serialize(peak::common::serialization::IArchive &archive) const override
Serializes the object's internal state into the provided archive.
void Deserialize(const peak::common::serialization::IArchive &archive) override
Restores the object's state from the provided archive.
Color correction applies a 3x3 ColorCorrectionMatrix to the image.
Adjusts the saturation of an image by modifying a 3x3 ColorCorrectionMatrix.
Interface for a module used within a pipeline.
@ BGR10p32
BGR 10-Bit packed 32-bit format.
@ RGB10p32
RGB 10-Bit packed 32-bit format.
void ExecuteAndMapReturnCodes(const CallableType &callableObject)
void ValidateVersion(int64_t version, const std::string &moduleName)