Skip to content

Commit

Permalink
Merge pull request #47 from lpinca/fix/corrupted-image-file
Browse files Browse the repository at this point in the history
Fix corrupted image file
Fixes #29
  • Loading branch information
Leonhardt Wille committed Dec 17, 2013
2 parents ddb824f + dee5f56 commit ef08ea1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
17 changes: 10 additions & 7 deletions src/camera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,18 @@ void GPCamera::Async_CaptureCb(uv_work_t *req, int status){
}
else if(capture_req->data && capture_req->download) {
argc = 2;
node::Buffer* slowBuffer = node::Buffer::New(capture_req->length);
Local<Object> globalObj = Context::GetCurrent()->Global();
Local<Function> bufferConstructor = Local<Function>::Cast(globalObj->Get(String::New("Buffer")));
Handle<Value> constructorArgs[1];
constructorArgs[0] = capture_req->length
? Integer::New(capture_req->length)
: Integer::New(0);
Local<Object> buffer = bufferConstructor->NewInstance(1, constructorArgs);
if(capture_req->length){
memmove(Buffer::Data(slowBuffer), capture_req->data, capture_req->length);
Local<Object> globalObj = Context::GetCurrent()->Global();
Local<Function> bufferConstructor = Local<Function>::Cast(globalObj->Get(String::New("Buffer")));
Handle<Value> constructorArgs[3] = { slowBuffer->handle_, Integer::New(capture_req->length), Integer::New(0) };
Local<Object> actualBuffer = bufferConstructor->NewInstance(3, constructorArgs);
argv[1] = actualBuffer;
memmove(Buffer::Data(buffer), capture_req->data, capture_req->length);
delete capture_req->data;
}
argv[1] = buffer;
}else{
argc = 2;
argv[1] = cv::CastToJS(capture_req->path);
Expand Down
11 changes: 9 additions & 2 deletions src/camera_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ GPCamera::getCameraFile(take_picture_request *req, CameraFile **file){
void
GPCamera::downloadPicture(take_picture_request *req){
CameraFile *file;
const char *data;
int retval;
std::ostringstream folder;
std::string name;
Expand All @@ -293,15 +294,21 @@ GPCamera::downloadPicture(take_picture_request *req){
retval = getCameraFile(req, &file);

if (retval == GP_OK) {
retval = gp_camera_file_get ( req->camera, folder.str().c_str(), name.c_str(), GP_FILE_TYPE_NORMAL, file, req->context);
retval = gp_camera_file_get(req->camera, folder.str().c_str(), name.c_str(), GP_FILE_TYPE_NORMAL, file, req->context);
} else {
req->ret=retval;
return;
}

// Fallback to downloading into buffer
if(retval == GP_OK && req->target_path.empty()){
retval = gp_file_get_data_and_size (file, &req->data, &req->length);
retval = gp_file_get_data_and_size(file, &data, &req->length);
if (retval == GP_OK && req->length != 0) {
// `gp_file_free` will call `free` on `file->data` pointer. We need to save our data
req->data = new char[req->length];
memmove(const_cast<char *>(req->data), data, req->length);
}
data = NULL;
}

if(retval == GP_OK){
Expand Down

0 comments on commit ef08ea1

Please sign in to comment.