Skip to content

Commit

Permalink
Merge pull request #327 from justadudewhohacks/imencode-electron-fix
Browse files Browse the repository at this point in the history
fix imencode crashes in electron when buffer is released
  • Loading branch information
justadudewhohacks committed Jun 12, 2018
2 parents 2aa1aed + 20d45a0 commit c42fe0f
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions cc/modules/io/ioBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,57 @@ namespace IoBindings {
std::string path;
int flags = cv::IMREAD_COLOR;
cv::Mat img;

std::string executeCatchCvExceptionWorker() {
img = cv::imread(path, flags);
if (img.rows == 0 && img.cols == 0) {
return "empty Mat";
}
return "";
}

bool unwrapRequiredArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
return StringConverter::arg(0, &path, info);
}

bool unwrapOptionalArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
return IntConverter::optArg(1, &flags, info);
}

FF_VAL getReturnValue() {
return Mat::Converter::wrap(img);
}
};

struct ImwriteWorker : CatchCvExceptionWorker {
public:
std::string path;
cv::Mat img;
std::vector<int> flags;

std::string executeCatchCvExceptionWorker() {
cv::imwrite(path, img);
return "";
}

bool unwrapRequiredArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
return StringConverter::arg(0, &path, info)
|| Mat::Converter::arg(1, &img, info);
}

bool unwrapOptionalArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
return IntArrayConverter::optArg(2, &flags, info);
}
};

struct ImencodeWorker : CatchCvExceptionWorker {
public:
std::string ext;
cv::Mat img;
std::vector<int> flags;
char *data;
size_t dataSize;

std::string executeCatchCvExceptionWorker() {
std::vector<uchar> dataVec;
cv::imencode(ext, img, dataVec, flags);
Expand All @@ -69,40 +69,44 @@ namespace IoBindings {
memcpy(data, reinterpret_cast<char*>(dataVec.data()), dataSize);
return "";
}

bool unwrapRequiredArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
return StringConverter::arg(0, &ext, info)
|| Mat::Converter::arg(1, &img, info);
}

bool unwrapOptionalArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
return IntArrayConverter::optArg(2, &flags, info);
}


static void freeBufferCallback(char* data, void* hint) {
free(data);
}

FF_VAL getReturnValue() {
return Nan::NewBuffer(data, dataSize).ToLocalChecked();
return Nan::NewBuffer(data, dataSize, freeBufferCallback, 0).ToLocalChecked();
}
};

struct ImdecodeWorker : CatchCvExceptionWorker {
public:
int flags;
cv::Mat img;
char *data;
size_t dataSize;

std::string executeCatchCvExceptionWorker() {
std::vector<uchar> vec(dataSize);
memcpy(vec.data(), data, dataSize);
img = cv::imdecode(vec, flags);
return "";
}

FF_VAL getReturnValue() {
return Mat::Converter::wrap(img);
}
};


}

Expand Down

0 comments on commit c42fe0f

Please sign in to comment.