File size: 5,267 Bytes
80bcac8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | import { PaddingMode, ConvolutionStep, ImageDataExtended } from '@/types'
export function getPixel(
image: ImageDataExtended,
x: number,
y: number,
channel: number,
padding: PaddingMode
): number {
const { width, height, channels, data } = image
let px = x
let py = y
switch (padding) {
case 'zero':
if (px < 0 || px >= width || py < 0 || py >= height) return 0
break
case 'replicate':
px = Math.max(0, Math.min(px, width - 1))
py = Math.max(0, Math.min(py, height - 1))
break
case 'reflect':
if (px < 0) px = -px
if (px >= width) px = 2 * width - px - 2
if (py < 0) py = -py
if (py >= height) py = 2 * height - py - 2
px = Math.max(0, Math.min(px, width - 1))
py = Math.max(0, Math.min(py, height - 1))
break
case 'wrap':
px = ((px % width) + width) % width
py = ((py % height) + height) % height
break
}
const index = (py * width + px) * channels + channel
return data[index] / 255
}
export function setPixel(
image: ImageDataExtended,
x: number,
y: number,
channel: number,
value: number
): void {
const { width, channels, data } = image
if (x < 0 || x >= width || y < 0 || y >= image.height) return
const index = (y * width + x) * channels + channel
data[index] = Math.max(0, Math.min(255, Math.round(value * 255)))
}
export function* convolutionSteps(
image: ImageDataExtended,
kernel: number[][],
padding: PaddingMode,
normalize: boolean,
onProgress?: (progress: number) => void
): Generator<ConvolutionStep> {
const { width, height, channels } = image
const kernelSize = kernel.length
const halfKernel = Math.floor(kernelSize / 2)
const totalPixels = width * height * channels
let processedPixels = 0
const kernelSum = kernel.flat().reduce((a, b) => a + b, 0)
const normalizeFactor = normalize && kernelSum !== 0 ? 1 / kernelSum : 1
const outputData = new Float32Array(width * height * channels)
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
for (let c = 0; c < channels; c++) {
const inputPatch: number[][] = []
const multiplications: number[][] = []
let sum = 0
for (let ky = 0; ky < kernelSize; ky++) {
const patchRow: number[] = []
const multRow: number[] = []
for (let kx = 0; kx < kernelSize; kx++) {
const px = x + kx - halfKernel
const py = y + ky - halfKernel
const pixelValue = getPixel(image, px, py, c, padding)
const kernelValue = kernel[ky][kx]
patchRow.push(pixelValue)
const mult = pixelValue * kernelValue
multRow.push(mult)
sum += mult
}
inputPatch.push(patchRow)
multiplications.push(multRow)
}
const outputValue = sum
const normalizedValue = sum * normalizeFactor
const outputIndex = (y * width + x) * channels + c
outputData[outputIndex] = normalizedValue
processedPixels++
const progress = processedPixels / totalPixels
yield {
x,
y,
channel: c,
inputPatch,
kernel,
multiplications,
sum: outputValue,
outputValue: normalizedValue,
normalizedValue,
progress,
}
if (onProgress) onProgress(progress)
}
}
}
// Convert output to ImageData
const outputImageData = new ImageData(width, height)
for (let i = 0; i < outputData.length; i++) {
outputImageData.data[i] = Math.max(0, Math.min(255, Math.round(outputData[i] * 255)))
}
// Final yield with complete output
yield {
x: -1,
y: -1,
channel: -1,
inputPatch: [],
kernel,
multiplications: [],
sum: 0,
outputValue: 0,
normalizedValue: 0,
progress: 1,
}
}
export function convolveImage(
imageData: ImageData,
kernel: number[][],
padding: PaddingMode = 'zero',
normalize: boolean = true
): ImageData {
const extended: ImageDataExtended = {
data: imageData.data,
width: imageData.width,
height: imageData.height,
channels: 4,
}
const { width, height } = imageData
const kernelSize = kernel.length
const halfKernel = Math.floor(kernelSize / 2)
const kernelSum = kernel.flat().reduce((a, b) => a + b, 0)
const normalizeFactor = normalize && kernelSum !== 0 ? 1 / kernelSum : 1
const outputData = new Uint8ClampedArray(imageData.data.length)
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
for (let c = 0; c < 3; c++) {
let sum = 0
for (let ky = 0; ky < kernelSize; ky++) {
for (let kx = 0; kx < kernelSize; kx++) {
const px = x + kx - halfKernel
const py = y + ky - halfKernel
const pixelValue = getPixel(extended, px, py, c, padding)
sum += pixelValue * kernel[ky][kx]
}
}
const outputIndex = (y * width + x) * 4 + c
outputData[outputIndex] = Math.max(0, Math.min(255, Math.round(sum * normalizeFactor * 255)))
}
outputData[(y * width + x) * 4 + 3] = 255
}
}
return new ImageData(outputData, imageData.width, imageData.height)
} |