From 5c52401d5504ab4b84d1510042c6b97504c50933 Mon Sep 17 00:00:00 2001 From: Asheem Mamoowala Date: Tue, 2 May 2017 09:54:06 -0700 Subject: [core] Make Map.addImage tests pass (#8843) Fix Node tests suite implementation to parse pixelRatio from style json Premultiply images in the node binding before sending to mbgl core --- cmake/core-files.cmake | 2 +- include/mbgl/util/image.hpp | 5 ++++- include/mbgl/util/premultiply.hpp | 12 ++++++++++++ mapbox-gl-js | 2 +- platform/node/src/node_map.cpp | 6 ++++-- platform/node/test/suite_implementation.js | 2 +- src/mbgl/util/premultiply.hpp | 12 ------------ test/util/image.test.cpp | 15 +++++++++++++++ 8 files changed, 38 insertions(+), 18 deletions(-) create mode 100644 include/mbgl/util/premultiply.hpp delete mode 100644 src/mbgl/util/premultiply.hpp diff --git a/cmake/core-files.cmake b/cmake/core-files.cmake index 3594b55be6..17498ec427 100644 --- a/cmake/core-files.cmake +++ b/cmake/core-files.cmake @@ -511,6 +511,7 @@ set(MBGL_CORE_FILES include/mbgl/util/noncopyable.hpp include/mbgl/util/optional.hpp include/mbgl/util/platform.hpp + include/mbgl/util/premultiply.hpp include/mbgl/util/projection.hpp include/mbgl/util/range.hpp include/mbgl/util/run_loop.hpp @@ -565,7 +566,6 @@ set(MBGL_CORE_FILES src/mbgl/util/offscreen_texture.cpp src/mbgl/util/offscreen_texture.hpp src/mbgl/util/premultiply.cpp - src/mbgl/util/premultiply.hpp src/mbgl/util/rapidjson.hpp src/mbgl/util/rect.hpp src/mbgl/util/std.hpp diff --git a/include/mbgl/util/image.hpp b/include/mbgl/util/image.hpp index c019bb949c..a41b8462bd 100644 --- a/include/mbgl/util/image.hpp +++ b/include/mbgl/util/image.hpp @@ -40,11 +40,14 @@ public: Image(Image&& o) : size(o.size), - data(std::move(o.data)) {} + data(std::move(o.data)) { + o.size.width = o.size.height = 0; + } Image& operator=(Image&& o) { size = o.size; data = std::move(o.data); + o.size.width = o.size.height = 0; return *this; } diff --git a/include/mbgl/util/premultiply.hpp b/include/mbgl/util/premultiply.hpp new file mode 100644 index 0000000000..532c7cb11d --- /dev/null +++ b/include/mbgl/util/premultiply.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include + +namespace mbgl { +namespace util { + +PremultipliedImage premultiply(UnassociatedImage&&); +UnassociatedImage unpremultiply(PremultipliedImage&&); + +} // namespace util +} // namespace mbgl diff --git a/mapbox-gl-js b/mapbox-gl-js index d3b39cbba4..431bce18e1 160000 --- a/mapbox-gl-js +++ b/mapbox-gl-js @@ -1 +1 @@ -Subproject commit d3b39cbba4f09d26ef51159aa56270e67006c51b +Subproject commit 431bce18e1b3a26851b8e5037b1017ba35c41117 diff --git a/platform/node/src/node_map.cpp b/platform/node/src/node_map.cpp index ab49ca6405..1c8bf6d1d2 100644 --- a/platform/node/src/node_map.cpp +++ b/platform/node/src/node_map.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include @@ -682,8 +683,9 @@ void NodeMap::AddImage(const Nan::FunctionCallbackInfo& info) { std::unique_ptr data = std::make_unique(imageLength); std::copy(imageDataBuffer, imageDataBuffer + imageLength, data.get()); - mbgl::PremultipliedImage cPremultipliedImage({ imageWidth, imageHeight}, std::move(data)); - + + mbgl::UnassociatedImage cImage({ imageWidth, imageHeight}, std::move(data)); + mbgl::PremultipliedImage cPremultipliedImage = mbgl::util::premultiply(std::move(cImage)); nodeMap->map->addImage(*Nan::Utf8String(info[0]), std::make_unique(std::move(cPremultipliedImage), pixelRatio)); } diff --git a/platform/node/test/suite_implementation.js b/platform/node/test/suite_implementation.js index a5e96f7265..8ac372b7c3 100644 --- a/platform/node/test/suite_implementation.js +++ b/platform/node/test/suite_implementation.js @@ -76,7 +76,7 @@ module.exports = function (style, options, callback) { map.addImage(operation[1], img.data, { height: img.height, width: img.width, - pixelRatio: 1 + pixelRatio: operation[3] || 1 }); applyOperations(operations.slice(1), callback); diff --git a/src/mbgl/util/premultiply.hpp b/src/mbgl/util/premultiply.hpp deleted file mode 100644 index 532c7cb11d..0000000000 --- a/src/mbgl/util/premultiply.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -namespace mbgl { -namespace util { - -PremultipliedImage premultiply(UnassociatedImage&&); -UnassociatedImage unpremultiply(PremultipliedImage&&); - -} // namespace util -} // namespace mbgl diff --git a/test/util/image.test.cpp b/test/util/image.test.cpp index 0cd4a7d8af..4cacf89253 100644 --- a/test/util/image.test.cpp +++ b/test/util/image.test.cpp @@ -115,6 +115,21 @@ TEST(Image, Copy) { EXPECT_THROW(PremultipliedImage::copy(src10, dst10, {0, 0}, {0, 1}, {0, max}), std::out_of_range); } +TEST(Image, Move) { + UnassociatedImage rgba({ 1, 1 }); + rgba.data[0] = 255; + rgba.data[1] = 254; + rgba.data[2] = 253; + rgba.data[3] = 128; + + auto moved = std::move(rgba); + + EXPECT_EQ(0u, rgba.size.width); + EXPECT_EQ(nullptr, rgba.data.get()); + EXPECT_EQ(254, moved.data[1]); + EXPECT_EQ(1u, moved.size.width); +} + TEST(Image, Premultiply) { UnassociatedImage rgba({ 1, 1 }); rgba.data[0] = 255; -- cgit v1.2.1