forked from python-pillow/Pillow
/
_sane.c
1325 lines (1204 loc) · 38 KB
/
_sane.c
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
/***********************************************************
(C) Copyright 2003 A.M. Kuchling. All Rights Reserved
(C) Copyright 2004 A.M. Kuchling, Ralph Heinkel All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of A.M. Kuchling and
Ralph Heinkel not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.
A.M. KUCHLING, R.H. HEINKEL DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
******************************************************************/
/* SaneDev objects */
#include "Python.h"
#include "Imaging.h"
#include <sane/sane.h>
#include <sys/time.h>
static PyObject *ErrorObject;
typedef struct {
PyObject_HEAD
SANE_Handle h;
} SaneDevObject;
#ifdef WITH_THREAD
PyThreadState *_save;
#endif
/* Raise a SANE exception */
PyObject *
PySane_Error(SANE_Status st)
{
const char *string;
if (st==SANE_STATUS_GOOD) {Py_INCREF(Py_None); return (Py_None);}
string=sane_strstatus(st);
PyErr_SetString(ErrorObject, string);
return NULL;
}
staticforward PyTypeObject SaneDev_Type;
#define SaneDevObject_Check(v) ((v)->ob_type == &SaneDev_Type)
static SaneDevObject *
newSaneDevObject(void)
{
SaneDevObject *self;
self = PyObject_NEW(SaneDevObject, &SaneDev_Type);
if (self == NULL)
return NULL;
self->h=NULL;
return self;
}
/* SaneDev methods */
static void
SaneDev_dealloc(SaneDevObject *self)
{
if (self->h) sane_close(self->h);
self->h=NULL;
PyObject_DEL(self);
}
static PyObject *
SaneDev_close(SaneDevObject *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ""))
return NULL;
if (self->h) sane_close(self->h);
self->h=NULL;
Py_INCREF(Py_None);
return (Py_None);
}
static PyObject *
SaneDev_get_parameters(SaneDevObject *self, PyObject *args)
{
SANE_Status st;
SANE_Parameters p;
char *format="unknown format";
if (!PyArg_ParseTuple(args, ""))
return NULL;
if (self->h==NULL)
{
PyErr_SetString(ErrorObject, "SaneDev object is closed");
return NULL;
}
Py_BEGIN_ALLOW_THREADS
st=sane_get_parameters(self->h, &p);
Py_END_ALLOW_THREADS
if (st) return PySane_Error(st);
switch (p.format)
{
case(SANE_FRAME_GRAY): format="gray"; break;
case(SANE_FRAME_RGB): format="color"; break;
case(SANE_FRAME_RED): format="red"; break;
case(SANE_FRAME_GREEN): format="green"; break;
case(SANE_FRAME_BLUE): format="blue"; break;
}
return Py_BuildValue("si(ii)ii", format, p.last_frame, p.pixels_per_line,
p.lines, p.depth, p.bytes_per_line);
}
static PyObject *
SaneDev_fileno(SaneDevObject *self, PyObject *args)
{
SANE_Status st;
SANE_Int fd;
if (!PyArg_ParseTuple(args, ""))
return NULL;
if (self->h==NULL)
{
PyErr_SetString(ErrorObject, "SaneDev object is closed");
return NULL;
}
st=sane_get_select_fd(self->h, &fd);
if (st) return PySane_Error(st);
return PyInt_FromLong(fd);
}
static PyObject *
SaneDev_start(SaneDevObject *self, PyObject *args)
{
SANE_Status st;
if (!PyArg_ParseTuple(args, ""))
return NULL;
if (self->h==NULL)
{
PyErr_SetString(ErrorObject, "SaneDev object is closed");
return NULL;
}
/* sane_start can take several seconds, if the user initiates
a new scan, while the scan head of a flatbed scanner moves
back to the start position after finishing a previous scan.
Hence it is worth to allow threads here.
*/
Py_BEGIN_ALLOW_THREADS
st=sane_start(self->h);
Py_END_ALLOW_THREADS
if (st) return PySane_Error(st);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
SaneDev_cancel(SaneDevObject *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ""))
return NULL;
if (self->h==NULL)
{
PyErr_SetString(ErrorObject, "SaneDev object is closed");
return NULL;
}
sane_cancel(self->h);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
SaneDev_get_options(SaneDevObject *self, PyObject *args)
{
const SANE_Option_Descriptor *d;
PyObject *list, *value;
int i=1;
if (!PyArg_ParseTuple(args, ""))
return NULL;
if (self->h==NULL)
{
PyErr_SetString(ErrorObject, "SaneDev object is closed");
return NULL;
}
if (!(list = PyList_New(0)))
return NULL;
do
{
d=sane_get_option_descriptor(self->h, i);
if (d!=NULL)
{
PyObject *constraint=NULL;
int j;
switch (d->constraint_type)
{
case(SANE_CONSTRAINT_NONE):
Py_INCREF(Py_None); constraint=Py_None; break;
case(SANE_CONSTRAINT_RANGE):
if (d->type == SANE_TYPE_INT)
constraint=Py_BuildValue("iii", d->constraint.range->min,
d->constraint.range->max,
d->constraint.range->quant);
else
constraint=Py_BuildValue("ddd",
SANE_UNFIX(d->constraint.range->min),
SANE_UNFIX(d->constraint.range->max),
SANE_UNFIX(d->constraint.range->quant));
break;
case(SANE_CONSTRAINT_WORD_LIST):
constraint=PyList_New(d->constraint.word_list[0]);
if (d->type == SANE_TYPE_INT)
for (j=1; j<=d->constraint.word_list[0]; j++)
PyList_SetItem(constraint, j-1,
PyInt_FromLong(d->constraint.word_list[j]));
else
for (j=1; j<=d->constraint.word_list[0]; j++)
PyList_SetItem(constraint, j-1,
PyFloat_FromDouble(SANE_UNFIX(d->constraint.word_list[j])));
break;
case(SANE_CONSTRAINT_STRING_LIST):
constraint=PyList_New(0);
for(j=0; d->constraint.string_list[j]!=NULL; j++)
PyList_Append(constraint,
PyString_FromString(d->constraint.string_list[j]));
break;
}
value=Py_BuildValue("isssiiiiO", i, d->name, d->title, d->desc,
d->type, d->unit, d->size, d->cap, constraint);
PyList_Append(list, value);
}
i++;
} while (d!=NULL);
return list;
}
static PyObject *
SaneDev_get_option(SaneDevObject *self, PyObject *args)
{
SANE_Status st;
const SANE_Option_Descriptor *d;
PyObject *value=NULL;
int n;
void *v;
if (!PyArg_ParseTuple(args, "i", &n))
{
return NULL;
}
if (self->h==NULL)
{
PyErr_SetString(ErrorObject, "SaneDev object is closed");
return NULL;
}
d=sane_get_option_descriptor(self->h, n);
v=malloc(d->size+1);
st=sane_control_option(self->h, n, SANE_ACTION_GET_VALUE,
v, NULL);
if (st)
{
free(v);
return PySane_Error(st);
}
switch(d->type)
{
case(SANE_TYPE_BOOL):
case(SANE_TYPE_INT):
value=Py_BuildValue("i", *( (SANE_Int*)v) );
break;
case(SANE_TYPE_FIXED):
value=Py_BuildValue("d", SANE_UNFIX((*((SANE_Fixed*)v))) );
break;
case(SANE_TYPE_STRING):
value=Py_BuildValue("s", v);
break;
case(SANE_TYPE_BUTTON):
case(SANE_TYPE_GROUP):
value=Py_BuildValue("O", Py_None);
break;
}
free(v);
return value;
}
static PyObject *
SaneDev_set_option(SaneDevObject *self, PyObject *args)
{
SANE_Status st;
const SANE_Option_Descriptor *d;
SANE_Int i;
PyObject *value;
int n;
void *v;
if (!PyArg_ParseTuple(args, "iO", &n, &value))
return NULL;
if (self->h==NULL)
{
PyErr_SetString(ErrorObject, "SaneDev object is closed");
return NULL;
}
d=sane_get_option_descriptor(self->h, n);
v=malloc(d->size+1);
switch(d->type)
{
case(SANE_TYPE_BOOL):
if (!PyInt_Check(value))
{
PyErr_SetString(PyExc_TypeError, "SANE_BOOL requires an integer");
free(v);
return NULL;
}
/* fall through */
case(SANE_TYPE_INT):
if (!PyInt_Check(value))
{
PyErr_SetString(PyExc_TypeError, "SANE_INT requires an integer");
free(v);
return NULL;
}
*( (SANE_Int*)v) = PyInt_AsLong(value);
break;
case(SANE_TYPE_FIXED):
if (!PyFloat_Check(value))
{
PyErr_SetString(PyExc_TypeError, "SANE_FIXED requires a floating point number");
free(v);
return NULL;
}
*( (SANE_Fixed*)v) = SANE_FIX(PyFloat_AsDouble(value));
break;
case(SANE_TYPE_STRING):
if (!PyString_Check(value))
{
PyErr_SetString(PyExc_TypeError, "SANE_STRING requires a string");
free(v);
return NULL;
}
strncpy(v, PyString_AsString(value), d->size-1);
((char*)v)[d->size-1] = 0;
break;
case(SANE_TYPE_BUTTON):
case(SANE_TYPE_GROUP):
break;
}
st=sane_control_option(self->h, n, SANE_ACTION_SET_VALUE,
v, &i);
if (st) {free(v); return PySane_Error(st);}
free(v);
return Py_BuildValue("i", i);
}
static PyObject *
SaneDev_set_auto_option(SaneDevObject *self, PyObject *args)
{
SANE_Status st;
const SANE_Option_Descriptor *d;
SANE_Int i;
int n;
if (!PyArg_ParseTuple(args, "i", &n))
return NULL;
if (self->h==NULL)
{
PyErr_SetString(ErrorObject, "SaneDev object is closed");
return NULL;
}
d=sane_get_option_descriptor(self->h, n);
st=sane_control_option(self->h, n, SANE_ACTION_SET_AUTO,
NULL, &i);
if (st) {return PySane_Error(st);}
return Py_BuildValue("i", i);
}
#define READSIZE 32768
static PyObject *
SaneDev_snap(SaneDevObject *self, PyObject *args)
{
SANE_Status st;
/* The buffer should be a multiple of 3 in size, so each sane_read
operation will return an integral number of RGB triples. */
SANE_Byte buffer[READSIZE]; /* XXX how big should the buffer be? */
SANE_Int len, lastlen;
Imaging im;
SANE_Parameters p;
int px, py, remain, cplen, bufpos, padbytes;
long L;
char errmsg[80];
union
{ char c[2];
INT16 i16;
}
endian;
PyObject *pyNoCancel = NULL;
int noCancel = 0;
endian.i16 = 1;
if (!PyArg_ParseTuple(args, "l|O", &L, &pyNoCancel))
return NULL;
if (self->h==NULL)
{
PyErr_SetString(ErrorObject, "SaneDev object is closed");
return NULL;
}
im=(Imaging)L;
if (pyNoCancel)
noCancel = PyObject_IsTrue(pyNoCancel);
st=SANE_STATUS_GOOD; px=py=0;
/* xxx not yet implemented
- handscanner support (i.e., unknown image length during start)
- generally: move the functionality from method snap in sane.py
down here -- I don't like this cross-dependency.
we need to call sane_get_parameters here, and we can create
the result Image object here.
*/
Py_UNBLOCK_THREADS
sane_get_parameters(self->h, &p);
if (p.format == SANE_FRAME_GRAY)
{
switch (p.depth)
{
case 1:
remain = p.bytes_per_line * im->ysize;
padbytes = p.bytes_per_line - (im->xsize+7)/8;
bufpos = 0;
lastlen = len = 0;
while (st!=SANE_STATUS_EOF && py < im->ysize)
{
while (len > 0 && py < im->ysize)
{
int i, j, k;
j = buffer[bufpos++];
k = 0x80;
for (i = 0; i < 8 && px < im->xsize; i++)
{
im->image8[py][px++] = (k&j) ? 0 : 0xFF;
k = k >> 1;
}
len--;
if (px >= im->xsize)
{
bufpos += padbytes;
len -= padbytes;
py++;
px = 0;
}
}
st=sane_read(self->h, buffer,
remain<READSIZE ? remain : READSIZE, &len);
if (st && (st!=SANE_STATUS_EOF))
{
sane_cancel(self->h);
Py_BLOCK_THREADS
return PySane_Error(st);
}
bufpos -= lastlen;
lastlen = len;
remain -= len;
/* skip possible pad bytes at the start of the buffer */
len -= bufpos;
}
break;
case 8:
remain = p.bytes_per_line * im->ysize;
padbytes = p.bytes_per_line - im->xsize;
bufpos = 0;
len = 0;
while (st!=SANE_STATUS_EOF && py < im->ysize)
{
while (len > 0 && py < im->ysize)
{
cplen = len;
if (px + cplen >= im->xsize)
cplen = im->xsize - px;
memcpy(&im->image8[py][px], &buffer[bufpos], cplen);
len -= cplen;
bufpos += cplen;
px += cplen;
if (px >= im->xsize)
{
px = 0;
py++;
bufpos += padbytes;
len -= padbytes;
}
}
bufpos = -len;
st=sane_read(self->h, buffer,
remain<READSIZE ? remain : READSIZE, &len);
if (st && (st!=SANE_STATUS_EOF))
{
sane_cancel(self->h);
Py_BLOCK_THREADS
return PySane_Error(st);
}
remain -= len;
len -= bufpos;
}
break;
case 16:
remain = p.bytes_per_line * im->ysize;
padbytes = p.bytes_per_line - 2 * im->xsize;
bufpos = endian.c[0];
lastlen = len = 0;
while (st!=SANE_STATUS_EOF && py < im->ysize)
{
while (len > 0 && py < im->ysize)
{
im->image8[py][px++] = buffer[bufpos];
bufpos += 2;
len -= 2;
if (px >= im->xsize)
{
bufpos += padbytes;
len -= padbytes;
py++;
px = 0;
}
}
st=sane_read(self->h, buffer,
remain<READSIZE ? remain : READSIZE, &len);
if (st && (st!=SANE_STATUS_EOF))
{
sane_cancel(self->h);
Py_BLOCK_THREADS
return PySane_Error(st);
}
remain -= len;
bufpos -= lastlen;
lastlen = len;
len -= bufpos;
}
break;
default:
/* other depths are not formally "illegal" according to the
Sane API, but it's agreed by Sane developers that other
depths than 1, 8, 16 should not be used
*/
sane_cancel(self->h);
Py_BLOCK_THREADS
snprintf(errmsg, 80, "unsupported pixel depth: %i", p.depth);
PyErr_SetString(ErrorObject, errmsg);
return NULL;
}
}
else if (p.format == SANE_FRAME_RGB)
{
int incr, color, pxs, pxmax, bit, val, mask;
switch (p.depth)
{
case 1:
remain = p.bytes_per_line * im->ysize;
padbytes = p.bytes_per_line - ((im->xsize+7)/8) * 3;
bufpos = 0;
len = 0;
lastlen = 0;
pxmax = 4 * im->xsize;
while (st!=SANE_STATUS_EOF && py < im->ysize)
{
pxs = px;
for (color = 0; color < 3; color++)
{
while (len <= 0 && st == SANE_STATUS_GOOD)
{
st=sane_read(self->h, buffer,
remain<READSIZE ? remain : READSIZE, &len);
if (st && (st!=SANE_STATUS_EOF))
{
sane_cancel(self->h);
Py_BLOCK_THREADS
return PySane_Error(st);
}
bufpos -= lastlen;
remain -= len;
lastlen = len;
/* skip possible pad bytes at the start of the buffer */
len -= bufpos;
}
if (st == SANE_STATUS_EOF) break;
pxs = px;
val = buffer[bufpos++];
len--;
mask = 0x80;
for (bit = 0; (bit < 8) && (px < pxmax); bit++)
{
((UINT8**)(im->image32))[py][px] = (val&mask) ? 0xFF : 0;
mask = mask >> 1;
px += 4;
}
pxs++;
px = pxs;
}
if (st == SANE_STATUS_EOF)
break;
for (bit = 0; bit < 8 && px < pxmax; bit++)
{
((UINT8**)(im->image32))[py][px] = 0;
px += 4;
}
px -= 3;
if (px >= pxmax)
{
bufpos += padbytes;
len -= padbytes;
py++;
px = 0;
}
}
break;
case 8:
case 16:
if (p.depth == 8)
{
padbytes = p.bytes_per_line - 3 * im->xsize;
bufpos = 0;
incr = 1;
}
else
{
padbytes = p.bytes_per_line - 6 * im->xsize;
bufpos = endian.c[0];
incr = 2;
}
remain = p.bytes_per_line * im->ysize;
len = 0;
lastlen = 0;
pxmax = 4 * im->xsize;
/* probably not very efficient. But we have to deal with these
possible conditions:
- we may have padding bytes at the end of a scan line
- the number of bytes read with sane_read may be smaller
than the number of pad bytes
- the buffer may become empty after setting any of the
red/green/blue pixel values
*/
while (st != SANE_STATUS_EOF && py < im->ysize)
{
for (color = 0; color < 3; color++)
{
while (len <= 0 && st == SANE_STATUS_GOOD)
{
bufpos -= lastlen;
if (remain == 0)
{
sane_cancel(self->h);
Py_BLOCK_THREADS
PyErr_SetString(ErrorObject, "internal _sane error: premature end of scan");
return NULL;
}
st = sane_read(self->h, buffer,
remain<(READSIZE) ? remain : (READSIZE), &len);
if (st && (st!=SANE_STATUS_EOF))
{
sane_cancel(self->h);
Py_BLOCK_THREADS
return PySane_Error(st);
}
lastlen = len;
remain -= len;
len -= bufpos;
}
if (st == SANE_STATUS_EOF) break;
((UINT8**)(im->image32))[py][px++] = buffer[bufpos];
bufpos += incr;
len -= incr;
}
if (st == SANE_STATUS_EOF) break;
((UINT8**)(im->image32))[py][px++] = 0;
if (px >= pxmax)
{
px = 0;
py++;
bufpos += padbytes;
len -= padbytes;
}
}
break;
default:
Py_BLOCK_THREADS
sane_cancel(self->h);
snprintf(errmsg, 80, "unsupported pixel depth: %i", p.depth);
PyErr_SetString(ErrorObject, errmsg);
return NULL;
}
}
else /* should be SANE_FRAME_RED, GREEN or BLUE */
{
int lastlen, pxa, pxmax, offset, incr, frame_count = 0;
/* at least the Sane test backend behaves a bit weird, if
it returns "premature EOF" for sane_read, i.e., if the
option "return value of sane_read" is set to SANE_STATUS_EOF.
In this case, the test backend does not advance to the next frame,
so p.last_frame will never be set...
So let's count the number of frames we try to acquire
*/
while (!p.last_frame && frame_count < 4)
{
frame_count++;
st = sane_get_parameters(self->h, &p);
if (st)
{
sane_cancel(self->h);
Py_BLOCK_THREADS
return PySane_Error(st);
}
remain = p.bytes_per_line * im->ysize;
bufpos = 0;
len = 0;
lastlen = 0;
py = 0;
switch (p.format)
{
case SANE_FRAME_RED:
offset = 0;
break;
case SANE_FRAME_GREEN:
offset = 1;
break;
case SANE_FRAME_BLUE:
offset = 2;
break;
default:
sane_cancel(self->h);
Py_BLOCK_THREADS
snprintf(errmsg, 80, "unknown/invalid frame format: %i", p.format);
PyErr_SetString(ErrorObject, errmsg);
return NULL;
}
px = offset;
pxa = 3;
pxmax = im->xsize * 4;
switch (p.depth)
{
case 1:
padbytes = p.bytes_per_line - (im->xsize+7)/8;
st = SANE_STATUS_GOOD;
while (st != SANE_STATUS_EOF && py < im->ysize)
{
while (len > 0)
{
int bit, mask, val;
val = buffer[bufpos++]; len--;
mask = 0x80;
for (bit = 0; bit < 8 && px < pxmax; bit++)
{
((UINT8**)(im->image32))[py][px]
= val&mask ? 0xFF : 0;
((UINT8**)(im->image32))[py][pxa] = 0;
px += 4;
pxa += 4;
mask = mask >> 1;
}
if (px >= pxmax)
{
px = offset;
pxa = 3;
py++;
bufpos += padbytes;
len -= padbytes;
}
}
while (len <= 0 && st == SANE_STATUS_GOOD && remain > 0)
{
bufpos -= lastlen;
st = sane_read(self->h, buffer,
remain<(READSIZE) ? remain : (READSIZE), &len);
if (st && (st!=SANE_STATUS_EOF))
{
sane_cancel(self->h);
Py_BLOCK_THREADS
return PySane_Error(st);
}
remain -= len;
lastlen = len;
len -= bufpos;
}
}
break;
case 8:
case 16:
if (p.depth == 8)
{
padbytes = p.bytes_per_line - im->xsize;
incr = 1;
}
else
{
padbytes = p.bytes_per_line - 2 * im->xsize;
incr = 2;
bufpos = endian.c[0];
}
st = SANE_STATUS_GOOD;
while (st != SANE_STATUS_EOF && py < im->ysize)
{
while (len <= 0)
{
bufpos -= lastlen;
if (remain == 0)
{
sane_cancel(self->h);
Py_BLOCK_THREADS
PyErr_SetString(ErrorObject, "internal _sane error: premature end of scan");
return NULL;
}
st = sane_read(self->h, buffer,
remain<(READSIZE) ? remain : (READSIZE), &len);
if (st && (st!=SANE_STATUS_EOF))
{
sane_cancel(self->h);
Py_BLOCK_THREADS
return PySane_Error(st);
}
if (st == SANE_STATUS_EOF)
break;
lastlen = len;
remain -= len;
if (bufpos >= len)
len = 0;
else
len -= bufpos;
}
if (st == SANE_STATUS_EOF)
break;
((UINT8**)(im->image32))[py][px] = buffer[bufpos];
((UINT8**)(im->image32))[py][pxa] = 0;
bufpos += incr;
len -= incr;
px += 4;
pxa += 4;
if (px >= pxmax)
{
px = offset;
pxa = 3;
py++;
bufpos += padbytes;
len -= padbytes;
}
}
break;
default:
sane_cancel(self->h);
Py_BLOCK_THREADS
snprintf(errmsg, 80, "unsupported pixel depth: %i", p.depth);
PyErr_SetString(ErrorObject, errmsg);
return NULL;
}
if (!p.last_frame)
{
/* all sane_read calls in the above loop may return
SANE_STATUS_GOOD, but the backend may need another sane_read
call which returns SANE_STATUS_EOF in order to start
a new frame.
*/
do {
st = sane_read(self->h, buffer, READSIZE, &len);
}
while (st == SANE_STATUS_GOOD);
if (st != SANE_STATUS_EOF)
{
Py_BLOCK_THREADS
sane_cancel(self->h);
return PySane_Error(st);
}
st = sane_start(self->h);
if (st)
{
Py_BLOCK_THREADS
return PySane_Error(st);
}
}
}
}
/* enforce SANE_STATUS_EOF. Can be necessary for ADF scans for some backends */
do {
st = sane_read(self->h, buffer, READSIZE, &len);
}
while (st == SANE_STATUS_GOOD);
if (st != SANE_STATUS_EOF)
{
sane_cancel(self->h);
Py_BLOCK_THREADS
return PySane_Error(st);
}
if (!noCancel)
sane_cancel(self->h);
Py_BLOCK_THREADS
Py_INCREF(Py_None);
return Py_None;
}
#ifdef WITH_NUMARRAY
#include "numarray/libnumarray.h"
/* this global variable is set to 1 in 'init_sane()' after successfully
importing the numarray module. */
int NUMARRAY_IMPORTED = 0;
static PyObject *
SaneDev_arr_snap(SaneDevObject *self, PyObject *args)
{
SANE_Status st;
SANE_Byte buffer[READSIZE];
SANE_Int len;
SANE_Parameters p;
PyArrayObject *pyArr = NULL;
NumarrayType arrType;
int line, line_index, buffer_index, remain_bytes_line, num_pad_bytes;
int cp_num_bytes, total_remain, bpp, arr_bytes_per_line;
int pixels_per_line = -1;
char errmsg[80];
if (!NUMARRAY_IMPORTED)
{
PyErr_SetString(ErrorObject, "numarray package not available");
return NULL;
}
if (!PyArg_ParseTuple(args, "|i", &pixels_per_line))
return NULL;
if (self->h==NULL)
{
PyErr_SetString(ErrorObject, "SaneDev object is closed");
return NULL;
}
sane_get_parameters(self->h, &p);
if (p.format != SANE_FRAME_GRAY)
{
sane_cancel(self->h);
snprintf(errmsg, 80, "numarray only supports gray-scale images");
PyErr_SetString(ErrorObject, errmsg);
return NULL;
}
if (p.depth == 8)
{
bpp=1; /* bytes-per-pixel */
arrType = tUInt8;
}
else if (p.depth == 16)
{
bpp=2; /* bytes-per-pixel */
arrType = tUInt16;
}
else
{
sane_cancel(self->h);
snprintf(errmsg, 80, "arrsnap: unsupported pixel depth: %i", p.depth);
PyErr_SetString(ErrorObject, errmsg);
return NULL;
}
if (pixels_per_line < 1)
/* The user can choose a smaller result array than the actual scan */
pixels_per_line = p.pixels_per_line;
else
if (pixels_per_line > p.pixels_per_line)
{
PyErr_SetString(ErrorObject,"given pixels_per_line too big");
return NULL;
}
/* important: NumArray have indices like (y, x) !! */
if (!(pyArr = NA_NewArray(NULL, arrType, 2, p.lines, pixels_per_line)))
{
PyErr_SetString(ErrorObject, "failed to create NumArray object");