diff --git a/examples/readimage.js b/examples/readimage.js index 435d6bb..6017e1e 100644 --- a/examples/readimage.js +++ b/examples/readimage.js @@ -1,6 +1,12 @@ var cv = require('../lib/opencv'); +// read as a buffer for decode operations later +var fs = require('fs'); +var imgdata = fs.readFileSync("./files/mona.png"); + + + var img = cv.readImage("./files/mona.png"); console.log('Synchronous readImage("./files/mona.png")'+img.width()+'x'+img.height()); @@ -10,18 +16,43 @@ cv.readImage("./files/mona.png", function(err, im){ }); img = cv.readImage( 100, 100 ); -console.log('Synchronous readImage(100, 100) (create mat)'+img.width()+'x'+img.height()); +console.log('Synchronous readImage(100, 100) (create mat)'+img.width()+'x'+img.height()+' type '+img.type()); cv.readImage(100, 100, function(err, im){ console.log('callback readImage(100, 100, fn(){}) (create mat)'+im.width()+'x'+im.height()); }); -var fs = require('fs'); -var imgdata = fs.readFileSync("./files/mona.png"); - img = cv.readImage(imgdata); console.log('Synchronous readImage(imgdata:Buffer)'+img.width()+'x'+img.height()); cv.readImage(imgdata, function(err, im){ console.log('callback readImage(imgdata:Buffer, fn(){})'+im.width()+'x'+im.height()); }); + + +// try with flags now +console.log('Now with flags'); + +img = cv.readImage("./files/mona.png", 0); +console.log('Synchronous readImage("./files/mona.png", 0) (monochrome)'+img.width()+'x'+img.height()+' type '+img.type()); + +cv.readImage("./files/mona.png", 1, function(err, im){ + console.log('callback readImage("./files/mona.png", 1, fn(){}) (colour)'+im.width()+'x'+im.height()+' type '+im.type()); +}); + +img = cv.readImage( 100, 100, cv.Constants.CV_8UC3 ); +console.log('Synchronous readImage(100, 100, cv.Constants.CV_8UC3) (create 8 bit 3 channel mat)'+img.width()+'x'+img.height()+' type '+img.type()); + +cv.readImage(100, 100, cv.Constants.CV_8UC1, function(err, im){ + console.log('callback readImage(100, 100, cv.Constants.CV_8UC1, fn(){}) (create mat)'+im.width()+'x'+im.height()+' type '+im.type()); +}); + +img = cv.readImage(imgdata, 0); +console.log('Synchronous readImage(imgdata:Buffer, 0) (monochrome)'+img.width()+'x'+img.height()+' type '+img.type()); + +cv.readImage(imgdata, 1, function(err, im){ + console.log('callback readImage(imgdata:Buffer, 1, fn(){}) (colour)'+im.width()+'x'+im.height()+' type '+im.type()); +}); + + + diff --git a/src/OpenCV.cc b/src/OpenCV.cc index 9002c5e..a282057 100755 --- a/src/OpenCV.cc +++ b/src/OpenCV.cc @@ -42,25 +42,41 @@ NAN_METHOD(OpenCV::ReadImage) { if (info[0]->IsNumber() && info[1]->IsNumber()) { int width, height; + int type = CV_64FC1; + // if we have a type arg + if ((numargs > 2) && info[2]->IsNumber()){ + type = info[2]->Uint32Value(); + } width = info[0]->Uint32Value(); height = info[1]->Uint32Value(); - mat = *(new cv::Mat(width, height, CV_64FC1)); + mat = *(new cv::Mat(width, height, type)); } else if (info[0]->IsString()) { std::string filename = std::string(*Nan::Utf8String(info[0]->ToString())); - mat = cv::imread(filename, CV_LOAD_IMAGE_UNCHANGED); + int flags = CV_LOAD_IMAGE_UNCHANGED; + if (numargs > 1){ + if (info[1]->IsNumber()){ + flags = info[1]->Uint32Value(); + } + } + mat = cv::imread(filename, flags); } else if (Buffer::HasInstance(info[0])) { uint8_t *buf = (uint8_t *) Buffer::Data(info[0]->ToObject()); unsigned len = Buffer::Length(info[0]->ToObject()); - + int flags = CV_LOAD_IMAGE_UNCHANGED; + if (numargs > 1){ + if (info[1]->IsNumber()){ + flags = info[1]->Uint32Value(); + } + } cv::Mat *mbuf = new cv::Mat(len, 1, CV_64FC1, buf); - mat = cv::imdecode(*mbuf, CV_LOAD_IMAGE_UNCHANGED); - + mat = cv::imdecode(*mbuf, flags); if (mat.empty()) { success = 0; argv[0] = Nan::Error("Error loading file"); } + } img->mat = mat;