summaryrefslogtreecommitdiff
path: root/internal/dynamic_depth/depth_map.cc
blob: 4d16c8dede4da7a3928c8564918742e092857c1b (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "dynamic_depth/depth_map.h"

#include "android-base/logging.h"
#include "dynamic_depth/const.h"
#include "dynamic_depth/item.h"
#include "strings/numbers.h"
#include "xmpmeta/base64.h"

using photos_editing_formats::dynamic_depth::Item;
using photos_editing_formats::xml::Deserializer;
using photos_editing_formats::xml::Serializer;

namespace photos_editing_formats {
namespace dynamic_depth {
namespace {
constexpr const char* kNamespaceHref =
    "http://ns.google.com/photos/dd/1.0/depthmap/";

constexpr const char* kFormat = "Format";
constexpr const char* kNear = "Near";
constexpr const char* kFar = "Far";
constexpr const char* kUnits = "Units";
constexpr const char* kDepthUri = "DepthURI";
constexpr const char* kItemSemantic = "ItemSemantic";
constexpr const char* kConfidenceUri = "ConfidenceURI";
constexpr const char* kMeasureType = "MeasureType";
constexpr const char* kSoftware = "Software";
constexpr const char* kFocalTable = "FocalTable";
constexpr const char* kFocalTableEntryCount = "FocalTableEntryCount";

constexpr const char* kFormatRangeInverse = "RangeInverse";
constexpr const char* kFormatRangeLinear = "RangeLinear";
constexpr const char* kFormatRangeInverseLower = "rangeinverse";
constexpr const char* kFormatRangeLinearLower = "rangelinear";

constexpr const char* kUnitsMeters = "Meters";
constexpr const char* kUnitsDiopters = "Diopters";
constexpr const char* kUnitsNone = "None";
constexpr const char* kUnitsMetersLower = "meters";
constexpr const char* kUnitsDioptersLower = "diopters";

constexpr const char* kMeasureTypeOpticalAxis = "OpticalAxis";
constexpr const char* kMeasureTypeOpticRay = "OpticRay";
constexpr const char* kMeasureTypeOpticRayLower = "opticray";

constexpr const char* kItemSemanticDepth = "Depth";
constexpr const char* kItemSemanticSegmentation = "Segmentation";
constexpr const char* kItemSemanticSegmentationLower = "segmentation";

string ItemSemanticToString(DepthItemSemantic item_semantic) {
  switch (item_semantic) {
    case DepthItemSemantic::kDepth:
      return kItemSemanticDepth;
    case DepthItemSemantic::kSegmentation:
      return kItemSemanticSegmentation;
  }
}

DepthItemSemantic StringToItemSemantic(const string& semantic_str) {
  string semantic_str_lower = semantic_str;
  std::transform(semantic_str_lower.begin(), semantic_str_lower.end(),
                 semantic_str_lower.begin(), ::tolower);
  if (kItemSemanticSegmentationLower == semantic_str_lower) {
    return DepthItemSemantic::kSegmentation;
  }

  return DepthItemSemantic::kDepth;
}

string FormatToString(DepthFormat format) {
  switch (format) {
    case DepthFormat::kRangeInverse:
      return kFormatRangeInverse;
    case DepthFormat::kRangeLinear:
      return kFormatRangeLinear;
    case DepthFormat::kFormatNone:
      return "";
  }
}

// Case insensitive.
DepthFormat StringToFormat(const string& format_str) {
  string format_str_lower = format_str;
  std::transform(format_str_lower.begin(), format_str_lower.end(),
                 format_str_lower.begin(), ::tolower);
  if (kFormatRangeInverseLower == format_str_lower) {
    return DepthFormat::kRangeInverse;
  }

  if (kFormatRangeLinearLower == format_str_lower) {
    return DepthFormat::kRangeLinear;
  }

  return DepthFormat::kFormatNone;
}

string UnitsToString(DepthUnits units) {
  switch (units) {
    case DepthUnits::kMeters:
      return kUnitsMeters;
    case DepthUnits::kDiopters:
      return kUnitsDiopters;
    case DepthUnits::kUnitsNone:
      return kUnitsNone;
  }
}

DepthUnits StringToUnits(const string& units_str) {
  string units_str_lower = units_str;
  std::transform(units_str_lower.begin(), units_str_lower.end(),
                 units_str_lower.begin(), ::tolower);
  if (kUnitsMetersLower == units_str_lower) {
    return DepthUnits::kMeters;
  }

  if (kUnitsDioptersLower == units_str_lower) {
    return DepthUnits::kDiopters;
  }

  return DepthUnits::kUnitsNone;
}

string MeasureTypeToString(DepthMeasureType measure_type) {
  switch (measure_type) {
    case DepthMeasureType::kOpticRay:
      return kMeasureTypeOpticRay;
    case DepthMeasureType::kOpticalAxis:
      return kMeasureTypeOpticalAxis;
  }
}

DepthMeasureType StringToMeasureType(const string& measure_type_str) {
  string measure_type_str_lower = measure_type_str;
  std::transform(measure_type_str_lower.begin(), measure_type_str_lower.end(),
                 measure_type_str_lower.begin(), ::tolower);
  if (kMeasureTypeOpticRayLower == measure_type_str_lower) {
    return DepthMeasureType::kOpticRay;
  }

  return DepthMeasureType::kOpticalAxis;
}

}  // namespace

// Private constructor.
DepthMap::DepthMap(const DepthMapParams& params) : params_(params) {}

// Private parser.
std::unique_ptr<DepthMap> DepthMap::ParseFields(
    const Deserializer& deserializer) {
  const string& prefix = DynamicDepthConst::DepthMap();
  string format_str;
  float near;
  float far;
  string units_str;
  string depth_uri;
  string item_semantic_str;

  if (!deserializer.ParseString(prefix, kItemSemantic, &item_semantic_str) ||
      !deserializer.ParseString(prefix, kFormat, &format_str) ||
      !deserializer.ParseFloat(prefix, kNear, &near) ||
      !deserializer.ParseFloat(prefix, kFar, &far) ||
      !deserializer.ParseString(prefix, kUnits, &units_str) ||
      !deserializer.ParseString(prefix, kDepthUri, &depth_uri)) {
    return nullptr;
  }

  DepthMapParams params(StringToFormat(format_str), near, far,
                        StringToUnits(units_str), depth_uri);
  params.item_semantic = StringToItemSemantic(item_semantic_str);

  string confidence_uri;
  if (deserializer.ParseString(prefix, kConfidenceUri, &confidence_uri)) {
    params.confidence_uri = confidence_uri;
  }

  string measure_type_str;
  if (deserializer.ParseString(prefix, kMeasureType, &measure_type_str)) {
    params.measure_type = StringToMeasureType(measure_type_str);
  }

  string software;
  if (deserializer.ParseString(prefix, kSoftware, &software)) {
    params.software = software;
  }

  std::vector<float> focal_table;
  int focal_table_entry_count;
  if (deserializer.ParseFloatArrayBase64(prefix, kFocalTable, &focal_table) &&
      (!deserializer.ParseInt(prefix, kFocalTableEntryCount,
                              &focal_table_entry_count) &&
       focal_table.size() / 2 != focal_table_entry_count)) {
    return nullptr;
  }
  params.focal_table = focal_table;

  return std::unique_ptr<DepthMap>(new DepthMap(params));  // NOLINT
}

// Public methods.
void DepthMap::GetNamespaces(
    std::unordered_map<string, string>* ns_name_href_map) {
  if (ns_name_href_map == nullptr) {
    LOG(ERROR) << "Namespace list or own namespace is null";
    return;
  }
  ns_name_href_map->emplace(DynamicDepthConst::DepthMap(), kNamespaceHref);
}

std::unique_ptr<DepthMap> DepthMap::FromData(
    const DepthMapParams& params, std::vector<std::unique_ptr<Item>>* items) {
  if (params.format == DepthFormat::kFormatNone) {
    LOG(ERROR)
        << "Format must be specified, cannot be of type DepthFormat::NONE";
    return nullptr;
  }

  if (params.depth_uri.empty() || params.depth_image_data.empty()) {
    LOG(ERROR) << "Depth image data and URI must be provided";
    return nullptr;
  }

  if (!params.focal_table.empty() && params.focal_table.size() % 2 != 0) {
    LOG(ERROR) << "Focal table entries must consist of pairs";
    return nullptr;
  }

  if (items == nullptr) {
    LOG(ERROR) << "List of items is null";
    return nullptr;
  }

  if (params.mime.empty()) {
    LOG(ERROR) << "Depth image mime must be provided to DepthMapParams";
    return nullptr;
  }

  ItemParams depth_item_params(params.mime, params.depth_image_data.size(),
                               params.depth_uri);
  depth_item_params.payload_to_serialize = params.depth_image_data;
  items->emplace_back(Item::FromData(depth_item_params));

  bool available_confidence_uri_and_data = true;
  if (!params.confidence_uri.empty() && !params.confidence_data.empty()) {
    // Assumes that the confidence mime is the same as that of the depth map.
    ItemParams confidence_item_params(
        params.mime, params.confidence_data.size(), params.confidence_uri);
    confidence_item_params.payload_to_serialize = params.confidence_data;
    items->emplace_back(Item::FromData(confidence_item_params));
  } else if (!params.confidence_uri.empty() && params.confidence_data.empty()) {
    LOG(ERROR) << "No confidence data provided, the URI will be set to empty "
                  "and not serialized";
    available_confidence_uri_and_data = false;
  }

  auto depth_map = std::unique_ptr<DepthMap>(new DepthMap(params));  // NOLINT
  if (!available_confidence_uri_and_data) {
    // Ensure we don't serialize the confidence URI if no data has been
    // provided.
    depth_map->params_.confidence_uri = "";
  }

  return depth_map;
}

std::unique_ptr<DepthMap> DepthMap::FromDeserializer(
    const xml::Deserializer& parent_deserializer) {
  std::unique_ptr<Deserializer> deserializer =
      parent_deserializer.CreateDeserializer(
          DynamicDepthConst::Namespace(DynamicDepthConst::DepthMap()),
          DynamicDepthConst::DepthMap());
  if (deserializer == nullptr) {
    LOG(ERROR) << "Deserializer must not be null";
    return nullptr;
  }

  return ParseFields(*deserializer);
}

DepthFormat DepthMap::GetFormat() const { return params_.format; }
float DepthMap::GetNear() const { return params_.near; }
float DepthMap::GetFar() const { return params_.far; }
DepthUnits DepthMap::GetUnits() const { return params_.units; }
const string DepthMap::GetDepthUri() const { return params_.depth_uri; }
DepthItemSemantic DepthMap::GetItemSemantic() const {
  return params_.item_semantic;
}
const string DepthMap::GetConfidenceUri() const {
  return params_.confidence_uri;
}

DepthMeasureType DepthMap::GetMeasureType() const {
  return params_.measure_type;
}

const string DepthMap::GetSoftware() const { return params_.software; }
const std::vector<float>& DepthMap::GetFocalTable() const {
  return params_.focal_table;
}

size_t DepthMap::GetFocalTableEntryCount() const {
  return params_.focal_table.size() / 2;
}

bool DepthMap::Serialize(Serializer* serializer) const {
  if (serializer == nullptr) {
    LOG(ERROR) << "Serializer is null";
    return false;
  }
  if (params_.depth_uri.empty()) {
    LOG(ERROR) << "Depth image URI is empty";
    return false;
  }

  const string& prefix = DynamicDepthConst::DepthMap();
  // Error checking is already done in FromData.
  if (!serializer->WriteProperty(prefix, kItemSemantic,
                                 ItemSemanticToString(params_.item_semantic)) ||
      !serializer->WriteProperty(prefix, kFormat,
                                 FormatToString(params_.format)) ||
      !serializer->WriteProperty(prefix, kUnits,
                                 UnitsToString(params_.units)) ||
      !serializer->WriteProperty(prefix, kNear, std::to_string(params_.near)) ||
      !serializer->WriteProperty(prefix, kFar, std::to_string(params_.far)) ||
      !serializer->WriteProperty(prefix, kDepthUri, params_.depth_uri)) {
    return false;
  }

  serializer->WriteProperty(prefix, kMeasureType,
                            MeasureTypeToString(params_.measure_type));

  if (!params_.confidence_uri.empty()) {
    serializer->WriteProperty(prefix, kConfidenceUri, params_.confidence_uri);
  }

  if (!params_.software.empty()) {
    serializer->WriteProperty(prefix, kSoftware, params_.software);
  }

  if (!params_.focal_table.empty()) {
    string base64_encoded_focal_table;
    if (!EncodeFloatArrayBase64(params_.focal_table,
                                &base64_encoded_focal_table)) {
      LOG(ERROR) << "Focal table encoding failed";
    } else {
      int focal_table_entry_count =
          static_cast<int>(params_.focal_table.size() / 2);
      if (!serializer->WriteProperty(
              prefix, kFocalTableEntryCount,
              ::dynamic_depth::strings::SimpleItoa(focal_table_entry_count)) ||
          !serializer->WriteProperty(prefix, kFocalTable,
                                     base64_encoded_focal_table)) {
        LOG(ERROR) << "Focal table or entry count could not be serialized";
      }
    }
  }

  return true;
}

}  // namespace dynamic_depth
}  // namespace photos_editing_formats