File: Sane.xs

package info (click to toggle)
libimage-sane-perl 0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 328 kB
  • ctags: 81
  • sloc: perl: 2,714; ansic: 5; makefile: 3
file content (579 lines) | stat: -rw-r--r-- 23,994 bytes parent folder | download
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
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include <saneperl.h>

MODULE = Image::Sane	PACKAGE = Image::Sane::Device		PREFIX = sane_

PROTOTYPES: ENABLE
  
void
sane_DESTROY (handle)
		SANE_Handle	handle
        CODE:
       		SV* sv = get_sv("Image::Sane::DEBUG", FALSE);
               	if (SvTRUE(sv)) printf("Closing SANE_Handle %p\n", (void *) handle);
		sane_close(handle);

void
sane__open(name)
		SANE_String_Const	name
	INIT:
		SANE_Status		status;
                SANE_Handle		h;
        PPCODE:
        	status = sane_open(name, &h);
		SV* sv = get_sv("Image::Sane::DEBUG", FALSE);
		if (SvTRUE(sv)) printf("sane_open returned SANE_Handle %p\n", (void *) h);
                PUSHMARK(sp);
	        XPUSHs(sv_2mortal(newSViv(status)));
                if (!status)
	                XPUSHs(sv_2mortal(newSViv(PTR2IV(h))));
                PUTBACK;

void
sane_get_option_descriptor (h, n)
		SANE_Handle	h
		SANE_Int	n
	INIT:
		const SANE_Option_Descriptor *	opt;
               	HV* chv = (HV*) sv_2mortal((SV*) newHV());
               	AV* cav = (AV*) sv_2mortal((SV*) newAV());
               	HV* hv = (HV*) sv_2mortal((SV*) newHV());
                int i;
        PPCODE:
       		SV* sv = get_sv("Image::Sane::DEBUG", FALSE);
               	if (SvTRUE(sv)) printf("Getting option description %d from SANE_Handle %p\n", n, (void *) h);
		opt = sane_get_option_descriptor (h, n);
                if (!opt) croak("Error getting sane_get_option_descriptor");
		if (opt->name != NULL) {
			hv_store(hv, "name", 4, newSVpv(opt->name, 0), 0);
		}
		if (opt->title != NULL) {
			hv_store(hv, "title", 5, newSVpv(opt->title, 0), 0);
		}
		if (opt->desc != NULL) {
			hv_store(hv, "desc", 4, newSVpv(opt->desc, 0), 0);
		}
		hv_store(hv, "type", 4, newSViv(opt->type), 0);
		hv_store(hv, "unit", 4, newSViv(opt->unit), 0);
		if (opt->type == SANE_TYPE_STRING)
                 hv_store(hv, "max_values", 10, newSViv(1), 0);
		else
                 hv_store(hv, "max_values", 10, newSViv(opt->size/(SANE_Int) sizeof (SANE_Word)), 0);
		hv_store(hv, "cap", 3, newSViv(opt->cap), 0);
		hv_store(hv, "constraint_type", 15, newSViv(opt->constraint_type), 0);
		switch (opt->constraint_type) {
			case SANE_CONSTRAINT_RANGE:
				if (opt->type == SANE_TYPE_FIXED) {
	                                hv_store(chv, "min", 3, newSVnv(SANE_UNFIX (opt->constraint.range->min)), 0);
					hv_store(chv, "max", 3, newSVnv(SANE_UNFIX (opt->constraint.range->max)), 0);
					hv_store(chv, "quant", 5, newSVnv(SANE_UNFIX (opt->constraint.range->quant)), 0);
					hv_store(hv, "constraint", 10, newRV((SV *)chv), 0);
                                }
                                else {
	                                hv_store(chv, "min", 3, newSViv(opt->constraint.range->min), 0);
					hv_store(chv, "max", 3, newSViv(opt->constraint.range->max), 0);
					hv_store(chv, "quant", 5, newSViv(opt->constraint.range->quant), 0);
					hv_store(hv, "constraint", 10, newRV((SV *)chv), 0);
                                }
				break;
			case SANE_CONSTRAINT_WORD_LIST:
				for (i = 0; i < opt->constraint.word_list[0]; ++i) {
					if (opt->type == SANE_TYPE_INT)
						av_push(cav, newSViv(opt->constraint.word_list[i + 1]));
					else
						av_push(cav, newSVnv(SANE_UNFIX (opt->constraint.word_list[i + 1])));
				}
				hv_store(hv, "constraint", 10, newRV((SV *)cav), 0);
				break;
			case SANE_CONSTRAINT_STRING_LIST:
				for (i = 0; opt->constraint.string_list[i]; ++i) {
					av_push(cav, newSVpv(opt->constraint.string_list[i], 0));
				}
				hv_store(hv, "constraint", 10, newRV((SV *)cav), 0);
				break;
			default:
				break;
		}
                PUSHMARK(sp);
		XPUSHs(newRV((SV *)hv));
                PUTBACK;

void
sane__get_option (h, n)
		SANE_Handle	h
		SANE_Int	n
	INIT:
		SANE_Status	status;
		void *		value;
		const SANE_Option_Descriptor *	opt;
        PPCODE:
       		SV* sv = get_sv("Image::Sane::DEBUG", FALSE);
               	if (SvTRUE(sv)) printf("Getting option %d from SANE_Handle %p\n", n, (void *) h);
		opt = sane_get_option_descriptor (h, n);
                if (!opt) croak("Error getting sane_get_option_descriptor");
                if ( opt->size == 0 ) {
                        XSRETURN_UNDEF;
                        return;
                }
		value = malloc (opt->size);
		if (!value) croak("Error allocating memory");
		status = sane_control_option (h, n, SANE_ACTION_GET_VALUE, value, 0);
                PUSHMARK(sp);
	        XPUSHs(sv_2mortal(newSViv(status)));
		if (status) {
                }
                else if (opt->type == SANE_TYPE_STRING)
			XPUSHs(sv_2mortal(newSVpv((char *) value, 0)));
                else if (opt->size > (SANE_Int) sizeof (SANE_Word)) {
               		AV* av = (AV*) sv_2mortal((SV*) newAV());
			int vector_length = opt->size / sizeof (SANE_Word);
                        int i;
			for (i = 0; i < vector_length; ++i)
				if (opt->type == SANE_TYPE_INT)
					av_push(av, newSViv(*(SANE_Int *) (value+i*sizeof(SANE_Word))));
				else
					av_push(av, newSVnv(SANE_UNFIX (*(SANE_Word *) (value+i*sizeof(SANE_Word)))));
			XPUSHs(newRV((SV *)av));
                }
                else {
	                switch (opt->type) {
				case SANE_TYPE_BOOL:
					XPUSHs(sv_2mortal(newSViv(*(SANE_Bool *) value)));
					break;
				case SANE_TYPE_INT:
					XPUSHs(sv_2mortal(newSViv(*(SANE_Int *) value)));
					break;
				case SANE_TYPE_FIXED:
                                	XPUSHs(sv_2mortal(newSVnv(SANE_UNFIX (*(SANE_Word *) value))));
					break;
				default:
					break;
			}
                }
                PUTBACK;
		free (value);

void
sane__set_auto (h, n)
		SANE_Handle	h
		SANE_Int	n
	INIT:
		SANE_Status	status;
		SANE_Int	info;
        PPCODE:
       		SV* sv = get_sv("Image::Sane::DEBUG", FALSE);
               	if (SvTRUE(sv)) printf("Setting option %d to automatic on SANE_Handle %p\n", n, (void *) h);
		status = sane_control_option (h, n, SANE_ACTION_SET_AUTO, 0, &info);
                PUSHMARK(sp);
	        XPUSHs(sv_2mortal(newSViv(status)));
                XPUSHs(sv_2mortal(newSViv(info)));
                PUTBACK;

void
sane__set_option (h, n, value)
		SANE_Handle	h
		SANE_Int	n
                SV*		value
	INIT:
		SANE_Status	status;
		SANE_Int	info;
		void *		valuep;
		const SANE_Option_Descriptor *	opt;
		SANE_Bool	b;
		SANE_Fixed      fixed;
		int		i, vector_length = 0;
		SV **		svp;
		SANE_Word *	vector;
		char *		string;
        PPCODE:
       		SV* sv = get_sv("Image::Sane::DEBUG", FALSE);
               	if (SvTRUE(sv)) printf("Setting option %d on SANE_Handle %p\n", n, (void *) h);
		opt = sane_get_option_descriptor (h, n);
                if (!opt) croak("Error getting sane_get_option_descriptor");
		switch (opt->type) {
			case SANE_TYPE_BOOL:
				b = (SANE_Bool)SvIV(value);
				valuep = &b;
				break;
			case SANE_TYPE_INT:
			case SANE_TYPE_FIXED:
				if (SvNIOK(value) || SvPOK(value)) {
					if (opt->type == SANE_TYPE_INT)
							fixed = (SANE_Int)SvIV(value);
                                        else
							fixed = (SANE_Fixed)SANE_FIX(SvNV(value));
					valuep = &fixed;
                                }
                                else if (SvROK(value) && SvTYPE(SvRV(value)) == SVt_PVAV) {
	                                AV* array = (AV*) SvRV(value);
                                        vector_length = av_len((AV*) array) + 1;
					if (vector_length > opt->size / sizeof (SANE_Word))
						croak("Array has too many elements");
					vector = malloc (opt->size);
					if (!vector)
						croak("Error allocating memory");
					for (i = 0; i < vector_length; i++) {
						svp = av_fetch(array, i, 0);
						if (SvNIOK(*svp) || SvPOK(*svp)) {
							if (opt->type == SANE_TYPE_INT)
	                                        		vector[i] = (SANE_Int)SvIV(*(svp));
                                        		else
                                        			vector[i] = (SANE_Fixed)SANE_FIX(SvNV(*svp));
                                                }
					}
                                	valuep = vector;
				}
                                else
	                                croak("Value is neither a number, nor an array reference");
                                break;
			case SANE_TYPE_STRING:
				string = (char *)SvPV_nolen(value);
                                valuep = malloc (opt->size);
				if (!valuep) croak("Error allocating memory");
				strncpy (valuep, string, opt->size);
				((char *) valuep)[opt->size - 1] = 0;
				break;
			default:
				break;
		}
		status = sane_control_option (h, n, SANE_ACTION_SET_VALUE, valuep, &info);
       	        if (opt->type == SANE_TYPE_STRING
                    || ((opt->type == SANE_TYPE_INT
                         || opt->type == SANE_TYPE_FIXED) && vector_length))
                	free(valuep);
                PUSHMARK(sp);
	        XPUSHs(sv_2mortal(newSViv(status)));
               	XPUSHs(sv_2mortal(newSViv(info)));
                PUTBACK;

SANE_Status
sane__start (handle)
		SANE_Handle	handle
	INIT:
        	SANE_Status	status;
	CODE:
       		SV* sv = get_sv("Image::Sane::DEBUG", FALSE);
               	if (SvTRUE(sv)) printf("Running sane_start for SANE_Handle %p\n", (void *) handle);
       		RETVAL = sane_start(handle);
        OUTPUT:
                RETVAL

void
sane__get_parameters (handle)
		SANE_Handle		handle
	INIT:
		SANE_Status		status;
		SANE_Parameters		params;
               	HV* hv = (HV*) sv_2mortal((SV*) newHV());
        PPCODE:
       		SV* sv = get_sv("Image::Sane::DEBUG", FALSE);
               	if (SvTRUE(sv)) printf("Getting parameters for SANE_Handle %p\n", (void *) handle);
		status = sane_get_parameters (handle, &params);
                PUSHMARK(sp);
	        XPUSHs(sv_2mortal(newSViv(status)));
                if (!status) {
			hv_store(hv, "format", 6, newSViv(params.format), 0);
			hv_store(hv, "last_frame", 10, newSViv(params.last_frame), 0);
			hv_store(hv, "bytes_per_line", 14, newSViv(params.bytes_per_line), 0);
			hv_store(hv, "pixels_per_line", 15, newSViv(params.pixels_per_line), 0);
			hv_store(hv, "lines", 5, newSViv(params.lines), 0);
			hv_store(hv, "depth", 5, newSViv(params.depth), 0);
			XPUSHs(newRV((SV *)hv));
                }
                PUTBACK;

void
sane__read (handle, max_length)
		SANE_Handle	handle
		SANE_Int	max_length
	INIT:
		SANE_Status	status;
                SANE_Byte *	data;
                SANE_Int	length;
        PPCODE:
		data = malloc (max_length);
		status = sane_read (handle, data, max_length, &length);
                PUSHMARK(sp);
	        XPUSHs(sv_2mortal(newSViv(status)));
                if (!status) {
                	XPUSHs(sv_2mortal(newSVpvn(data, length)));
			XPUSHs(sv_2mortal(newSViv(length)));
                }
                PUTBACK;
                free (data);

void
sane_cancel (handle)
	SANE_Handle	handle

void
sane__set_io_mode (handle, non_blocking)
		SANE_Handle	handle
        	SANE_Bool	non_blocking
	INIT:
		SANE_Status	status;
	CODE:
       		SV* sv = get_sv("Image::Sane::DEBUG", FALSE);
               	if (SvTRUE(sv)) printf("Setting IO mode to %d on SANE_Handle %p\n", non_blocking, (void *) handle);
       		status = sane_set_io_mode (handle, non_blocking);
                PUSHMARK(sp);
	        XPUSHs(sv_2mortal(newSViv(status)));
                PUTBACK;

void
sane__get_select_fd (handle)
		SANE_Handle	handle
	INIT:
		SANE_Status	status;
		SANE_Int	fd;
        PPCODE:
       		SV* sv = get_sv("Image::Sane::DEBUG", FALSE);
               	if (SvTRUE(sv)) printf("Getting file handle of SANE_Handle %p\n", (void *) handle);
		status = sane_get_select_fd (handle, &fd);
                PUSHMARK(sp);
	        XPUSHs(sv_2mortal(newSViv(status)));
                if (!status)
	                XPUSHs(sv_2mortal(newSViv(fd)));
                PUTBACK;


MODULE = Image::Sane       PACKAGE = Image::Sane		  PREFIX = sane_

PROTOTYPES: ENABLE
  
BOOT:
	HV *stash;
	stash = gv_stashpv("Image::Sane", TRUE);

	newCONSTSUB(stash, "SANE_FALSE", newSViv(SANE_FALSE));
	newCONSTSUB(stash, "SANE_TRUE", newSViv(SANE_TRUE));

	newCONSTSUB(stash, "SANE_STATUS_GOOD", newSViv(SANE_STATUS_GOOD));
	newCONSTSUB(stash, "SANE_STATUS_UNSUPPORTED", newSViv(SANE_STATUS_UNSUPPORTED));
	newCONSTSUB(stash, "SANE_STATUS_CANCELLED", newSViv(SANE_STATUS_CANCELLED));
	newCONSTSUB(stash, "SANE_STATUS_DEVICE_BUSY", newSViv(SANE_STATUS_DEVICE_BUSY));
	newCONSTSUB(stash, "SANE_STATUS_INVAL", newSViv(SANE_STATUS_INVAL));
	newCONSTSUB(stash, "SANE_STATUS_EOF", newSViv(SANE_STATUS_EOF));
	newCONSTSUB(stash, "SANE_STATUS_JAMMED", newSViv(SANE_STATUS_JAMMED));
	newCONSTSUB(stash, "SANE_STATUS_NO_DOCS", newSViv(SANE_STATUS_NO_DOCS));
	newCONSTSUB(stash, "SANE_STATUS_COVER_OPEN", newSViv(SANE_STATUS_COVER_OPEN));
	newCONSTSUB(stash, "SANE_STATUS_IO_ERROR", newSViv(SANE_STATUS_IO_ERROR));
	newCONSTSUB(stash, "SANE_STATUS_NO_MEM", newSViv(SANE_STATUS_NO_MEM));
	newCONSTSUB(stash, "SANE_STATUS_ACCESS_DENIED", newSViv(SANE_STATUS_ACCESS_DENIED));

	newCONSTSUB(stash, "SANE_TYPE_BOOL", newSViv(SANE_TYPE_BOOL));
	newCONSTSUB(stash, "SANE_TYPE_INT", newSViv(SANE_TYPE_INT));
	newCONSTSUB(stash, "SANE_TYPE_FIXED", newSViv(SANE_TYPE_FIXED));
	newCONSTSUB(stash, "SANE_TYPE_STRING", newSViv(SANE_TYPE_STRING));
	newCONSTSUB(stash, "SANE_TYPE_BUTTON", newSViv(SANE_TYPE_BUTTON));
	newCONSTSUB(stash, "SANE_TYPE_GROUP", newSViv(SANE_TYPE_GROUP));

	newCONSTSUB(stash, "SANE_UNIT_NONE", newSViv(SANE_UNIT_NONE));
	newCONSTSUB(stash, "SANE_UNIT_PIXEL", newSViv(SANE_UNIT_PIXEL));
	newCONSTSUB(stash, "SANE_UNIT_BIT", newSViv(SANE_UNIT_BIT));
	newCONSTSUB(stash, "SANE_UNIT_MM", newSViv(SANE_UNIT_MM));
	newCONSTSUB(stash, "SANE_UNIT_DPI", newSViv(SANE_UNIT_DPI));
	newCONSTSUB(stash, "SANE_UNIT_PERCENT", newSViv(SANE_UNIT_PERCENT));
	newCONSTSUB(stash, "SANE_UNIT_MICROSECOND", newSViv(SANE_UNIT_MICROSECOND));

	newCONSTSUB(stash, "SANE_CAP_SOFT_SELECT", newSViv(SANE_CAP_SOFT_SELECT));
	newCONSTSUB(stash, "SANE_CAP_HARD_SELECT", newSViv(SANE_CAP_HARD_SELECT));
	newCONSTSUB(stash, "SANE_CAP_SOFT_DETECT", newSViv(SANE_CAP_SOFT_DETECT));
	newCONSTSUB(stash, "SANE_CAP_EMULATED", newSViv(SANE_CAP_EMULATED));
	newCONSTSUB(stash, "SANE_CAP_AUTOMATIC", newSViv(SANE_CAP_AUTOMATIC));
	newCONSTSUB(stash, "SANE_CAP_INACTIVE", newSViv(SANE_CAP_INACTIVE));
	newCONSTSUB(stash, "SANE_CAP_ADVANCED", newSViv(SANE_CAP_ADVANCED));
#ifdef SANE_CAP_ALWAYS_SETTABLE
	newCONSTSUB(stash, "SANE_CAP_ALWAYS_SETTABLE", newSViv(SANE_CAP_ALWAYS_SETTABLE));
#endif

	newCONSTSUB(stash, "SANE_INFO_INEXACT", newSViv(SANE_INFO_INEXACT));
	newCONSTSUB(stash, "SANE_INFO_RELOAD_OPTIONS", newSViv(SANE_INFO_RELOAD_OPTIONS));
	newCONSTSUB(stash, "SANE_INFO_RELOAD_PARAMS", newSViv(SANE_INFO_RELOAD_PARAMS));

	newCONSTSUB(stash, "SANE_CONSTRAINT_NONE", newSViv(SANE_CONSTRAINT_NONE));
	newCONSTSUB(stash, "SANE_CONSTRAINT_RANGE", newSViv(SANE_CONSTRAINT_RANGE));
	newCONSTSUB(stash, "SANE_CONSTRAINT_WORD_LIST", newSViv(SANE_CONSTRAINT_WORD_LIST));
	newCONSTSUB(stash, "SANE_CONSTRAINT_STRING_LIST", newSViv(SANE_CONSTRAINT_STRING_LIST));

	newCONSTSUB(stash, "SANE_FRAME_GRAY", newSViv(SANE_FRAME_GRAY));
	newCONSTSUB(stash, "SANE_FRAME_RGB", newSViv(SANE_FRAME_RGB));
	newCONSTSUB(stash, "SANE_FRAME_RED", newSViv(SANE_FRAME_RED));
	newCONSTSUB(stash, "SANE_FRAME_GREEN", newSViv(SANE_FRAME_GREEN));
	newCONSTSUB(stash, "SANE_FRAME_BLUE", newSViv(SANE_FRAME_BLUE));

	newCONSTSUB(stash, "SANE_NAME_NUM_OPTIONS", newSVpv(SANE_NAME_NUM_OPTIONS, 0));
	newCONSTSUB(stash, "SANE_NAME_PREVIEW", newSVpv(SANE_NAME_PREVIEW, 0));
	newCONSTSUB(stash, "SANE_NAME_GRAY_PREVIEW", newSVpv(SANE_NAME_GRAY_PREVIEW, 0));
	newCONSTSUB(stash, "SANE_NAME_BIT_DEPTH", newSVpv(SANE_NAME_BIT_DEPTH, 0));
	newCONSTSUB(stash, "SANE_NAME_SCAN_MODE", newSVpv(SANE_NAME_SCAN_MODE, 0));
	newCONSTSUB(stash, "SANE_NAME_SCAN_SPEED", newSVpv(SANE_NAME_SCAN_SPEED, 0));
	newCONSTSUB(stash, "SANE_NAME_SCAN_SOURCE", newSVpv(SANE_NAME_SCAN_SOURCE, 0));
	newCONSTSUB(stash, "SANE_NAME_BACKTRACK", newSVpv(SANE_NAME_BACKTRACK, 0));
	newCONSTSUB(stash, "SANE_NAME_SCAN_TL_X", newSVpv(SANE_NAME_SCAN_TL_X, 0));
	newCONSTSUB(stash, "SANE_NAME_SCAN_TL_Y", newSVpv(SANE_NAME_SCAN_TL_Y, 0));
	newCONSTSUB(stash, "SANE_NAME_SCAN_BR_X", newSVpv(SANE_NAME_SCAN_BR_X, 0));
	newCONSTSUB(stash, "SANE_NAME_SCAN_BR_Y", newSVpv(SANE_NAME_SCAN_BR_Y, 0));
	newCONSTSUB(stash, "SANE_NAME_SCAN_RESOLUTION", newSVpv(SANE_NAME_SCAN_RESOLUTION, 0));
	newCONSTSUB(stash, "SANE_NAME_SCAN_X_RESOLUTION", newSVpv(SANE_NAME_SCAN_X_RESOLUTION, 0));
	newCONSTSUB(stash, "SANE_NAME_SCAN_Y_RESOLUTION", newSVpv(SANE_NAME_SCAN_Y_RESOLUTION, 0));
#ifdef SANE_NAME_PAGE_WIDTH
	newCONSTSUB(stash, "SANE_NAME_PAGE_WIDTH", newSVpv(SANE_NAME_PAGE_WIDTH, 0));
#endif
#ifdef SANE_NAME_PAGE_HEIGHT
	newCONSTSUB(stash, "SANE_NAME_PAGE_HEIGHT", newSVpv(SANE_NAME_PAGE_HEIGHT, 0));
#endif
	newCONSTSUB(stash, "SANE_NAME_CUSTOM_GAMMA", newSVpv(SANE_NAME_CUSTOM_GAMMA, 0));
	newCONSTSUB(stash, "SANE_NAME_GAMMA_VECTOR", newSVpv(SANE_NAME_GAMMA_VECTOR, 0));
	newCONSTSUB(stash, "SANE_NAME_GAMMA_VECTOR_R", newSVpv(SANE_NAME_GAMMA_VECTOR_R, 0));
	newCONSTSUB(stash, "SANE_NAME_GAMMA_VECTOR_G", newSVpv(SANE_NAME_GAMMA_VECTOR_G, 0));
	newCONSTSUB(stash, "SANE_NAME_GAMMA_VECTOR_B", newSVpv(SANE_NAME_GAMMA_VECTOR_B, 0));
	newCONSTSUB(stash, "SANE_NAME_BRIGHTNESS", newSVpv(SANE_NAME_BRIGHTNESS, 0));
	newCONSTSUB(stash, "SANE_NAME_CONTRAST", newSVpv(SANE_NAME_CONTRAST, 0));
	newCONSTSUB(stash, "SANE_NAME_GRAIN_SIZE", newSVpv(SANE_NAME_GRAIN_SIZE, 0));
	newCONSTSUB(stash, "SANE_NAME_HALFTONE", newSVpv(SANE_NAME_HALFTONE, 0));
	newCONSTSUB(stash, "SANE_NAME_BLACK_LEVEL", newSVpv(SANE_NAME_BLACK_LEVEL, 0));
	newCONSTSUB(stash, "SANE_NAME_WHITE_LEVEL", newSVpv(SANE_NAME_WHITE_LEVEL, 0));
	newCONSTSUB(stash, "SANE_NAME_WHITE_LEVEL_R", newSVpv(SANE_NAME_WHITE_LEVEL_R, 0));
	newCONSTSUB(stash, "SANE_NAME_WHITE_LEVEL_G", newSVpv(SANE_NAME_WHITE_LEVEL_G, 0));
	newCONSTSUB(stash, "SANE_NAME_WHITE_LEVEL_B", newSVpv(SANE_NAME_WHITE_LEVEL_B, 0));
	newCONSTSUB(stash, "SANE_NAME_SHADOW", newSVpv(SANE_NAME_SHADOW, 0));
	newCONSTSUB(stash, "SANE_NAME_SHADOW_R", newSVpv(SANE_NAME_SHADOW_R, 0));
	newCONSTSUB(stash, "SANE_NAME_SHADOW_G", newSVpv(SANE_NAME_SHADOW_G, 0));
	newCONSTSUB(stash, "SANE_NAME_SHADOW_B", newSVpv(SANE_NAME_SHADOW_B, 0));
	newCONSTSUB(stash, "SANE_NAME_HIGHLIGHT", newSVpv(SANE_NAME_HIGHLIGHT, 0));
	newCONSTSUB(stash, "SANE_NAME_HIGHLIGHT_R", newSVpv(SANE_NAME_HIGHLIGHT_R, 0));
	newCONSTSUB(stash, "SANE_NAME_HIGHLIGHT_G", newSVpv(SANE_NAME_HIGHLIGHT_G, 0));
	newCONSTSUB(stash, "SANE_NAME_HIGHLIGHT_B", newSVpv(SANE_NAME_HIGHLIGHT_B, 0));
	newCONSTSUB(stash, "SANE_NAME_HUE", newSVpv(SANE_NAME_HUE, 0));
	newCONSTSUB(stash, "SANE_NAME_SATURATION", newSVpv(SANE_NAME_SATURATION, 0));
	newCONSTSUB(stash, "SANE_NAME_FILE", newSVpv(SANE_NAME_FILE, 0));
	newCONSTSUB(stash, "SANE_NAME_HALFTONE_DIMENSION", newSVpv(SANE_NAME_HALFTONE_DIMENSION, 0));
	newCONSTSUB(stash, "SANE_NAME_HALFTONE_PATTERN", newSVpv(SANE_NAME_HALFTONE_PATTERN, 0));
	newCONSTSUB(stash, "SANE_NAME_RESOLUTION_BIND", newSVpv(SANE_NAME_RESOLUTION_BIND, 0));
	newCONSTSUB(stash, "SANE_NAME_NEGATIVE", newSVpv(SANE_NAME_NEGATIVE, 0));
	newCONSTSUB(stash, "SANE_NAME_QUALITY_CAL", newSVpv(SANE_NAME_QUALITY_CAL, 0));
	newCONSTSUB(stash, "SANE_NAME_DOR", newSVpv(SANE_NAME_DOR, 0));
	newCONSTSUB(stash, "SANE_NAME_RGB_BIND", newSVpv(SANE_NAME_RGB_BIND, 0));
	newCONSTSUB(stash, "SANE_NAME_THRESHOLD", newSVpv(SANE_NAME_THRESHOLD, 0));
	newCONSTSUB(stash, "SANE_NAME_ANALOG_GAMMA", newSVpv(SANE_NAME_ANALOG_GAMMA, 0));
	newCONSTSUB(stash, "SANE_NAME_ANALOG_GAMMA_R", newSVpv(SANE_NAME_ANALOG_GAMMA_R, 0));
	newCONSTSUB(stash, "SANE_NAME_ANALOG_GAMMA_G", newSVpv(SANE_NAME_ANALOG_GAMMA_G, 0));
	newCONSTSUB(stash, "SANE_NAME_ANALOG_GAMMA_B", newSVpv(SANE_NAME_ANALOG_GAMMA_B, 0));
	newCONSTSUB(stash, "SANE_NAME_ANALOG_GAMMA_BIND", newSVpv(SANE_NAME_ANALOG_GAMMA_BIND, 0));
	newCONSTSUB(stash, "SANE_NAME_WARMUP", newSVpv(SANE_NAME_WARMUP, 0));
	newCONSTSUB(stash, "SANE_NAME_CAL_EXPOS_TIME", newSVpv(SANE_NAME_CAL_EXPOS_TIME, 0));
	newCONSTSUB(stash, "SANE_NAME_CAL_EXPOS_TIME_R", newSVpv(SANE_NAME_CAL_EXPOS_TIME_R, 0));
	newCONSTSUB(stash, "SANE_NAME_CAL_EXPOS_TIME_G", newSVpv(SANE_NAME_CAL_EXPOS_TIME_G, 0));
	newCONSTSUB(stash, "SANE_NAME_CAL_EXPOS_TIME_B", newSVpv(SANE_NAME_CAL_EXPOS_TIME_B, 0));
	newCONSTSUB(stash, "SANE_NAME_SCAN_EXPOS_TIME", newSVpv(SANE_NAME_SCAN_EXPOS_TIME, 0));
	newCONSTSUB(stash, "SANE_NAME_SCAN_EXPOS_TIME_R", newSVpv(SANE_NAME_SCAN_EXPOS_TIME_R, 0));
	newCONSTSUB(stash, "SANE_NAME_SCAN_EXPOS_TIME_G", newSVpv(SANE_NAME_SCAN_EXPOS_TIME_G, 0));
	newCONSTSUB(stash, "SANE_NAME_SCAN_EXPOS_TIME_B", newSVpv(SANE_NAME_SCAN_EXPOS_TIME_B, 0));
	newCONSTSUB(stash, "SANE_NAME_SELECT_EXPOSURE_TIME", newSVpv(SANE_NAME_SELECT_EXPOSURE_TIME, 0));
	newCONSTSUB(stash, "SANE_NAME_CAL_LAMP_DEN", newSVpv(SANE_NAME_CAL_LAMP_DEN, 0));
	newCONSTSUB(stash, "SANE_NAME_SCAN_LAMP_DEN", newSVpv(SANE_NAME_SCAN_LAMP_DEN, 0));
	newCONSTSUB(stash, "SANE_NAME_SELECT_LAMP_DENSITY", newSVpv(SANE_NAME_SELECT_LAMP_DENSITY, 0));
	newCONSTSUB(stash, "SANE_NAME_LAMP_OFF_AT_EXIT", newSVpv(SANE_NAME_LAMP_OFF_AT_EXIT, 0));

void
sane__init ()
	INIT:
		SANE_Status		status;
                SANE_Int		version_code;
	PPCODE:
                call_pv("Image::Sane::_exit", G_VOID|G_NOARGS);
       	        SV* sv = get_sv("Image::Sane::DEBUG", FALSE);
               	if (SvTRUE(sv)) printf("Running sane_init\n");
		status = sane_init(&version_code, NULL);
                PUSHMARK(sp);
	        XPUSHs(sv_2mortal(newSViv(status)));
                if (!status)
	                XPUSHs(sv_2mortal(newSViv(version_code)));
                PUTBACK;

 # Can't test this. If needed, should be integrated into _init above
 # void
 # CallSubSV(name)
 # 		SV *			name
 # 		SANE_String_Const	resource
 # 		SANE_Char *		username
 # 		SANE_Char *		password
 # 	CODE:
 # 		dSP;
 # 		int count;
 # 
 # 		ENTER;
 # 		SAVETMPS;
 # 
 # 		PUSHMARK(SP);
 # 		XPUSHs(sv_2mortal(newSVpv(resource, 0)));
 # 		XPUSHs(sv_2mortal(newSVpv(username, 0)));
 # 		XPUSHs(sv_2mortal(newSVpv(password, 0)));
 # 		PUTBACK;
 # 
 # 		count = call_sv(name, G_DISCARD, G_ARRAY);
 # 
 # 		SPAGAIN;
 # 
 # 		if (count != 2) croak("Big trouble\n");
 # 
 # 		printf ("Returned %s and %s\n\n", POPs, POPs);
 # 
 # 		PUTBACK;
 # 		FREETMPS;
 # 		LEAVE;

void
sane__get_version (version_code)
                SANE_Int	version_code
        PPCODE:
                PUSHMARK(sp);
		XPUSHs(sv_2mortal(newSViv(SANE_VERSION_MAJOR (version_code))));
		XPUSHs(sv_2mortal(newSViv(SANE_VERSION_MINOR (version_code))));
		XPUSHs(sv_2mortal(newSViv(SANE_VERSION_BUILD (version_code))));
                PUTBACK;

void
sane__get_devices (local=SANE_FALSE)
		SANE_Bool	local
	INIT:
		SANE_Status	status;
		AV * array;
                int i;
                const SANE_Device **	device_list;
		array = (AV *)sv_2mortal((SV *)newAV());
        PPCODE:
       		SV* sv = get_sv("Image::Sane::DEBUG", FALSE);
               	if (SvTRUE(sv)) printf("Running sane_get_devices\n");
                status = sane_get_devices (&device_list, local);
                PUSHMARK(sp);
	        XPUSHs(sv_2mortal(newSViv(status)));
                if (!status) {
			for (i = 0; device_list[i]; ++i) {
        	        	HV* hv = (HV*) sv_2mortal((SV*) newHV());
				hv_store(hv, "name", 4, newSVpv(device_list[i]->name, 0), 0);
				hv_store(hv, "vendor", 6, newSVpv(device_list[i]->vendor, 0), 0);
				hv_store(hv, "model", 5, newSVpv(device_list[i]->model, 0), 0);
				hv_store(hv, "type", 4, newSVpv(device_list[i]->type, 0), 0);
				XPUSHs(newRV((SV *)hv));
	                }
                }
                PUTBACK;

SANE_String_Const
sane_strstatus (status)
	SANE_Status	status

void
sane__exit ()
	CODE:
       		SV* sv = get_sv("Image::Sane::_vc", FALSE);
               	if (SvTRUE(sv)) {
        		sv = get_sv("Image::Sane::DEBUG", FALSE);
                	if (SvTRUE(sv)) printf("Exiting via sane_exit\n");
			sane_exit;
		}