summaryrefslogtreecommitdiff
path: root/platform/default/webp_reader.cpp
blob: 5b0eaf47419938e3b416dcaf5aa62bad5589afdd (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/image.hpp>
#include <mbgl/util/premultiply.hpp>
#include <mbgl/platform/log.hpp>

extern "C"
{
#include <webp/decode.h>
}

namespace mbgl {

PremultipliedImage decodeWebP(const uint8_t* data, size_t size) {
    int width = 0, height = 0;
    if (WebPGetInfo(data, size, &width, &height) == 0) {
        throw std::runtime_error("failed to retrieve WebP basic header information");
    }

    int stride = width * 4;
    size_t webpSize = stride * height;
    auto webp = std::make_unique<uint8_t[]>(webpSize);

    if (!WebPDecodeRGBAInto(data, size, webp.get(), webpSize, stride)) {
        throw std::runtime_error("failed to decode WebP data");
    }

    UnassociatedImage image { size_t(width), size_t(height), std::move(webp) };
    return util::premultiply(std::move(image));
}

} // namespace mbgl