From 0e72fc4ec3a903bf6a91c3074e6a27275cd87cdd Mon Sep 17 00:00:00 2001 From: Bobby Sudekum Date: Fri, 7 Apr 2017 10:23:31 -0700 Subject: [node] Calculate image buffer and length once (#8673) --- platform/node/src/node_map.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/platform/node/src/node_map.cpp b/platform/node/src/node_map.cpp index 674aae7451..1890529393 100644 --- a/platform/node/src/node_map.cpp +++ b/platform/node/src/node_map.cpp @@ -635,13 +635,16 @@ void NodeMap::AddImage(const Nan::FunctionCallbackInfo& info) { uint32_t pixelRatio = Nan::Get(optionObject, Nan::New("pixelRatio").ToLocalChecked()).ToLocalChecked()->Uint32Value(); auto imageBuffer = Nan::To(info[1]).ToLocalChecked()->ToObject(); + + char * imageDataBuffer = node::Buffer::Data(imageBuffer); + size_t imageLength = node::Buffer::Length(imageBuffer); - if (node::Buffer::Length(imageBuffer) != imageHeight * imageWidth * 4) { + if (imageLength != imageHeight * imageWidth * 4) { return Nan::ThrowTypeError("Image size does not match buffer size"); } - std::unique_ptr data = std::make_unique(node::Buffer::Length(imageBuffer)); - std::copy(node::Buffer::Data(imageBuffer), node::Buffer::Data(imageBuffer) + node::Buffer::Length(imageBuffer), data.get()); + std::unique_ptr data = std::make_unique(imageLength); + std::copy(imageDataBuffer, imageDataBuffer + imageLength, data.get()); mbgl::PremultipliedImage cPremultipliedImage({ imageWidth, imageHeight}, std::move(data)); nodeMap->map->addImage(*Nan::Utf8String(info[0]), std::make_unique(std::move(cPremultipliedImage), pixelRatio)); -- cgit v1.2.1