Skip to content

Commit

Permalink
zft for typescript simd
Browse files Browse the repository at this point in the history
  • Loading branch information
kalwalt committed Nov 21, 2024
1 parent 874e625 commit 1a74b61
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 23 deletions.
2 changes: 1 addition & 1 deletion dist/ARToolkitNFT_simd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/artoolkitNFT_ES6.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const browser = (function () {
if (browser == "Apple Safari") {
importScripts("../dist/ARToolkitNFT.js");
} else {
importScripts("../dist/ARToolkitNFT.js");
importScripts("../dist/ARToolkitNFT_simd.js");
}
// Import OneEuroFilter class into the worker.
importScripts("./one-euro-filter.js");
Expand Down
157 changes: 136 additions & 21 deletions src/ARToolkitNFT_simd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,45 @@ export class ARToolkitNFT implements IARToolkitNFT {
onError2(errorNumber);
};

const loadZFT = (prefix: any) => {
const marker_num = prefix.substring(11);
const prefixTemp = "/tempMarkerNFT_" + marker_num;

const response = this.instance._decompressZFT(prefix, prefixTemp);

let contentIsetUint8 = this.FS.readFile(prefixTemp + ".iset");
let contentFsetUint8 = this.FS.readFile(prefixTemp + ".fset");
let contentFset3Uint8 = this.FS.readFile(prefixTemp + ".fset3");

this.FS.unlink(prefixTemp + ".iset");
this.FS.unlink(prefixTemp + ".fset");
this.FS.unlink(prefixTemp + ".fset3");

let hexStrIset = this.Uint8ArrayToStr(contentIsetUint8);
let hexStrFset = this.Uint8ArrayToStr(contentFsetUint8);
let hexStrFset3 = this.Uint8ArrayToStr(contentFset3Uint8);

let contentIset = new Uint8Array(
hexStrIset.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)),
);
let contentFset = new Uint8Array(
hexStrFset.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)),
);
let contentFset3 = new Uint8Array(
hexStrFset3.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)),
);

this._storeDataFile(contentFset, prefix + ".fset");
this._storeDataFile(contentIset, prefix + ".iset");
this._storeDataFile(contentFset3, prefix + ".fset3");
onSuccess(contentFset);
};

const onSuccessZFT = function () {
loadZFT(arguments[1]);
//onSuccess(contentFset);
};

let Ids: Array<number> = [];

urls.forEach((element, index) => {
Expand All @@ -387,6 +426,7 @@ export class ARToolkitNFT implements IARToolkitNFT {
filename,
onSuccess.bind(filename),
onError.bind(filename),
prefix,
);
});

Expand All @@ -395,25 +435,41 @@ export class ARToolkitNFT implements IARToolkitNFT {
var filename1 = prefix + ".fset";
var filename2 = prefix + ".iset";
var filename3 = prefix + ".fset3";
const filename4 = prefix + ".zft";

this.ajax(
element + ".fset",
filename1,
onSuccess.bind(filename1),
onError.bind(filename1),
);
this.ajax(
element + ".iset",
filename2,
onSuccess.bind(filename2),
onError.bind(filename2),
);
this.ajax(
element + ".fset3",
filename3,
onSuccess.bind(filename3),
onError.bind(filename3),
);
let type = this.checkZFT(element + ".zft");
if (type) {
pending -= 2;
this.ajax(
element + ".zft",
filename4,
onSuccessZFT.bind(filename4),
onError.bind(filename4),
prefix,
);
} else {
this.ajax(
element + ".fset",
filename1,
onSuccess.bind(filename1),
onError.bind(filename1),
prefix,
);
this.ajax(
element + ".iset",
filename2,
onSuccess.bind(filename2),
onError.bind(filename2),
prefix,
);
this.ajax(
element + ".fset3",
filename3,
onSuccess.bind(filename3),
onError.bind(filename3),
prefix,
);
}

this.markerNFTCount += 1;
}
Expand All @@ -424,6 +480,63 @@ export class ARToolkitNFT implements IARToolkitNFT {
return Ids;
}

private checkZFT(url: any) {
let isZFT = null;

let request = new XMLHttpRequest();
request.open("GET", url, false); // `false` makes the request synchronous
request.send(null);

if (request.status === 200) {
isZFT = true;
} else if (request.status === 404) {
isZFT = false;
}

return isZFT;
}

private Uint8ArrayToStr(array: any) {
let out, i, len, c;
let char2, char3;

out = "";
len = array.length;
i = 0;
while (i < len) {
c = array[i++];
switch (c >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12:
case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode(((c & 0x1f) << 6) | (char2 & 0x3f));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(
((c & 0x0f) << 12) | ((char2 & 0x3f) << 6) | ((char3 & 0x3f) << 0),
);
break;
}
}

return out;
}

// ---------------------------------------------------------------------------

// implementation
Expand Down Expand Up @@ -451,24 +564,26 @@ export class ARToolkitNFT implements IARToolkitNFT {
target: string,
callback: (byteArray: Uint8Array) => void,
errorCallback: (url: string, message: number) => void,
prefix: string,
) {
var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer"; // blob arraybuffer
const writeByteArrayToFS = (
target: string,
byteArray: Uint8Array,
callback: (byteArray: Uint8Array) => void,
callback: (byteArray: Uint8Array, prefix: string) => void,
prefix: string,
) => {
this.FS.writeFile(target, byteArray, { encoding: "binary" });
callback(byteArray);
callback(byteArray, prefix);
};

oReq.onload = function () {
if (this.status == 200) {
var arrayBuffer = oReq.response;
var byteArray = new Uint8Array(arrayBuffer);
writeByteArrayToFS(target, byteArray, callback);
writeByteArrayToFS(target, byteArray, callback, prefix);
} else {
errorCallback(url, this.status);
}
Expand Down
2 changes: 2 additions & 0 deletions types/src/ARToolkitNFT_simd.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export declare class ARToolkitNFT implements IARToolkitNFT {
passVideoData(videoFrame: Uint8ClampedArray, videoLuma: Uint8Array): void;
loadCamera(urlOrData: Uint8Array | string): Promise<number>;
addNFTMarkers(urls: Array<string | Array<string>>, callback: (filename: number[]) => void, onError2: (errorNumber: number) => void): Array<number>;
private checkZFT;
private Uint8ArrayToStr;
private _storeDataFile;
private ajax;
}

0 comments on commit 1a74b61

Please sign in to comment.