sane-wasm 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE-LGPL.txt +504 -0
- package/LICENSE.txt +339 -0
- package/README.md +87 -0
- package/build/libsane.html +357 -0
- package/build/libsane.js +8581 -0
- package/build/libsane.wasm +0 -0
- package/build/libsane.worker.js +204 -0
- package/libsane.d.ts +163 -0
- package/package.json +28 -0
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<!--
|
|
3
|
+
SANE WebAssembly (sane-wasm)
|
|
4
|
+
Copyright (C) 2023 Gonçalo MB <me@goncalomb.com>
|
|
5
|
+
GNU GPLv2 + GNU LGPLv2.1
|
|
6
|
+
-->
|
|
7
|
+
<html lang="en">
|
|
8
|
+
<head>
|
|
9
|
+
<meta charset="utf-8">
|
|
10
|
+
<title>Test Page for SANE WebAssembly (sane-wasm)</title>
|
|
11
|
+
<style>
|
|
12
|
+
body {
|
|
13
|
+
font-family: sans-serif;
|
|
14
|
+
max-width: 950px;
|
|
15
|
+
margin: 0 auto;
|
|
16
|
+
}
|
|
17
|
+
canvas {
|
|
18
|
+
border: 2px solid #555;
|
|
19
|
+
margin-bottom: 25px;
|
|
20
|
+
}
|
|
21
|
+
</style>
|
|
22
|
+
</head>
|
|
23
|
+
<body>
|
|
24
|
+
<header>
|
|
25
|
+
<h1>Test Page for SANE WebAssembly (sane-wasm)</h1>
|
|
26
|
+
<p>This project (sane-wasm) is a WebAssembly port of the <a href="http://sane-project.org/">SANE API</a> for in-browser scanning (USB scanners).</p>
|
|
27
|
+
</header>
|
|
28
|
+
<main>
|
|
29
|
+
<ul>
|
|
30
|
+
<li><strong>This is a test page.</strong> It's not designed for final image acquisition.</li>
|
|
31
|
+
<li>This project uses some of the latest web features, a modern browser is required.</li>
|
|
32
|
+
<li>Using this page requires understanding the <a href="https://sane-project.gitlab.io/standard/api.html#code-flow">SANE Code Flow</a> and the overall SANE library.</li>
|
|
33
|
+
<li>If you reload/leave the page without clearing the scanner state (e.g. closing the page while scanning) you might need to reset the device (i.e. reconnecting).</li>
|
|
34
|
+
<li>There are some safeguards in place, but it might still be possible to lock the browser window or the scanner.</li>
|
|
35
|
+
<li>Check the console for useful information.</li>
|
|
36
|
+
</ul>
|
|
37
|
+
<h2>Device Pairing (WebUSB)</h2>
|
|
38
|
+
<p>
|
|
39
|
+
<button id="btn-request-device">navigator.usb.requestDevice()</button>
|
|
40
|
+
<button id="btn-request-device-all">(no filters)</button>
|
|
41
|
+
</p>
|
|
42
|
+
<ul id="list-paired-devices"></ul>
|
|
43
|
+
<h2>LibSANE (sane-wasm)</h2>
|
|
44
|
+
<div id="sane-controls">Loading LibSANE (sane-wasm)...</div>
|
|
45
|
+
<h2>Scan Result</h2>
|
|
46
|
+
<canvas></canvas>
|
|
47
|
+
</main>
|
|
48
|
+
<script async type="text/javascript" src="libsane.js"></script>
|
|
49
|
+
<script>
|
|
50
|
+
(function() {
|
|
51
|
+
// util function to build the dom
|
|
52
|
+
const tree = (data, parent = document.createDocumentFragment()) => {
|
|
53
|
+
(Array.isArray(data) ? data : [data]).filter(spec => spec).map(spec => {
|
|
54
|
+
if (!Array.isArray(spec)) return document.createTextNode(spec.toString());
|
|
55
|
+
const el = spec[0] ? document.createElement(spec[0]) : document.createDocumentFragment();
|
|
56
|
+
Object.keys(spec[1] || {}).forEach(attr => el[attr] = spec[1][attr]);
|
|
57
|
+
return tree(spec[2] || [], el);
|
|
58
|
+
}).forEach(el => parent.appendChild(el));
|
|
59
|
+
return parent;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// check support
|
|
63
|
+
if (!navigator || !navigator.usb) {
|
|
64
|
+
const main = document.querySelector("main");
|
|
65
|
+
main.innerHTML = "";
|
|
66
|
+
main.append(tree([
|
|
67
|
+
["p", null, [
|
|
68
|
+
["strong", null, "Browser not supported!"],
|
|
69
|
+
" Missing ",
|
|
70
|
+
["a", { href: "https://developer.mozilla.org/en-US/docs/Web/API/WebUSB_API" }, "WebUSB API"],
|
|
71
|
+
".",
|
|
72
|
+
]]
|
|
73
|
+
]));
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/* Device Pairing */
|
|
78
|
+
|
|
79
|
+
function updatePairedDevices() {
|
|
80
|
+
const listPairedDevices = document.querySelector("#list-paired-devices");
|
|
81
|
+
listPairedDevices.innerHTML = "";
|
|
82
|
+
navigator.usb.getDevices().then(devices => {
|
|
83
|
+
listPairedDevices.append(tree(devices.map(device => (
|
|
84
|
+
["li", null, `${device.productName} (${device.manufacturerName}) [${device.serialNumber}]`]
|
|
85
|
+
))));
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function requestDevice(filters = false) {
|
|
90
|
+
navigator.usb.requestDevice({
|
|
91
|
+
filters: filters ? [
|
|
92
|
+
{ classCode: 0x07 }, // printer
|
|
93
|
+
] : []
|
|
94
|
+
}).finally(() => updatePairedDevices());
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
navigator.usb.addEventListener("connect", e => updatePairedDevices());
|
|
98
|
+
navigator.usb.addEventListener("disconnect", e => updatePairedDevices());
|
|
99
|
+
updatePairedDevices();
|
|
100
|
+
|
|
101
|
+
document.querySelector("#btn-request-device").addEventListener("click", e => requestDevice(true));
|
|
102
|
+
document.querySelector("#btn-request-device-all").addEventListener("click", e => requestDevice());
|
|
103
|
+
|
|
104
|
+
/* SANE */
|
|
105
|
+
|
|
106
|
+
const strstatusCache = {};
|
|
107
|
+
|
|
108
|
+
function callSANE(name, ...args) {
|
|
109
|
+
const res = LibSANE[name](...args);
|
|
110
|
+
return res instanceof Promise ? res : Promise.resolve(res);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// XXX: improve this
|
|
114
|
+
async function testScan() {
|
|
115
|
+
const state = await callSANE("sane_get_state");
|
|
116
|
+
if (!state.open) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
console.log("TEST SCAN START");
|
|
121
|
+
|
|
122
|
+
await callSANE("sane_start");
|
|
123
|
+
const { parameters: params } = await callSANE("sane_get_parameters");;
|
|
124
|
+
|
|
125
|
+
const canvas = document.querySelector("canvas");
|
|
126
|
+
canvas.width = params.pixels_per_line;
|
|
127
|
+
canvas.height = params.lines;
|
|
128
|
+
const ctx = canvas.getContext("2d");
|
|
129
|
+
|
|
130
|
+
const reader = (callback) => new Promise((resolve, reject) => {
|
|
131
|
+
try {
|
|
132
|
+
const read = async () => {
|
|
133
|
+
const { status, data } = await callSANE("sane_read");
|
|
134
|
+
if (status == 5) { // EOF
|
|
135
|
+
resolve();
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (status != 0) {
|
|
139
|
+
reject();
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (data.length) {
|
|
143
|
+
callback(data);
|
|
144
|
+
}
|
|
145
|
+
setTimeout(read, data.length > 0 ? 10 : 200);
|
|
146
|
+
} // XXX: uncaught exception
|
|
147
|
+
setTimeout(read, 200);
|
|
148
|
+
} catch (e) {
|
|
149
|
+
reject(e);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
let line = 0;
|
|
154
|
+
let rem = new Uint8ClampedArray();
|
|
155
|
+
reader(data => {
|
|
156
|
+
// TUDO: detect and support other frame types
|
|
157
|
+
const lc = Math.floor((rem.length + data.length) / (3 * params.pixels_per_line));
|
|
158
|
+
|
|
159
|
+
const idata = new Uint8ClampedArray(rem.length + data.length);
|
|
160
|
+
idata.set(rem, 0);
|
|
161
|
+
idata.set(data, rem.length);
|
|
162
|
+
|
|
163
|
+
if (lc) {
|
|
164
|
+
const odata = new Uint8ClampedArray(lc * params.pixels_per_line * 4);
|
|
165
|
+
var i = 0;
|
|
166
|
+
for (var j = 0; j < odata.length; i += 3, j += 4) {
|
|
167
|
+
odata.set(idata.subarray(i, i + 3), j);
|
|
168
|
+
odata[j + 3] = 0xff;
|
|
169
|
+
}
|
|
170
|
+
rem = idata.subarray(i);
|
|
171
|
+
ctx.putImageData(new ImageData(odata, params.pixels_per_line), 0, line);
|
|
172
|
+
line += lc;
|
|
173
|
+
} else {
|
|
174
|
+
rem = idata;
|
|
175
|
+
}
|
|
176
|
+
}).finally(() => {
|
|
177
|
+
callSANE("sane_cancel").finally(() => {
|
|
178
|
+
console.log("TEST SCAN STOP");
|
|
179
|
+
})
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function populateDevices({ devices }) {
|
|
184
|
+
const listDevices = document.querySelector("#list-devices");
|
|
185
|
+
listDevices.innerHTML = "";
|
|
186
|
+
listDevices.append(tree(devices.map(device => (
|
|
187
|
+
["li", null, [
|
|
188
|
+
["button", { onclick: e => callSANE("sane_open", device.name).then(populateOptions) }, "LibSANE.sane_open(...)"], " ",
|
|
189
|
+
`${device.model} (${device.vendor}) [${device.name}] [${device.type}]`
|
|
190
|
+
]]
|
|
191
|
+
))));
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function populateOptions() {
|
|
195
|
+
const state = await callSANE("sane_get_state");
|
|
196
|
+
if (!state.open) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const auto = Symbol("auto");
|
|
200
|
+
async function createOptionControl(n, opt) {
|
|
201
|
+
if (opt.cap.INACTIVE) {
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
async function setValue(value) {
|
|
205
|
+
const { status, info } = value === auto ? await callSANE("sane_control_option_set_auto", n) : await callSANE("sane_control_option_set_value", n, value);
|
|
206
|
+
if (status == LibSANE.SANE_STATUS.GOOD) {
|
|
207
|
+
if (info.RELOAD_OPTIONS) {
|
|
208
|
+
populateOptions();
|
|
209
|
+
} else /*if (info.INEXACT)*/ { // always reload
|
|
210
|
+
await createOptionControl(n, opt);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
function inputElement(value) {
|
|
215
|
+
function selectElement(constraint, number = false) {
|
|
216
|
+
return ["select", { onchange: e => setValue(number ? Number(e.target.value) : e.target.value) },
|
|
217
|
+
constraint.map(v => (
|
|
218
|
+
["option", { value: v, selected: v == value ? true : null }, v]
|
|
219
|
+
))
|
|
220
|
+
];
|
|
221
|
+
}
|
|
222
|
+
switch (opt.type) {
|
|
223
|
+
case LibSANE.SANE_TYPE.BOOL: return ["input", { type: "checkbox", onchange: e => setValue(e.target.checked), checked: value }];
|
|
224
|
+
case LibSANE.SANE_TYPE.INT:
|
|
225
|
+
case LibSANE.SANE_TYPE.FIXED:
|
|
226
|
+
if (opt.size == 1 && opt.constraint_type == LibSANE.SANE_CONSTRAINT.WORD_LIST) {
|
|
227
|
+
return selectElement(opt.constraint, true);
|
|
228
|
+
} else {
|
|
229
|
+
return ["input", { onchange: e => setValue(JSON.parse(e.target.value)), value: JSON.stringify(value) }]
|
|
230
|
+
}
|
|
231
|
+
case LibSANE.SANE_TYPE.STRING:
|
|
232
|
+
if (opt.constraint_type == LibSANE.SANE_CONSTRAINT.STRING_LIST) {
|
|
233
|
+
return selectElement(opt.constraint);
|
|
234
|
+
} else {
|
|
235
|
+
return ["input", { onchange: e => setValue(JSON.parse(e.target.value)), value: JSON.stringify(value) }]
|
|
236
|
+
}
|
|
237
|
+
case LibSANE.SANE_TYPE.BUTTON: return ["button", { onclick: e => setValue(null) }, opt.title];
|
|
238
|
+
case LibSANE.SANE_TYPE.GROUP: return null;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
const result = [];
|
|
242
|
+
let value;
|
|
243
|
+
if (opt.cap.SOFT_DETECT) {
|
|
244
|
+
({ value } = await callSANE("sane_control_option_get_value", n));
|
|
245
|
+
result.push([null, null, JSON.stringify(value)]);
|
|
246
|
+
}
|
|
247
|
+
if (opt.cap.SOFT_SELECT) {
|
|
248
|
+
result.push(inputElement(value));
|
|
249
|
+
}
|
|
250
|
+
if (opt.cap.AUTOMATIC) {
|
|
251
|
+
result.push(["button", { onclick: e => setValue(auto) }, "AUTO"]);
|
|
252
|
+
}
|
|
253
|
+
const el = document.querySelector(`#option-${n}`);
|
|
254
|
+
el.innerHTML = "";
|
|
255
|
+
el.appendChild(tree(result));
|
|
256
|
+
}
|
|
257
|
+
const option_descriptors = [];
|
|
258
|
+
for (let opt, i = 0; opt || !i; i++) {
|
|
259
|
+
({ option_descriptor: opt } = await callSANE("sane_get_option_descriptor", i));
|
|
260
|
+
if (opt) {
|
|
261
|
+
option_descriptors.push(opt);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
const options = document.querySelector("#options");
|
|
265
|
+
options.innerHTML = "";
|
|
266
|
+
options.append(tree(option_descriptors.map((opt, n) => (
|
|
267
|
+
["div", null, [
|
|
268
|
+
["p", null, [
|
|
269
|
+
`${n}: ${opt.title} [name=${opt.name}]`, ["br"],
|
|
270
|
+
["small", null, opt.desc], ["br"],
|
|
271
|
+
["small", null, `[type=${LibSANE.SANE_TYPE.asString(opt.type)}] [unit=${LibSANE.SANE_UNIT.asString(opt.unit)}] [size=${opt.size}] [cap=${JSON.stringify(opt.cap)}]`], ["br"],
|
|
272
|
+
["small", null, `[constraint_type=${LibSANE.SANE_CONSTRAINT.asString(opt.constraint_type)}] [constraint=${JSON.stringify(opt.constraint)}]`],
|
|
273
|
+
]],
|
|
274
|
+
["div", { id: `option-${n}` }]
|
|
275
|
+
]]
|
|
276
|
+
))));
|
|
277
|
+
let n = 0;
|
|
278
|
+
for (const opt of option_descriptors) {
|
|
279
|
+
await createOptionControl(n++, opt);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// reload with debug
|
|
284
|
+
function reloadWithDebug() {
|
|
285
|
+
const sane = {
|
|
286
|
+
debugSANE: true,
|
|
287
|
+
debugUSB: true,
|
|
288
|
+
debugFunctionCalls: true,
|
|
289
|
+
debugTestDevices: 5,
|
|
290
|
+
};
|
|
291
|
+
location.hash = JSON.stringify(sane);
|
|
292
|
+
location.reload();
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// initialization
|
|
296
|
+
window.addEventListener("load", () => {
|
|
297
|
+
const sane = JSON.parse(decodeURIComponent(location.hash.substring(1)) || null) || {
|
|
298
|
+
debugFunctionCalls: true,
|
|
299
|
+
debugTestDevices: 3,
|
|
300
|
+
};
|
|
301
|
+
console.log("sane = ", sane);
|
|
302
|
+
window.LibSANE({ sane }).then(LibSANE => {
|
|
303
|
+
// publish module to window
|
|
304
|
+
window.LibSANE = LibSANE;
|
|
305
|
+
// initialize
|
|
306
|
+
const saneControls = document.querySelector("#sane-controls");
|
|
307
|
+
saneControls.innerHTML = "";
|
|
308
|
+
saneControls.append(tree([
|
|
309
|
+
["p", null, [
|
|
310
|
+
["button", { onclick: e => reloadWithDebug() }, "RELOAD WITH DEBUG (SANE+USB)"], " ",
|
|
311
|
+
["small", null, `${LibSANE.SANE_WASM_VERSION} (${LibSANE.SANE_WASM_COMMIT})`],
|
|
312
|
+
]],
|
|
313
|
+
["p", null, [
|
|
314
|
+
["small", null, [
|
|
315
|
+
["strong", null, `SANE backends (${LibSANE.SANE_WASM_BACKENDS.split(" ").length}): `],
|
|
316
|
+
LibSANE.SANE_WASM_BACKENDS
|
|
317
|
+
]],
|
|
318
|
+
]],
|
|
319
|
+
["h3", null, "Initialize"],
|
|
320
|
+
["p", null, [
|
|
321
|
+
["button", { onclick: e => callSANE("sane_get_state") }, "LibSANE.sane_get_state()"], " ",
|
|
322
|
+
["button", { onclick: e => callSANE("sane_init") }, "LibSANE.sane_init()"], " ",
|
|
323
|
+
["button", { onclick: e => callSANE("sane_exit") }, "LibSANE.sane_exit()"], " ",
|
|
324
|
+
["button", { onclick: e => callSANE("sane_strstatus", 999) }, "LibSANE.sane_strstatus(999)"], " ",
|
|
325
|
+
]],
|
|
326
|
+
["h3", null, "Devices"],
|
|
327
|
+
["p", null, [
|
|
328
|
+
["button", { onclick: e => callSANE("sane_get_devices").then(populateDevices) }, "LibSANE.sane_get_devices()"], " ",
|
|
329
|
+
]],
|
|
330
|
+
["ul", { id: "list-devices" }],
|
|
331
|
+
["p", null, [
|
|
332
|
+
["button", { onclick: e => callSANE("sane_get_option_descriptor", 0) }, "LibSANE.sane_get_option_descriptor(0)"], " ",
|
|
333
|
+
["button", { onclick: e => callSANE("sane_close") }, "LibSANE.sane_close()"], " ",
|
|
334
|
+
]],
|
|
335
|
+
["h3", null, "Options"],
|
|
336
|
+
["div", { id: "options" }],
|
|
337
|
+
["h3", null, "Scan"],
|
|
338
|
+
["p", null, [
|
|
339
|
+
["button", { onclick: e => callSANE("sane_get_parameters") }, "LibSANE.sane_get_parameters()"], " ",
|
|
340
|
+
]],
|
|
341
|
+
["p", null, [
|
|
342
|
+
["button", { onclick: e => callSANE("sane_start") }, "LibSANE.sane_start()"], " ",
|
|
343
|
+
["button", { onclick: e => callSANE("sane_read") }, "LibSANE.sane_read()"], " ",
|
|
344
|
+
["button", { onclick: e => callSANE("sane_cancel") }, "LibSANE.sane_cancel()"], " ",
|
|
345
|
+
]],
|
|
346
|
+
["p", null, [
|
|
347
|
+
["button", { onclick: e => testScan() }, "TEST SCAN"], " ",
|
|
348
|
+
]],
|
|
349
|
+
]));
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
})();
|
|
353
|
+
</script>
|
|
354
|
+
</body>
|
|
355
|
+
</html>
|
|
356
|
+
|
|
357
|
+
|