IDS peak ICV v1.2.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{
42
43PEAK_COMMON_NO_DISCARD inline std::string ToString(const DebayerConversionPolicy& policy)
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
61PEAK_COMMON_NO_DISCARD inline DebayerConversionPolicy ToDebayerConversionPolicy(const std::string& policy)
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
175 void Deserialize(const peak::common::serialization::IArchive& archive) override;
176
184 void SetEnabled(bool enabled) override;
185
193 PEAK_COMMON_NO_DISCARD bool IsEnabled() const override;
194
202 PEAK_COMMON_NO_DISCARD const char* GetType() const override;
203
210 void ResetToDefault() override;
211
223
231 PEAK_COMMON_NO_DISCARD DebayerChannelLayout GetChannelLayout() const;
232
244
252 PEAK_COMMON_NO_DISCARD DebayerConversionPolicy GetConversionPolicy() const;
253
264 void SetTargetBitDepth(size_t bitDepth)
265 {
266 m_targetBitDepth = bitDepth;
267 }
268
285
301 PEAK_COMMON_NO_DISCARD peak::icv::Image Process(const peak::icv::Image& input) const;
302
320 PEAK_COMMON_NO_DISCARD peak::common::Any Process(const peak::common::Any& input) const override;
321
335 void ReleaseBuffers() const;
336
337private:
338 PEAK_COMMON_NO_DISCARD peak::common::PixelFormat GetOutputFormat(peak::common::PixelFormat inputFormat) const;
339 PEAK_COMMON_NO_DISCARD size_t GetOutputBitDepth(size_t inputBitDepth) const;
340
341 static void ValidateChannelLayout(DebayerChannelLayout layout);
342 static void ValidateConversionPolicy(DebayerConversionPolicy policy);
343
344 static constexpr int moduleVersion{ 1 };
345 static constexpr auto conversionPolicyKey{ "ConversionPolicy" };
346 static constexpr auto channelLayoutKey{ "ChannelLayout" };
347
348 peak_icv_image_converter_handle m_handle{};
351
352 size_t m_targetBitDepth{ 8 };
353 ProcessingPolicy m_processingPolicy{ ProcessingPolicy::Fast };
354
355 bool m_enabled{ true };
356};
357
359{
361 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ImageConverter_Create(&m_handle);
362 });
363}
364
366{
367 if (m_handle != nullptr)
368 {
369 std::ignore = PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ImageConverter_Destroy(m_handle);
370 }
371}
372
374{
375 m_channelLayout = other.m_channelLayout;
376 m_conversionPolicy = other.m_conversionPolicy;
377 m_enabled = other.m_enabled;
378
379 std::swap(m_handle, other.m_handle);
380}
381
383{
384 if (this != &other)
385 {
386 m_channelLayout = other.m_channelLayout;
387 m_conversionPolicy = other.m_conversionPolicy;
388 m_enabled = other.m_enabled;
389 std::swap(m_handle, other.m_handle);
390 }
391
392 return *this;
393}
394
396{
397 archive.SetBool("Enabled", IsEnabled());
398 archive.SetInt("Version", moduleVersion);
399
400 archive.SetString(channelLayoutKey, ToString(m_channelLayout));
401 archive.SetString(conversionPolicyKey, ToString(m_conversionPolicy));
402}
403
405{
406 const auto version = archive.GetInt("Version");
408
409 SetEnabled(archive.GetBool("Enabled"));
410 m_channelLayout = ToDebayerChannelLayout(archive.GetString(channelLayoutKey));
411 m_conversionPolicy = ToDebayerConversionPolicy(archive.GetString(conversionPolicyKey));
412}
413
415{
416 m_channelLayout = DebayerChannelLayout::RGB;
417 m_conversionPolicy = DebayerConversionPolicy::BayerOnly;
418}
419
420inline void DebayerModule::SetEnabled(bool enabled)
421{
422 m_enabled = enabled;
423}
424
425inline bool DebayerModule::IsEnabled() const
426{
427 return m_enabled;
428}
429
430inline const char* DebayerModule::GetType() const
431{
432 return "Debayer";
433}
434
436{
437 ValidateChannelLayout(layout);
438 m_channelLayout = layout;
439}
440
442{
443 return m_channelLayout;
444}
445
447{
448 ValidateConversionPolicy(policy);
449 m_conversionPolicy = policy;
450}
451
453{
454 return m_conversionPolicy;
455}
456
457inline size_t DebayerModule::GetOutputBitDepth(const size_t inputBitDepth) const
458{
459 if (m_processingPolicy == ProcessingPolicy::Fast)
460 {
461 return (std::min)(inputBitDepth, m_targetBitDepth);
462 }
463 if (m_processingPolicy == ProcessingPolicy::Enhanced)
464 {
465 return (std::max)(inputBitDepth, m_targetBitDepth);
466 }
467 return inputBitDepth;
468}
469
470inline peak::common::PixelFormat DebayerModule::GetOutputFormat(peak::common::PixelFormat inputFormat) const
471{
472 if (m_conversionPolicy == DebayerConversionPolicy::Bypass)
473 {
474 return inputFormat;
475 }
476
477 const peak::common::PixelFormatInfo info(inputFormat);
478
479 if (!info.IsSingleChannel())
480 {
481 return inputFormat;
482 }
483
484 const auto isBayer = info.HasChannel(peak::common::Channel::Bayer);
485 const auto isMono = info.HasIntensityChannel();
486
487 if (!isBayer && (!isMono || m_conversionPolicy != DebayerConversionPolicy::BayerAndMono))
488 {
489 return inputFormat;
490 }
491
492 const auto inputBitDepth = info.GetStorageBitsPerChannel();
493 const auto outputBitDepth = GetOutputBitDepth(inputBitDepth);
494
495 if (outputBitDepth <= 8)
496 {
497 switch (m_channelLayout)
498 {
507 }
508 }
509 if (outputBitDepth <= 10)
510 {
511 switch (m_channelLayout)
512 {
521 }
522 }
523
524 switch (m_channelLayout)
525 {
534 }
535
536 std::ostringstream oss;
537 oss << "0x" << std::uppercase << std::hex << static_cast<int>(inputFormat);
538 throw peak::icv::NotSupportedException("The given input pixel format " + oss.str() + " is unknown!");
539}
540
541inline void DebayerModule::ValidateChannelLayout(DebayerChannelLayout layout)
542{
544 && layout != DebayerChannelLayout::BGRA)
545 {
546 throw peak::icv::NotSupportedException("The given channel layout " + ToString(layout) + " is invalid!");
547 }
548}
549
550inline void DebayerModule::ValidateConversionPolicy(DebayerConversionPolicy policy)
551{
554 {
555 throw peak::icv::NotSupportedException("The given conversion policy " + ToString(policy) + " is invalid!");
556 }
557}
558
560{
561 m_processingPolicy = policy;
562}
563
565{
566 return Process(peak::common::Any(input)).AnyCast<peak::icv::Image>();
567}
568
570{
571 if (!IsEnabled())
572 {
573 return input;
574 }
575
576 const auto& inputImage = input.AnyCast<peak::icv::Image>();
577
578 const auto inputFormat = inputImage.GetPixelFormat();
579 const auto outputFormat = GetOutputFormat(inputFormat);
580
581 if (inputFormat == outputFormat)
582 {
583 return input;
584 }
585
586 auto* const inputImageHandle = peak::common::detail::BackendAccessor<peak::icv::Image>::BackendHandle(inputImage);
587 peak_icv_image_handle outputHandle = nullptr;
588
590 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ImageConverter_Convert(
591 m_handle, inputImageHandle, peak::common::detail::CppPixelFormatToCPixelFormat(outputFormat), &outputHandle);
592 });
593
594 return peak::common::Any(peak::common::detail::BackendAccessor<peak::icv::Image>::CreateInstance(outputHandle));
595}
596
598{
600 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ImageConverter_ReleaseBuffers(m_handle);
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.
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.
DebayerConversionPolicy
Defines the conditions under which the Debayer module performs color conversion.
DebayerChannelLayout
Specifies the desired channel layout of the output image.
ProcessingPolicy
Specifies the processing policy.
@ BayerOnly
Convert only Bayer-pattern images.
@ BayerAndMono
Convert both Bayer-pattern and monochrome images.
@ 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.
constexpr peak_common_pixel_format CppPixelFormatToCPixelFormat(PixelFormat pixelFormat) noexcept
void ExecuteAndMapReturnCodes(const CallableType &callableObject)
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