44#include < mapnik/image_any.hpp> // for image_any
55#include < mapnik/image_util.hpp> // for save_to_string, guess_type, etc
66#include " mapnik_image.hpp"
7+ #include " mapnik_image_view.hpp"
78#include " mapnik_palette.hpp"
89#include " mapnik_color.hpp"
910#include " pixel_utils.hpp"
@@ -54,6 +55,7 @@ Napi::Object Image::Initialize(Napi::Env env, Napi::Object exports)
5455 InstanceMethod<&Image::filterSync>(" filterSync" ),
5556 InstanceMethod<&Image::filter>(" filter" ),
5657 InstanceMethod<&Image::composite>(" composite" ),
58+ InstanceMethod<&Image::view>(" view" ),
5759 StaticMethod<&Image::openSync>(" openSync" ),
5860 StaticMethod<&Image::open>(" open" ),
5961 StaticMethod<&Image::fromBufferSync>(" fromBufferSync" ),
@@ -64,9 +66,6 @@ Napi::Object Image::Initialize(Napi::Env env, Napi::Object exports)
6466 StaticMethod<&Image::fromSVGBytesSync>(" fromSVGBytesSync" ),
6567 StaticMethod<&Image::fromSVGBytes>(" fromSVGBytes" )
6668 });
67- /*
68- InstanceMethod("view", &view),
69- */
7069 constructor = Napi::Persistent (func);
7170 constructor.SuppressDestruct ();
7271 exports.Set (" Image" , func);
@@ -103,7 +102,7 @@ Image::Image(Napi::CallbackInfo const& info)
103102 if (info.Length () == 1 && info[0 ].IsExternal ())
104103 {
105104 auto ext = info[0 ].As <Napi::External<image_ptr>>();
106- image_ = *ext.Data ();
105+ if (ext) image_ = *ext.Data ();
107106 return ;
108107 }
109108 if (info.Length () >= 2 )
@@ -314,7 +313,7 @@ Napi::Value Image::getPixel(Napi::CallbackInfo const& info)
314313 }
315314 else
316315 {
317- detail::visitor_get_pixel<mapnik::image_any> visitor ( env, x, y) ;
316+ detail::visitor_get_pixel<mapnik::image_any> visitor{ env, x, y} ;
318317 return mapnik::util::apply_visitor (visitor, *image_);
319318 }
320319 }
@@ -588,7 +587,7 @@ Napi::Value Image::painted(Napi::CallbackInfo const& info)
588587
589588Napi::Value Image::width (Napi::CallbackInfo const & info)
590589{
591- return Napi::Number::New (info.Env (), static_cast <std:: int32_t >( image_->width () ));
590+ return Napi::Number::New (info.Env (), image_->width ());
592591}
593592
594593/* *
@@ -605,7 +604,7 @@ Napi::Value Image::width(Napi::CallbackInfo const& info)
605604
606605Napi::Value Image::height (Napi::CallbackInfo const & info)
607606{
608- return Napi::Number::New (info.Env (), static_cast <std:: int32_t >( image_->height () ));
607+ return Napi::Number::New (info.Env (), image_->height ());
609608}
610609
611610
@@ -627,26 +626,27 @@ Napi::Value Image::height(Napi::CallbackInfo const& info)
627626 * var img2 = img.view(0, 0, 5, 5);
628627 * console.log(img.width(), img2.width()); // 10, 5
629628 */
630- /*
629+
631630Napi::Value Image::view (Napi::CallbackInfo const & info)
632631{
633- if ( (info.Length() != 4) || (!info[0].IsNumber() && !info[1].IsNumber() && !info[2].IsNumber() && !info[3].IsNumber() )) {
632+ Napi::Env env = info.Env ();
633+ Napi::EscapableHandleScope scope (env);
634+
635+ if ( (info.Length () != 4 ) ||
636+ (!info[0 ].IsNumber () && !info[1 ].IsNumber () && !info[2 ].IsNumber () && !info[3 ].IsNumber () ))
637+ {
634638 Napi::TypeError::New (env, " requires 4 integer arguments: x, y, width, height" ).ThrowAsJavaScriptException ();
635639 return env.Undefined ();
636640 }
637-
638- // TODO parse args
639- unsigned x = info[0].As<Napi::Number>().Int32Value();
640- unsigned y = info[1].As<Napi::Number>().Int32Value();
641- unsigned w = info[2].As<Napi::Number>().Int32Value();
642- unsigned h = info[3].As<Napi::Number>().Int32Value();
643-
644- Image* im = info.Holder().Unwrap<Image>();
645- return ImageView::NewInstance(im,x,y,w,h);
641+ Napi::Number x = info[0 ].As <Napi::Number>();
642+ Napi::Number y = info[1 ].As <Napi::Number>();
643+ Napi::Number w = info[2 ].As <Napi::Number>();
644+ Napi::Number h = info[3 ].As <Napi::Number>();
645+ Napi::Value image_obj = Napi::External<image_ptr>::New (env, &image_);
646+ Napi::Object obj = ImageView::constructor.New ({image_obj, x, y, w, h });
647+ return scope.Escape (napi_value (obj)).ToObject ();
646648}
647649
648- */
649-
650650Napi::Value Image::offset (Napi::CallbackInfo const & info)
651651{
652652 return Napi::Number::New (info.Env (), image_->get_offset ());
0 commit comments