tethr
Version:
Controlls USB-connected cameras, webcam, and smartphone camera from browser
72 lines • 1.99 kB
JavaScript
export function toHexString(data, bytes = 'auto') {
if (typeof data === 'number') {
const hex = data.toString(16);
let length;
if (bytes === 'auto') {
length = hex.length;
}
else {
length = bytes;
}
return '0x' + data.toString(16).padStart(length, '0');
}
else {
const arr = [...new Uint8Array(data)];
return arr.map(byte => byte.toString(16).padStart(2, '0')).join(' ');
}
}
export function isntNil(value) {
return value != null;
}
export function sliceJpegData(buffer) {
const bytes = new Uint8Array(buffer);
const len = bytes.length;
// look for the JPEG SOI marker (0xFFD8) in data
let start = null;
for (let i = 0; i + 1 < len; i++) {
if (bytes[i] === 0xff && bytes[i + 1] === 0xd8) {
// SOI found
start = i;
break;
}
}
if (start === null) /* no SOI -> no JPEG */
throw new Error('SOI not found');
// look for the JPEG SOI marker (0xFFD8) in data
let end = null;
for (let i = start + 2; i + 1 < len; i++) {
if (bytes[i] === 0xff && bytes[i + 1] === 0xd9) {
// EOI found
end = i + 2;
break;
}
}
if (end === null)
// no EOI -> no JPEG
throw new Error('EOI not found');
return buffer.slice(start, end);
}
export function computeShutterSpeedSeconds(ss) {
if (ss === 'bulb' || ss === 'sync') {
return Infinity;
}
if (ss.includes('/')) {
const [fraction, denominator] = ss.split('/');
return parseInt(fraction) / parseInt(denominator);
}
return parseFloat(ss);
}
export const UnsupportedConfigDesc = {
writable: false,
value: null,
};
export const UnsupportedOperationResult = {
status: 'unsupported',
};
export function readonlyConfigDesc(value) {
return {
writable: false,
value,
};
}
//# sourceMappingURL=util.js.map