IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
peak_icv_downsampling_module.hpp
Go to the documentation of this file.
1
11#pragma once
12
19#include <peak_icv_c/algorithms/preprocessing/peak_icv_downsampling.h>
20
21namespace peak
22{
23namespace pipeline
24{
25
26namespace detail
27{
28
39{
42
44 BinningSum = 1,
45
47 Decimation = 2,
48};
49
50PEAK_COMMON_NO_DISCARD inline std::string ToString(DownsamplingMode mode)
51{
53 {
54 return "BinningAverage";
55 }
57 {
58 return "BinningSum";
59 }
61 {
62 return "Decimation";
63 }
64 const auto modeStr = std::to_string(static_cast<int>(mode));
65 throw peak::icv::NotSupportedException("The given downsampling mode " + modeStr + " is unknown!");
66}
67
68PEAK_COMMON_NO_DISCARD inline DownsamplingMode ToMode(const std::string& mode)
69{
70 if (mode == "BinningAverage")
71 {
73 }
74 if (mode == "BinningSum")
75 {
77 }
78 if (mode == "Decimation")
79 {
81 }
82 throw peak::icv::NotSupportedException("The given downsampling mode " + mode + " is unknown!");
83}
84
111{
112public:
118 explicit DownsamplingModule();
119
126 explicit DownsamplingModule(DownsamplingMode defaultMode, std::string customName);
127
133 ~DownsamplingModule() override;
134
143
149 DownsamplingModule(DownsamplingModule&& other) noexcept;
150
159
166
174 void SetEnabled(bool enabled) override;
175
183 PEAK_COMMON_NO_DISCARD bool IsEnabled() const override;
184
196 void Serialize(peak::common::serialization::IArchive& archive) const override;
197
213 void Deserialize(const peak::common::serialization::IArchive& archive) override;
214
235 PEAK_COMMON_NO_DISCARD peak::icv::Image Process(const peak::icv::Image& input) const;
236
258 PEAK_COMMON_NO_DISCARD peak::common::Any Process(const peak::common::Any& input) const override;
259
267 PEAK_COMMON_NO_DISCARD const char* GetType() const override;
268
276 void SetCustomType(const std::string& type);
277
286 void ResetToDefault() override;
287
295 void SetX(uint32_t x);
296
304 void SetY(uint32_t y);
305
313 PEAK_COMMON_NO_DISCARD uint32_t GetX() const;
314
322 PEAK_COMMON_NO_DISCARD uint32_t GetY() const;
323
331 PEAK_COMMON_NO_DISCARD peak::common::IntervalU GetRange() const;
332
340 void SetMode(const DownsamplingMode& mode);
341
349 PEAK_COMMON_NO_DISCARD DownsamplingMode GetMode() const;
350
351private:
352 static constexpr auto modeKey{ "Mode" };
353 static constexpr auto xKey = "X";
354 static constexpr auto yKey = "Y";
355 static constexpr auto defaultModeKey = "DefaultMode";
356
357 static constexpr int moduleVersion = 1;
358
360
361 peak_icv_downsampling_handle m_handle{};
362 bool m_enabled{ true };
363
364 std::string m_customType;
365};
366
368 : DownsamplingModule(DownsamplingMode::BinningAverage, {})
369{}
370
371inline DownsamplingModule::DownsamplingModule(DownsamplingMode defaultMode, std::string customName)
372 : m_defaultMode{ defaultMode }
373 , m_customType{ std::move(customName) }
374{
376 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_Create(
377 &m_handle, peak_icv_downsampling_factor{ 1, 1 }, static_cast<peak_icv_downsampling_mode>(m_defaultMode));
378 });
379}
380
382{
383 if (m_handle != nullptr)
384 {
385 std::ignore = PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_Destroy(m_handle);
386 }
387}
388
389inline DownsamplingModule::DownsamplingModule(DownsamplingModule&& other) noexcept
390 : m_defaultMode{ std::exchange(other.m_defaultMode, DownsamplingMode::BinningAverage) }
391 , m_handle{ std::exchange(other.m_handle, nullptr) }
392 , m_enabled{ std::exchange(other.m_enabled, false) }
393{}
394
395inline DownsamplingModule& DownsamplingModule::operator=(DownsamplingModule&& other) noexcept
396{
397 if (this != &other)
398 {
399 if (m_handle != nullptr)
400 {
402 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_Destroy(m_handle);
403 });
404 }
405
406 m_handle = std::exchange(other.m_handle, nullptr);
407 m_enabled = std::exchange(other.m_enabled, false);
408 m_defaultMode = std::exchange(other.m_defaultMode, DownsamplingMode::BinningAverage);
409 }
410
411 return *this;
412}
413
414inline void DownsamplingModule::SetEnabled(const bool enabled)
415{
416 m_enabled = enabled;
417}
418
419inline bool DownsamplingModule::IsEnabled() const
420{
421 return m_enabled;
422}
423
425{
426 archive.SetInt("Version", moduleVersion);
427 archive.SetBool("Enabled", IsEnabled());
428
429 archive.SetInt(xKey, GetX());
430 archive.SetInt(yKey, GetY());
431
432 const auto& mode = GetMode();
433 archive.SetString(modeKey, ToString(mode));
434
435 archive.SetString(defaultModeKey, ToString(m_defaultMode));
436}
437
439{
440 const auto version = archive.GetInt("Version");
442
443 SetEnabled(archive.GetBool("Enabled"));
444
445 SetX(static_cast<uint32_t>(archive.GetInt(xKey)));
446 SetY(static_cast<uint32_t>(archive.GetInt(yKey)));
447
448 SetMode(ToMode(archive.GetString(modeKey)));
449 m_defaultMode = ToMode(archive.GetString(defaultModeKey));
450}
451
453{
454 return Process(peak::common::Any(input)).AnyCast<peak::icv::Image>();
455}
456
458{
459 auto needsProcessing = [&] {
460 bool value{};
462 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_NeedsProcessing(m_handle, &value);
463 });
464 return value;
465 };
466
467 if (!IsEnabled() || !needsProcessing())
468 {
469 return input;
470 }
471
472 const auto& inputImage = input.AnyCast<peak::icv::Image>();
473 auto* const inputImageHandle = peak::common::detail::BackendAccessor<peak::icv::Image>::BackendHandle(inputImage);
474
475 peak_common_size outputImageSize{};
477 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_GetOutputImageSize(m_handle, inputImageHandle, &outputImageSize);
478 });
479
480 peak::icv::Image outputImage{ inputImage.GetPixelFormat(), { outputImageSize.width, outputImageSize.height } };
481 auto* const outputImageHandle = peak::common::detail::BackendAccessor<peak::icv::Image>::BackendHandle(outputImage);
482
484 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_Process(m_handle, inputImageHandle, outputImageHandle);
485 });
486
487 return peak::common::Any{ outputImage };
488}
489
490inline const char* DownsamplingModule::GetType() const
491{
492 if (!m_customType.empty())
493 {
494 return m_customType.c_str();
496 return "Downsampling";
497}
498
500{
501 SetX(1);
502 SetY(1);
503 SetMode(m_defaultMode);
504}
505
506inline void DownsamplingModule::SetX(uint32_t x)
507{
509 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_SetFactor(m_handle, peak_icv_downsampling_factor{ x, GetY() });
510 });
511}
512
513inline void DownsamplingModule::SetY(uint32_t y)
514{
516 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_SetFactor(m_handle, peak_icv_downsampling_factor{ GetX(), y });
517 });
518}
519
520inline uint32_t DownsamplingModule::GetX() const
521{
522 peak_icv_downsampling_factor factor{};
523
525 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_GetFactor(m_handle, &factor);
526 });
527
528 return factor.x;
529}
530
531inline uint32_t DownsamplingModule::GetY() const
532{
533 peak_icv_downsampling_factor factor{};
534
536 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_GetFactor(m_handle, &factor);
537 });
539 return factor.y;
540}
541
543{
544 peak_common_interval_u cRange{};
545
547 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_GetRange(m_handle, &cRange);
548 });
549
550 return { cRange.minimum, cRange.maximum };
551}
553inline void DownsamplingModule::SetMode(const DownsamplingMode& mode)
554{
556 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_SetMode(
557 m_handle, static_cast<peak_icv_downsampling_mode>(mode));
558 });
559}
560
562{
563 peak_icv_downsampling_mode mode{};
564
566 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Downsampling_GetMode(m_handle, &mode);
567 });
568
569 return static_cast<DownsamplingMode>(mode);
570}
572} // namespace detail
573} // namespace pipeline
574} // 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.
Occurs when a requested operation is not implemented or supported (e.g., exporting to an unsupported ...
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
Enables or disables the 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 their default values defined at initialization, i.e. x = 1,...
bool IsEnabled() const override
Gets whether this module is currently enabled.
Interface for a module used within a pipeline.
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)