19#include <peak_icv_c/algorithms/preprocessing/peak_icv_color_matrix_transformation.h>
247#ifndef DOXYGEN_SHOULD_SKIP_THIS
253 void ApplyMatrix()
const;
254 void UpdateCCMInAutoFeatureModule()
const;
256 void SetSaturationEnabled(
bool enabled);
257 void SetColorCorrectionEnabled(
bool enabled);
261 ColorCorrectionMatrix m_colorCorrectionMatrix{
"Matrix", { ColorCorrectionMatrix::Matrix::Identity() } };
262 static constexpr auto saturationDefault = 1.0F;
264 static constexpr auto saturationKey =
"Saturation";
265 static constexpr auto matrixKey =
"ColorCorrection";
267 peak_icv_color_matrix_transformation_handle m_handle{};
269 bool m_saturationEnabled{
true };
270 bool m_colorCorrectionEnabled{
true };
272 float m_saturation{ saturationDefault };
273 std::shared_ptr<modules::IAutoFeature> m_autoFeatureModule;
279 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_Create(&m_handle);
282 m_colorCorrectionMatrix = FetchMatrix();
287 if (m_handle !=
nullptr)
289 std::ignore = PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_Destroy(m_handle);
294 : m_colorCorrectionMatrix(std::move(other.m_colorCorrectionMatrix))
295 , m_saturationEnabled(other.m_saturationEnabled)
296 , m_colorCorrectionEnabled(other.m_colorCorrectionEnabled)
298 m_handle = std::exchange(other.m_handle,
nullptr);
305 if (m_handle !=
nullptr)
308 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_Destroy(m_handle);
312 m_handle = std::exchange(other.m_handle,
nullptr);
313 m_colorCorrectionEnabled = other.m_colorCorrectionEnabled;
314 m_saturationEnabled = other.m_saturationEnabled;
315 m_colorCorrectionMatrix = std::move(other.m_colorCorrectionMatrix);
323 archive.
SetInt(
"Version", 1);
326 saturationArchive->SetBool(
"Enabled", IsColorCorrectionEnabled());
328 archive.
SetArchive(saturationKey, saturationArchive);
331 matrixArchive->SetBool(
"Enabled", IsColorCorrectionEnabled());
333 m_colorCorrectionMatrix.
Serialize(*matrixArchive);
339 const auto version = archive.
GetInt(
"Version");
343 const auto saturationArchive = archive.
GetArchive(saturationKey);
344 const auto matrixArchive = archive.
GetArchive(matrixKey);
346 SetColorCorrectionEnabled(saturationArchive->GetBool(
"Enabled"));
347 SetSaturationEnabled(matrixArchive->GetBool(
"Enabled"));
349 m_colorCorrectionMatrix.
Deserialize(*matrixArchive);
350 SetSaturation(
static_cast<float>(saturationArchive->GetDouble(
"Value")));
357 SetColorCorrectionEnabled(enabled);
358 SetSaturationEnabled(enabled);
360 UpdateCCMInAutoFeatureModule();
365 return m_colorCorrectionEnabled || m_saturationEnabled;
370 return "ColorMatrixTransformation";
376 std::copy(identity.cbegin(), identity.cend(), m_colorCorrectionMatrix.
begin());
378 if (m_colorCorrectionEnabled)
388 return m_colorCorrectionMatrix;
393 std::copy(matrix.
cbegin(), matrix.
cend(), m_colorCorrectionMatrix.
begin());
395 if (m_colorCorrectionEnabled)
403 if (m_saturationEnabled)
406 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_SetSaturation(m_handle, saturation);
408 m_saturation = saturation;
409 UpdateCCMInAutoFeatureModule();
413 m_saturation = saturation;
424 peak_common_interval_f intervalF;
426 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_GetSaturationRange(m_handle, &intervalF);
429 return { intervalF.minimum, intervalF.maximum };
439 auto needsProcessing = [&] {
442 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_NeedsProcessing(m_handle, &value);
463 auto*
const inputImageHandle = peak::common::detail::BackendAccessor<peak::icv::Image>::BackendHandle(inputImage);
466 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_ProcessInplace(m_handle, inputImageHandle);
472inline void ColorMatrixTransformationModule::UpdateCCMInAutoFeatureModule()
const
474 if (m_autoFeatureModule !=
nullptr)
476 std::array<float, 9> ccm{};
478 const auto matrix = (m_saturationEnabled || m_colorCorrectionEnabled) ? GetEffectiveMatrix() :
480 std::copy(matrix.cbegin(), matrix.cend(), ccm.begin());
482 m_autoFeatureModule->SetColorCorrectionMatrix(ccm);
488 m_autoFeatureModule =
module;
490 UpdateCCMInAutoFeatureModule();
497 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_GetColorCorrectionMatrix(
498 m_handle,
reinterpret_cast<peak_icv_color_correction_matrix*
>(&colorCorrectionMatrix.At(0, 0)));
501 return colorCorrectionMatrix;
504inline void ColorMatrixTransformationModule::ApplyMatrix()
const
508 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_SetColorCorrectionMatrix(
509 m_handle, *
reinterpret_cast<peak_icv_color_correction_matrix*
>(&matrix.At(0, 0)));
512 UpdateCCMInAutoFeatureModule();
515inline ColorCorrectionMatrix ColorMatrixTransformationModule::GetEffectiveMatrix()
const
517 ColorCorrectionMatrix matrix{};
519 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_GetEffectiveMatrix(
520 m_handle,
reinterpret_cast<peak_icv_color_correction_matrix*
>(&matrix.At(0, 0)));
525inline void ColorMatrixTransformationModule::SetSaturationEnabled(
bool enabled)
527 m_saturationEnabled = enabled;
530 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_SetSaturation(
531 m_handle, m_saturationEnabled ? m_saturation : saturationDefault);
535inline void ColorMatrixTransformationModule::SetColorCorrectionEnabled(
bool enabled)
537 m_colorCorrectionEnabled = enabled;
541inline bool ColorMatrixTransformationModule::IsSaturationEnabled()
const
543 return m_saturationEnabled;
546inline bool ColorMatrixTransformationModule::IsColorCorrectionEnabled()
const
548 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.
#define PEAK_COMMON_NO_DISCARD
Compatability macro for the nodiscard attribute.
@ 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)