Spaces:
Running
Running
File size: 9,349 Bytes
599349f | 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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | // GPU-accelerated augmentation using WebGL
// Applies brightness, contrast, and noise operations on the GPU
// Check if WebGL is available
let webglAvailable: boolean | null = null
let glContext: WebGLRenderingContext | null = null
let glCanvas: HTMLCanvasElement | null = null
export function isWebGLAvailable(): boolean {
if (webglAvailable !== null) return webglAvailable
try {
const canvas = document.createElement('canvas')
const gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl')
webglAvailable = gl !== null
return webglAvailable
} catch {
webglAvailable = false
return false
}
}
// Shader source code
const vertexShaderSource = `
attribute vec2 a_position;
attribute vec2 a_texCoord;
varying vec2 v_texCoord;
void main() {
gl_Position = vec4(a_position, 0.0, 1.0);
v_texCoord = a_texCoord;
}
`
const fragmentShaderSource = `
precision mediump float;
uniform sampler2D u_image;
uniform float u_brightness;
uniform float u_contrast;
uniform float u_noiseAmount;
uniform float u_seed;
varying vec2 v_texCoord;
// Simple pseudo-random function
float random(vec2 co) {
return fract(sin(dot(co.xy + u_seed, vec2(12.9898, 78.233))) * 43758.5453);
}
void main() {
vec4 color = texture2D(u_image, v_texCoord);
// Apply brightness (additive)
color.rgb += u_brightness;
// Apply contrast (multiplicative from midpoint)
color.rgb = (color.rgb - 0.5) * u_contrast + 0.5;
// Apply noise
if (u_noiseAmount > 0.0) {
float noise = (random(v_texCoord) - 0.5) * u_noiseAmount;
color.rgb += noise;
}
// Clamp values
color.rgb = clamp(color.rgb, 0.0, 1.0);
gl_FragColor = color;
}
`
// Compile shader
function compileShader(gl: WebGLRenderingContext, source: string, type: number): WebGLShader | null {
const shader = gl.createShader(type)
if (!shader) return null
gl.shaderSource(shader, source)
gl.compileShader(shader)
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
console.error('Shader compile error:', gl.getShaderInfoLog(shader))
gl.deleteShader(shader)
return null
}
return shader
}
// Create shader program
function createProgram(gl: WebGLRenderingContext): WebGLProgram | null {
const vertexShader = compileShader(gl, vertexShaderSource, gl.VERTEX_SHADER)
const fragmentShader = compileShader(gl, fragmentShaderSource, gl.FRAGMENT_SHADER)
if (!vertexShader || !fragmentShader) return null
const program = gl.createProgram()
if (!program) return null
gl.attachShader(program, vertexShader)
gl.attachShader(program, fragmentShader)
gl.linkProgram(program)
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
console.error('Program link error:', gl.getProgramInfoLog(program))
gl.deleteProgram(program)
return null
}
return program
}
// Initialize WebGL context and program
let program: WebGLProgram | null = null
let positionBuffer: WebGLBuffer | null = null
let texCoordBuffer: WebGLBuffer | null = null
function initWebGL(width: number, height: number): boolean {
if (glCanvas && glContext && program) {
// Resize if needed
if (glCanvas.width !== width || glCanvas.height !== height) {
glCanvas.width = width
glCanvas.height = height
glContext.viewport(0, 0, width, height)
}
return true
}
try {
glCanvas = document.createElement('canvas')
glCanvas.width = width
glCanvas.height = height
glContext = glCanvas.getContext('webgl', { preserveDrawingBuffer: true }) as WebGLRenderingContext
if (!glContext) {
console.warn('WebGL not available')
return false
}
program = createProgram(glContext)
if (!program) {
console.warn('Failed to create WebGL program')
return false
}
// Create buffers
const gl = glContext
// Position buffer (full screen quad)
positionBuffer = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer)
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
-1, -1, 1, -1, -1, 1,
-1, 1, 1, -1, 1, 1
]), gl.STATIC_DRAW)
// Texture coordinate buffer
texCoordBuffer = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer)
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
0, 1, 1, 1, 0, 0,
0, 0, 1, 1, 1, 0
]), gl.STATIC_DRAW)
return true
} catch (err) {
console.warn('WebGL initialization failed:', err)
return false
}
}
export interface GPUAugmentOptions {
brightness?: number // -1 to 1 (0 = no change)
contrast?: number // 0 to 2 (1 = no change)
noiseAmount?: number // 0 to 1
seed?: number
}
/**
* Apply GPU-accelerated augmentation to an image
* Returns the augmented canvas, or null if WebGL is not available
*/
export function applyGPUAugmentation(
sourceCanvas: HTMLCanvasElement,
options: GPUAugmentOptions
): HTMLCanvasElement | null {
const { brightness = 0, contrast = 1, noiseAmount = 0, seed = Math.random() } = options
// Skip if no augmentation needed
if (brightness === 0 && contrast === 1 && noiseAmount === 0) {
return sourceCanvas
}
if (!initWebGL(sourceCanvas.width, sourceCanvas.height)) {
return null // Fallback to CPU
}
const gl = glContext!
try {
gl.useProgram(program!)
// Create texture from source canvas
const texture = gl.createTexture()
gl.bindTexture(gl.TEXTURE_2D, texture)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, sourceCanvas)
// Set up attributes
const positionLocation = gl.getAttribLocation(program!, 'a_position')
const texCoordLocation = gl.getAttribLocation(program!, 'a_texCoord')
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer!)
gl.enableVertexAttribArray(positionLocation)
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0)
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer!)
gl.enableVertexAttribArray(texCoordLocation)
gl.vertexAttribPointer(texCoordLocation, 2, gl.FLOAT, false, 0, 0)
// Set uniforms
gl.uniform1f(gl.getUniformLocation(program!, 'u_brightness'), brightness)
gl.uniform1f(gl.getUniformLocation(program!, 'u_contrast'), contrast)
gl.uniform1f(gl.getUniformLocation(program!, 'u_noiseAmount'), noiseAmount)
gl.uniform1f(gl.getUniformLocation(program!, 'u_seed'), seed)
// Draw
gl.drawArrays(gl.TRIANGLES, 0, 6)
// Clean up texture
gl.deleteTexture(texture)
return glCanvas!
} catch (err) {
console.warn('GPU augmentation failed:', err)
return null
}
}
/**
* Apply GPU augmentation and return the result as ImageData
*/
export function applyGPUAugmentationToImageData(
imageData: ImageData,
options: GPUAugmentOptions
): ImageData | null {
// Create temporary canvas with the image data
const tempCanvas = document.createElement('canvas')
tempCanvas.width = imageData.width
tempCanvas.height = imageData.height
const ctx = tempCanvas.getContext('2d')
if (!ctx) return null
ctx.putImageData(imageData, 0, 0)
const resultCanvas = applyGPUAugmentation(tempCanvas, options)
if (!resultCanvas) return null
const resultCtx = resultCanvas.getContext('2d') || resultCanvas.getContext('webgl')
// For WebGL canvas, we need to read pixels differently
if (glContext) {
const pixels = new Uint8Array(imageData.width * imageData.height * 4)
glContext.readPixels(0, 0, imageData.width, imageData.height, glContext.RGBA, glContext.UNSIGNED_BYTE, pixels)
// Flip Y axis (WebGL has origin at bottom-left)
const flipped = new Uint8ClampedArray(pixels.length)
const rowSize = imageData.width * 4
for (let y = 0; y < imageData.height; y++) {
const srcRow = (imageData.height - 1 - y) * rowSize
const dstRow = y * rowSize
flipped.set(pixels.subarray(srcRow, srcRow + rowSize), dstRow)
}
return new ImageData(flipped, imageData.width, imageData.height)
}
return null
}
/**
* Clean up WebGL resources
*/
export function cleanupGPU() {
if (glContext && program) {
glContext.deleteProgram(program)
if (positionBuffer) glContext.deleteBuffer(positionBuffer)
if (texCoordBuffer) glContext.deleteBuffer(texCoordBuffer)
}
program = null
positionBuffer = null
texCoordBuffer = null
glContext = null
glCanvas = null
}
|