-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-optris.js
362 lines (311 loc) · 12.2 KB
/
node-optris.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
const ffi = require('ffi-napi');
const ref = require('ref-napi')
const refArray = require('ref-array-napi')
// Define useful datatypes
var ushortArray = refArray('ushort')
var intPointer = ref.refType('int')
var ucharArray = refArray('uchar')
// Initialize lib as a global var
var lib
const loadDLL = function(path) {
try {
lib = ffi.Library(path, {
"evo_irimager_usb_init": [ "int", ["string", "string", "string"]],
"evo_irimager_tcp_init": [ "int", ["string", "int"]],
"evo_irimager_terminate": [ "int", []],
"evo_irimager_get_thermal_image_size": [ "int", [intPointer, intPointer]],
"evo_irimager_get_palette_image_size": [ "int", [intPointer, intPointer]],
"evo_irimager_get_thermal_image" : ["int", [intPointer, intPointer, ushortArray]],
"evo_irimager_get_palette_image" : ["int", [intPointer, intPointer, ucharArray]],
"evo_irimager_get_thermal_palette_image": ["void", [intPointer, intPointer, ushortArray, intPointer, intPointer, ucharArray]],
"evo_irimager_set_palette":["void", ["int"]],
"evo_irimager_set_shutter_mode":["int", ["int"]],
"evo_irimager_trigger_shutter_flag":["int", []],
"evo_irimager_daemon_launch": ["int",[]],
"evo_irimager_daemon_is_running": ["int",[]],
"evo_irimager_daemon_kill": ["int",[]]
});
} catch (error) {
throw new Error("Impossible to read the DLL")
}
}
// @brief Initializes an IRImager instance connected to this computer via USB
// @param[in] xml_config path to xml config
// @param[in] formats_def path to Formats.def file. Set zero for standard value.
// @param[in] log_file path to log file. Set zero for standard value.
// @return 0 on success, -1 on error
// __IRDIRECTSDK_API__ int evo_irimager_usb_init(const char* xml_config, const char* formats_def, const char* log_file);
var usb_init = function(xml_config, formats_def, log_file) {
let res = lib.evo_irimager_usb_init(xml_config, formats_def, log_file)
if (res !== 0) {
throw new Error("Impossible to establish a connection to the camera")
}
};
// @brief Initializes the TCP connection to the daemon process (non-blocking)
// @param[in] IP address of the machine where the daemon process is running ("localhost" can be resolved)
// @param port Port of daemon, default 1337
// @return error code: 0 on success, -1 on host not found (wrong IP, daemon not running), -2 on fatal error
//
// __IRDIRECTSDK_API__ int evo_irimager_tcp_init(const char* ip, int port);
var tcp_init = function(ip, port) {
let res = lib.evo_irimager_tcp_init(ip, port)
if (res !== 0) {
throw new Error("Impossible to establish a connection to the camera (let have a look at the Node-Red logs for any further information")
}
};
//
// @brief Disconnects the camera, either connected via USB or TCP
// @return 0 on success, -1 on error
//
// __IRDIRECTSDK_API__ int evo_irimager_terminate();
//
var terminate = function() {
let res = lib.evo_irimager_terminate()
if (res !== 0) {
throw new Error("The process was not terminated correctly")
}
};
/**
* @brief Accessor to image width and height
* @param[out] w width
* @param[out] h height
* @return 0 on success, -1 on error
__IRDIRECTSDK_API__ int evo_irimager_get_thermal_image_size(int* w, int* h);
*/
var get_thermal_image_size = function() {
let w = ref.alloc('int');
let h = ref.alloc('int');
let res = lib.evo_irimager_get_thermal_image_size(w, h)
if (res !== 0) {
throw new Error("Impossible to compute the thermal image size, is the libirimager process initialized ?")
}
else {
return [w.deref(), h.deref()]
}
}
/**
* @brief Accessor to width and height of false color coded palette image
* @param[out] w width
* @param[out] h height
* @return 0 on success, -1 on error
__IRDIRECTSDK_API__ int evo_irimager_get_palette_image_size(int* w, int* h);
*/
var get_palette_image_size = function() {
// Alloc two integers
let w = ref.alloc('int');
let h = ref.alloc('int');
let res = lib.evo_irimager_get_palette_image_size(w, h)
if (res !== 0) {
throw new Error("Impossible to compute the palette image size, is the libirimager process initialized ?")
}
else {
return [w.deref(), h.deref()]
}
}
/**
* @brief Accessor to thermal image by reference
* Conversion to temperature values are to be performed as follows:
* t = ((double)data[x] - 1000.0) / 10.0;
* @param[in] w image width
* @param[in] h image height
* @param[out] data pointer to unsigned short array allocate by the user (size of w * h)
* @return error code: 0 on success, -1 on error, -2 on fatal error (only TCP connection)
__IRDIRECTSDK_API__ int evo_irimager_get_thermal_image(int* w, int* h, unsigned short* data);
*/
var get_thermal_image = function(w, h) {
const size = w * h
let arr = new ushortArray(size)
w = ref.alloc('int', w);
h = ref.alloc('int', h);
let res = lib.evo_irimager_get_thermal_image(w, h, arr)
if (res !== 0) {
throw new Error("Impossible to get the thermal frame")
}
else {
// Transform the result to a Uint16Array, is it the fastest way to do that ?
arr = new Uint16Array(arr.buffer.buffer)
// Copy data to normal array
new_arr = []
for (let i=0;i < arr.length; i++) {
new_arr[i] = (arr[i] - 1000) / 10.0
}
return new_arr
}
}
/**
* @brief Accessor to an RGB palette image by reference
* data format: unsigned char array (size 3 * w * h) r,g,b
* @param[in] w image width
* @param[in] h image height
* @param[out] data pointer to unsigned char array allocate by the user (size of 3 * w * h)
* @return error code: 0 on success, -1 on error, -2 on fatal error (only TCP connection)
__IRDIRECTSDK_API__ int evo_irimager_get_palette_image(int* w, int* h, unsigned char* data);
*/
// TODO: to be tested
var get_palette_image = function(w, h) {
let arr = new ucharArray(w * h * 3)
w_ = ref.alloc('int', w);
h_ = ref.alloc('int', h);
let res = lib.evo_irimager_get_palette_image(w_, h_, arr)
if (res !== 0) {
throw new Error("Impossible to get the palette frame")
}
else {
// Return raw buffer
return Buffer.from(new Uint8Array(arr.buffer))
}
}
/**
* @brief Accessor to an RGB palette image and a thermal image by reference
* @param[in] w_t width of thermal image
* @param[in] h_t height of thermal image
* @param[out] data_t data pointer to unsigned short array allocate by the user (size of w * h)
* @param[in] w_p width of palette image (can differ from thermal image width due to striding)
* @param[in] h_p height of palette image (can differ from thermal image height due to striding)
* @param[out] data_p data pointer to unsigned char array allocate by the user (size of 3 * w * h)
* @return error code: 0 on success, -1 on error, -2 on fatal error (only TCP connection)
__IRDIRECTSDK_API__ int evo_irimager_get_thermal_palette_image(int w_t, int h_t, unsigned short* data_t, int w_p, int h_p, unsigned char* data_p );
*/
// TODO: to be tested
var get_thermal_palette_image = function(w, h) {
// Allocate thermal variables
let thermal_arr = new ushortArray(w * h)
let w_t = ref.alloc('int', w);
let h_t = ref.alloc('int', h);
// Allocate palette variables
let palette_arr = new ucharArray(w * h * 3)
let w_p = ref.ref(w)
let h_p = ref.ref(h)
let res = evo_irimager_get_thermal_palette_image(w_t, h_t, thermal_arr, w_p, h_p, palette_arr)
if (res !== 0) {
throw new Error("Impossible to get thermal and palette frame")
}
else {
// Post process thermal frame
thermal_arr = new Uint16Array(thermal_arr.buffer.buffer)
// Copy data to normal array
let processedThermalArray = []
for (let i=0;i < thermal_arr.length; i++) {
processedThermalArray[i] = (thermal_arr[i] - 1000) / 10.0
}
// Post process palette frame
let processedPaletteBuffer = Buffer.from(new Uint8Array(palette_arr.buffer))
return {"thermal":processedThermalArray, "palette":processedPaletteBuffer}
}
}
/**
* @brief sets palette format to daemon.
* Defined in IRImager Direct-SDK, see
* enum EnumOptrisColoringPalette{eAlarmBlue = 1,
* eAlarmBlueHi = 2,
* eGrayBW = 3,
* eGrayWB = 4,
* eAlarmGreen = 5,
* eIron = 6,
* eIronHi = 7,
* eMedical = 8,
* eRainbow = 9,
* eRainbowHi = 10,
* eAlarmRed = 11 };
*
* @param id palette id
* @return error code: 0 on success, -1 on error, -2 on fatal error (only TCP connection)
__IRDIRECTSDK_API__ int evo_irimager_set_palette(int id);
*/
const colorPalette = {
eAlarmBlue :1,
eAlarmBlueHi :2,
eGrayBW :3,
eGrayWB :4,
eAlarmGreen :5,
eIron :6,
eIronHi :7,
eMedical :8,
eRainbow :9,
eRainbowHi :10,
eAlarmRed :11
}
// TODO: to be tested
var set_palette = function(id) {
let res = lib.evo_irimager_set_palette(id)
if (res !== 0) {
throw new Error("Impossible to get set the palette color")
}
}
/**
* @brief sets shutter flag control mode
* @param mode 0 means manual control, 1 means automate
* @return error code: 0 on success, -1 on error, -2 on fatal error (only TCP connection)
__IRDIRECTSDK_API__ int evo_irimager_set_shutter_mode(int mode);
*/
// TODO: to be tested
var set_shutter_mode = function(mode) {
let res = lib.evo_irimager_set_shutter_mode(mode)
if (res !== 0) {
throw new Error("Impossible to get set the shutter mode, please enter 0 for manual control and 1 for auto mode")
}
}
/**
* @brief forces a shutter flag cycle
* @return error code: 0 on success, -1 on error, -2 on fatal error (only TCP connection)
__IRDIRECTSDK_API__ int evo_irimager_trigger_shutter_flag();
*/
// TODO: to be tested
var trigger_shutter_flag = function() {
let res = lib.evo_irimager_trigger_shutter_flag()
if (res !== 0) {
throw new Error("Impossible to trigger the shutter flag")
}
}
/**
* Launch TCP daemon
* @return error code: 0 on success, -1 on error, -2 on fatal error (only TCP connection)
__IRDIRECTSDK_API__ int evo_irimager_daemon_launch();
*/
// TODO: to be tested
var daemon_launch = function() {
let res = lib.evo_irimager_daemon_launch()
if (res !== 0) {
throw new Error("Impossible to launch the TCP daemon")
}
}
/**
* Check whether daemon is already running
* @return error code: 0 daemon is already active, -1 daemon is not started yet
__IRDIRECTSDK_API__ int evo_irimager_daemon_is_running();
*/
// TODO: to be tested
var daemon_is_running = function() {
let res = lib.evo_irimager_daemon_is_running()
if (res !== 0) {
throw new Error("Impossible to retrieve if daemon is running correctly")
}
}
/**
* Kill TCP daemon
* @return error code: 0 on success, -1 on error, -2 on fatal error (only TCP connection)
__IRDIRECTSDK_API__ int evo_irimager_daemon_kill();
*/
var daemon_kill = function() {
let res = lib.evo_irimager_daemon_kill()
if (res !== 0) {
throw new Error("Impossible to kill the TCP daemon")
}
}
// Exports functions
module.exports.loadDLL = loadDLL
module.exports.usb_init = usb_init
module.exports.tcp_init = tcp_init
module.exports.terminate = terminate
module.exports.get_thermal_image_size = get_thermal_image_size
module.exports.get_palette_image_size = get_palette_image_size
module.exports.get_thermal_image = get_thermal_image
module.exports.get_palette_image = get_palette_image
module.exports.get_thermal_palette_image = get_thermal_palette_image
module.exports.set_palette = set_palette
module.exports.colorPalette = colorPalette
module.exports.set_shutter_mode = set_shutter_mode
module.exports.trigger_shutter_flag = trigger_shutter_flag
module.exports.daemon_launch = daemon_launch
module.exports.daemon_is_running = daemon_is_running
module.exports.daemon_kill = daemon_kill