IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
peak_icv_transformation_module.hpp
Go to the documentation of this file.
1
11#pragma once
12
17#include <peak_icv_c/algorithms/preprocessing/peak_icv_image_transformation.h>
18
19namespace peak
20{
21namespace pipeline
22{
23
24namespace features
25{
26class MirrorFeature;
27class RotationFeature;
28} // namespace features
29
30namespace detail
31{
32
41{
42public:
51
57 ~TransformationModule() override = default;
58
67
74
83
90
102 void Serialize(peak::common::serialization::IArchive& archive) const override;
103
120 void Deserialize(const peak::common::serialization::IArchive& archive) override;
121
129 void SetEnabled(bool enabled) override;
130
138 PEAK_COMMON_NO_DISCARD bool IsEnabled() const override;
139
147 PEAK_COMMON_NO_DISCARD const char* GetType() const override;
148
158 void ResetToDefault() override;
159
167 void SetMirrorLeftRightEnabled(bool enabled);
168
176 void SetMirrorUpDownEnabled(bool enabled);
177
185 PEAK_COMMON_NO_DISCARD bool IsMirrorLeftRightEnabled() const;
186
194 PEAK_COMMON_NO_DISCARD bool IsMirrorUpDownEnabled() const;
195
203 void SetRotationAngle(Rotation angle);
204
212 PEAK_COMMON_NO_DISCARD Rotation GetRotationAngle() const;
213
232 PEAK_COMMON_NO_DISCARD peak::icv::Image Process(const peak::icv::Image& input) const;
233
253 PEAK_COMMON_NO_DISCARD peak::common::Any Process(const peak::common::Any& input) const override;
254
255private:
256#ifndef DOXYGEN_SHOULD_SKIP_THIS
257 friend class features::MirrorFeature;
258 friend class features::RotationFeature;
259#endif
260
261 void SetMirrorEnabled(bool enabled);
262 PEAK_COMMON_NO_DISCARD bool IsMirrorEnabled() const;
263
264 void SetRotationEnabled(bool enabled);
265 PEAK_COMMON_NO_DISCARD bool IsRotationEnabled() const;
266
267 static constexpr int moduleVersion{ 1 };
268 static constexpr auto mirrorKey{ "Mirror" };
269 static constexpr auto rotationKey{ "Rotation" };
270 static constexpr auto mirrorLeftRightKey{ "LeftRight" };
271 static constexpr auto mirrorUpDownKey{ "UpDown" };
272 static constexpr auto rotationAngleKey{ "Angle" };
273 static constexpr Rotation defaultRotation{ Rotation::None };
274
275 bool m_leftRight{ false };
276 bool m_upDown{ false };
277 Rotation m_angle{ defaultRotation };
278
279 bool m_mirrorEnabled{ true };
280 bool m_rotationEnabled{ true };
281};
282
283inline void TransformationModule::SetEnabled(bool enabled)
284{
285 m_mirrorEnabled = enabled;
286 m_rotationEnabled = enabled;
287}
288
289inline bool TransformationModule::IsEnabled() const
290{
291 return m_mirrorEnabled || m_rotationEnabled;
292}
293
294inline const char* TransformationModule::GetType() const
295{
296 return "TransformationModule";
297}
298
299inline TransformationModule::TransformationModule(TransformationModule&& other) noexcept
300{
301 m_leftRight = other.m_leftRight;
302 m_upDown = other.m_upDown;
303 m_angle = other.m_angle;
304 m_mirrorEnabled = other.m_mirrorEnabled;
305 m_rotationEnabled = other.m_rotationEnabled;
306}
307
308inline TransformationModule& TransformationModule::operator=(TransformationModule&& other) noexcept
309{
310 if (this != &other)
311 {
312 m_leftRight = other.m_leftRight;
313 m_upDown = other.m_upDown;
314 m_angle = other.m_angle;
315 m_mirrorEnabled = other.m_mirrorEnabled;
316 m_rotationEnabled = other.m_rotationEnabled;
317 }
318
319 return *this;
320}
321
323{
324 archive.SetInt("Version", moduleVersion);
325
326 const auto mirrorArchive = archive.CreateArchive();
327 const auto rotationArchive = archive.CreateArchive();
328
329 mirrorArchive->SetBool("Enabled", m_mirrorEnabled);
330 rotationArchive->SetBool("Enabled", m_rotationEnabled);
331
332 mirrorArchive->SetBool(mirrorLeftRightKey, m_leftRight);
333 mirrorArchive->SetBool(mirrorUpDownKey, m_upDown);
334
335 rotationArchive->SetInt(rotationAngleKey, static_cast<int64_t>(m_angle));
336
337 archive.SetArchive(mirrorKey, mirrorArchive);
338 archive.SetArchive(rotationKey, rotationArchive);
339}
340
342{
343 const auto version = archive.GetInt("Version");
345
346 const auto mirrorArchive = archive.GetArchive(mirrorKey);
347 const auto rotationArchive = archive.GetArchive(rotationKey);
348 const auto rotation = rotationArchive->GetInt(rotationAngleKey);
349 if (rotation != 0 && rotation != 90 && rotation != 180 && rotation != 270)
350 {
351 throw peak::icv::NotSupportedException("The given rotation angle " + std::to_string(rotation) + " is invalid!");
352 }
353
354 m_mirrorEnabled = mirrorArchive->GetBool("Enabled");
355 m_rotationEnabled = rotationArchive->GetBool("Enabled");
356
357 m_leftRight = mirrorArchive->GetBool(mirrorLeftRightKey);
358 m_upDown = mirrorArchive->GetBool(mirrorUpDownKey);
359
360 m_angle = static_cast<Rotation>(rotation);
361}
362
364{
365 m_leftRight = false;
366 m_upDown = false;
367 m_angle = defaultRotation;
368}
369
371{
372 m_leftRight = enabled;
373}
374
375inline void TransformationModule::SetMirrorUpDownEnabled(bool enabled)
376{
377 m_upDown = enabled;
378}
379
381{
382 return m_leftRight;
383}
384
386{
387 return m_upDown;
388}
389
391{
392 if (static_cast<uint32_t>(angle) % 90 != 0 || static_cast<uint32_t>(angle) > 270)
393 {
394 throw peak::icv::NotSupportedException("The given rotation angle is not supported.");
395 }
396 m_angle = angle;
398
400{
401 return m_angle;
402}
405{
406 return Process(peak::common::Any(input)).AnyCast<peak::icv::Image>();
407}
410{
411 const auto mirrorNeedsProcessing = m_mirrorEnabled && (m_leftRight || m_upDown);
412 const auto rotationNeedsProcessing = m_rotationEnabled && (m_angle != Rotation::None);
414 if (!mirrorNeedsProcessing && !rotationNeedsProcessing)
415 {
416 return input;
417 }
418
419 const auto& inputImage = input.AnyCast<peak::icv::Image>();
420
421 auto* const inputImageHandle = peak::common::detail::BackendAccessor<peak::icv::Image>::BackendHandle(inputImage);
423 const peak_icv_preprocessing_transformation_parameters parameters{ m_mirrorEnabled && m_leftRight, m_mirrorEnabled && m_upDown,
424 m_rotationEnabled ? static_cast<peak_icv_preprocessing_transformation_rotation_angle>(m_angle) :
425 PEAK_ICV_PREPROCESSING_TRANSFORMATION_ROTATION_NONE };
426
427 peak_common_pixel_format outputPixelFormat;
428
430 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Transformation_GetOutputPixelFormat(
431 peak::common::detail::CppPixelFormatToCPixelFormat(inputImage.GetPixelFormat()), parameters, &outputPixelFormat);
432 });
433
434 const auto transpose = m_rotationEnabled && (m_angle == Rotation::Degree90Clockwise || m_angle == Rotation::Degree90Counterclockwise);
435 const auto outputSize = transpose ? inputImage.GetSize().Transposed() : inputImage.GetSize();
436 peak::icv::Image outputImage{ peak::common::detail::CPixelFormatToCppPixelFormat(outputPixelFormat), outputSize };
437 auto* const outputImageHandle = peak::common::detail::BackendAccessor<peak::icv::Image>::BackendHandle(outputImage);
438
440 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Transformation_Process(inputImageHandle, parameters, outputImageHandle);
441 });
442
443 return peak::common::Any(outputImage);
444}
445
446inline void TransformationModule::SetMirrorEnabled(bool enabled)
447{
448 m_mirrorEnabled = enabled;
449}
450
451inline bool TransformationModule::IsMirrorEnabled() const
452{
453 return m_mirrorEnabled;
454}
456inline void TransformationModule::SetRotationEnabled(bool enabled)
457{
458 m_rotationEnabled = enabled;
459}
460
461inline bool TransformationModule::IsRotationEnabled() const
462{
463 return m_rotationEnabled;
464}
465
466} // namespace detail
467} // namespace pipeline
468} // namespace peak
A type-erased container for any copyable value.
T & AnyCast()
Casts the stored value to the specified type.
Interface for a hierarchical key-value archive system.
virtual std::shared_ptr< IArchive > GetArchive(const std::string &key) const =0
Retrieves a nested archive by key.
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::shared_ptr< IArchive > CreateArchive() const =0
Creates a new, empty archive instance.
virtual void SetArchive(const std::string &key, const std::shared_ptr< IArchive > &value)=0
Stores a nested archive object.
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 ...
Transformation is an image pipeline module that provides mirror and rotation functionality.
void SetMirrorLeftRightEnabled(bool enabled)
Sets whether the image should be mirrored left-right.
const char * GetType() const override
Returns the type of the module for serialization purposes.
bool IsMirrorLeftRightEnabled() const
Gets the current left-right mirroring setting.
bool IsMirrorUpDownEnabled() const
Gets the current up-down mirroring setting.
TransformationModule()=default
Creates a TransformationModule module with default values for mirror and rotation.
TransformationModule(const TransformationModule &other)=delete
Copy constructor for class TransformationModule 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
Processes the input image and returns a mirrored and/or rotated copy based on current settings.
void SetEnabled(bool enabled) override
Enables or disables the module.
void SetRotationAngle(Rotation angle)
Sets the desired rotation angle.
Rotation GetRotationAngle() const
Gets the current rotation angle.
void Deserialize(const peak::common::serialization::IArchive &archive) override
Restores the object's state from the provided archive.
void ResetToDefault() override
Resets all settings to the default values for mirror and rotation.
TransformationModule & operator=(const TransformationModule &other)=delete
Copy assignment for class TransformationModule is deleted.
void SetMirrorUpDownEnabled(bool enabled)
Sets whether the image should be mirrored up-down.
bool IsEnabled() const override
Gets whether this module is currently enabled.
Mirror provides functionality to flip the image left-right or up-down.
Rotation provides fixed-angle rotation functionality.
Interface for a module used within a pipeline.
Rotation
Angle parameter for the rotation algorithm.
@ None
No rotation applied to the image.
@ Degree90Counterclockwise
Rotate the image 90 degrees counterclockwise.
@ Degree90Clockwise
Rotate the image 90 degrees clockwise.
constexpr peak_common_pixel_format CppPixelFormatToCPixelFormat(PixelFormat pixelFormat) noexcept
constexpr PixelFormat CPixelFormatToCppPixelFormat(peak_common_pixel_format pixelFormat) noexcept
void ExecuteAndMapReturnCodes(const CallableType &callableObject)
void ValidateVersion(int64_t version, const std::string &moduleName)