diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2016-01-19 16:32:11 +0200 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2016-01-26 13:13:08 +0200 |
commit | b9cf3f0d1ee555d3524077de1275602b2028f0b2 (patch) | |
tree | f8b1017cd8afdefe079d6ce06b4ef48a671a17e3 | |
parent | 70966b060115706a0620a6122916b2a995badd8a (diff) | |
download | qtlocation-mapboxgl-upstream/webp-darwin.tar.gz |
[darwin] Added WebP tile supportupstream/webp-darwin
-rw-r--r-- | platform/darwin/image.mm | 17 | ||||
-rw-r--r-- | platform/default/image.cpp | 8 | ||||
-rw-r--r-- | src/mbgl/util/mapbox.cpp | 4 | ||||
-rw-r--r-- | test/miscellaneous/image.cpp | 4 | ||||
-rw-r--r-- | test/miscellaneous/mapbox.cpp | 4 |
5 files changed, 26 insertions, 11 deletions
diff --git a/platform/darwin/image.mm b/platform/darwin/image.mm index 854b9157f8..e984b0688a 100644 --- a/platform/darwin/image.mm +++ b/platform/darwin/image.mm @@ -2,6 +2,8 @@ #import <ImageIO/ImageIO.h> +#import <webp/decode.h> + #if TARGET_OS_IPHONE #import <MobileCoreServices/MobileCoreServices.h> #else @@ -65,8 +67,21 @@ std::string encodePNG(const PremultipliedImage& src) { return result; } +PremultipliedImage decodeWebP(const uint8_t*, size_t); + PremultipliedImage decodeImage(const std::string &source_data) { - CFDataRef data = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const unsigned char *>(source_data.data()), source_data.size(), kCFAllocatorNull); + // CoreFoundation does not decode WebP natively. + size_t size = source_data.size(); + if (size >= 12) { + const uint8_t* data = reinterpret_cast<const uint8_t*>(source_data.data()); + 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); + } + } + + CFDataRef data = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const unsigned char *>(source_data.data()), size, kCFAllocatorNull); if (!data) { throw std::runtime_error("CFDataCreateWithBytesNoCopy failed"); } diff --git a/platform/default/image.cpp b/platform/default/image.cpp index 71fb5414b3..db920eb0d3 100644 --- a/platform/default/image.cpp +++ b/platform/default/image.cpp @@ -70,9 +70,9 @@ std::string encodePNG(const PremultipliedImage& pre) { return result; } -#if !defined(__ANDROID__) && !defined(__APPLE__) +#if !defined(__ANDROID__) PremultipliedImage decodeWebP(const uint8_t*, size_t); -#endif // !defined(__ANDROID__) && !defined(__APPLE__) +#endif // !defined(__ANDROID__) PremultipliedImage decodePNG(const uint8_t*, size_t); PremultipliedImage decodeJPEG(const uint8_t*, size_t); @@ -81,7 +81,7 @@ PremultipliedImage decodeImage(const std::string& string) { const uint8_t* data = reinterpret_cast<const uint8_t*>(string.data()); const size_t size = string.size(); -#if !defined(__ANDROID__) && !defined(__APPLE__) +#if !defined(__ANDROID__) 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]; @@ -89,7 +89,7 @@ PremultipliedImage decodeImage(const std::string& string) { return decodeWebP(data, size); } } -#endif // !defined(__ANDROID__) && !defined(__APPLE__) +#endif // !defined(__ANDROID__) if (size >= 4) { uint32_t magic = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; diff --git a/src/mbgl/util/mapbox.cpp b/src/mbgl/util/mapbox.cpp index d94a9a5f7d..cbb2f19cff 100644 --- a/src/mbgl/util/mapbox.cpp +++ b/src/mbgl/util/mapbox.cpp @@ -129,12 +129,12 @@ std::string normalizeRasterTileURL(const std::string& url) { } std::string normalizedURL(url); -#if !defined(__ANDROID__) && !defined(__APPLE__) +#if !defined(__ANDROID__) // Replace PNG with WebP. if (normalizedURL.compare(extensionIdx + 1, 3, "png") == 0) { normalizedURL.replace(extensionIdx + 1, 3, "webp"); } -#endif // !defined(__ANDROID__) && !defined(__APPLE__) +#endif // !defined(__ANDROID__) normalizedURL.insert(extensionIdx, "{ratio}"); return normalizedURL; } diff --git a/test/miscellaneous/image.cpp b/test/miscellaneous/image.cpp index 9886eede45..66c3b85339 100644 --- a/test/miscellaneous/image.cpp +++ b/test/miscellaneous/image.cpp @@ -78,13 +78,13 @@ TEST(Image, JPEGTile) { EXPECT_EQ(256, image.height); } -#if !defined(__ANDROID__) && !defined(__APPLE__) +#if !defined(__ANDROID__) 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__) +#endif // !defined(__ANDROID__) TEST(Image, Premultiply) { UnassociatedImage rgba { 1, 1 }; diff --git a/test/miscellaneous/mapbox.cpp b/test/miscellaneous/mapbox.cpp index d6f9948e66..809182d70a 100644 --- a/test/miscellaneous/mapbox.cpp +++ b/test/miscellaneous/mapbox.cpp @@ -37,7 +37,7 @@ TEST(Mapbox, SpriteURL) { TEST(Mapbox, TileURL) { try { -#if defined(__ANDROID__) || defined(__APPLE__) +#if defined(__ANDROID__) EXPECT_EQ("http://path.png/tile{ratio}.png", mbgl::util::mapbox::normalizeRasterTileURL("http://path.png/tile.png")); EXPECT_EQ("http://path.png/tile{ratio}.png32", mbgl::util::mapbox::normalizeRasterTileURL("http://path.png/tile.png32")); EXPECT_EQ("http://path.png/tile{ratio}.png70", mbgl::util::mapbox::normalizeRasterTileURL("http://path.png/tile.png70")); @@ -47,7 +47,7 @@ TEST(Mapbox, TileURL) { EXPECT_EQ("http://path.png/tile{ratio}.webp32", mbgl::util::mapbox::normalizeRasterTileURL("http://path.png/tile.png32")); EXPECT_EQ("http://path.png/tile{ratio}.webp70", mbgl::util::mapbox::normalizeRasterTileURL("http://path.png/tile.png70")); EXPECT_EQ("http://path.png/tile{ratio}.webp?access_token=foo", mbgl::util::mapbox::normalizeRasterTileURL("http://path.png/tile.png?access_token=foo")); -#endif // defined(__ANDROID__) || defined(__APPLE__) +#endif // defined(__ANDROID__) EXPECT_EQ("http://path.png/tile{ratio}.pbf", mbgl::util::mapbox::normalizeRasterTileURL("http://path.png/tile.pbf")); EXPECT_EQ("http://path.png/tile{ratio}.pbf?access_token=foo", mbgl::util::mapbox::normalizeRasterTileURL("http://path.png/tile.pbf?access_token=foo")); EXPECT_EQ("http://path.png/tile{ratio}.pbf?access_token=foo.png", mbgl::util::mapbox::normalizeRasterTileURL("http://path.png/tile.pbf?access_token=foo.png")); |