From 2a9d5c26d2b3f39e3b7d7f3ffef78c9cabf67b93 Mon Sep 17 00:00:00 2001
From: Jake Read <jake.read@cba.mit.edu>
Date: Sun, 17 Nov 2019 09:58:20 -0500
Subject: [PATCH] rudimentary

---
 hunks/adhoc/correlate.js | 115 ++++++++++++++++++++++++++++++---------
 1 file changed, 88 insertions(+), 27 deletions(-)

diff --git a/hunks/adhoc/correlate.js b/hunks/adhoc/correlate.js
index cf80e1b..1dd1995 100644
--- a/hunks/adhoc/correlate.js
+++ b/hunks/adhoc/correlate.js
@@ -15,22 +15,47 @@ let computedResult = null
 let running = false
 
 let correlate = (a, b, x, y) => {
-  // score a against b,
+  // score a against b, starting b from x, y
+  // a, b are float64 arrays, pixel-wise
   let sumA = 0
   let sumB = 0
   let sumDot = 0
   let bi = 0
+  // i will be index within the flat-af array under a,
+  for(let i = 0; i < a.length; i ++){
+    for(let j = 0; j < a[0].length; j ++){
+      sumA += a[i][j]
+      sumB += b[i + x][j + y]
+      sumDot += a[i][j] * b[i + x][j + y]
+    }
+  }
+  return sumDot / Math.sqrt(sumA * sumA * sumB * sumB)
+
+  /*
   for(let i = 0; i < a.data.length; i ++){
     sumA += a.data[i]
-
-    // something is up w/ this feer sure
-    bi = (y * b.width + x) * 4
-    bi += Math.floor(i / (a.width * 4)) * (b.width * 4) + i
+    // to find the reciprocal pixel in b,
+    // we can start at the top-left corner: here is the 'i_th' element
+    // within b, correspondign to the XY of the top-left of a,
+    bi = (y * b.width * 4) + x * 4
+    // to get down from here, the row of a this i is in:
+    let rca = Math.floor(i / a.width * 4)
+    bi += rca * b.width * 4
+    // and the increment,
+    bi += (i % (b.width * 4))
+    // ok ?
+    if(bi >= b.data.length){
+      bi = b.data.length
+      // happens all the time, go home do this tomorrow
+      console.log('yep', bi, rca, i, x, y)
+      //throw new Error('halting')
+    }
     sumB += b.data[bi]
 
     sumDot += a.data[i] * b.data[bi]
   }
-  return (sumDot / Math.sqrt(sumA*sumA + sumB*sumB))
+  */
+  //return 55 //(sumDot / Math.sqrt(sumA*sumA + sumB*sumB))
   // a, b are str8 up matricies
   //console.log(b.width)
   // ok, now we can do the maths on a and b, thx
@@ -44,6 +69,23 @@ let delay = (time) => {
   })
 }
 
+let packAndGrayscaleImage = (imgD) => {
+  // make an md array shaped like[x][y]
+  let arr = []
+  for(let x = 0; x < imgD.width; x ++){
+    arr.push(new Float64Array(imgD.height))
+  }
+  let p = 0
+  for(let y = 0; y < imgD.height; y ++){
+    for(let x = 0; x < imgD.width; x ++){
+      // grayscale all values as we load them up:
+      arr[x][y] = (imgD.data[p] + imgD.data[p + 1] + imgD.data[p + 2] + imgD.data[p + 3]) / 4;
+      p += 4;
+    }
+  }
+  return arr
+}
+
 async function run(a, b){
   running = true
   // a is img to search for, b is img to search within. both are ImageData types
@@ -51,35 +93,54 @@ async function run(a, b){
   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 = packAndGrayscaleImage(b)
+  let aArr = packAndGrayscaleImage(a)
+  // ok, results array like[x][y]
   let result = []
-  // so, let's see about this ... we
-  for(let x = 0; x < b.width - a.width; x ++){
-    for(let y = 0; y < b.height - a.height; y ++){
-      // best to use b in-place,
-      let corr = correlate(a, b, x, y)
-      // four channels ...
-      result.push(corr, corr, corr, corr)
+  for(let x = 0; x < resX; x ++){
+    result.push(new Float64Array(resY))
+  }
+  // now fill,
+  for(let x = 0; x < resX; x ++){
+    for(let y = 0; y < resY; y ++){
+      result[x][y] = correlate(aArr, bArr, x, y)
     }
-    // not blocking ...
-    await delay(0)
+    await delay(0) // to avoid blocking,
   }
-  console.log('complete')
+  console.log('run complete')
   // make image from the result,
   let max = -Infinity
   let min = Infinity
-  for(let i = 0; i < result.length; i ++){
-    if(result[i] > max) max = result[i];
-    if(result[i] < min) min = result[i];
-  }
-  for(let i = 0; i < result.length; i ++){
-    result[i] = (result[i] - min) * (255 / (max-min))
+  let mp = {x: 0, y: 0}
+  for(let x = 0; x < resX; x ++){
+    for(let y = 0; y < resY; y ++){
+      if(result[x][y] > max){
+        max = result[x][y]
+        mp.x = x
+        mp.y = y
+      }
+      if(result[x][y] < min) min = result[x][y]
+    }
   }
   console.log('max, min', max, min)
-  console.log('result', result)
-  let u8 = Uint8ClampedArray.from(result)
-  console.log('u8', u8)
-  console.log('resX, resY', resX, resY)
-  let imgRes = new ImageData(u8, resX, resY)
+  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)
+  let lc = 0
+  let dt = 0
+  for(let y = 0; y < resY; y ++){
+    for(let x = 0; x < resX; x ++){
+      // stretch to full range,
+      dt = (result[x][y] - min) * (255 / (max - min))
+      imdBuffer[lc ++] = dt
+      imdBuffer[lc ++] = dt
+      imdBuffer[lc ++] = dt
+      imdBuffer[lc ++] = dt
+    }
+  }
+  let imgRes = new ImageData(imdBuffer, resX, resY)
   console.log('imgRes', imgRes)
   running = false
   computedResult = imgRes
-- 
GitLab