mirror of
https://github.com/peterbraden/node-opencv.git
synced 2025-12-08 19:45:55 +00:00
Added binding to imreadmulti with test case for included .tif file.
This commit is contained in:
parent
c0f7942636
commit
d90fcf78e0
BIN
examples/files/multipage.tif
Normal file
BIN
examples/files/multipage.tif
Normal file
Binary file not shown.
@ -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;
|
||||
}
|
||||
|
||||
@ -67,6 +67,7 @@ public:
|
||||
static void Init(Local<Object> target);
|
||||
|
||||
static NAN_METHOD(ReadImage);
|
||||
static NAN_METHOD(ReadImageMulti);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
14
test/unit.js
14
test/unit.js
@ -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){
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user