gridfinder
Advanced tools
Comparing version 1.46.0 to 1.48.0
import {findGrid1d} from "./findGrid.js"; | ||
function* probes(limit) { | ||
let i = 7; | ||
for (;;) { | ||
i += i >> 1; | ||
let step = Math.sqrt(limit) + 1 |0; | ||
let half = limit / 2 |0; | ||
if (i >= limit) return; | ||
for (let i = step; i < limit; i += step) { | ||
yield i; | ||
@@ -17,23 +15,43 @@ } | ||
const localtreshold = 8; | ||
let line = Array(primaryLimt).fill(0); | ||
for (let x = 0; x < primaryLimt; x++) { | ||
let total = 0; | ||
let samples = []; | ||
for (let x = localtreshold; x < primaryLimt - localtreshold; x++) { | ||
samples.length = 0; | ||
// Edge detection range. | ||
let from = x - localtreshold; | ||
let to = x + localtreshold; | ||
for (let y of probes(secondaryLimit)) { | ||
let off = offset(x, y) * 4; | ||
let sum = pixels[off] + pixels[off+1] + pixels[off+2] + pixels[off+3]; | ||
off = offset(x, y - 1) * 4; | ||
sum += pixels[off] + pixels[off+1] + pixels[off+2] + pixels[off+3]; | ||
if (y+1 < secondaryLimit) { | ||
off = offset(x, y + 1) * 4; | ||
sum += pixels[off] + pixels[off+1] + pixels[off+2] + pixels[off+3]; | ||
let lavg = sum; | ||
for (let i = from; i < x; i++) { | ||
let off = offset(i, y) * 4; | ||
lavg += pixels[off] + pixels[off+1] + pixels[off+2] + pixels[off+3]; | ||
} | ||
total += sum; | ||
lavg /= localtreshold; | ||
let ravg = sum; | ||
for (let i = x + 1; i <= to; i++) { | ||
let off = offset(i, y) * 4; | ||
ravg += pixels[off] + pixels[off+1] + pixels[off+2] + pixels[off+3]; | ||
} | ||
ravg /= localtreshold; | ||
if ((sum < lavg) != (sum < ravg)) { | ||
samples.push(0); | ||
} else { | ||
let ldiff = Math.abs(sum - lavg); | ||
let rdiff = Math.abs(sum - ravg); | ||
samples.push(Math.min(ldiff, rdiff)); | ||
} | ||
} | ||
// 11 isn't correct around edges but disadvantages edges which we probably want anyways. | ||
line[x] += total * 11; | ||
for (let i = Math.max(0, x-5); i < Math.min(x+5, primaryLimt); i++) | ||
line[i] -= total; | ||
samples.sort((a, b) => a - b); | ||
let middle = samples.slice(samples.length * 0.25 | 0, samples.length * 0.75 | 0) | ||
line[x] = middle.reduce((a,b)=>a+b)/middle.length; | ||
} | ||
@@ -48,14 +66,8 @@ | ||
let point_count = 8; | ||
if (sorted.length < 40) { | ||
point_count = sorted.length/4 | 0; | ||
} else if (sorted.length > 1000) { | ||
point_count = sorted.length*0.01 | 0; | ||
} | ||
let point_count = Math.log(sorted.length) |0; | ||
let threshold = sorted[sorted.length - point_count]; | ||
let threshold = sorted[point_count]; | ||
let points = []; | ||
for (let y = 0; y < line.length; y++) { | ||
if (line[y] < threshold) { | ||
if (line[y] >= threshold) { | ||
points.push(y); | ||
@@ -72,3 +84,3 @@ } | ||
let rows = extractLine(img, img.height, img.width, (y, x) => x + y*img.height); | ||
let rows = extractLine(img, img.height, img.width, (y, x) => x + y*img.width); | ||
let ydim = detectGrid1d(rows); | ||
@@ -75,0 +87,0 @@ |
@@ -8,8 +8,42 @@ import * as gridfinder from "./lib.js"; | ||
Object.entries({ | ||
"astral-journey.jpg": { | ||
width: 35, | ||
}, | ||
// "crossroad-keep.jpg": { | ||
// width: 22.25, | ||
// }, | ||
"grass.jpg": { | ||
width: 12.5, | ||
height: 8.5, // Wrong. | ||
offY: -3, // Wrong. | ||
}, | ||
"sky-islands.jpg": { | ||
"irish-pub.jpg": { | ||
width: 70, | ||
height: 1315, // Wrong, detects roof tiles. | ||
offY: -575, // Wrong. | ||
}, | ||
"mythictable-tutorial.jpg": { | ||
width: 70, | ||
}, | ||
"overgrown-courtyard.jpg": { | ||
width: 35, | ||
height: 35/2, // Wrong | ||
tolerance: 0.1, | ||
}, | ||
"floating-islands.jpg": { | ||
width: 17, | ||
}, | ||
// "spaceship-main-blur.jpg": { | ||
// width: 10.66, | ||
// tolerance: 0.1, | ||
// offTolerance: 5, | ||
// }, | ||
// "spaceship-main-sharp.jpg": { | ||
// width: 10.66, | ||
// tolerance: 0.1, | ||
// offTolerance: 5, | ||
// }, | ||
// "xak-tsaroth.jpg": { | ||
// width: 35, | ||
// }, | ||
}).forEach(function run([map, cfg]) { | ||
@@ -37,8 +71,10 @@ it(map, async () => { | ||
let {cellSize, offset} = gridfinder.detectGrid(data); | ||
console.log({cellSize, offset}); | ||
assert(Math.abs(cellSize.width/width - 1) < tolerance, `Expected width ${width} got ${cellSize.width}`); | ||
assert(Math.abs(cellSize.height/height - 1) < tolerance, `Expected height ${height} got ${cellSize.height}`); | ||
assert(Math.abs(offset.x - offX) < offTolerance, `Expected X-off ${offX} got ${offset.x}`); | ||
assert(Math.abs(offset.y - offY) < offTolerance, `Expected height ${offY} got ${offset.y}`); | ||
assert(Math.abs(offset.y - offY) < offTolerance, `Expected Y-off ${offY} got ${offset.y}`); | ||
}) | ||
}) | ||
}) |
{ | ||
"name": "gridfinder", | ||
"version": "1.46.0", | ||
"version": "1.48.0", | ||
"license": "Apache 2.0", | ||
@@ -11,2 +11,3 @@ "homepage": "https://gridfinder.kevincox.ca", | ||
"author": "kevincox@kevincox.ca", | ||
"files": "lib/", | ||
"main": "lib/lib.js", | ||
@@ -13,0 +14,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
8678707
41
1012
0