|
| 1 | +// @flow |
| 2 | + |
| 3 | +module.exports = (WASM_INIT) => { |
| 4 | + let wasm = WASM_INIT() |
| 5 | + |
| 6 | + function isWasmLoaded() { |
| 7 | + return Boolean(wasm.setImageSize) // any property works |
| 8 | + } |
| 9 | + |
| 10 | + const mod = { unresolvedCalls: [] } |
| 11 | + |
| 12 | + async function resolveCalls() { |
| 13 | + while (mod.unresolvedCalls.length > 0) { |
| 14 | + const { f, args, resolve, reject } = mod.unresolvedCalls[0] |
| 15 | + mod.unresolvedCalls.shift() |
| 16 | + await f(...args) |
| 17 | + .then(resolve) |
| 18 | + .catch(reject) |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + function eventuallyResolve(f) { |
| 23 | + return (...args) => { |
| 24 | + if (isWasmLoaded()) { |
| 25 | + return resolveCalls().then(() => { |
| 26 | + return f(...args) |
| 27 | + }) |
| 28 | + } else { |
| 29 | + return new Promise((resolve, reject) => { |
| 30 | + mod.unresolvedCalls.push({ f, args, resolve, reject }) |
| 31 | + }) |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + let checkWasmLoadedInterval = setInterval(() => { |
| 37 | + if (isWasmLoaded()) { |
| 38 | + clearInterval(checkWasmLoadedInterval) |
| 39 | + resolveCalls() |
| 40 | + } |
| 41 | + }, 100) |
| 42 | + |
| 43 | + mod.defaultConfig = { |
| 44 | + mode: "autoseg", |
| 45 | + maxClusters: 1000, |
| 46 | + classColors: [ |
| 47 | + 0x88000000, |
| 48 | + 2285257716, |
| 49 | + 2297665057, |
| 50 | + 2286989132, |
| 51 | + 2281729263, |
| 52 | + 2286441849, |
| 53 | + 2285412200, |
| 54 | + 2288197353, |
| 55 | + 2293245852, |
| 56 | + 2293584191, |
| 57 | + 2290652672, |
| 58 | + 2285493453, |
| 59 | + 2290842976, |
| 60 | + ], |
| 61 | + classNames: [], |
| 62 | + } |
| 63 | + mod.config = { ...mod.defaultConfig } |
| 64 | + |
| 65 | + mod.setConfig = eventuallyResolve(async (config) => { |
| 66 | + mod.config = { ...mod.defaultConfig, ...config } |
| 67 | + }) |
| 68 | + mod.loadImage = eventuallyResolve(async (imageData) => { |
| 69 | + wasm.setSimpleMode(mod.config.mode === "simple") |
| 70 | + wasm.setMaxClusters(mod.config.maxClusters) |
| 71 | + wasm.setImageSize(imageData.width, imageData.height) |
| 72 | + mod.config.imageSize = { width: imageData.width, height: imageData.height } |
| 73 | + for (let i = 0; i < mod.config.classColors.length; i++) { |
| 74 | + wasm.setClassColor(i, mod.config.classColors[i]) |
| 75 | + } |
| 76 | + const imageAddress = wasm.getImageAddr() |
| 77 | + wasm.HEAPU8.set(imageData.data, imageAddress) |
| 78 | + wasm.computeSuperPixels() |
| 79 | + mod.imageLoaded = true |
| 80 | + }) |
| 81 | + mod.getMask = eventuallyResolve(async (objects) => { |
| 82 | + wasm.clearClassElements() |
| 83 | + const { width, height } = mod.config.imageSize |
| 84 | + // convert bounding boxes to polygons |
| 85 | + objects = objects.map((r) => { |
| 86 | + if (r.regionType !== "bounding-box") return r |
| 87 | + return { |
| 88 | + regionType: "polygon", |
| 89 | + cls: r.cls, |
| 90 | + points: [ |
| 91 | + { x: r.x, y: r.y }, |
| 92 | + { x: r.x + r.w, y: r.y }, |
| 93 | + { x: r.x + r.w, y: r.y + r.h }, |
| 94 | + { x: r.x, y: r.y + r.h }, |
| 95 | + ], |
| 96 | + } |
| 97 | + }) |
| 98 | + for (let object of objects) { |
| 99 | + const clsIndex = |
| 100 | + typeof object.cls === "number" |
| 101 | + ? object.cls |
| 102 | + : mod.config.classNames.indexOf(object.cls) |
| 103 | + if (clsIndex > mod.config.classColors.length || clsIndex === -1) { |
| 104 | + continue |
| 105 | + } |
| 106 | + |
| 107 | + switch (object.regionType) { |
| 108 | + case "polygon": { |
| 109 | + const { points } = object |
| 110 | + const pi = wasm.addPolygon(clsIndex) |
| 111 | + const pointPairs = points.map((p, i) => [ |
| 112 | + p, |
| 113 | + points[(i + 1) % points.length], |
| 114 | + ]) |
| 115 | + for (const [p1, p2] of pointPairs) { |
| 116 | + const ri1 = Math.round(p1.y * height) |
| 117 | + const ci1 = Math.round(p1.x * width) |
| 118 | + const ri2 = Math.round(p2.y * height) |
| 119 | + const ci2 = Math.round(p2.x * width) |
| 120 | + wasm.addLineToPolygon(pi, ri1, ci1, ri2, ci2) |
| 121 | + } |
| 122 | + break |
| 123 | + } |
| 124 | + case "point": { |
| 125 | + const { x, y } = object |
| 126 | + if (x < 0 || x >= 1) continue |
| 127 | + if (y < 0 || y >= 1) continue |
| 128 | + |
| 129 | + wasm.addClassPoint( |
| 130 | + clsIndex, |
| 131 | + Math.floor(y * mod.config.height), |
| 132 | + Math.floor(x * mod.config.width) |
| 133 | + ) |
| 134 | + break |
| 135 | + } |
| 136 | + default: { |
| 137 | + continue |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + wasm.computeMasks() |
| 143 | + const maskAddress = wasm.getColoredMask() |
| 144 | + const cppImDataUint8 = new Uint8ClampedArray( |
| 145 | + wasm.HEAPU8.buffer, |
| 146 | + maskAddress, |
| 147 | + width * height * 4 |
| 148 | + ) |
| 149 | + |
| 150 | + if (typeof ImageData !== "undefined") { |
| 151 | + // Browser |
| 152 | + return new ImageData(cppImDataUint8, width, height) |
| 153 | + } else { |
| 154 | + // NodeJS |
| 155 | + return { data: cppImDataUint8, width, height } |
| 156 | + } |
| 157 | + }) |
| 158 | + |
| 159 | + return mod |
| 160 | +} |
0 commit comments