IDS peak ICV v1.0.0
Loading...
Searching...
No Matches
peak_icv_debayer_module.hpp
Go to the documentation of this file.
1
11#pragma once
12
18#include <peak_icv_c/algorithms/preprocessing/peak_icv_image_converter.h>
19
20#include <algorithm>
21#include <sstream>
22#include <string>
23
24namespace peak
25{
26namespace pipeline
27{
28namespace detail
29{
30
42
44{
46 {
47 return "BayerAndMono";
48 }
50 {
51 return "BayerOnly";
52 }
54 {
55 return "Bypass";
56 }
57 const auto policyStr = std::to_string(static_cast<int>(policy));
58 throw peak::icv::NotSupportedException("The given debayer conversion policy " + policyStr + " is unknown!");
59}
60
62{
63 if (policy == "BayerAndMono")
64 {
66 }
67 if (policy == "BayerOnly")
68 {
70 }
71 if (policy == "Bypass")
72 {
74 }
75 throw peak::icv::NotSupportedException("The given debayer conversion policy " + policy + " is unknown!");
76}
77
98{
99public:
106
112 ~DebayerModule() override;
113
121 DebayerModule(const DebayerModule& other) = delete;
122
128 DebayerModule(DebayerModule&& other) noexcept;
129
137 DebayerModule& operator=(const DebayerModule& other) = delete;
138
144 DebayerModule& operator=(DebayerModule&& other) noexcept;
145
157 void Serialize(peak::common::serialization::IArchive& archive) const override;
158
159
174 void Deserialize(const peak::common::serialization::IArchive& archive) override;
175
183 void SetEnabled(bool enabled) override;
184
192 PEAK_COMMON_NO_DISCARD bool IsEnabled() const override;
193
201 PEAK_COMMON_NO_DISCARD const char* GetType() const override;
202
209 void ResetToDefault() override;
210
222
231
243
252
263 void SetTargetBitDepth(size_t bitDepth)
264 {
265 m_targetBitDepth = bitDepth;
266 }
267
284
301
320
334 void ReleaseBuffers() const;
335
336private:
338 PEAK_COMMON_NO_DISCARD size_t GetOutputBitDepth(size_t inputBitDepth) const;
339
340 static void ValidateChannelLayout(DebayerChannelLayout layout);
341 static void ValidateConversionPolicy(DebayerConversionPolicy policy);
342
343 static constexpr int moduleVersion{ 1 };
344 static constexpr auto conversionPolicyKey{ "ConversionPolicy" };
345 static constexpr auto channelLayoutKey{ "ChannelLayout" };
346
347 peak_icv_image_converter_handle m_handle{};
350
351 size_t m_targetBitDepth{ 8 };
352 ProcessingPolicy m_processingPolicy{ ProcessingPolicy::Fast };
353
354 bool m_enabled{ true };
355};
356
358{
360 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ImageConverter_Create(&m_handle);
361 });
362}
363
365{
366 if (m_handle != nullptr)
367 {
368 std::ignore = PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ImageConverter_Destroy(m_handle);
369 }
370}
371
373{
374 m_channelLayout = other.m_channelLayout;
375 m_conversionPolicy = other.m_conversionPolicy;
376 m_enabled = other.m_enabled;
377
378 std::swap(m_handle, other.m_handle);
379}
380
382{
383 if (this != &other)
384 {
385 m_channelLayout = other.m_channelLayout;
386 m_conversionPolicy = other.m_conversionPolicy;
387 m_enabled = other.m_enabled;
388 std::swap(m_handle, other.m_handle);
389 }
390
391 return *this;
392}
393
395{
396 archive.SetBool("Enabled", IsEnabled());
397 archive.SetInt("Version", moduleVersion);
398
399 archive.SetString(channelLayoutKey, ToString(m_channelLayout));
400 archive.SetString(conversionPolicyKey, ToString(m_conversionPolicy));
401}
402
404{
405 const auto version = archive.GetInt("Version");
407
408 SetEnabled(archive.GetBool("Enabled"));
409 m_channelLayout = ToDebayerChannelLayout(archive.GetString(channelLayoutKey));
410 m_conversionPolicy = ToDebayerConversionPolicy(archive.GetString(conversionPolicyKey));
411}
412
414{
415 m_channelLayout = DebayerChannelLayout::RGB;
416 m_conversionPolicy = DebayerConversionPolicy::BayerOnly;
417}
418
419inline void DebayerModule::SetEnabled(bool enabled)
420{
421 m_enabled = enabled;
422}
423
424inline bool DebayerModule::IsEnabled() const
425{
426 return m_enabled;
427}
428
429inline const char* DebayerModule::GetType() const
430{
431 return "Debayer";
432}
433
435{
436 ValidateChannelLayout(layout);
437 m_channelLayout = layout;
438}
439
441{
442 return m_channelLayout;
443}
444
446{
447 ValidateConversionPolicy(policy);
448 m_conversionPolicy = policy;
449}
450
452{
453 return m_conversionPolicy;
454}
455
456inline size_t DebayerModule::GetOutputBitDepth(const size_t inputBitDepth) const
457{
458 if (m_processingPolicy == ProcessingPolicy::Fast)
459 {
460 return (std::min)(inputBitDepth, m_targetBitDepth);
461 }
462 if (m_processingPolicy == ProcessingPolicy::Enhanced)
463 {
464 return (std::max)(inputBitDepth, m_targetBitDepth);
465 }
466 return inputBitDepth;
467}
468
469inline peak::common::PixelFormat DebayerModule::GetOutputFormat(peak::common::PixelFormat inputFormat) const
470{
471 if (m_conversionPolicy == DebayerConversionPolicy::Bypass)
472 {
473 return inputFormat;
474 }
475
476 const peak::common::PixelFormatInfo info(inputFormat);
477
478 if (!info.IsSingleChannel())
479 {
480 return inputFormat;
481 }
482
483 const auto isBayer = info.HasChannel(peak::common::Channel::Bayer);
484 const auto isMono = info.HasIntensityChannel();
485
486 if (!isBayer && (!isMono || m_conversionPolicy != DebayerConversionPolicy::BayerAndMono))
487 {
488 return inputFormat;
489 }
490
491 const auto inputBitDepth = info.GetStorageBitsPerChannel();
492 const auto outputBitDepth = GetOutputBitDepth(inputBitDepth);
493
494 if (outputBitDepth <= 8)
495 {
496 switch (m_channelLayout)
497 {
506 }
507 }
508 if (outputBitDepth <= 10)
509 {
510 switch (m_channelLayout)
511 {
520 }
521 }
522
523 switch (m_channelLayout)
524 {
533 }
534
535 std::ostringstream oss;
536 oss << "0x" << std::uppercase << std::hex << static_cast<int>(inputFormat);
537 throw peak::icv::NotSupportedException("The given input pixel format " + oss.str() + " is unknown!");
538}
539
540inline void DebayerModule::ValidateChannelLayout(DebayerChannelLayout layout)
541{
543 && layout != DebayerChannelLayout::BGRA)
544 {
545 throw peak::icv::NotSupportedException("The given channel layout " + ToString(layout) + " is invalid!");
546 }
547}
548
549inline void DebayerModule::ValidateConversionPolicy(DebayerConversionPolicy policy)
550{
553 {
554 throw peak::icv::NotSupportedException("The given conversion policy " + ToString(policy) + " is invalid!");
555 }
556}
557
559{
560 m_processingPolicy = policy;
561}
562
564{
565 return Process(peak::common::Any(input)).AnyCast<peak::icv::Image>();
566}
567
569{
570 if (!IsEnabled())
571 {
572 return input;
573 }
574
575 const auto& inputImage = input.AnyCast<peak::icv::Image>();
576
577 const auto inputFormat = inputImage.GetPixelFormat();
578 const auto outputFormat = GetOutputFormat(inputFormat);
579
580 if (inputFormat == outputFormat)
581 {
582 return input;
583 }
584
585 auto* const inputImageHandle = peak::common::detail::BackendAccessor<peak::icv::Image>::BackendHandle(inputImage);
586 peak_icv_image_handle outputHandle = nullptr;
587
589 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ImageConverter_Convert(
590 m_handle, inputImageHandle, static_cast<peak_common_pixel_format>(outputFormat), &outputHandle);
591 });
592
593 return peak::common::Any(peak::common::detail::BackendAccessor<peak::icv::Image>::CreateInstance(outputHandle));
594}
595
597{
599 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ImageConverter_ReleaseBuffers(m_handle);
600 });
601}
602
603} // namespace detail
604} // namespace pipeline
605} // 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.
Interface for a hierarchical key-value archive system.
virtual void SetBool(const std::string &key, bool value)=0
Stores a boolean value.
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::string GetString(const std::string &key) const =0
Retrieves a string value by key.
virtual bool GetBool(const std::string &key) const =0
Retrieves a boolean value by key.
virtual void SetString(const std::string &key, const std::string &value)=0
Stores a UTF-8 string.
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.
Thrown when a requested operation is not implemented or supported.
Debayer is an image pipeline module that converts Bayer RAW images into standard color formats.
const char * GetType() const override
Returns the type of the module for serialization purposes.
DebayerChannelLayout GetChannelLayout() const
Gets the current channel layout setting.
DebayerModule()
Creates an instance of class DebayerModule with default settings.
void ReleaseBuffers() const
Frees unused internal buffers.
DebayerModule(const DebayerModule &other)=delete
Copy constructor for class DebayerModule is deleted.
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
Converts a Bayer or mono image to a color image, or passes through unmodified images.
void SetEnabled(bool enabled) override
Sets the enabled state of this module.
DebayerModule & operator=(const DebayerModule &other)=delete
Copy assignment for class DebayerModule is deleted.
void SetConversionPolicy(DebayerConversionPolicy policy)
Sets the conversion policy for the Debayer module.
void SetChannelLayout(DebayerChannelLayout layout)
Sets the desired channel layout for the output image.
void Deserialize(const peak::common::serialization::IArchive &archive) override
Restores the object's state from the provided archive.
void SetProcessingPolicy(ProcessingPolicy policy)
Set the processing policy.
void ResetToDefault() override
Resets the channel layout and the conversion policy to the default settings.
void SetTargetBitDepth(size_t bitDepth)
Set the target bit depth.
DebayerConversionPolicy GetConversionPolicy() const
Retrieves the current conversion policy.
bool IsEnabled() const override
Checks if the module is currently enabled.
Interface for a module used within a pipeline.
#define PEAK_COMMON_NO_DISCARD
Compatability macro for the nodiscard attribute.
PixelFormat
Enum listing all supported pixel formats and their internal IDs.
@ RGBa10
RGBA 10-Bit pixel format.
@ RGB8
RGB 8-Bit pixel format.
@ RGBa8
RGBA 8-Bit pixel format.
@ RGBa12
RGBA 12-Bit pixel format.
@ BGRa12
BGRA 12-Bit pixel format.
@ BGR12
BGR 12-Bit pixel format.
@ RGB10
RGB 10-Bit pixel format.
@ BGR10
BGR 10-Bit pixel format.
@ RGB12
RGB 12-Bit pixel format.
@ BGRa8
BGRA 8-Bit pixel format.
@ BGRa10
BGRA 10-Bit pixel format.
@ BGR8
BGR 8-Bit pixel format.
DebayerChannelLayout
Specifies the desired channel layout of the output image.
ProcessingPolicy
Specifies the processing policy.
@ BGR
Blue-Green-Red format with 3 channels.
@ BGRA
Blue-Green-Red-Alpha format with 4 channels.
@ RGB
Red-Green-Blue format with 3 channels.
@ RGBA
Red-Green-Blue-Alpha format with 4 channels.
void ExecuteAndMapReturnCodes(const CallableType &callableObject)
DebayerConversionPolicy
Defines the conditions under which the Debayer module performs color conversion.
@ BayerOnly
Convert only Bayer-pattern images.
@ BayerAndMono
Convert both Bayer-pattern and monochrome images.
std::string ToString(const DebayerConversionPolicy &policy)
DebayerConversionPolicy ToDebayerConversionPolicy(const std::string &policy)
DebayerChannelLayout ToDebayerChannelLayout(const std::string &layout)
Converts a string representation to a DebayerChannelLayout enum value.
void ValidateVersion(int64_t version, const std::string &moduleName)
peak_icv_image * peak_icv_image_handle