xref: /aosp_15_r20/external/dynamic_depth/internal/dynamic_depth/light_estimate.cc (revision a62be0856e8e1158f43b03e41bbad10f4d005fde)
1 #include "dynamic_depth/light_estimate.h"
2 
3 #include "android-base/logging.h"
4 #include "dynamic_depth/const.h"
5 #include "xmpmeta/base64.h"
6 
7 using ::dynamic_depth::xmpmeta::xml::Deserializer;
8 using ::dynamic_depth::xmpmeta::xml::Serializer;
9 
10 namespace dynamic_depth {
11 namespace {
12 constexpr int kColorCorrectionSize = 3;
13 
14 constexpr char kPixelIntensity[] = "PixelIntensity";
15 constexpr char kColorCorrectionR[] = "ColorCorrectionR";
16 constexpr char kColorCorrectionG[] = "ColorCorrectionG";
17 constexpr char kColorCorrectionB[] = "ColorCorrectionB";
18 
19 constexpr char kNamespaceHref[] =
20     "http://ns.google.com/photos/dd/1.0/lightestimate/";
21 
22 }  // namespace
23 
24 // Private constructor.
LightEstimate()25 LightEstimate::LightEstimate() {}
26 
27 // Public methods.
GetNamespaces(std::unordered_map<string,string> * ns_name_href_map)28 void LightEstimate::GetNamespaces(
29     std::unordered_map<string, string>* ns_name_href_map) {
30   if (ns_name_href_map == nullptr) {
31     LOG(ERROR) << "Namespace list or own namespace is null";
32     return;
33   }
34   ns_name_href_map->emplace(DynamicDepthConst::LightEstimate(), kNamespaceHref);
35 }
36 
FromData(float pixel_intensity)37 std::unique_ptr<LightEstimate> LightEstimate::FromData(float pixel_intensity) {
38   return LightEstimate::FromData(pixel_intensity, {1.0f, 1.0f, 1.0f});
39 }
40 
FromData(float pixel_intensity,const std::vector<float> & color_correction)41 std::unique_ptr<LightEstimate> LightEstimate::FromData(
42     float pixel_intensity, const std::vector<float>& color_correction) {
43   // Priate constructor.
44   std::unique_ptr<LightEstimate> light_estimate(
45       std::unique_ptr<LightEstimate>(new LightEstimate()));  // NOLINT
46   light_estimate->pixel_intensity_ = pixel_intensity;
47 
48   if (color_correction.size() >= kColorCorrectionSize) {
49     std::copy(color_correction.begin(),
50               color_correction.begin() + kColorCorrectionSize,
51               light_estimate->color_correction_.begin());
52   } else {
53     LOG(WARNING) << "Color correction had fewer than three values, "
54                  << "reverting to default of 1.0 for all RGB values";
55   }
56 
57   return light_estimate;
58 }
59 
GetPixelIntensity() const60 float LightEstimate::GetPixelIntensity() const { return pixel_intensity_; }
61 
GetColorCorrection() const62 const std::vector<float>& LightEstimate::GetColorCorrection() const {
63   return color_correction_;
64 }
65 
FromDeserializer(const Deserializer & parent_deserializer)66 std::unique_ptr<LightEstimate> LightEstimate::FromDeserializer(
67     const Deserializer& parent_deserializer) {
68   std::unique_ptr<Deserializer> deserializer =
69       parent_deserializer.CreateDeserializer(
70           DynamicDepthConst::Namespace(DynamicDepthConst::LightEstimate()),
71           DynamicDepthConst::LightEstimate());
72   if (deserializer == nullptr) {
73     return nullptr;
74   }
75 
76   std::unique_ptr<LightEstimate> light_estimate(
77       std::unique_ptr<LightEstimate>(new LightEstimate()));  // NOLINT
78   if (!deserializer->ParseFloat(DynamicDepthConst::LightEstimate(),
79                                 kPixelIntensity,
80                                 &light_estimate->pixel_intensity_)) {
81     return nullptr;
82   }
83 
84   float color_correction_r;
85   float color_correction_g;
86   float color_correction_b;
87   if (deserializer->ParseFloat(DynamicDepthConst::LightEstimate(),
88                                kColorCorrectionR, &color_correction_r) &&
89       deserializer->ParseFloat(DynamicDepthConst::LightEstimate(),
90                                kColorCorrectionG, &color_correction_g) &&
91       deserializer->ParseFloat(DynamicDepthConst::LightEstimate(),
92                                kColorCorrectionB, &color_correction_b)) {
93     light_estimate->color_correction_[0] = color_correction_r;
94     light_estimate->color_correction_[1] = color_correction_g;
95     light_estimate->color_correction_[2] = color_correction_b;
96   }
97 
98   return light_estimate;
99 }
100 
Serialize(Serializer * serializer) const101 bool LightEstimate::Serialize(Serializer* serializer) const {
102   if (serializer == nullptr) {
103     LOG(ERROR) << "Serializer is null";
104     return false;
105   }
106 
107   if (!serializer->WriteProperty(DynamicDepthConst::LightEstimate(),
108                                  kPixelIntensity,
109                                  std::to_string(pixel_intensity_))) {
110     return false;
111   }
112 
113   CHECK(color_correction_.size() >= 3)
114       << "Color correction not initialized to a size-3 vector";
115   return serializer->WriteProperty(DynamicDepthConst::LightEstimate(),
116                                    kColorCorrectionR,
117                                    std::to_string(color_correction_[0])) &&
118          serializer->WriteProperty(DynamicDepthConst::LightEstimate(),
119                                    kColorCorrectionG,
120                                    std::to_string(color_correction_[1])) &&
121          serializer->WriteProperty(DynamicDepthConst::LightEstimate(),
122                                    kColorCorrectionB,
123                                    std::to_string(color_correction_[2]));
124 }
125 
126 }  // namespace dynamic_depth
127