diff --git a/src/Matrix.cc b/src/Matrix.cc index 81a6ba3..38527e3 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -26,6 +26,7 @@ void Matrix::Init(Local target) { Nan::SetPrototypeMethod(ctor, "pixelCol", PixelCol); Nan::SetPrototypeMethod(ctor, "empty", Empty); Nan::SetPrototypeMethod(ctor, "get", Get); + Nan::SetPrototypeMethod(ctor, "getPixel", GetPixel); Nan::SetPrototypeMethod(ctor, "set", Set); Nan::SetPrototypeMethod(ctor, "put", Put); Nan::SetPrototypeMethod(ctor, "brightness", Brightness); @@ -274,6 +275,35 @@ NAN_METHOD(Matrix::Get) { info.GetReturnValue().Set(Nan::New(val)); } +NAN_METHOD(Matrix::GetPixel) { + SETUP_FUNCTION(Matrix) + + int y = info[0]->IntegerValue(); + int x = info[1]->IntegerValue(); + + if (self->mat.channels() == 4) { + v8::Local < v8::Array > arr = Nan::New(4); + cv::Vec4b pixel = self->mat.at(y, x); + arr->Set(0, Nan::New((double) pixel.val[0])); + arr->Set(1, Nan::New((double) pixel.val[1])); + arr->Set(2, Nan::New((double) pixel.val[2])); + arr->Set(3, Nan::New((double) pixel.val[3])); + info.GetReturnValue().Set(arr); + } else if (self->mat.channels() == 3) { + v8::Local < v8::Array > arr = Nan::New(3); + cv::Vec3b pixel = self->mat.at(y, x); + arr->Set(0, Nan::New((double) pixel.val[0])); + arr->Set(1, Nan::New((double) pixel.val[1])); + arr->Set(2, Nan::New((double) pixel.val[2])); + info.GetReturnValue().Set(arr); + } else if(self->mat.channels() == 1) { + int pixel = (int)self->mat.at(y, x); + info.GetReturnValue().Set(pixel); + } else { + Nan::ThrowTypeError("Only 4, 3 and 1 channel matrix are supported"); + } +} + NAN_METHOD(Matrix::Set) { SETUP_FUNCTION(Matrix) @@ -567,7 +597,17 @@ NAN_METHOD(Matrix::PixelRow) { int y = info[0]->IntegerValue(); v8::Local < v8::Array > arr; - if (self->mat.channels() == 3) { + if (self->mat.channels() == 4) { + arr = Nan::New(width * 4); + for (int x = 0; x < width; x++) { + cv::Vec4b pixel = self->mat.at(y, x); + int offset = x * 4; + arr->Set(offset, Nan::New((double) pixel.val[0])); + arr->Set(offset + 1, Nan::New((double) pixel.val[1])); + arr->Set(offset + 2, Nan::New((double) pixel.val[2])); + arr->Set(offset + 3, Nan::New((double) pixel.val[3])); + } + } else if(self->mat.channels() == 3){ arr = Nan::New(width * 3); for (int x = 0; x < width; x++) { cv::Vec3b pixel = self->mat.at(y, x); @@ -576,12 +616,14 @@ NAN_METHOD(Matrix::PixelRow) { arr->Set(offset + 1, Nan::New((double) pixel.val[1])); arr->Set(offset + 2, Nan::New((double) pixel.val[2])); } - } else { + } else if(self->mat.channels() == 1){ arr = Nan::New(width); for (int x = 0; x < width; x++) { int pixel = (int)self->mat.at(y, x); arr->Set(x, Nan::New(pixel)); } + } else { + Nan::ThrowTypeError("Only 4, 3 and 1 channel matrix are supported"); } info.GetReturnValue().Set(arr); @@ -608,7 +650,17 @@ NAN_METHOD(Matrix::PixelCol) { int x = info[0]->IntegerValue(); v8::Local < v8::Array > arr; - if (self->mat.channels() == 3) { + if (self->mat.channels() == 4) { + arr = Nan::New(height * 4); + for (int y = 0; y < height; y++) { + cv::Vec4b pixel = self->mat.at(y, x); + int offset = y * 4; + arr->Set(offset, Nan::New((double) pixel.val[0])); + arr->Set(offset + 1, Nan::New((double) pixel.val[1])); + arr->Set(offset + 2, Nan::New((double) pixel.val[2])); + arr->Set(offset + 3, Nan::New((double) pixel.val[3])); + } + } else if (self->mat.channels() == 3) { arr = Nan::New(height * 3); for (int y = 0; y < height; y++) { cv::Vec3b pixel = self->mat.at(y, x); @@ -617,13 +669,16 @@ NAN_METHOD(Matrix::PixelCol) { arr->Set(offset + 1, Nan::New((double) pixel.val[1])); arr->Set(offset + 2, Nan::New((double) pixel.val[2])); } - } else { + } else if(self->mat.channels() == 1) { arr = Nan::New(height); for (int y = 0; y < height; y++) { int pixel = (int)self->mat.at(y, x); arr->Set(y, Nan::New(pixel)); } + } else { + Nan::ThrowTypeError("Only 4, 3 and 1 channel matrix are supported"); } + info.GetReturnValue().Set(arr); } diff --git a/src/Matrix.h b/src/Matrix.h index c9a9875..d1142e9 100755 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -20,6 +20,7 @@ public: JSFUNC(Eye) // factory JSFUNC(Get) // at + JSFUNC(GetPixel) JSFUNC(Set) JSFUNC(Put) diff --git a/test/unit.js b/test/unit.js index 77adf18..9df2790 100755 --- a/test/unit.js +++ b/test/unit.js @@ -449,5 +449,36 @@ test('setColor works will alpha channels', function(assert) { }); }); +test('getPixel',function(assert){ + cv.readImage('./examples/files/alpha-test.png', function(err, imgMat) { + if (!err) { + assert.equal(imgMat.channels(),4); + assert.deepEqual(imgMat.getPixel(10,10),[0,187,255,255]); + assert.deepEqual(imgMat.getPixel(30,80),[241,161,0,128]); + assert.deepEqual(imgMat.getPixel(80,30),[0,187,124,200]); + assert.deepEqual(imgMat.getPixel(80,80),[20,83,246,70]); + + var channels = imgMat.split(); + imgMat.merge([channels[0],channels[1],channels[2]]); + assert.equal(imgMat.channels(),3); + assert.deepEqual(imgMat.getPixel(10,10),[0,187,255]); + assert.deepEqual(imgMat.getPixel(30,80),[241,161,0]); + assert.deepEqual(imgMat.getPixel(80,30),[0,187,124]); + assert.deepEqual(imgMat.getPixel(80,80),[20,83,246]); + + imgMat.merge([channels[3]]); + assert.equal(imgMat.channels(),1); + assert.deepEqual(imgMat.getPixel(0,0),255); + assert.deepEqual(imgMat.getPixel(30,80),128); + assert.deepEqual(imgMat.getPixel(80,30),200); + assert.deepEqual(imgMat.getPixel(80,80),70); + + } else { + throw err; + } + assert.end(); + }); +}); + // Test the examples folder. require('./examples')()