Skip to content

Commit 1d1021a

Browse files
committed
N-API mapnik.ImageView (WIP) [skip ci]
1 parent 591735b commit 1d1021a

11 files changed

Lines changed: 142 additions & 277 deletions

binding.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"src/mapnik_image_resize.cpp",
3535
"src/mapnik_image_compositing.cpp",
3636
"src/mapnik_image_filter.cpp",
37-
#"src/mapnik_image_view.cpp",
37+
"src/mapnik_image_view.cpp",
3838
#"src/mapnik_grid.cpp",
3939
#"src/mapnik_grid_view.cpp",
4040
"src/mapnik_palette.cpp",

src/mapnik_color.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class Color : public Napi::ObjectWrap<Color>
77
{
88
friend class Image;
9+
friend class ImageView;
910
friend class Map;
1011
public:
1112
// initializer

src/mapnik_image.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
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

589588
Napi::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

606605
Napi::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+
631630
Napi::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-
650650
Napi::Value Image::offset(Napi::CallbackInfo const& info)
651651
{
652652
return Napi::Number::New(info.Env(), image_->get_offset());

src/mapnik_image.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ class Image : public Napi::ObjectWrap<Image>
7171
static Napi::Value fromSVGBytes(Napi::CallbackInfo const& info);
7272

7373
Napi::Value painted(Napi::CallbackInfo const& info);
74-
75-
//Napi::Value view(Napi::CallbackInfo const& info);
74+
Napi::Value view(Napi::CallbackInfo const& info);
7675
Napi::Value composite(Napi::CallbackInfo const& info);
7776
Napi::Value filterSync(Napi::CallbackInfo const& info);
7877
Napi::Value filter(Napi::CallbackInfo const& info);

0 commit comments

Comments
 (0)