• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "dynamic_depth/cameras.h"
2 
3 #include "android-base/logging.h"
4 #include "dynamic_depth/const.h"
5 
6 using ::dynamic_depth::xmpmeta::xml::Deserializer;
7 using ::dynamic_depth::xmpmeta::xml::Serializer;
8 
9 namespace dynamic_depth {
10 
11 const char kNodeName[] = "Cameras";
12 const char kCameraName[] = "Camera";
13 
14 // Private constructor.
Cameras()15 Cameras::Cameras() {}
16 
17 // Public methods.
GetNamespaces(std::unordered_map<string,string> * ns_name_href_map)18 void Cameras::GetNamespaces(
19     std::unordered_map<string, string>* ns_name_href_map) {
20   if (ns_name_href_map == nullptr || camera_list_.empty()) {
21     LOG(ERROR) << "Namespace list is null or camera list is empty";
22     return;
23   }
24   for (const auto& camera : camera_list_) {
25     camera->GetNamespaces(ns_name_href_map);
26   }
27 }
28 
FromCameraArray(std::vector<std::unique_ptr<Camera>> * camera_list)29 std::unique_ptr<Cameras> Cameras::FromCameraArray(
30     std::vector<std::unique_ptr<Camera>>* camera_list) {
31   if (camera_list == nullptr || camera_list->empty()) {
32     LOG(ERROR) << "Camera list is empty";
33     return nullptr;
34   }
35   std::unique_ptr<Cameras> cameras(new Cameras());
36   cameras->camera_list_ = std::move(*camera_list);
37   return cameras;
38 }
39 
FromDeserializer(const Deserializer & parent_deserializer)40 std::unique_ptr<Cameras> Cameras::FromDeserializer(
41     const Deserializer& parent_deserializer) {
42   std::unique_ptr<Cameras> cameras(new Cameras());
43   int i = 0;
44   std::unique_ptr<Deserializer> deserializer =
45       parent_deserializer.CreateDeserializerFromListElementAt(
46           DynamicDepthConst::Namespace(kNodeName), kNodeName, 0);
47   while (deserializer) {
48     std::unique_ptr<Camera> camera = Camera::FromDeserializer(*deserializer);
49     if (camera == nullptr) {
50       LOG(ERROR) << "Unable to deserialize a camera";
51       return nullptr;
52     }
53     cameras->camera_list_.emplace_back(std::move(camera));
54     deserializer = parent_deserializer.CreateDeserializerFromListElementAt(
55         DynamicDepthConst::Namespace(kNodeName), kNodeName, ++i);
56   }
57 
58   if (cameras->camera_list_.empty()) {
59     return nullptr;
60   }
61   return cameras;
62 }
63 
GetCameras() const64 const std::vector<const Camera*> Cameras::GetCameras() const {
65   std::vector<const Camera*> camera_list;
66   for (const auto& camera : camera_list_) {
67     camera_list.push_back(camera.get());
68   }
69   return camera_list;
70 }
71 
Serialize(Serializer * serializer) const72 bool Cameras::Serialize(Serializer* serializer) const {
73   if (camera_list_.empty()) {
74     LOG(ERROR) << "Camera list is empty";
75     return false;
76   }
77   std::unique_ptr<Serializer> cameras_serializer =
78       serializer->CreateListSerializer(DynamicDepthConst::Namespace(kNodeName),
79                                        kNodeName);
80   if (cameras_serializer == nullptr) {
81     // Error is logged in Serializer.
82     return false;
83   }
84   for (int i = 0; i < camera_list_.size(); i++) {
85     std::unique_ptr<Serializer> camera_serializer =
86         cameras_serializer->CreateItemSerializer(
87             DynamicDepthConst::Namespace(kCameraName), kCameraName);
88     if (camera_serializer == nullptr) {
89       LOG(ERROR) << "Could not create a list item serializer for Camera";
90       return false;
91     }
92     if (!camera_list_[i]->Serialize(camera_serializer.get())) {
93       LOG(ERROR) << "Could not serialize camera " << i;
94       return false;
95     }
96   }
97   return true;
98 }
99 
100 }  // namespace dynamic_depth
101