Skip to content

Commit

Permalink
Image constructor - pass width, height, premultipled and `paint…
Browse files Browse the repository at this point in the history
…ed` in `CallbackInfo` and avoid mutating input `Buffer` object (ref #979 (comment))
  • Loading branch information
artemp committed Oct 25, 2021
1 parent b0ffb6e commit 2aef6f5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
11 changes: 5 additions & 6 deletions src/mapnik_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,13 @@ Image::Image(Napi::CallbackInfo const& info)
return;
}

if (info.Length() == 1 && info[0].IsBuffer())
if (info.Length() == 5 && info[0].IsBuffer() && info[1].IsNumber() && info[2].IsNumber() && info[3].IsBoolean() && info[4].IsBoolean())
{
auto buf = info[0].As<Napi::Buffer<unsigned char>>();
auto obj = info[0].As<Napi::Object>();
bool premultiplied = obj.Get("premultiplied").As<Napi::Boolean>();
bool painted = obj.Get("painted").As<Napi::Boolean>();
int width = obj.Get("width").As<Napi::Number>().Int32Value();
int height = obj.Get("height").As<Napi::Number>().Int32Value();
int width = info[1].As<Napi::Number>().Int32Value();
int height = info[2].As<Napi::Number>().Int32Value();
bool premultiplied = info[3].As<Napi::Boolean>();
bool painted = info[4].As<Napi::Boolean>();
mapnik::image_rgba8 im_wrapper(width, height, buf.Data(), premultiplied, painted);
image_ = std::make_shared<mapnik::image_any>(im_wrapper);
buf_ref_ = Napi::Persistent(buf);
Expand Down
10 changes: 5 additions & 5 deletions src/mapnik_image_from_bytes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,11 @@ Napi::Value Image::fromBufferSync(Napi::CallbackInfo const& info)

try
{
obj.Set("width", width);
obj.Set("height", height);
obj.Set("premultiplied", premultiplied);
obj.Set("painted", painted);
Napi::Object image_obj = constructor.New({obj});
Napi::Object image_obj = constructor.New({obj,
Napi::Number::New(env, width),
Napi::Number::New(env, height),
Napi::Boolean::New(env, premultiplied),
Napi::Boolean::New(env, painted)});
return scope.Escape(image_obj);
}
catch (std::exception const& ex)
Expand Down

0 comments on commit 2aef6f5

Please sign in to comment.