| 616 | } |
| 617 | |
| 618 | sampleBlock(imageData, startX, startY, blockSize) { |
| 619 | const { data, width, height } = imageData; |
| 620 | let r = 0, g = 0, b = 0, a = 0, count = 0; |
| 621 | |
| 622 | for (let y = startY; y < Math.min(startY + blockSize, height); y++) { |
| 623 | for (let x = startX; x < Math.min(startX + blockSize, width); x++) { |
| 624 | const index = (y * width + x) * 4; |
| 625 | r += data[index]; |
| 626 | g += data[index + 1]; |
| 627 | b += data[index + 2]; |
| 628 | a += data[index + 3]; |
| 629 | count++; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | if (count === 0) return { r: 0, g: 0, b: 0, a: 0 }; |
| 634 | |
| 635 | return { |
| 636 | r: Math.round(r / count), |
| 637 | g: Math.round(g / count), |
| 638 | b: Math.round(b / count), |
| 639 | a: Math.round(a / count) |
| 640 | }; |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * 像素化到指定宽度 |