IDS peak ICV v1.0.0
Loading...
Searching...
No Matches
peak_icv_color_matrix_transformation_module.hpp
Go to the documentation of this file.
1
11#pragma once
12
19#include <peak_icv_c/algorithms/preprocessing/peak_icv_color_matrix_transformation.h>
20
21namespace peak
22{
23namespace pipeline
24{
25namespace features
26{
29} // namespace features
30
31namespace detail
32{
33
44{
45public:
54
61
70
77
86
93
105 void Serialize(peak::common::serialization::IArchive& archive) const override;
106
121 void Deserialize(const peak::common::serialization::IArchive& archive) override;
122
130 void SetEnabled(bool enabled) override;
131
139 PEAK_COMMON_NO_DISCARD bool IsEnabled() const override;
140
148 PEAK_COMMON_NO_DISCARD const char* GetType() const override;
149
156 void ResetToDefault() override;
157
165 void SetMatrix(const ColorCorrectionMatrix& matrix);
166
175
185 void SetSaturation(float saturation);
186
195
204
220
238
244 void SetAutoFeatureModule(const std::shared_ptr<modules::IAutoFeature>& module);
245
246private:
247#ifndef DOXYGEN_SHOULD_SKIP_THIS
250#endif
251
253 void ApplyMatrix() const;
254 void UpdateCCMInAutoFeatureModule() const;
255 PEAK_COMMON_NO_DISCARD ColorCorrectionMatrix GetEffectiveMatrix() const;
256 void SetSaturationEnabled(bool enabled);
257 void SetColorCorrectionEnabled(bool enabled);
258 PEAK_COMMON_NO_DISCARD bool IsSaturationEnabled() const;
259 PEAK_COMMON_NO_DISCARD bool IsColorCorrectionEnabled() const;
260
261 ColorCorrectionMatrix m_colorCorrectionMatrix{ "Matrix", { ColorCorrectionMatrix::Matrix::Identity() } };
262 static constexpr auto saturationDefault = 1.0F;
263
264 static constexpr auto saturationKey = "Saturation";
265 static constexpr auto matrixKey = "ColorCorrection";
266
267 peak_icv_color_matrix_transformation_handle m_handle{};
268
269 bool m_saturationEnabled{ true };
270 bool m_colorCorrectionEnabled{ true };
271
272 float m_saturation{ saturationDefault };
273 std::shared_ptr<modules::IAutoFeature> m_autoFeatureModule;
274};
275
277{
279 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_Create(&m_handle);
280 });
281
282 m_colorCorrectionMatrix = FetchMatrix();
283}
284
286{
287 if (m_handle != nullptr)
288 {
289 std::ignore = PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_Destroy(m_handle);
290 }
291}
292
294 : m_colorCorrectionMatrix(std::move(other.m_colorCorrectionMatrix))
295 , m_saturationEnabled(other.m_saturationEnabled)
296 , m_colorCorrectionEnabled(other.m_colorCorrectionEnabled)
297{
298 m_handle = std::exchange(other.m_handle, nullptr);
299}
300
302{
303 if (this != &other)
304 {
305 if (m_handle != nullptr)
306 {
308 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_Destroy(m_handle);
309 });
310 }
311
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);
316 }
317
318 return *this;
319}
320
322{
323 archive.SetInt("Version", 1);
324
325 const auto saturationArchive = archive.CreateArchive();
326 saturationArchive->SetBool("Enabled", IsColorCorrectionEnabled());
327 saturationArchive->SetDouble("Value", GetSaturation());
328 archive.SetArchive(saturationKey, saturationArchive);
329
330 const auto matrixArchive = archive.CreateArchive();
331 matrixArchive->SetBool("Enabled", IsColorCorrectionEnabled());
332
333 m_colorCorrectionMatrix.Serialize(*matrixArchive);
334 archive.SetArchive(matrixKey, matrixArchive);
335}
336
338{
339 const auto version = archive.GetInt("Version");
340
342
343 const auto saturationArchive = archive.GetArchive(saturationKey);
344 const auto matrixArchive = archive.GetArchive(matrixKey);
345
346 SetColorCorrectionEnabled(saturationArchive->GetBool("Enabled"));
347 SetSaturationEnabled(matrixArchive->GetBool("Enabled"));
348
349 m_colorCorrectionMatrix.Deserialize(*matrixArchive);
350 SetSaturation(static_cast<float>(saturationArchive->GetDouble("Value")));
351
352 ApplyMatrix();
353}
354
356{
357 SetColorCorrectionEnabled(enabled);
358 SetSaturationEnabled(enabled);
359
360 UpdateCCMInAutoFeatureModule();
361}
362
364{
365 return m_colorCorrectionEnabled || m_saturationEnabled;
366}
367
369{
370 return "ColorMatrixTransformation";
371}
372
374{
375 const auto identity = ColorCorrectionMatrix::Identity();
376 std::copy(identity.cbegin(), identity.cend(), m_colorCorrectionMatrix.begin());
377
378 if (m_colorCorrectionEnabled)
379 {
380 ApplyMatrix();
381 }
382
383 SetSaturation(saturationDefault);
384}
385
387{
388 return m_colorCorrectionMatrix;
389}
390
392{
393 std::copy(matrix.cbegin(), matrix.cend(), m_colorCorrectionMatrix.begin());
394
395 if (m_colorCorrectionEnabled)
396 {
397 ApplyMatrix();
398 }
399}
400
402{
403 if (m_saturationEnabled)
404 {
406 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_SetSaturation(m_handle, saturation);
407 });
408 m_saturation = saturation;
409 UpdateCCMInAutoFeatureModule();
410 }
411 else
412 {
413 m_saturation = saturation;
414 }
415}
416
418{
419 return m_saturation;
420}
421
423{
424 peak_common_interval_f intervalF;
426 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_GetSaturationRange(m_handle, &intervalF);
427 });
428
429 return { intervalF.minimum, intervalF.maximum };
430}
431
436
438{
439 auto needsProcessing = [&] {
440 bool value{};
442 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_NeedsProcessing(m_handle, &value);
443 });
444 return value;
445 };
446
447 if (!IsEnabled() || !needsProcessing())
448 {
449 return input;
450 }
451
452 const auto& inputImage = input.AnyCast<peak::icv::Image>();
453
454 const auto pixelFormat = inputImage.GetPixelFormat();
455 const peak::common::PixelFormatInfo info(pixelFormat);
458 if (!isColorImage || pixelFormat == peak::common::PixelFormat::RGB10p32 || pixelFormat == peak::common::PixelFormat::BGR10p32)
459 {
460 return input;
461 }
462
463 auto* const inputImageHandle = peak::common::detail::BackendAccessor<peak::icv::Image>::BackendHandle(inputImage);
464
466 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_ProcessInplace(m_handle, inputImageHandle);
467 });
468
469 return peak::common::Any{ inputImage };
470}
471
472inline void ColorMatrixTransformationModule::UpdateCCMInAutoFeatureModule() const
473{
474 if (m_autoFeatureModule != nullptr)
475 {
476 std::array<float, 9> ccm{};
477
478 const auto matrix = (m_saturationEnabled || m_colorCorrectionEnabled) ? GetEffectiveMatrix() :
480 std::copy(matrix.cbegin(), matrix.cend(), ccm.begin());
481
482 m_autoFeatureModule->SetColorCorrectionMatrix(ccm);
483 }
484}
485
486inline void ColorMatrixTransformationModule::SetAutoFeatureModule(const std::shared_ptr<modules::IAutoFeature>& module)
487{
488 m_autoFeatureModule = module;
489
490 UpdateCCMInAutoFeatureModule();
491}
492
493inline ColorCorrectionMatrix ColorMatrixTransformationModule::FetchMatrix()
494{
495 ColorCorrectionMatrix colorCorrectionMatrix{ m_colorCorrectionMatrix };
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)));
499 });
500
501 return colorCorrectionMatrix;
502}
503
504inline void ColorMatrixTransformationModule::ApplyMatrix() const
505{
506 auto matrix = m_colorCorrectionEnabled ? m_colorCorrectionMatrix : ColorCorrectionMatrix(ColorCorrectionMatrix::Identity());
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)));
510 });
511
512 UpdateCCMInAutoFeatureModule();
513}
514
515inline ColorCorrectionMatrix ColorMatrixTransformationModule::GetEffectiveMatrix() const
516{
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)));
521 });
522 return matrix;
523}
524
525inline void ColorMatrixTransformationModule::SetSaturationEnabled(bool enabled)
526{
527 m_saturationEnabled = enabled;
528
530 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ColorMatrixTransformation_SetSaturation(
531 m_handle, m_saturationEnabled ? m_saturation : saturationDefault);
532 });
533}
534
535inline void ColorMatrixTransformationModule::SetColorCorrectionEnabled(bool enabled)
536{
537 m_colorCorrectionEnabled = enabled;
538 ApplyMatrix();
539}
540
541inline bool ColorMatrixTransformationModule::IsSaturationEnabled() const
542{
543 return m_saturationEnabled;
544}
545
546inline bool ColorMatrixTransformationModule::IsColorCorrectionEnabled() const
547{
548 return m_colorCorrectionEnabled;
549}
550
551} // namespace detail
552} // namespace pipeline
553} // namespace peak
A type-erased container for any copyable value.
T & AnyCast()
Casts the stored value to the specified type.
Provides information and utility functions for pixel formats.
bool HasChannel(Channel channel) const
Checks whether a specific channel is present in the pixel format.
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 matrix transformation is an image pipeline module that applies a ColorCorrectionMatrix to an im...
ColorCorrectionMatrix GetMatrix() const
Returns the current ColorCorrectionMatrix.
ColorMatrixTransformationModule()
Creates an instance of class ColorMatrixTransformationModule with the default color correction matrix...
const char * GetType() const override
Returns the type of the module for serialization purposes.
ColorMatrixTransformationModule(const ColorMatrixTransformationModule &other)=delete
Copy constructor for class ColorMatrixTransformation is deleted.
void SetAutoFeatureModule(const std::shared_ptr< modules::IAutoFeature > &module)
Sets the auto feature module that should take the color correction matrix into account for automatic ...
void SetMatrix(const ColorCorrectionMatrix &matrix)
Sets the ColorCorrectionMatrix.
void Serialize(peak::common::serialization::IArchive &archive) const override
Serializes the object's internal state into the provided archive.
peak::icv::Image Process(const peak::icv::Image &input) const
Applies the configured ColorCorrectionMatrix and saturation adjustment to the input image.
void SetEnabled(bool enabled) override
Sets the enabled state of this module.
ColorMatrixTransformationModule & operator=(const ColorMatrixTransformationModule &other)=delete
Copy assignment for class ColorMatrixTransformation is deleted.
peak::common::IntervalF GetSaturationRange() const
Returns the saturation range.
void Deserialize(const peak::common::serialization::IArchive &archive) override
Restores the object's state from the provided archive.
void ResetToDefault() override
Resets the ColorCorrectionMatrix to the default and the saturation value to the default.
bool IsEnabled() const override
Checks if the module is currently enabled.
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)