| 47 | const PUZZLE_PAD = 10; |
| 48 | |
| 49 | class PuzzleRecognizer { |
| 50 | constructor(bg, patch, y) { |
| 51 | // console.log(bg); |
| 52 | const imgBg = new PNGDecoder(Buffer.from(bg, 'base64')); |
| 53 | const imgPatch = new PNGDecoder(Buffer.from(patch, 'base64')); |
| 54 | |
| 55 | // console.log(imgBg); |
| 56 | |
| 57 | this.bg = imgBg; |
| 58 | this.patch = imgPatch; |
| 59 | this.rawBg = bg; |
| 60 | this.rawPatch = patch; |
| 61 | this.y = y; |
| 62 | this.w = imgBg.width; |
| 63 | this.h = imgBg.height; |
| 64 | } |
| 65 | |
| 66 | async run() { |
| 67 | await this.bg.decodeToPixels(); |
| 68 | await this.patch.decodeToPixels(); |
| 69 | |
| 70 | return this.recognize(); |
| 71 | } |
| 72 | |
| 73 | recognize() { |
| 74 | const {ctx, w: width, bg} = this; |
| 75 | const {width: patchWidth, height: patchHeight} = this.patch; |
| 76 | const posY = this.y + PUZZLE_PAD + ((patchHeight - PUZZLE_PAD) / 2) - (PUZZLE_GAP / 2); |
| 77 | // const cData = ctx.getImageData(0, a.y + 10 + 20 - 4, 360, 8).data; |
| 78 | const cData = bg.getImageData(0, posY, width, PUZZLE_GAP).data; |
| 79 | const lumas = []; |
| 80 | |
| 81 | for (let x = 0; x < width; x++) { |
| 82 | var sum = 0; |
| 83 | |
| 84 | // y xais |
| 85 | for (let y = 0; y < PUZZLE_GAP; y++) { |
| 86 | var idx = x * 4 + y * (width * 4); |
| 87 | var r = cData[idx]; |
| 88 | var g = cData[idx + 1]; |
| 89 | var b = cData[idx + 2]; |
| 90 | var luma = 0.2126 * r + 0.7152 * g + 0.0722 * b; |
| 91 | |
| 92 | sum += luma; |
| 93 | } |
| 94 | |
| 95 | lumas.push(sum / PUZZLE_GAP); |
| 96 | } |
| 97 | |
| 98 | const n = 2; // minium macroscopic image width (px) |
| 99 | const margin = patchWidth - PUZZLE_PAD; |
| 100 | const diff = 20; // macroscopic brightness difference |
| 101 | const radius = PUZZLE_PAD; |
| 102 | for (let i = 0, len = lumas.length - 2 * 4; i < len; i++) { |
| 103 | const left = (lumas[i] + lumas[i + 1]) / n; |
| 104 | const right = (lumas[i + 2] + lumas[i + 3]) / n; |
| 105 | const mi = margin + i; |
| 106 | const mLeft = (lumas[mi] + lumas[mi + 1]) / n; |
nothing calls this directly
no outgoing calls
no test coverage detected