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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
|
/* sane - Scanner Access Now Easy.
Copyright (C) 1998 David F. Skoll
Heavily based on "hp.c" driver for HP Scanners, by
David Mosberger-Tang.
This file is part of the SANE package.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
As a special exception, the authors of SANE give permission for
additional uses of the libraries contained in this release of SANE.
The exception is that, if you link a SANE library with other files
to produce an executable, this does not by itself cause the
resulting executable to be covered by the GNU General Public
License. Your use of that executable is in no way restricted on
account of linking the SANE library code into it.
This exception does not, however, invalidate any other reasons why
the executable file might be covered by the GNU General Public
License.
If you submit changes to SANE to the maintainers to be included in
a subsequent release, you agree by submitting the changes that
those changes may be distributed with this exception intact.
If you write modifications of your own for SANE, it is your choice
whether to permit this exception to apply to your modifications.
If you do not wish that, delete this exception notice.
This file implements a SANE backend for the Polaroid Digital
Microscope Camera. */
/* $Id$ */
#include "../include/sane/config.h"
#include <limits.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "../include/_stdint.h"
#include "../include/sane/sane.h"
#include "../include/sane/saneopts.h"
#include "../include/sane/sanei_scsi.h"
#define BACKEND_NAME dmc
#include "../include/sane/sanei_backend.h"
#ifndef PATH_MAX
# define PATH_MAX 1024
#endif
#include "../include/sane/sanei_config.h"
#define DMC_CONFIG_FILE "dmc.conf"
#include "dmc.h"
/* A linked-list of attached devices and handles */
static DMC_Device *FirstDevice = NULL;
static DMC_Camera *FirstHandle = NULL;
static int NumDevices = 0;
static SANE_String_Const ValidModes[] = { "Full frame", "Viewfinder",
"Raw", "Thumbnail",
"Super-Resolution",
NULL };
static SANE_String_Const ValidBalances[] = { "Daylight", "Incandescent",
"Fluorescent", NULL };
static SANE_Word ValidASAs[] = { 3, 25, 50, 100 };
/* Convert between 32-us ticks and milliseconds */
#define MS_TO_TICKS(x) (((x) * 1000 + 16) / 32)
#define TICKS_TO_MS(x) (((x) * 32) / 1000)
/* Macros for stepping along the raw lines for super-resolution mode
They are very ugly because they handle boundary conditions at
the edges of the image. Yuck... */
#define PREV_RED(i) (((i)/3)*3)
#define NEXT_RED(i) (((i) >= BYTES_PER_RAW_LINE-3) ? BYTES_PER_RAW_LINE-3 : \
PREV_RED(i)+3)
#define PREV_GREEN(i) ((i)<1 ? 1 : PREV_RED((i)-1)+1)
#define NEXT_GREEN(i) ((i)<1 ? 1 : ((i) >= BYTES_PER_RAW_LINE-2) ? \
BYTES_PER_RAW_LINE-2 : PREV_GREEN(i)+3)
#define PREV_BLUE(i) ((i)<2 ? 2 : PREV_RED((i)-2)+2)
#define NEXT_BLUE(i) ((i)<2 ? 2 : ((i) >= BYTES_PER_RAW_LINE-1) ? \
BYTES_PER_RAW_LINE-1 : PREV_BLUE(i)+3)
#define ADVANCE_COEFF(i) (((i)==1) ? 3 : (i)-1);
/**********************************************************************
//%FUNCTION: DMCRead
//%ARGUMENTS:
// fd -- file descriptor
// typecode -- data type code
// qualifier -- data type qualifier
// maxlen -- tranfer length
// buf -- buffer to store data in
// len -- set to actual length of data
//%RETURNS:
// A SANE status code
//%DESCRIPTION:
// Reads the particular data selected by typecode and qualifier
// *********************************************************************/
static SANE_Status
DMCRead(int fd, unsigned int typecode, unsigned int qualifier,
SANE_Byte *buf, size_t maxlen, size_t *len)
{
uint8_t readCmd[10];
SANE_Status status;
readCmd[0] = 0x28;
readCmd[1] = 0;
readCmd[2] = typecode;
readCmd[3] = 0;
readCmd[4] = (qualifier >> 8) & 0xFF;
readCmd[5] = qualifier & 0xFF;
readCmd[6] = (maxlen >> 16) & 0xFF;
readCmd[7] = (maxlen >> 8) & 0xFF;
readCmd[8] = maxlen & 0xFF;
readCmd[9] = 0;
DBG(3, "DMCRead: typecode=%x, qualifier=%x, maxlen=%lu\n",
typecode, qualifier, (u_long) maxlen);
*len = maxlen;
status = sanei_scsi_cmd(fd, readCmd, sizeof(readCmd), buf, len);
DBG(3, "DMCRead: Read %lu bytes\n", (u_long) *len);
return status;
}
/**********************************************************************
//%FUNCTION: DMCWrite
//%ARGUMENTS:
// fd -- file descriptor
// typecode -- data type code
// qualifier -- data type qualifier
// maxlen -- tranfer length
// buf -- buffer to store data in
//%RETURNS:
// A SANE status code
//%DESCRIPTION:
// Writes the particular data selected by typecode and qualifier
// *********************************************************************/
static SANE_Status
DMCWrite(int fd, unsigned int typecode, unsigned int qualifier,
SANE_Byte *buf, size_t maxlen)
{
uint8_t *writeCmd;
SANE_Status status;
writeCmd = malloc(maxlen + 10);
if (!writeCmd) return SANE_STATUS_NO_MEM;
writeCmd[0] = 0x2A;
writeCmd[1] = 0;
writeCmd[2] = typecode;
writeCmd[3] = 0;
writeCmd[4] = (qualifier >> 8) & 0xFF;
writeCmd[5] = qualifier & 0xFF;
writeCmd[6] = (maxlen >> 16) & 0xFF;
writeCmd[7] = (maxlen >> 8) & 0xFF;
writeCmd[8] = maxlen & 0xFF;
writeCmd[9] = 0;
memcpy(writeCmd+10, buf, maxlen);
DBG(3, "DMCWrite: typecode=%x, qualifier=%x, maxlen=%lu\n",
typecode, qualifier, (u_long) maxlen);
status = sanei_scsi_cmd(fd, writeCmd, 10+maxlen, NULL, NULL);
free(writeCmd);
return status;
}
/**********************************************************************
//%FUNCTION: DMCAttach
//%ARGUMENTS:
// devname -- name of device file to open
// devp -- a DMC_Device structure which we fill in if it's not NULL.
//%RETURNS:
// SANE_STATUS_GOOD -- We have a Polaroid DMC attached and all looks good.
// SANE_STATUS_INVAL -- There's a problem.
//%DESCRIPTION:
// Verifies that a Polaroid DMC is attached. Sets up device options in
// DMC_Device structure.
// *********************************************************************/
#define INQ_LEN 255
static SANE_Status
DMCAttach(char const *devname, DMC_Device **devp)
{
DMC_Device *dev;
SANE_Status status;
int fd;
size_t size;
char result[INQ_LEN];
uint8_t exposureCalculationResults[16];
uint8_t userInterfaceSettings[16];
static uint8_t const inquiry[] =
{ 0x12, 0x00, 0x00, 0x00, INQ_LEN, 0x00 };
static uint8_t const test_unit_ready[] =
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static uint8_t const no_viewfinder[] =
{ 0xC6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
/* If we're already attached, do nothing */
for (dev = FirstDevice; dev; dev = dev->next) {
if (!strcmp(dev->sane.name, devname)) {
if (devp) *devp = dev;
return SANE_STATUS_GOOD;
}
}
DBG(3, "DMCAttach: opening `%s'\n", devname);
status = sanei_scsi_open(devname, &fd, 0, 0);
if (status != SANE_STATUS_GOOD) {
DBG(1, "DMCAttach: open failed (%s)\n", sane_strstatus(status));
return status;
}
DBG(3, "DMCAttach: sending INQUIRY\n");
size = sizeof(result);
status = sanei_scsi_cmd(fd, inquiry, sizeof(inquiry), result, &size);
if (status != SANE_STATUS_GOOD || size < 32) {
if (status == SANE_STATUS_GOOD) status = SANE_STATUS_INVAL;
DBG(1, "DMCAttach: inquiry failed (%s)\n", sane_strstatus(status));
sanei_scsi_close(fd);
return status;
}
/* Verify that we have a Polaroid DMC */
if (result[0] != 6 ||
strncmp(result+8, "POLAROID", 8) ||
strncmp(result+16, "DMC ", 8)) {
sanei_scsi_close(fd);
DBG(1, "DMCAttach: Device does not look like a Polaroid DMC\n");
return SANE_STATUS_INVAL;
}
DBG(3, "DMCAttach: sending TEST_UNIT_READY\n");
status = sanei_scsi_cmd(fd, test_unit_ready, sizeof(test_unit_ready),
NULL, NULL);
if (status != SANE_STATUS_GOOD) {
DBG(1, "DMCAttach: test unit ready failed (%s)\n",
sane_strstatus(status));
sanei_scsi_close(fd);
return status;
}
/* Read current ASA and shutter speed settings */
status = DMCRead(fd, 0x87, 0x4, exposureCalculationResults,
sizeof(exposureCalculationResults), &size);
if (status != SANE_STATUS_GOOD ||
size < sizeof(exposureCalculationResults)) {
DBG(1, "DMCAttach: Couldn't read exposure calculation results (%s)\n",
sane_strstatus(status));
sanei_scsi_close(fd);
if (status == SANE_STATUS_GOOD) status = SANE_STATUS_IO_ERROR;
return status;
}
/* Read current white balance settings */
status = DMCRead(fd, 0x82, 0x0, userInterfaceSettings,
sizeof(userInterfaceSettings), &size);
if (status != SANE_STATUS_GOOD ||
size < sizeof(userInterfaceSettings)) {
DBG(1, "DMCAttach: Couldn't read user interface settings (%s)\n",
sane_strstatus(status));
sanei_scsi_close(fd);
if (status == SANE_STATUS_GOOD) status = SANE_STATUS_IO_ERROR;
return status;
}
/* Shut off viewfinder mode */
status = sanei_scsi_cmd(fd, no_viewfinder, sizeof(no_viewfinder),
NULL, NULL);
if (status != SANE_STATUS_GOOD) {
sanei_scsi_close(fd);
return status;
}
sanei_scsi_close(fd);
DBG(3, "DMCAttach: Looks like we have a Polaroid DMC\n");
dev = malloc(sizeof(*dev));
if (!dev) return SANE_STATUS_NO_MEM;
memset(dev, 0, sizeof(*dev));
dev->sane.name = strdup(devname);
dev->sane.vendor = "Polaroid";
dev->sane.model = "DMC";
dev->sane.type = "still camera";
dev->next = FirstDevice;
dev->whiteBalance = userInterfaceSettings[5];
if (dev->whiteBalance > WHITE_BALANCE_FLUORESCENT) {
dev->whiteBalance = WHITE_BALANCE_FLUORESCENT;
}
/* Bright Eyes documentation gives these as shutter speed ranges (ms) */
/* dev->shutterSpeedRange.min = 8; */
/* dev->shutterSpeedRange.max = 320; */
/* User's manual says these are shutter speed ranges (ms) */
dev->shutterSpeedRange.min = 8;
dev->shutterSpeedRange.max = 1000;
dev->shutterSpeedRange.quant = 2;
dev->shutterSpeed =
(exposureCalculationResults[10] << 8) +
exposureCalculationResults[11];
/* Convert from ticks to ms */
dev->shutterSpeed = TICKS_TO_MS(dev->shutterSpeed);
dev->asa = exposureCalculationResults[13];
if (dev->asa > ASA_100) dev->asa = ASA_100;
dev->asa = ValidASAs[dev->asa + 1];
FirstDevice = dev;
NumDevices++;
if (devp) *devp = dev;
return SANE_STATUS_GOOD;
}
/**********************************************************************
//%FUNCTION: ValidateHandle
//%ARGUMENTS:
// handle -- a handle for an opened camera
//%RETURNS:
// A validated pointer to the camera or NULL if handle is not valid.
// *********************************************************************/
static DMC_Camera *
ValidateHandle(SANE_Handle handle)
{
DMC_Camera *c;
for (c = FirstHandle; c; c = c->next) {
if (c == handle) return c;
}
DBG(1, "ValidateHandle: invalid handle %p\n", handle);
return NULL;
}
/**********************************************************************
//%FUNCTION: DMCInitOptions
//%ARGUMENTS:
// c -- a DMC camera device
//%RETURNS:
// SANE_STATUS_GOOD -- OK
// SANE_STATUS_INVAL -- There's a problem.
//%DESCRIPTION:
// Initializes the options in the DMC_Camera structure
// *********************************************************************/
static SANE_Status
DMCInitOptions(DMC_Camera *c)
{
int i;
/* Image is initially 801x600 */
c->tl_x_range.min = 0;
c->tl_x_range.max = c->tl_x_range.min;
c->tl_x_range.quant = 1;
c->tl_y_range.min = 0;
c->tl_y_range.max = c->tl_y_range.min;
c->tl_y_range.quant = 1;
c->br_x_range.min = 800;
c->br_x_range.max = c->br_x_range.min;
c->br_x_range.quant = 1;
c->br_y_range.min = 599;
c->br_y_range.max = c->br_y_range.min;
c->br_y_range.quant = 1;
memset(c->opt, 0, sizeof(c->opt));
memset(c->val, 0, sizeof(c->val));
for (i=0; i<NUM_OPTIONS; i++) {
c->opt[i].type = SANE_TYPE_INT;
c->opt[i].size = sizeof(SANE_Word);
c->opt[i].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
c->opt[i].unit = SANE_UNIT_NONE;
}
c->opt[OPT_NUM_OPTS].name = SANE_NAME_NUM_OPTIONS;
c->opt[OPT_NUM_OPTS].title = SANE_TITLE_NUM_OPTIONS;
c->opt[OPT_NUM_OPTS].desc = SANE_DESC_NUM_OPTIONS;
c->opt[OPT_NUM_OPTS].cap = SANE_CAP_SOFT_DETECT;
c->opt[OPT_NUM_OPTS].type = SANE_TYPE_INT;
c->opt[OPT_NUM_OPTS].constraint_type = SANE_CONSTRAINT_NONE;
c->val[OPT_NUM_OPTS].w = NUM_OPTIONS;
c->opt[OPT_GEOMETRY_GROUP].type = SANE_TYPE_GROUP;
c->opt[OPT_GEOMETRY_GROUP].name = "";
c->opt[OPT_GEOMETRY_GROUP].title = "Geometry";
c->opt[OPT_GEOMETRY_GROUP].desc = "";
c->opt[OPT_GEOMETRY_GROUP].cap = SANE_CAP_ADVANCED;
c->opt[OPT_GEOMETRY_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
/* top-left x */
c->opt[OPT_TL_X].name = SANE_NAME_SCAN_TL_X;
c->opt[OPT_TL_X].title = SANE_TITLE_SCAN_TL_X;
c->opt[OPT_TL_X].desc = SANE_DESC_SCAN_TL_X;
c->opt[OPT_TL_X].type = SANE_TYPE_INT;
c->opt[OPT_TL_X].unit = SANE_UNIT_PIXEL;
c->opt[OPT_TL_X].constraint_type = SANE_CONSTRAINT_RANGE;
c->opt[OPT_TL_X].constraint.range = &c->tl_x_range;
c->val[OPT_TL_X].w = c->tl_x_range.min;
/* top-left y */
c->opt[OPT_TL_Y].name = SANE_NAME_SCAN_TL_Y;
c->opt[OPT_TL_Y].title = SANE_TITLE_SCAN_TL_Y;
c->opt[OPT_TL_Y].desc = SANE_DESC_SCAN_TL_Y;
c->opt[OPT_TL_Y].type = SANE_TYPE_INT;
c->opt[OPT_TL_Y].unit = SANE_UNIT_PIXEL;
c->opt[OPT_TL_Y].constraint_type = SANE_CONSTRAINT_RANGE;
c->opt[OPT_TL_Y].constraint.range = &c->tl_y_range;
c->val[OPT_TL_Y].w = c->tl_y_range.min;
/* bottom-right x */
c->opt[OPT_BR_X].name = SANE_NAME_SCAN_BR_X;
c->opt[OPT_BR_X].title = SANE_TITLE_SCAN_BR_X;
c->opt[OPT_BR_X].desc = SANE_DESC_SCAN_BR_X;
c->opt[OPT_BR_X].type = SANE_TYPE_INT;
c->opt[OPT_BR_X].unit = SANE_UNIT_PIXEL;
c->opt[OPT_BR_X].constraint_type = SANE_CONSTRAINT_RANGE;
c->opt[OPT_BR_X].constraint.range = &c->br_x_range;
c->val[OPT_BR_X].w = c->br_x_range.min;
/* bottom-right y */
c->opt[OPT_BR_Y].name = SANE_NAME_SCAN_BR_Y;
c->opt[OPT_BR_Y].title = SANE_TITLE_SCAN_BR_Y;
c->opt[OPT_BR_Y].desc = SANE_DESC_SCAN_BR_Y;
c->opt[OPT_BR_Y].type = SANE_TYPE_INT;
c->opt[OPT_BR_Y].unit = SANE_UNIT_PIXEL;
c->opt[OPT_BR_Y].constraint_type = SANE_CONSTRAINT_RANGE;
c->opt[OPT_BR_Y].constraint.range = &c->br_y_range;
c->val[OPT_BR_Y].w = c->br_y_range.min;
c->opt[OPT_MODE_GROUP].type = SANE_TYPE_GROUP;
c->opt[OPT_MODE_GROUP].name = "";
c->opt[OPT_MODE_GROUP].title = "Imaging Mode";
c->opt[OPT_MODE_GROUP].desc = "";
c->opt[OPT_MODE_GROUP].cap = SANE_CAP_ADVANCED;
c->opt[OPT_MODE_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
c->opt[OPT_IMAGE_MODE].name = "imagemode";
c->opt[OPT_IMAGE_MODE].title = "Image Mode";
c->opt[OPT_IMAGE_MODE].desc = "Selects image mode: 800x600 full frame, 270x201 viewfinder mode, 1599x600 \"raw\" image, 80x60 thumbnail image or 1599x1200 \"super-resolution\" image";
c->opt[OPT_IMAGE_MODE].type = SANE_TYPE_STRING;
c->opt[OPT_IMAGE_MODE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
c->opt[OPT_IMAGE_MODE].constraint.string_list = ValidModes;
c->opt[OPT_IMAGE_MODE].size = 16;
c->val[OPT_IMAGE_MODE].s = "Full frame";
c->opt[OPT_ASA].name = "asa";
c->opt[OPT_ASA].title = "ASA Setting";
c->opt[OPT_ASA].desc = "Equivalent ASA setting";
c->opt[OPT_ASA].constraint_type = SANE_CONSTRAINT_WORD_LIST;
c->opt[OPT_ASA].constraint.word_list = ValidASAs;
c->val[OPT_ASA].w = c->hw->asa;
c->opt[OPT_SHUTTER_SPEED].name = "shutterspeed";
c->opt[OPT_SHUTTER_SPEED].title = "Shutter Speed (ms)";
c->opt[OPT_SHUTTER_SPEED].desc = "Shutter Speed in milliseconds";
c->opt[OPT_SHUTTER_SPEED].constraint_type = SANE_CONSTRAINT_RANGE;
c->opt[OPT_SHUTTER_SPEED].constraint.range = &c->hw->shutterSpeedRange;
c->val[OPT_SHUTTER_SPEED].w = c->hw->shutterSpeed;
c->opt[OPT_WHITE_BALANCE].name = "whitebalance";
c->opt[OPT_WHITE_BALANCE].title = "White Balance";
c->opt[OPT_WHITE_BALANCE].desc = "Selects white balance";
c->opt[OPT_WHITE_BALANCE].type = SANE_TYPE_STRING;
c->opt[OPT_WHITE_BALANCE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
c->opt[OPT_WHITE_BALANCE].constraint.string_list = ValidBalances;
c->opt[OPT_WHITE_BALANCE].size = 16;
c->val[OPT_WHITE_BALANCE].s = (SANE_String) ValidBalances[c->hw->whiteBalance];
return SANE_STATUS_GOOD;
}
/**********************************************************************
//%FUNCTION: DMCSetMode
//%ARGUMENTS:
// c -- a DMC camera device
// mode -- Imaging mode
//%RETURNS:
// SANE_STATUS_GOOD -- OK
// SANE_STATUS_INVAL -- There's a problem.
//%DESCRIPTION:
// Sets the camera's imaging mode.
// *********************************************************************/
static SANE_Status
DMCSetMode(DMC_Camera *c, int mode)
{
switch(mode) {
case IMAGE_MFI:
c->tl_x_range.min = 0;
c->tl_x_range.max = c->tl_x_range.max;
c->tl_y_range.min = 0;
c->tl_y_range.max = c->tl_y_range.max;
c->br_x_range.min = 800;
c->br_x_range.max = c->br_x_range.max;
c->br_y_range.min = 599;
c->br_y_range.max = c->br_y_range.max;
break;
case IMAGE_VIEWFINDER:
c->tl_x_range.min = 0;
c->tl_x_range.max = c->tl_x_range.max;
c->tl_y_range.min = 0;
c->tl_y_range.max = c->tl_y_range.max;
c->br_x_range.min = 269;
c->br_x_range.max = c->br_x_range.max;
c->br_y_range.min = 200;
c->br_y_range.max = c->br_y_range.max;
break;
case IMAGE_RAW:
c->tl_x_range.min = 0;
c->tl_x_range.max = c->tl_x_range.max;
c->tl_y_range.min = 0;
c->tl_y_range.max = c->tl_y_range.max;
c->br_x_range.min = 1598;
c->br_x_range.max = c->br_x_range.max;
c->br_y_range.min = 599;
c->br_y_range.max = c->br_y_range.max;
break;
case IMAGE_THUMB:
c->tl_x_range.min = 0;
c->tl_x_range.max = c->tl_x_range.max;
c->tl_y_range.min = 0;
c->tl_y_range.max = c->tl_y_range.max;
c->br_x_range.min = 79;
c->br_x_range.max = c->br_x_range.max;
c->br_y_range.min = 59;
c->br_y_range.max = c->br_y_range.max;
break;
case IMAGE_SUPER_RES:
c->tl_x_range.min = 0;
c->tl_x_range.max = c->tl_x_range.max;
c->tl_y_range.min = 0;
c->tl_y_range.max = c->tl_y_range.max;
c->br_x_range.min = 1598;
c->br_x_range.max = c->br_x_range.max;
c->br_y_range.min = 1199;
c->br_y_range.max = c->br_y_range.max;
break;
default:
return SANE_STATUS_INVAL;
}
c->imageMode = mode;
c->val[OPT_TL_X].w = c->tl_x_range.min;
c->val[OPT_TL_Y].w = c->tl_y_range.min;
c->val[OPT_BR_X].w = c->br_x_range.min;
c->val[OPT_BR_Y].w = c->br_y_range.min;
return SANE_STATUS_GOOD;
}
/**********************************************************************
//%FUNCTION: DMCCancel
//%ARGUMENTS:
// c -- a DMC camera device
//%RETURNS:
// SANE_STATUS_CANCELLED
//%DESCRIPTION:
// Cancels DMC image acquisition
// *********************************************************************/
static SANE_Status
DMCCancel(DMC_Camera *c)
{
if (c->fd >= 0) {
sanei_scsi_close(c->fd);
c->fd = -1;
}
return SANE_STATUS_CANCELLED;
}
/**********************************************************************
//%FUNCTION: DMCSetASA
//%ARGUMENTS:
// fd -- SCSI file descriptor
// asa -- the ASA to set
//%RETURNS:
// A sane status value
//%DESCRIPTION:
// Sets the equivalent ASA setting of the camera.
// *********************************************************************/
static SANE_Status
DMCSetASA(int fd, unsigned int asa)
{
uint8_t exposureCalculationResults[16];
SANE_Status status;
size_t len;
int i;
DBG(3, "DMCSetAsa: %d\n", asa);
for (i=1; i<=ASA_100+1; i++) {
if (asa == (unsigned int) ValidASAs[i]) break;
}
if (i > ASA_100+1) return SANE_STATUS_INVAL;
status = DMCRead(fd, 0x87, 0x4, exposureCalculationResults,
sizeof(exposureCalculationResults), &len);
if (status != SANE_STATUS_GOOD) return status;
if (len < sizeof(exposureCalculationResults)) return SANE_STATUS_IO_ERROR;
exposureCalculationResults[13] = (uint8_t) i - 1;
return DMCWrite(fd, 0x87, 0x4, exposureCalculationResults,
sizeof(exposureCalculationResults));
}
/**********************************************************************
//%FUNCTION: DMCSetWhiteBalance
//%ARGUMENTS:
// fd -- SCSI file descriptor
// mode -- white balance mode
//%RETURNS:
// A sane status value
//%DESCRIPTION:
// Sets the equivalent ASA setting of the camera.
// *********************************************************************/
static SANE_Status
DMCSetWhiteBalance(int fd, int mode)
{
uint8_t userInterfaceSettings[16];
SANE_Status status;
size_t len;
DBG(3, "DMCSetWhiteBalance: %d\n", mode);
status = DMCRead(fd, 0x82, 0x0, userInterfaceSettings,
sizeof(userInterfaceSettings), &len);
if (status != SANE_STATUS_GOOD) return status;
if (len < sizeof(userInterfaceSettings)) return SANE_STATUS_IO_ERROR;
userInterfaceSettings[5] = (uint8_t) mode;
return DMCWrite(fd, 0x82, 0x0, userInterfaceSettings,
sizeof(userInterfaceSettings));
}
/**********************************************************************
//%FUNCTION: DMCSetShutterSpeed
//%ARGUMENTS:
// fd -- SCSI file descriptor
// speed -- shutter speed in ms
//%RETURNS:
// A sane status value
//%DESCRIPTION:
// Sets the shutter speed of the camera
// *********************************************************************/
static SANE_Status
DMCSetShutterSpeed(int fd, unsigned int speed)
{
uint8_t exposureCalculationResults[16];
SANE_Status status;
size_t len;
DBG(3, "DMCSetShutterSpeed: %u\n", speed);
/* Convert from ms to ticks */
speed = MS_TO_TICKS(speed);
status = DMCRead(fd, 0x87, 0x4, exposureCalculationResults,
sizeof(exposureCalculationResults), &len);
if (status != SANE_STATUS_GOOD) return status;
if (len < sizeof(exposureCalculationResults)) return SANE_STATUS_IO_ERROR;
exposureCalculationResults[10] = (speed >> 8) & 0xFF;
exposureCalculationResults[11] = speed & 0xFF;
return DMCWrite(fd, 0x87, 0x4, exposureCalculationResults,
sizeof(exposureCalculationResults));
}
/**********************************************************************
//%FUNCTION: DMCReadTwoSuperResolutionLines
//%ARGUMENTS:
// c -- DMC Camera
// buf -- where to put output.
// lastLine -- if true, these are the last two lines in the super-resolution
// image to read.
//%RETURNS:
// Nothing
//%DESCRIPTION:
// Reads a single "raw" line from the camera (if needed) and constructs
// two "super-resolution" output lines in "buf"
// *********************************************************************/
static SANE_Status
DMCReadTwoSuperResolutionLines(DMC_Camera *c, SANE_Byte *buf, int lastLine)
{
SANE_Status status;
size_t len;
SANE_Byte *output, *prev;
int redCoeff, greenCoeff, blueCoeff;
int red, green, blue;
int i;
if (c->nextRawLineValid) {
memcpy(c->currentRawLine, c->nextRawLine, BYTES_PER_RAW_LINE);
} else {
status = DMCRead(c->fd, 0x00, IMAGE_RAW,
c->currentRawLine, BYTES_PER_RAW_LINE, &len);
if (status != SANE_STATUS_GOOD) return status;
}
if (!lastLine) {
status = DMCRead(c->fd, 0x00, IMAGE_RAW,
c->nextRawLine, BYTES_PER_RAW_LINE, &len);
if (status != SANE_STATUS_GOOD) return status;
c->nextRawLineValid = 1;
}
redCoeff = 3;
greenCoeff = 1;
blueCoeff = 2;
/* Do the first super-resolution line */
output = buf;
for (i=0; i<BYTES_PER_RAW_LINE; i++) {
red = redCoeff * c->currentRawLine[PREV_RED(i)] +
(3-redCoeff) * c->currentRawLine[NEXT_RED(i)];
green = greenCoeff * c->currentRawLine[PREV_GREEN(i)] +
(3-greenCoeff) * c->currentRawLine[NEXT_GREEN(i)];
blue = blueCoeff * c->currentRawLine[PREV_BLUE(i)] +
(3-blueCoeff) * c->currentRawLine[NEXT_BLUE(i)];
*output++ = red/3;
*output++ = green/3;
*output++ = blue/3;
redCoeff = ADVANCE_COEFF(redCoeff);
greenCoeff = ADVANCE_COEFF(greenCoeff);
blueCoeff = ADVANCE_COEFF(blueCoeff);
}
/* Do the next super-resolution line and interpolate vertically */
if (lastLine) {
memcpy(buf+BYTES_PER_RAW_LINE*3, buf, BYTES_PER_RAW_LINE*3);
return SANE_STATUS_GOOD;
}
redCoeff = 3;
greenCoeff = 1;
blueCoeff = 2;
prev = buf;
for (i=0; i<BYTES_PER_RAW_LINE; i++) {
red = redCoeff * c->nextRawLine[PREV_RED(i)] +
(3-redCoeff) * c->nextRawLine[NEXT_RED(i)];
green = greenCoeff * c->nextRawLine[PREV_GREEN(i)] +
(3-greenCoeff) * c->nextRawLine[NEXT_GREEN(i)];
blue = blueCoeff * c->nextRawLine[PREV_BLUE(i)] +
(3-blueCoeff) * c->nextRawLine[NEXT_BLUE(i)];
*output++ = (red/3 + *prev++) / 2;
*output++ = (green/3 + *prev++) / 2;
*output++ = (blue/3 + *prev++) / 2;
redCoeff = ADVANCE_COEFF(redCoeff);
greenCoeff = ADVANCE_COEFF(greenCoeff);
blueCoeff = ADVANCE_COEFF(blueCoeff);
}
return SANE_STATUS_GOOD;
}
/***********************************************************************
//%FUNCTION: attach_one (static function)
//%ARGUMENTS:
// dev -- device to attach
//%RETURNS:
// SANE_STATUS_GOOD
//%DESCRIPTION:
// tries to attach a device found by sanei_config_attach_matching_devices
// *********************************************************************/
static SANE_Status
attach_one (const char *dev)
{
DMCAttach (dev, 0);
return SANE_STATUS_GOOD;
}
/**********************************************************************
//%FUNCTION: sane_init
//%ARGUMENTS:
// version_code -- pointer to where we stick our version code
// authorize -- authorization function
//%RETURNS:
// A sane status value
//%DESCRIPTION:
// Initializes DMC sane system.
// *********************************************************************/
SANE_Status
sane_init(SANE_Int *version_code, SANE_Auth_Callback authorize)
{
char dev_name[PATH_MAX];
size_t len;
FILE *fp;
authorize = authorize;
DBG_INIT();
if (version_code) {
*version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, V_MINOR, 0);
}
fp = sanei_config_open(DMC_CONFIG_FILE);
if (!fp) {
/* default to /dev/camera instead of insisting on config file */
if (DMCAttach ("/dev/camera", NULL) != SANE_STATUS_GOOD) {
/* OK, try /dev/scanner */
DMCAttach("/dev/scanner", NULL);
}
return SANE_STATUS_GOOD;
}
while (sanei_config_read (dev_name, sizeof (dev_name), fp)) {
if (dev_name[0] == '#') { /* ignore line comments */
continue;
}
len = strlen (dev_name);
if (!len) continue; /* ignore empty lines */
sanei_config_attach_matching_devices(dev_name, attach_one);
}
fclose (fp);
return SANE_STATUS_GOOD;
}
/**********************************************************************
//%FUNCTION: sane_exit
//%ARGUMENTS:
// None
//%RETURNS:
// Nothing
//%DESCRIPTION:
// Cleans up all the SANE information
// *********************************************************************/
void
sane_exit(void)
{
DMC_Device *dev, *next;
/* Close all handles */
while(FirstHandle) {
sane_close(FirstHandle);
}
/* Free all devices */
dev = FirstDevice;
while(dev) {
next = dev->next;
free((char *) dev->sane.model);
free(dev);
dev = next;
}
}
/**********************************************************************
//%FUNCTION: sane_get_devices
//%ARGUMENTS:
// device_list -- set to allocated list of devices
// local_only -- ignored
//%RETURNS:
// A SANE status
//%DESCRIPTION:
// Returns a list of all known DMC devices
// *********************************************************************/
SANE_Status
sane_get_devices(SANE_Device const ***device_list, SANE_Bool local_only)
{
static SANE_Device const **devlist = 0;
DMC_Device *dev;
int i = 0;
local_only = local_only;
if (devlist) free(devlist);
devlist = malloc((NumDevices+1) * sizeof(devlist[0]));
if (!devlist) return SANE_STATUS_NO_MEM;
for (dev=FirstDevice; dev; dev = dev->next) {
devlist[i++] = &dev->sane;
}
devlist[i] = NULL;
if (device_list) *device_list = devlist;
return SANE_STATUS_GOOD;
}
/**********************************************************************
//%FUNCTION: sane_open
//%ARGUMENTS:
// name -- name of device to open
// handle -- set to a handle for the opened device
//%RETURNS:
// A SANE status
//%DESCRIPTION:
// Opens a DMC camera device
// *********************************************************************/
SANE_Status
sane_open(SANE_String_Const name, SANE_Handle *handle)
{
SANE_Status status;
DMC_Device *dev;
DMC_Camera *c;
/* If we're given a device name, search for it */
if (*name) {
for (dev = FirstDevice; dev; dev = dev->next) {
if (!strcmp(dev->sane.name, name)) {
break;
}
}
if (!dev) {
status = DMCAttach(name, &dev);
if (status != SANE_STATUS_GOOD) return status;
}
} else {
dev = FirstDevice;
}
if (!dev) return SANE_STATUS_INVAL;
c = malloc(sizeof(*c));
if (!c) return SANE_STATUS_NO_MEM;
memset(c, 0, sizeof(*c));
c->fd = -1;
c->hw = dev;
c->readBuffer = NULL;
c->readPtr = NULL;
c->imageMode = IMAGE_MFI;
c->inViewfinderMode = 0;
c->nextRawLineValid = 0;
DMCInitOptions(c);
c->next = FirstHandle;
FirstHandle = c;
if (handle) *handle = c;
return SANE_STATUS_GOOD;
}
/**********************************************************************
//%FUNCTION: sane_close
//%ARGUMENTS:
// handle -- handle of device to close
//%RETURNS:
// A SANE status
//%DESCRIPTION:
// Closes a DMC camera device
// *********************************************************************/
void
sane_close(SANE_Handle handle)
{
DMC_Camera *prev, *c;
prev = NULL;
for (c = FirstHandle; c; c = c->next) {
if (c == handle) break;
prev = c;
}
if (!c) {
DBG(1, "close: invalid handle %p\n", handle);
return;
}
DMCCancel(c);
if (prev) prev->next = c->next;
else FirstHandle = c->next;
if (c->readBuffer) {
free(c->readBuffer);
}
free(c);
}
/**********************************************************************
//%FUNCTION: sane_get_option_descriptor
//%ARGUMENTS:
// handle -- handle of device
// option -- option number to retrieve
//%RETURNS:
// An option descriptor or NULL on error
// *********************************************************************/
SANE_Option_Descriptor const *
sane_get_option_descriptor(SANE_Handle handle, SANE_Int option)
{
DMC_Camera *c = ValidateHandle(handle);
if (!c) return NULL;
if ((unsigned) option >= NUM_OPTIONS) return NULL;
return c->opt + option;
}
/**********************************************************************
//%FUNCTION: sane_control_option
//%ARGUMENTS:
// handle -- handle of device
// option -- option number to retrieve
// action -- what to do with the option
// val -- value to set option to
// info -- returned info flags
//%RETURNS:
// SANE status
//%DESCRIPTION:
// Sets or queries option values
// *********************************************************************/
SANE_Status
sane_control_option(SANE_Handle handle, SANE_Int option,
SANE_Action action, void *val, SANE_Int *info)
{
DMC_Camera *c;
SANE_Word cap;
SANE_Status status;
int i;
if (info) *info = 0;
c = ValidateHandle(handle);
if (!c) return SANE_STATUS_INVAL;
if (c->fd >= 0) return SANE_STATUS_DEVICE_BUSY;
if (option >= NUM_OPTIONS) return SANE_STATUS_INVAL;
cap = c->opt[option].cap;
if (!SANE_OPTION_IS_ACTIVE(cap)) return SANE_STATUS_INVAL;
if (action == SANE_ACTION_GET_VALUE) {
switch(c->opt[option].type) {
case SANE_TYPE_INT:
* (SANE_Int *) val = c->val[option].w;
return SANE_STATUS_GOOD;
case SANE_TYPE_STRING:
strcpy(val, c->val[option].s);
return SANE_STATUS_GOOD;
default:
DBG(3, "impossible option type!\n");
return SANE_STATUS_INVAL;
}
}
if (action == SANE_ACTION_SET_AUTO) {
return SANE_STATUS_UNSUPPORTED;
}
switch(option) {
case OPT_IMAGE_MODE:
for (i=0; i<NUM_IMAGE_MODES; i++) {
if (!strcmp(val, ValidModes[i])) {
status = DMCSetMode(c, i);
c->val[OPT_IMAGE_MODE].s = (SANE_String) ValidModes[i];
if (info) *info |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
return SANE_STATUS_GOOD;
}
}
break;
case OPT_WHITE_BALANCE:
for (i=0; i<=WHITE_BALANCE_FLUORESCENT; i++) {
if (!strcmp(val, ValidBalances[i])) {
c->val[OPT_WHITE_BALANCE].s = (SANE_String) ValidBalances[i];
return SANE_STATUS_GOOD;
}
}
break;
case OPT_ASA:
for (i=1; i<= ASA_100+1; i++) {
if (* ((SANE_Int *) val) == ValidASAs[i]) {
c->val[OPT_ASA].w = ValidASAs[i];
return SANE_STATUS_GOOD;
}
}
break;
case OPT_SHUTTER_SPEED:
if (* (SANE_Int *) val < c->hw->shutterSpeedRange.min ||
* (SANE_Int *) val > c->hw->shutterSpeedRange.max) {
return SANE_STATUS_INVAL;
}
c->val[OPT_SHUTTER_SPEED].w = * (SANE_Int *) val;
/* Do any roundoff */
c->val[OPT_SHUTTER_SPEED].w =
TICKS_TO_MS(MS_TO_TICKS(c->val[OPT_SHUTTER_SPEED].w));
if (c->val[OPT_SHUTTER_SPEED].w != * (SANE_Int *) val) {
if (info) *info |= SANE_INFO_INEXACT;
}
return SANE_STATUS_GOOD;
default:
/* Should really be INVAL, but just bit-bucket set requests... */
return SANE_STATUS_GOOD;
}
return SANE_STATUS_INVAL;
}
/**********************************************************************
//%FUNCTION: sane_get_parameters
//%ARGUMENTS:
// handle -- handle of device
// params -- set to device parameters
//%RETURNS:
// SANE status
//%DESCRIPTION:
// Returns parameters for current or next image.
// *********************************************************************/
SANE_Status
sane_get_parameters(SANE_Handle handle, SANE_Parameters *params)
{
DMC_Camera *c = ValidateHandle(handle);
if (!c) return SANE_STATUS_INVAL;
if (c->fd < 0) {
int width, height;
memset(&c->params, 0, sizeof(c->params));
width = c->val[OPT_BR_X].w - c->val[OPT_TL_X].w;
height = c->val[OPT_BR_Y].w - c->val[OPT_TL_Y].w;
c->params.pixels_per_line = width + 1;
c->params.lines = height+1;
c->params.depth = 8;
c->params.last_frame = SANE_TRUE;
switch(c->imageMode) {
case IMAGE_SUPER_RES:
case IMAGE_MFI:
case IMAGE_THUMB:
c->params.format = SANE_FRAME_RGB;
c->params.bytes_per_line = c->params.pixels_per_line * 3;
break;
case IMAGE_RAW:
case IMAGE_VIEWFINDER:
c->params.format = SANE_FRAME_GRAY;
c->params.bytes_per_line = c->params.pixels_per_line;
break;
}
}
if (params) *params = c->params;
return SANE_STATUS_GOOD;
}
/**********************************************************************
//%FUNCTION: sane_start
//%ARGUMENTS:
// handle -- handle of device
//%RETURNS:
// SANE status
//%DESCRIPTION:
// Starts acquisition
// *********************************************************************/
SANE_Status
sane_start(SANE_Handle handle)
{
static uint8_t const acquire[] =
{ 0xC1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static uint8_t const viewfinder[] =
{ 0xCB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static uint8_t const no_viewfinder[] =
{ 0xC6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
DMC_Camera *c = ValidateHandle(handle);
SANE_Status status;
int i;
if (!c) return SANE_STATUS_INVAL;
/* If we're already open, barf -- not sure this is the best status */
if (c->fd >= 0) return SANE_STATUS_DEVICE_BUSY;
/* Get rid of old read buffers */
if (c->readBuffer) {
free(c->readBuffer);
c->readBuffer = NULL;
c->readPtr = NULL;
}
c->nextRawLineValid = 0;
/* Refresh parameter list */
status = sane_get_parameters(c, NULL);
if (status != SANE_STATUS_GOOD) return status;
status = sanei_scsi_open(c->hw->sane.name, &c->fd, NULL, NULL);
if (status != SANE_STATUS_GOOD) {
c->fd = -1;
DBG(1, "DMC: Open of `%s' failed: %s\n",
c->hw->sane.name, sane_strstatus(status));
return status;
}
/* Set ASA and shutter speed if they're no longer current */
if (c->val[OPT_ASA].w != c->hw->asa) {
status = DMCSetASA(c->fd, c->val[OPT_ASA].w);
if (status != SANE_STATUS_GOOD) {
DMCCancel(c);
return status;
}
c->hw->asa = c->val[OPT_ASA].w;
}
if ((unsigned int) c->val[OPT_SHUTTER_SPEED].w != c->hw->shutterSpeed) {
status = DMCSetShutterSpeed(c->fd, c->val[OPT_SHUTTER_SPEED].w);
if (status != SANE_STATUS_GOOD) {
DMCCancel(c);
return status;
}
c->hw->shutterSpeed = c->val[OPT_SHUTTER_SPEED].w;
}
/* Set white balance mode if needed */
for (i=0; i<=WHITE_BALANCE_FLUORESCENT; i++) {
if (!strcmp(ValidBalances[i], c->val[OPT_WHITE_BALANCE].s)) {
if (i != c->hw->whiteBalance) {
status = DMCSetWhiteBalance(c->fd, i);
if (status != SANE_STATUS_GOOD) {
DMCCancel(c);
return status;
}
c->hw->whiteBalance = i;
}
}
}
/* Flip into viewfinder mode if needed */
if (c->imageMode == IMAGE_VIEWFINDER && !c->inViewfinderMode) {
status = sanei_scsi_cmd(c->fd, viewfinder, sizeof(viewfinder),
NULL, NULL);
if (status != SANE_STATUS_GOOD) {
DMCCancel(c);
return status;
}
c->inViewfinderMode = 1;
}
/* Flip out of viewfinder mode if needed */
if (c->imageMode != IMAGE_VIEWFINDER && c->inViewfinderMode) {
status = sanei_scsi_cmd(c->fd, no_viewfinder, sizeof(no_viewfinder),
NULL, NULL);
if (status != SANE_STATUS_GOOD) {
DMCCancel(c);
return status;
}
c->inViewfinderMode = 0;
}
status = sanei_scsi_cmd(c->fd, acquire, sizeof(acquire), NULL, NULL);
if (status != SANE_STATUS_GOOD) {
DMCCancel(c);
return status;
}
c->bytes_to_read = c->params.bytes_per_line * c->params.lines;
return SANE_STATUS_GOOD;
}
/**********************************************************************
//%FUNCTION: sane_read
//%ARGUMENTS:
// handle -- handle of device
// buf -- destination for data
// max_len -- maximum amount of data to store
// len -- set to actual amount of data stored.
//%RETURNS:
// SANE status
//%DESCRIPTION:
// Reads image data from the camera
// *********************************************************************/
SANE_Status
sane_read(SANE_Handle handle, SANE_Byte *buf, SANE_Int max_len, SANE_Int *len)
{
SANE_Status status;
DMC_Camera *c = ValidateHandle(handle);
size_t size;
SANE_Int i;
if (!c) return SANE_STATUS_INVAL;
if (c->fd < 0) return SANE_STATUS_INVAL;
if (c->bytes_to_read == 0) {
if (c->readBuffer) {
free(c->readBuffer);
c->readBuffer = NULL;
c->readPtr = NULL;
}
DMCCancel(c);
return SANE_STATUS_EOF;
}
if (max_len == 0) {
return SANE_STATUS_GOOD;
}
if (c->imageMode == IMAGE_SUPER_RES) {
/* We have to read *two* complete rows... */
max_len = (max_len / (2*c->params.bytes_per_line)) *
(2*c->params.bytes_per_line);
/* If user is trying to read less than two complete lines, fail */
if (max_len == 0) return SANE_STATUS_INVAL;
if ((unsigned int) max_len > c->bytes_to_read) max_len = c->bytes_to_read;
for (i=0; i<max_len; i += 2*c->params.bytes_per_line) {
c->bytes_to_read -= 2*c->params.bytes_per_line;
status = DMCReadTwoSuperResolutionLines(c, buf+i,
!c->bytes_to_read);
if (status != SANE_STATUS_GOOD) return status;
}
*len = max_len;
return SANE_STATUS_GOOD;
}
if (c->imageMode == IMAGE_MFI || c->imageMode == IMAGE_RAW) {
/* We have to read complete rows... */
max_len = (max_len / c->params.bytes_per_line) * c->params.bytes_per_line;
/* If user is trying to read less than one complete row, fail */
if (max_len == 0) return SANE_STATUS_INVAL;
if ((unsigned int) max_len > c->bytes_to_read) max_len = c->bytes_to_read;
c->bytes_to_read -= (unsigned int) max_len;
status = DMCRead(c->fd, 0x00, c->imageMode, buf, max_len, &size);
*len = size;
return status;
}
if ((unsigned int) max_len > c->bytes_to_read) max_len = c->bytes_to_read;
if (c->readPtr) {
*len = max_len;
memcpy(buf, c->readPtr, max_len);
c->readPtr += max_len;
c->bytes_to_read -= max_len;
return SANE_STATUS_GOOD;
}
/* Fill the read buffer completely */
c->readBuffer = malloc(c->bytes_to_read);
if (!c->readBuffer) return SANE_STATUS_NO_MEM;
c->readPtr = c->readBuffer;
status = DMCRead(c->fd, 0x00, c->imageMode, (SANE_Byte *) c->readBuffer,
c->bytes_to_read, &size);
*len = size;
if (status != SANE_STATUS_GOOD) return status;
if ((unsigned int) *len != c->bytes_to_read) return SANE_STATUS_IO_ERROR;
/* Now copy */
*len = max_len;
memcpy(buf, c->readPtr, max_len);
c->readPtr += max_len;
c->bytes_to_read -= max_len;
return SANE_STATUS_GOOD;
}
/**********************************************************************
//%FUNCTION: sane_cancel
//%ARGUMENTS:
// handle -- handle of device
//%RETURNS:
// Nothing
//%DESCRIPTION:
// A quick cancellation of the scane
// *********************************************************************/
void
sane_cancel (SANE_Handle handle)
{
DMC_Camera *c = ValidateHandle(handle);
if (!c) return;
DMCCancel(c);
}
SANE_Status
sane_set_io_mode (SANE_Handle handle, SANE_Bool non_blocking)
{
handle = handle;
non_blocking = non_blocking;
return SANE_STATUS_UNSUPPORTED;
}
SANE_Status
sane_get_select_fd (SANE_Handle handle, SANE_Int *fd)
{
handle = handle;
fd = fd;
return SANE_STATUS_UNSUPPORTED;
}
|