-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnode_libraw.cc
130 lines (99 loc) · 3.36 KB
/
node_libraw.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <v8.h>
#include <nan.h>
//#include <libraw/libraw.h>
#include "libraw/libraw.h"
namespace node_libraw {
using v8::Exception;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::String;
using v8::Value;
using v8::Null;
NAN_METHOD(Extract) {
Nan::HandleScope scope;
LibRaw RawProcessor;
v8::Isolate* isolate = info.GetIsolate();
v8::String::Utf8Value filenameFromArgs(isolate, info[0]);
std::string filename = std::string(*filenameFromArgs);
v8::String::Utf8Value outputFromArgs(isolate, info[1]);
std::string output = std::string(*outputFromArgs);
Nan::Callback *callback = new Nan::Callback(Local<Function>::Cast(info[2]));
std::ifstream file;
file.open(filename.c_str(), std::ios::binary | std::ios::ate);
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<char> buffer(size);
if (file.read(buffer.data(), size)) {
RawProcessor.open_buffer(buffer.data(), size);
RawProcessor.unpack();
RawProcessor.imgdata.params.output_tiff = 1;
RawProcessor.imgdata.params.output_bps = 16;
RawProcessor.imgdata.params.use_camera_wb = 1;
RawProcessor.dcraw_process();
output = output + ".tiff";
RawProcessor.dcraw_ppm_tiff_writer(output.c_str());
RawProcessor.recycle();
//info.GetReturnValue().Set(Nan::New(output).ToLocalChecked());
Local<v8::Value> argv[2] = {
Nan::Null(),
Nan::New(output).ToLocalChecked()
};
callback->Call(2, argv);
}
file.close();
}
NAN_METHOD(ExtractThumb) {
Nan::HandleScope scope;
LibRaw RawProcessor;
v8::Isolate* isolate = info.GetIsolate();
v8::String::Utf8Value filenameFromArgs(isolate, info[0]);
std::string filename = std::string(*filenameFromArgs);
v8::String::Utf8Value outputFromArgs(isolate, info[1]);
std::string output = std::string(*outputFromArgs);
Nan::Callback *callback = new Nan::Callback(Local<Function>::Cast(info[2]));
std::string extension = "thumb.ppm";
std::ifstream file;
file.open(filename.c_str(), std::ios::binary | std::ios::ate);
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<char> buffer(size);
if (file.read(buffer.data(), size)) {
RawProcessor.open_buffer(buffer.data(), size);
RawProcessor.unpack();
RawProcessor.unpack_thumb();
if (RawProcessor.imgdata.thumbnail.tformat == LIBRAW_THUMBNAIL_JPEG) {
extension = "thumb.jpg";
}
output = output + "." + extension;
RawProcessor.dcraw_thumb_writer(output.c_str());
RawProcessor.recycle();
Local<v8::Value> argv[2] = {
Nan::Null(),
Nan::New(output).ToLocalChecked()
};
callback->Call(2, argv);
}
file.close();
}
void init(Local<Object> exports) {
Nan::Set(
exports,
Nan::New<String>("extractThumb").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(ExtractThumb)).ToLocalChecked()
);
Nan::Set(
exports,
Nan::New<String>("extract").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(Extract)).ToLocalChecked()
);
}
NODE_MODULE(libraw, init)
}