Last active
October 13, 2015 22:48
-
-
Save iegik/4268197 to your computer and use it in GitHub Desktop.
Replace broken images with noise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| HTMLCanvasElement.prototype.addNoise = function(r, g, b, a, l) { | |
| // default variables: red, green, blue, alpha, light | |
| a||(a=-.4); | |
| l||(l=.5); | |
| //(!red&&green)||(alpha=green,green=128); | |
| //(!green&&blue)||(brightness=blue,blue=128); | |
| r||(r=1); | |
| g||(g=1); | |
| b||(b=1); | |
| r*=255; | |
| g*=255; | |
| b*=255; | |
| a+=.5; | |
| l*=255; | |
| var ctx = this.getContext('2d'); | |
| var imageData = ctx.getImageData(0, 0, this.width, this.height), | |
| pixels = imageData.data; | |
| for (var i = 0, il = pixels.length; i < il; i += 4) { | |
| // pixel of noize generation | |
| var color = Math.random() * l; | |
| // alpha-composition | |
| !pixels[i]&&pixels[i]!=0 ||(pixels[i] =pixels[i] +(!pixels[i]||128) *color/r*a+pixels[i] *(1 - a)); | |
| !pixels[i+1]&&pixels[i+1]!=0||(pixels[i+1]=pixels[i+1]+(!pixels[i+1]||128)*color/g*a+pixels[i+1]*(1 - a)); | |
| !pixels[i+2]&&pixels[i+2]!=0||(pixels[i+2]=pixels[i+2]+(!pixels[i+2]||128)*color/b*a+pixels[i+2]*(1 - a)); | |
| !pixels[i+3]&&pixels[i+3]!=0||(pixels[i+3]=pixels[i+3]+(!pixels[i+2]||128)*color/a*a+pixels[i+3]*(1 - a)); | |
| } | |
| ctx.putImageData(imageData, 0, 0); | |
| return this; | |
| }; | |
| HTMLImageElement.prototype.addNoise = function(r, g, b, a, l){ | |
| var c=document.createElement('canvas'),e=this; | |
| c.width = e.width||100; | |
| c.height = e.height||100; | |
| var ctx=c.getContext("2d"); | |
| // ctx.font="30px Arial"; | |
| e.alt||ctx.fillText(e.alt,10,(e.height/2)-15); | |
| c.addNoise(r, g, b, a, l); | |
| e.src = c.toDataURL(); | |
| return e; | |
| }; | |
| (function(m){ | |
| for(i in m){ | |
| // is number or zero | |
| !((i*1||i==0)&&m[i])||m[i].addNoise(); | |
| } | |
| })(document.getElementsByTagName('img')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment