-
Notifications
You must be signed in to change notification settings - Fork 0
/
glif.min.js
27 lines (27 loc) · 11.2 KB
/
glif.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*!
* WebGL image filter library
* https://github.com/slub/webgl-image-filter/
*
* Released under the MIT license (https://github.com/slub/webgl-image-filter/blob/master/LICENSE)
*
* Date: 2015-11-04
*/
(function(){var f=this;function g(a,c){var d=a.split("."),b=f;d[0]in b||!b.execScript||b.execScript("var "+d[0]);for(var e;d.length&&(e=d.shift());)d.length||void 0===c?b[e]?b=b[e]:b=b[e]={}:b[e]=c};var l={a:{},compile:function(a,c,d){c=a.createShader(c);a.shaderSource(c,d);a.compileShader(c);return a.getShaderParameter(c,a.COMPILE_STATUS)?c:(console.log(a.getShaderInfoLog(c)),null)},f:function(a,c){function d(a,b,c){a.replace(new RegExp("\\b"+b+" \\w+ (\\w+)","ig"),function(a,b){c[b]=0})}var b=a.createProgram(),e=l.compile(a,a.VERTEX_SHADER,l.VERTEX_IDENTITY),h=l.compile(a,a.FRAGMENT_SHADER,c);b.uniform={};b.attribute={};a.attachShader(b,e);a.attachShader(b,h);a.linkProgram(b);a.getProgramParameter(b,
a.LINK_STATUS)||console.log(a.getProgramInfoLog(b));d(l.VERTEX_IDENTITY,"attribute",b.attribute);for(var k in b.attribute)b.attribute[k]=a.getAttribLocation(b,k);d(l.VERTEX_IDENTITY,"uniform",b.uniform);d(c,"uniform",b.uniform);for(var m in b.uniform)b.uniform[m]=a.getUniformLocation(b,m);return b},BLUR:"precision highp float;\nvarying vec2 vUv;\nuniform sampler2D texture;\nuniform vec2 px;\nvoid main(void) {\ngl_FragColor = vec4(0.0);\ngl_FragColor += texture2D(texture, vUv + vec2(-7.0*px.x, -7.0*px.y))*0.0044299121055113265;\ngl_FragColor += texture2D(texture, vUv + vec2(-6.0*px.x, -6.0*px.y))*0.00895781211794;\ngl_FragColor += texture2D(texture, vUv + vec2(-5.0*px.x, -5.0*px.y))*0.0215963866053;\ngl_FragColor += texture2D(texture, vUv + vec2(-4.0*px.x, -4.0*px.y))*0.0443683338718;\ngl_FragColor += texture2D(texture, vUv + vec2(-3.0*px.x, -3.0*px.y))*0.0776744219933;\ngl_FragColor += texture2D(texture, vUv + vec2(-2.0*px.x, -2.0*px.y))*0.115876621105;\ngl_FragColor += texture2D(texture, vUv + vec2(-1.0*px.x, -1.0*px.y))*0.147308056121;\ngl_FragColor += texture2D(texture, vUv )*0.159576912161;\ngl_FragColor += texture2D(texture, vUv + vec2( 1.0*px.x, 1.0*px.y))*0.147308056121;\ngl_FragColor += texture2D(texture, vUv + vec2( 2.0*px.x, 2.0*px.y))*0.115876621105;\ngl_FragColor += texture2D(texture, vUv + vec2( 3.0*px.x, 3.0*px.y))*0.0776744219933;\ngl_FragColor += texture2D(texture, vUv + vec2( 4.0*px.x, 4.0*px.y))*0.0443683338718;\ngl_FragColor += texture2D(texture, vUv + vec2( 5.0*px.x, 5.0*px.y))*0.0215963866053;\ngl_FragColor += texture2D(texture, vUv + vec2( 6.0*px.x, 6.0*px.y))*0.00895781211794;\ngl_FragColor += texture2D(texture, vUv + vec2( 7.0*px.x, 7.0*px.y))*0.0044299121055113265;\n}",
COLOR_WITH_ALPHA:"precision highp float;\nvarying vec2 vUv;\nuniform sampler2D texture;\nuniform float m[20];\nvoid main(void) {\nvec4 c = texture2D(texture, vUv);\ngl_FragColor.r = m[0] * c.r + m[1] * c.g + m[2] * c.b + m[3] * c.a + m[4];\ngl_FragColor.g = m[5] * c.r + m[6] * c.g + m[7] * c.b + m[8] * c.a + m[9];\ngl_FragColor.b = m[10] * c.r + m[11] * c.g + m[12] * c.b + m[13] * c.a + m[14];\ngl_FragColor.a = m[15] * c.r + m[16] * c.g + m[17] * c.b + m[18] * c.a + m[19];\n}",COLOR_WITHOUT_ALPHA:"precision highp float;\nvarying vec2 vUv;\nuniform sampler2D texture;\nuniform float m[20];\nvoid main(void) {\nvec4 c = texture2D(texture, vUv);\ngl_FragColor.r = m[0] * c.r + m[1] * c.g + m[2] * c.b + m[4];\ngl_FragColor.g = m[5] * c.r + m[6] * c.g + m[7] * c.b + m[9];\ngl_FragColor.b = m[10] * c.r + m[11] * c.g + m[12] * c.b + m[14];\ngl_FragColor.a = c.a;\n}",
CONVOLUTION:"precision highp float;\nvarying vec2 vUv;\nuniform sampler2D texture;\nuniform vec2 px;\nuniform float m[9];\nvoid main(void) {\nvec4 c11 = texture2D(texture, vUv - px);\nvec4 c12 = texture2D(texture, vec2(vUv.x, vUv.y - px.y));\nvec4 c13 = texture2D(texture, vec2(vUv.x + px.x, vUv.y - px.y));\nvec4 c21 = texture2D(texture, vec2(vUv.x - px.x, vUv.y) );\nvec4 c22 = texture2D(texture, vUv);\nvec4 c23 = texture2D(texture, vec2(vUv.x + px.x, vUv.y) );\nvec4 c31 = texture2D(texture, vec2(vUv.x - px.x, vUv.y + px.y) );\nvec4 c32 = texture2D(texture, vec2(vUv.x, vUv.y + px.y) );\nvec4 c33 = texture2D(texture, vUv + px );\ngl_FragColor = \nc11 * m[0] + c12 * m[1] + c22 * m[2] +\nc21 * m[3] + c22 * m[4] + c23 * m[5] +\nc31 * m[6] + c32 * m[7] + c33 * m[8];\ngl_FragColor.a = c22.a;\nif (c22.r == 255.0 && c22.g == 255.0 && c22.b == 255.0){\ngl_FragColor.rgb = vec3(255,255,255);\n}\n}",
COPY:"precision highp float;\nvarying vec2 vUv;\nuniform sampler2D texture;\nvoid main(void) {\ngl_FragColor = texture2D(texture, vUv);\n}",INVERT:"precision highp float;\nvarying vec2 vUv;\nuniform sampler2D texture;\n\nvoid main(void) {\nvec4 c = texture2D(texture, vUv);\ngl_FragColor = vec4(1.0 - c.r, 1.0 - c.g, 1.0 - c.b, c.a);\n}",VERTEX_IDENTITY:"precision highp float;\nattribute vec2 pos;\nattribute vec2 uv;\nvarying vec2 vUv;\nuniform float flipY;\nvoid main(void) {\nvUv = uv;\ngl_Position = vec4(pos.x, pos.y*flipY, 0.0, 1.);\n}"};var r={blur:function(a,c,d,b){var e=void 0!==b?b:1;b=e/7/c.width;var e=e/7/c.height,h=n(a,"BLUR");p(a,h);a.uniform2f(h.uniform.px,0,e);q(a,c,h,!1,!0);a.uniform2f(h.uniform.px,b,0);q(a,c,h,d)},brightness:function(a,c,d,b){b=void 0!==b?b:1;r.colorMatrix.call(this,[b,0,0,0,0,0,b,0,0,0,0,0,b,0,0,0,0,0,1,0],a,c,d)},brownie:function(a,c,d){r.colorMatrix.call(this,[.5997023498159715,.34553243048391263,-.2708298674538042,0,47.43192855600873,-.037703249837783157,.8609577587992641,.15059552388459913,0,-36.96841498319127,
.24113635128153335,-.07441037908422492,.44972182064877153,0,-7.562075277591283,0,0,0,1,0],a,c,d)},colorMatrix:function(a,c,d,b){a=new Float32Array(a);a[4]/=255;a[9]/=255;a[14]/=255;a[19]/=255;var e=n(c,1==a[18]&&0==a[3]&&0==a[8]&&0==a[13]&&0==a[15]&&0==a[16]&&0==a[17]&&0==a[19]?"COLOR_WITHOUT_ALPHA":"COLOR_WITH_ALPHA");p(c,e);c.uniform1fv(e.uniform.m,a);q(c,d,e,b)},contrast:function(a,c,d,b){b=void 0!==b?b:1;var e=-128*(b-1);r.colorMatrix.call(this,[b,0,0,0,e,0,b,0,0,e,0,0,b,0,e,0,0,0,1,0],a,c,d)},
convolution:function(a,c,d,b){a=new Float32Array(a);var e=1/d.width,h=1/d.height,k=n(c,"CONVOLUTION");p(c,k);c.uniform1fv(k.uniform.m,a);c.uniform2f(k.uniform.px,e,h);q(c,d,k,b)},desaturateLuminance:function(a,c,d){r.colorMatrix.call(this,[.2764723,.929708,.0938197,0,-37.1,.2764723,.929708,.0938197,0,-37.1,.2764723,.929708,.0938197,0,-37.1,0,0,0,1,0],a,c,d)},detectEdges:function(a,c,d){r.convolution.call(this,[0,1,0,-3,1,0,1,0],a,c,d)},emboss:function(a,c,d,b){b=void 0!==b?b:1;r.convolution.call(this,
[-2*b,-1*b,0,-1*b,1,1*b,0,1*b,2*b],a,c,d)},hue:function(a,c,d,b){var e=(void 0!==b?b:0)/180*Math.PI;b=Math.cos(e);e=Math.sin(e);r.colorMatrix.call(this,[.213+.787*b+-.213*e,.715+-.715*b+-.715*e,.072+-.072*b+.928*e,0,0,.213+-.213*b+.143*e,.715+b*(1-.715)+.14*e,.072+-.072*b+-.283*e,0,0,.213+-.213*b+-.787*e,.715+-.715*b+.715*e,.072+.928*b+.072*e,0,0,0,0,0,1,0],a,c,d)},invert:function(a,c,d){var b=n(a,"INVERT");p(a,b);q(a,c,b,d)},kodachrome:function(a,c,d){r.colorMatrix.call(this,[1.1285582396593525,
-.3967382283601348,-.03992559172921793,0,63.72958762196502,-.16404339962244616,1.0835251566291304,-.05498805115633132,0,24.732407896706203,-.16786010706155763,-.5603416277695248,1.6014850761964943,0,35.62982807460946,0,0,0,1,0],a,c,d)},negative:function(a,c,d){r.contrast(a,c,d,-2)},polaroid:function(a,c,d){r.colorMatrix.call(this,[1.438,-.062,-.062,0,0,-.122,1.378,-.122,0,0,-.016,-.016,1.483,0,0,0,0,0,1,0],a,c,d)},saturation:function(a,c,d,b){b=2*(void 0!==b?b:0)/3+1;var e=-.5*(b-1);r.colorMatrix.call(this,
[b,e,e,0,0,e,b,e,0,0,e,e,b,0,0,0,0,0,1,0],a,c,d)},sepia:function(a,c,d){r.colorMatrix.call(this,[.393,.7689999,.18899999,0,0,.349,.6859999,.16799999,0,0,.272,.5339999,.13099999,0,0,0,0,0,1,0],a,c,d)},sharpen:function(a,c,d,b){b=void 0!==b?b:1;r.convolution.call(this,[0,-1*b,0,-1*b,1+4*b,-1*b,0,-1*b,0],a,c,d)},shiftToBGR:function(a,c,d){r.colorMatrix.call(this,[0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0],a,c,d)},sobelX:function(a,c,d){r.convolution.call(this,[-1,0,1,-2,0,2,-1,0,1],a,c,d)},sobelY:function(a,
c,d){r.convolution.call(this,[-1,-2,-1,0,0,0,1,2,1],a,c,d)},technicolor:function(a,c,d){r.colorMatrix.call(this,[1.9125277891456083,-.8545344976951645,-.09155508482755585,0,11.793603434377337,-.3087833385928097,1.7658908555458428,-.10601743074722245,0,-70.35205161461398,-.231103377548616,-.7501899197440212,1.847597816108189,0,30.950940869491138,0,0,0,1,0],a,c,d)},vintagePinhole:function(a,c,d){r.colorMatrix.call(this,[.6279345635605994,.3202183420819367,-.03965408211312453,0,9.651285835294123,.02578397704808868,
.6441188644374771,.03259127616149294,0,7.462829176470591,.0466055556782719,-.0851232987247891,.5241648018700465,0,5.159190588235296,0,0,0,1,0],a,c,d)}};var t=[],u=0,v=0,w=[null,null];g("glif.addFilter",function(a){t.push({g:r[a],c:Array.prototype.slice.call(arguments,1)})});
g("glif.apply",function(a,c,d){u=0;if(0!=t.length){w=[null,null];x(a);a.pixelStorei(a.UNPACK_PREMULTIPLY_ALPHA_WEBGL,!0);a.viewport(0,0,c.width,c.height);for(var b=0;b<t.length;b++){var e=t[b];e.g.apply(this,[a,c,b===t.length-1].concat(e.c||[]))}void 0!==d&&!0===d&&(d=y(a,c),b=z(a,c),e=n(a,"COPY"),x(a),a.pixelStorei(a.UNPACK_PREMULTIPLY_ALPHA_WEBGL,!0),a.viewport(0,0,c.width,c.height),p(a,e),a.bindTexture(a.TEXTURE_2D,d),a.bindFramebuffer(a.FRAMEBUFFER,b.b),a.uniform1f(e.uniform.flipY,-1),a.drawArrays(a.TRIANGLES,
0,6))}});function x(a){var c=new Float32Array([-1,-1,0,1,1,-1,1,1,-1,1,0,0,-1,1,0,0,1,-1,1,1,1,1,1,0]),d=a.createBuffer();a.bindBuffer(a.ARRAY_BUFFER,d);a.bufferData(a.ARRAY_BUFFER,c,a.STATIC_DRAW)}
function z(a,c){var d=a.createFramebuffer();a.bindFramebuffer(a.FRAMEBUFFER,d);a.bindRenderbuffer(a.RENDERBUFFER,a.createRenderbuffer());var b=a.createTexture();a.bindTexture(a.TEXTURE_2D,b);a.texImage2D(a.TEXTURE_2D,0,a.RGBA,c.width,c.height,0,a.RGBA,a.UNSIGNED_BYTE,null);a.texParameteri(a.TEXTURE_2D,a.TEXTURE_MAG_FILTER,a.LINEAR);a.texParameteri(a.TEXTURE_2D,a.TEXTURE_MIN_FILTER,a.LINEAR);a.texParameteri(a.TEXTURE_2D,a.TEXTURE_WRAP_S,a.CLAMP_TO_EDGE);a.texParameteri(a.TEXTURE_2D,a.TEXTURE_WRAP_T,
a.CLAMP_TO_EDGE);a.framebufferTexture2D(a.FRAMEBUFFER,a.COLOR_ATTACHMENT0,a.TEXTURE_2D,b,0);a.bindTexture(a.TEXTURE_2D,null);a.bindFramebuffer(a.FRAMEBUFFER,null);return{b:d,h:b}}function q(a,c,d,b,e){var h=null,k=null,m=null,h=0==u?y(a,c):A(a,c).h;u++;!b||e&1?(v=(v+1)%2,k=A(a,c).b):(k=null,m=0==u%2);a.bindTexture(a.TEXTURE_2D,h);a.bindFramebuffer(a.FRAMEBUFFER,k);a.uniform1f(d.uniform.flipY,m?-1:1);a.drawArrays(a.TRIANGLES,0,6)}function A(a,c){var d=v;w[d]=w[d]||z(a,c);return w[d]}
function n(a,c){var d=c.toUpperCase(),b=void 0!==l.a[d]?l.a[d]:l.f(a,l[d]);l.a.hasOwnProperty(d)||(l.a[d]=b);return b}
function y(a,c){var d=a.createTexture();a.bindTexture(a.TEXTURE_2D,d);a.texParameteri(a.TEXTURE_2D,a.TEXTURE_WRAP_S,a.CLAMP_TO_EDGE);a.texParameteri(a.TEXTURE_2D,a.TEXTURE_WRAP_T,a.CLAMP_TO_EDGE);a.texParameteri(a.TEXTURE_2D,a.TEXTURE_MIN_FILTER,a.NEAREST);a.texParameteri(a.TEXTURE_2D,a.TEXTURE_MAG_FILTER,a.NEAREST);a.texImage2D(a.TEXTURE_2D,0,a.RGBA,a.RGBA,a.UNSIGNED_BYTE,c);return d}g("glif.reset",function(){t=[];for(var a in l.a)delete l.a[a]});
function p(a,c){a.useProgram(c);var d=Float32Array.BYTES_PER_ELEMENT,b=4*d;a.enableVertexAttribArray(c.attribute.pos);a.vertexAttribPointer(c.attribute.pos,2,a.FLOAT,!1,b,0*d);a.enableVertexAttribArray(c.attribute.uv);a.vertexAttribPointer(c.attribute.uv,2,a.FLOAT,!1,b,2*d)};}).call(window);