From ac6c685b56773d1008e1ab575db6fda30134bf6c Mon Sep 17 00:00:00 2001 From: Peter Braden Date: Mon, 29 Oct 2012 23:48:12 -0700 Subject: [PATCH] Fix async toBufffer test --- src/Matrix.cc | 26 +++++++++++++------------- test/unit.js | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index c1f84ae..48ee47b 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -326,7 +326,8 @@ Matrix::ToBuffer(const v8::Arguments& args){ struct matrixToBuffer_baton_t { Matrix *mm; Persistent cb; - Persistent res; + std::vector res; + int resSize; int sleep_for; uv_work_t request; @@ -360,16 +361,8 @@ void AsyncToBufferAsync(uv_work_t *req) { cv::imencode(".jpg", baton->mm->mat, vec, params); - node::Buffer *buf = node::Buffer::New(vec.size()); - uchar* data = (uchar*) Buffer::Data(buf); - memcpy(data, &vec[0], vec.size()); - v8::Local globalObj = v8::Context::GetCurrent()->Global(); - v8::Local bufferConstructor = v8::Local::Cast(globalObj->Get(v8::String::New("Buffer"))); - v8::Handle constructorArgs[3] = {buf->handle_, v8::Integer::New(vec.size()), v8::Integer::New(0)}; - v8::Persistent actualBuffer = v8::Persistent(bufferConstructor->NewInstance(3, constructorArgs)); - - baton->res = actualBuffer; + baton->res = vec; } void AfterAsyncToBufferAsync(uv_work_t *req) { @@ -383,9 +376,16 @@ void AfterAsyncToBufferAsync(uv_work_t *req) { argv[0] = Local::New(Null()); - v8::Local res = v8::Local(*baton->res); - argv[1] = res; - baton->res.Dispose(); + node::Buffer *buf = node::Buffer::New(baton->res.size()); + uchar* data = (uchar*) Buffer::Data(buf); + memcpy(data, &baton->res[0], baton->res.size()); + + v8::Local globalObj = v8::Context::GetCurrent()->Global(); + v8::Local bufferConstructor = v8::Local::Cast(globalObj->Get(v8::String::New("Buffer"))); + v8::Handle constructorArgs[3] = {buf->handle_, v8::Integer::New(baton->res.size()), v8::Integer::New(0)}; + v8::Local actualBuffer = bufferConstructor->NewInstance(3, constructorArgs); + + argv[1] = actualBuffer; TryCatch try_catch; diff --git a/test/unit.js b/test/unit.js index 171bc0e..abbc34f 100755 --- a/test/unit.js +++ b/test/unit.js @@ -137,6 +137,23 @@ vows.describe('Smoke Tests OpenCV').addBatch({ }) } + + , "toBufferAsync": { + topic: function(cv){ + var buf = fs.readFileSync('./examples/mona.png') + , cb = this.callback + cv.readImage(buf.slice(0), function(err, mat){ + var buff = mat.toBufferAsync(function(){ + cb.apply(this, arguments) + }) + }) + } + , 'gives a buffer' : function(e, res){ + assert.ok(!e) + assert.ok(res); + assert.ok(res.length > 100); + } + } , "detectObject": {