This repository has been archived by the owner on Nov 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
1083 lines (933 loc) · 40.6 KB
/
lib.rs
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! See [Toupcam](struct.Toupcam.html).
extern crate libc;
#[macro_use]
extern crate bitflags;
use std::str;
use std::ptr::{null, null_mut};
use std::sync::mpsc::{sync_channel, channel, SyncSender, Receiver};
use std::thread;
use std::ffi::CStr;
use libc::{c_void, c_char, c_uchar, c_short, c_ushort, c_int, c_uint, c_double};
#[repr(i32)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[must_use]
#[allow(overflowing_literals, non_camel_case_types, dead_code)]
enum HRESULT {
S_OK = 0x00000000, /* Operation successful */
S_FALSE = 0x00000001, /* Operation successful */
E_FAIL = 0x80004005 as i32, /* Unspecified failure */
E_INVALIDARG = 0x80070057 as i32, /* One or more arguments are not valid */
E_NOTIMPL = 0x80004001 as i32, /* Not supported or not implemented */
E_POINTER = 0x80004003 as i32, /* Pointer that is not valid */
E_UNEXPECTED = 0x8000FFFF as i32, /* Unexpected failure */
}
bitflags! {
flags Flags: u32 {
const FLAG_CMOS = 0x00000001, /* cmos sensor */
const FLAG_CCD_PROGRESSIVE = 0x00000002, /* progressive ccd sensor */
const FLAG_CCD_INTERLACED = 0x00000004, /* interlaced ccd sensor */
const FLAG_ROI_HARDWARE = 0x00000008, /* support hardware ROI */
const FLAG_MONO = 0x00000010, /* monochromatic */
const FLAG_BINSKIP_SUPPORTED = 0x00000020, /* support bin/skip mode, see Toupcam_put_Mode and Toupcam_get_Mode */
const FLAG_USB30 = 0x00000040, /* USB 3.0 */
const FLAG_COOLED = 0x00000080, /* Cooled */
const FLAG_USB30_OVER_USB20 = 0x00000100, /* usb3.0 camera connected to usb2.0 port */
const FLAG_ST4 = 0x00000200, /* ST4 */
const FLAG_GETTEMPERATURE = 0x00000400, /* support to get the temperature of sensor */
const FLAG_PUTTEMPERATURE = 0x00000800, /* support to put the temperature of sensor */
const FLAG_BITDEPTH10 = 0x00001000, /* Maximum Bit Depth = 10 */
const FLAG_BITDEPTH12 = 0x00002000, /* Maximum Bit Depth = 12 */
const FLAG_BITDEPTH14 = 0x00004000, /* Maximum Bit Depth = 14 */
const FLAG_BITDEPTH16 = 0x00008000, /* Maximum Bit Depth = 16 */
const FLAG_FAN = 0x00010000, /* cooling fan */
const FLAG_COOLERONOFF = 0x00020000, /* cooler can be turn on or off */
const FLAG_ISP = 0x00040000, /* image signal processing supported */
const FLAG_TRIGGER = 0x00080000, /* support the trigger mode */
}
}
#[repr(u32)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Event {
Exposure = 0x0001, /* exposure time changed */
TempTint = 0x0002, /* white balance changed, Temp/Tint mode */
Chrome = 0x0003, /* reversed, do not use it */
Image = 0x0004, /* live image arrived, use Toupcam_PullImage to get this image */
StillImage = 0x0005, /* snap (still) frame arrived, use Toupcam_PullStillImage to get this frame */
WBGain = 0x0006, /* white balance changed, RGB Gain mode */
Error = 0x0080, /* something error happens */
Disconnected = 0x0081 /* camera disconnected */
}
#[repr(u32)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[allow(dead_code)]
enum Option {
NoFrameTimeout = 0x01, /* iValue: 1 = enable; 0 = disable. default: enable */
ThreadPriority = 0x02, /* set the priority of the internal thread which grab data from the usb device. iValue: 0 = THREAD_PRIORITY_NORMAL; 1 = THREAD_PRIORITY_ABOVE_NORMAL; 2 = THREAD_PRIORITY_HIGHEST; default: 0; see: msdn SetThreadPriority */
ProcessMode = 0x03, /* 0 = better image quality, more cpu usage. this is the default value
1 = lower image quality, less cpu usage */
Raw = 0x04, /* raw mode, read the sensor data. This can be set only BEFORE Toupcam_StartXXX() */
Histogram = 0x05, /* 0 = only one, 1 = continue mode */
BitDepth = 0x06, /* 0 = 8bits mode, 1 = 16bits mode */
Fan = 0x07, /* 0 = turn off the cooling fan, 1 = turn on the cooling fan */
Cooler = 0x08, /* 0 = turn off cooler, 1 = turn on cooler */
Linear = 0x09, /* 0 = turn off tone linear, 1 = turn on tone linear */
Curve = 0x0a, /* 0 = turn off tone curve, 1 = turn on tone curve */
Trigger = 0x0b, /* 0 = continuous mode, 1 = trigger mode, default value = 0 */
RGB48 = 0x0c /* enable RGB48 format when bitdepth > 8 */
}
#[repr(C)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Resolution {
pub width : u32,
pub height : u32,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Image {
pub resolution : Resolution,
pub bits : u32,
pub data : Vec<u8>,
}
#[repr(u32)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Layout {
GBRG = 0x47524247,
RGGB = 0x42474752,
BGGR = 0x52474742,
GRBG = 0x47425247,
YUYV = 0x56595559,
YYYY = 0x59595959,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Format {
pub fourcc : Layout,
pub bit_depth : u32,
}
#[repr(C)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Rect {
pub left : u32,
pub top : u32,
pub right : u32,
pub bottom : u32,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Range<T> {
pub minimum : T,
pub maximum : T,
pub default : T,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(i32)]
pub enum Flicker {
AC60Hz = 0,
AC50Hz = 1,
DC = 2,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(i32)]
pub enum SamplingMode {
Bin = 0,
Skip = 1,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct TempTint {
pub temperature : u32,
pub tint : u32,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct WhiteBalanceTempTint {
pub temperature : u32,
pub tint : u32,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct WhiteBalanceRGB {
pub red : i32,
pub green : i32,
pub blue : i32,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct LevelRanges {
pub red : (u16, u16),
pub green : (u16, u16),
pub blue : (u16, u16),
pub gray : (u16, u16),
}
#[repr(u16)]
pub enum LEDState {
Off = 0,
On = 1,
Flashing = 2,
}
#[repr(C)]
struct ModelInternal {
name : *const c_char,
flags : u32,
maxspeed : c_uint,
preview : c_uint,
still : c_uint,
res : [Resolution; 16],
}
#[derive(Clone, Debug)]
pub struct Model
{
pub name : String,
pub flags : Flags,
pub maximum_speed : u32,
pub preview_resolutions : Vec<Resolution>,
pub still_resolutions : Vec<Resolution>,
}
#[repr(C)]
struct InstanceInternal {
displayname : [c_char; 64],
id : [c_char; 64],
model : *const ModelInternal,
}
#[derive(Clone, Debug)]
pub struct Instance
{
pub display_name : String, /* display name */
pub unique_id : String, /* unique and opaque id of a connected camera */
pub model : Model,
}
type Handle = c_void;
/* FFI functions */
#[link(name = "toupcam")]
#[allow(dead_code)]
extern {
fn Toupcam_Version() -> *const c_char;
fn Toupcam_HotPlug(pHotPlugCallback: std::option::Option<extern fn(*mut c_void)>,
pCallbackCtx: *mut c_void);
fn Toupcam_Enum(pti: *mut [InstanceInternal; 16]) -> c_uint;
fn Toupcam_Open(id: *const c_char) -> *mut Handle;
fn Toupcam_Close(h: *mut Handle);
fn Toupcam_get_SerialNumber(h: *mut Handle, sn: *mut [c_char; 32]) -> HRESULT;
fn Toupcam_get_ProductionDate(h: *mut Handle, pdate: *mut [c_char; 10]) -> HRESULT;
fn Toupcam_StartPullModeWithCallback(h: *mut Handle,
pEventCallback: extern fn(Event, *mut c_void),
pCallbackCtx: *mut c_void) -> HRESULT;
fn Toupcam_PullImage(h: *mut Handle, pImageData: *mut u8, bits: c_int,
pnWidth: *mut c_uint, pnHeight: *mut c_uint) -> HRESULT;
fn Toupcam_PullStillImage(h: *mut Handle, pImageData: *mut u8, bits: c_int,
pnWidth: *mut c_uint, pnHeight: *mut c_uint) -> HRESULT;
fn Toupcam_Stop(h: *mut Handle) -> HRESULT;
fn Toupcam_Pause(h: *mut Handle, bPause: c_int) -> HRESULT;
fn Toupcam_get_FwVersion(h: *mut Handle, fwver: *mut [c_char; 16]) -> HRESULT;
fn Toupcam_get_HwVersion(h: *mut Handle, hwver: *mut [c_char; 16]) -> HRESULT;
fn Toupcam_get_MaxSpeed(h: *mut Handle) -> HRESULT;
fn Toupcam_get_MaxBitDepth(h: *mut Handle) -> HRESULT;
fn Toupcam_get_RawFormat(h: *mut Handle,
nFourCC: *mut Layout, bitdepth: *mut c_uint) -> HRESULT;
fn Toupcam_get_ResolutionNumber(h: *mut Handle) -> HRESULT;
fn Toupcam_get_Resolution(h: *mut Handle, nResolutionIndex: c_uint,
pWidth: *mut c_int, pHeight: *mut c_int) -> HRESULT;
fn Toupcam_get_ResolutionRatio(h: *mut Handle, nResolutionIndex: c_uint,
pNumerator: *mut c_int, pDenominator: *mut c_int) -> HRESULT;
fn Toupcam_put_Size(h: *mut Handle, nWidth: c_int, nHeight: c_int) -> HRESULT;
fn Toupcam_get_Size(h: *mut Handle, nWidth: *mut c_int, nHeight: *mut c_int) -> HRESULT;
fn Toupcam_put_eSize(h: *mut Handle, nResolutionIndex: c_uint) -> HRESULT;
fn Toupcam_get_eSize(h: *mut Handle, nResolutionIndex: *mut c_uint) -> HRESULT;
fn Toupcam_Trigger(h: *mut Handle) -> HRESULT;
fn Toupcam_get_StillResolutionNumber(h: *mut Handle) -> HRESULT;
fn Toupcam_get_StillResolution(h: *mut Handle, nIndex: c_uint,
pWidth: *mut c_int, pHeight: *mut c_int) -> HRESULT;
fn Toupcam_Snap(h: *mut Handle, nResolutionIndex: c_uint) -> HRESULT;
fn Toupcam_put_RealTime(h: *mut Handle, bEnable: c_int) -> HRESULT;
fn Toupcam_get_RealTime(h: *mut Handle, bEnable: *mut c_int) -> HRESULT;
fn Toupcam_get_Temperature(h: *mut Handle, pTemperature: *mut c_short) -> HRESULT;
fn Toupcam_put_Temperature(h: *mut Handle, nTemperature: c_short) -> HRESULT;
fn Toupcam_get_Roi(h: *mut Handle, pxOffset: *mut c_uint, pyOffset: *mut c_uint,
pxWidth: *mut c_uint, pyHeight: *mut c_uint) -> HRESULT;
fn Toupcam_put_Roi(h: *mut Handle, xOffset: c_uint, yOffset: c_uint,
xWidth: c_uint, yHeight: c_uint) -> HRESULT;
fn Toupcam_get_AutoExpoEnable(h: *mut Handle, bAutoExposure: *mut c_int) -> HRESULT;
fn Toupcam_put_AutoExpoEnable(h: *mut Handle, bAutoExposure: c_int) -> HRESULT;
fn Toupcam_get_AutoExpoTarget(h: *mut Handle, Target: *mut c_ushort) -> HRESULT;
fn Toupcam_put_AutoExpoTarget(h: *mut Handle, Target: c_ushort) -> HRESULT;
fn Toupcam_put_MaxAutoExpoTimeAGain(h: *mut Handle,
maxTime: c_uint, maxAGain: c_ushort) -> HRESULT;
fn Toupcam_get_ExpoTime(h: *mut Handle, Time: *mut c_uint) -> HRESULT;
fn Toupcam_put_ExpoTime(h: *mut Handle, Time: c_uint) -> HRESULT;
fn Toupcam_get_ExpTimeRange(h: *mut Handle,
nMin: *mut c_uint, nMax: *mut c_uint,
nDef: *mut c_uint) -> HRESULT;
fn Toupcam_get_ExpoAGain(h: *mut Handle, AGain: *mut c_ushort) -> HRESULT;
fn Toupcam_put_ExpoAGain(h: *mut Handle, AGain: c_ushort) -> HRESULT;
fn Toupcam_get_ExpoAGainRange(h: *mut Handle,
nMin: *mut c_ushort, nMax: *mut c_ushort,
nDef: *mut c_ushort) -> HRESULT;
fn Toupcam_put_AEAuxRect(h: *mut Handle, pAuxRect: *const Rect) -> HRESULT;
fn Toupcam_get_AEAuxRect(h: *mut Handle, pAuxRect: *mut Rect) -> HRESULT;
fn Toupcam_put_Hue(h: *mut Handle, Hue: c_int) -> HRESULT;
fn Toupcam_get_Hue(h: *mut Handle, Hue: *mut c_int) -> HRESULT;
fn Toupcam_put_Saturation(h: *mut Handle, Saturation: c_int) -> HRESULT;
fn Toupcam_get_Saturation(h: *mut Handle, Saturation: *mut c_int) -> HRESULT;
fn Toupcam_put_Brightness(h: *mut Handle, Brightness: c_int) -> HRESULT;
fn Toupcam_get_Brightness(h: *mut Handle, Brightness: *mut c_int) -> HRESULT;
fn Toupcam_get_Contrast(h: *mut Handle, Contrast: *mut c_int) -> HRESULT;
fn Toupcam_put_Contrast(h: *mut Handle, Contrast: c_int) -> HRESULT;
fn Toupcam_get_Gamma(h: *mut Handle, Gamma: *mut c_int) -> HRESULT;
fn Toupcam_put_Gamma(h: *mut Handle, Gamma: c_int) -> HRESULT;
fn Toupcam_get_Chrome(h: *mut Handle, bChrome: *mut c_int) -> HRESULT;
fn Toupcam_put_Chrome(h: *mut Handle, bChrome: c_int) -> HRESULT;
fn Toupcam_get_MonoMode(h: *mut Handle) -> HRESULT;
fn Toupcam_get_VFlip(h: *mut Handle, bVFlip: *mut c_int) -> HRESULT;
fn Toupcam_put_VFlip(h: *mut Handle, bVFlip: c_int) -> HRESULT;
fn Toupcam_get_HFlip(h: *mut Handle, bHFlip: *mut c_int) -> HRESULT;
fn Toupcam_put_HFlip(h: *mut Handle, bHFlip: c_int) -> HRESULT;
fn Toupcam_get_Negative(h: *mut Handle, bNegative: *mut c_int) -> HRESULT;
fn Toupcam_put_Negative(h: *mut Handle, bNegative: c_int) -> HRESULT;
fn Toupcam_put_Speed(h: *mut Handle, nSpeed: c_ushort) -> HRESULT;
fn Toupcam_get_Speed(h: *mut Handle, pSpeed: *mut c_ushort) -> HRESULT;
fn Toupcam_put_HZ(h: *mut Handle, nHZ: Flicker) -> HRESULT;
fn Toupcam_get_HZ(h: *mut Handle, nHZ: *mut Flicker) -> HRESULT;
fn Toupcam_put_Mode(h: *mut Handle, bSkip: SamplingMode) -> HRESULT;
fn Toupcam_get_Mode(h: *mut Handle, bSkip: *mut SamplingMode) -> HRESULT;
fn Toupcam_put_TempTint(h: *mut Handle, nTemp: c_int, nTint: c_int) -> HRESULT;
fn Toupcam_get_TempTint(h: *mut Handle, nTemp: *mut c_int, nTint: *mut c_int) -> HRESULT;
fn Toupcam_AwbOnePush(h: *mut Handle,
fnTTProc: std::option::Option<extern fn(c_int, c_int, *mut c_void)>,
pTTCtx: *mut c_void) -> HRESULT;
fn Toupcam_put_WhiteBalanceGain(h: *mut Handle, aGain: *const [c_int; 3]) -> HRESULT;
fn Toupcam_get_WhiteBalanceGain(h: *mut Handle, aGain: *mut [c_int; 3]) -> HRESULT;
fn Toupcam_AwbInit(h: *mut Handle,
fnWBProc: std::option::Option<extern fn(*const [c_int; 3], *mut c_void)>,
pWBCtx: *mut c_void) -> HRESULT;
fn Toupcam_put_AWBAuxRect(h: *mut Handle, pAuxRect: *const Rect) -> HRESULT;
fn Toupcam_get_AWBAuxRect(h: *mut Handle, pAuxRect: *mut Rect) -> HRESULT;
fn Toupcam_put_LevelRange(h: *mut Handle,
aLow: *const [c_ushort; 4], aHigh: *const [c_ushort; 4]) -> HRESULT;
fn Toupcam_get_LevelRange(h: *mut Handle,
aLow: *mut [c_ushort; 4], aHigh: *mut [c_ushort; 4]) -> HRESULT;
fn Toupcam_LevelRangeAuto(h: *mut Handle) -> HRESULT;
fn Toupcam_put_ExpoCallback(h: *mut Handle,
fnExpoProc: std::option::Option<extern fn(*mut c_void)>,
pExpoCtx: *mut c_void) -> HRESULT;
fn Toupcam_put_ChromeCallback(h: *mut Handle,
fnChromeProc: std::option::Option<extern fn(*mut c_void)>,
pChromeCtx: *mut c_void) -> HRESULT;
fn Toupcam_put_LEDState(h: *mut Handle, iLed: c_ushort,
iState: LEDState, iPeriod: c_ushort) -> HRESULT;
fn Toupcam_write_EEPROM(h: *mut Handle, addr: c_uint,
pData: *const u8, nDataLen: c_uint) -> HRESULT;
fn Toupcam_read_EEPROM(h: *mut Handle, addr: c_uint,
pBuffer: *mut u8, nBufferLen: c_uint) -> HRESULT;
fn Toupcam_put_Option(h: *mut Handle, iOption: Option, iValue: c_uint) -> HRESULT;
fn Toupcam_get_Option(h: *mut Handle, iOption: Option, iValue: *mut c_uint) -> HRESULT;
fn Toupcam_GetHistogram(h: *mut Handle,
fnHistogramProc: extern fn(*const [c_double; 256],
*const [c_double; 256],
*const [c_double; 256],
*const [c_double; 256],
*mut c_void),
pHistogramCtx: *mut c_void) -> HRESULT;
fn Toupcam_calc_ClarityFactor(pImageData: *const u8, bits: c_int,
nImgWidth: c_uint, nImgHeight: c_uint) -> c_double;
fn Toupcam_deBayer(nBayer: c_uint, nW: c_int, nH: c_int,
input: *const u8, output: *mut u8, nBitDepth: c_uchar);
}
/* Helper functions */
fn accept(result: HRESULT) {
match result {
HRESULT::S_OK | HRESULT::S_FALSE => (),
_ => panic!("toupcam: {:?}", result)
}
}
fn accept_u32(result: HRESULT) -> u32 {
accept(result);
result as u32
}
unsafe fn unmarshal_static_string(buf: *const c_char) -> &'static str {
str::from_utf8(CStr::from_ptr(buf).to_bytes()).unwrap()
}
unsafe fn unmarshal_string(buf: *const c_char) -> String {
str::from_utf8(CStr::from_ptr(buf).to_bytes()).unwrap().to_owned()
}
unsafe fn unmarshal_strary(ary: &[c_char]) -> String {
unmarshal_string(&ary[0])
}
macro_rules! property {
(bool, $reader:ident, $writer:ident, $raw_reader:ident, $raw_writer:ident) =>
(
pub fn $reader(&self) -> bool {
unsafe {
let mut value = 0;
accept($raw_reader(self.handle, &mut value));
value == 1
}
}
pub fn $writer(&self, value: bool) {
unsafe { accept($raw_writer(self.handle, value as c_int)) }
}
);
(i16, $reader:ident, $writer:ident, $raw_reader:ident, $raw_writer:ident) =>
(
pub fn $reader(&self) -> i16 {
unsafe {
let mut value = 0;
accept($raw_reader(self.handle, &mut value));
value as i16
}
}
pub fn $writer(&self, value: i16) {
unsafe { accept($raw_writer(self.handle, value as c_short)) }
}
);
(u16, $reader:ident, $writer:ident, $raw_reader:ident, $raw_writer:ident) =>
(
pub fn $reader(&self) -> u16 {
unsafe {
let mut value = 0;
accept($raw_reader(self.handle, &mut value));
value as u16
}
}
pub fn $writer(&self, value: u16) {
unsafe { accept($raw_writer(self.handle, value as c_ushort)) }
}
);
(i32, $reader:ident, $writer:ident, $raw_reader:ident, $raw_writer:ident) =>
(
pub fn $reader(&self) -> i32 {
unsafe {
let mut value = 0;
accept($raw_reader(self.handle, &mut value));
value as i32
}
}
pub fn $writer(&self, value: i32) {
unsafe { accept($raw_writer(self.handle, value as c_int)) }
}
); (u32, $reader:ident, $writer:ident, $raw_reader:ident, $raw_writer:ident) =>
(
pub fn $reader(&self) -> u32 {
unsafe {
let mut value = 0;
accept($raw_reader(self.handle, &mut value));
value as u32
}
}
pub fn $writer(&self, value: u32) {
unsafe { accept($raw_writer(self.handle, value as c_uint)) }
}
);
(Rect, $reader:ident, $writer:ident, $raw_reader:ident, $raw_writer:ident) =>
(
pub fn $reader(&self) -> Rect {
unsafe {
let mut value = std::mem::zeroed();
accept($raw_reader(self.handle, &mut value));
value
}
}
pub fn $writer(&self, value: Rect) {
unsafe { accept($raw_writer(self.handle, &value)) }
}
);
(bool option, $reader:ident, $writer:ident, $option:expr) =>
(
pub fn $reader(&self) -> bool {
unsafe {
let mut value = 0;
accept(Toupcam_get_Option(self.handle, $option, &mut value));
value == 1
}
}
pub fn $writer(&self, value: bool) {
unsafe { accept(Toupcam_put_Option(self.handle, $option, value as c_uint)) }
}
)
}
/* API wrapper */
pub struct Toupcam {
handle: *mut Handle
}
impl Toupcam {
pub fn version() -> &'static str {
unsafe { unmarshal_static_string(Toupcam_Version()) }
}
pub fn hotplug<F>(mut body: F) where F: FnMut(&Receiver<()>) {
extern fn wrapper(sender: *mut c_void) {
unsafe { (*(sender as *const SyncSender<()>)).send(()).unwrap() }
}
struct Guard;
impl Drop for Guard {
fn drop(&mut self) {
unsafe { Toupcam_HotPlug(None, null_mut()) }
}
}
let (tx, rx) = sync_channel(0);
unsafe { Toupcam_HotPlug(Some (wrapper), &tx as *const _ as *mut c_void) };
let _guard = Guard;
body(&rx)
}
pub fn enumerate() -> Vec<Instance> {
let mut instances = Vec::new();
unsafe {
let mut i_instances: [InstanceInternal; 16] = std::mem::zeroed();
for i in 0..Toupcam_Enum(&mut i_instances) {
let i_inst = &i_instances[i as usize];
let i_model = i_inst.model;
instances.push(Instance {
display_name: unmarshal_strary(&(*i_inst).displayname),
unique_id: unmarshal_strary(&(*i_inst).id),
model: Model {
name: unmarshal_string((*i_model).name),
flags: Flags::from_bits((*i_model).flags).unwrap(),
maximum_speed: (*i_model).maxspeed,
preview_resolutions:
(*i_model).res[..(*i_model).preview as usize].to_owned(),
still_resolutions:
(*i_model).res[..(*i_model).still as usize].to_owned(),
},
})
}
}
instances
}
pub fn open(unique_id: std::option::Option<&str>) -> std::option::Option<Toupcam> {
let id =
match unique_id {
None => null(),
Some(str) => str.as_ptr() as *const c_char
};
let handle = unsafe { Toupcam_Open(id) };
if handle.is_null() {
None
} else {
Some(Toupcam { handle: handle })
}
}
pub fn serial_number(&self) -> String {
unsafe {
let mut ret: [c_char; 32] = std::mem::zeroed();
accept(Toupcam_get_SerialNumber(self.handle, &mut ret));
unmarshal_string(&ret[0])
}
}
pub fn production_date(&self) -> String {
unsafe {
let mut ret: [c_char; 10] = std::mem::zeroed();
accept(Toupcam_get_ProductionDate(self.handle, &mut ret));
unmarshal_string(&ret[0])
}
}
pub fn start<F>(&self, mut body: F) where F: FnMut(&Receiver<Event>) {
extern fn wrapper(event: Event, sender: *mut c_void) {
unsafe { (*(sender as *const SyncSender<Event>)).try_send(event).unwrap() }
}
struct Guard(*mut Handle);
impl Drop for Guard {
fn drop(&mut self) {
// ignore errors in a destructor
unsafe { let _ = Toupcam_Stop(self.0); }
}
}
let (sync_tx, sync_rx) = sync_channel(64); /* can't allocate inside the callback */
unsafe { accept(Toupcam_StartPullModeWithCallback(
self.handle, wrapper, &sync_tx as *const _ as *mut c_void)) };
let _guard = Guard(self.handle);
let (tx, rx) = channel();
thread::Builder::new()
.name(String::from("touptek::start() proxy"))
.spawn(move || {
loop {
match sync_rx.recv() {
Err(_) => break,
Ok(value) => {
match tx.send(value) {
Err(_) => break,
Ok(_) => ()
}
}
}
}
})
.unwrap();
body(&rx)
}
fn buffer_size(&self, bits: u32, width: u32, height: u32) -> usize {
#[allow(non_snake_case)]
fn DIBWIDTHBYTES(bits: u32) -> u32 { ((bits + 31) & !31) / 8 }
(if self.is_raw_capture_enabled() {
if self.is_rgb48_format_enabled() {
width * height * 2
} else {
width * height
}
} else {
match bits {
24 => DIBWIDTHBYTES(24 * width) * height,
32 => width * height * 4,
48 => DIBWIDTHBYTES(48 * width) * height,
8 => DIBWIDTHBYTES(8 * width) * height,
_ => unreachable!()
}
}) as usize
}
pub fn pull_image(&self, bits: u32) -> Image {
unsafe {
let (mut width, mut height) = std::mem::zeroed();
accept(Toupcam_PullImage(self.handle, null_mut(), bits as i32,
&mut width, &mut height));
let data_size = self.buffer_size(bits, width, height);
let mut data: Vec<u8> = Vec::with_capacity(data_size);
accept(Toupcam_PullImage(self.handle, data.as_mut_ptr(), bits as i32,
null_mut(), null_mut()));
data.set_len(data_size);
Image {
resolution: Resolution { width: width, height: height },
bits: bits,
data: data
}
}
}
pub fn pull_still_image(&self, bits: u32) -> Image {
unsafe {
let (mut width, mut height) = std::mem::zeroed();
accept(Toupcam_PullStillImage(self.handle, null_mut(), bits as i32,
&mut width, &mut height));
let mut data = vec![0; self.buffer_size(bits, width, height)];
accept(Toupcam_PullStillImage(self.handle, data.as_mut_ptr(), bits as i32,
null_mut(), null_mut()));
Image {
resolution: Resolution { width: width, height: height },
bits: bits,
data: data
}
}
}
pub fn pause(&self, do_pause: bool) {
unsafe {
accept(Toupcam_Pause(self.handle, if do_pause { 1 } else { 0 }));
}
}
pub fn firmware_version(&self) -> String {
unsafe {
let mut ret: [c_char; 16] = std::mem::zeroed();
accept(Toupcam_get_FwVersion(self.handle, &mut ret));
unmarshal_string(&ret[0])
}
}
pub fn hardware_version(&self) -> String {
unsafe {
let mut ret: [c_char; 16] = std::mem::zeroed();
accept(Toupcam_get_HwVersion(self.handle, &mut ret));
unmarshal_string(&ret[0])
}
}
pub fn maximum_bit_depth(&self) -> u32 {
unsafe {
accept_u32(Toupcam_get_MaxBitDepth(self.handle))
}
}
pub fn raw_format(&self) -> Format {
unsafe {
let (mut fourcc, mut bit_depth) = std::mem::zeroed();
accept(Toupcam_get_RawFormat(self.handle, &mut fourcc, &mut bit_depth));
Format { fourcc: fourcc, bit_depth: bit_depth }
}
}
pub fn preview_resolutions(&self) -> Vec<Resolution> {
let mut resolutions = Vec::new();
unsafe {
for i in 0..accept_u32(Toupcam_get_ResolutionNumber(self.handle)) {
let (mut w, mut h) = (0, 0);
accept(Toupcam_get_Resolution(self.handle, i, &mut w, &mut h));
resolutions.push(Resolution { width: w as u32, height: h as u32 })
}
}
resolutions
}
pub fn preview_size(&self) -> Resolution {
unsafe {
let (mut w, mut h) = (0, 0);
accept(Toupcam_get_Size(self.handle, &mut w, &mut h));
Resolution { width: w as u32, height: h as u32 }
}
}
pub fn set_preview_size(&self, res: Resolution) {
unsafe {
accept(Toupcam_put_Size(self.handle, res.width as c_int, res.height as c_int))
}
}
pub fn preview_size_index(&self) -> usize {
unsafe {
let mut index = 0;
accept(Toupcam_get_eSize(self.handle, &mut index));
index as usize
}
}
pub fn set_preview_size_index(&self, index: usize) {
unsafe {
accept(Toupcam_put_eSize(self.handle, index as u32))
}
}
pub fn still_resolutions(&self) -> Vec<Resolution> {
let mut resolutions = Vec::new();
unsafe {
for i in 0..accept_u32(Toupcam_get_StillResolutionNumber(self.handle)) {
let (mut w, mut h) = (0, 0);
accept(Toupcam_get_StillResolution(self.handle, i, &mut w, &mut h));
resolutions.push(Resolution { width: w as u32, height: h as u32 })
}
}
resolutions
}
pub fn snap(&self, res: Resolution) {
let index = self.still_resolutions().iter().position(|r| *r == res).unwrap();
self.snap_index(index)
}
pub fn snap_index(&self, index: usize) {
unsafe {
accept(Toupcam_Snap(self.handle, index as u32))
}
}
property!(bool, is_real_time, set_real_time,
Toupcam_get_RealTime, Toupcam_put_RealTime);
property!(i16, sensor_temperature, set_sensor_temperature,
Toupcam_get_Temperature, Toupcam_put_Temperature);
pub fn rectangle_of_interest(&self) -> Rect {
unsafe {
let (mut left, mut top, mut width, mut height) = (0, 0, 0, 0);
accept(Toupcam_get_Roi(self.handle, &mut left, &mut top, &mut width, &mut height));
Rect { left: left, top: top, right: left + width, bottom: top + height }
}
}
pub fn set_rectangle_of_interest(&self, value: Rect) {
unsafe {
accept(Toupcam_put_Roi(self.handle, value.left, value.top,
value.right - value.left, value.bottom - value.top))
}
}
property!(bool, is_automatic_exposure, set_automatic_exposure,
Toupcam_get_AutoExpoEnable, Toupcam_put_AutoExpoEnable);
property!(u16, automatic_exposure_target, set_automatic_exposure_target,
Toupcam_get_AutoExpoTarget, Toupcam_put_AutoExpoTarget);
pub fn set_maximum_exposure_time_and_gain(&self, max_time: u32, max_gain: u16) {
unsafe {
accept(Toupcam_put_MaxAutoExpoTimeAGain(self.handle, max_time, max_gain))
}
}
/* in microseconds */
property!(u32, exposure_time, set_exposure_time,
Toupcam_get_ExpoTime, Toupcam_put_ExpoTime);
pub fn exposure_time_range(&self) -> Range<u32> {
unsafe {
let (mut min, mut max, mut def) = (0, 0, 0);
accept(Toupcam_get_ExpTimeRange(self.handle, &mut min, &mut max, &mut def));
Range { minimum: min, maximum: max, default: def }
}
}
/* in percents */
property!(u16, exposure_gain, set_exposure_gain,
Toupcam_get_ExpoAGain, Toupcam_put_ExpoAGain);
pub fn exposure_gain_range(&self) -> Range<u16> {
unsafe {
let (mut min, mut max, mut def) = (0, 0, 0);
accept(Toupcam_get_ExpoAGainRange(self.handle, &mut min, &mut max, &mut def));
Range { minimum: min, maximum: max, default: def }
}
}
property!(Rect, automatic_exposure_area, set_automatic_exposure_area,
Toupcam_get_AEAuxRect, Toupcam_put_AEAuxRect);
property!(i32, hue, set_hue,
Toupcam_get_Hue, Toupcam_put_Hue);
property!(i32, saturation, set_saturation,
Toupcam_get_Saturation, Toupcam_put_Saturation);
property!(i32, brightness, set_brightness,
Toupcam_get_Brightness, Toupcam_put_Brightness);
property!(i32, contrast, set_contrast,
Toupcam_get_Contrast, Toupcam_put_Contrast);
property!(i32, gamma, set_gamma,
Toupcam_get_Gamma, Toupcam_put_Gamma);
property!(bool, is_monochromatic, set_monochromatic,
Toupcam_get_Chrome, Toupcam_put_Chrome);
property!(bool, is_flipped_vertically, set_flipped_vertically,
Toupcam_get_VFlip, Toupcam_put_VFlip);
property!(bool, is_flipped_horizontally, set_flipped_horizontally,
Toupcam_get_HFlip, Toupcam_put_HFlip);
property!(bool, is_negated, set_negated,
Toupcam_get_Negative, Toupcam_put_Negative);
property!(u16, speed, get_speed,
Toupcam_get_Speed, Toupcam_put_Speed);
pub fn maximum_speed(&self) -> u32 {
unsafe {
accept_u32(Toupcam_get_MaxSpeed(self.handle))
}
}
pub fn flicker_compensation(&self) -> Flicker {
unsafe {
let mut hz: Flicker = std::mem::zeroed();
accept(Toupcam_get_HZ(self.handle, &mut hz));
hz
}
}
pub fn set_flicker_compensation(&self, value: Flicker) {
unsafe {
accept(Toupcam_put_HZ(self.handle, value));
}
}
pub fn sampling_mode(&self) -> SamplingMode {
unsafe {
let mut mode: SamplingMode = std::mem::zeroed();
accept(Toupcam_get_Mode(self.handle, &mut mode));
mode
}
}
pub fn set_sampling_mode(&self, value: SamplingMode) {
unsafe {
accept(Toupcam_put_Mode(self.handle, value));
}
}
pub fn white_balance_temp_tint(&self) -> WhiteBalanceTempTint {
unsafe {
let (mut temp, mut tint) = (0, 0);
accept(Toupcam_get_TempTint(self.handle, &mut temp, &mut tint));
WhiteBalanceTempTint { temperature: temp as u32, tint: tint as u32 }
}
}
pub fn set_white_balance_temp_tint(&self, value: WhiteBalanceTempTint) {
unsafe {
accept(Toupcam_put_TempTint(self.handle, value.temperature as i32, value.tint as i32));
}
}
pub fn automatic_white_balance_oneshot(&self) {
unsafe {
accept(Toupcam_AwbOnePush(self.handle, None, null_mut()))
}
}
pub fn white_balance_rgb(&self) -> WhiteBalanceRGB {
unsafe {
let mut gain: [c_int; 3] = std::mem::zeroed();
accept(Toupcam_get_WhiteBalanceGain(self.handle, &mut gain));
WhiteBalanceRGB { red: gain[0], green: gain[1], blue: gain[2] }
}
}
pub fn set_white_balance_rgb(&self, value: WhiteBalanceRGB) {
unsafe {
let gain = [value.red, value.green, value.blue];
accept(Toupcam_put_WhiteBalanceGain(self.handle, &gain));
}
}
pub fn automatic_white_balance_continuous(&self) {
unsafe {
accept(Toupcam_AwbInit(self.handle, None, null_mut()))
}
}
property!(Rect, automatic_white_balance_area, set_automatic_white_balance_area,
Toupcam_get_AWBAuxRect, Toupcam_put_AWBAuxRect);
pub fn level_ranges(&self) -> LevelRanges {
unsafe {
let mut low: [c_ushort; 4] = std::mem::zeroed();
let mut high: [c_ushort; 4] = std::mem::zeroed();
accept(Toupcam_get_LevelRange(self.handle, &mut low, &mut high));
LevelRanges { red: (low[0], high[0]), green: (low[1], high[1]),
blue: (low[2], high[2]), gray: (low[3], high[3]) }
}
}
pub fn set_level_ranges(&self, value: LevelRanges) {
unsafe {
let low = [value.red.0, value.green.0, value.blue.0, value.gray.0];
let high = [value.red.1, value.green.1, value.blue.1, value.gray.1];
accept(Toupcam_put_LevelRange(self.handle, &low, &high));
}
}
pub fn automatic_level_ranges(&self) {
unsafe {
accept(Toupcam_LevelRangeAuto(self.handle));
}
}
pub fn set_led_state(&self, led_number: u16, state: LEDState, period: u16) {
unsafe {
accept(Toupcam_put_LEDState(self.handle, led_number, state, period))
}
}
pub unsafe fn read_eeprom(&self, address: u32, data: &mut [u8]) {
accept(Toupcam_read_EEPROM(self.handle, address, &mut data[0], data.len() as u32));
}
pub unsafe fn write_eeprom(&self, address: u32, data: &[u8]) {
accept(Toupcam_write_EEPROM(self.handle, address, &data[0], data.len() as u32));
}
property!(bool option, is_noframe_timeout_enabled, set_noframe_timeout_enabled,
Option::NoFrameTimeout);
property!(bool option, is_high_quality_enabled, set_high_quality_enabled,
Option::ProcessMode);
property!(bool option, is_raw_capture_enabled, set_raw_capture_enabled,
Option::Raw);