From b79c30069ffef1374112985936a41f52437dc97a Mon Sep 17 00:00:00 2001 From: Sebastian Fastner Date: Thu, 3 Oct 2013 16:01:33 +0200 Subject: [PATCH 1/2] Add arguments support for async toBuffer --- src/Matrix.cc | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index a708453..1868851 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -400,6 +400,8 @@ struct matrixToBuffer_baton_t { Matrix *mm; Persistent cb; std::vector res; + std::vector params; + const char *ext; uv_work_t request; }; @@ -413,6 +415,32 @@ Matrix::ToBufferAsync(const v8::Arguments& args){ REQ_FUN_ARG(0, cb); matrixToBuffer_baton_t *baton = new matrixToBuffer_baton_t(); + + + const char *ext = ".jpg"; + // See if the options argument is passed + if ((args.Length() > 1) && (args[1]->IsObject())) { + // Get this options argument + v8::Handle options = v8::Handle::Cast(args[1]); + // If the extension (image format) is provided + if (options->Has(v8::String::New("ext"))) { + v8::String::Utf8Value str ( options->Get(v8::String::New("ext"))->ToString() ); + std::string str2 = std::string(*str); + ext = (const char *) str2.c_str(); + } + if (options->Has(v8::String::New("jpegQuality"))) { + int compression = options->Get(v8::String::New("jpegQuality"))->IntegerValue(); + baton->params.push_back(CV_IMWRITE_JPEG_QUALITY); + baton->params.push_back(compression); + } + if (options->Has(v8::String::New("pngCompression"))) { + int compression = options->Get(v8::String::New("pngCompression"))->IntegerValue(); + baton->params.push_back(CV_IMWRITE_PNG_COMPRESSION); + baton->params.push_back(compression); + } + } + + baton->ext = ext; baton->mm = self; baton->cb = Persistent::New(cb); baton->request.data = baton; @@ -426,9 +454,9 @@ void AsyncToBufferAsync(uv_work_t *req) { matrixToBuffer_baton_t *baton = static_cast(req->data); std::vector vec(0); - std::vector params(0);//CV_IMWRITE_JPEG_QUALITY 90 + //std::vector params(0);//CV_IMWRITE_JPEG_QUALITY 90 - cv::imencode(".jpg", baton->mm->mat, vec, params); + cv::imencode(baton->ext, baton->mm->mat, vec, baton->params); baton->res = vec; From bbf36e72b1eb4eb824bdfbdda2620771d5da6713 Mon Sep 17 00:00:00 2001 From: Sebastian Fastner Date: Thu, 3 Oct 2013 16:32:13 +0200 Subject: [PATCH 2/2] Add parameter support to async buffer generation --- src/Matrix.cc | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index 1868851..40f5d65 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -340,7 +340,7 @@ Handle Matrix::ToBuffer(const v8::Arguments& args){ SETUP_FUNCTION(Matrix) - if ((args.Length() > 0) && (args[args.Length() - 1]->IsFunction())) { + if ((args.Length() > 0) && (args[0]->IsFunction())) { return Matrix::ToBufferAsync(args); } @@ -401,7 +401,7 @@ struct matrixToBuffer_baton_t { Persistent cb; std::vector res; std::vector params; - const char *ext; + std::string ext; uv_work_t request; }; @@ -414,10 +414,11 @@ Matrix::ToBufferAsync(const v8::Arguments& args){ REQ_FUN_ARG(0, cb); + matrixToBuffer_baton_t *baton = new matrixToBuffer_baton_t(); - const char *ext = ".jpg"; + std::string ext = std::string(".jpg"); // See if the options argument is passed if ((args.Length() > 1) && (args[1]->IsObject())) { // Get this options argument @@ -426,7 +427,7 @@ Matrix::ToBufferAsync(const v8::Arguments& args){ if (options->Has(v8::String::New("ext"))) { v8::String::Utf8Value str ( options->Get(v8::String::New("ext"))->ToString() ); std::string str2 = std::string(*str); - ext = (const char *) str2.c_str(); + ext = str2; } if (options->Has(v8::String::New("jpegQuality"))) { int compression = options->Get(v8::String::New("jpegQuality"))->IntegerValue(); @@ -456,7 +457,8 @@ void AsyncToBufferAsync(uv_work_t *req) { std::vector vec(0); //std::vector params(0);//CV_IMWRITE_JPEG_QUALITY 90 - cv::imencode(baton->ext, baton->mm->mat, vec, baton->params); + const char * ext = (const char *) baton->ext.c_str(); + cv::imencode(ext, baton->mm->mat, vec, baton->params); baton->res = vec;