IDS peak ICV v1.0.0
Loading...
Searching...
No Matches
peak_icv_downsampling_module.hpp
Go to the documentation of this file.
1
11#pragma once
12
20#include <peak_icv_c/algorithms/preprocessing/peak_icv_downsampling.h>
21
22namespace peak
23{
24namespace pipeline
25{
26
27namespace detail
28{
29
40{
43
45 BinningSum = 1,
46
48 Decimation = 2,
49};
50
52{
54 {
55 return "BinningAverage";
56 }
58 {
59 return "BinningSum";
60 }
62 {
63 return "Decimation";
64 }
65 const auto modeStr = std::to_string(static_cast<int>(mode));
66 throw peak::icv::NotSupportedException("The given downsampling mode " + modeStr + " is unknown!");
67}
68
69PEAK_COMMON_NO_DISCARD inline DownsamplingMode ToMode(const std::string& mode)
70{
71 if (mode == "BinningAverage")
72 {
74 }
75 if (mode == "BinningSum")
76 {
78 }
79 if (mode == "Decimation")
80 {
82 }
83 throw peak::icv::NotSupportedException("The given downsampling mode " + mode + " is unknown!");
84}
85
112{
113public:
119 explicit DownsamplingModule();
120
127 explicit DownsamplingModule(DownsamplingMode defaultMode, std::string customName);
128
134 ~DownsamplingModule() override;
135
144
150 DownsamplingModule(DownsamplingModule&& other) noexcept;
151
160
167
175 void SetEnabled(bool enabled) override;
176
184 PEAK_COMMON_NO_DISCARD bool IsEnabled() const override;
185
197 void Serialize(peak::common::serialization::IArchive& archive) const override;
198
213 void Deserialize(const peak::common::serialization::IArchive& archive) override;
214
233
253
261 PEAK_COMMON_NO_DISCARD const char* GetType() const override;
262
270 void SetCustomType(const std::string& type);
271
277 void ResetToDefault() override;
278
286 void SetX(uint32_t x);
287
295 void SetY(uint32_t y);
296
304 PEAK_COMMON_NO_DISCARD uint32_t GetX() const;
305
313 PEAK_COMMON_NO_DISCARD uint32_t GetY() const;
314
323
331 void SetMode(const DownsamplingMode& mode);
332
341
342private:
343 static constexpr auto modeKey{ "Mode" };
344 static constexpr auto xKey = "X";
345 static constexpr auto yKey = "Y";
346 static constexpr auto defaultModeKey = "DefaultMode";
347
348 static constexpr int moduleVersion = 1;
349
351
352 peak_icv_downsampling_handle m_handle{};
353 bool m_enabled{ true };
354
355 std::string m_customType;
356};
357
361
362inline DownsamplingModule::DownsamplingModule(DownsamplingMode defaultMode, std::string customName)
363 : m_defaultMode{ defaultMode }
364 , m_customType{ std::move(customName) }
365{
367 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_Create(
368 &m_handle, peak_icv_downsampling_factor{ 1, 1 }, static_cast<peak_icv_downsampling_mode>(m_defaultMode));
369 });
370}
371
373{
374 if (m_handle != nullptr)
375 {
376 std::ignore = PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_Destroy(m_handle);
377 }
378}
379
381 : m_defaultMode{ std::exchange(other.m_defaultMode, DownsamplingMode::BinningAverage) }
382 , m_handle{ std::exchange(other.m_handle, nullptr) }
383 , m_enabled{ std::exchange(other.m_enabled, false) }
384{}
385
387{
388 if (this != &other)
389 {
390 if (m_handle != nullptr)
391 {
393 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_Destroy(m_handle);
394 });
395 }
396
397 m_handle = std::exchange(other.m_handle, nullptr);
398 m_enabled = std::exchange(other.m_enabled, false);
399 m_defaultMode = std::exchange(other.m_defaultMode, DownsamplingMode::BinningAverage);
400 }
401
402 return *this;
403}
404
405inline void DownsamplingModule::SetEnabled(const bool enabled)
406{
407 m_enabled = enabled;
408}
409
411{
412 return m_enabled;
413}
414
416{
417 archive.SetInt("Version", moduleVersion);
418 archive.SetBool("Enabled", IsEnabled());
419
420 archive.SetInt(xKey, GetX());
421 archive.SetInt(yKey, GetY());
422
423 const auto& mode = GetMode();
424 archive.SetString(modeKey, ToString(mode));
425
426 archive.SetString(defaultModeKey, ToString(m_defaultMode));
427}
428
430{
431 const auto version = archive.GetInt("Version");
433
434 SetEnabled(archive.GetBool("Enabled"));
435
436 SetX(static_cast<uint32_t>(archive.GetInt(xKey)));
437 SetY(static_cast<uint32_t>(archive.GetInt(yKey)));
438
439 SetMode(ToMode(archive.GetString(modeKey)));
440 m_defaultMode = ToMode(archive.GetString(defaultModeKey));
441}
442
444{
445 return Process(peak::common::Any(input)).AnyCast<peak::icv::Image>();
446}
447
449{
450 auto needsProcessing = [&] {
451 bool value{};
453 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_NeedsProcessing(m_handle, &value);
454 });
455 return value;
456 };
457
458 if (!IsEnabled() || !needsProcessing())
459 {
460 return input;
461 }
462
463 const auto& inputImage = input.AnyCast<peak::icv::Image>();
464 auto* const inputImageHandle = peak::common::detail::BackendAccessor<peak::icv::Image>::BackendHandle(inputImage);
465
466 peak_common_size outputImageSize{};
468 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_GetOutputImageSize(m_handle, inputImageHandle, &outputImageSize);
469 });
470
471 peak::icv::Image outputImage{ inputImage.GetPixelFormat(), { outputImageSize.width, outputImageSize.height } };
472 auto* const outputImageHandle = peak::common::detail::BackendAccessor<peak::icv::Image>::BackendHandle(outputImage);
473
475 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_Process(m_handle, inputImageHandle, outputImageHandle);
476 });
477
478 return peak::common::Any{ outputImage };
479}
480
481inline const char* DownsamplingModule::GetType() const
482{
483 if (!m_customType.empty())
484 {
485 return m_customType.c_str();
486 }
487 return "Downsampling";
488}
489
491{
492 SetEnabled(false);
493 SetX(1);
494 SetY(1);
495 SetMode(m_defaultMode);
496}
497
498inline void DownsamplingModule::SetX(uint32_t x)
499{
501 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_SetFactor(m_handle, peak_icv_downsampling_factor{ x, GetY() });
502 });
503}
504
505inline void DownsamplingModule::SetY(uint32_t y)
506{
508 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_SetFactor(m_handle, peak_icv_downsampling_factor{ GetX(), y });
509 });
510}
511
512inline uint32_t DownsamplingModule::GetX() const
513{
514 peak_icv_downsampling_factor factor{};
515
517 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_GetFactor(m_handle, &factor);
518 });
519
520 return factor.x;
521}
522
523inline uint32_t DownsamplingModule::GetY() const
524{
525 peak_icv_downsampling_factor factor{};
526
528 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_GetFactor(m_handle, &factor);
529 });
530
531 return factor.y;
532}
533
535{
536 peak_common_interval_u cRange{};
537
539 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_GetRange(m_handle, &cRange);
540 });
541
542 return { cRange.minimum, cRange.maximum };
543}
544
546{
548 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_SetMode(
549 m_handle, static_cast<peak_icv_downsampling_mode>(mode));
550 });
551}
552
554{
555 peak_icv_downsampling_mode mode{};
556
558 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_GetMode(m_handle, &mode);
559 });
560
561 return static_cast<DownsamplingMode>(mode);
562}
563
564} // namespace detail
565} // namespace pipeline
566} // namespace peak
A type-erased container for any copyable value.
T & AnyCast()
Casts the stored value to the specified type.
Represents a continuous numeric interval with inclusive lower and upper bounds.
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.
Thrown when a requested operation is not implemented or supported.
Downsampling is used to decrease the image size.
uint32_t GetY() const
Returns the current number of rows used for downsampling.
DownsamplingModule(const DownsamplingModule &other)=delete
Copy constructor for class DownsamplingModule is deleted.
const char * GetType() const override
Returns the type of the module for serialization purposes or the custom type, if defined.
DownsamplingModule()
Creates a DownsamplingModule with default values.
void SetCustomType(const std::string &type)
Sets the custom type of the module for serialization purposes.
DownsamplingMode GetMode() const
Returns the current DownsamplingMode.
void SetMode(const DownsamplingMode &mode)
Sets the DownsamplingMode.
void Serialize(peak::common::serialization::IArchive &archive) const override
Serializes the object's internal state into the provided archive.
uint32_t GetX() const
Returns the current number of columns used for downsampling.
peak::icv::Image Process(const peak::icv::Image &input) const
Applies the downsampling factors and mode to the input image and returns a downsampled copy based on ...
void SetEnabled(bool enabled) override
Sets the enabled state of this module.
void SetY(uint32_t y)
Sets the number of rows to downsample.
void SetX(uint32_t x)
Sets the number of columns to downsample.
void Deserialize(const peak::common::serialization::IArchive &archive) override
Restores the object's state from the provided archive.
DownsamplingModule & operator=(const DownsamplingModule &other)=delete
Copy assignment for class DownsamplingModule is deleted.
peak::common::IntervalU GetRange() const
Returns the valid range for downsampling factors x and y.
void ResetToDefault() override
Resets all settings to default values.
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.
DownsamplingMode
Mode parameter for the downsampling algorithm.
@ BinningAverage
The averaged pixel values of neighboring rows and/or columns are computed during binning.
@ BinningSum
The pixel values of neighboring rows and/or columns are summed during binning.
@ Decimation
The additional pixel values of neighboring rows and/or columns are skipped during decimation.
void ExecuteAndMapReturnCodes(const CallableType &callableObject)
std::string ToString(const DebayerConversionPolicy &policy)
void ValidateVersion(int64_t version, const std::string &moduleName)
DownsamplingMode ToMode(const std::string &mode)