libsane 0.1.0

Scan documents and images from Rust: safe bindings to SANE (Scanner Access Now Easy) for flatbed scanners, document feeders and multifunction printers on Linux, BSD and macOS
Documentation
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
//! Tests against SANE's virtual `test` backend: `tests/sane.d` enables only
//! that backend, and `tests/sane.d/test.conf` configures it.
//!
//! SANE is initialized once per process at a time, so every test takes a
//! lock before initializing it.

use std::io::Read;
use std::sync::{Mutex, MutexGuard, Once, PoisonError};

use libsane::{
    Capabilities, Constraint, Error, Fixed, FrameFormat, Range, Sane, Unit, Value, ValueType,
    well_known,
};

static LOCK: Mutex<()> = Mutex::new(());
static ENV: Once = Once::new();

/// Takes the process-wide test lock, after pointing SANE to the test
/// configuration (libsane reads `SANE_CONFIG_DIR` once per process, at the
/// first `sane_init`).
fn lock() -> MutexGuard<'static, ()> {
    let guard = LOCK.lock().unwrap_or_else(PoisonError::into_inner);
    ENV.call_once(|| {
        // SAFETY: called under LOCK, before libsane runs in this process;
        // the other test threads wait for LOCK.
        unsafe {
            std::env::set_var(
                "SANE_CONFIG_DIR",
                concat!(env!("CARGO_MANIFEST_DIR"), "/tests/sane.d"),
            );
        }
    });
    guard
}

/// Runs `test` with an initialized SANE, alone in the process.
fn with_sane<R>(test: impl FnOnce(&Sane) -> R) -> R {
    let _guard = lock();
    let sane = Sane::init().expect("sane_init failed");
    test(&sane)
}

#[test]
fn init_is_exclusive_and_can_be_repeated() {
    with_sane(|sane| {
        assert_eq!(sane.version().major, 1);
        assert_eq!(Sane::init().unwrap_err(), Error::AlreadyInitialized);
    });
    // Dropping the guard released SANE.
    with_sane(|_| {});
}

#[test]
fn lists_the_test_devices() {
    with_sane(|sane| {
        let devices = sane.devices(true).unwrap();
        let names: Vec<_> = devices.iter().map(|device| device.name.as_str()).collect();
        assert_eq!(names, ["test:0", "test:1"]);
        let device = &devices[0];
        assert_eq!(device.vendor, "Noname");
        assert_eq!(device.model, "frontend-tester");
        assert_eq!(device.kind, "virtual device");
        let opened = sane.open_device(device).unwrap();
        assert_eq!(opened.name(), "test:0");
    });
}

#[test]
fn open_errors() {
    with_sane(|sane| {
        assert_eq!(sane.open("nonexistent:0").unwrap_err(), Error::Invalid);
        assert_eq!(sane.open("test\0").unwrap_err(), Error::NulByte);
        // Two devices can be open at once.
        let _first = sane.open("test:0").unwrap();
        let _second = sane.open("test:1").unwrap();
    });
}

#[test]
fn describes_options_and_constraints() {
    with_sane(|sane| {
        let device = sane.open("test:0").unwrap();
        let options = device.options();
        assert!(options.iter().all(|option| option.index > 0));
        assert!(
            options
                .iter()
                .any(|option| option.value_type == ValueType::Group)
        );

        let mode = device.option(well_known::MODE).unwrap();
        assert_eq!(mode.value_type, ValueType::String);
        assert_eq!(
            mode.constraint,
            Constraint::StringList(vec!["Gray".into(), "Color".into()])
        );
        assert!(mode.is_active() && mode.is_settable() && mode.is_readable());

        let depth = device.option(well_known::DEPTH).unwrap();
        assert_eq!(depth.constraint, Constraint::IntList(vec![1, 8, 16]));

        let resolution = device.option(well_known::RESOLUTION).unwrap();
        assert_eq!(resolution.unit, Unit::Dpi);
        assert_eq!(resolution.value_type, ValueType::Fixed);
        assert!(matches!(
            resolution.constraint,
            Constraint::FixedRange(Range { min, max, .. })
                if min == Fixed::from(1) && max == Fixed::from(1200)
        ));

        let fixed = device.option("fixed-constraint-range").unwrap();
        assert_eq!(fixed.value_type, ValueType::Fixed);
        assert!(matches!(fixed.constraint, Constraint::FixedRange(_)));
        let fixed_list = device.option("fixed-constraint-word-list").unwrap();
        assert!(
            matches!(&fixed_list.constraint, Constraint::FixedList(values) if values.contains(&Fixed::from(42)))
        );

        let array = device.option("int-constraint-array").unwrap();
        assert!(array.len() > 1);

        let auto = device.option("bool-soft-select-soft-detect-auto").unwrap();
        assert!(
            auto.capabilities
                .contains(Capabilities::AUTOMATIC | Capabilities::ADVANCED)
        );

        // Lookup by index and by descriptor.
        assert_eq!(device.option(mode.index).unwrap().name, "mode");
        assert_eq!(device.option(mode).unwrap().index, mode.index);
        assert!(device.option("no-such-option").is_none());
    });
}

#[test]
fn reads_and_sets_typed_values() {
    with_sane(|sane| {
        let mut device = sane.open("test:0").unwrap();

        assert_eq!(device.value("int").unwrap(), Value::Int(42));
        device.set_value("int", -7).unwrap();
        assert_eq!(device.value("int").unwrap(), Value::Int(-7));

        assert_eq!(
            device.value("bool-soft-select-soft-detect").unwrap(),
            Value::Bool(false)
        );
        device
            .set_value("bool-soft-select-soft-detect", true)
            .unwrap();
        assert_eq!(
            device.value("bool-soft-select-soft-detect").unwrap(),
            Value::Bool(true)
        );

        device.set_value("fixed", 1.5).unwrap();
        assert_eq!(
            device.value("fixed").unwrap(),
            Value::Fixed(Fixed::from_f64(1.5))
        );
        // An integer is accepted for a fixed option.
        device.set_value("fixed", 3).unwrap();
        assert_eq!(device.value("fixed").unwrap().as_f64(), Some(3.0));

        device.set_value("string", "hello").unwrap();
        assert_eq!(
            device.value("string").unwrap(),
            Value::String("hello".into())
        );
        device
            .set_value("string-constraint-string-list", "Second entry")
            .unwrap();
        assert_eq!(
            device
                .value("string-constraint-string-list")
                .unwrap()
                .as_str(),
            Some("Second entry")
        );

        let len = device.option("int-constraint-array").unwrap().len();
        let values: Vec<i32> = (0..len as i32).collect();
        device
            .set_value("int-constraint-array", values.clone())
            .unwrap();
        assert_eq!(
            device.value("int-constraint-array").unwrap(),
            Value::IntArray(values)
        );

        let info = device.set_value("int-inexact", 100).unwrap();
        assert!(info.inexact());

        device.press("button").unwrap();
        device
            .set_auto("bool-soft-select-soft-detect-auto")
            .unwrap();
    });
}

#[test]
fn rejects_invalid_values() {
    with_sane(|sane| {
        let mut device = sane.open("test:0").unwrap();
        assert!(matches!(
            device.set_value("int", "text"),
            Err(Error::TypeMismatch {
                expected: ValueType::Int,
                ..
            })
        ));
        assert!(matches!(
            device.set_value("int-constraint-array", 1),
            Err(Error::InvalidValue { .. })
        ));
        let size = device.option("string").unwrap().size;
        assert!(matches!(
            device.set_value("string", "x".repeat(size)),
            Err(Error::InvalidValue { .. })
        ));
        assert_eq!(
            device.set_value("mode", "Sepia").unwrap_err(),
            Error::Invalid
        );
        assert!(
            matches!(device.set_value("no-such-option", 1), Err(Error::UnknownOption(name)) if name == "no-such-option")
        );
        assert!(matches!(
            device.value("button"),
            Err(Error::WriteOnlyOption(_))
        ));
        assert!(matches!(
            device.press("int"),
            Err(Error::TypeMismatch { .. })
        ));
        // Inactive in gray mode.
        assert!(matches!(
            device.set_value("three-pass", true),
            Err(Error::InactiveOption(_))
        ));
        assert!(matches!(
            device.value("three-pass"),
            Err(Error::InactiveOption(_))
        ));
    });
}

#[test]
fn reloads_options_when_the_backend_asks() {
    with_sane(|sane| {
        let mut device = sane.open("test:0").unwrap();
        let before = device.option("three-pass").unwrap().clone();
        assert!(!before.is_active());

        let info = device
            .set_value(well_known::MODE, well_known::MODE_COLOR)
            .unwrap();
        assert!(info.reload_options());
        assert!(info.reload_params());
        // The device reloaded its descriptors: three-pass is now active.
        assert!(device.option("three-pass").unwrap().is_active());
        device.set_value("three-pass", true).unwrap();

        device.reload_options().unwrap();
        assert!(device.option("three-pass").unwrap().is_active());
    });
}

#[test]
fn scans_a_gray_frame() {
    with_sane(|sane| {
        let mut device = sane.open("test:0").unwrap();
        let estimate = device.parameters().unwrap();
        let frame = device.scan().unwrap();
        let parameters = frame.parameters;
        assert_eq!(parameters.format, FrameFormat::Gray);
        assert_eq!(parameters.depth, 8);
        assert!(parameters.last_frame);
        assert_eq!(parameters.pixels_per_line, estimate.pixels_per_line);
        assert_eq!(parameters.lines, estimate.lines);
        assert_eq!(frame.data.len(), parameters.bytes_per_line * frame.height());
        // Solid black.
        assert!(frame.data.iter().all(|&byte| byte == 0));

        // The device can scan again.
        device.set_value("test-picture", "Solid white").unwrap();
        let frame = device.scan().unwrap();
        assert!(frame.data.iter().all(|&byte| byte == 0xff));
    });
}

#[test]
fn reads_in_chunks_and_cancels() {
    with_sane(|sane| {
        let mut device = sane.open("test:0").unwrap();
        device.set_value("read-limit", true).unwrap();
        device.set_value("read-limit-size", 100).unwrap();

        let mut scan = device.start().unwrap();
        let total = scan.parameters().bytes_per_line * scan.parameters().lines.unwrap();
        let mut chunk = [0; 1000];
        let len = scan.read(&mut chunk).unwrap();
        assert!(len > 0 && len <= 100);
        // `io::Read` reads the rest of the frame.
        let mut rest = Vec::new();
        scan.read_to_end(&mut rest).unwrap();
        assert_eq!(len + rest.len(), total);
        assert!(scan.is_frame_done());
        assert_eq!(scan.read(&mut chunk).unwrap(), 0);
        assert!(!scan.next_frame().unwrap());
        drop(scan);

        // Cancel in the middle of a frame, then scan again.
        let mut scan = device.start().unwrap();
        assert!(scan.read(&mut chunk).unwrap() > 0);
        assert!(matches!(scan.next_frame(), Err(Error::InvalidState(_))));
        scan.cancel();
        assert_eq!(device.scan().unwrap().data.len(), total);
    });
}

#[test]
fn scans_color_at_every_depth() {
    with_sane(|sane| {
        let mut device = sane.open("test:0").unwrap();
        device
            .set_value(well_known::MODE, well_known::MODE_COLOR)
            .unwrap();
        device.set_value("test-picture", "Color pattern").unwrap();
        for depth in [1, 8, 16] {
            device.set_value(well_known::DEPTH, depth).unwrap();
            let frame = device.scan().unwrap();
            assert_eq!(frame.parameters.format, FrameFormat::Rgb);
            assert_eq!(frame.parameters.depth, depth as u32);
            // 1-bit lines hold one byte per channel for each group of 8 pixels.
            let expected = match depth {
                1 => frame.width().div_ceil(8) * 3,
                _ => frame.width() * 3 * depth as usize / 8,
            };
            assert_eq!(frame.parameters.bytes_per_line, expected);
            assert_eq!(
                frame.data.len(),
                frame.parameters.bytes_per_line * frame.height()
            );
        }
    });
}

/// The samples of every line, without the padding bits at the end of 1-bit
/// lines (backends may leave them set).
fn pixels(frame: &libsane::Frame) -> Vec<Vec<u8>> {
    let width = frame.width();
    (0..frame.height())
        .map(|y| {
            let mut line = frame.line(y).to_vec();
            if frame.parameters.depth == 1 && width % 8 != 0 {
                let channels = frame.parameters.format.channels();
                let last = width / 8 * channels;
                for byte in &mut line[last..] {
                    *byte &= !(0xff >> (width % 8));
                }
            }
            line
        })
        .collect()
}

#[test]
fn merges_three_pass_scans() {
    with_sane(|sane| {
        let mut device = sane.open("test:0").unwrap();
        device
            .set_value(well_known::MODE, well_known::MODE_COLOR)
            .unwrap();
        device.set_value("test-picture", "Color pattern").unwrap();
        for depth in [1, 8, 16] {
            device.set_value(well_known::DEPTH, depth).unwrap();
            device.set_value("three-pass", false).unwrap();
            let single_pass = device.scan().unwrap();

            device.set_value("three-pass", true).unwrap();
            device.set_value("three-pass-order", "GBR").unwrap();
            let scan = device.start().unwrap();
            assert_eq!(scan.parameters().format, FrameFormat::Green);
            assert!(!scan.parameters().last_frame);
            drop(scan);

            let three_pass = device.scan().unwrap();
            assert_eq!(three_pass.parameters.format, FrameFormat::Rgb);
            assert!(three_pass.parameters.last_frame);
            assert_eq!(
                three_pass.parameters, single_pass.parameters,
                "depth {depth}"
            );
            assert_eq!(pixels(&three_pass), pixels(&single_pass), "depth {depth}");
        }
    });
}

#[test]
fn handles_unknown_line_counts() {
    with_sane(|sane| {
        let mut device = sane.open("test:0").unwrap();
        device.set_value("hand-scanner", true).unwrap();
        assert_eq!(device.parameters().unwrap().lines, None);
        let frame = device.scan().unwrap();
        assert!(frame.height() > 0);
        assert_eq!(
            frame.data.len(),
            frame.parameters.bytes_per_line * frame.height()
        );
    });
}

#[test]
fn maps_read_statuses_to_errors() {
    with_sane(|sane| {
        let mut device = sane.open("test:0").unwrap();
        let cases = [
            ("SANE_STATUS_JAMMED", Error::Jammed),
            ("SANE_STATUS_NO_DOCS", Error::NoDocs),
            ("SANE_STATUS_COVER_OPEN", Error::CoverOpen),
            ("SANE_STATUS_IO_ERROR", Error::Io),
            ("SANE_STATUS_NO_MEM", Error::NoMem),
            ("SANE_STATUS_ACCESS_DENIED", Error::AccessDenied),
            ("SANE_STATUS_DEVICE_BUSY", Error::DeviceBusy),
            ("SANE_STATUS_UNSUPPORTED", Error::Unsupported),
            ("SANE_STATUS_CANCELLED", Error::Cancelled),
            ("SANE_STATUS_INVAL", Error::Invalid),
        ];
        for (status, error) in cases {
            device.set_value("read-return-value", status).unwrap();
            assert_eq!(device.scan().unwrap_err(), error, "{status}");
        }
        // EOF ends the frame at once: an empty image.
        device
            .set_value("read-return-value", "SANE_STATUS_EOF")
            .unwrap();
        assert_eq!(device.scan().unwrap().height(), 0);
    });
}

#[test]
fn feeds_pages_until_the_feeder_is_empty() {
    with_sane(|sane| {
        let mut device = sane.open("test:0").unwrap();
        device
            .set_value(well_known::SOURCE, "Automatic Document Feeder")
            .unwrap();
        let mut scan = device.start().unwrap();
        let mut pages = 0;
        loop {
            scan.read_image().unwrap();
            pages += 1;
            if !scan.next_page().unwrap() {
                break;
            }
        }
        // The test backend's feeder holds 10 pages.
        assert_eq!(pages, 10);
    });
}

#[test]
fn authorization_callback_is_installed() {
    let _guard = lock();
    let sane =
        Sane::init_with_auth(|_resource| Some(libsane::Credentials::new("user", "password")))
            .unwrap();
    assert_eq!(Sane::init().unwrap_err(), Error::AlreadyInitialized);
    drop(sane);
}

#[cfg(feature = "image")]
#[test]
fn converts_frames_to_images() {
    with_sane(|sane| {
        let mut device = sane.open("test:0").unwrap();
        device.set_value("test-picture", "Grid").unwrap();
        let frame = device.scan().unwrap();
        let image = frame.to_image().unwrap();
        assert_eq!(image.width() as usize, frame.width());
        assert_eq!(image.height() as usize, frame.height());
        assert!(matches!(image, image::DynamicImage::ImageLuma8(_)));

        device
            .set_value(well_known::MODE, well_known::MODE_COLOR)
            .unwrap();
        device.set_value(well_known::DEPTH, 16).unwrap();
        let image = device.scan().unwrap().to_image().unwrap();
        assert!(matches!(image, image::DynamicImage::ImageRgb16(_)));

        device
            .set_value(well_known::MODE, well_known::MODE_GRAY)
            .unwrap();
        device.set_value(well_known::DEPTH, 1).unwrap();
        let image = device.scan().unwrap().to_image().unwrap().into_luma8();
        // The grid has black and white squares.
        assert!(image.pixels().any(|pixel| pixel.0 == [0]));
        assert!(image.pixels().any(|pixel| pixel.0 == [255]));
    });
}