summaryrefslogtreecommitdiff
path: root/src/mbgl/util/premultiply.cpp
blob: 3ee5a1e1299f7ace9f5a8691be79d7b8d8124ae3 (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
#include <mbgl/util/premultiply.hpp>

#include <cmath>

namespace mbgl {
namespace util {

PremultipliedImage premultiply(UnassociatedImage&& src) {
   PremultipliedImage 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];
        r = (r * a + 127) / 255;
        g = (g * a + 127) / 255;
        b = (b * a + 127) / 255;
    }

    return std::move(dst);
}

}
}