summaryrefslogtreecommitdiff
path: root/backend/snapscan-sources.c
blob: 45ad6b7e8183e5fa1f3038a2c937e5eb46726475 (plain)
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
/* sane - Scanner Access Now Easy.

   Copyright (C) 1997, 1998, 2002, 2013 Franck Schnefra, Michel Roelofs,
   Emmanuel Blot, Mikko Tyolajarvi, David Mosberger-Tang, Wolfgang Goeller,
   Petter Reinholdtsen, Gary Plewa, Sebastien Sable, Max Ushakov,
   Andrew Goodbody, Oliver Schwartz and Kevin Charter

   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, see <https://www.gnu.org/licenses/>.

   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 is a component of the implementation of a backend for many
   of the AGFA SnapScan and Acer Vuego/Prisa flatbed scanners.
*/

/*
   SnapScan backend data sources (implementation)
*/

/**************************************************************************************
If you get confused from all the structs (like I did when I first saw them),
think of it as "C++ in C". If you're accustomed to OO and UML maybe the
following diagram helps you to make sense of it:

                       ------------------------
                       !        Source        !
                       ------------------------
                       !pss: SnapScan_Scanner*!
                       ------------------------   +psub
                       !init() = 0            !-----------------
                       !remaining() = 0       !                !
                       !bytesPerLine()        !                !
                       !pixelsPerLine()       !                !
                       !get() = 0             !                !{TransformerSource forwards
                       !done() = 0            !                ! function calls to corres-
                       ------------------------                ! ponding functions in psub}
                                   ^                           !
                                  /_\                          !
                                   !                           !
       --------------------------------------------------     /\
       !               !                !               !     \/
-------------    -------------    -------------    -------------------
!SCSISource !    ! FDSource  !    !BufSource  !    !TransformerSource!
=============    =============    =============    ===================
!remaining()!    !remaining()!    !remaining()!    !init()           !
!get()      !    !get()      !    !get()      !    !remaining()      !
!done()     !    !done()     !    !done()     !    !bytesPerLine()   !
!init()     !    !init()     !    !init()     !    !pixelsPerLine()  !
-------------    -------------    -------------    !get()            !
                                                   !done()           !
                                                   -------------------
                                                            ^
                                                           /_\
                                                            !
                                       ------------------------------------
                                       !                 !                !
                              ----------------    -------------    -------------
                              !   Expander   !    ! RGBRouter !    !  Inverter !
                              ================    =============    =============
                              !remaining()   !    !remaining()!    !remaining()!
                              !bytesPerLine()!    !get()      !    !get()      !
                              !get()         !    !done()     !    !done()     !
                              !done()        !    !init()     !    !init()     !
                              !init()        !    -------------    -------------
                              ----------------
All instances of the descendants of TransformerSource can be chained together. For
color scanning, a typical source chain would consist of an RGBRouter sitting on top
of a SCSISource. In the get() method, RGBRouter will then call the get() method of
the subsource, process the data and return it.

I hope this makes sense to you (and I got the right idea of the original author's
intention).
***********************************************************************************/

static SANE_Status Source_init (Source *pself,
                                SnapScan_Scanner *pss,
                                SourceRemaining remaining,
                                SourceBytesPerLine bytesPerLine,
                                SourcePixelsPerLine pixelsPerLine,
                                SourceGet get,
                                SourceDone done)
{
    pself->pss = pss;
    pself->remaining = remaining;
    pself->bytesPerLine = bytesPerLine;
    pself->pixelsPerLine = pixelsPerLine;
    pself->get = get;
    pself->done = done;
    return SANE_STATUS_GOOD;
}

/* these are defaults, normally used only by base sources */

static SANE_Int Source_bytesPerLine (Source *pself)
{
    return pself->pss->bytes_per_line;
}

static SANE_Int Source_pixelsPerLine (Source *pself)
{
    return pself->pss->pixels_per_line;
}

/**********************************************************************/

/* the base sources */
typedef enum
{
    SCSI_SRC,
    FD_SRC,
    BUF_SRC
} BaseSourceType;


typedef struct
{
    SOURCE_GUTS;
    SANE_Int scsi_buf_pos;    /* current position in scsi buffer */
    SANE_Int scsi_buf_max;    /* data limit */
    SANE_Int absolute_max;    /* largest possible data read */
} SCSISource;

static SANE_Int SCSISource_remaining (Source *pself)
{
    SCSISource *ps = (SCSISource *) pself;
    return ps->pss->bytes_remaining + (ps->scsi_buf_max - ps->scsi_buf_pos);
}

static SANE_Status SCSISource_get (Source *pself,
                                   SANE_Byte *pbuf,
                                   SANE_Int *plen)
{
    SCSISource *ps = (SCSISource *) pself;
    SANE_Status status = SANE_STATUS_GOOD;
    SANE_Int remaining = *plen;
    char* me = "SCSISource_get";

    DBG (DL_CALL_TRACE, "%s\n", me);
    while (remaining > 0
           && pself->remaining(pself) > 0
           && status == SANE_STATUS_GOOD
           && !cancelRead)
    {
        SANE_Int ndata = ps->scsi_buf_max - ps->scsi_buf_pos;
        DBG (DL_DATA_TRACE, "%s: ndata %d; remaining %d\n", me, ndata, remaining);
        if (ndata == 0)
        {
            ps->pss->expected_read_bytes = MIN((size_t)ps->absolute_max,
                                           ps->pss->bytes_remaining);
            ps->scsi_buf_pos = 0;
            ps->scsi_buf_max = 0;
            status = scsi_read (ps->pss, READ_IMAGE);
            if (status != SANE_STATUS_GOOD)
                break;
            ps->scsi_buf_max = ps->pss->read_bytes;
            ndata = ps->pss->read_bytes;
            ps->pss->bytes_remaining -= ps->pss->read_bytes;
            DBG (DL_DATA_TRACE, "%s: pos: %d; max: %d; expected: %lu; read: %lu\n",
                me, ps->scsi_buf_pos, ps->scsi_buf_max, (u_long) ps->pss->expected_read_bytes,
                (u_long) ps->pss->read_bytes);
        }
        ndata = MIN(ndata, remaining);
        memcpy (pbuf, ps->pss->buf + ps->scsi_buf_pos, (size_t)ndata);
        pbuf += ndata;
        ps->scsi_buf_pos += ndata;
        remaining -= ndata;
    }
    *plen -= remaining;
    return status;
}

static SANE_Status SCSISource_done (Source *pself)
{
    DBG(DL_MINOR_INFO, "SCSISource_done\n");
    UNREFERENCED_PARAMETER(pself);
    return SANE_STATUS_GOOD;
}

static SANE_Status SCSISource_init (SCSISource *pself, SnapScan_Scanner *pss)
{
    SANE_Status status = Source_init ((Source *) pself, pss,
                                      SCSISource_remaining,
                                      Source_bytesPerLine,
                                      Source_pixelsPerLine,
                                      SCSISource_get,
                                      SCSISource_done);
    if (status == SANE_STATUS_GOOD)
    {
        pself->scsi_buf_max = 0;
        pself->scsi_buf_pos = 0;
        pself->absolute_max =
            (pss->phys_buf_sz/pss->bytes_per_line)*pss->bytes_per_line;
    }
    return status;
}

/* File sources */

typedef struct
{
    SOURCE_GUTS;
    int fd;
    SANE_Int bytes_remaining;
} FDSource;

static SANE_Int FDSource_remaining (Source *pself)
{
    FDSource *ps = (FDSource *) pself;
    return ps->bytes_remaining;
}

static SANE_Status FDSource_get (Source *pself, SANE_Byte *pbuf, SANE_Int *plen)
{
    SANE_Status status = SANE_STATUS_GOOD;
    FDSource *ps = (FDSource *) pself;
    SANE_Int remaining = *plen;

    while (remaining > 0
           && pself->remaining(pself) > 0
           && status == SANE_STATUS_GOOD)
    {
        SANE_Int bytes_read = read (ps->fd, pbuf, remaining);
        if (bytes_read == -1)
        {
            if (errno == EAGAIN)
            {
                /* No data currently available */
                break;
            }
            /* It's an IO error */
            DBG (DL_MAJOR_ERROR, "%s: read failed: %s\n",
                     __func__, strerror(errno));
            status = SANE_STATUS_IO_ERROR;
        }
        else if (bytes_read == 0)
        {
            /* EOF of current reading */
            DBG(DL_DATA_TRACE, "%s: EOF\n",__func__);
            break;
        }
        ps->bytes_remaining -= bytes_read;
        remaining -= bytes_read;
        pbuf += bytes_read;
    }
    *plen -= remaining;
    return status;
}

static SANE_Status FDSource_done (Source *pself)
{
    close(((FDSource *) pself)->fd);
    return SANE_STATUS_GOOD;
}

static SANE_Status FDSource_init (FDSource *pself,
                                  SnapScan_Scanner *pss,
                                  int fd)
{
    SANE_Status status = Source_init ((Source *) pself,
                                      pss,
                                      FDSource_remaining,
                                      Source_bytesPerLine,
                                      Source_pixelsPerLine,
                                      FDSource_get,
                                      FDSource_done);
    if (status == SANE_STATUS_GOOD)
    {
        pself->fd = fd;
        pself->bytes_remaining = pss->bytes_per_line * (pss->lines + pss->chroma);
    }
    return status;
}


/* buffer sources simply read from a pre-filled buffer; we have these
   so that we can include source chain processing overhead in the
   measure_transfer_rate() function */

typedef struct
{
    SOURCE_GUTS;
    SANE_Byte *buf;
    SANE_Int buf_size;
    SANE_Int buf_pos;
} BufSource;

static SANE_Int BufSource_remaining (Source *pself)
{
    BufSource *ps = (BufSource *) pself;
    return  ps->buf_size - ps->buf_pos;
}

static SANE_Status BufSource_get (Source *pself,
                                  SANE_Byte *pbuf,
                                  SANE_Int *plen)
{
    BufSource *ps = (BufSource *) pself;
    SANE_Status status = SANE_STATUS_GOOD;
    SANE_Int to_move = MIN(*plen, pself->remaining(pself));
    if (to_move == 0)
    {
        status = SANE_STATUS_EOF;
    }
    else
    {
        memcpy (pbuf, ps->buf + ps->buf_pos, to_move);
        ps->buf_pos += to_move;
        *plen = to_move;
    }
    return status;
}

static SANE_Status BufSource_done (Source *pself)
{
    UNREFERENCED_PARAMETER(pself);
    return SANE_STATUS_GOOD;
}

static SANE_Status BufSource_init (BufSource *pself,
                                   SnapScan_Scanner *pss,
                                     SANE_Byte *buf,
                                   SANE_Int buf_size)
{
    SANE_Status status = Source_init ((Source *) pself,
                                      pss,
                                      BufSource_remaining,
                                      Source_bytesPerLine,
                                      Source_pixelsPerLine,
                                      BufSource_get,
                                      BufSource_done);
    DBG(DL_DATA_TRACE, "BufSource_init: buf_size=%d\n", buf_size);
    if (status == SANE_STATUS_GOOD)
    {
        pself->buf = buf;
        pself->buf_size = buf_size;
        pself->buf_pos = 0;
    }
    return status;
}

/* base source creation */

static SANE_Status create_base_source (SnapScan_Scanner *pss,
                                       BaseSourceType st,
                                       Source **pps)
{
    SANE_Status status = SANE_STATUS_GOOD;
    *pps = NULL;
    switch (st)
    {
    case SCSI_SRC:
        *pps = (Source *) malloc(sizeof(SCSISource));
        if (*pps == NULL)
        {
            DBG (DL_MAJOR_ERROR, "failed to allocate SCSISource");
            status = SANE_STATUS_NO_MEM;
        }
        else
        {
            status = SCSISource_init ((SCSISource *) *pps, pss);
        }
    break;
    case FD_SRC:
        *pps = (Source *) malloc(sizeof(FDSource));
        if (*pps == NULL)
        {
            DBG (DL_MAJOR_ERROR, "failed to allocate FDSource");
            status = SANE_STATUS_NO_MEM;
        }
        else
        {
            status = FDSource_init ((FDSource *) *pps, pss, pss->rpipe[0]);
        }
    break;
    case BUF_SRC:
        *pps = (Source *) malloc(sizeof(BufSource));
        if (*pps == NULL)
        {
            DBG (DL_MAJOR_ERROR, "failed to allocate BufSource");
            status = SANE_STATUS_NO_MEM;
        }
        else
        {
            status = BufSource_init ((BufSource *) *pps,
                                     pss,
                                     pss->buf,
                                     pss->read_bytes);
        }
    break;
    default:
        DBG (DL_MAJOR_ERROR, "illegal base source type %d", st);
        break;
    }
    return status;
}

/**********************************************************************/

/* The transformer sources */

#define TX_SOURCE_GUTS \
        SOURCE_GUTS;\
        Source *psub    /* sub-source */

typedef struct
{
    TX_SOURCE_GUTS;
} TxSource;

static SANE_Int TxSource_remaining (Source *pself)
{
    TxSource *ps = (TxSource *) pself;
    return ps->psub->remaining(ps->psub);
}

static SANE_Int TxSource_bytesPerLine (Source *pself)
{
    TxSource *ps = (TxSource *) pself;
    return ps->psub->bytesPerLine(ps->psub);
}

static SANE_Int TxSource_pixelsPerLine (Source *pself)
{
    TxSource *ps = (TxSource *) pself;
    return ps->psub->pixelsPerLine(ps->psub);
}

static SANE_Status TxSource_get (Source *pself, SANE_Byte *pbuf, SANE_Int *plen)
{
    TxSource *ps = (TxSource *) pself;
    return ps->psub->get(ps->psub, pbuf, plen);
}

static SANE_Status TxSource_done (Source *pself)
{
    TxSource *ps = (TxSource *) pself;
    SANE_Status status = ps->psub->done(ps->psub);
    free(ps->psub);
    ps->psub = NULL;
    return status;
}

static SANE_Status TxSource_init (TxSource *pself,
                                  SnapScan_Scanner *pss,
                                  SourceRemaining remaining,
                                  SourceBytesPerLine bytesPerLine,
                                  SourcePixelsPerLine pixelsPerLine,
                                  SourceGet get,
                                  SourceDone done,
                                  Source *psub)
{
    SANE_Status status = Source_init((Source *) pself,
                                     pss,
                                     remaining,
                                     bytesPerLine,
                                     pixelsPerLine,
                                     get,
                                     done);
    if (status == SANE_STATUS_GOOD)
        pself->psub = psub;
    return status;
}

/* The expander makes three-channel, one-bit, raw scanner data into
   8-bit data. It is used to support the bilevel colour scanning mode */

typedef struct
{
    TX_SOURCE_GUTS;
    SANE_Byte *ch_buf;            /* channel buffer */
    SANE_Int   ch_size;            /* channel buffer size = #bytes in a channel */
    SANE_Int   ch_ndata;        /* actual #bytes in channel buffer */
    SANE_Int   ch_pos;            /* position in buffer */
    SANE_Int   bit;                /* current bit */
    SANE_Int   last_bit;        /* current last bit (counting down) */
    SANE_Int   last_last_bit;    /* last bit in the last byte of the channel */
} Expander;

static SANE_Int Expander_remaining (Source *pself)
{
    Expander *ps = (Expander *) pself;
    SANE_Int sub_remaining = TxSource_remaining(pself);
    SANE_Int sub_bits_per_channel = TxSource_pixelsPerLine(pself);
    SANE_Int whole_channels = sub_remaining/ps->ch_size;
    SANE_Int result = whole_channels*sub_bits_per_channel;

    if (ps->ch_pos < ps->ch_size)
    {
        SANE_Int bits_covered = MAX((ps->ch_pos - 1)*8, 0) + 7 - ps->bit;
        result += sub_bits_per_channel - bits_covered;
    }

    return result;
}

static SANE_Int Expander_bytesPerLine (Source *pself)
{
    return TxSource_pixelsPerLine(pself)*3;
}

static SANE_Status Expander_get (Source *pself, SANE_Byte *pbuf, SANE_Int *plen)
{
    Expander *ps = (Expander *) pself;
    SANE_Status status = SANE_STATUS_GOOD;
    SANE_Int remaining = *plen;

    while (remaining > 0
           &&
           pself->remaining(pself) > 0 &&
           !cancelRead)
    {
        if (ps->ch_pos == ps->ch_ndata)
        {
            /* we need more data; try to get the remainder of the current
               channel, or else the next channel */
            SANE_Int ndata = ps->ch_size - ps->ch_ndata;
            if (ndata == 0)
            {
                ps->ch_ndata = 0;
                ps->ch_pos = 0;
                ndata = ps->ch_size;
            }
            status = TxSource_get(pself, ps->ch_buf + ps->ch_pos, &ndata);
            if (status != SANE_STATUS_GOOD)
                break;
            if (ndata == 0)
                break;
            ps->ch_ndata += ndata;
            if (ps->ch_pos == (ps->ch_size - 1))
                ps->last_bit = ps->last_last_bit;
            else
                ps->last_bit = 0;
            ps->bit = 7;
        }
        *pbuf = ((ps->ch_buf[ps->ch_pos] >> ps->bit) & 0x01) ? 0xFF : 0x00;
        pbuf++;
        remaining--;

        if (ps->bit == ps->last_bit)
        {
            ps->bit = 7;
            ps->ch_pos++;
            if (ps->ch_pos == (ps->ch_size - 1))
                ps->last_bit = ps->last_last_bit;
            else
                ps->last_bit = 0;
        }
        else
        {
            ps->bit--;
        }
    }

    *plen -= remaining;
    return status;
}

static SANE_Status Expander_done (Source *pself)
{
    Expander *ps = (Expander *) pself;
    SANE_Status status = TxSource_done(pself);
    free(ps->ch_buf);
    ps->ch_buf = NULL;
    ps->ch_size = 0;
    ps->ch_pos = 0;
    return status;
}

static SANE_Status Expander_init (Expander *pself,
                  SnapScan_Scanner *pss,
                                  Source *psub)
{
    SANE_Status status = TxSource_init((TxSource *) pself,
                                       pss,
                                       Expander_remaining,
                                       Expander_bytesPerLine,
                                       TxSource_pixelsPerLine,
                                       Expander_get,
                                       Expander_done,
                                       psub);
    if (status == SANE_STATUS_GOOD)
    {
        pself->ch_size = TxSource_bytesPerLine((Source *) pself)/3;
        pself->ch_buf = (SANE_Byte *) malloc(pself->ch_size);
        if (pself->ch_buf == NULL)
        {
            DBG (DL_MAJOR_ERROR,
                 "%s: couldn't allocate channel buffer.\n",
                 __func__);
            status = SANE_STATUS_NO_MEM;
        }
        else
        {
            pself->ch_ndata = 0;
            pself->ch_pos = 0;
            pself->last_last_bit = pself->pixelsPerLine((Source *) pself)%8;
            if (pself->last_last_bit == 0)
                pself->last_last_bit = 7;
            pself->last_last_bit = 7 - pself->last_last_bit;
            pself->bit = 7;
            if (pself->ch_size > 1)
                pself->last_bit = 0;
            else
                pself->last_bit = pself->last_last_bit;
        }
    }
    return status;
}

static SANE_Status create_Expander (SnapScan_Scanner *pss,
                                    Source *psub,
                                    Source **pps)
{
    SANE_Status status = SANE_STATUS_GOOD;
    *pps = (Source *) malloc(sizeof(Expander));
    if (*pps == NULL)
    {
        DBG (DL_MAJOR_ERROR,
             "%s: failed to allocate Expander.\n",
             __func__);
        status = SANE_STATUS_NO_MEM;
    }
    else
    {
        status = Expander_init ((Expander *) *pps, pss, psub);
    }
    return status;
}

/*
   This filter implements a fix for scanners that have some columns
   of pixels offset. Currently it only shifts every other column
   starting with the first one down ch_offset pixels.

   The Deinterlacer detects if data is in SANE RGB frame format (3 bytes/pixel)
   or in Grayscale (1 byte/pixel).

   The first ch_offset lines of data in the output are fudged so that even indexed
   add odd indexed pixels will have the same value. This is necessary because
   the real pixel values of the columns that are shifted down are not
   in the data for the first ch_offset lines. A better way to handle this would be to
   scan in ch_offset extra lines of data, but I haven't figured out how to do this
   yet.

*/

typedef struct
{
    TX_SOURCE_GUTS;
    SANE_Byte *ch_buf;            /* channel buffer */
    SANE_Int   ch_size;           /* channel buffer size */
    SANE_Int   ch_line_size;      /* size of one line */
    SANE_Int   ch_ndata;          /* actual #bytes in channel buffer */
    SANE_Int   ch_pos;            /* position in buffer */
    SANE_Int   ch_bytes_per_pixel;
    SANE_Bool  ch_lineart;
    SANE_Int   ch_offset;         /* The number of lines to be shifted */
    SANE_Bool  ch_past_init;      /* flag indicating if we have enough data to shift pixels down */
    SANE_Bool  ch_shift_even;     /* flag indicating whether even or odd pixels are shifted */
} Deinterlacer;

static SANE_Int Deinterlacer_remaining (Source *pself)
{
    Deinterlacer *ps = (Deinterlacer *) pself;
    SANE_Int result = TxSource_remaining(pself);
    result += ps->ch_ndata - ps->ch_pos;
    return result;
}

static SANE_Status Deinterlacer_get (Source *pself, SANE_Byte *pbuf, SANE_Int *plen)
{
    Deinterlacer *ps = (Deinterlacer *) pself;
    SANE_Status status = SANE_STATUS_GOOD;
    SANE_Int remaining = *plen;
    SANE_Int org_len = *plen;
    char *me = "Deinterlacer_get";

    DBG(DL_DATA_TRACE, "%s: remaining=%d, pself->remaining=%d, ch_ndata=%d, ch_pos=%d\n",
            me, remaining, pself->remaining(pself), ps->ch_ndata, ps->ch_pos);

    while (remaining > 0
           &&
           pself->remaining(pself) > 0 &&
           !cancelRead)
    {
        if (ps->ch_pos % (ps->ch_line_size) == ps->ch_ndata % (ps->ch_line_size) )
        {
            /* we need more data; try to get the remainder of the current
               line, or else the next line */
            SANE_Int ndata = (ps->ch_line_size) - ps->ch_ndata % (ps->ch_line_size);
            if (ps->ch_pos >= ps->ch_size)
            {
	        /* wrap to the beginning of the buffer if we need to */
                ps->ch_ndata = 0;
                ps->ch_pos = 0;
                ndata = ps->ch_line_size;
            }
            status = TxSource_get(pself, ps->ch_buf + ps->ch_pos, &ndata);
            if (status != SANE_STATUS_GOOD)
                break;
            if (ndata == 0)
                break;
            ps->ch_ndata += ndata;
        }
        /* Handle special lineart mode: Valid pixels need to be masked */
        if (ps->ch_lineart)
        {
            if (ps->ch_past_init)
            {
                if (ps->ch_shift_even)
                {
                    /* Even columns need to be shifted, i.e. bits 1,3,5,7 -> 0xaa */
                    /* use valid pixels from this line and shifted pixels from ch_size lines back */
                    *pbuf = (ps->ch_buf[ps->ch_pos] & 0x55) |
                            (ps->ch_buf[(ps->ch_pos + (ps->ch_line_size)) % ps->ch_size] & 0xaa);
                }
                else
                {
                    /* Odd columns need to be shifted, i.e. bits 0,2,4,6 -> 0x55 */
                    *pbuf = (ps->ch_buf[ps->ch_pos] & 0xaa) |
                            (ps->ch_buf[(ps->ch_pos + (ps->ch_line_size)) % ps->ch_size] & 0x55);
                }
            }
            else
            {
                /* not enough data. duplicate pixel values from previous column */
                if (ps->ch_shift_even)
                {
                    /* bits 0,2,4,6 contain valid data -> 0x55 */
                    SANE_Byte valid_pixel = ps->ch_buf[ps->ch_pos] & 0x55;
                    *pbuf = valid_pixel | (valid_pixel >> 1);
                }
                else
                {

                    /* bits 1,3,5,7 contain valid data -> 0xaa */
                    SANE_Byte valid_pixel = ps->ch_buf[ps->ch_pos] & 0xaa;
                    *pbuf = valid_pixel | (valid_pixel << 1);
                }
            }
        }
        else /* colour / grayscale mode */
        {
            if ((ps->ch_shift_even && ((ps->ch_pos/ps->ch_bytes_per_pixel) % 2 == 0)) ||
                (!ps->ch_shift_even && ((ps->ch_pos/ps->ch_bytes_per_pixel) % 2 == 1)))
            {
                /* the even indexed pixels need to be shifted down */
                if (ps->ch_past_init){
                    /* We need to use data 4 lines back */
                    /* So we just go one forward and it will wrap around to 4 back. */
                    *pbuf = ps->ch_buf[(ps->ch_pos + (ps->ch_line_size)) % ps->ch_size];
                }else{
                    /* Use data from the next pixel for even indexed pixels
                      if we are on the first few lines.
                      TODO: also we will overread the buffer if the buffer read ended
                      on the first pixel. */
                    if (ps->ch_pos % (ps->ch_line_size) == 0 )
                        *pbuf = ps->ch_buf[ps->ch_pos+ps->ch_bytes_per_pixel];
                    else
                        *pbuf = ps->ch_buf[ps->ch_pos-ps->ch_bytes_per_pixel];
                }
            }else{
                /* odd indexed pixels are okay */
                *pbuf = ps->ch_buf[ps->ch_pos];
            }
        }
	/* set the flag so we know we have enough data to start shifting columns */
	if (ps->ch_pos >= ps->ch_line_size * ps->ch_offset)
	   ps->ch_past_init = SANE_TRUE;

        pbuf++;
        remaining--;
        ps->ch_pos++;
    }

    *plen -= remaining;

    DBG(DL_DATA_TRACE,
        "%s: Request=%d, remaining=%d, read=%d, TXSource_rem=%d, bytes_rem=%lu\n",
        me,
        org_len,
        pself->remaining(pself),
        *plen,
        TxSource_remaining(pself),
        (u_long) ps->pss->bytes_remaining);
    return status;
}

static SANE_Status Deinterlacer_done (Source *pself)
{
    Deinterlacer *ps = (Deinterlacer *) pself;
    SANE_Status status = TxSource_done(pself);
    free(ps->ch_buf);
    ps->ch_buf = NULL;
    ps->ch_size = 0;
    ps->ch_line_size = 0;
    ps->ch_pos = 0;
    return status;
}

static SANE_Status Deinterlacer_init (Deinterlacer *pself,
                  SnapScan_Scanner *pss,
                                  Source *psub)
{
    SANE_Status status = TxSource_init((TxSource *) pself,
                                       pss,
                                       Deinterlacer_remaining,
                                       TxSource_bytesPerLine,
                                       TxSource_pixelsPerLine,
                                       Deinterlacer_get,
                                       Deinterlacer_done,
                                       psub);
    if (status == SANE_STATUS_GOOD)
    {
        pself->ch_shift_even = SANE_TRUE;
        switch (pss->pdev->model)
        {
        case PERFECTION3490:
            pself->ch_offset = 8;
            if ((actual_mode(pss) == MD_GREYSCALE) || (actual_mode(pss) == MD_LINEART))
                 pself->ch_shift_even = SANE_FALSE;
            break;
        case PERFECTION2480:
        default:
            pself->ch_offset = 4;
            break;
        }
        pself->ch_line_size = TxSource_bytesPerLine((Source *) pself);
        /* We need at least ch_offset+1 lines of buffer in order
           to shift up ch_offset pixels. */
        pself->ch_size = pself->ch_line_size * (pself->ch_offset + 1);
        pself->ch_buf = (SANE_Byte *) malloc(pself->ch_size);
        if (pself->ch_buf == NULL)
        {
            DBG (DL_MAJOR_ERROR,
                 "%s: couldn't allocate channel buffer.\n",
                 __func__);
            status = SANE_STATUS_NO_MEM;
        }
        else
        {
            pself->ch_ndata = 0;
            pself->ch_pos = 0;
            pself->ch_past_init = SANE_FALSE;
	    if ((actual_mode(pss) == MD_GREYSCALE) || (actual_mode(pss) == MD_LINEART))
	    	pself->ch_bytes_per_pixel = 1;
	    else
	    	pself->ch_bytes_per_pixel = 3;
	    if (pss->bpp_scan == 16)
		pself->ch_bytes_per_pixel *= 2;
        }
        pself->ch_lineart = (actual_mode(pss) == MD_LINEART);
    }
    return status;
}

static SANE_Status create_Deinterlacer (SnapScan_Scanner *pss,
                                    Source *psub,
                                    Source **pps)
{
    SANE_Status status = SANE_STATUS_GOOD;
    *pps = (Source *) malloc(sizeof(Deinterlacer));
    if (*pps == NULL)
    {
        DBG (DL_MAJOR_ERROR,
             "%s: failed to allocate Deinterlacer.\n",
             __func__);
        status = SANE_STATUS_NO_MEM;
    }
    else
    {
        status = Deinterlacer_init ((Deinterlacer *) *pps, pss, psub);
    }
    return status;
}

/* ----------------------------------------------------- */

/* the RGB router assumes 8-bit RGB data arranged in contiguous
   channels, possibly with R-G and R-B offsets, and rearranges the
   data into SANE RGB frame format */

typedef struct
{
    TX_SOURCE_GUTS;
    SANE_Byte *cbuf;            /* circular line buffer */
    SANE_Byte *xbuf;            /* single line buffer */
    SANE_Int pos;                    /* current position in xbuf */
    SANE_Int cb_size;            /* size of the circular buffer */
    SANE_Int cb_line_size;/* size of a line in the circular buffer */
    SANE_Int cb_start;        /* start of valid data in the circular buffer */
    SANE_Int cb_finish;        /* finish of valid data, for next read */
    SANE_Int ch_offset[3];/* offset in cbuf */
    SANE_Int round_req;
    SANE_Int round_read;
} RGBRouter;

static void put_int16r (int n, u_char *p)
{
	p[0] = (n & 0x00ff);
	p[1] = (n & 0xff00) >> 8;
}


static SANE_Int RGBRouter_remaining (Source *pself)
{
    RGBRouter *ps = (RGBRouter *) pself;
   SANE_Int remaining;
    if (ps->round_req == ps->cb_size)
        remaining = TxSource_remaining(pself) - ps->cb_size + ps->cb_line_size;
    else
      remaining = TxSource_remaining(pself) + ps->cb_line_size - ps->pos;
   return (remaining);
}

static SANE_Status RGBRouter_get (Source *pself,
                                  SANE_Byte *pbuf,
                                  SANE_Int *plen)
{
    RGBRouter *ps = (RGBRouter *) pself;
    SANE_Status status = SANE_STATUS_GOOD;
    SANE_Int remaining = *plen;
    SANE_Byte *s;
    SANE_Int i, t;
    SANE_Int r, g, b;
    SANE_Int run_req;
    SANE_Int org_len = *plen;
    char *me = "RGBRouter_get";

    while (remaining > 0  &&  pself->remaining(pself) > 0 && !cancelRead)
    {
        DBG(DL_DATA_TRACE, "%s: remaining=%d, pself->remaining=%d, round_req=%d, cb_size=%d\n",
            me, remaining, pself->remaining(pself), ps->round_req, ps->cb_size);
        /* Check if there is no valid data left from previous get */
        if (ps->pos >= ps->cb_line_size)
        {
            /* Try to get more data. either one line or
               full buffer (first time) */
            do
            {
                run_req = ps->round_req - ps->round_read;
                status = TxSource_get (pself,
                                       ps->cbuf + ps->cb_start + ps->round_read,
                                       &run_req);
                if (status != SANE_STATUS_GOOD || run_req==0)
                {
                    *plen -= remaining;
                    if ( *plen > 0 )
                        DBG(DL_DATA_TRACE, "%s: request=%d, read=%d\n",
                            me, org_len, *plen);
                    return status;
                }
                ps->round_read += run_req;
            }
            while ((ps->round_req > ps->round_read) && !cancelRead);

            /* route RGB */
            ps->cb_start = (ps->cb_start + ps->round_read)%ps->cb_size;
            s = ps->xbuf;
            r = (ps->cb_start + ps->ch_offset[0])%ps->cb_size;
            g = (ps->cb_start + ps->ch_offset[1])%ps->cb_size;
            b = (ps->cb_start + ps->ch_offset[2])%ps->cb_size;
            for (i = 0;  i < ps->cb_line_size/3;  i++)
            {
                if (pself->pss->bpp_scan == 8)
                {
                    *s++ = ps->cbuf[r++];
                    *s++ = ps->cbuf[g++];
                    *s++ = ps->cbuf[b++];
                }
                else if (pself->pss->pdev->model == SCANWIT2720S)
                {
                    t = (((ps->cbuf[r+1] << 8) | ps->cbuf[r]) & 0xfff) << 4;
                    put_int16r (t, s);
                    s += 2;
                    r += 2;
                    t = (((ps->cbuf[g+1] << 8) | ps->cbuf[g]) & 0xfff) << 4;
                    put_int16r (t, s);
                    s += 2;
                    g += 2;
                    t = (((ps->cbuf[b+1] << 8) | ps->cbuf[b]) & 0xfff) << 4;
                    put_int16r (t, s);
                    s += 2;
                    b += 2;
                    i++;
                }
                else
                {
                    *s++ = ps->cbuf[r++];
                    *s++ = ps->cbuf[r++];
                    *s++ = ps->cbuf[g++];
                    *s++ = ps->cbuf[g++];
                    *s++ = ps->cbuf[b++];
                    *s++ = ps->cbuf[b++];
                    i++;
                }
            }

            /* end of reading & offsetiing whole line data;
               reset valid position */
            ps->pos = 0;

            /* prepare for next round */
            ps->round_req = ps->cb_line_size;
            ps->round_read =0;
        }

        /* Repack the whole scan line and copy to caller's buffer */
        while (remaining > 0  &&  ps->pos < ps->cb_line_size)
        {
            *pbuf++ = ps->xbuf[ps->pos++];
            remaining--;
        }
    }
    *plen -= remaining;
    DBG(DL_DATA_TRACE,
        "%s: Request=%d, remaining=%d, read=%d, TXSource_rem=%d, bytes_rem=%lu\n",
        me,
        org_len,
        pself->remaining(pself),
        *plen,
        TxSource_remaining(pself),
        (u_long) ps->pss->bytes_remaining);
    return status;
}

static SANE_Status RGBRouter_done (Source *pself)
{
    RGBRouter *ps = (RGBRouter *) pself;
    SANE_Status status = TxSource_done(pself);

    free(ps->cbuf);
    free(ps->xbuf);
    ps->cbuf = NULL;
    ps->cb_start = -1;
    ps->pos = 0;
    return status;
}

static SANE_Status RGBRouter_init (RGBRouter *pself,
                           SnapScan_Scanner *pss,
                                   Source *psub)
{
    SANE_Status status = TxSource_init((TxSource *) pself,
                                       pss,
                                       RGBRouter_remaining,
                                       TxSource_bytesPerLine,
                                       TxSource_pixelsPerLine,
                                       RGBRouter_get,
                                       RGBRouter_done,
                                       psub);
    if (status == SANE_STATUS_GOOD)
    {
        SANE_Int lines_in_buffer = 0;

        /* Size the buffer to accommodate the necessary number of scan
           lines to cater for the offset between R, G and B */
        lines_in_buffer = pss->chroma + 1;
        pself->cb_line_size = pself->bytesPerLine((Source *) pself);
        pself->cb_size = pself->cb_line_size*lines_in_buffer;
        pself->pos = pself->cb_line_size;

        pself->round_req = pself->cb_size;
        pself->round_read = 0;

        pself->cbuf = (SANE_Byte *) malloc(pself->cb_size);
        pself->xbuf = (SANE_Byte *) malloc(pself->cb_line_size);
        if (pself->cbuf == NULL  ||  pself->xbuf == NULL)
        {
            DBG (DL_MAJOR_ERROR,
                 "%s: failed to allocate circular buffer.\n",
                 __func__);
            status = SANE_STATUS_NO_MEM;
        }
        else
        {
            SANE_Int ch;

            pself->cb_start = 0;
            for (ch = 0;  ch < 3;  ch++)
            {
                pself->ch_offset[ch] =
                        pss->chroma_offset[ch] * pself->cb_line_size
                        + ch * (pself->cb_line_size / 3);
            }
        }
        DBG(DL_MINOR_INFO, "RGBRouter_init: buf_size: %d x %d = %d\n",
            pself->cb_line_size, lines_in_buffer, pself->cb_size);
        DBG(DL_MINOR_INFO, "RGBRouter_init: buf offset R:%d G:%d B:%d\n",
            pself->ch_offset[0], pself->ch_offset[1],pself->ch_offset[2]);
    }
    return status;
}

static SANE_Status create_RGBRouter (SnapScan_Scanner *pss,
                                     Source *psub,
                                     Source **pps)
{
    static char me[] = "create_RGBRouter";
    SANE_Status status = SANE_STATUS_GOOD;

    DBG (DL_CALL_TRACE, "%s\n", me);
    *pps = (Source *) malloc(sizeof(RGBRouter));
    if (*pps == NULL)
    {
        DBG (DL_MAJOR_ERROR, "%s: failed to allocate RGBRouter.\n",
             __func__);
        status = SANE_STATUS_NO_MEM;
    }
    else
    {
        status = RGBRouter_init ((RGBRouter *) *pps, pss, psub);
    }
    return status;
}

/* An Inverter is used to invert the bits in a lineart image */

typedef struct
{
    TX_SOURCE_GUTS;
} Inverter;

static SANE_Status Inverter_get (Source *pself, SANE_Byte *pbuf, SANE_Int *plen)
{
    SANE_Status status = TxSource_get (pself, pbuf, plen);
    if (status == SANE_STATUS_GOOD)
    {
        int i;
        for (i = 0;  i < *plen;  i++)
            pbuf[i] ^= 0xFF;
    }
    return status;
}

static SANE_Status Inverter_init (Inverter *pself,
                                  SnapScan_Scanner *pss,
                                  Source *psub)
{
    return  TxSource_init ((TxSource *) pself,
                           pss,
                           TxSource_remaining,
                           TxSource_bytesPerLine,
                           TxSource_pixelsPerLine,
                           Inverter_get,
                           TxSource_done,
                           psub);
}

static SANE_Status create_Inverter (SnapScan_Scanner *pss,
                                    Source *psub,
                                    Source **pps)
{
    SANE_Status status = SANE_STATUS_GOOD;
    *pps = (Source *) malloc(sizeof(Inverter));
    if (*pps == NULL)
    {
        DBG (DL_MAJOR_ERROR, "%s: failed to allocate Inverter.\n",
                 __func__);
        status = SANE_STATUS_NO_MEM;
    }
    else
    {
        status = Inverter_init ((Inverter *) *pps, pss, psub);
    }
    return status;
}

/* Source chain creation */

static SANE_Status create_source_chain (SnapScan_Scanner *pss,
                                        BaseSourceType bst,
                                        Source **pps)
{
   static char me[] = "create_source_chain";
    SANE_Status status = create_base_source (pss, bst, pps);

   DBG (DL_CALL_TRACE, "%s\n", me);
    if (status == SANE_STATUS_GOOD)
    {
        SnapScan_Mode mode = actual_mode(pss);
        switch (mode)
        {
        case MD_COLOUR:
            status = create_RGBRouter (pss, *pps, pps);
            /* We only have the interlace problems on
               some scanners like the Epson Perfection 2480/2580
               at 2400 dpi. */
            if (status == SANE_STATUS_GOOD &&
                ((pss->pdev->model == PERFECTION2480 && pss->res == 2400) ||
                (pss->pdev->model == PERFECTION3490 && pss->res == 3200) ||
                (pss->pdev->model == PRISA5000E && pss->res == 1200)))
                status = create_Deinterlacer (pss, *pps, pps);
            break;
        case MD_BILEVELCOLOUR:
            status = create_Expander (pss, *pps, pps);
            if (status == SANE_STATUS_GOOD)
                status = create_RGBRouter (pss, *pps, pps);
            if (status == SANE_STATUS_GOOD &&
                ((pss->pdev->model == PERFECTION2480 && pss->res == 2400) ||
                (pss->pdev->model == PERFECTION3490 && pss->res == 3200) ||
                (pss->pdev->model == PRISA5000E && pss->res == 1200)))
                status = create_Deinterlacer (pss, *pps, pps);
            break;
        case MD_GREYSCALE:
            if ((pss->pdev->model == PERFECTION2480 && pss->res == 2400) ||
                (pss->pdev->model == PERFECTION3490 && pss->res == 3200) ||
                (pss->pdev->model == PRISA5000E && pss->res == 1200))
                status = create_Deinterlacer (pss, *pps, pps);
            break;
        case MD_LINEART:
            /* The SnapScan creates a negative image by
               default... so for the user interface to make sense,
               the internal meaning of "negative" is reversed */
            if (pss->negative == SANE_FALSE)
                status = create_Inverter (pss, *pps, pps);
            if (pss->pdev->model == PERFECTION3490 && pss->res == 3200)
                status = create_Deinterlacer (pss, *pps, pps);
            break;
        default:
            DBG (DL_MAJOR_ERROR, "%s: bad mode value %d (internal error)\n",
                 __func__, mode);
            status = SANE_STATUS_INVAL;
            break;
        }
    }
    return status;
}

/*
 * Revision 1.21  2005/12/02 19:12:54  oliver-guest
 * Another fix for lineart mode for the Epson 3490 @ 3200 DPI
 *
 * Revision 1.20  2005/11/28 19:28:29  oliver-guest
 * Fix for lineart mode of Epson 3490 @ 3200 DPI
 *
 * Revision 1.19  2005/11/25 17:24:48  oliver-guest
 * Fix for Epson 3490 @ 3200 DPI for grayscale and lineart mode
 *
 * Revision 1.18  2005/11/17 23:47:11  oliver-guest
 * Revert previous 'fix', disable 2400 dpi for Epson 3490, use 1600 dpi instead
 *
 * Revision 1.17  2005/11/17 23:32:23  oliver-guest
 * Fixes for Epson 3490 @ 2400 DPI
 *
 * Revision 1.16  2005/11/10 19:42:02  oliver-guest
 * Added deinterlacing for Epson 3490
 *
 * Revision 1.15  2005/10/31 21:08:47  oliver-guest
 * Distinguish between Benq 5000/5000E/5000U
 *
 * Revision 1.14  2005/10/13 22:43:30  oliver-guest
 * Fixes for 16 bit scan mode from Simon Munton
 *
 * Revision 1.13  2005/10/11 18:47:07  oliver-guest
 * Fixes for Epson 3490 and 16 bit scan mode
 *
 * Revision 1.12  2004/11/14 19:26:38  oliver-guest
 * Applied patch from Julien Blache to change ch_past_init from SANE_Int to SANE_Bool
 *
 * Revision 1.11  2004/11/09 23:17:38  oliver-guest
 * First implementation of deinterlacer for Epson scanners at high resolutions (thanks to Brad Johnson)
 *
 * Revision 1.10  2004/10/03 17:34:36  hmg-guest
 * 64 bit platform fixes (bug #300799).
 *
 * Revision 1.9  2004/04/09 16:18:37  oliver-guest
 * Fix initialization of FDSource.bytes_remaining
 *
 * Revision 1.8  2004/04/09 11:59:02  oliver-guest
 * Fixes for pthread implementation
 *
 * Revision 1.7  2004/04/08 21:53:10  oliver-guest
 * Use sanei_thread in snapscan backend
 *
 * Revision 1.6  2001/12/17 22:51:49  oliverschwartz
 * Update to snapscan-20011212 (snapscan 1.4.3)
 *
 * Revision 1.18  2001/12/12 19:44:59  oliverschwartz
 * Clean up CVS log
 *
 * Revision 1.17  2001/11/27 23:16:17  oliverschwartz
 * - Fix color alignment for SnapScan 600
 * - Added documentation in snapscan-sources.c
 * - Guard against TL_X < BR_X and TL_Y < BR_Y
 *
 * Revision 1.16  2001/10/08 18:22:02  oliverschwartz
 * - Disable quality calibration for Acer Vuego 310F
 * - Use sanei_scsi_max_request_size as scanner buffer size
 *   for SCSI devices
 * - Added new devices to snapscan.desc
 *
 * Revision 1.15  2001/09/28 15:56:51  oliverschwartz
 * - fix hanging for SNAPSCAN300 / VUEGO 310
 *
 * Revision 1.14  2001/09/28 13:39:16  oliverschwartz
 * - Added "Snapscan 300" ID string
 * - cleanup
 * - more debugging messages in snapscan-sources.c
 *
 * Revision 1.13  2001/09/18 15:01:07  oliverschwartz
 * - Read scanner id string again after firmware upload
 *   to identify correct model
 * - Make firmware upload work for AGFA scanners
 * - Change copyright notice
 *
 * Revision 1.12  2001/09/09 18:06:32  oliverschwartz
 * add changes from Acer (new models; automatic firmware upload for USB scanners); fix distorted colour scans after greyscale scans (call set_window only in sane_start); code cleanup
 *
 * Revision 1.11  2001/04/13 13:12:18  oliverschwartz
 * use absolute_max as expected_read_bytes for PRISA620S
 *
 * Revision 1.10  2001/04/10 11:04:31  sable
 * Adding support for snapscan e40 an e50 thanks to Giuseppe Tanzilli
 *
 * Revision 1.9  2001/03/17 22:53:21  sable
 * Applying Mikael Magnusson patch concerning Gamma correction
 * Support for 1212U_2
 *
 * Revision 1.8  2000/11/28 03:55:07  cbagwell
 * Reverting a fix to RGBRouter_remaining to original fix.  This allows
 * most scanners to scan at 600 dpi by ignoring insufficient data in
 * the RGB circular buffer and always returning size = 1 in those cases.
 * This should probably be fixed at a higher level.
 *
 * Revision 1.7  2000/11/20 01:02:42  cbagwell
 * Updates so that USB will continue reading when it receives an EAGAIN error.
 * Also, changed RGBRouter_remaining to not be able to return a negative
 * value.
 *
 * Revision 1.6  2000/11/04 01:53:58  cbagwell
 * Committing some needed USB updates.  Added extra test logic to detect
 * bad bytes_expected values.  Just to help debug faster on scanners
 * that tickle the bug.
 *
 * Revision 1.5  2000/10/30 22:32:20  sable
 * Support for vuego310s vuego610s and 1236s
 *
 * Revision 1.4  2000/10/28 14:16:10  sable
 * Bug correction for SnapScan310
 *
 * Revision 1.3  2000/10/28 14:06:35  sable
 * Add support for Acer300f
 *
 * Revision 1.2  2000/10/13 03:50:27  cbagwell
 * Updating to source from SANE 1.0.3.  Calling this version 1.1
 * */