summaryrefslogtreecommitdiff
path: root/src/mbgl/util/premultiply.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mbgl/util/premultiply.cpp')
-rw-r--r--src/mbgl/util/premultiply.cpp35
1 files changed, 29 insertions, 6 deletions
diff --git a/src/mbgl/util/premultiply.cpp b/src/mbgl/util/premultiply.cpp
index 3ee5a1e129..8a62012251 100644
--- a/src/mbgl/util/premultiply.cpp
+++ b/src/mbgl/util/premultiply.cpp
@@ -6,14 +6,14 @@ namespace mbgl {
namespace util {
PremultipliedImage premultiply(UnassociatedImage&& src) {
- PremultipliedImage dst;
+ PremultipliedImage dst;
- dst.width = src.width;
- dst.height = src.height;
- dst.data = std::move(src.data);
+ dst.width = src.width;
+ dst.height = src.height;
+ dst.data = std::move(src.data);
- uint8_t* data = dst.data.get();
- for (size_t i = 0; i < dst.size(); i += 4) {
+ uint8_t* data = dst.data.get();
+ for (size_t i = 0; i < dst.size(); i += 4) {
uint8_t& r = data[i + 0];
uint8_t& g = data[i + 1];
uint8_t& b = data[i + 2];
@@ -26,5 +26,28 @@ PremultipliedImage premultiply(UnassociatedImage&& src) {
return std::move(dst);
}
+UnassociatedImage unpremultiply(PremultipliedImage&& src) {
+ UnassociatedImage dst;
+
+ dst.width = src.width;
+ dst.height = src.height;
+ dst.data = std::move(src.data);
+
+ uint8_t* data = dst.data.get();
+ for (size_t i = 0; i < dst.size(); i += 4) {
+ uint8_t& r = data[i + 0];
+ uint8_t& g = data[i + 1];
+ uint8_t& b = data[i + 2];
+ uint8_t& a = data[i + 3];
+ if (a) {
+ r = (255 * r + (a / 2)) / a;
+ g = (255 * g + (a / 2)) / a;
+ b = (255 * b + (a / 2)) / a;
+ }
+ }
+
+ return std::move(dst);
+}
+
}
}