Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Make Map.addImage tests pass (#8843)
Browse files Browse the repository at this point in the history
Fix Node tests suite implementation to parse pixelRatio from style json
Premultiply images in the node binding before sending to mbgl core
  • Loading branch information
Asheem Mamoowala authored May 2, 2017
1 parent 3f0c89d commit 5c52401
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmake/core-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion include/mbgl/util/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ class Image : private util::noncopyable {

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;
}

Expand Down
File renamed without changes.
6 changes: 4 additions & 2 deletions platform/node/src/node_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <mbgl/style/image.hpp>
#include <mbgl/map/backend_scope.hpp>
#include <mbgl/map/query.hpp>
#include <mbgl/util/premultiply.hpp>

#include <unistd.h>

Expand Down Expand Up @@ -682,8 +683,9 @@ void NodeMap::AddImage(const Nan::FunctionCallbackInfo<v8::Value>& info) {

std::unique_ptr<uint8_t[]> data = std::make_unique<uint8_t[]>(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<mbgl::style::Image>(std::move(cPremultipliedImage), pixelRatio));
}

Expand Down
2 changes: 1 addition & 1 deletion platform/node/test/suite_implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions test/util/image.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5c52401

Please sign in to comment.