(rgba, width, height)
| 19 | } |
| 20 | |
| 21 | function removeOuterBackground(rgba, width, height) { |
| 22 | const visited = new Uint8Array(width * height); |
| 23 | const queue = []; |
| 24 | |
| 25 | for (let x = 0; x < width; x++) { |
| 26 | queue.push(x, 0, x, height - 1); |
| 27 | } |
| 28 | for (let y = 1; y < height - 1; y++) { |
| 29 | queue.push(0, y, width - 1, y); |
| 30 | } |
| 31 | |
| 32 | while (queue.length) { |
| 33 | const y = queue.pop(); |
| 34 | const x = queue.pop(); |
| 35 | const idx = y * width + x; |
| 36 | if (x < 0 || y < 0 || x >= width || y >= height || visited[idx]) continue; |
| 37 | |
| 38 | const i = idx * 4; |
| 39 | const r = rgba[i]; |
| 40 | const g = rgba[i + 1]; |
| 41 | const b = rgba[i + 2]; |
| 42 | const a = rgba[i + 3]; |
| 43 | if (!isBackground(r, g, b, a)) continue; |
| 44 | |
| 45 | visited[idx] = 1; |
| 46 | rgba[i + 3] = 0; |
| 47 | |
| 48 | queue.push(x + 1, y, x - 1, y, x, y + 1, x, y - 1); |
| 49 | } |
| 50 | |
| 51 | return rgba; |
| 52 | } |
| 53 | |
| 54 | function getBounds(rgba, width, height) { |
| 55 | let minX = width; |
no test coverage detected