StackBlur.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. StackBlur - a fast almost Gaussian Blur For Canvas
  3. Version: 0.5
  4. Author: Mario Klingemann
  5. Contact: mario@quasimondo.com
  6. Website: http://www.quasimondo.com/StackBlurForCanvas
  7. Twitter: @quasimondo
  8. In case you find this class useful - especially in commercial projects -
  9. I am not totally unhappy for a small donation to my PayPal account
  10. mario@quasimondo.de
  11. Or support me on flattr:
  12. https://flattr.com/thing/72791/StackBlur-a-fast-almost-Gaussian-Blur-Effect-for-CanvasJavascript
  13. Copyright (c) 2010 Mario Klingemann
  14. Permission is hereby granted, free of charge, to any person
  15. obtaining a copy of this software and associated documentation
  16. files (the "Software"), to deal in the Software without
  17. restriction, including without limitation the rights to use,
  18. copy, modify, merge, publish, distribute, sublicense, and/or sell
  19. copies of the Software, and to permit persons to whom the
  20. Software is furnished to do so, subject to the following
  21. conditions:
  22. The above copyright notice and this permission notice shall be
  23. included in all copies or substantial portions of the Software.
  24. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  26. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  28. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  29. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  30. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  31. OTHER DEALINGS IN THE SOFTWARE.
  32. */
  33. var mul_table = [
  34. 512,512,456,512,328,456,335,512,405,328,271,456,388,335,292,512,
  35. 454,405,364,328,298,271,496,456,420,388,360,335,312,292,273,512,
  36. 482,454,428,405,383,364,345,328,312,298,284,271,259,496,475,456,
  37. 437,420,404,388,374,360,347,335,323,312,302,292,282,273,265,512,
  38. 497,482,468,454,441,428,417,405,394,383,373,364,354,345,337,328,
  39. 320,312,305,298,291,284,278,271,265,259,507,496,485,475,465,456,
  40. 446,437,428,420,412,404,396,388,381,374,367,360,354,347,341,335,
  41. 329,323,318,312,307,302,297,292,287,282,278,273,269,265,261,512,
  42. 505,497,489,482,475,468,461,454,447,441,435,428,422,417,411,405,
  43. 399,394,389,383,378,373,368,364,359,354,350,345,341,337,332,328,
  44. 324,320,316,312,309,305,301,298,294,291,287,284,281,278,274,271,
  45. 268,265,262,259,257,507,501,496,491,485,480,475,470,465,460,456,
  46. 451,446,442,437,433,428,424,420,416,412,408,404,400,396,392,388,
  47. 385,381,377,374,370,367,363,360,357,354,350,347,344,341,338,335,
  48. 332,329,326,323,320,318,315,312,310,307,304,302,299,297,294,292,
  49. 289,287,285,282,280,278,275,273,271,269,267,265,263,261,259];
  50. var shg_table = [
  51. 9, 11, 12, 13, 13, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17,
  52. 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19,
  53. 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20,
  54. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21,
  55. 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
  56. 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22,
  57. 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
  58. 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23,
  59. 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
  60. 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
  61. 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
  62. 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  63. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  64. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  65. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  66. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24 ];
  67. function stackBlurImage( imageID, canvasID, radius, blurAlphaChannel )
  68. {
  69. var img = document.getElementById( imageID );
  70. var w = img.naturalWidth;
  71. var h = img.naturalHeight;
  72. var canvas = document.getElementById( canvasID );
  73. canvas.style.width = w + "px";
  74. canvas.style.height = h + "px";
  75. canvas.width = w;
  76. canvas.height = h;
  77. var context = canvas.getContext("2d");
  78. context.clearRect( 0, 0, w, h );
  79. context.drawImage( img, 0, 0 );
  80. if ( isNaN(radius) || radius < 1 ) return;
  81. if ( blurAlphaChannel )
  82. stackBlurCanvasRGBA( canvasID, 0, 0, w, h, radius );
  83. else
  84. stackBlurCanvasRGB( canvasID, 0, 0, w, h, radius );
  85. }
  86. function stackBlurCanvasRGBA( id, top_x, top_y, width, height, radius )
  87. {
  88. if ( isNaN(radius) || radius < 1 ) return;
  89. radius |= 0;
  90. var canvas = document.getElementById( id );
  91. var context = canvas.getContext("2d");
  92. var imageData;
  93. try {
  94. try {
  95. imageData = context.getImageData( top_x, top_y, width, height );
  96. } catch(e) {
  97. // NOTE: this part is supposedly only needed if you want to work with local files
  98. // so it might be okay to remove the whole try/catch block and just use
  99. // imageData = context.getImageData( top_x, top_y, width, height );
  100. try {
  101. netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
  102. imageData = context.getImageData( top_x, top_y, width, height );
  103. } catch(e) {
  104. alert("Cannot access local image");
  105. throw new Error("unable to access local image data: " + e);
  106. return;
  107. }
  108. }
  109. } catch(e) {
  110. alert("Cannot access image");
  111. throw new Error("unable to access image data: " + e);
  112. }
  113. var pixels = imageData.data;
  114. var x, y, i, p, yp, yi, yw, r_sum, g_sum, b_sum, a_sum,
  115. r_out_sum, g_out_sum, b_out_sum, a_out_sum,
  116. r_in_sum, g_in_sum, b_in_sum, a_in_sum,
  117. pr, pg, pb, pa, rbs;
  118. var div = radius + radius + 1;
  119. var w4 = width << 2;
  120. var widthMinus1 = width - 1;
  121. var heightMinus1 = height - 1;
  122. var radiusPlus1 = radius + 1;
  123. var sumFactor = radiusPlus1 * ( radiusPlus1 + 1 ) / 2;
  124. var stackStart = new BlurStack();
  125. var stack = stackStart;
  126. for ( i = 1; i < div; i++ )
  127. {
  128. stack = stack.next = new BlurStack();
  129. if ( i == radiusPlus1 ) var stackEnd = stack;
  130. }
  131. stack.next = stackStart;
  132. var stackIn = null;
  133. var stackOut = null;
  134. yw = yi = 0;
  135. var mul_sum = mul_table[radius];
  136. var shg_sum = shg_table[radius];
  137. for ( y = 0; y < height; y++ )
  138. {
  139. r_in_sum = g_in_sum = b_in_sum = a_in_sum = r_sum = g_sum = b_sum = a_sum = 0;
  140. r_out_sum = radiusPlus1 * ( pr = pixels[yi] );
  141. g_out_sum = radiusPlus1 * ( pg = pixels[yi+1] );
  142. b_out_sum = radiusPlus1 * ( pb = pixels[yi+2] );
  143. a_out_sum = radiusPlus1 * ( pa = pixels[yi+3] );
  144. r_sum += sumFactor * pr;
  145. g_sum += sumFactor * pg;
  146. b_sum += sumFactor * pb;
  147. a_sum += sumFactor * pa;
  148. stack = stackStart;
  149. for( i = 0; i < radiusPlus1; i++ )
  150. {
  151. stack.r = pr;
  152. stack.g = pg;
  153. stack.b = pb;
  154. stack.a = pa;
  155. stack = stack.next;
  156. }
  157. for( i = 1; i < radiusPlus1; i++ )
  158. {
  159. p = yi + (( widthMinus1 < i ? widthMinus1 : i ) << 2 );
  160. r_sum += ( stack.r = ( pr = pixels[p])) * ( rbs = radiusPlus1 - i );
  161. g_sum += ( stack.g = ( pg = pixels[p+1])) * rbs;
  162. b_sum += ( stack.b = ( pb = pixels[p+2])) * rbs;
  163. a_sum += ( stack.a = ( pa = pixels[p+3])) * rbs;
  164. r_in_sum += pr;
  165. g_in_sum += pg;
  166. b_in_sum += pb;
  167. a_in_sum += pa;
  168. stack = stack.next;
  169. }
  170. stackIn = stackStart;
  171. stackOut = stackEnd;
  172. for ( x = 0; x < width; x++ )
  173. {
  174. pixels[yi+3] = pa = (a_sum * mul_sum) >> shg_sum;
  175. if ( pa != 0 )
  176. {
  177. pa = 255 / pa;
  178. pixels[yi] = ((r_sum * mul_sum) >> shg_sum) * pa;
  179. pixels[yi+1] = ((g_sum * mul_sum) >> shg_sum) * pa;
  180. pixels[yi+2] = ((b_sum * mul_sum) >> shg_sum) * pa;
  181. } else {
  182. pixels[yi] = pixels[yi+1] = pixels[yi+2] = 0;
  183. }
  184. r_sum -= r_out_sum;
  185. g_sum -= g_out_sum;
  186. b_sum -= b_out_sum;
  187. a_sum -= a_out_sum;
  188. r_out_sum -= stackIn.r;
  189. g_out_sum -= stackIn.g;
  190. b_out_sum -= stackIn.b;
  191. a_out_sum -= stackIn.a;
  192. p = ( yw + ( ( p = x + radius + 1 ) < widthMinus1 ? p : widthMinus1 ) ) << 2;
  193. r_in_sum += ( stackIn.r = pixels[p]);
  194. g_in_sum += ( stackIn.g = pixels[p+1]);
  195. b_in_sum += ( stackIn.b = pixels[p+2]);
  196. a_in_sum += ( stackIn.a = pixels[p+3]);
  197. r_sum += r_in_sum;
  198. g_sum += g_in_sum;
  199. b_sum += b_in_sum;
  200. a_sum += a_in_sum;
  201. stackIn = stackIn.next;
  202. r_out_sum += ( pr = stackOut.r );
  203. g_out_sum += ( pg = stackOut.g );
  204. b_out_sum += ( pb = stackOut.b );
  205. a_out_sum += ( pa = stackOut.a );
  206. r_in_sum -= pr;
  207. g_in_sum -= pg;
  208. b_in_sum -= pb;
  209. a_in_sum -= pa;
  210. stackOut = stackOut.next;
  211. yi += 4;
  212. }
  213. yw += width;
  214. }
  215. for ( x = 0; x < width; x++ )
  216. {
  217. g_in_sum = b_in_sum = a_in_sum = r_in_sum = g_sum = b_sum = a_sum = r_sum = 0;
  218. yi = x << 2;
  219. r_out_sum = radiusPlus1 * ( pr = pixels[yi]);
  220. g_out_sum = radiusPlus1 * ( pg = pixels[yi+1]);
  221. b_out_sum = radiusPlus1 * ( pb = pixels[yi+2]);
  222. a_out_sum = radiusPlus1 * ( pa = pixels[yi+3]);
  223. r_sum += sumFactor * pr;
  224. g_sum += sumFactor * pg;
  225. b_sum += sumFactor * pb;
  226. a_sum += sumFactor * pa;
  227. stack = stackStart;
  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. yp = width;
  237. for( i = 1; i <= radius; i++ )
  238. {
  239. yi = ( yp + x ) << 2;
  240. r_sum += ( stack.r = ( pr = pixels[yi])) * ( rbs = radiusPlus1 - i );
  241. g_sum += ( stack.g = ( pg = pixels[yi+1])) * rbs;
  242. b_sum += ( stack.b = ( pb = pixels[yi+2])) * rbs;
  243. a_sum += ( stack.a = ( pa = pixels[yi+3])) * rbs;
  244. r_in_sum += pr;
  245. g_in_sum += pg;
  246. b_in_sum += pb;
  247. a_in_sum += pa;
  248. stack = stack.next;
  249. if( i < heightMinus1 )
  250. {
  251. yp += width;
  252. }
  253. }
  254. yi = x;
  255. stackIn = stackStart;
  256. stackOut = stackEnd;
  257. for ( y = 0; y < height; y++ )
  258. {
  259. p = yi << 2;
  260. pixels[p+3] = pa = (a_sum * mul_sum) >> shg_sum;
  261. if ( pa > 0 )
  262. {
  263. pa = 255 / pa;
  264. pixels[p] = ((r_sum * mul_sum) >> shg_sum ) * pa;
  265. pixels[p+1] = ((g_sum * mul_sum) >> shg_sum ) * pa;
  266. pixels[p+2] = ((b_sum * mul_sum) >> shg_sum ) * pa;
  267. } else {
  268. pixels[p] = pixels[p+1] = pixels[p+2] = 0;
  269. }
  270. r_sum -= r_out_sum;
  271. g_sum -= g_out_sum;
  272. b_sum -= b_out_sum;
  273. a_sum -= a_out_sum;
  274. r_out_sum -= stackIn.r;
  275. g_out_sum -= stackIn.g;
  276. b_out_sum -= stackIn.b;
  277. a_out_sum -= stackIn.a;
  278. p = ( x + (( ( p = y + radiusPlus1) < heightMinus1 ? p : heightMinus1 ) * width )) << 2;
  279. r_sum += ( r_in_sum += ( stackIn.r = pixels[p]));
  280. g_sum += ( g_in_sum += ( stackIn.g = pixels[p+1]));
  281. b_sum += ( b_in_sum += ( stackIn.b = pixels[p+2]));
  282. a_sum += ( a_in_sum += ( stackIn.a = pixels[p+3]));
  283. stackIn = stackIn.next;
  284. r_out_sum += ( pr = stackOut.r );
  285. g_out_sum += ( pg = stackOut.g );
  286. b_out_sum += ( pb = stackOut.b );
  287. a_out_sum += ( pa = stackOut.a );
  288. r_in_sum -= pr;
  289. g_in_sum -= pg;
  290. b_in_sum -= pb;
  291. a_in_sum -= pa;
  292. stackOut = stackOut.next;
  293. yi += width;
  294. }
  295. }
  296. context.putImageData( imageData, top_x, top_y );
  297. }
  298. function stackBlurCanvasRGB( id, top_x, top_y, width, height, radius )
  299. {
  300. if ( isNaN(radius) || radius < 1 ) return;
  301. radius |= 0;
  302. var canvas = document.getElementById( id );
  303. var context = canvas.getContext("2d");
  304. var imageData;
  305. try {
  306. try {
  307. imageData = context.getImageData( top_x, top_y, width, height );
  308. } catch(e) {
  309. // NOTE: this part is supposedly only needed if you want to work with local files
  310. // so it might be okay to remove the whole try/catch block and just use
  311. // imageData = context.getImageData( top_x, top_y, width, height );
  312. try {
  313. netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
  314. imageData = context.getImageData( top_x, top_y, width, height );
  315. } catch(e) {
  316. alert("Cannot access local image");
  317. throw new Error("unable to access local image data: " + e);
  318. return;
  319. }
  320. }
  321. } catch(e) {
  322. alert("Cannot access image");
  323. throw new Error("unable to access image data: " + e);
  324. }
  325. var pixels = imageData.data;
  326. var x, y, i, p, yp, yi, yw, r_sum, g_sum, b_sum,
  327. r_out_sum, g_out_sum, b_out_sum,
  328. r_in_sum, g_in_sum, b_in_sum,
  329. pr, pg, pb, rbs;
  330. var div = radius + radius + 1;
  331. var w4 = width << 2;
  332. var widthMinus1 = width - 1;
  333. var heightMinus1 = height - 1;
  334. var radiusPlus1 = radius + 1;
  335. var sumFactor = radiusPlus1 * ( radiusPlus1 + 1 ) / 2;
  336. var stackStart = new BlurStack();
  337. var stack = stackStart;
  338. for ( i = 1; i < div; i++ )
  339. {
  340. stack = stack.next = new BlurStack();
  341. if ( i == radiusPlus1 ) var stackEnd = stack;
  342. }
  343. stack.next = stackStart;
  344. var stackIn = null;
  345. var stackOut = null;
  346. yw = yi = 0;
  347. var mul_sum = mul_table[radius];
  348. var shg_sum = shg_table[radius];
  349. for ( y = 0; y < height; y++ )
  350. {
  351. r_in_sum = g_in_sum = b_in_sum = r_sum = g_sum = b_sum = 0;
  352. r_out_sum = radiusPlus1 * ( pr = pixels[yi] );
  353. g_out_sum = radiusPlus1 * ( pg = pixels[yi+1] );
  354. b_out_sum = radiusPlus1 * ( pb = pixels[yi+2] );
  355. r_sum += sumFactor * pr;
  356. g_sum += sumFactor * pg;
  357. b_sum += sumFactor * pb;
  358. stack = stackStart;
  359. for( i = 0; i < radiusPlus1; i++ )
  360. {
  361. stack.r = pr;
  362. stack.g = pg;
  363. stack.b = pb;
  364. stack = stack.next;
  365. }
  366. for( i = 1; i < radiusPlus1; i++ )
  367. {
  368. p = yi + (( widthMinus1 < i ? widthMinus1 : i ) << 2 );
  369. r_sum += ( stack.r = ( pr = pixels[p])) * ( rbs = radiusPlus1 - i );
  370. g_sum += ( stack.g = ( pg = pixels[p+1])) * rbs;
  371. b_sum += ( stack.b = ( pb = pixels[p+2])) * rbs;
  372. r_in_sum += pr;
  373. g_in_sum += pg;
  374. b_in_sum += pb;
  375. stack = stack.next;
  376. }
  377. stackIn = stackStart;
  378. stackOut = stackEnd;
  379. for ( x = 0; x < width; x++ )
  380. {
  381. pixels[yi] = (r_sum * mul_sum) >> shg_sum;
  382. pixels[yi+1] = (g_sum * mul_sum) >> shg_sum;
  383. pixels[yi+2] = (b_sum * mul_sum) >> shg_sum;
  384. r_sum -= r_out_sum;
  385. g_sum -= g_out_sum;
  386. b_sum -= b_out_sum;
  387. r_out_sum -= stackIn.r;
  388. g_out_sum -= stackIn.g;
  389. b_out_sum -= stackIn.b;
  390. p = ( yw + ( ( p = x + radius + 1 ) < widthMinus1 ? p : widthMinus1 ) ) << 2;
  391. r_in_sum += ( stackIn.r = pixels[p]);
  392. g_in_sum += ( stackIn.g = pixels[p+1]);
  393. b_in_sum += ( stackIn.b = pixels[p+2]);
  394. r_sum += r_in_sum;
  395. g_sum += g_in_sum;
  396. b_sum += b_in_sum;
  397. stackIn = stackIn.next;
  398. r_out_sum += ( pr = stackOut.r );
  399. g_out_sum += ( pg = stackOut.g );
  400. b_out_sum += ( pb = stackOut.b );
  401. r_in_sum -= pr;
  402. g_in_sum -= pg;
  403. b_in_sum -= pb;
  404. stackOut = stackOut.next;
  405. yi += 4;
  406. }
  407. yw += width;
  408. }
  409. for ( x = 0; x < width; x++ )
  410. {
  411. g_in_sum = b_in_sum = r_in_sum = g_sum = b_sum = r_sum = 0;
  412. yi = x << 2;
  413. r_out_sum = radiusPlus1 * ( pr = pixels[yi]);
  414. g_out_sum = radiusPlus1 * ( pg = pixels[yi+1]);
  415. b_out_sum = radiusPlus1 * ( pb = pixels[yi+2]);
  416. r_sum += sumFactor * pr;
  417. g_sum += sumFactor * pg;
  418. b_sum += sumFactor * pb;
  419. stack = stackStart;
  420. for( i = 0; i < radiusPlus1; i++ )
  421. {
  422. stack.r = pr;
  423. stack.g = pg;
  424. stack.b = pb;
  425. stack = stack.next;
  426. }
  427. yp = width;
  428. for( i = 1; i <= radius; i++ )
  429. {
  430. yi = ( yp + x ) << 2;
  431. r_sum += ( stack.r = ( pr = pixels[yi])) * ( rbs = radiusPlus1 - i );
  432. g_sum += ( stack.g = ( pg = pixels[yi+1])) * rbs;
  433. b_sum += ( stack.b = ( pb = pixels[yi+2])) * rbs;
  434. r_in_sum += pr;
  435. g_in_sum += pg;
  436. b_in_sum += pb;
  437. stack = stack.next;
  438. if( i < heightMinus1 )
  439. {
  440. yp += width;
  441. }
  442. }
  443. yi = x;
  444. stackIn = stackStart;
  445. stackOut = stackEnd;
  446. for ( y = 0; y < height; y++ )
  447. {
  448. p = yi << 2;
  449. pixels[p] = (r_sum * mul_sum) >> shg_sum;
  450. pixels[p+1] = (g_sum * mul_sum) >> shg_sum;
  451. pixels[p+2] = (b_sum * mul_sum) >> shg_sum;
  452. r_sum -= r_out_sum;
  453. g_sum -= g_out_sum;
  454. b_sum -= b_out_sum;
  455. r_out_sum -= stackIn.r;
  456. g_out_sum -= stackIn.g;
  457. b_out_sum -= stackIn.b;
  458. p = ( x + (( ( p = y + radiusPlus1) < heightMinus1 ? p : heightMinus1 ) * width )) << 2;
  459. r_sum += ( r_in_sum += ( stackIn.r = pixels[p]));
  460. g_sum += ( g_in_sum += ( stackIn.g = pixels[p+1]));
  461. b_sum += ( b_in_sum += ( stackIn.b = pixels[p+2]));
  462. stackIn = stackIn.next;
  463. r_out_sum += ( pr = stackOut.r );
  464. g_out_sum += ( pg = stackOut.g );
  465. b_out_sum += ( pb = stackOut.b );
  466. r_in_sum -= pr;
  467. g_in_sum -= pg;
  468. b_in_sum -= pb;
  469. stackOut = stackOut.next;
  470. yi += width;
  471. }
  472. }
  473. context.putImageData( imageData, top_x, top_y );
  474. }
  475. function BlurStack()
  476. {
  477. this.r = 0;
  478. this.g = 0;
  479. this.b = 0;
  480. this.a = 0;
  481. this.next = null;
  482. }