• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 #include "dynamic_depth/camera.h"
3 
4 #include "android-base/logging.h"
5 #include "dynamic_depth/const.h"
6 
7 using ::dynamic_depth::xmpmeta::xml::Deserializer;
8 using ::dynamic_depth::xmpmeta::xml::Serializer;
9 
10 namespace dynamic_depth {
11 namespace {
12 
13 const char kNamespaceHref[] = "http://ns.google.com/photos/dd/1.0/camera/";
14 
15 constexpr const char* kTrait = "Trait";
16 constexpr const char* kTraitPhysical = "Physical";
17 constexpr const char* kTraitPhysicalLower = "physical";
18 constexpr const char* kTraitLogical = "Logical";
19 constexpr const char* kTraitLogicalLower = "logical";
20 
21 constexpr const char* kImageJpegMime = "image/jpeg";
22 
TraitToString(CameraTrait trait)23 string TraitToString(CameraTrait trait) {
24   switch (trait) {
25     case PHYSICAL:
26       return kTraitPhysical;
27     case LOGICAL:
28       return kTraitLogical;
29     case NONE:  // Fallthrough.
30     default:
31       return "";
32   }
33 }
34 
StringToTrait(const string & trait_name)35 CameraTrait StringToTrait(const string& trait_name) {
36   string trait_lower = trait_name;
37   std::transform(trait_lower.begin(), trait_lower.end(), trait_lower.begin(),
38                  ::tolower);
39   if (kTraitPhysicalLower == trait_lower) {
40     return CameraTrait::PHYSICAL;
41   }
42 
43   if (kTraitLogicalLower == trait_lower) {
44     return CameraTrait::LOGICAL;
45   }
46 
47   return CameraTrait::NONE;
48 }
49 
ParseFields(const Deserializer & deserializer)50 std::unique_ptr<Camera> ParseFields(const Deserializer& deserializer) {
51   string trait_str;
52   deserializer.ParseString(DynamicDepthConst::Camera(), kTrait, &trait_str);
53   CameraTrait trait = StringToTrait(trait_str);
54 
55   std::unique_ptr<Image> image = Image::FromDeserializer(deserializer);
56   if (image == nullptr) {
57     LOG(ERROR) << "An image must be present in a Camera, but none was found";
58     return nullptr;
59   }
60 
61   std::unique_ptr<LightEstimate> light_estimate =
62       LightEstimate::FromDeserializer(deserializer);
63 
64   std::unique_ptr<Pose> pose =
65       Pose::FromDeserializer(deserializer, DynamicDepthConst::Camera());
66 
67   std::unique_ptr<DepthMap> depth_map =
68       DepthMap::FromDeserializer(deserializer);
69 
70   std::unique_ptr<ImagingModel> imaging_model =
71       ImagingModel::FromDeserializer(deserializer);
72 
73   std::unique_ptr<PointCloud> point_cloud =
74       PointCloud::FromDeserializer(deserializer);
75 
76   std::unique_ptr<VendorInfo> vendor_info =
77       VendorInfo::FromDeserializer(deserializer, DynamicDepthConst::Camera());
78 
79   std::unique_ptr<AppInfo> app_info =
80       AppInfo::FromDeserializer(deserializer, DynamicDepthConst::Camera());
81 
82   std::unique_ptr<CameraParams> params(new CameraParams(std::move(image)));
83   params->depth_map = std::move(depth_map);
84   params->light_estimate = std::move(light_estimate);
85   params->pose = std::move(pose);
86   params->imaging_model = std::move(imaging_model);
87   params->point_cloud = std::move(point_cloud);
88   params->vendor_info = std::move(vendor_info);
89   params->app_info = std::move(app_info);
90   params->trait = trait;
91   return Camera::FromData(std::move(params));
92 }
93 
94 }  // namespace
95 
96 // Private constructor.
Camera(std::unique_ptr<CameraParams> params)97 Camera::Camera(std::unique_ptr<CameraParams> params) {
98   params_ = std::move(params);
99 }
100 
101 // Public methods.
GetNamespaces(std::unordered_map<string,string> * ns_name_href_map)102 void Camera::GetNamespaces(
103     std::unordered_map<string, string>* ns_name_href_map) {
104   if (ns_name_href_map == nullptr) {
105     LOG(ERROR) << "Namespace list is null";
106     return;
107   }
108   ns_name_href_map->emplace(DynamicDepthConst::Camera(), kNamespaceHref);
109   if (params_->image) {
110     params_->image->GetNamespaces(ns_name_href_map);
111   }
112   if (params_->light_estimate) {
113     params_->light_estimate->GetNamespaces(ns_name_href_map);
114   }
115   if (params_->pose) {
116     params_->pose->GetNamespaces(ns_name_href_map);
117   }
118   if (params_->depth_map) {
119     params_->depth_map->GetNamespaces(ns_name_href_map);
120   }
121   if (params_->imaging_model) {
122     params_->imaging_model->GetNamespaces(ns_name_href_map);
123   }
124   if (params_->point_cloud) {
125     params_->point_cloud->GetNamespaces(ns_name_href_map);
126   }
127   if (params_->vendor_info) {
128     params_->vendor_info->GetNamespaces(ns_name_href_map);
129   }
130   if (params_->app_info) {
131     params_->app_info->GetNamespaces(ns_name_href_map);
132   }
133 }
134 
FromDataForCamera0(std::unique_ptr<CameraParams> params,std::vector<std::unique_ptr<Item>> * items)135 std::unique_ptr<Camera> Camera::FromDataForCamera0(
136     std::unique_ptr<CameraParams> params,
137     std::vector<std::unique_ptr<Item>>* items) {
138   if (params->image == nullptr) {
139     params->image = Image::FromDataForPrimaryImage(kImageJpegMime, items);
140   }
141   return std::unique_ptr<Camera>(new Camera(std::move(params)));  // NOLINT
142 }
143 
FromData(std::unique_ptr<CameraParams> params)144 std::unique_ptr<Camera> Camera::FromData(std::unique_ptr<CameraParams> params) {
145   if (params->image == nullptr) {
146     LOG(ERROR) << "Camera must have an image eleemnt";
147     return nullptr;
148   }
149 
150   return std::unique_ptr<Camera>(new Camera(std::move(params)));  // NOLINT
151 }
152 
FromDeserializer(const Deserializer & parent_deserializer)153 std::unique_ptr<Camera> Camera::FromDeserializer(
154     const Deserializer& parent_deserializer) {
155   std::unique_ptr<Deserializer> deserializer =
156       parent_deserializer.CreateDeserializer(
157           DynamicDepthConst::Namespace(DynamicDepthConst::Camera()),
158           DynamicDepthConst::Camera());
159   if (deserializer == nullptr) {
160     return nullptr;
161   }
162 
163   return ParseFields(*deserializer);
164 }
165 
GetImage() const166 const Image* Camera::GetImage() const { return params_->image.get(); }
167 
GetLightEstimate() const168 const LightEstimate* Camera::GetLightEstimate() const {
169   return params_->light_estimate.get();
170 }
171 
GetPose() const172 const Pose* Camera::GetPose() const { return params_->pose.get(); }
173 
GetDepthMap() const174 const DepthMap* Camera::GetDepthMap() const { return params_->depth_map.get(); }
175 
GetImagingModel() const176 const ImagingModel* Camera::GetImagingModel() const {
177   return params_->imaging_model.get();
178 }
179 
GetPointCloud() const180 const PointCloud* Camera::GetPointCloud() const {
181   return params_->point_cloud.get();
182 }
183 
GetVendorInfo() const184 const VendorInfo* Camera::GetVendorInfo() const {
185   return params_->vendor_info.get();
186 }
187 
GetAppInfo() const188 const AppInfo* Camera::GetAppInfo() const { return params_->app_info.get(); }
189 
GetTrait() const190 CameraTrait Camera::GetTrait() const { return params_->trait; }
191 
Serialize(Serializer * serializer) const192 bool Camera::Serialize(Serializer* serializer) const {
193   if (serializer == nullptr) {
194     LOG(ERROR) << "Serializer is null";
195     return false;
196   }
197 
198   if (params_->trait != CameraTrait::NONE) {
199     string trait_name = TraitToString(params_->trait);
200     serializer->WriteProperty(DynamicDepthConst::Camera(), kTrait, trait_name);
201   }
202 
203   // Error checking has already been done at instantiation time.
204   if (params_->image != nullptr) {
205     std::unique_ptr<Serializer> image_serializer = serializer->CreateSerializer(
206         DynamicDepthConst::Namespace(DynamicDepthConst::Image()),
207         DynamicDepthConst::Image());
208     if (!params_->image->Serialize(image_serializer.get())) {
209       LOG(WARNING) << "Could not serialize Image";
210     }
211   }
212 
213   if (params_->depth_map != nullptr) {
214     std::unique_ptr<Serializer> depth_map_serializer =
215         serializer->CreateSerializer(DynamicDepthConst::Camera(),
216                                      DynamicDepthConst::DepthMap());
217     if (!params_->depth_map->Serialize(depth_map_serializer.get())) {
218       LOG(WARNING) << "Could not serializer Depth Map";
219     }
220   }
221 
222   if (params_->light_estimate != nullptr) {
223     std::unique_ptr<Serializer> light_estimate_serializer =
224         serializer->CreateSerializer(
225             DynamicDepthConst::Namespace(DynamicDepthConst::LightEstimate()),
226             DynamicDepthConst::LightEstimate());
227     if (!params_->light_estimate->Serialize(light_estimate_serializer.get())) {
228       LOG(WARNING) << "Could not serialize LightEstimate";
229     }
230   }
231 
232   if (params_->pose != nullptr) {
233     std::unique_ptr<Serializer> pose_serializer = serializer->CreateSerializer(
234         DynamicDepthConst::Camera(), DynamicDepthConst::Pose());
235     if (!params_->pose->Serialize(pose_serializer.get())) {
236       LOG(WARNING) << "Could not serialize Pose";
237     }
238   }
239 
240   if (params_->imaging_model != nullptr) {
241     std::unique_ptr<Serializer> imaging_model_serializer =
242         serializer->CreateSerializer(
243             DynamicDepthConst::Namespace(DynamicDepthConst::ImagingModel()),
244             DynamicDepthConst::ImagingModel());
245     if (!params_->imaging_model->Serialize(imaging_model_serializer.get())) {
246       LOG(WARNING) << "Could not serialize ImagingModel";
247     }
248   }
249 
250   if (params_->point_cloud != nullptr) {
251     std::unique_ptr<Serializer> point_cloud_serializer =
252         serializer->CreateSerializer(DynamicDepthConst::Camera(),
253                                      DynamicDepthConst::PointCloud());
254     if (!params_->point_cloud->Serialize(point_cloud_serializer.get())) {
255       LOG(WARNING) << "Could not serialize PointCloud";
256     }
257   }
258 
259   if (params_->vendor_info != nullptr) {
260     std::unique_ptr<Serializer> vendor_info_serializer =
261         serializer->CreateSerializer(DynamicDepthConst::Camera(),
262                                      DynamicDepthConst::VendorInfo());
263     if (!params_->vendor_info->Serialize(vendor_info_serializer.get())) {
264       LOG(WARNING) << "Could not serialize VendorInfo";
265     }
266   }
267 
268   if (params_->app_info != nullptr) {
269     std::unique_ptr<Serializer> app_info_serializer =
270         serializer->CreateSerializer(DynamicDepthConst::Camera(),
271                                      DynamicDepthConst::AppInfo());
272     if (!params_->app_info->Serialize(app_info_serializer.get())) {
273       LOG(WARNING) << "Could not serialize AppInfo";
274     }
275   }
276 
277   return true;
278 }
279 
280 }  // namespace dynamic_depth
281