summaryrefslogtreecommitdiff
path: root/internal/dynamic_depth/device.cc
blob: 2cfff99ab3908b5455d0aab7d5a4656c7ed30719 (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
#include "dynamic_depth/device.h"

#include <libxml/tree.h>

#include "android-base/logging.h"
#include "dynamic_depth/const.h"
#include "dynamic_depth/vendor_info.h"
#include "xmpmeta/xml/const.h"
#include "xmpmeta/xml/deserializer_impl.h"
#include "xmpmeta/xml/search.h"
#include "xmpmeta/xml/serializer_impl.h"
#include "xmpmeta/xml/utils.h"
#include "xmpmeta/xmp_data.h"
#include "xmpmeta/xmp_parser.h"
#include "xmpmeta/xmp_writer.h"

using photos_editing_formats::xml::DepthFirstSearch;
using photos_editing_formats::xml::DeserializerImpl;
using photos_editing_formats::xml::GetFirstDescriptionElement;
using photos_editing_formats::xml::Serializer;
using photos_editing_formats::xml::SerializerImpl;
using photos_editing_formats::xml::ToXmlChar;
using photos_editing_formats::xml::XmlConst;

namespace photos_editing_formats {
namespace dynamic_depth {
namespace {

const char kRevision[] = "Revision";
const char kNamespaceHref[] = "http://ns.google.com/photos/dd/1.0/device/";

// Parses Device fields and children elements from xmlDocPtr.
std::unique_ptr<Device> ParseFields(const xmlDocPtr& xmlDoc) {
  // Find and parse the Device node.
  // Only these two fields are required to be present; the rest are optional.
  // TODO(miraleung): Search for Device by namespace.
  xmlNodePtr device_node =
      DepthFirstSearch(xmlDoc, DynamicDepthConst::Device());
  if (device_node == nullptr) {
    LOG(ERROR) << "No device node found";
    return nullptr;
  }

  const DeserializerImpl deserializer(device_node);
  auto cameras = Cameras::FromDeserializer(deserializer);
  if (cameras == nullptr) {
    LOG(ERROR) << "No cameras found";
    return nullptr;
  }

  auto container = Container::FromDeserializer(deserializer);

  // The list of cameras is now guaranteed to have at least one element, because
  // of implementation in cameras.cc
  auto planes = Planes::FromDeserializer(deserializer);
  auto earth_pose = EarthPose::FromDeserializer(deserializer);
  auto pose = Pose::FromDeserializer(deserializer, DynamicDepthConst::Device());
  auto profiles = Profiles::FromDeserializer(deserializer);
  auto vendor_info =
      VendorInfo::FromDeserializer(deserializer, DynamicDepthConst::Device());
  auto app_info =
      AppInfo::FromDeserializer(deserializer, DynamicDepthConst::Device());

  std::unique_ptr<DeviceParams>
      params(new DeviceParams(std::move(cameras)));  // NOLINT
  params->container = std::move(container);
  params->planes = std::move(planes);
  params->earth_pose = std::move(earth_pose);
  params->pose = std::move(pose);
  params->profiles = std::move(profiles);
  params->vendor_info = std::move(vendor_info);
  params->app_info = std::move(app_info);
  return Device::FromData(std::move(params));
}

// Parses Device fields and children elements from XmpData.
std::unique_ptr<Device> ParseFields(const XmpData& xmp) {
  if (xmp.ExtendedSection() == nullptr) {
    LOG(ERROR) << "XMP extended section is null";
    return nullptr;
  }

  return ParseFields(xmp.ExtendedSection());
}

}  // namespace

// Private constructor.
Device::Device(std::unique_ptr<DeviceParams> params) {
  params_ = std::move(params);
}

// Public methods.
std::unique_ptr<Device> Device::FromData(std::unique_ptr<DeviceParams> params) {
  if (params->cameras == nullptr) {
    LOG(ERROR) << "At least one camera must be provided";
    return nullptr;
  }

  // The list of cameras is now guaranteed to have at least one element, because
  // of the implementation in cameras.cc
  return std::unique_ptr<Device>(new Device(std::move(params)));  // NOLINT
}

std::unique_ptr<Device> Device::FromXmp(const XmpData& xmp) {
  return ParseFields(xmp);
}

std::unique_ptr<Device> Device::FromJpegFile(const string& filename) {
  XmpData xmp;
  const bool kSkipExtended = false;
  if (!ReadXmpHeader(filename, kSkipExtended, &xmp)) {
    return nullptr;
  }
  return FromXmp(xmp);
}

// Creates a Device by parsing XML file containing the metadata.
std::unique_ptr<Device> Device::FromXmlFile(const string& filename) {
  xmlDocPtr xmlDoc = xmlReadFile(filename.c_str(), nullptr, 0);
  if (xmlDoc == nullptr) {
    LOG(ERROR) << "Failed to read file: " << filename;
    return nullptr;
  }

  auto device = ParseFields(xmlDoc);
  xmlFreeDoc(xmlDoc);
  return device;
}

const Cameras* Device::GetCameras() const { return params_->cameras.get(); }

const Container* Device::GetContainer() const {
  return params_->container.get();
}

const EarthPose* Device::GetEarthPose() const {
  return params_->earth_pose.get();
}

const Pose* Device::GetPose() const { return params_->pose.get(); }

const Planes* Device::GetPlanes() const { return params_->planes.get(); }

const Profiles* Device::GetProfiles() const { return params_->profiles.get(); }

const VendorInfo* Device::GetVendorInfo() const {
  return params_->vendor_info.get();
}

const AppInfo* Device::GetAppInfo() const { return params_->app_info.get(); }

// This cannot be const because of memory management for the namespaces.
// namespaces_ are freed when the XML document(s) in xmp are freed.
// If namespaces_ are populated at object creation time and this
// object is serialized, freeing the xmlNs objects in the destructor will result
// memory management errors.
bool Device::SerializeToXmp(XmpData* xmp) {
  if (xmp == nullptr || xmp->StandardSection() == nullptr ||
      xmp->ExtendedSection() == nullptr) {
    LOG(ERROR) << "XmpData or its sections are null";
    return false;
  }
  return Serialize(xmp->MutableExtendedSection());
}

bool Device::SerializeToXmlFile(const char* filename) {
  std::unique_ptr<XmpData> xmp_data = CreateXmpData(true);
  if (!Serialize(xmp_data->MutableExtendedSection())) {
    return false;
  }
  return xmlSaveFile(filename, xmp_data->ExtendedSection()) != -1;
}

// Private methods.
bool Device::Serialize(xmlDocPtr* xmlDoc) {
  xmlNodePtr root_node = GetFirstDescriptionElement(*xmlDoc);
  if (root_node == nullptr) {
    LOG(ERROR) << "Extended section has no rdf:Description node";
    return false;
  }

  if (params_->cameras == nullptr) {
    LOG(ERROR) << "At least one camera must be present, stopping serialization";
    return false;
  }

  // Create a node here instead of through a new deserializer, otherwise
  // an extraneous prefix will be written to the node name.
  xmlNodePtr device_node =
      xmlNewNode(nullptr, ToXmlChar(DynamicDepthConst::Device()));
  xmlAddChild(root_node, device_node);

  PopulateNamespaces();
  xmlNsPtr prev_ns = root_node->ns;
  for (const auto& entry : namespaces_) {
    if (prev_ns != nullptr) {
      prev_ns->next = entry.second;
    }
    prev_ns = entry.second;
  }

  // Set up serialization on the first description node in the extended section.
  SerializerImpl device_serializer(namespaces_, device_node);

  // Serialize elements.
  if (params_->container &&
      !params_->container->Serialize(&device_serializer)) {
    return false;
  }

  if (params_->earth_pose) {
    std::unique_ptr<Serializer> earth_pose_serializer =
        device_serializer.CreateSerializer(
            DynamicDepthConst::Namespace(DynamicDepthConst::EarthPose()),
            DynamicDepthConst::EarthPose());
    if (!params_->earth_pose->Serialize(earth_pose_serializer.get())) {
      return false;
    }
  }

  if (params_->pose) {
    std::unique_ptr<Serializer> pose_serializer =
        device_serializer.CreateSerializer(DynamicDepthConst::Device(),
                                           DynamicDepthConst::Pose());
    if (!params_->pose->Serialize(pose_serializer.get())) {
      return false;
    }
  }

  if (params_->profiles && !params_->profiles->Serialize(&device_serializer)) {
    return false;
  }

  // Serialize Planes before Cameras, since the data in Planes is likely to be
  // significantly smaller than the potential media types in a Camera.
  if (params_->planes && !params_->planes->Serialize(&device_serializer)) {
    return false;
  }

  if (params_->cameras && !params_->cameras->Serialize(&device_serializer)) {
    return false;
  }

  if (params_->vendor_info) {
    std::unique_ptr<Serializer> vendor_info_serializer =
        device_serializer.CreateSerializer(DynamicDepthConst::Device(),
                                           DynamicDepthConst::VendorInfo());
    if (!params_->vendor_info->Serialize(vendor_info_serializer.get())) {
      return false;
    }
  }

  if (params_->app_info) {
    std::unique_ptr<Serializer> app_info_serializer =
        device_serializer.CreateSerializer(DynamicDepthConst::Device(),
                                           DynamicDepthConst::AppInfo());
    if (!params_->app_info->Serialize(app_info_serializer.get())) {
      return false;
    }
  }

  return true;
}
void Device::GetNamespaces(
    std::unordered_map<string, string>* ns_name_href_map) const {
  if (ns_name_href_map == nullptr) {
    LOG(ERROR) << "Namespace list is null";
    return;
  }
  ns_name_href_map->emplace(XmlConst::RdfPrefix(), XmlConst::RdfNodeNs());
  ns_name_href_map->emplace(DynamicDepthConst::Device(), kNamespaceHref);
  if (params_->earth_pose) {
    params_->earth_pose->GetNamespaces(ns_name_href_map);
  }
  if (params_->pose) {
    params_->pose->GetNamespaces(ns_name_href_map);
  }
  if (params_->profiles) {
    params_->profiles->GetNamespaces(ns_name_href_map);
  }
  if (params_->planes) {
    params_->planes->GetNamespaces(ns_name_href_map);
  }
  if (params_->cameras) {
    params_->cameras->GetNamespaces(ns_name_href_map);
  }
  if (params_->container) {
    params_->container->GetNamespaces(ns_name_href_map);
  }
  if (params_->vendor_info) {
    params_->vendor_info->GetNamespaces(ns_name_href_map);
  }
  if (params_->app_info) {
    params_->app_info->GetNamespaces(ns_name_href_map);
  }
}

// Gathers all the XML namespaces of child elements.
void Device::PopulateNamespaces() {
  std::unordered_map<string, string> ns_name_href_map;
  GetNamespaces(&ns_name_href_map);
  for (const auto& entry : ns_name_href_map) {
    if (!namespaces_.count(entry.first)) {
      namespaces_.emplace(entry.first,
                          xmlNewNs(nullptr, ToXmlChar(entry.second.data()),
                                   ToXmlChar(entry.first.data())));
    }
  }
}

}  // namespace dynamic_depth
}  // namespace photos_editing_formats