diff --git a/gyp/platform-linux.gypi b/gyp/platform-linux.gypi index 79cd78074..d43915863 100644 --- a/gyp/platform-linux.gypi +++ b/gyp/platform-linux.gypi @@ -17,6 +17,7 @@ '../platform/default/application_root.cpp', '../platform/default/thread.cpp', '../platform/default/image.cpp', + '../platform/default/webp_reader.cpp', '../platform/default/png_reader.cpp', '../platform/default/jpeg_reader.cpp', '../platform/default/timer.cpp', diff --git a/include/mbgl/util/image.hpp b/include/mbgl/util/image.hpp index fde42c7c5..7927b777e 100644 --- a/include/mbgl/util/image.hpp +++ b/include/mbgl/util/image.hpp @@ -21,6 +21,11 @@ public: height(h), data(std::make_unique(size())) {} + Image(size_t w, size_t h, std::unique_ptr data_) + : width(w), + height(h), + data(std::move(data_)) {} + size_t stride() const { return width * 4; } size_t size() const { return width * height * 4; } diff --git a/platform/default/image.cpp b/platform/default/image.cpp index bf8071af5..71fb5414b 100644 --- a/platform/default/image.cpp +++ b/platform/default/image.cpp @@ -70,6 +70,10 @@ std::string encodePNG(const PremultipliedImage& pre) { return result; } +#if !defined(__ANDROID__) && !defined(__APPLE__) +PremultipliedImage decodeWebP(const uint8_t*, size_t); +#endif // !defined(__ANDROID__) && !defined(__APPLE__) + PremultipliedImage decodePNG(const uint8_t*, size_t); PremultipliedImage decodeJPEG(const uint8_t*, size_t); @@ -77,16 +81,26 @@ PremultipliedImage decodeImage(const std::string& string) { const uint8_t* data = reinterpret_cast(string.data()); const size_t size = string.size(); +#if !defined(__ANDROID__) && !defined(__APPLE__) + if (size >= 12) { + uint32_t riff_magic = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; + uint32_t webp_magic = (data[8] << 24) | (data[9] << 16) | (data[10] << 8) | data[11]; + if (riff_magic == 0x52494646 && webp_magic == 0x57454250) { + return decodeWebP(data, size); + } + } +#endif // !defined(__ANDROID__) && !defined(__APPLE__) + if (size >= 4) { - unsigned int magic = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; + uint32_t magic = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; if (magic == 0x89504E47U) { return decodePNG(data, size); } } if (size >= 2) { - unsigned int magic = ((data[0] << 8) | data[1]) & 0xffff; - if (magic == 0xffd8) { + uint16_t magic = ((data[0] << 8) | data[1]) & 0xffff; + if (magic == 0xFFD8) { return decodeJPEG(data, size); } } diff --git a/platform/default/webp_reader.cpp b/platform/default/webp_reader.cpp new file mode 100644 index 000000000..37d23da11 --- /dev/null +++ b/platform/default/webp_reader.cpp @@ -0,0 +1,27 @@ +#include +#include +#include + +extern "C" +{ +#include +} + +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"); + } + + std::unique_ptr webp(WebPDecodeRGBA(data, size, &width, &height)); + if (!webp) { + 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 diff --git a/test/fixtures/image/tile.webp b/test/fixtures/image/tile.webp new file mode 100644 index 000000000..5dbad8f88 Binary files /dev/null and b/test/fixtures/image/tile.webp differ diff --git a/test/miscellaneous/image.cpp b/test/miscellaneous/image.cpp index 2db62b96d..9886eede4 100644 --- a/test/miscellaneous/image.cpp +++ b/test/miscellaneous/image.cpp @@ -78,6 +78,14 @@ TEST(Image, JPEGTile) { EXPECT_EQ(256, image.height); } +#if !defined(__ANDROID__) && !defined(__APPLE__) +TEST(Image, WebPTile) { + PremultipliedImage image = decodeImage(util::read_file("test/fixtures/image/tile.webp")); + EXPECT_EQ(256, image.width); + EXPECT_EQ(256, image.height); +} +#endif // !defined(__ANDROID__) && !defined(__APPLE__) + TEST(Image, Premultiply) { UnassociatedImage rgba { 1, 1 }; rgba.data[0] = 255;