Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

canvasimages.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /**
  2. * @fileOverview HTML5 canvas image functions
  3. * @author Ian Moore (imoore76 at yahoo dot com)
  4. * @version $Id: canvasimages.js 591 2015-04-11 22:40:47Z imoore76 $
  5. * @copyright Copyright (C) 2010-2015 Ian Moore (imoore76 at yahoo dot com)
  6. */
  7. /**
  8. * Returns true if broswer supports canvas
  9. * @return {Boolean} true if broswer supports canvas
  10. */
  11. var __vboxIsCanvasSupported = null; // cached
  12. var isCanvasSupported = function(){
  13. if(__vboxIsCanvasSupported === null) {
  14. try {
  15. var elem = document.createElement('canvas');
  16. __vboxIsCanvasSupported = !!(elem && elem.getContext && elem.getContext('2d'));
  17. } catch (err) {
  18. __vboxIsCanvasSupported = false;
  19. }
  20. }
  21. return __vboxIsCanvasSupported;
  22. };
  23. var __vboxPreviewCanvasCache = [];
  24. function vboxDrawPreviewCanvas(can, imageObj, text, width, height) {
  25. var screenMargin = 7;
  26. var margin = 10;
  27. var resizeToImage = (imageObj && (imageObj.width == width));
  28. // Height / width comes from direct image values
  29. if(imageObj && resizeToImage) {
  30. height = imageObj.height;
  31. width = imageObj.width;
  32. // Set height while maintaining aspect ratio
  33. } else if (imageObj) {
  34. height = imageObj.height * (width/imageObj.width);
  35. }
  36. // Margins are added to width
  37. height += ((margin+screenMargin)*2);
  38. width += ((margin+screenMargin)*2);
  39. // Does canvas still exist?
  40. // VM selection can change while this function is running
  41. // in which case the canvas goes away
  42. if(!can) return;
  43. // Set canvas values
  44. can.height = height;
  45. can.width = width;
  46. var ctx = can.getContext('2d');
  47. // Clear the canvas
  48. ctx.clearRect(0,0,width,height);
  49. ctx.save();
  50. // Draw and cache monitor image if it is not present
  51. if(!__vboxPreviewCanvasCache[width+'x'+height]) {
  52. var cachedCanvas = document.createElement('canvas');
  53. cachedCanvas.width = width;
  54. cachedCanvas.height = height;
  55. var cachedCtx = cachedCanvas.getContext('2d');
  56. cachedCtx.beginPath();
  57. cachedCtx.strokeStyle = "#000000";
  58. cachedCtx.lineWidth = 0.3;
  59. cachedCtx.lineCap = 'butt';
  60. cachedCtx.moveTo(margin*2,margin);
  61. // top and top right
  62. cachedCtx.lineTo(width-(margin*2), margin);
  63. cachedCtx.arcTo(width-margin, margin, width-margin,margin*2, margin);
  64. // Side and bottom right
  65. cachedCtx.lineTo(width-margin, height-(margin*2));
  66. cachedCtx.arcTo(width-margin, height-margin, width-(margin*2), height-margin, margin);
  67. // bottom and bottom left
  68. cachedCtx.lineTo(margin*2, height-margin);
  69. cachedCtx.arcTo(margin, height-margin, margin, height-(margin*2), margin);
  70. // Left line and top left
  71. cachedCtx.lineTo(margin, margin*2);
  72. cachedCtx.arcTo(margin, margin, margin * 2, margin, margin);
  73. cachedCtx.closePath();
  74. cachedCtx.save();
  75. cachedCtx.shadowOffsetX = 5;
  76. cachedCtx.shadowOffsetY = 5;
  77. cachedCtx.shadowBlur = 4;
  78. cachedCtx.shadowColor = "rgba(30, 30, 30, 0.2)";
  79. var grad = cachedCtx.createLinearGradient(0, margin, 0, height);
  80. grad.addColorStop(0, "rgb(200,200,200)");
  81. grad.addColorStop(0.4, "rgb(100,100,100)");
  82. grad.addColorStop(0.5, "rgb(66,66,66)");
  83. grad.addColorStop(0.7, "rgb(100,100,100)");
  84. grad.addColorStop(1, "rgb(200,200,200)");
  85. cachedCtx.fillStyle = grad;
  86. cachedCtx.fill();
  87. // Redraw so that shadow is seen on all sides
  88. cachedCtx.shadowOffsetX = -5;
  89. cachedCtx.shadowOffsetY = -5;
  90. cachedCtx.fill();
  91. cachedCtx.restore();
  92. cachedCtx.fillRect(margin+screenMargin,margin+screenMargin,width-(margin*2)-(screenMargin*2),height-(margin*2)-(screenMargin*2));
  93. cachedCtx.stroke();
  94. cachedCtx.restore();
  95. var cvs = document.createElement('canvas');
  96. /* Gloss */
  97. var rectX = 0;
  98. var rectY = 0;
  99. var rectWidth = width-(margin+screenMargin)*2;
  100. var rectHeight = height-(margin+screenMargin)*2;
  101. cvs.width = rectWidth;
  102. cvs.height = rectHeight;
  103. var ctxBlur = cvs.getContext('2d');
  104. ctxBlur.beginPath();
  105. ctxBlur.lineWidth = 1;
  106. ctxBlur.strokeStyle = "#000000";
  107. ctxBlur.moveTo(rectX,rectY);
  108. ctxBlur.lineTo(rectWidth, rectY);
  109. ctxBlur.lineTo(rectWidth,rectHeight*1.0/3.0);
  110. ctxBlur.bezierCurveTo(rectX+rectWidth / 2.0, rectY + rectHeight * 1.0/3.0,
  111. rectX+rectWidth / 2.0, rectY + rectHeight * 2.0/3.0,
  112. rectX, rectY + rectHeight * 2.0/3.0);
  113. ctxBlur.closePath();
  114. ctxBlur.fillStyle="rgba(255,255,255,0.3)";
  115. ctxBlur.fill();
  116. stackBlurCanvasRGBA( cvs, 0, 0, rectWidth, rectHeight, 17 );
  117. ctx.drawImage(cvs, margin+screenMargin, margin+screenMargin, rectWidth, rectHeight);
  118. __vboxPreviewCanvasCache[width+'x'+height] = {
  119. 'monitor' : cachedCanvas,
  120. 'gloss' : cvs
  121. };
  122. }
  123. // Draw cached monitor canvas
  124. ctx.drawImage(__vboxPreviewCanvasCache[width+'x'+height]['monitor'], 0, 0, width, height);
  125. /* Screenshot */
  126. if(imageObj) {
  127. ctx.drawImage(imageObj, 0, 0, imageObj.width, imageObj.height, (margin+screenMargin), (margin+screenMargin), width-(margin*2)-(screenMargin*2),height-(margin*2)-(screenMargin*2));
  128. }
  129. // Draw cached gloss canvas
  130. ctx.drawImage(__vboxPreviewCanvasCache[width+'x'+height]['gloss'], 0, 0, width, height);
  131. /* Text */
  132. if(!imageObj) {
  133. txtCan = document.createElement('canvas');
  134. txtCan.width = width-(margin+screenMargin)*2;
  135. txtCan.height = height-(margin+screenMargin)*2;
  136. fitTextToCanvas(txtCan, text, 18);
  137. ctx.drawImage(txtCan, (margin+screenMargin), (margin+screenMargin));
  138. }
  139. return;
  140. }
  141. /* HELPERS */
  142. /**
  143. * Fit text to a canvas element
  144. *
  145. * @author Ian Moore
  146. */
  147. var fitTextToCanvas = function(can, text, fontSize) {
  148. var lineHeightOffset = 1.4;
  149. var lineHeight = fontSize * lineHeightOffset;
  150. var minFontSize = 10;
  151. var padding = 2;
  152. var words = text.split(" ");
  153. var maxWidth = can.width;
  154. var maxHeight = can.height;
  155. var context = can.getContext('2d');
  156. context.moveTo(0,0);
  157. var wrapTextLines = function() {
  158. context.font = "bold " + fontSize + "pt Arial";
  159. var line = '';
  160. var lines = [];
  161. for (var n = 0; n < words.length; n++) {
  162. var testLine = line + (line.length ? ' ' : '') + words[n];
  163. if((context.measureText(testLine).width + padding) > maxWidth) {
  164. // Only one word is too big
  165. if(testLine.indexOf(' ') == -1) {
  166. if(fontSize > minFontSize) {
  167. fontSize *= 0.9;
  168. return wrapTextLines();
  169. }
  170. line = testLine;
  171. } else {
  172. lines[lines.length] = line;
  173. line = words[n];
  174. }
  175. } else {
  176. line = testLine;
  177. }
  178. }
  179. if(line.length) {
  180. if((context.measureText(line).width + padding) > maxWidth && fontSize > minFontSize) {
  181. fontSize *= 0.9;
  182. return wrapTextLines();
  183. }
  184. lines[lines.length] = line;
  185. }
  186. return lines;
  187. };
  188. // Initial wrap
  189. lines = wrapTextLines();
  190. // Since text will be aligned to the bottom, we subtract
  191. // one lineheight addition because it will be off the
  192. // visible canvas and should not be included in calculations
  193. while(((lines.length * lineHeight)-(lineHeight-fontSize) > maxHeight) && fontSize > minFontSize) {
  194. fontSize *= 0.9;
  195. lines = wrapTextLines();
  196. lineHeight = fontSize * lineHeightOffset;
  197. }
  198. context.fillStyle = "#ffffff";
  199. context.textAlign = "center";
  200. context.textBaseline = 'alphabetic';
  201. context.strokeStyle = "#ff0000";
  202. var totalHeight = Math.round((lines.length * lineHeight)-(lineHeight-fontSize));
  203. for(var i = 0; i < lines.length; i++) {
  204. /*
  205. Uncomment to debug line heights
  206. context.moveTo(0, (maxHeight/2)-(totalHeight/2)+(lineHeight*i));
  207. context.lineTo(maxWidth, (maxHeight/2)-(totalHeight/2)+(lineHeight*i));
  208. context.stroke();
  209. context.moveTo(0, (maxHeight/2)-(totalHeight/2)+(lineHeight*i)+lineHeight);
  210. context.lineTo(maxWidth, (maxHeight/2)-(totalHeight/2)+(lineHeight*i)+lineHeight);
  211. context.stroke();
  212. */
  213. context.fillText(lines[i],(maxWidth/2),(maxHeight/2)-(totalHeight/2)+(lineHeight*i)+lineHeight-(lineHeight-fontSize));
  214. }
  215. return can;
  216. };
  217. /*
  218. StackBlur - a fast almost Gaussian Blur For Canvas
  219. Version: 0.5
  220. Author: Mario Klingemann
  221. Contact: mario@quasimondo.com
  222. Website: http://www.quasimondo.com/StackBlurForCanvas
  223. Twitter: @quasimondo
  224. In case you find this class useful - especially in commercial projects -
  225. I am not totally unhappy for a small donation to my PayPal account
  226. mario@quasimondo.de
  227. Or support me on flattr:
  228. https://flattr.com/thing/72791/StackBlur-a-fast-almost-Gaussian-Blur-Effect-for-CanvasJavascript
  229. Copyright (c) 2010 Mario Klingemann
  230. Permission is hereby granted, free of charge, to any person
  231. obtaining a copy of this software and associated documentation
  232. files (the "Software"), to deal in the Software without
  233. restriction, including without limitation the rights to use,
  234. copy, modify, merge, publish, distribute, sublicense, and/or sell
  235. copies of the Software, and to permit persons to whom the
  236. Software is furnished to do so, subject to the following
  237. conditions:
  238. The above copyright notice and this permission notice shall be
  239. included in all copies or substantial portions of the Software.
  240. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  241. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  242. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  243. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  244. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  245. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  246. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  247. OTHER DEALINGS IN THE SOFTWARE.
  248. */
  249. var mul_table = [
  250. 512,512,456,512,328,456,335,512,405,328,271,456,388,335,292,512,
  251. 454,405,364,328,298,271,496,456,420,388,360,335,312,292,273,512,
  252. 482,454,428,405,383,364,345,328,312,298,284,271,259,496,475,456,
  253. 437,420,404,388,374,360,347,335,323,312,302,292,282,273,265,512,
  254. 497,482,468,454,441,428,417,405,394,383,373,364,354,345,337,328,
  255. 320,312,305,298,291,284,278,271,265,259,507,496,485,475,465,456,
  256. 446,437,428,420,412,404,396,388,381,374,367,360,354,347,341,335,
  257. 329,323,318,312,307,302,297,292,287,282,278,273,269,265,261,512,
  258. 505,497,489,482,475,468,461,454,447,441,435,428,422,417,411,405,
  259. 399,394,389,383,378,373,368,364,359,354,350,345,341,337,332,328,
  260. 324,320,316,312,309,305,301,298,294,291,287,284,281,278,274,271,
  261. 268,265,262,259,257,507,501,496,491,485,480,475,470,465,460,456,
  262. 451,446,442,437,433,428,424,420,416,412,408,404,400,396,392,388,
  263. 385,381,377,374,370,367,363,360,357,354,350,347,344,341,338,335,
  264. 332,329,326,323,320,318,315,312,310,307,304,302,299,297,294,292,
  265. 289,287,285,282,280,278,275,273,271,269,267,265,263,261,259];
  266. var shg_table = [
  267. 9, 11, 12, 13, 13, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17,
  268. 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19,
  269. 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20,
  270. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21,
  271. 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
  272. 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22,
  273. 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
  274. 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23,
  275. 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
  276. 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
  277. 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
  278. 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  279. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  280. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  281. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  282. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24 ];
  283. var stackBlurCanvasRGBA = function( canvas, top_x, top_y, width, height, radius )
  284. {
  285. if ( isNaN(radius) || radius < 1 ) return;
  286. radius |= 0;
  287. //var canvas = document.getElementById( id );
  288. var context = canvas.getContext("2d");
  289. var imageData;
  290. try {
  291. try {
  292. imageData = context.getImageData( top_x, top_y, width, height );
  293. } catch(e) {
  294. // NOTE: this part is supposedly only needed if you want to work with local files
  295. // so it might be okay to remove the whole try/catch block and just use
  296. // imageData = context.getImageData( top_x, top_y, width, height );
  297. try {
  298. netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
  299. imageData = context.getImageData( top_x, top_y, width, height );
  300. } catch(e) {
  301. alert("Cannot access local image");
  302. throw new Error("unable to access local image data: " + e);
  303. return;
  304. }
  305. }
  306. } catch(e) {
  307. alert("Cannot access image");
  308. throw new Error("unable to access image data: " + e);
  309. }
  310. var pixels = imageData.data;
  311. var x, y, i, p, yp, yi, yw, r_sum, g_sum, b_sum, a_sum,
  312. r_out_sum, g_out_sum, b_out_sum, a_out_sum,
  313. r_in_sum, g_in_sum, b_in_sum, a_in_sum,
  314. pr, pg, pb, pa, rbs;
  315. var div = radius + radius + 1;
  316. var widthMinus1 = width - 1;
  317. var heightMinus1 = height - 1;
  318. var radiusPlus1 = radius + 1;
  319. var sumFactor = radiusPlus1 * ( radiusPlus1 + 1 ) / 2;
  320. var stackStart = new BlurStack();
  321. var stack = stackStart;
  322. for ( i = 1; i < div; i++ )
  323. {
  324. stack = stack.next = new BlurStack();
  325. if ( i == radiusPlus1 ) var stackEnd = stack;
  326. }
  327. stack.next = stackStart;
  328. var stackIn = null;
  329. var stackOut = null;
  330. yw = yi = 0;
  331. var mul_sum = mul_table[radius];
  332. var shg_sum = shg_table[radius];
  333. for ( y = 0; y < height; y++ )
  334. {
  335. r_in_sum = g_in_sum = b_in_sum = a_in_sum = r_sum = g_sum = b_sum = a_sum = 0;
  336. r_out_sum = radiusPlus1 * ( pr = pixels[yi] );
  337. g_out_sum = radiusPlus1 * ( pg = pixels[yi+1] );
  338. b_out_sum = radiusPlus1 * ( pb = pixels[yi+2] );
  339. a_out_sum = radiusPlus1 * ( pa = pixels[yi+3] );
  340. r_sum += sumFactor * pr;
  341. g_sum += sumFactor * pg;
  342. b_sum += sumFactor * pb;
  343. a_sum += sumFactor * pa;
  344. stack = stackStart;
  345. for( i = 0; i < radiusPlus1; i++ )
  346. {
  347. stack.r = pr;
  348. stack.g = pg;
  349. stack.b = pb;
  350. stack.a = pa;
  351. stack = stack.next;
  352. }
  353. for( i = 1; i < radiusPlus1; i++ )
  354. {
  355. p = yi + (( widthMinus1 < i ? widthMinus1 : i ) << 2 );
  356. r_sum += ( stack.r = ( pr = pixels[p])) * ( rbs = radiusPlus1 - i );
  357. g_sum += ( stack.g = ( pg = pixels[p+1])) * rbs;
  358. b_sum += ( stack.b = ( pb = pixels[p+2])) * rbs;
  359. a_sum += ( stack.a = ( pa = pixels[p+3])) * rbs;
  360. r_in_sum += pr;
  361. g_in_sum += pg;
  362. b_in_sum += pb;
  363. a_in_sum += pa;
  364. stack = stack.next;
  365. }
  366. stackIn = stackStart;
  367. stackOut = stackEnd;
  368. for ( x = 0; x < width; x++ )
  369. {
  370. pixels[yi+3] = pa = (a_sum * mul_sum) >> shg_sum;
  371. if ( pa != 0 )
  372. {
  373. pa = 255 / pa;
  374. pixels[yi] = ((r_sum * mul_sum) >> shg_sum) * pa;
  375. pixels[yi+1] = ((g_sum * mul_sum) >> shg_sum) * pa;
  376. pixels[yi+2] = ((b_sum * mul_sum) >> shg_sum) * pa;
  377. } else {
  378. pixels[yi] = pixels[yi+1] = pixels[yi+2] = 0;
  379. }
  380. r_sum -= r_out_sum;
  381. g_sum -= g_out_sum;
  382. b_sum -= b_out_sum;
  383. a_sum -= a_out_sum;
  384. r_out_sum -= stackIn.r;
  385. g_out_sum -= stackIn.g;
  386. b_out_sum -= stackIn.b;
  387. a_out_sum -= stackIn.a;
  388. p = ( yw + ( ( p = x + radius + 1 ) < widthMinus1 ? p : widthMinus1 ) ) << 2;
  389. r_in_sum += ( stackIn.r = pixels[p]);
  390. g_in_sum += ( stackIn.g = pixels[p+1]);
  391. b_in_sum += ( stackIn.b = pixels[p+2]);
  392. a_in_sum += ( stackIn.a = pixels[p+3]);
  393. r_sum += r_in_sum;
  394. g_sum += g_in_sum;
  395. b_sum += b_in_sum;
  396. a_sum += a_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. a_out_sum += ( pa = stackOut.a );
  402. r_in_sum -= pr;
  403. g_in_sum -= pg;
  404. b_in_sum -= pb;
  405. a_in_sum -= pa;
  406. stackOut = stackOut.next;
  407. yi += 4;
  408. }
  409. yw += width;
  410. }
  411. for ( x = 0; x < width; x++ )
  412. {
  413. g_in_sum = b_in_sum = a_in_sum = r_in_sum = g_sum = b_sum = a_sum = r_sum = 0;
  414. yi = x << 2;
  415. r_out_sum = radiusPlus1 * ( pr = pixels[yi]);
  416. g_out_sum = radiusPlus1 * ( pg = pixels[yi+1]);
  417. b_out_sum = radiusPlus1 * ( pb = pixels[yi+2]);
  418. a_out_sum = radiusPlus1 * ( pa = pixels[yi+3]);
  419. r_sum += sumFactor * pr;
  420. g_sum += sumFactor * pg;
  421. b_sum += sumFactor * pb;
  422. a_sum += sumFactor * pa;
  423. stack = stackStart;
  424. for( i = 0; i < radiusPlus1; i++ )
  425. {
  426. stack.r = pr;
  427. stack.g = pg;
  428. stack.b = pb;
  429. stack.a = pa;
  430. stack = stack.next;
  431. }
  432. yp = width;
  433. for( i = 1; i <= radius; i++ )
  434. {
  435. yi = ( yp + x ) << 2;
  436. r_sum += ( stack.r = ( pr = pixels[yi])) * ( rbs = radiusPlus1 - i );
  437. g_sum += ( stack.g = ( pg = pixels[yi+1])) * rbs;
  438. b_sum += ( stack.b = ( pb = pixels[yi+2])) * rbs;
  439. a_sum += ( stack.a = ( pa = pixels[yi+3])) * rbs;
  440. r_in_sum += pr;
  441. g_in_sum += pg;
  442. b_in_sum += pb;
  443. a_in_sum += pa;
  444. stack = stack.next;
  445. if( i < heightMinus1 )
  446. {
  447. yp += width;
  448. }
  449. }
  450. yi = x;
  451. stackIn = stackStart;
  452. stackOut = stackEnd;
  453. for ( y = 0; y < height; y++ )
  454. {
  455. p = yi << 2;
  456. pixels[p+3] = pa = (a_sum * mul_sum) >> shg_sum;
  457. if ( pa > 0 )
  458. {
  459. pa = 255 / pa;
  460. pixels[p] = ((r_sum * mul_sum) >> shg_sum ) * pa;
  461. pixels[p+1] = ((g_sum * mul_sum) >> shg_sum ) * pa;
  462. pixels[p+2] = ((b_sum * mul_sum) >> shg_sum ) * pa;
  463. } else {
  464. pixels[p] = pixels[p+1] = pixels[p+2] = 0;
  465. }
  466. r_sum -= r_out_sum;
  467. g_sum -= g_out_sum;
  468. b_sum -= b_out_sum;
  469. a_sum -= a_out_sum;
  470. r_out_sum -= stackIn.r;
  471. g_out_sum -= stackIn.g;
  472. b_out_sum -= stackIn.b;
  473. a_out_sum -= stackIn.a;
  474. p = ( x + (( ( p = y + radiusPlus1) < heightMinus1 ? p : heightMinus1 ) * width )) << 2;
  475. r_sum += ( r_in_sum += ( stackIn.r = pixels[p]));
  476. g_sum += ( g_in_sum += ( stackIn.g = pixels[p+1]));
  477. b_sum += ( b_in_sum += ( stackIn.b = pixels[p+2]));
  478. a_sum += ( a_in_sum += ( stackIn.a = pixels[p+3]));
  479. stackIn = stackIn.next;
  480. r_out_sum += ( pr = stackOut.r );
  481. g_out_sum += ( pg = stackOut.g );
  482. b_out_sum += ( pb = stackOut.b );
  483. a_out_sum += ( pa = stackOut.a );
  484. r_in_sum -= pr;
  485. g_in_sum -= pg;
  486. b_in_sum -= pb;
  487. a_in_sum -= pa;
  488. stackOut = stackOut.next;
  489. yi += width;
  490. }
  491. }
  492. context.putImageData( imageData, top_x, top_y );
  493. };
  494. var BlurStack = function()
  495. {
  496. this.r = 0;
  497. this.g = 0;
  498. this.b = 0;
  499. this.a = 0;
  500. this.next = null;
  501. };