IDS peak ICV v1.3.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{
45public:
54
61
70
77
86
93
105 void Serialize(peak::common::serialization::IArchive& archive) const override;
106
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
160 void ResetToDefault() override;
161
175 void SetGamma(float gamma);
176
184 PEAK_COMMON_NO_DISCARD float GetGamma() const;
185
193 PEAK_COMMON_NO_DISCARD peak::common::IntervalF GetGammaRange() const;
194
206 void SetDigitalBlack(float digitalBlack);
207
215 PEAK_COMMON_NO_DISCARD float GetDigitalBlack() const;
216
224 PEAK_COMMON_NO_DISCARD peak::common::IntervalF GetDigitalBlackRange() const;
225
241 PEAK_COMMON_NO_DISCARD peak::icv::Image Process(const peak::icv::Image& input) const;
242
260 PEAK_COMMON_NO_DISCARD peak::common::Any Process(const peak::common::Any& input) const override;
261
262private:
263#ifndef DOXYGEN_SHOULD_SKIP_THIS
264 friend class features::GammaFeature;
266#endif
267
268 PEAK_COMMON_NO_DISCARD float FetchGamma() const;
269 void ApplyGamma(float value) const;
270 PEAK_COMMON_NO_DISCARD float FetchDigitalBlack() const;
271 void ApplyDigitalBlack(float value) const;
272 void SetGammaEnabled(bool enabled);
273 void SetDigitalBlackEnabled(bool enabled);
274 PEAK_COMMON_NO_DISCARD bool IsGammaEnabled() const;
275 PEAK_COMMON_NO_DISCARD bool IsDigitalBlackEnabled() const;
276
277 static constexpr int moduleVersion{ 1 };
278 static constexpr auto gammaKey{ "Gamma" };
279 static constexpr auto digitalBlackKey{ "DigitalBlack" };
280
281 static constexpr auto gammaDefault = 1.0F;
282 static constexpr auto digitalBlackDefault = 0.0F;
283
284 peak_icv_tone_curve_correction_handle m_handle{};
285 bool m_gammaEnabled{ true };
286 bool m_digitalBlackEnabled{ true };
287
288 float m_gamma{ gammaDefault };
289 float m_digitalBlack{ digitalBlackDefault };
290};
291
293{
295 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_Create(&m_handle);
296 });
297}
298
300{
301 if (m_handle != nullptr)
302 {
303 std::ignore = PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_Destroy(m_handle);
304 }
305}
306
308{
309 m_handle = std::exchange(other.m_handle, nullptr);
310 m_gammaEnabled = other.m_gammaEnabled;
311 m_gamma = other.m_gamma;
312 m_digitalBlackEnabled = other.m_digitalBlackEnabled;
313 m_digitalBlack = other.m_digitalBlack;
314}
315
317{
318 if (this != &other)
319 {
320 if (m_handle != nullptr)
321 {
323 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_Destroy(m_handle);
324 });
325 }
326
327 m_handle = std::exchange(other.m_handle, nullptr);
328 m_gammaEnabled = other.m_gammaEnabled;
329 m_gamma = other.m_gamma;
330 m_digitalBlackEnabled = other.m_digitalBlackEnabled;
331 m_digitalBlack = other.m_digitalBlack;
332 }
333
334 return *this;
335}
336
338{
339 archive.SetInt("Version", moduleVersion);
340
341 const auto gammaArchive = archive.CreateArchive();
342 const auto digitalBlackArchive = archive.CreateArchive();
343
344 gammaArchive->SetBool("Enabled", m_gammaEnabled);
345 digitalBlackArchive->SetBool("Enabled", m_digitalBlackEnabled);
346
347 gammaArchive->SetDouble("Value", m_gamma);
348 digitalBlackArchive->SetDouble("Value", m_digitalBlack);
349
350 archive.SetArchive(gammaKey, gammaArchive);
351 archive.SetArchive(digitalBlackKey, digitalBlackArchive);
352}
353
355{
356 const auto version = archive.GetInt("Version");
357
359
360 const auto gammaArchive = archive.GetArchive(gammaKey);
361 const auto digitalBlackArchive = archive.GetArchive(digitalBlackKey);
362
363 m_gammaEnabled = gammaArchive->GetBool("Enabled");
364 m_digitalBlackEnabled = digitalBlackArchive->GetBool("Enabled");
365
366 m_gamma = static_cast<float>(gammaArchive->GetDouble("Value"));
367 m_digitalBlack = static_cast<float>(digitalBlackArchive->GetDouble("Value"));
368
369 ApplyGamma(m_gammaEnabled ? m_gamma : gammaDefault);
370 ApplyDigitalBlack(m_digitalBlackEnabled ? m_digitalBlack : digitalBlackDefault);
371}
372
374{
375 SetGammaEnabled(enabled);
376 SetDigitalBlackEnabled(enabled);
377}
378
380{
381 return m_gammaEnabled || m_digitalBlackEnabled;
382}
383
384inline const char* ToneCurveCorrectionModule::GetType() const
385{
386 return "ToneCurveCorrection";
387}
388
390{
391 ApplyGamma(gammaDefault);
392 m_gamma = gammaDefault;
393 ApplyDigitalBlack(digitalBlackDefault);
394 m_digitalBlack = digitalBlackDefault;
395}
396
398{
399 if (!m_gammaEnabled)
400 {
401 return m_gamma;
402 }
403
404 return FetchGamma();
405}
406
408{
409 peak_common_interval_f cRange{};
411 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_GetGammaRange(m_handle, &cRange);
412 });
413
414 return peak::common::detail::BackendAccessor<peak::common::IntervalF>::CreateInstance(cRange);
415}
416
418{
419 peak_common_interval_f cRange{};
421 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_GetDigitalBlackRange(m_handle, &cRange);
422 });
423
424 return peak::common::detail::BackendAccessor<peak::common::IntervalF>::CreateInstance(cRange);
425}
426
427inline void ToneCurveCorrectionModule::SetGamma(const float gamma)
428{
429 m_gamma = gamma;
430
431 if (m_gammaEnabled)
432 {
433 ApplyGamma(gamma);
434 }
435}
436
438{
439 if (!m_digitalBlackEnabled)
440 {
441 return m_digitalBlack;
442 }
443 return FetchDigitalBlack();
444}
445
446inline void ToneCurveCorrectionModule::SetDigitalBlack(const float digitalBlack)
447{
448 m_digitalBlack = digitalBlack;
449
450 if (m_digitalBlackEnabled)
451 {
452 ApplyDigitalBlack(digitalBlack);
453 }
454}
455
457{
458 return Process(peak::common::Any(input)).AnyCast<peak::icv::Image>();
459}
460
462{
463 auto needsProcessing = [&] {
464 bool value{};
466 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_NeedsProcessing(m_handle, &value);
467 });
468 return value;
469 };
470
471 if (!IsEnabled() || !needsProcessing())
472 {
473 return input;
474 }
475
476 const auto& inputImage = input.AnyCast<peak::icv::Image>();
477
478 const auto pixelFormat = inputImage.GetPixelFormat();
479 const auto info = peak::common::PixelFormatInfo(pixelFormat);
480 const auto isSupportedMonoImage = pixelFormat == peak::common::PixelFormat::Mono8
482 || pixelFormat == peak::common::PixelFormat::Mono16;
483 const auto isColorImage = info.HasChannel(peak::common::Channel::Red) && info.HasChannel(peak::common::Channel::Green)
484 && info.HasChannel(peak::common::Channel::Blue);
485 const auto isSupportedColorImage = isColorImage && pixelFormat != peak::common::PixelFormat::RGB10p32
486 && pixelFormat != peak::common::PixelFormat::BGR10p32;
487 if (!isSupportedMonoImage && !isSupportedColorImage)
488 {
489 return input;
490 }
491
492 auto* const inputImageHandle = peak::common::detail::BackendAccessor<peak::icv::Image>::BackendHandle(inputImage);
493
495 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_ProcessInplace(m_handle, inputImageHandle);
496 });
497
498 return peak::common::Any{ inputImage };
499}
500
501inline float ToneCurveCorrectionModule::FetchGamma() const
502{
503 float value{};
505 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_GetGamma(m_handle, &value);
506 });
507 return value;
508}
509
510inline void ToneCurveCorrectionModule::ApplyGamma(float value) const
511{
513 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_SetGamma(m_handle, value);
514 });
515}
516
517inline float ToneCurveCorrectionModule::FetchDigitalBlack() const
518{
519 float value{};
521 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_GetDigitalBlack(m_handle, &value);
522 });
523 return value;
524}
525
526inline void ToneCurveCorrectionModule::ApplyDigitalBlack(float value) const
527{
529 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ToneCurveCorrection_SetDigitalBlack(m_handle, value);
530 });
531}
532
533inline void ToneCurveCorrectionModule::SetGammaEnabled(bool enabled)
534{
535 m_gammaEnabled = enabled;
536
537 ApplyGamma(enabled ? m_gamma : gammaDefault);
538}
539
540inline void ToneCurveCorrectionModule::SetDigitalBlackEnabled(bool enabled)
541{
542 m_digitalBlackEnabled = enabled;
543
544 ApplyDigitalBlack(enabled ? m_digitalBlack : digitalBlackDefault);
545}
546
547inline bool ToneCurveCorrectionModule::IsGammaEnabled() const
548{
549 return m_gammaEnabled;
550}
551
552inline bool ToneCurveCorrectionModule::IsDigitalBlackEnabled() const
553{
554 return m_digitalBlackEnabled;
555}
556
557} // namespace detail
558} // namespace pipeline
559} // 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
Enables or disables the 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
Gets whether this 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.
@ 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)