IDS peak ICV v1.3.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{
45 if (policy == DebayerConversionPolicy::BayerAndMono)
46 {
47 return "BayerAndMono";
48 }
49 if (policy == DebayerConversionPolicy::BayerOnly)
50 {
51 return "BayerOnly";
52 }
53 if (policy == DebayerConversionPolicy::Bypass)
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 {
65 return DebayerConversionPolicy::BayerAndMono;
66 }
67 if (policy == "BayerOnly")
68 {
69 return DebayerConversionPolicy::BayerOnly;
70 }
71 if (policy == "Bypass")
72 {
73 return DebayerConversionPolicy::Bypass;
74 }
75 throw peak::icv::NotSupportedException("The given debayer conversion policy " + policy + " is unknown!");
76}
77
100{
101public:
108
114 ~DebayerModule() override;
115
123 DebayerModule(const DebayerModule& other) = delete;
124
130 DebayerModule(DebayerModule&& other) noexcept;
131
139 DebayerModule& operator=(const DebayerModule& other) = delete;
140
146 DebayerModule& operator=(DebayerModule&& other) noexcept;
147
159 void Serialize(peak::common::serialization::IArchive& archive) const override;
160
176 void Deserialize(const peak::common::serialization::IArchive& archive) override;
177
185 void SetEnabled(bool enabled) override;
186
194 PEAK_COMMON_NO_DISCARD bool IsEnabled() const override;
195
203 PEAK_COMMON_NO_DISCARD const char* GetType() const override;
204
212 void ResetToDefault() override;
213
224
232 PEAK_COMMON_NO_DISCARD DebayerChannelLayout GetChannelLayout() const;
233
245
253 PEAK_COMMON_NO_DISCARD DebayerConversionPolicy GetConversionPolicy() const;
254
268 void SetTargetBitDepth(size_t bitDepth)
269 {
270 m_targetBitDepth = bitDepth;
271 }
272
284
304 PEAK_COMMON_NO_DISCARD peak::icv::Image Process(const peak::icv::Image& input) const;
305
327 PEAK_COMMON_NO_DISCARD peak::common::Any Process(const peak::common::Any& input) const override;
328
342 void ReleaseBuffers() const;
343
344private:
345 PEAK_COMMON_NO_DISCARD peak::common::PixelFormat GetOutputFormat(peak::common::PixelFormat inputFormat) const;
346 PEAK_COMMON_NO_DISCARD size_t GetOutputBitDepth(size_t inputBitDepth) const;
347
348 static void ValidateChannelLayout(DebayerChannelLayout layout);
349 static void ValidateConversionPolicy(DebayerConversionPolicy policy);
350
351 static constexpr int moduleVersion{ 1 };
352 static constexpr auto conversionPolicyKey{ "ConversionPolicy" };
353 static constexpr auto channelLayoutKey{ "ChannelLayout" };
354
355 peak_icv_image_converter_handle m_handle{};
356 DebayerChannelLayout m_channelLayout{ DebayerChannelLayout::RGB };
357 DebayerConversionPolicy m_conversionPolicy{ DebayerConversionPolicy::BayerOnly };
358
359 size_t m_targetBitDepth{ 8 };
360 ProcessingPolicy m_processingPolicy{ ProcessingPolicy::Fast };
361
362 bool m_enabled{ true };
363};
364
366{
368 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ImageConverter_Create(&m_handle);
369 });
370}
371
373{
374 if (m_handle != nullptr)
375 {
376 std::ignore = PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ImageConverter_Destroy(m_handle);
377 }
378}
379
381{
382 m_channelLayout = other.m_channelLayout;
383 m_conversionPolicy = other.m_conversionPolicy;
384 m_enabled = other.m_enabled;
385
386 std::swap(m_handle, other.m_handle);
387}
388
390{
391 if (this != &other)
392 {
393 m_channelLayout = other.m_channelLayout;
394 m_conversionPolicy = other.m_conversionPolicy;
395 m_enabled = other.m_enabled;
396 std::swap(m_handle, other.m_handle);
397 }
398
399 return *this;
400}
401
403{
404 archive.SetBool("Enabled", IsEnabled());
405 archive.SetInt("Version", moduleVersion);
406
407 archive.SetString(channelLayoutKey, ToString(m_channelLayout));
408 archive.SetString(conversionPolicyKey, ToString(m_conversionPolicy));
409}
410
412{
413 const auto version = archive.GetInt("Version");
415
416 SetEnabled(archive.GetBool("Enabled"));
417 m_channelLayout = ToDebayerChannelLayout(archive.GetString(channelLayoutKey));
418 m_conversionPolicy = ToDebayerConversionPolicy(archive.GetString(conversionPolicyKey));
419}
420
422{
423 m_channelLayout = DebayerChannelLayout::RGB;
424 m_conversionPolicy = DebayerConversionPolicy::BayerOnly;
425}
426
427inline void DebayerModule::SetEnabled(bool enabled)
428{
429 m_enabled = enabled;
430}
431
432inline bool DebayerModule::IsEnabled() const
433{
434 return m_enabled;
435}
436
437inline const char* DebayerModule::GetType() const
438{
439 return "Debayer";
440}
441
443{
444 ValidateChannelLayout(layout);
445 m_channelLayout = layout;
446}
447
449{
450 return m_channelLayout;
451}
452
454{
455 ValidateConversionPolicy(policy);
456 m_conversionPolicy = policy;
457}
458
460{
461 return m_conversionPolicy;
462}
463
464inline size_t DebayerModule::GetOutputBitDepth(const size_t inputBitDepth) const
465{
466 if (m_processingPolicy == ProcessingPolicy::Fast)
467 {
468 return (std::min)(inputBitDepth, m_targetBitDepth);
469 }
470 if (m_processingPolicy == ProcessingPolicy::Enhanced)
471 {
472 return (std::max)(inputBitDepth, m_targetBitDepth);
473 }
474 return inputBitDepth;
475}
476
477inline peak::common::PixelFormat DebayerModule::GetOutputFormat(peak::common::PixelFormat inputFormat) const
478{
479 if (m_conversionPolicy == DebayerConversionPolicy::Bypass)
480 {
481 return inputFormat;
482 }
483
484 const peak::common::PixelFormatInfo info(inputFormat);
485
486 if (!info.IsSingleChannel())
487 {
488 return inputFormat;
489 }
490
491 const auto isBayer = info.HasChannel(peak::common::Channel::Bayer);
492 const auto isMono = info.HasIntensityChannel();
493
494 if (!isBayer && (!isMono || m_conversionPolicy != DebayerConversionPolicy::BayerAndMono))
495 {
496 return inputFormat;
497 }
498
499 const auto inputBitDepth = info.GetStorageBitsPerChannel();
500 const auto outputBitDepth = GetOutputBitDepth(inputBitDepth);
501
502 if (outputBitDepth <= 8)
503 {
504 switch (m_channelLayout)
505 {
506 case DebayerChannelLayout::RGB:
508 case DebayerChannelLayout::BGR:
510 case DebayerChannelLayout::RGBA:
512 case DebayerChannelLayout::BGRA:
514 }
515 }
516 if (outputBitDepth <= 10)
517 {
518 switch (m_channelLayout)
519 {
520 case DebayerChannelLayout::RGB:
522 case DebayerChannelLayout::BGR:
524 case DebayerChannelLayout::RGBA:
526 case DebayerChannelLayout::BGRA:
528 }
529 }
530
531 switch (m_channelLayout)
532 {
533 case DebayerChannelLayout::RGB:
535 case DebayerChannelLayout::BGR:
537 case DebayerChannelLayout::RGBA:
539 case DebayerChannelLayout::BGRA:
541 }
542
543 std::ostringstream oss;
544 oss << "0x" << std::uppercase << std::hex << static_cast<int>(inputFormat);
545 throw peak::icv::NotSupportedException("The given input pixel format " + oss.str() + " is unknown!");
546}
547
548inline void DebayerModule::ValidateChannelLayout(DebayerChannelLayout layout)
549{
550 if (layout != DebayerChannelLayout::RGB && layout != DebayerChannelLayout::BGR && layout != DebayerChannelLayout::RGBA
551 && layout != DebayerChannelLayout::BGRA)
552 {
553 throw peak::icv::NotSupportedException("The given channel layout " + ToString(layout) + " is invalid!");
554 }
555}
556
557inline void DebayerModule::ValidateConversionPolicy(DebayerConversionPolicy policy)
558{
559 if (policy != DebayerConversionPolicy::BayerOnly && policy != DebayerConversionPolicy::BayerAndMono
560 && policy != DebayerConversionPolicy::Bypass)
561 {
562 throw peak::icv::NotSupportedException("The given conversion policy " + ToString(policy) + " is invalid!");
563 }
564}
565
567{
568 m_processingPolicy = policy;
569}
570
572{
573 return Process(peak::common::Any(input)).AnyCast<peak::icv::Image>();
574}
575
577{
578 if (!IsEnabled())
579 {
580 return input;
581 }
582
583 const auto& inputImage = input.AnyCast<peak::icv::Image>();
584
585 const auto inputFormat = inputImage.GetPixelFormat();
586 const auto outputFormat = GetOutputFormat(inputFormat);
587
588 if (inputFormat == outputFormat)
589 {
590 return input;
591 }
592
593 auto* const inputImageHandle = peak::common::detail::BackendAccessor<peak::icv::Image>::BackendHandle(inputImage);
594 peak_icv_image_handle outputHandle = nullptr;
595
597 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ImageConverter_Convert(
598 m_handle, inputImageHandle, peak::common::detail::CppPixelFormatToCPixelFormat(outputFormat), &outputHandle);
599 });
600
601 return peak::common::Any(peak::common::detail::BackendAccessor<peak::icv::Image>::CreateInstance(outputHandle));
602}
603
605{
607 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_ImageConverter_ReleaseBuffers(m_handle);
608 });
609}
610} // namespace detail
611} // namespace pipeline
612} // namespace peak
IDSImaging.Peak.ICV.Pipeline.Types.DebayerChannelLayout DebayerChannelLayout
IDSImaging.Peak.ICV.Pipeline.Types.ProcessingPolicy ProcessingPolicy
IDSImaging.Peak.ICV.Pipeline.Types.DebayerConversionPolicy DebayerConversionPolicy
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.
Occurs when a requested operation is not implemented or supported (e.g., exporting to an unsupported ...
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 returns the input image unchanged.
void SetEnabled(bool enabled) override
Enables or disables the module.
DebayerModule & operator=(const DebayerModule &other)=delete
Copy assignment for class DebayerModule is deleted.
void SetConversionPolicy(DebayerConversionPolicy policy)
Set the debayer conversion policy.
void SetChannelLayout(DebayerChannelLayout layout)
Set the output channel layout for debayered images.
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 all module settings to its default settings.
void SetTargetBitDepth(size_t bitDepth)
Set the target bit depth for processed images.
DebayerConversionPolicy GetConversionPolicy() const
Retrieves the current conversion policy.
bool IsEnabled() const override
Gets whether this 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
Specifies the conditions under which the Debayer module performs color conversion.
@ BayerOnly
Convert only Bayer-pattern images.
@ BayerAndMono
Convert both Bayer-pattern and monochrome images.
DebayerChannelLayout
Specifies the channel layout used by the debayer module in image processing pipelines.
ProcessingPolicy
Specifies the processing policy.
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