Merge pull request #85 from fastner/fix/tobuffer-options

Add support for options in async toBuffer()
This commit is contained in:
Peter Braden 2013-10-09 15:11:59 -07:00
commit 0d53c15058

View File

@ -340,7 +340,7 @@ Handle<Value>
Matrix::ToBuffer(const v8::Arguments& args){ Matrix::ToBuffer(const v8::Arguments& args){
SETUP_FUNCTION(Matrix) SETUP_FUNCTION(Matrix)
if ((args.Length() > 0) && (args[args.Length() - 1]->IsFunction())) { if ((args.Length() > 0) && (args[0]->IsFunction())) {
return Matrix::ToBufferAsync(args); return Matrix::ToBufferAsync(args);
} }
@ -400,6 +400,8 @@ struct matrixToBuffer_baton_t {
Matrix *mm; Matrix *mm;
Persistent<Function> cb; Persistent<Function> cb;
std::vector<uchar> res; std::vector<uchar> res;
std::vector<int> params;
std::string ext;
uv_work_t request; uv_work_t request;
}; };
@ -412,7 +414,34 @@ Matrix::ToBufferAsync(const v8::Arguments& args){
REQ_FUN_ARG(0, cb); REQ_FUN_ARG(0, cb);
matrixToBuffer_baton_t *baton = new matrixToBuffer_baton_t(); matrixToBuffer_baton_t *baton = new matrixToBuffer_baton_t();
std::string ext = std::string(".jpg");
// See if the options argument is passed
if ((args.Length() > 1) && (args[1]->IsObject())) {
// Get this options argument
v8::Handle<v8::Object> options = v8::Handle<v8::Object>::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 = str2;
}
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->mm = self;
baton->cb = Persistent<Function>::New(cb); baton->cb = Persistent<Function>::New(cb);
baton->request.data = baton; baton->request.data = baton;
@ -426,9 +455,10 @@ void AsyncToBufferAsync(uv_work_t *req) {
matrixToBuffer_baton_t *baton = static_cast<matrixToBuffer_baton_t *>(req->data); matrixToBuffer_baton_t *baton = static_cast<matrixToBuffer_baton_t *>(req->data);
std::vector<uchar> vec(0); std::vector<uchar> vec(0);
std::vector<int> params(0);//CV_IMWRITE_JPEG_QUALITY 90 //std::vector<int> params(0);//CV_IMWRITE_JPEG_QUALITY 90
cv::imencode(".jpg", baton->mm->mat, vec, params); const char * ext = (const char *) baton->ext.c_str();
cv::imencode(ext, baton->mm->mat, vec, baton->params);
baton->res = vec; baton->res = vec;