1 #ifndef DYNAMIC_DEPTH_INCLUDES_DYNAMIC_DEPTH_DEPTH_MAP_H_  // NOLINT
2 #define DYNAMIC_DEPTH_INCLUDES_DYNAMIC_DEPTH_DEPTH_MAP_H_  // NOLINT
3 
4 #include <memory>
5 #include <string>
6 #include <unordered_map>
7 #include <utility>
8 
9 #include "dynamic_depth/element.h"
10 #include "dynamic_depth/item.h"
11 #include "xmpmeta/xml/deserializer.h"
12 #include "xmpmeta/xml/serializer.h"
13 
14 namespace dynamic_depth {
15 
16 // The depth conversion format. Please see the Depth Map element in the
17 // Dynamnic Depth specification for more details.
18 enum class DepthFormat { kFormatNone = 0, kRangeInverse = 1, kRangeLinear = 2 };
19 
20 //  The Units of the depth map. Please see the Depth Map element in the Dynamic
21 //  Depth specification.
22 enum class DepthUnits { kUnitsNone = 0, kMeters = 1, kDiopters = 2 };
23 
24 // The type of depth measurement. Please see the Depth Map element in the
25 // Dynamic Depth specification.
26 enum class DepthMeasureType { kOpticalAxis = 1, kOpticRay = 2 };
27 
28 // The semantics of this depth map.
29 enum class DepthItemSemantic { kDepth = 1, kSegmentation = 2 };
30 
31 struct DepthMapParams {
32   // Mandatory values.
33   DepthFormat format;
34   float near;
35   float far;
36   DepthUnits units;
37   string depth_uri;
38   string mime;
39   DepthItemSemantic item_semantic = DepthItemSemantic::kDepth;
40 
41   // The bytes of the depth image. Must be non-empty at write-time (i.e.
42   // programmatic construction).
43   string depth_image_data = "";
44 
45   // Optional values.
46   DepthMeasureType measure_type = DepthMeasureType::kOpticalAxis;
47   string confidence_uri = "";
48   // The bytes of the confidence map. If confidence_uri is not empty, the
49   // confidence data must be non-empty at write-time (i.e. programmatic
50   // construction).
51   string confidence_data = "";
52   string software = "";
53 
54   // A list of (distance, radius) pairs. This should generally have a short
55   // length, so copying is expected to be inexpensive.
56   std::vector<float> focal_table;
57 
DepthMapParamsDepthMapParams58   explicit DepthMapParams(DepthFormat in_format, float in_near, float in_far,
59                           DepthUnits in_units, string in_depth_uri)
60       : format(in_format),
61         near(in_near),
62         far(in_far),
63         units(in_units),
64         depth_uri(in_depth_uri) {}
65 
66   inline bool operator==(const DepthMapParams& other) const {
67     return format == other.format && near == other.near && far == other.far &&
68            units == other.units && depth_uri == other.depth_uri &&
69            depth_image_data == other.depth_image_data &&
70            measure_type == other.measure_type &&
71            confidence_uri == other.confidence_uri &&
72            confidence_data == other.confidence_data &&
73            software == other.software && focal_table == other.focal_table;
74   }
75 
76   inline bool operator!=(const DepthMapParams& other) const {
77     return !(*this == other);
78   }
79 };
80 
81 // Implements the Depth Map element from the Dynamic Depth specification, with
82 // serialization and deserialization.
83 class DepthMap : public Element {
84  public:
85   void GetNamespaces(
86       std::unordered_map<string, string>* ns_name_href_map) override;
87 
88   bool Serialize(
89       ::dynamic_depth::xmpmeta::xml::Serializer* serializer) const override;
90 
91   // Creates a DepthMap from the given objects in params.
92   static std::unique_ptr<DepthMap> FromData(
93       const DepthMapParams& params, std::vector<std::unique_ptr<Item>>* items);
94 
95   // Returns the deserialized DepthMap object, null if parsing fails.
96   // Not sensitive to case when parsing the Format, Units, or MeasureType
97   // fields.
98   static std::unique_ptr<DepthMap> FromDeserializer(
99       const ::dynamic_depth::xmpmeta::xml::Deserializer& parent_deserializer);
100 
101   DepthFormat GetFormat() const;
102   float GetNear() const;
103   float GetFar() const;
104   DepthUnits GetUnits() const;
105   const string GetDepthUri() const;
106   DepthItemSemantic GetItemSemantic() const;
107   const string GetConfidenceUri() const;
108   DepthMeasureType GetMeasureType() const;
109   const string GetSoftware() const;
110   const std::vector<float>& GetFocalTable() const;
111   size_t GetFocalTableEntryCount() const;
112 
113   // Disallow copying
114   DepthMap(const DepthMap&) = delete;
115   void operator=(const DepthMap&) = delete;
116 
117  private:
118   explicit DepthMap(const DepthMapParams& params);
119   static std::unique_ptr<DepthMap> ParseFields(
120       const ::dynamic_depth::xmpmeta::xml::Deserializer& deserializer);
121 
122   DepthMapParams params_;
123 };
124 
125 }  // namespace dynamic_depth
126 
127 #endif // DYNAMIC_DEPTH_INCLUDES_DYNAMIC_DEPTH_DEPTH_MAP_H_  // NOLINT
128