diff --git a/public_html/js/lib/reactive-filter.js b/public_html/js/lib/reactive-filter.js index 510482b..84d0954 100644 --- a/public_html/js/lib/reactive-filter.js +++ b/public_html/js/lib/reactive-filter.js @@ -202,3 +202,69 @@ reactiveContrast.apply = function(){ /* (2) Copie le résultat sur le `` */ o.context.putImageData(imageData, 0, 0); }; + + + + +/* PRODUIT DE CONVOLUTION +* +*/ +Filter.convolution = function(data, options){ + + options.kernel = !options.hasOwnProperty('kernel') ? [] : options.kernel; + + console.log(options.kernel); + + /* [0] Initialisation + =========================================================*/ + // Offset pour ne pas dépasser + var offset = { + y: parseInt(options.kernel.length/2), // On calcule l'offset sur y (pour que le kernel ne déborde pas) + x: parseInt(options.kernel[0].length/2) // On calcule l'offset sur x (pour que le kernel ne déborde pas) + }; + + // Front buffer (copie buffer) + var original = data.buffer.slice(0); + + + + /* [1] Pour chaque pixel de la map + =========================================================*/ + for( var y = offset.y ; y < data.height ; y++ ){ + for( var x = offset.x ; x < data.width ; x++ ){ + + /* [2] Pour chaque pixel du @kernel + =========================================================*/ + /* (1) Contiendra le compte final */ + var convolved = { + r: 0x00, + g: 0x00, + b: 0x00, + a: 0x00 + }; + + for( var ky = -offset.y ; ky <= offset.y ; ky++ ){ + for( var kx = -offset.x ; kx <= offset.x ; kx++ ){ + + var coeff = options.kernel[offset.y+ky][offset.x+kx]; // Valeur du kernel + var sIndex = 4*( (y+ky)*data.width + (x+kx)); // indice source + + convolved.r += original[sIndex+0] * coeff; + convolved.g += original[sIndex+1] * coeff; + convolved.b += original[sIndex+2] * coeff; + convolved.a += original[sIndex+3] * coeff; + + } + } + + + /* (2) On applique la valeur sur le buffer de destination */ + var dIndex = 4*(y*data.width + x); // indice destination + data.buffer[dIndex+0] = convolved.r; + data.buffer[dIndex+1] = convolved.g; + data.buffer[dIndex+2] = convolved.b; + data.buffer[dIndex+3] = convolved.a; + } + } + +};