IDS peak ICV v1.0.0
Loading...
Searching...
No Matches
peak_icv_tone_curve_correction_module.hpp
Go to the documentation of this file.
1
11#pragma once
12
17#include <peak_icv_c/algorithms/preprocessing/peak_icv_tone_curve_correction.h>
18
19namespace peak
20{
21namespace pipeline
22{
23namespace features
24{
25class GammaFeature;
26class DigitalBlackFeature;
27} // namespace features
28
29namespace detail
30{
31
44{
45
46public:
55
62
71
78
87
94
106 void Serialize(peak::common::serialization::IArchive& archive) const override;
107
122 void Deserialize(const peak::common::serialization::IArchive& archive) override;
123
131 void SetEnabled(bool enabled) override;
132
140 PEAK_COMMON_NO_DISCARD bool IsEnabled() const override;
141
149 PEAK_COMMON_NO_DISCARD const char* GetType() const override;
150
158 void ResetToDefault() override;
159
173 void SetGamma(float gamma);
174
182 PEAK_COMMON_NO_DISCARD float GetGamma() const;
183
192
204 void SetDigitalBlack(float digitalBlack);
205
214
223
240
259
260private:
261#ifndef DOXYGEN_SHOULD_SKIP_THIS
262 friend class features::GammaFeature;
264#endif
265
266 PEAK_COMMON_NO_DISCARD float FetchGamma() const;
267 void ApplyGamma(float value) const;
268 PEAK_COMMON_NO_DISCARD float FetchDigitalBlack() const;
269 void ApplyDigitalBlack(float value) const;
270 void SetGammaEnabled(bool enabled);
271 void SetDigitalBlackEnabled(bool enabled);
272 PEAK_COMMON_NO_DISCARD bool IsGammaEnabled() const;
273 PEAK_COMMON_NO_DISCARD bool IsDigitalBlackEnabled() const;
274
275 static constexpr int moduleVersion{ 1 };
276 static constexpr auto gammaKey{ "Gamma" };
277 static constexpr auto digitalBlackKey{ "DigitalBlack" };
278
279 static constexpr auto gammaDefault = 1.0F;
280 static constexpr auto digitalBlackDefault = 0.0F;
281
282 peak_icv_tone_curve_correction_handle m_handle{};
283 bool m_gammaEnabled{ true };
284 bool m_digitalBlackEnabled{ true };
285
286 float m_gamma{ gammaDefault };
287 float m_digitalBlack{ digitalBlackDefault };
288};
289
291{
293 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_Create(&m_handle);
294 });
295}
296
298{
299 if (m_handle != nullptr)
300 {
301 std::ignore = PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_Destroy(m_handle);
302 }
303}
304
306{
307 m_handle = std::exchange(other.m_handle, nullptr);
308 m_gammaEnabled = other.m_gammaEnabled;
309 m_gamma = other.m_gamma;
310 m_digitalBlackEnabled = other.m_digitalBlackEnabled;
311 m_digitalBlack = other.m_digitalBlack;
312}
313
315{
316 if (this != &other)
317 {
318 if (m_handle != nullptr)
319 {
321 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_Destroy(m_handle);
322 });
323 }
324
325 m_handle = std::exchange(other.m_handle, nullptr);
326 m_gammaEnabled = other.m_gammaEnabled;
327 m_gamma = other.m_gamma;
328 m_digitalBlackEnabled = other.m_digitalBlackEnabled;
329 m_digitalBlack = other.m_digitalBlack;
330 }
331
332 return *this;
333}
334
336{
337 archive.SetInt("Version", moduleVersion);
338
339 const auto gammaArchive = archive.CreateArchive();
340 const auto digitalBlackArchive = archive.CreateArchive();
341
342 gammaArchive->SetBool("Enabled", m_gammaEnabled);
343 digitalBlackArchive->SetBool("Enabled", m_digitalBlackEnabled);
344
345 gammaArchive->SetDouble("Value", m_gamma);
346 digitalBlackArchive->SetDouble("Value", m_digitalBlack);
347
348 archive.SetArchive(gammaKey, gammaArchive);
349 archive.SetArchive(digitalBlackKey, digitalBlackArchive);
350}
351
353{
354 const auto version = archive.GetInt("Version");
355
357
358 const auto gammaArchive = archive.GetArchive(gammaKey);
359 const auto digitalBlackArchive = archive.GetArchive(digitalBlackKey);
360
361 m_gammaEnabled = gammaArchive->GetBool("Enabled");
362 m_digitalBlackEnabled = digitalBlackArchive->GetBool("Enabled");
363
364 m_gamma = static_cast<float>(gammaArchive->GetDouble("Value"));
365 m_digitalBlack = static_cast<float>(digitalBlackArchive->GetDouble("Value"));
366
367 ApplyGamma(m_gammaEnabled ? m_gamma : gammaDefault);
368 ApplyDigitalBlack(m_digitalBlackEnabled ? m_digitalBlack : digitalBlackDefault);
369}
370
372{
373 m_gammaEnabled = enabled;
374 m_digitalBlackEnabled = enabled;
375}
376
378{
379 return m_gammaEnabled || m_digitalBlackEnabled;
380}
381
382inline const char* ToneCurveCorrectionModule::GetType() const
383{
384 return "ToneCurveCorrection";
385}
386
388{
389 ApplyGamma(gammaDefault);
390 ApplyDigitalBlack(digitalBlackDefault);
391}
392
394{
395 if (!m_gammaEnabled)
396 {
397 return m_gamma;
398 }
399
400 return FetchGamma();
401}
402
404{
405 peak_common_interval_f cRange{};
407 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_GetGammaRange(m_handle, &cRange);
408 });
409
410 return peak::common::detail::BackendAccessor<peak::common::IntervalF>::CreateInstance(cRange);
411}
412
414{
415 peak_common_interval_f cRange{};
417 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_GetDigitalBlackRange(m_handle, &cRange);
418 });
419
420 return peak::common::detail::BackendAccessor<peak::common::IntervalF>::CreateInstance(cRange);
421}
422
423inline void ToneCurveCorrectionModule::SetGamma(const float gamma)
424{
425 m_gamma = gamma;
426
427 if (m_gammaEnabled)
428 {
429 ApplyGamma(gamma);
430 }
431}
432
434{
435 if (!m_digitalBlackEnabled)
436 {
437 return m_digitalBlack;
438 }
439 return FetchDigitalBlack();
440}
441
442inline void ToneCurveCorrectionModule::SetDigitalBlack(const float digitalBlack)
443{
444 m_digitalBlack = digitalBlack;
445
446 if (m_digitalBlackEnabled)
447 {
448 ApplyDigitalBlack(digitalBlack);
449 }
450}
451
453{
454 return Process(peak::common::Any(input)).AnyCast<peak::icv::Image>();
455}
456
458{
459 auto needsProcessing = [&] {
460 bool value{};
462 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_NeedsProcessing(m_handle, &value);
463 });
464 return value;
465 };
466
467 if (!IsEnabled() || !needsProcessing())
468 {
469 return input;
470 }
471
472 const auto& inputImage = input.AnyCast<peak::icv::Image>();
473
474 const auto pixelFormat = inputImage.GetPixelFormat();
475 const auto info = peak::common::PixelFormatInfo(pixelFormat);
476 const auto isSupportedMonoImage = pixelFormat == peak::common::PixelFormat::Mono8
478 || pixelFormat == peak::common::PixelFormat::Mono16;
479 const auto isColorImage = info.HasChannel(peak::common::Channel::Red) && info.HasChannel(peak::common::Channel::Green)
480 && info.HasChannel(peak::common::Channel::Blue);
481 const auto isSupportedColorImage = isColorImage && pixelFormat != peak::common::PixelFormat::RGB10p32
482 && pixelFormat != peak::common::PixelFormat::BGR10p32;
483 if (!isSupportedMonoImage && !isSupportedColorImage)
484 {
485 return input;
486 }
487
488 auto* const inputImageHandle = peak::common::detail::BackendAccessor<peak::icv::Image>::BackendHandle(inputImage);
489
491 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_ProcessInplace(m_handle, inputImageHandle);
492 });
493
494 return peak::common::Any{ inputImage };
495}
496
497inline float ToneCurveCorrectionModule::FetchGamma() const
498{
499 float value{};
501 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_GetGamma(m_handle, &value);
502 });
503 return value;
504}
505
506inline void ToneCurveCorrectionModule::ApplyGamma(float value) const
507{
509 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_SetGamma(m_handle, value);
510 });
511}
512
513inline float ToneCurveCorrectionModule::FetchDigitalBlack() const
514{
515 float value{};
517 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_GetDigitalBlack(m_handle, &value);
518 });
519 return value;
520}
521
522inline void ToneCurveCorrectionModule::ApplyDigitalBlack(float value) const
523{
525 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_SetDigitalBlack(m_handle, value);
526 });
527}
528
529inline void ToneCurveCorrectionModule::SetGammaEnabled(bool enabled)
530{
531 m_gammaEnabled = enabled;
532
533 ApplyGamma(enabled ? m_gamma : gammaDefault);
534}
535
536inline void ToneCurveCorrectionModule::SetDigitalBlackEnabled(bool enabled)
537{
538 m_digitalBlackEnabled = enabled;
539
540 ApplyDigitalBlack(enabled ? m_digitalBlack : digitalBlackDefault);
541}
542
543inline bool ToneCurveCorrectionModule::IsGammaEnabled() const
544{
545 return m_gammaEnabled;
546}
547
548inline bool ToneCurveCorrectionModule::IsDigitalBlackEnabled() const
549{
550 return m_digitalBlackEnabled;
551}
552
553} // namespace detail
554} // namespace pipeline
555} // 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.
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.
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.
Tone curve correction is an image pipeline module that applies digital black correction and inverse g...
const char * GetType() const override
Returns the type of the module for serialization purposes.
void SetDigitalBlack(float digitalBlack)
Sets the digital black used for digital black correction.
peak::common::IntervalF GetDigitalBlackRange() const
Returns the valid range for the digital black correction value.
ToneCurveCorrectionModule & operator=(const ToneCurveCorrectionModule &other)=delete
Copy assignment for class ToneCurveCorrectionModule is deleted.
void SetGamma(float gamma)
Sets the gamma correction exponent.
float GetGamma() const
Gets the current gamma correction exponent.
peak::common::IntervalF GetGammaRange() const
Returns the valid range for the gamma correction exponent.
void Serialize(peak::common::serialization::IArchive &archive) const override
Serializes the object's internal state into the provided archive.
ToneCurveCorrectionModule(const ToneCurveCorrectionModule &other)=delete
Copy constructor for class ToneCurveCorrectionModule is deleted.
peak::icv::Image Process(const peak::icv::Image &input) const
Processes the input image and returns a tone curve corrected output image.
void SetEnabled(bool enabled) override
Sets the enabled state of this module.
ToneCurveCorrectionModule()
Creates an instance of class ToneCurveCorrectionModule with default values for gamma and black.
void Deserialize(const peak::common::serialization::IArchive &archive) override
Restores the object's state from the provided archive.
void ResetToDefault() override
Resets all settings to the default values for gamma and black.
float GetDigitalBlack() const
Gets the current digital black used for correction.
bool IsEnabled() const override
Checks if the module is currently enabled.
Digital black applies digital black correction to an image.
Gamma applies an inverse gamma transformation to an image.
Interface for a module used within a pipeline.
#define PEAK_COMMON_NO_DISCARD
Compatability macro for the nodiscard attribute.
@ Mono12
Mono 12-Bit pixel format.
@ BGR10p32
BGR 10-Bit packed 32-bit format.
@ Mono16
Mono 16-Bit pixel format.
@ Mono10
Mono 10-Bit pixel format.
@ Mono8
Mono 8-Bit pixel format.
@ RGB10p32
RGB 10-Bit packed 32-bit format.
void ExecuteAndMapReturnCodes(const CallableType &callableObject)
void ValidateVersion(int64_t version, const std::string &moduleName)