* Find the size of a connected cluster of diff pixels using BFS
( pixels, startX, startY, width, height, radius, visited )
| 264 | * Find the size of a connected cluster of diff pixels using BFS |
| 265 | */ |
| 266 | function findClusterSize( |
| 267 | pixels, |
| 268 | startX, startY, |
| 269 | width, height, |
| 270 | radius, |
| 271 | visited |
| 272 | ) { |
| 273 | const queue = [{ x: startX, y: startY }]; |
| 274 | let size = 0; |
| 275 | const clusterPixels = []; |
| 276 | |
| 277 | while (queue.length > 0) { |
| 278 | const { x, y } = queue.shift(); |
| 279 | const pos = (y * width + x) * 4; |
| 280 | |
| 281 | // Skip if already visited |
| 282 | if (visited.has(pos)) continue; |
| 283 | |
| 284 | // Skip if not a diff pixel |
| 285 | if ( |
| 286 | pixels[pos] !== 255 || |
| 287 | pixels[pos + 1] !== 0 || |
| 288 | pixels[pos + 2] !== 0 |
| 289 | ) continue; |
| 290 | |
| 291 | // Mark as visited |
| 292 | visited.add(pos); |
| 293 | size++; |
| 294 | clusterPixels.push({ x, y }); |
| 295 | |
| 296 | // Add neighbors to queue |
| 297 | for (let dy = -radius; dy <= radius; dy++) { |
| 298 | for (let dx = -radius; dx <= radius; dx++) { |
| 299 | const nx = x + dx; |
| 300 | const ny = y + dy; |
| 301 | |
| 302 | // Skip if out of bounds |
| 303 | if (nx < 0 || nx >= width || ny < 0 || ny >= height) continue; |
| 304 | |
| 305 | // Skip if already visited |
| 306 | const npos = (ny * width + nx) * 4; |
| 307 | if (!visited.has(npos)) { |
| 308 | queue.push({ x: nx, y: ny }); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | let isLineShift = false; |
| 315 | if (clusterPixels.length > 0) { |
| 316 | // Count pixels with limited neighbors (line-like characteristic) |
| 317 | let linelikePixels = 0; |
| 318 | |
| 319 | for (const { x, y } of clusterPixels) { |
| 320 | // Count neighbors |
| 321 | let neighbors = 0; |
| 322 | for (let dy = -1; dy <= 1; dy++) { |
| 323 | for (let dx = -1; dx <= 1; dx++) { |
no test coverage detected