summaryrefslogtreecommitdiff
path: root/includes/dynamic_depth/depth_map.h
blob: 836ff23657b0820a22e1e97dd67eba8ed6d2afdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#ifndef DYNAMIC_DEPTH_INCLUDES_DYNAMIC_DEPTH_DEPTH_MAP_H_  // NOLINT
#define DYNAMIC_DEPTH_INCLUDES_DYNAMIC_DEPTH_DEPTH_MAP_H_  // NOLINT

#include <memory>
#include <string>
#include <unordered_map>
#include <utility>

#include "dynamic_depth/element.h"
#include "dynamic_depth/item.h"
#include "xmpmeta/xml/deserializer.h"
#include "xmpmeta/xml/serializer.h"

namespace photos_editing_formats {
namespace dynamic_depth {

// The depth conversion format. Please see the Depth Map element in the
// Dynamnic Depth specification for more details.
enum class DepthFormat { kFormatNone = 0, kRangeInverse = 1, kRangeLinear = 2 };

//  The Units of the depth map. Please see the Depth Map element in the Dynamic
//  Depth specification.
enum class DepthUnits { kUnitsNone = 0, kMeters = 1, kDiopters = 2 };

// The type of depth measurement. Please see the Depth Map element in the
// Dynamic Depth specification.
enum class DepthMeasureType { kOpticalAxis = 1, kOpticRay = 2 };

// The semantics of this depth map.
enum class DepthItemSemantic { kDepth = 1, kSegmentation = 2 };

struct DepthMapParams {
  // Mandatory values.
  DepthFormat format;
  float near;
  float far;
  DepthUnits units;
  string depth_uri;
  string mime;
  DepthItemSemantic item_semantic = DepthItemSemantic::kDepth;

  // The bytes of the depth image. Must be non-empty at write-time (i.e.
  // programmatic construction).
  string depth_image_data = "";

  // Optional values.
  DepthMeasureType measure_type = DepthMeasureType::kOpticalAxis;
  string confidence_uri = "";
  // The bytes of the confidence map. If confidence_uri is not empty, the
  // confidence data must be non-empty at write-time (i.e. programmatic
  // construction).
  string confidence_data = "";
  string software = "";

  // A list of (distance, radius) pairs. This should generally have a short
  // length, so copying is expected to be inexpensive.
  std::vector<float> focal_table;

  explicit DepthMapParams(DepthFormat in_format, float in_near, float in_far,
                          DepthUnits in_units, string in_depth_uri)
      : format(in_format),
        near(in_near),
        far(in_far),
        units(in_units),
        depth_uri(in_depth_uri) {}

  inline bool operator==(const DepthMapParams& other) const {
    return format == other.format && near == other.near && far == other.far &&
           units == other.units && depth_uri == other.depth_uri &&
           depth_image_data == other.depth_image_data &&
           measure_type == other.measure_type &&
           confidence_uri == other.confidence_uri &&
           confidence_data == other.confidence_data &&
           software == other.software && focal_table == other.focal_table;
  }

  inline bool operator!=(const DepthMapParams& other) const {
    return !(*this == other);
  }
};

// Implements the Depth Map element from the Dynamic Depth specification, with
// serialization and deserialization.
class DepthMap : public Element {
 public:
  void GetNamespaces(
      std::unordered_map<string, string>* ns_name_href_map) override;

  bool Serialize(xml::Serializer* serializer) const override;

  // Creates a DepthMap from the given objects in params.
  static std::unique_ptr<DepthMap> FromData(
      const DepthMapParams& params, std::vector<std::unique_ptr<Item>>* items);

  // Returns the deserialized DepthMap object, null if parsing fails.
  // Not sensitive to case when parsing the Format, Units, or MeasureType
  // fields.
  static std::unique_ptr<DepthMap> FromDeserializer(
      const xml::Deserializer& parent_deserializer);

  DepthFormat GetFormat() const;
  float GetNear() const;
  float GetFar() const;
  DepthUnits GetUnits() const;
  const string GetDepthUri() const;
  DepthItemSemantic GetItemSemantic() const;
  const string GetConfidenceUri() const;
  DepthMeasureType GetMeasureType() const;
  const string GetSoftware() const;
  const std::vector<float>& GetFocalTable() const;
  size_t GetFocalTableEntryCount() const;

  // Disallow copying
  DepthMap(const DepthMap&) = delete;
  void operator=(const DepthMap&) = delete;

 private:
  explicit DepthMap(const DepthMapParams& params);
  static std::unique_ptr<DepthMap> ParseFields(
      const xml::Deserializer& deserializer);

  DepthMapParams params_;
};

}  // namespace dynamic_depth
}  // namespace photos_editing_formats

#endif // DYNAMIC_DEPTH_INCLUDES_DYNAMIC_DEPTH_DEPTH_MAP_H_  // NOLINT