Skip to content
Snippets Groups Projects
Commit a731e159 authored by Jake Read's avatar Jake Read
Browse files

workers

parent 2b5f62c4
Branches
No related tags found
No related merge requests found
......@@ -11,9 +11,8 @@ import {
State
} from '../hunks.js'
let computedResult = null
let running = false
function worker(){
'use strict'
let correlateGrayscale = (a, b, x, y) => {
// score a against b, starting b from x, y
// a, b are float64 arrays, pixel-wise
......@@ -57,14 +56,6 @@ let correlateRBGA = (a, b, x, y) => {
return sumDot / Math.sqrt(sumA * sumA * sumB * sumB)
}
let delay = (time) => {
return new Promise(resolve =>{
setTimeout(() => {
resolve()
}, time)
})
}
let packGrayscale = (imgD) => {
// make an md array shaped like[x][y]
let arr = []
......@@ -102,13 +93,11 @@ let packRBGA = (imgD) => {
return arr
}
async function run(a, b){
running = true
function run(a, b) {
// a is img to search for, b is img to search within. both are ImageData types
let resX = b.width - a.width
let resY = b.height - a.height
let numruns = resX * resY
console.log('numruns', numruns)
// the move now is to make an md array of these values,
let bArr = packRBGA(b) //packGrayscale(b)
let aArr = packRBGA(a) //packGrayscale(a)
......@@ -122,9 +111,7 @@ async function run(a, b){
for (let y = 0; y < resY; y++) {
result[x][y] = correlateRBGA(aArr, bArr, x, y)
}
await delay(0) // to avoid blocking,
}
console.log('run complete')
// make image from the result,
let max = -Infinity
let min = Infinity
......@@ -139,8 +126,6 @@ async function run(a, b){
if (result[x][y] < min) min = result[x][y]
}
}
console.log('max, min', max, min)
console.log('mp', mp)
// now we want to unwrap this into an imagedata type,
// filling back in grayscale type
let imdBuffer = new Uint8ClampedArray(resX * resY * 4)
......@@ -157,11 +142,17 @@ async function run(a, b){
}
}
let imgRes = new ImageData(imdBuffer, resX, resY)
console.log('imgRes', imgRes)
running = false
computedResult = imgRes
self.postMessage(imgRes)
}
onmessage = (evt) => {
run(evt.data.a, evt.data.b)
}
}
let computedResult = null
let running = false
export default function Correlate() {
Hunkify(this)
......@@ -180,7 +171,19 @@ export default function Correlate(){
this.dom = $('<div>').get(0)
let webWorker
this.init = () => {
// startup the worker:
let bleb = new Blob(['('+worker.toString()+'())'])
let url = window.URL.createObjectURL(bleb)
webWorker = new Worker(url)
webWorker.addEventListener('message', (evt) => {
running = false
if(!resOut.io()){
resOut.put(evt.data)
}
})
}
this.onload = () => {
......@@ -190,6 +193,12 @@ export default function Correlate(){
this.loop = () => {
if (imgIn.io() && !running) {
/*
imgIn.get()
running = true
console.log("POSTING")
webWorker.postMessage('msg')
*/
// ok, the image data:
let img = imgIn.get()
// to scale this thing, we draw it into a canvas (virtual ... not attached to page)
......@@ -208,10 +217,8 @@ export default function Correlate(){
let a = ctxB.getImageData(b.width / 2, b.height / 2, 50, 50)
// and write that out, to debug ...
ctxA.putImageData(a, 0, 0)
run(a, b)
}
if(!resOut.io() && computedResult !== null){
resOut.put(computedResult)
webWorker.postMessage({a: a, b: b})
running = true
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment