├── .gitignore ├── index.js ├── package.json └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | StackBlur - a fast almost Gaussian Blur For Canvas 4 | 5 | Version: 0.5 6 | Author: Mario Klingemann 7 | Contact: mario@quasimondo.com 8 | Website: http://www.quasimondo.com/StackBlurForCanvas 9 | Twitter: @quasimondo 10 | 11 | In case you find this class useful - especially in commercial projects - 12 | I am not totally unhappy for a small donation to my PayPal account 13 | mario@quasimondo.de 14 | 15 | Or support me on flattr: 16 | https://flattr.com/thing/72791/StackBlur-a-fast-almost-Gaussian-Blur-Effect-for-CanvasJavascript 17 | 18 | Copyright (c) 2010 Mario Klingemann 19 | 20 | Permission is hereby granted, free of charge, to any person 21 | obtaining a copy of this software and associated documentation 22 | files (the "Software"), to deal in the Software without 23 | restriction, including without limitation the rights to use, 24 | copy, modify, merge, publish, distribute, sublicense, and/or sell 25 | copies of the Software, and to permit persons to whom the 26 | Software is furnished to do so, subject to the following 27 | conditions: 28 | 29 | The above copyright notice and this permission notice shall be 30 | included in all copies or substantial portions of the Software. 31 | 32 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 33 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 34 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 35 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 36 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 37 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 38 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 39 | OTHER DEALINGS IN THE SOFTWARE. 40 | */ 41 | 42 | var mul_table = [ 43 | 512,512,456,512,328,456,335,512,405,328,271,456,388,335,292,512, 44 | 454,405,364,328,298,271,496,456,420,388,360,335,312,292,273,512, 45 | 482,454,428,405,383,364,345,328,312,298,284,271,259,496,475,456, 46 | 437,420,404,388,374,360,347,335,323,312,302,292,282,273,265,512, 47 | 497,482,468,454,441,428,417,405,394,383,373,364,354,345,337,328, 48 | 320,312,305,298,291,284,278,271,265,259,507,496,485,475,465,456, 49 | 446,437,428,420,412,404,396,388,381,374,367,360,354,347,341,335, 50 | 329,323,318,312,307,302,297,292,287,282,278,273,269,265,261,512, 51 | 505,497,489,482,475,468,461,454,447,441,435,428,422,417,411,405, 52 | 399,394,389,383,378,373,368,364,359,354,350,345,341,337,332,328, 53 | 324,320,316,312,309,305,301,298,294,291,287,284,281,278,274,271, 54 | 268,265,262,259,257,507,501,496,491,485,480,475,470,465,460,456, 55 | 451,446,442,437,433,428,424,420,416,412,408,404,400,396,392,388, 56 | 385,381,377,374,370,367,363,360,357,354,350,347,344,341,338,335, 57 | 332,329,326,323,320,318,315,312,310,307,304,302,299,297,294,292, 58 | 289,287,285,282,280,278,275,273,271,269,267,265,263,261,259]; 59 | 60 | 61 | var shg_table = [ 62 | 9, 11, 12, 13, 13, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 63 | 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 64 | 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 65 | 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21, 66 | 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 67 | 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 68 | 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 69 | 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 70 | 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 71 | 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 72 | 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 73 | 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 74 | 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 75 | 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 76 | 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 77 | 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24 ]; 78 | 79 | function blur( pixels, width, height, radius ) 80 | { 81 | if ( isNaN(radius) || radius < 1 ) return; 82 | radius |= 0; 83 | 84 | var x, y, i, p, yp, yi, yw, r_sum, g_sum, b_sum, a_sum, 85 | r_out_sum, g_out_sum, b_out_sum, a_out_sum, 86 | r_in_sum, g_in_sum, b_in_sum, a_in_sum, 87 | pr, pg, pb, pa, rbs; 88 | 89 | var div = radius + radius + 1; 90 | var w4 = width << 2; 91 | var widthMinus1 = width - 1; 92 | var heightMinus1 = height - 1; 93 | var radiusPlus1 = radius + 1; 94 | var sumFactor = radiusPlus1 * ( radiusPlus1 + 1 ) / 2; 95 | 96 | var stackStart = new BlurStack(); 97 | var stack = stackStart; 98 | for ( i = 1; i < div; i++ ) 99 | { 100 | stack = stack.next = new BlurStack(); 101 | if ( i == radiusPlus1 ) var stackEnd = stack; 102 | } 103 | stack.next = stackStart; 104 | var stackIn = null; 105 | var stackOut = null; 106 | 107 | yw = yi = 0; 108 | 109 | var mul_sum = mul_table[radius]; 110 | var shg_sum = shg_table[radius]; 111 | 112 | for ( y = 0; y < height; y++ ) 113 | { 114 | r_in_sum = g_in_sum = b_in_sum = a_in_sum = r_sum = g_sum = b_sum = a_sum = 0; 115 | 116 | r_out_sum = radiusPlus1 * ( pr = pixels[yi] ); 117 | g_out_sum = radiusPlus1 * ( pg = pixels[yi+1] ); 118 | b_out_sum = radiusPlus1 * ( pb = pixels[yi+2] ); 119 | a_out_sum = radiusPlus1 * ( pa = pixels[yi+3] ); 120 | 121 | r_sum += sumFactor * pr; 122 | g_sum += sumFactor * pg; 123 | b_sum += sumFactor * pb; 124 | a_sum += sumFactor * pa; 125 | 126 | stack = stackStart; 127 | 128 | for( i = 0; i < radiusPlus1; i++ ) 129 | { 130 | stack.r = pr; 131 | stack.g = pg; 132 | stack.b = pb; 133 | stack.a = pa; 134 | stack = stack.next; 135 | } 136 | 137 | for( i = 1; i < radiusPlus1; i++ ) 138 | { 139 | p = yi + (( widthMinus1 < i ? widthMinus1 : i ) << 2 ); 140 | r_sum += ( stack.r = ( pr = pixels[p])) * ( rbs = radiusPlus1 - i ); 141 | g_sum += ( stack.g = ( pg = pixels[p+1])) * rbs; 142 | b_sum += ( stack.b = ( pb = pixels[p+2])) * rbs; 143 | a_sum += ( stack.a = ( pa = pixels[p+3])) * rbs; 144 | 145 | r_in_sum += pr; 146 | g_in_sum += pg; 147 | b_in_sum += pb; 148 | a_in_sum += pa; 149 | 150 | stack = stack.next; 151 | } 152 | 153 | 154 | stackIn = stackStart; 155 | stackOut = stackEnd; 156 | for ( x = 0; x < width; x++ ) 157 | { 158 | pixels[yi+3] = pa = (a_sum * mul_sum) >> shg_sum; 159 | if ( pa != 0 ) 160 | { 161 | pa = 255 / pa; 162 | pixels[yi] = ((r_sum * mul_sum) >> shg_sum) * pa; 163 | pixels[yi+1] = ((g_sum * mul_sum) >> shg_sum) * pa; 164 | pixels[yi+2] = ((b_sum * mul_sum) >> shg_sum) * pa; 165 | } else { 166 | pixels[yi] = pixels[yi+1] = pixels[yi+2] = 0; 167 | } 168 | 169 | r_sum -= r_out_sum; 170 | g_sum -= g_out_sum; 171 | b_sum -= b_out_sum; 172 | a_sum -= a_out_sum; 173 | 174 | r_out_sum -= stackIn.r; 175 | g_out_sum -= stackIn.g; 176 | b_out_sum -= stackIn.b; 177 | a_out_sum -= stackIn.a; 178 | 179 | p = ( yw + ( ( p = x + radius + 1 ) < widthMinus1 ? p : widthMinus1 ) ) << 2; 180 | 181 | r_in_sum += ( stackIn.r = pixels[p]); 182 | g_in_sum += ( stackIn.g = pixels[p+1]); 183 | b_in_sum += ( stackIn.b = pixels[p+2]); 184 | a_in_sum += ( stackIn.a = pixels[p+3]); 185 | 186 | r_sum += r_in_sum; 187 | g_sum += g_in_sum; 188 | b_sum += b_in_sum; 189 | a_sum += a_in_sum; 190 | 191 | stackIn = stackIn.next; 192 | 193 | r_out_sum += ( pr = stackOut.r ); 194 | g_out_sum += ( pg = stackOut.g ); 195 | b_out_sum += ( pb = stackOut.b ); 196 | a_out_sum += ( pa = stackOut.a ); 197 | 198 | r_in_sum -= pr; 199 | g_in_sum -= pg; 200 | b_in_sum -= pb; 201 | a_in_sum -= pa; 202 | 203 | stackOut = stackOut.next; 204 | 205 | yi += 4; 206 | } 207 | yw += width; 208 | } 209 | 210 | 211 | for ( x = 0; x < width; x++ ) 212 | { 213 | g_in_sum = b_in_sum = a_in_sum = r_in_sum = g_sum = b_sum = a_sum = r_sum = 0; 214 | 215 | yi = x << 2; 216 | r_out_sum = radiusPlus1 * ( pr = pixels[yi]); 217 | g_out_sum = radiusPlus1 * ( pg = pixels[yi+1]); 218 | b_out_sum = radiusPlus1 * ( pb = pixels[yi+2]); 219 | a_out_sum = radiusPlus1 * ( pa = pixels[yi+3]); 220 | 221 | r_sum += sumFactor * pr; 222 | g_sum += sumFactor * pg; 223 | b_sum += sumFactor * pb; 224 | a_sum += sumFactor * pa; 225 | 226 | stack = stackStart; 227 | 228 | for( i = 0; i < radiusPlus1; i++ ) 229 | { 230 | stack.r = pr; 231 | stack.g = pg; 232 | stack.b = pb; 233 | stack.a = pa; 234 | stack = stack.next; 235 | } 236 | 237 | yp = width; 238 | 239 | for( i = 1; i <= radius; i++ ) 240 | { 241 | yi = ( yp + x ) << 2; 242 | 243 | r_sum += ( stack.r = ( pr = pixels[yi])) * ( rbs = radiusPlus1 - i ); 244 | g_sum += ( stack.g = ( pg = pixels[yi+1])) * rbs; 245 | b_sum += ( stack.b = ( pb = pixels[yi+2])) * rbs; 246 | a_sum += ( stack.a = ( pa = pixels[yi+3])) * rbs; 247 | 248 | r_in_sum += pr; 249 | g_in_sum += pg; 250 | b_in_sum += pb; 251 | a_in_sum += pa; 252 | 253 | stack = stack.next; 254 | 255 | if( i < heightMinus1 ) 256 | { 257 | yp += width; 258 | } 259 | } 260 | 261 | yi = x; 262 | stackIn = stackStart; 263 | stackOut = stackEnd; 264 | for ( y = 0; y < height; y++ ) 265 | { 266 | p = yi << 2; 267 | pixels[p+3] = pa = (a_sum * mul_sum) >> shg_sum; 268 | if ( pa > 0 ) 269 | { 270 | pa = 255 / pa; 271 | pixels[p] = ((r_sum * mul_sum) >> shg_sum ) * pa; 272 | pixels[p+1] = ((g_sum * mul_sum) >> shg_sum ) * pa; 273 | pixels[p+2] = ((b_sum * mul_sum) >> shg_sum ) * pa; 274 | } else { 275 | pixels[p] = pixels[p+1] = pixels[p+2] = 0; 276 | } 277 | 278 | r_sum -= r_out_sum; 279 | g_sum -= g_out_sum; 280 | b_sum -= b_out_sum; 281 | a_sum -= a_out_sum; 282 | 283 | r_out_sum -= stackIn.r; 284 | g_out_sum -= stackIn.g; 285 | b_out_sum -= stackIn.b; 286 | a_out_sum -= stackIn.a; 287 | 288 | p = ( x + (( ( p = y + radiusPlus1) < heightMinus1 ? p : heightMinus1 ) * width )) << 2; 289 | 290 | r_sum += ( r_in_sum += ( stackIn.r = pixels[p])); 291 | g_sum += ( g_in_sum += ( stackIn.g = pixels[p+1])); 292 | b_sum += ( b_in_sum += ( stackIn.b = pixels[p+2])); 293 | a_sum += ( a_in_sum += ( stackIn.a = pixels[p+3])); 294 | 295 | stackIn = stackIn.next; 296 | 297 | r_out_sum += ( pr = stackOut.r ); 298 | g_out_sum += ( pg = stackOut.g ); 299 | b_out_sum += ( pb = stackOut.b ); 300 | a_out_sum += ( pa = stackOut.a ); 301 | 302 | r_in_sum -= pr; 303 | g_in_sum -= pg; 304 | b_in_sum -= pb; 305 | a_in_sum -= pa; 306 | 307 | stackOut = stackOut.next; 308 | 309 | yi += width; 310 | } 311 | } 312 | } 313 | 314 | function BlurStack() 315 | { 316 | this.r = 0; 317 | this.g = 0; 318 | this.b = 0; 319 | this.a = 0; 320 | this.next = null; 321 | } 322 | 323 | module.exports = blur; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "stackblur", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/mattdesl/stackblur.git" 12 | }, 13 | "author": "", 14 | "license": "BSD-2-Clause" 15 | } 16 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # about 2 | 3 | A bare-bones port of Mario Klingemann's stackblur that just operates on a Uint8 pixel array. 4 | 5 | Original implementation: 6 | http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html 7 | 8 | # API 9 | 10 | ``` 11 | var stackblur = require('stackblur'); 12 | stackblur( pixels, width, height, radius ) 13 | ``` 14 | 15 | Takes the pixel array (typically Uint8ClampedArray), the width and height of the image, and the radius to blur. 16 | 17 | # License (Mario Klingemann) 18 | 19 | ``` 20 | StackBlur - a fast almost Gaussian Blur For Canvas 21 | 22 | Version: 0.5 23 | Author: Mario Klingemann 24 | Contact: mario@quasimondo.com 25 | Website: http://www.quasimondo.com/StackBlurForCanvas 26 | Twitter: @quasimondo 27 | 28 | In case you find this class useful - especially in commercial projects - 29 | I am not totally unhappy for a small donation to my PayPal account 30 | mario@quasimondo.de 31 | 32 | Or support me on flattr: 33 | https://flattr.com/thing/72791/StackBlur-a-fast-almost-Gaussian-Blur-Effect-for-CanvasJavascript 34 | 35 | Copyright (c) 2010 Mario Klingemann 36 | 37 | Permission is hereby granted, free of charge, to any person 38 | obtaining a copy of this software and associated documentation 39 | files (the "Software"), to deal in the Software without 40 | restriction, including without limitation the rights to use, 41 | copy, modify, merge, publish, distribute, sublicense, and/or sell 42 | copies of the Software, and to permit persons to whom the 43 | Software is furnished to do so, subject to the following 44 | conditions: 45 | 46 | The above copyright notice and this permission notice shall be 47 | included in all copies or substantial portions of the Software. 48 | 49 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 50 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 51 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 52 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 53 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 54 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 55 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 56 | OTHER DEALINGS IN THE SOFTWARE. 57 | ``` --------------------------------------------------------------------------------