IDS peak ICV v1.0.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
118 void Deserialize(const peak::common::serialization::IArchive& archive) override;
119
127 void SetEnabled(bool enabled) override;
128
136 PEAK_COMMON_NO_DISCARD bool IsEnabled() const override;
137
145 PEAK_COMMON_NO_DISCARD const char* GetType() const override;
146
154 void ResetToDefault() override;
155
163 void SetMirrorLeftRightEnabled(bool enabled);
164
172 void SetMirrorUpDownEnabled(bool enabled);
173
182
191
199 void SetRotationAngle(Rotation angle);
200
209
232
256
257private:
258#ifndef DOXYGEN_SHOULD_SKIP_THIS
259 friend class features::MirrorFeature;
260 friend class features::RotationFeature;
261#endif
262
263 void SetMirrorEnabled(bool enabled);
264 PEAK_COMMON_NO_DISCARD bool IsMirrorEnabled() const;
265
266 void SetRotationEnabled(bool enabled);
267 PEAK_COMMON_NO_DISCARD bool IsRotationEnabled() const;
268
269 static constexpr int moduleVersion{ 1 };
270 static constexpr auto mirrorKey{ "Mirror" };
271 static constexpr auto rotationKey{ "Rotation" };
272 static constexpr auto mirrorLeftRightKey{ "LeftRight" };
273 static constexpr auto mirrorUpDownKey{ "UpDown" };
274 static constexpr auto rotationAngleKey{ "Angle" };
275 static constexpr Rotation defaultRotation{ Rotation::None };
276
277 bool m_leftRight{ false };
278 bool m_upDown{ false };
279 Rotation m_angle{ defaultRotation };
280
281 bool m_mirrorEnabled{ true };
282 bool m_rotationEnabled{ true };
283};
284
285inline void TransformationModule::SetEnabled(bool enabled)
286{
287 m_mirrorEnabled = enabled;
288 m_rotationEnabled = enabled;
289}
290
292{
293 return m_mirrorEnabled || m_rotationEnabled;
294}
295
296inline const char* TransformationModule::GetType() const
297{
298 return "TransformationModule";
299}
300
302{
303 m_leftRight = other.m_leftRight;
304 m_upDown = other.m_upDown;
305 m_angle = other.m_angle;
306 m_mirrorEnabled = other.m_mirrorEnabled;
307 m_rotationEnabled = other.m_rotationEnabled;
308}
309
311{
312 if (this != &other)
313 {
314 m_leftRight = other.m_leftRight;
315 m_upDown = other.m_upDown;
316 m_angle = other.m_angle;
317 m_mirrorEnabled = other.m_mirrorEnabled;
318 m_rotationEnabled = other.m_rotationEnabled;
319 }
320
321 return *this;
322}
323
325{
326 archive.SetInt("Version", moduleVersion);
327
328 const auto mirrorArchive = archive.CreateArchive();
329 const auto rotationArchive = archive.CreateArchive();
330
331 mirrorArchive->SetBool("Enabled", m_mirrorEnabled);
332 rotationArchive->SetBool("Enabled", m_rotationEnabled);
333
334 mirrorArchive->SetBool(mirrorLeftRightKey, m_leftRight);
335 mirrorArchive->SetBool(mirrorUpDownKey, m_upDown);
336
337 rotationArchive->SetInt(rotationAngleKey, static_cast<int64_t>(m_angle));
338
339 archive.SetArchive(mirrorKey, mirrorArchive);
340 archive.SetArchive(rotationKey, rotationArchive);
341}
342
344{
345 const auto version = archive.GetInt("Version");
347
348 const auto mirrorArchive = archive.GetArchive(mirrorKey);
349 const auto rotationArchive = archive.GetArchive(rotationKey);
350
351 m_mirrorEnabled = mirrorArchive->GetBool("Enabled");
352 m_rotationEnabled = rotationArchive->GetBool("Enabled");
353
354 m_leftRight = mirrorArchive->GetBool(mirrorLeftRightKey);
355 m_upDown = mirrorArchive->GetBool(mirrorUpDownKey);
356
357 const auto rotation = rotationArchive->GetInt(rotationAngleKey);
358 if (rotation != 0 && rotation != 90 && rotation != 180 && rotation != 270)
359 {
360 throw peak::common::NotSupportedException("The given rotation angle " + std::to_string(rotation) + " is invalid!");
361 }
362 m_angle = static_cast<Rotation>(rotation);
363}
364
366{
367 m_leftRight = false;
368 m_upDown = false;
369 m_angle = defaultRotation;
370}
371
373{
374 m_leftRight = enabled;
375}
376
378{
379 m_upDown = enabled;
380}
381
383{
384 return m_leftRight;
385}
386
388{
389 return m_upDown;
390}
391
393{
394 if (static_cast<uint32_t>(angle) % 90 != 0 || static_cast<uint32_t>(angle) > 270)
395 {
396 throw peak::icv::NotSupportedException("The given rotation angle is not supported.");
397 }
398 m_angle = angle;
399}
400
402{
403 return m_angle;
404}
405
407{
408 return Process(peak::common::Any(input)).AnyCast<peak::icv::Image>();
409}
410
412{
413 const auto mirrorNeedsProcessing = m_mirrorEnabled && (m_leftRight || m_upDown);
414 const auto rotationNeedsProcessing = m_rotationEnabled && (m_angle != Rotation::None);
415
416 if (!mirrorNeedsProcessing && !rotationNeedsProcessing)
417 {
418 return input;
419 }
420
421 const auto& inputImage = input.AnyCast<peak::icv::Image>();
422
423 auto* const inputImageHandle = peak::common::detail::BackendAccessor<peak::icv::Image>::BackendHandle(inputImage);
424
425 const peak_icv_preprocessing_transformation_parameters parameters{ m_mirrorEnabled && m_leftRight, m_mirrorEnabled && m_upDown,
426 m_rotationEnabled ? static_cast<peak_icv_preprocessing_transformation_rotation_angle>(m_angle) :
427 PEAK_ICV_PREPROCESSING_TRANSFORMATION_ROTATION_NONE };
428
429 peak_common_pixel_format outputPixelFormat;
430
432 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Transformation_GetOutputPixelFormat(
433 static_cast<peak_common_pixel_format>(inputImage.GetPixelFormat()), parameters, &outputPixelFormat);
434 });
435
436 const auto transpose = m_rotationEnabled && (m_angle == Rotation::Degree90Clockwise || m_angle == Rotation::Degree90Counterclockwise);
437 const auto outputSize = transpose ? inputImage.GetSize().Transposed() : inputImage.GetSize();
438 peak::icv::Image outputImage{ static_cast<peak::common::PixelFormat>(outputPixelFormat), outputSize };
439 auto* const outputImageHandle = peak::common::detail::BackendAccessor<peak::icv::Image>::BackendHandle(outputImage);
440
442 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Transformation_Process(inputImageHandle, parameters, outputImageHandle);
443 });
444
445 return peak::common::Any(outputImage);
446}
447
448inline void TransformationModule::SetMirrorEnabled(bool enabled)
449{
450 m_mirrorEnabled = enabled;
451}
452
453inline bool TransformationModule::IsMirrorEnabled() const
454{
455 return m_mirrorEnabled;
456}
457
458inline void TransformationModule::SetRotationEnabled(bool enabled)
459{
460 m_rotationEnabled = enabled;
461}
462
463inline bool TransformationModule::IsRotationEnabled() const
464{
465 return m_rotationEnabled;
466}
467
468} // namespace detail
469} // namespace pipeline
470} // namespace peak
A type-erased container for any copyable value.
T & AnyCast()
Casts the stored value to the specified type.
Thrown when an unsupported operation or feature is used.
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.
Thrown when a requested operation is not implemented or supported.
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
Sets the enabled state of this module.
void SetRotationAngle(Rotation angle)
Sets the desired rotation angle.
Rotation GetRotationAngle() const
Retrieves 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
Checks if the 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.
#define PEAK_COMMON_NO_DISCARD
Compatability macro for the nodiscard attribute.
PixelFormat
Enum listing all supported pixel formats and their internal IDs.
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.
void ExecuteAndMapReturnCodes(const CallableType &callableObject)
void ValidateVersion(int64_t version, const std::string &moduleName)