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

Update node bindings to support addImage with sdf images #15054

Closed
riastrad opened this issue Jul 3, 2019 · 0 comments · Fixed by #15057
Closed

Update node bindings to support addImage with sdf images #15054

riastrad opened this issue Jul 3, 2019 · 0 comments · Fixed by #15057
Assignees
Labels
Node.js node-mapbox-gl-native

Comments

@riastrad
Copy link
Contributor

riastrad commented Jul 3, 2019

Currently if you build the node library from source on master and run render tests, the runtime-styling/image-add-sdf test will fail. This is because the node bindings don't currently parse the sdf attribute for the image when adding it.

To my untrained eye, this is the relevant part of the bindings:

void NodeMap::AddImage(const Nan::FunctionCallbackInfo<v8::Value>& info) {
using namespace mbgl::style;
using namespace mbgl::style::conversion;
auto nodeMap = Nan::ObjectWrap::Unwrap<NodeMap>(info.Holder());
if (!nodeMap->map) return Nan::ThrowError(releasedMessage());
if (info.Length() != 3) {
return Nan::ThrowTypeError("Three arguments required");
}
if (!info[0]->IsString()) {
return Nan::ThrowTypeError("First argument must be a string");
}
if (!info[1]->IsObject()) {
return Nan::ThrowTypeError("Second argument must be an object");
}
if (!info[2]->IsObject()) {
return Nan::ThrowTypeError("Third argument must be an object");
}
auto optionObject = Nan::To<v8::Object>(info[2]).ToLocalChecked();
if (!Nan::Get(optionObject, Nan::New("height").ToLocalChecked()).ToLocalChecked()->IsUint32()) {
return Nan::ThrowTypeError("height parameter required");
}
if (!Nan::Get(optionObject, Nan::New("width").ToLocalChecked()).ToLocalChecked()->IsUint32()) {
return Nan::ThrowTypeError("width parameter required");
}
if (!Nan::Get(optionObject, Nan::New("pixelRatio").ToLocalChecked()).ToLocalChecked()->IsNumber()) {
return Nan::ThrowTypeError("pixelRatio parameter required");
}
uint32_t imageHeight = Nan::Get(optionObject, Nan::New("height").ToLocalChecked()).ToLocalChecked()->Uint32Value();
uint32_t imageWidth = Nan::Get(optionObject, Nan::New("width").ToLocalChecked()).ToLocalChecked()->Uint32Value();
if (imageWidth > 1024 || imageHeight > 1024) {
return Nan::ThrowTypeError("Max height and width is 1024");
}
float pixelRatio = Nan::Get(optionObject, Nan::New("pixelRatio").ToLocalChecked()).ToLocalChecked()->NumberValue();
auto imageBuffer = Nan::To<v8::Object>(info[1]).ToLocalChecked()->ToObject();
char * imageDataBuffer = node::Buffer::Data(imageBuffer);
size_t imageLength = node::Buffer::Length(imageBuffer);
if (imageLength != imageHeight * imageWidth * 4) {
return Nan::ThrowTypeError("Image size does not match buffer size");
}
std::unique_ptr<uint8_t[]> data = std::make_unique<uint8_t[]>(imageLength);
std::copy(imageDataBuffer, imageDataBuffer + imageLength, data.get());
mbgl::UnassociatedImage cImage({ imageWidth, imageHeight}, std::move(data));
mbgl::PremultipliedImage cPremultipliedImage = mbgl::util::premultiply(std::move(cImage));
nodeMap->map->getStyle().addImage(std::make_unique<mbgl::style::Image>(*Nan::Utf8String(info[0]), std::move(cPremultipliedImage), pixelRatio));
}

We should remedy this so that the node library can add SDF images without problems.

cc: @mapbox/gl-native @mapbox/maps-api

@riastrad riastrad added Node.js node-mapbox-gl-native needs changelog Indicates PR needs a changelog entry prior to merging. labels Jul 3, 2019
@riastrad riastrad self-assigned this Jul 3, 2019
@friedbunny friedbunny removed the needs changelog Indicates PR needs a changelog entry prior to merging. label Jul 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Node.js node-mapbox-gl-native
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants