MCPcopy Create free account
hub / github.com/32comic/image2pixel-web / sampleBlock

Method sampleBlock

pixelizer.js:618–641  ·  view source on GitHub ↗
(imageData, startX, startY, blockSize)

Source from the content-addressed store, hash-verified

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 * 像素化到指定宽度

Callers 1

downsampleToBaseMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected