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

Method downsampleToBase

pixelizer.js:587–616  ·  view source on GitHub ↗

* 下采样到基准像素图

(sourceCanvas, blockSize)

Source from the content-addressed store, hash-verified

585 * 下采样到基准像素图
586 */
587 downsampleToBase(sourceCanvas, blockSize) {
588 const sourceCtx = sourceCanvas.getContext('2d');
589 const sourceData = sourceCtx.getImageData(0, 0, sourceCanvas.width, sourceCanvas.height);
590
591 const targetWidth = Math.floor(sourceCanvas.width / blockSize);
592 const targetHeight = Math.floor(sourceCanvas.height / blockSize);
593
594 const targetCanvas = document.createElement('canvas');
595 targetCanvas.width = targetWidth;
596 targetCanvas.height = targetHeight;
597 const targetCtx = targetCanvas.getContext('2d');
598 targetCtx.imageSmoothingEnabled = false;
599
600 const targetData = targetCtx.createImageData(targetWidth, targetHeight);
601
602 for (let y = 0; y < targetHeight; y++) {
603 for (let x = 0; x < targetWidth; x++) {
604 const color = this.sampleBlock(sourceData, x * blockSize, y * blockSize, blockSize);
605 const targetIndex = (y * targetWidth + x) * 4;
606
607 targetData.data[targetIndex] = color.r;
608 targetData.data[targetIndex + 1] = color.g;
609 targetData.data[targetIndex + 2] = color.b;
610 targetData.data[targetIndex + 3] = color.a;
611 }
612 }
613
614 targetCtx.putImageData(targetData, 0, 0);
615 return targetCanvas;
616 }
617
618 sampleBlock(imageData, startX, startY, blockSize) {
619 const { data, width, height } = imageData;

Callers 1

processAutoModeMethod · 0.95

Calls 1

sampleBlockMethod · 0.95

Tested by

no test coverage detected