forked from Luomusha/node-twain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwainSession.cpp
1798 lines (1714 loc) · 53.7 KB
/
TwainSession.cpp
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
//
// Created by Lossa on 2022/7/4.
//
#include <napi.h>
#include "TwainSession.h"
TW_UINT16 message;
TW_UINT16 dsmCallback(pTW_IDENTITY pOrigin, pTW_IDENTITY pDest, TW_UINT32 uiDG, TW_UINT16 uiDAT, TW_UINT16 uiMSG, TW_MEMREF pData) {
std::cout << "Trigger callback" << std::endl;
switch(uiMSG) {
case MSG_XFERREADY:
std::cout << "Callback:" << "MSG_XFERREADY" << std::endl;
message = uiMSG;
break;
}
return TWRC_SUCCESS;
}
void TwainSession::fillIdentity(TW_IDENTITY id) {
identity = id;
}
TW_UINT16 TwainSession::loadDSM() {
TW_UINT16 rc = TWRC_FAILURE;
// load library
std::cout << "Load Library:" << kTWAIN_DSM_DIR << kTWAIN_DSM_DLL_NAME << std::endl;
pDSMLibrary = LOADLIBRARY(kTWAIN_DSM_DIR kTWAIN_DSM_DLL_NAME);
if (pDSMLibrary == NULL) {
std::cout << "Error - Could not load DSM:" << kTWAIN_DSM_DIR << kTWAIN_DSM_DLL_NAME << std::endl;
#ifdef TWH_CMP_GNU
std::cerr << "App - dlopen: " << dlerror() << std::endl;
#endif //TWH_CMP_GNU
return rc;
}
// Windows 验证TWAINDSM.dll签名
#ifdef TWH_CMP_MSC
WCHAR szPath[MAX_PATH];
if(GetModuleFileNameW(pDSMLibrary, szPath, MAX_PATH)) {
// if(!VerifyEmbeddedSignature(szPath)) {
// // Only continue using the DSM from trusted distributor
// freeDSM();
// return rc;
// }
}
#endif //TWH_CMP_MSC
// load function
dsmEntry = (DSMENTRYPROC) LOADFUNCTION(pDSMLibrary, "DSM_Entry");
#ifdef TWH_CMP_MSC // dlsym returning NULL is not an error on Unix
if (dsmEntry == NULL) {
std::cerr << "Error - Could not find DSM_Entry function in DSM:" << LIBRARY << std::endl;
return rc;
}
#endif //TWH_CMP_MSC
#ifdef TWH_CMP_GNU
bool error;
if(error = dlerror() != NULL) {
std::cerr << "App - dlsym:" << error << std::endl;
return rc;
}
#endif //TWH_CMP_GNU
state = 2;
rc = TWRC_SUCCESS;
return rc;
}
TW_UINT16 TwainSession::freeDSM() {
TW_UINT16 rc = TWRC_FAILURE;
if (state == 2) {
UNLOADLIBRARY(pDSMLibrary);
pDSMLibrary = NULL;
dsmEntry = NULL;
rc = TWRC_SUCCESS;
state = 1;
std::cout << "* State 2 -> 1 *" << std::endl;
} else {
status.ConditionCode = TWCC_SEQERROR;
rc = TWRC_FAILURE;
}
return rc;
}
TW_UINT16 TwainSession::entry(TW_UINT32 DG, TW_UINT16 DAT, TW_UINT16 MSG, TW_MEMREF pData, pTW_IDENTITY pDataSource) {
TW_UINT16 rc = TWRC_FAILURE;
status.ConditionCode = TWCC_SUCCESS;
rc = dsmEntry(&identity, pDataSource, DG, DAT, MSG, pData);
std::cout << "Triplet:"
<< convertDataGroupToString(DG)
<< " / "
<< convertDataArgTypeToString(DAT)
<< " / "
<< convertMessageToString(MSG)
<< std::endl;
std::cout << "RC:" << convertReturnCodeToString(rc) << std::endl;
if ((rc == TWRC_FAILURE) || (rc == TWRC_CHECKSTATUS)) {
TW_UINT16 erc = dsmEntry(&identity, pDataSource, DG_CONTROL, DAT_STATUS, MSG_GET, (TW_MEMREF) &status);
std::cout << "---------- ERROR -----------" << std::endl;
std::cout << "RC:" << convertReturnCodeToString(erc) << std::endl;
std::cout << "CC:" << convertConditionCodeToString(status.ConditionCode) << std::endl;
}
return rc;
}
TW_UINT16 TwainSession::openDSM() {
TW_UINT16 rc = TWRC_FAILURE;
if (state != 2) {
status.ConditionCode = TWCC_SEQERROR;
rc = TWRC_FAILURE;
std::cout << "You need to load the DSM first." << std::endl;
return rc;
}
rc = entry(DG_CONTROL, DAT_PARENT, MSG_OPENDSM, (TW_MEMREF) &parent);
if (rc == TWRC_SUCCESS && ((identity.SupportedGroups & DF_DSM2) == DF_DSM2)) {
gDSMEntry.Size = sizeof(TW_ENTRYPOINT);
rc = entry(DG_CONTROL, DAT_ENTRYPOINT, MSG_GET, (TW_MEMREF) &gDSMEntry);
}
if (rc == TWRC_SUCCESS) {
state = 3;
}
return rc;
}
TW_UINT16 TwainSession::closeDSM() {
TW_UINT16 rc = TWRC_FAILURE;
if (state == 3) {
rc = entry(DG_CONTROL, DAT_PARENT, MSG_CLOSEDSM, (TW_MEMREF) &parent);
if (rc == TWRC_SUCCESS) {
parent = NULL;
}
} else {
status.ConditionCode = TWCC_SEQERROR;
rc = TWRC_FAILURE;
}
return rc;
}
TW_UINT16 TwainSession::getDefaultDS() {
if (state < 3) {
std::cout << "getDefaultDS :: You need to open the DSM first." << state << std::endl;
return TWRC_FAILURE;
}
memset(&source, 0, sizeof(TW_IDENTITY));
TW_UINT16 rc = entry(DG_CONTROL, DAT_IDENTITY, MSG_GETDEFAULT, (TW_MEMREF) &source);
switch (rc) {
case TWRC_SUCCESS:
break;
case TWRC_FAILURE:
std::cerr << "Failed to get the data source info!" << std::endl;
}
return rc;
};
TW_UINT16 TwainSession::setDefaultDS(std::string name) {
if (state < 3) {
std::cout << "setDefaultDS :: You need to open the DSM first." << state << std::endl;
return TWRC_FAILURE;
} else if (state > 3) {
std::cout << "A source has already been opened, please close it first." << std::endl;
return TWRC_FAILURE;
}
TW_UINT16 rc = TWRC_FAILURE;
for (auto &it: sources) {
if (std::string(reinterpret_cast<char *>(it.ProductName)) == name) {
pSource = ⁢
rc = entry(DG_CONTROL, DAT_IDENTITY, MSG_SET, (TW_MEMREF) pSource);
switch (rc) {
case TWRC_SUCCESS:
break;
case TWRC_FAILURE:
std::cerr << "Failed to get the data source info!" << std::endl;
break;
}
}
}
return rc;
};
TW_UINT16 TwainSession::getSources() {
TW_UINT16 rc = TWRC_FAILURE;
if (state < 3) {
std::cout << "getSources :: You need to open the DSM first." << state << std::endl;
return rc;
}
// assert(true == sources.empty());
if(true != sources.empty()){
sources.clear();
}
TW_IDENTITY temp;
rc = entry(DG_CONTROL, DAT_IDENTITY, MSG_GETFIRST, (TW_MEMREF) &temp);
if (rc == TWRC_SUCCESS) {
sources.push_back(temp);
} else if (rc == TWRC_ENDOFLIST) {
std::cout << "TWRC_ENDOFLIST" << rc << std::endl;
}
while (rc == TWRC_SUCCESS) {
rc = entry(DG_CONTROL, DAT_IDENTITY, MSG_GETNEXT, (TW_MEMREF) &temp);
if (rc == TWRC_SUCCESS) {
sources.push_back(temp);
} else if (rc == TWRC_ENDOFLIST) {
std::cout << "while TWRC_ENDOFLIST" << rc << std::endl;
}
}
return rc;
}
TW_UINT16 TwainSession::openDS() {
TW_UINT16 rc = TWRC_FAILURE;
if (state != 3) {
status.ConditionCode = TWCC_SEQERROR;
rc = TWRC_FAILURE;
std::cout << "OpenDS Failed" << std::endl;
}
rc = entry(DG_CONTROL, DAT_IDENTITY, MSG_OPENDS, (TW_MEMREF) pSource);
std::cout << "openDS:rc:::::" << rc << std::endl;
if (rc == TWRC_SUCCESS) {
state = 4;
}
return rc;
}
TW_UINT16 TwainSession::openDS(std::string name) {
TW_UINT16 rc = TWRC_FAILURE;
if (state != 3) {
status.ConditionCode = TWCC_SEQERROR;
rc = TWRC_FAILURE;
std::cout << "OpenDS Failed" << std::endl;
}
for (auto &it: sources) {
if (std::string(reinterpret_cast<char *>(it.ProductName)) == name) {
pSource = ⁢
rc = entry(DG_CONTROL, DAT_IDENTITY, MSG_OPENDS, (TW_MEMREF) pSource);
switch (rc) {
case TWRC_SUCCESS:
state = 4;
break;
case TWRC_FAILURE:
std::cerr << "Failed to get the data source info!" << std::endl;
break;
}
return rc;
}
}
TW_IDENTITY defaultDS;
defaultDS.Id = 0;
rc = entry(DG_CONTROL, DAT_IDENTITY, MSG_OPENDS, (TW_MEMREF) &defaultDS);
std::cout << "openDS:rc:::::" << rc << std::endl;
if (rc == TWRC_SUCCESS) {
state = 4;
}
return rc;
}
TW_UINT16 TwainSession::closeDS() {
TW_UINT16 rc = TWRC_FAILURE;
if(state != 4) {
status.ConditionCode = TWCC_SEQERROR;
rc = TWRC_FAILURE;
std::cout << "CloseDS Failed" << std::endl;
}
rc = entry(DG_CONTROL, DAT_IDENTITY, MSG_CLOSEDS, (TW_MEMREF) pSource);
if(rc == TWRC_SUCCESS) {
state = 3;
}
return rc;
}
TW_UINT16 TwainSession::getCap(TW_CAPABILITY &cap) {
if (state < 4) {
std::cout << "You need to open a data source first." << std::endl;
return TWCC_SEQERROR;
}
if (cap.hContainer != NULL) {
freeMemory(cap.hContainer);
cap.hContainer = NULL;
}
cap.ConType = TWON_DONTCARE16;
TW_UINT16 rc = entry(DG_CONTROL, DAT_CAPABILITY, MSG_GET, (TW_MEMREF) &cap, pSource);
std::cout << "CAP_ORDER:" << convertCapToString(cap.Cap) << std::endl;
std::cout << "CAP_TYPE :" << convertConTypeToString(cap.ConType) << std::endl;
return rc;
}
TW_UINT16 TwainSession::getCurrentCap(TW_CAPABILITY &cap) {
if (state < 4) {
std::cout << "You need to open a data source first." << std::endl;
return TWCC_SEQERROR;
}
if (cap.hContainer != NULL) {
freeMemory(cap.hContainer);
cap.hContainer = NULL;
}
cap.ConType = TWON_ONEVALUE;
TW_UINT16 rc = entry(DG_CONTROL, DAT_CAPABILITY, MSG_GETCURRENT, (TW_MEMREF) &cap, pSource);
std::cout << "CAP_ORDER:" << convertCapToString(cap.Cap) << std::endl;
std::cout << "CAP_TYPE :" << convertConTypeToString(cap.ConType) << std::endl;
return rc;
}
TW_UINT16 TwainSession::setCap(TW_UINT16 Cap, const int value, TW_UINT16 type) {
TW_UINT16 rc = TWRC_FAILURE;
TW_CAPABILITY cap;
cap.Cap = Cap;
cap.ConType = TWON_ONEVALUE;
cap.hContainer = allocMemory(sizeof(TW_ONEVALUE));
if (cap.hContainer == NULL) {
return rc;
}
pTW_ONEVALUE pVal = (pTW_ONEVALUE) lockMemory(cap.hContainer);
pVal->ItemType = type;
switch (type) {
case TWTY_INT8:
*(TW_INT8 *) &pVal->Item = (TW_INT8) value;
break;
case TWTY_INT16:
*(TW_INT16 *) &pVal->Item = (TW_INT16) value;
break;
case TWTY_INT32:
*(TW_INT32 *) &pVal->Item = (TW_INT32) value;
break;
case TWTY_UINT8:
*(TW_UINT8 *) &pVal->Item = (TW_UINT8) value;
break;
case TWTY_UINT16:
*(TW_UINT16 *) &pVal->Item = (TW_UINT16) value;
break;
case TWTY_UINT32:
memcpy(&pVal->Item, &value, sizeof(TW_UINT32));
break;
case TWTY_BOOL:
memcpy(&pVal->Item, &value, sizeof(TW_BOOL));
break;
}
rc = entry(DG_CONTROL, DAT_CAPABILITY, MSG_SET, (TW_MEMREF) &cap);
return rc;
}
TW_UINT16 TwainSession::setCallback() {
if (state != 4) {
return TWRC_FAILURE;
}
TW_CALLBACK callback = {0};
callback.RefCon = 0;
callback.CallBackProc = (TW_MEMREF) dsmCallback;
TW_UINT16 rc = entry(DG_CONTROL, DAT_CALLBACK, MSG_REGISTER_CALLBACK, (TW_MEMREF) &callback, &source);
return rc;
}
TW_UINT16 TwainSession::enableDS() {
if (state < 3) {
status.ConditionCode = TWCC_SEQERROR;
std::cout << "enableDS :: You need to open the DSM first." << state << std::endl;
return TWRC_FAILURE;
}
if (state < 4) {
status.ConditionCode = TWCC_SEQERROR;
std::cout << "enableDS :: You need to open the DS first." << state << std::endl;
return TWRC_FAILURE;
}
if (state == 5) {
status.ConditionCode = TWCC_SEQERROR;
std::cout << "enableDS :: You have enabled the DS." << state << std::endl;
return TWRC_FAILURE;
}
std::cout << "Before message:" << message << std::endl;
ui.ShowUI = false;
ui.ModalUI = false;
ui.hParent = NULL;
TW_UINT16 rc = entry(DG_CONTROL, DAT_USERINTERFACE, MSG_ENABLEDS, (TW_MEMREF) &ui, pSource);
if (rc == TWRC_SUCCESS) {
state = 5;
}
std::cout << "After message:" << message << std::endl;
// todo 轮训/callback取到MSG
if (message == MSG_XFERREADY) {
state = 6;
}
return rc;
};
TW_UINT16 TwainSession::disableDS() {
if (state < 5) {
std::cout << "You need to enable the data source first" << std::endl;
return TWRC_FAILURE;
}
TW_UINT16 rc = entry(DG_CONTROL, DAT_USERINTERFACE, MSG_DISABLEDS, (TW_MEMREF) &ui,pSource);
if (rc == TWRC_SUCCESS) {
state = 4;
}
return rc;
}
TW_UINT16 TwainSession::getImageInfo() {
memset(&imageInfo, 0, sizeof(imageInfo));
std::cout << "Getting the image info..." << std::endl;
TW_UINT16 rc = entry(DG_IMAGE, DAT_IMAGEINFO, MSG_GET, (TW_MEMREF) &imageInfo, pSource);
if (rc == TWRC_SUCCESS) {
std::cout << "Image information - ImageWidth:" << imageInfo.ImageWidth << std::endl;
std::cout << "Image information - ImageLength:" << imageInfo.ImageLength << std::endl;
std::cout << "Image information - BitsPerSample:" << imageInfo.BitsPerSample << std::endl;
std::cout << "Image information - BitsPerPixel:" << imageInfo.BitsPerPixel << std::endl;
std::cout << "Image information - PixelType:" << imageInfo.PixelType << std::endl;
std::cout << "Image information - Planar:" << imageInfo.Planar << std::endl;
std::cout << "Image information - Compression:" << imageInfo.Compression << std::endl;
}
return rc;
}
TW_UINT16 TwainSession::scan(TW_UINT32 mech, std::string fileName) {
if(state != 6) {
std::cout << "A scan cannot be initiated unless we are in state 6" << std::endl;
return TWRC_FAILURE;
}
TW_UINT16 rc = getImageInfo();
if (TWRC_SUCCESS != rc) {
return rc;
};
switch (mech) {
case TWSX_NATIVE: {
transferNative();
break;
}
case TWSX_FILE: {
TW_CAPABILITY cap;
cap.Cap = ICAP_IMAGEFILEFORMAT;
cap.hContainer = 0;
TW_UINT16 rc = getCurrentCap(cap);
if( rc == TWRC_SUCCESS){
pTW_ONEVALUE pEnum = (pTW_ONEVALUE)lockMemory(cap.hContainer);
std::cout << pEnum->Item << std::endl;
transferFile(pEnum->Item, fileName);
}
break;
}
case TWSX_MEMORY: {
transferMemory();
break;
}
case TWSX_MEMFILE: {
transferMemory();
break;
}
}
return TWRC_SUCCESS;
}
void TwainSession::transferNative() {
std::cout << "starting a TWSX_NATIVE transfer..." << std::endl;
bool bPendingXfers = true;
TW_UINT16 rc = TWRC_FAILURE;
while (bPendingXfers) {
TW_MEMREF hImg = NULL;
rc = entry(DG_IMAGE, DAT_IMAGENATIVEXFER, MSG_GET, (TW_MEMREF) &hImg, pSource);
switch (rc) {
case TWRC_XFERDONE:
// save hImg
// checking to see if there are more images to transfer
TW_PENDINGXFERS pendXfers;
memset(&pendXfers, 0, sizeof(pendXfers));
rc = entry(DG_CONTROL, DAT_PENDINGXFERS, MSG_ENDXFER, (TW_MEMREF) &pendXfers, pSource);
if (rc == TWRC_SUCCESS) {
std::cout << "Remaining images to transfer" << pendXfers.Count << std::endl;
if (pendXfers.Count == 0) {
bPendingXfers = false;
}
} else {
bPendingXfers = false;
std::cerr << "Failed to properly end the transfer" << std::endl;
}
break;
case TWRC_CANCEL:
bPendingXfers = false;
std::cout << "Cancel transfer image" << std::endl;
break;
case TWRC_FAILURE:
bPendingXfers = false;
std::cout << "Failed transfer image" << std::endl;
break;
}
}
state = 5;
return;
}
void TwainSession::transferFile(TW_UINT16 fileFormat, std::string fileName) {
std::cout << "starting a TWSX_FILE transfer..." << std::endl;
std::string ext = convertImageFileFormatToExt(fileFormat);
std::cout << ext << std::endl;
bool bPendingXfers = true;
TW_UINT16 rc = TWRC_SUCCESS;
TW_SETUPFILEXFER fileXfer;
memset(&fileXfer, 0, sizeof(fileXfer));
std::cout << "Test::" << fileXfer.Format << std::endl;
fileXfer.Format = fileFormat;
strcpy(fileXfer.FileName, (fileName + ext).c_str());
// TW_STR255 str;
// snprintf((char *)fileXfer.FileName, str);
// fileXfer.FileName[0] = 'i';
// fileXfer.FileName[1] = 'm';
// fileXfer.FileName[2] = '.';
// fileXfer.FileName[3] = 't';
// fileXfer.FileName[4] = 'i';
// fileXfer.FileName[5] = 'f';
// fileXfer.FileName[6] = 'f';
while (bPendingXfers) {
rc = entry(DG_CONTROL, DAT_SETUPFILEXFER, MSG_SET, (TW_MEMREF) &fileXfer, pSource);
if (rc != TWRC_SUCCESS) {
std::cerr << "Error while trying to setup the file transfer" << std::endl;
break;
}
rc = entry(DG_IMAGE, DAT_IMAGEFILEXFER, MSG_GET, NULL, pSource);
if (rc == TWRC_XFERDONE) {
// rc = entry(DG_CONTROL, DAT_SETUPFILEXFER, MSG_GETDEFAULT, (TW_MEMREF) &fileXfer, pSource);
std::cout << "file saved..." << fileXfer.FileName << std::endl;
std::cout << "Checking to see if there are more images to transfer..." << std::endl;
TW_PENDINGXFERS pendXfers;
memset(&pendXfers, 0, sizeof(pendXfers));
rc = entry(DG_CONTROL, DAT_PENDINGXFERS, MSG_ENDXFER, (TW_MEMREF) &pendXfers, pSource);
if (rc == TWRC_SUCCESS) {
if (pendXfers.Count == 0) {
bPendingXfers = false;
}
} else {
std::cerr << "Failed to properly end the transfer" << std::endl;
bPendingXfers = false;
}
} else if (rc == TWRC_CANCEL) {
std::cerr << "Cancel to transfer image" << std::endl;
break;
} else if (rc == TWRC_FAILURE) {
std::cerr << "Failed to transfer image" << std::endl;
break;
}
}
state = 5;
return;
}
void TwainSession::transferMemory() {
std::cout << "starting a TWSX_MEMORY transfer..." << std::endl;
bool bPendingXfers = true;
TW_SETUPMEMXFER sourceBufferSizes;
TW_UINT16 rc = TWRC_FAILURE;
while (bPendingXfers) {
rc = entry(DG_CONTROL, DAT_SETUPMEMXFER, MSG_GET, (TW_MEMREF) &sourceBufferSizes);
if (rc != TWRC_SUCCESS) {
std::cerr << "Error while trying to get the buffer sizes from the source" << std::endl;
break;
}
TW_IMAGEMEMXFER memXferBufferTemplate;
memXferBufferTemplate.Compression = TWON_DONTCARE16;
memXferBufferTemplate.BytesPerRow = TWON_DONTCARE32;
memXferBufferTemplate.Columns = TWON_DONTCARE32;
memXferBufferTemplate.Rows = TWON_DONTCARE32;
memXferBufferTemplate.XOffset = TWON_DONTCARE32;
memXferBufferTemplate.YOffset = TWON_DONTCARE32;
memXferBufferTemplate.BytesWritten = TWON_DONTCARE32;
memXferBufferTemplate.Memory.Flags = TWMF_APPOWNS | TWMF_POINTER;
memXferBufferTemplate.Memory.Length = sourceBufferSizes.Preferred;
TW_HANDLE hMem = (TW_HANDLE) allocMemory(sourceBufferSizes.Preferred);
if (hMem == NULL) {
std::cerr << "Error allocating memory" << std::endl;
break;
}
memXferBufferTemplate.Memory.TheMem = (TW_MEMREF) lockMemory(hMem);
TW_IMAGEMEMXFER memXferBuf;
bool bScanStarted = false;
int nBytePerRow = (((imageInfo.ImageWidth * imageInfo.BitsPerPixel) + 7) / 8);
std::cout << "starting the memory transfer..." << std::endl;
while (true) {
memcpy(&memXferBuf, &memXferBufferTemplate, sizeof(memXferBufferTemplate));
memset(memXferBuf.Memory.TheMem, 0, memXferBuf.Memory.Length);
rc = entry(DG_IMAGE, DAT_IMAGEFILEXFER, MSG_GET, (TW_MEMREF) &(memXferBuf));
if (rc == TWRC_SUCCESS || rc == TWRC_XFERDONE) {
} else if (rc == TWRC_CANCEL) {
std::cerr << "Canceled transfer while trying to get strip of data from the source" << std::endl;
break;
} else if (rc == TWRC_FAILURE) {
std::cerr << "Error while trying to get strip of data from the source" << std::endl;
break;
}
}
}
state = 5;
return;
}
bool TwainSession::parseCapability(TW_CAPABILITY *pCap, TW_UINT32 &val) {
bool result = false;
if (pCap->hContainer == NULL) {
return result;
} else if (pCap->ConType == TWON_ENUMERATION) {
pTW_ENUMERATION pCapPT = (pTW_ENUMERATION) lockMemory(pCap->hContainer);
switch (pCapPT->ItemType) {
case TWTY_INT32:
val = (TW_INT32) ((pTW_INT32) (&pCapPT->ItemList))[pCapPT->CurrentIndex];
result = true;
break;
case TWTY_UINT32:
val = (TW_INT32) ((pTW_UINT32) (&pCapPT->ItemList))[pCapPT->CurrentIndex];
result = true;
break;
case TWTY_INT16:
val = (TW_INT32) ((pTW_INT16) (&pCapPT->ItemList))[pCapPT->CurrentIndex];
result = true;
break;
case TWTY_UINT16:
val = (TW_INT32) ((pTW_UINT16) (&pCapPT->ItemList))[pCapPT->CurrentIndex];
result = true;
break;
case TWTY_INT8:
val = (TW_INT32) ((pTW_INT8) (&pCapPT->ItemList))[pCapPT->CurrentIndex];
result = true;
break;
case TWTY_UINT8:
val = (TW_INT32) ((pTW_UINT8) (&pCapPT->ItemList))[pCapPT->CurrentIndex];
result = true;
break;
case TWTY_BOOL:
val = (TW_INT32) ((pTW_BOOL) (&pCapPT->ItemList))[pCapPT->CurrentIndex];
result = true;
break;
}
unlockMemory(pCap->hContainer);
} else if (pCap->ConType == TWON_ONEVALUE) {
pTW_ONEVALUE pCapPT = (pTW_ONEVALUE) lockMemory(pCap->hContainer);
if (pCapPT->ItemType < TWTY_FIX32) {
val = pCapPT->Item;
result = true;
}
unlockMemory(pCap->hContainer);
} else if (pCap->ConType == TWON_RANGE) {
pTW_RANGE pCapPT = (pTW_RANGE) lockMemory(pCap->hContainer);
if (pCapPT->ItemType < TWTY_FIX32) {
val = pCapPT->CurrentValue;
result = true;
}
unlockMemory(pCap->hContainer);
}
return result;
}
TW_HANDLE TwainSession::allocMemory(TW_UINT32 _size) {
if (gDSMEntry.DSM_MemAllocate) {
return gDSMEntry.DSM_MemAllocate(_size);
}
#ifdef TWH_CMP_MSC
return ::GlobalAlloc(GPTR, _size);
#else
return 0;
#endif
}
void TwainSession::freeMemory(TW_HANDLE _hMemory) {
if (gDSMEntry.DSM_MemFree) {
return gDSMEntry.DSM_MemFree(_hMemory);
}
#ifdef TWH_CMP_MSC
::GlobalFree(_hMemory);
#endif
return;
}
TW_MEMREF TwainSession::lockMemory(TW_HANDLE _hMemory) {
if (gDSMEntry.DSM_MemLock) {
return gDSMEntry.DSM_MemLock(_hMemory);
}
#ifdef TWH_CMP_MSC
return (TW_MEMREF)::GlobalLock(_hMemory);
#else
return 0;
#endif
}
void TwainSession::unlockMemory(TW_HANDLE _hMemory) {
if (gDSMEntry.DSM_MemUnlock) {
return gDSMEntry.DSM_MemUnlock(_hMemory);
}
#ifdef TWH_CMP_MSC
::GlobalUnlock(_hMemory);
#endif
return;
}
const std::string TwainSession::convertReturnCodeToString(const TW_UINT16 value) {
const char *text = NULL;
switch (value) {
case TWRC_SUCCESS:
text = "TWRC_SUCCESS";
break;
case TWRC_FAILURE:
text = "TWRC_FAILURE";
break;
case TWRC_CHECKSTATUS:
text = "TWRC_CHECKSTATUS";
break;
case TWRC_CANCEL:
text = "TWRC_CANCEL";
break;
case TWRC_DSEVENT:
text = "TWRC_DSEVENT";
break;
case TWRC_NOTDSEVENT:
text = "TWRC_NOTDSEVENT";
break;
case TWRC_XFERDONE:
text = "TWRC_XFERDONE";
break;
case TWRC_ENDOFLIST:
text = "TWRC_ENDOFLIST";
break;
case TWRC_INFONOTSUPPORTED:
text = "TWRC_INFONOTSUPPORTED";
break;
case TWRC_DATANOTAVAILABLE:
text = "TWRC_DATANOTAVAILABLE";
break;
case TWRC_BUSY:
text = "TWRC_BUSY";
break;
case TWRC_SCANNERLOCKED:
text = "TWRC_SCANNERLOCKED";
break;
default:
text = "Unknown";
}
return text;
};
const std::string TwainSession::convertConditionCodeToString(TW_UINT16 value) {
const char *text = NULL;
switch (value) {
case TWCC_SUCCESS:
text = "TWCC_SUCCESS";
break;
case TWCC_BUMMER:
text = "TWCC_BUMMER";
break;
case TWCC_LOWMEMORY:
text = "TWCC_LOWMEMORY";
break;
case TWCC_NODS:
text = "TWCC_NODS";
break;
case TWCC_MAXCONNECTIONS:
text = "TWCC_MAXCONNECTIONS";
break;
case TWCC_OPERATIONERROR:
text = "TWCC_OPERATIONERROR";
break;
case TWCC_BADCAP:
text = "TWCC_BADCAP";
break;
case TWCC_BADPROTOCOL:
text = "TWCC_BADPROTOCOL";
break;
case TWCC_BADVALUE:
text = "TWCC_BADVALUE";
break;
case TWCC_SEQERROR:
text = "TWCC_SEQERROR";
break;
case TWCC_BADDEST:
text = "TWCC_BADDEST";
break;
case TWCC_CAPUNSUPPORTED:
text = "TWCC_CAPUNSUPPORTED";
break;
case TWCC_CAPBADOPERATION:
text = "TWCC_CAPBADOPERATION";
break;
case TWCC_CAPSEQERROR:
text = "TWCC_CAPSEQERROR";
break;
case TWCC_DENIED:
text = "TWCC_DENIED";
break;
case TWCC_FILEEXISTS:
text = "TWCC_FILEEXISTS";
break;
case TWCC_FILENOTFOUND:
text = "TWCC_FILENOTFOUND";
break;
case TWCC_NOTEMPTY:
text = "TWCC_NOTEMPTY";
break;
case TWCC_PAPERJAM:
text = "TWCC_PAPERJAM";
break;
case TWCC_PAPERDOUBLEFEED:
text = "TWCC_PAPERDOUBLEFEED";
break;
case TWCC_FILEWRITEERROR:
text = "TWCC_FILEWRITEERROR";
break;
case TWCC_CHECKDEVICEONLINE:
text = "TWCC_CHECKDEVICEONLINE";
break;
case TWCC_INTERLOCK:
text = "TWCC_INTERLOCK";
break;
case TWCC_DAMAGEDCORNER:
text = "TWCC_DAMAGEDCORNER";
break;
case TWCC_FOCUSERROR:
text = "TWCC_FOCUSERROR";
break;
case TWCC_DOCTOOLIGHT:
text = "TWCC_DOCTOOLIGHT";
break;
case TWCC_DOCTOODARK:
text = "TWCC_DOCTOODARK";
break;
case TWCC_NOMEDIA:
text = "TWCC_NOMEDIA";
break;
default:
text = "Unknown";
}
return text;
}
const std::string TwainSession::convertDataGroupToString(const TW_UINT16 value) {
const char *text = NULL;
switch (value) {
case DG_CONTROL:
text = "DG_CONTROL";
break;
case DG_IMAGE:
text = "DG_IMAGE";
break;
case DG_AUDIO:
text = "DG_AUDIO";
break;
default:
text = "Unknown";
}
return text;
}
const std::string TwainSession::convertDataArgTypeToString(const TW_UINT16 value) {
const char *text = NULL;
switch (value) {
case DAT_NULL:
text = "DAT_NULL";
break;
case DAT_CUSTOMBASE:
text = "DAT_CUSTOMBASE";
break;
case DAT_CAPABILITY:
text = "DAT_CAPABILITY";
break;
case DAT_EVENT:
text = "DAT_EVENT";
break;
case DAT_IDENTITY:
text = "DAT_IDENTITY";
break;
case DAT_PARENT:
text = "DAT_PARENT";
break;
case DAT_PENDINGXFERS:
text = "DAT_PENDINGXFERS";
break;
case DAT_SETUPMEMXFER:
text = "DAT_SETUPMEMXFER";
break;
case DAT_SETUPFILEXFER:
text = "DAT_SETUPFILEXFER";
break;
case DAT_STATUS:
text = "DAT_STATUS";
break;
case DAT_USERINTERFACE:
text = "DAT_USERINTERFACE";
break;
case DAT_XFERGROUP:
text = "DAT_XFERGROUP";
break;
case DAT_CUSTOMDSDATA:
text = "DAT_CUSTOMDSDATA";
break;
case DAT_DEVICEEVENT:
text = "DAT_DEVICEEVENT";
break;
case DAT_FILESYSTEM:
text = "DAT_FILESYSTEM";
break;
case DAT_PASSTHRU:
text = "DAT_PASSTHRU";
break;
case DAT_CALLBACK:
text = "DAT_CALLBACK";
break;
case DAT_STATUSUTF8:
text = "DAT_STATUSUTF8";
break;
case DAT_CALLBACK2:
text = "DAT_CALLBACK2";
break;
case DAT_METRICS:
text = "DAT_METRICS";
break;
case DAT_TWAINDIRECT:
text = "DAT_TWAINDIRECT";
break;
case DAT_IMAGEINFO:
text = "DAT_IMAGEINFO";
break;
case DAT_IMAGELAYOUT:
text = "DAT_IMAGELAYOUT";
break;
case DAT_IMAGEMEMXFER:
text = "DAT_IMAGEMEMXFER";
break;
case DAT_IMAGENATIVEXFER:
text = "DAT_IMAGENATIVEXFER";
break;
case DAT_IMAGEFILEXFER:
text = "DAT_IMAGEFILEXFER";
break;
case DAT_CIECOLOR:
text = "DAT_CIECOLOR";
break;
case DAT_GRAYRESPONSE:
text = "DAT_GRAYRESPONSE";
break;
case DAT_RGBRESPONSE:
text = "DAT_RGBRESPONSE";
break;
case DAT_JPEGCOMPRESSION:
text = "DAT_JPEGCOMPRESSION";
break;
case DAT_PALETTE8:
text = "DAT_PALETTE8";
break;
case DAT_EXTIMAGEINFO:
text = "DAT_EXTIMAGEINFO";
break;
case DAT_FILTER:
text = "DAT_FILTER";
break;
case DAT_AUDIOFILEXFER:
text = "DAT_AUDIOFILEXFER";
break;
case DAT_AUDIOINFO:
text = "DAT_AUDIOINFO";
break;
case DAT_AUDIONATIVEXFER:
text = "DAT_AUDIONATIVEXFER";
break;
case DAT_ICCPROFILE:
text = "DAT_ICCPROFILE";
break;
case DAT_IMAGEMEMFILEXFER:
text = "DAT_IMAGEMEMFILEXFER";
break;
case DAT_ENTRYPOINT:
text = "DAT_ENTRYPOINT";
break;