1 #include <mbgl/util/image.hpp>
2 #include <mbgl/util/premultiply.hpp>
3 #include <mbgl/util/logging.hpp>
4
5 extern "C"
6 {
7 #include <webp/decode.h>
8 }
9
10 namespace mbgl {
11
decodeWebP(const uint8_t * data,size_t size)12 PremultipliedImage decodeWebP(const uint8_t* data, size_t size) {
13 int width = 0, height = 0;
14 if (WebPGetInfo(data, size, &width, &height) == 0) {
15 throw std::runtime_error("failed to retrieve WebP basic header information");
16 }
17
18 int stride = width * 4;
19 size_t webpSize = stride * height;
20 auto webp = std::make_unique<uint8_t[]>(webpSize);
21
22 if (!WebPDecodeRGBAInto(data, size, webp.get(), webpSize, stride)) {
23 throw std::runtime_error("failed to decode WebP data");
24 }
25
26 UnassociatedImage image({ static_cast<uint32_t>(width), static_cast<uint32_t>(height) },
27 std::move(webp));
28 return util::premultiply(std::move(image));
29 }
30
31 } // namespace mbgl
32