summaryrefslogtreecommitdiff
path: root/internal/dynamic_depth/light_estimate.cc
blob: 9ce3babff4dc16abd2470e802aed5b7f44292c32 (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
#include "dynamic_depth/light_estimate.h"

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

using photos_editing_formats::xml::Deserializer;
using photos_editing_formats::xml::Serializer;

namespace photos_editing_formats {
namespace dynamic_depth {
namespace {
constexpr int kColorCorrectionSize = 3;

constexpr char kPixelIntensity[] = "PixelIntensity";
constexpr char kColorCorrectionR[] = "ColorCorrectionR";
constexpr char kColorCorrectionG[] = "ColorCorrectionG";
constexpr char kColorCorrectionB[] = "ColorCorrectionB";

constexpr char kNamespaceHref[] =
    "http://ns.google.com/photos/dd/1.0/lightestimate/";

}  // namespace

// Private constructor.
LightEstimate::LightEstimate() {}

// Public methods.
void LightEstimate::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::LightEstimate(), kNamespaceHref);
}

std::unique_ptr<LightEstimate> LightEstimate::FromData(float pixel_intensity) {
  return LightEstimate::FromData(pixel_intensity, {1.0f, 1.0f, 1.0f});
}

std::unique_ptr<LightEstimate> LightEstimate::FromData(
    float pixel_intensity, const std::vector<float>& color_correction) {
  // Priate constructor.
  std::unique_ptr<LightEstimate> light_estimate(
      std::unique_ptr<LightEstimate>(new LightEstimate()));  // NOLINT
  light_estimate->pixel_intensity_ = pixel_intensity;

  if (color_correction.size() >= kColorCorrectionSize) {
    std::copy(color_correction.begin(),
              color_correction.begin() + kColorCorrectionSize,
              light_estimate->color_correction_.begin());
  } else {
    LOG(WARNING) << "Color correction had fewer than three values, "
                 << "reverting to default of 1.0 for all RGB values";
  }

  return light_estimate;
}

float LightEstimate::GetPixelIntensity() const { return pixel_intensity_; }

const std::vector<float>& LightEstimate::GetColorCorrection() const {
  return color_correction_;
}

std::unique_ptr<LightEstimate> LightEstimate::FromDeserializer(
    const Deserializer& parent_deserializer) {
  std::unique_ptr<Deserializer> deserializer =
      parent_deserializer.CreateDeserializer(
          DynamicDepthConst::Namespace(DynamicDepthConst::LightEstimate()),
          DynamicDepthConst::LightEstimate());
  if (deserializer == nullptr) {
    return nullptr;
  }

  std::unique_ptr<LightEstimate> light_estimate(
      std::unique_ptr<LightEstimate>(new LightEstimate()));  // NOLINT
  if (!deserializer->ParseFloat(DynamicDepthConst::LightEstimate(),
                                kPixelIntensity,
                                &light_estimate->pixel_intensity_)) {
    return nullptr;
  }

  float color_correction_r;
  float color_correction_g;
  float color_correction_b;
  if (deserializer->ParseFloat(DynamicDepthConst::LightEstimate(),
                               kColorCorrectionR, &color_correction_r) &&
      deserializer->ParseFloat(DynamicDepthConst::LightEstimate(),
                               kColorCorrectionG, &color_correction_g) &&
      deserializer->ParseFloat(DynamicDepthConst::LightEstimate(),
                               kColorCorrectionB, &color_correction_b)) {
    light_estimate->color_correction_[0] = color_correction_r;
    light_estimate->color_correction_[1] = color_correction_g;
    light_estimate->color_correction_[2] = color_correction_b;
  }

  return light_estimate;
}

bool LightEstimate::Serialize(Serializer* serializer) const {
  if (serializer == nullptr) {
    LOG(ERROR) << "Serializer is null";
    return false;
  }

  if (!serializer->WriteProperty(DynamicDepthConst::LightEstimate(),
                                 kPixelIntensity,
                                 std::to_string(pixel_intensity_))) {
    return false;
  }

  CHECK(color_correction_.size() >= 3)
      << "Color correction not initialized to a size-3 vector";
  return serializer->WriteProperty(DynamicDepthConst::LightEstimate(),
                                   kColorCorrectionR,
                                   std::to_string(color_correction_[0])) &&
         serializer->WriteProperty(DynamicDepthConst::LightEstimate(),
                                   kColorCorrectionG,
                                   std::to_string(color_correction_[1])) &&
         serializer->WriteProperty(DynamicDepthConst::LightEstimate(),
                                   kColorCorrectionB,
                                   std::to_string(color_correction_[2]));
}

}  // namespace dynamic_depth
}  // namespace photos_editing_formats