FrameGenerator.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * TI Voxel Lib component.
  3. *
  4. * Copyright (c) 2014 Texas Instruments Inc.
  5. */
  6. #ifndef VOXEL_FRAME_GENERATOR_H
  7. #define VOXEL_FRAME_GENERATOR_H
  8. #include <Frame.h>
  9. #include <Common.h>
  10. #include <Serializable.h>
  11. namespace Voxel
  12. {
  13. class FrameStreamWriter;
  14. class VOXEL_EXPORT FrameGenerator
  15. {
  16. protected:
  17. GeneratorIDType _id;
  18. int _frameType; // Generated frame type. Value as in Voxel::DepthCamera::FrameType
  19. Ptr<FrameStreamWriter> _frameStreamWriter;
  20. StringKeySerializableMap _frameGeneratorParameters; // Primarily for the purpose of serializing/deserializing these parameters
  21. uint8_t _majorVersion, _minorVersion; // Used for storing/reading configuration
  22. virtual bool _onReadConfiguration() = 0; // On read configuration
  23. virtual bool _onWriteConfiguration() = 0; // On write configuration
  24. inline bool _getParam(const String &name, SerializablePtr &param) const;
  25. template <typename T>
  26. inline bool _set(const String &name, const T &value); // Only for internal use
  27. public:
  28. FrameGenerator(GeneratorIDType id, int frameType, uint8_t majorVersion, uint8_t minorVersion): _id(id), _frameType(frameType),
  29. _majorVersion(majorVersion), _minorVersion(minorVersion)
  30. {
  31. _frameGeneratorParameters["version"] =
  32. SerializablePtr(new SerializableUnsignedInt((majorVersion << 8) + minorVersion));
  33. }
  34. inline const GeneratorIDType &id() const { return _id; }
  35. inline void setFrameStreamWriter(Ptr<FrameStreamWriter> &writer) { _frameStreamWriter = writer; }
  36. inline void removeFrameStreamWriter() { _frameStreamWriter = nullptr; }
  37. virtual bool readConfiguration(SerializedObject &object); // Read configuration from serialized data object
  38. virtual bool writeConfiguration(); // Write configuration to FrameStreamWriter
  39. virtual bool generate(const FramePtr &in, FramePtr &out) = 0;
  40. template <typename T>
  41. inline bool get(const String &name, T &value) const;
  42. virtual ~FrameGenerator() {}
  43. };
  44. typedef Ptr<FrameGenerator> FrameGeneratorPtr;
  45. inline bool FrameGenerator::_getParam(const String &name, SerializablePtr &param) const
  46. {
  47. auto x = _frameGeneratorParameters.find(name);
  48. if(x != _frameGeneratorParameters.end())
  49. {
  50. param = x->second;
  51. return true;
  52. }
  53. return false;
  54. }
  55. template <typename T>
  56. inline bool FrameGenerator::_set(const String &name, const T &value)
  57. {
  58. SerializablePtr param;
  59. if(!_getParam(name, param))
  60. return false;
  61. BasicSerializable<T> *p = dynamic_cast<BasicSerializable<T> *>(param.get());
  62. if(p)
  63. {
  64. p->value = value;
  65. return true;
  66. }
  67. return false;
  68. }
  69. template <>
  70. inline bool FrameGenerator::_set<String>(const String &name, const String &value)
  71. {
  72. SerializablePtr param;
  73. if(!_getParam(name, param))
  74. return false;
  75. SerializableString *p = dynamic_cast<SerializableString *>(param.get());
  76. if(p)
  77. {
  78. *p = value;
  79. return true;
  80. }
  81. return false;
  82. }
  83. template <typename T>
  84. inline bool FrameGenerator::get(const String &name, T &value) const
  85. {
  86. SerializablePtr param;
  87. if(!_getParam(name, param))
  88. return false;
  89. const BasicSerializable<T> *p = dynamic_cast<BasicSerializable<T> *>(param.get());
  90. if(p)
  91. {
  92. value = p->value;
  93. return true;
  94. }
  95. return false;
  96. }
  97. template <>
  98. inline bool FrameGenerator::get<String>(const String &name, String &value) const
  99. {
  100. SerializablePtr param;
  101. if(!_getParam(name, param))
  102. return false;
  103. SerializableString *p = dynamic_cast<SerializableString *>(param.get());
  104. if(p)
  105. {
  106. value = *p;
  107. return true;
  108. }
  109. return false;
  110. }
  111. class VOXEL_EXPORT DepthFrameGenerator: public FrameGenerator
  112. {
  113. public:
  114. DepthFrameGenerator(GeneratorIDType id, int frameType, uint8_t majorVersion, uint8_t minorVersion): FrameGenerator(id, frameType, majorVersion, minorVersion) {}
  115. virtual bool setProcessedFrameGenerator(FrameGeneratorPtr &p) = 0;
  116. virtual ~DepthFrameGenerator() {}
  117. };
  118. typedef Ptr<DepthFrameGenerator> DepthFrameGeneratorPtr;
  119. }
  120. #endif