Added binding to imreadmulti with test case for included .tif file.

This commit is contained in:
Nathan Graves 2017-02-16 16:38:51 -08:00
parent c0f7942636
commit d90fcf78e0
4 changed files with 56 additions and 0 deletions

Binary file not shown.

View File

@ -11,6 +11,7 @@ void OpenCV::Init(Local<Object> target) {
target->Set(Nan::New<String>("version").ToLocalChecked(), Nan::New<String>(out, n).ToLocalChecked());
Nan::SetMethod(target, "readImage", ReadImage);
Nan::SetMethod(target, "readImageMulti", ReadImageMulti);
}
NAN_METHOD(OpenCV::ReadImage) {
@ -66,3 +67,43 @@ NAN_METHOD(OpenCV::ReadImage) {
return;
}
NAN_METHOD(OpenCV::ReadImageMulti) {
Nan::EscapableHandleScope scope;
REQ_FUN_ARG(1, cb);
Local<Value> argv[2];
argv[0] = Nan::Null();
std::vector<cv::Mat> mats;
try {
if (info[0]->IsString()) {
std::string filename = std::string(*Nan::Utf8String(info[0]->ToString()));
cv::imreadmulti(filename, mats);
}
} catch (cv::Exception& e) {
argv[0] = Nan::Error(e.what());
argv[1] = Nan::Null();
}
Local <v8::Array> output = Nan::New<v8::Array>(mats.size());
for (std::vector<cv::Mat>::size_type i = 0; i < mats.size(); i ++) {
Local<Object> im_h = Nan::New(Matrix::constructor)->GetFunction()->NewInstance();
Matrix *img = Nan::ObjectWrap::Unwrap<Matrix>(im_h);
img->mat = mats[i];
output->Set(i, im_h);
}
argv[1] = output;
Nan::TryCatch try_catch;
cb->Call(Nan::GetCurrentContext()->Global(), 2, argv);
if (try_catch.HasCaught()) {
Nan::FatalException(try_catch);
}
return;
}

View File

@ -67,6 +67,7 @@ public:
static void Init(Local<Object> target);
static NAN_METHOD(ReadImage);
static NAN_METHOD(ReadImageMulti);
};
#endif

View File

@ -216,6 +216,20 @@ test("Image read from file", function(assert){
})
})
test("Multi-page image read from file", function(assert){
cv.readImageMulti("./examples/files/multipage.tif", function(err, imgs){
assert.ok(imgs);
assert.equal(imgs.length, 10);
for (var i = 0; i < imgs.length; i++) {
assert.ok(imgs[i]);
assert.equal(imgs[i].width(), 800);
assert.equal(imgs[i].height(), 600);
assert.equal(imgs[i].channels(), 3);
assert.equal(imgs[i].empty(), false);
}
assert.end();
})
})
test("read Image from buffer", function(assert){
cv.readImage(fs.readFileSync('./examples/files/opencv.png'), function(err, im){