IDS peak ICV v1.1.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
156 void ResetToDefault() override;
157
165 void SetMirrorLeftRightEnabled(bool enabled);
166
174 void SetMirrorUpDownEnabled(bool enabled);
175
183 PEAK_COMMON_NO_DISCARD bool IsMirrorLeftRightEnabled() const;
184
192 PEAK_COMMON_NO_DISCARD bool IsMirrorUpDownEnabled() const;
193
201 void SetRotationAngle(Rotation angle);
202
210 PEAK_COMMON_NO_DISCARD Rotation GetRotationAngle() const;
211
230 PEAK_COMMON_NO_DISCARD peak::icv::Image Process(const peak::icv::Image& input) const;
231
251 PEAK_COMMON_NO_DISCARD peak::common::Any Process(const peak::common::Any& input) const override;
252
253private:
254#ifndef DOXYGEN_SHOULD_SKIP_THIS
255 friend class features::MirrorFeature;
256 friend class features::RotationFeature;
257#endif
258
259 void SetMirrorEnabled(bool enabled);
260 PEAK_COMMON_NO_DISCARD bool IsMirrorEnabled() const;
261
262 void SetRotationEnabled(bool enabled);
263 PEAK_COMMON_NO_DISCARD bool IsRotationEnabled() const;
264
265 static constexpr int moduleVersion{ 1 };
266 static constexpr auto mirrorKey{ "Mirror" };
267 static constexpr auto rotationKey{ "Rotation" };
268 static constexpr auto mirrorLeftRightKey{ "LeftRight" };
269 static constexpr auto mirrorUpDownKey{ "UpDown" };
270 static constexpr auto rotationAngleKey{ "Angle" };
271 static constexpr Rotation defaultRotation{ Rotation::None };
272
273 bool m_leftRight{ false };
274 bool m_upDown{ false };
275 Rotation m_angle{ defaultRotation };
276
277 bool m_mirrorEnabled{ true };
278 bool m_rotationEnabled{ true };
279};
280
281inline void TransformationModule::SetEnabled(bool enabled)
282{
283 m_mirrorEnabled = enabled;
284 m_rotationEnabled = enabled;
285}
286
287inline bool TransformationModule::IsEnabled() const
288{
289 return m_mirrorEnabled || m_rotationEnabled;
290}
291
292inline const char* TransformationModule::GetType() const
293{
294 return "TransformationModule";
295}
296
297inline TransformationModule::TransformationModule(TransformationModule&& other) noexcept
298{
299 m_leftRight = other.m_leftRight;
300 m_upDown = other.m_upDown;
301 m_angle = other.m_angle;
302 m_mirrorEnabled = other.m_mirrorEnabled;
303 m_rotationEnabled = other.m_rotationEnabled;
304}
305
306inline TransformationModule& TransformationModule::operator=(TransformationModule&& other) noexcept
307{
308 if (this != &other)
309 {
310 m_leftRight = other.m_leftRight;
311 m_upDown = other.m_upDown;
312 m_angle = other.m_angle;
313 m_mirrorEnabled = other.m_mirrorEnabled;
314 m_rotationEnabled = other.m_rotationEnabled;
315 }
316
317 return *this;
318}
319
321{
322 archive.SetInt("Version", moduleVersion);
323
324 const auto mirrorArchive = archive.CreateArchive();
325 const auto rotationArchive = archive.CreateArchive();
326
327 mirrorArchive->SetBool("Enabled", m_mirrorEnabled);
328 rotationArchive->SetBool("Enabled", m_rotationEnabled);
329
330 mirrorArchive->SetBool(mirrorLeftRightKey, m_leftRight);
331 mirrorArchive->SetBool(mirrorUpDownKey, m_upDown);
332
333 rotationArchive->SetInt(rotationAngleKey, static_cast<int64_t>(m_angle));
334
335 archive.SetArchive(mirrorKey, mirrorArchive);
336 archive.SetArchive(rotationKey, rotationArchive);
337}
338
340{
341 const auto version = archive.GetInt("Version");
343
344 const auto mirrorArchive = archive.GetArchive(mirrorKey);
345 const auto rotationArchive = archive.GetArchive(rotationKey);
346 const auto rotation = rotationArchive->GetInt(rotationAngleKey);
347 if (rotation != 0 && rotation != 90 && rotation != 180 && rotation != 270)
348 {
349 throw peak::common::NotSupportedException("The given rotation angle " + std::to_string(rotation) + " is invalid!");
350 }
351
352 m_mirrorEnabled = mirrorArchive->GetBool("Enabled");
353 m_rotationEnabled = rotationArchive->GetBool("Enabled");
354
355 m_leftRight = mirrorArchive->GetBool(mirrorLeftRightKey);
356 m_upDown = mirrorArchive->GetBool(mirrorUpDownKey);
357
358 m_angle = static_cast<Rotation>(rotation);
359}
360
362{
363 m_leftRight = false;
364 m_upDown = false;
365 m_angle = defaultRotation;
366}
367
369{
370 m_leftRight = enabled;
371}
372
373inline void TransformationModule::SetMirrorUpDownEnabled(bool enabled)
374{
375 m_upDown = enabled;
376}
377
379{
380 return m_leftRight;
381}
382
384{
385 return m_upDown;
386}
387
388inline void TransformationModule::SetRotationAngle(Rotation angle)
389{
390 if (static_cast<uint32_t>(angle) % 90 != 0 || static_cast<uint32_t>(angle) > 270)
391 {
392 throw peak::icv::NotSupportedException("The given rotation angle is not supported.");
394 m_angle = angle;
395}
396
398{
399 return m_angle;
400}
401
403{
404 return Process(peak::common::Any(input)).AnyCast<peak::icv::Image>();
405}
406
408{
409 const auto mirrorNeedsProcessing = m_mirrorEnabled && (m_leftRight || m_upDown);
410 const auto rotationNeedsProcessing = m_rotationEnabled && (m_angle != Rotation::None);
411
412 if (!mirrorNeedsProcessing && !rotationNeedsProcessing)
413 {
414 return input;
415 }
416
417 const auto& inputImage = input.AnyCast<peak::icv::Image>();
419 auto* const inputImageHandle = peak::common::detail::BackendAccessor<peak::icv::Image>::BackendHandle(inputImage);
420
421 const peak_icv_preprocessing_transformation_parameters parameters{ m_mirrorEnabled && m_leftRight, m_mirrorEnabled && m_upDown,
422 m_rotationEnabled ? static_cast<peak_icv_preprocessing_transformation_rotation_angle>(m_angle) :
423 PEAK_ICV_PREPROCESSING_TRANSFORMATION_ROTATION_NONE };
424
425 peak_common_pixel_format outputPixelFormat;
426
428 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Transformation_GetOutputPixelFormat(
429 static_cast<peak_common_pixel_format>(inputImage.GetPixelFormat()), parameters, &outputPixelFormat);
430 });
431
432 const auto transpose = m_rotationEnabled && (m_angle == Rotation::Degree90Clockwise || m_angle == Rotation::Degree90Counterclockwise);
433 const auto outputSize = transpose ? inputImage.GetSize().Transposed() : inputImage.GetSize();
434 peak::icv::Image outputImage{ static_cast<peak::common::PixelFormat>(outputPixelFormat), outputSize };
435 auto* const outputImageHandle = peak::common::detail::BackendAccessor<peak::icv::Image>::BackendHandle(outputImage);
436
438 return PEAK_ICV_C_ABI_PREFIX peak_icv_Preprocessing_Transformation_Process(inputImageHandle, parameters, outputImageHandle);
439 });
440
441 return peak::common::Any(outputImage);
442}
443
444inline void TransformationModule::SetMirrorEnabled(bool enabled)
445{
446 m_mirrorEnabled = enabled;
447}
448
449inline bool TransformationModule::IsMirrorEnabled() const
450{
451 return m_mirrorEnabled;
452}
453
454inline void TransformationModule::SetRotationEnabled(bool enabled)
455{
456 m_rotationEnabled = enabled;
457}
458
459inline bool TransformationModule::IsRotationEnabled() const
460{
461 return m_rotationEnabled;
462}
463
464} // namespace detail
465} // namespace pipeline
466} // 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
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
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.
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)