(imageData, blockSize)
| 414 | } |
| 415 | |
| 416 | validateBlockSize(imageData, blockSize) { |
| 417 | const { data, width, height } = imageData; |
| 418 | let totalVariance = 0; |
| 419 | let blockCount = 0; |
| 420 | |
| 421 | // 采样验证 |
| 422 | const sampleSize = Math.min(100, Math.floor((width * height) / (blockSize * blockSize))); |
| 423 | |
| 424 | for (let i = 0; i < sampleSize; i++) { |
| 425 | const startX = Math.floor(Math.random() * (width - blockSize)); |
| 426 | const startY = Math.floor(Math.random() * (height - blockSize)); |
| 427 | |
| 428 | const variance = this.calculateBlockVariance(data, width, startX, startY, blockSize); |
| 429 | totalVariance += variance; |
| 430 | blockCount++; |
| 431 | } |
| 432 | |
| 433 | const averageVariance = totalVariance / blockCount; |
| 434 | |
| 435 | // 方差越小,置信度越高 |
| 436 | return Math.max(0, 1 - (averageVariance / 10000)); |
| 437 | } |
| 438 | |
| 439 | calculateBlockVariance(data, width, startX, startY, blockSize) { |
| 440 | const pixels = []; |
no test coverage detected