-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscopes.min.js
8 lines (7 loc) · 10.2 KB
/
scopes.min.js
1
2
3
4
5
6
7
8
/*
Scopes version 0.2.0 BETA
(c) 2015-2018 epistemex.com
Dual license applies (GPL2 and commercial licenses).
See included readme for details.
*/
function Scope(r,t,j){j=j||{};var i=this,k=j.quality||2,f=typeof j.interpolate==="boolean"?j.interpolate:!1,p=j.scopeType||"histogram",g=j.lumaType||"709",m=typeof j.scopeAlpha==="boolean"?j.scopeAlpha:!1,d={lumR:null,lumG:null,lumB:null,sWidth:null,sHeight:null,tWidth:null,tHeight:null,show:(j.show||"RGB").toUpperCase(),rCtx:null,gCtx:null,bCtx:null,lCtx:null,intensity:j.intensity||16},v,u,s,q,x,e,l,o=["histogram","waveform","parade","vector"],n=[Scope._histogram,Scope._waveform,Scope._parade,Scope._vector],a=document.createElement("canvas"),b,c;this.update=function(){var h;b.drawImage(r,0,0,x,e);h=b.getImageData(0,0,x,e).data;if(p==="histogram"){c.clearRect(0,0,v,u)}return l(h,d)};this.scopeType=function(w){if(!arguments.length){return p}var h=o.indexOf(w);if(h<0){throw"Invalid scope type."}p=w;l=n[h];v=t.width;u=t.height;s=r.videoWidth||r.naturalWidth||r.width;q=r.videoHeight||r.naturalHeight||r.height;Scope._graticule=null;Scope._vector._tmp=null;switch(w){case"histogram":x=Math.ceil(s/k);e=Math.ceil(q/k);break;case"waveform":x=Math.floor(v/3)*3;e=256;c.strokeStyle="rgba(255, 255, 255, 0.5)";break;case"parade":x=Math.floor(v/3)-1;e=256;c.strokeStyle="rgba(255, 255, 255, 0.5)";break;case"vector":x=Math.ceil(s/k);e=Math.ceil(q/k);break}d.sWidth=a.width=x;d.sHeight=a.height=e;d.tWidth=v;d.tHeight=u;b=a.getContext("2d",{alpha:m});c=t.getContext("2d",{alpha:m});c.setTransform(1,0,0,1,0,0);c.globalAlpha=1;c.clearRect(0,0,v,u);b.mozImageSmoothingEnabled=b.msImageSmoothingEnabled=b.oImageSmoothingEnabled=b.imageSmoothingEnabled=f;c.mozImageSmoothingEnabled=c.msImageSmoothingEnabled=c.oImageSmoothingEnabled=c.imageSmoothingEnabled=!1;d.rCtx=d.gCtx=d.bCtx=d.lCtx=c};this.refresh=function(){i.scopeType(p)};this.intensity=function(h){if(!arguments.length){return(p==="histogram")?null:d.intensity}d.intensity=Math.max(0,Math.min(255,h))*2.55};this.show=function(h){if(!arguments.length){return d.show}d.show=h.toUpperCase()};this.quality=function(h){if(!arguments.length){return k}if([1,2,4,8].indexOf(h)<0){throw"Illegal quality value"}if(h!==k){k=h;i.scopeType(p)}};this.lumaType=function(z){if(!arguments.length){return g}var y,w,h;switch(z){case"2020":y=0.2627;w=0.6780000000000001;h=0.0593;break;case"601":y=0.299;w=0.587;h=0.114;break;case"linear":y=w=h=0.333;break;default:y=0.2126;w=0.7152;h=0.0722;break}g=z;d.lumR=y;d.lumG=w;d.lumB=h};this.interpolate=function(h){if(!arguments.length){return f}if(h!==f){f=h;i.scopeType(p)}};this.support=function(h){switch(h||p){case"histogram":return{R:!0,G:!0,B:!0,L:!0,RGB:!0,RGBL:!0,RG:!0,RB:!0,GB:!0,quality:!0};case"waveform":return{R:!0,G:!0,B:!0,L:!0,RGB:!0,RGBL:!1,RG:!1,RB:!1,GB:!1,quality:!1};case"parade":return{R:!1,G:!1,B:!1,L:!1,RGB:!0,RGBL:!1,RG:!1,RB:!1,GB:!1,quality:!1};case"vector":return{R:!1,G:!1,B:!1,L:!1,RGB:!0,RGBL:!1,RG:!1,RB:!1,GB:!1,quality:!0}}return null};this.scopeType(p);this.lumaType(g)}if(typeof exports!=="undefined"){exports.Scopes=Scopes};Scope._histogram=function(f,B){var D=B.rCtx,k=B.gCtx,e=B.bCtx,q=B.lCtx,I=B.show.toUpperCase(),n=B.tHeight,K=B.tWidth,C=new Uint32Array(256),j=new Uint32Array(256),c=new Uint32Array(256),p=new Uint32Array(256),G,m,d,t,z=K*0.0125,L=(n>>1)+0.5,x=B.lumR,v=B.lumG,u=B.lumB,A=1,a=Math.min(1,B.intensity/255+0.75),H,J=B.tWidth/256,s=f.byteLength,o;for(o=0;o<s;o++){G=f[o++];m=f[o++];d=f[o++];t=(G*x+m*v+d*u+0.5)|0;C[G]++;j[m]++;c[d]++;p[t]++}for(o=1;o<255;o++){A=Math.max(A,C[o],j[o],c[o],p[o])}H=B.tHeight/A;if(I.indexOf("R")>-1){F(D);D.fillStyle="rgba(255, 110, 110, "+a+")";E(D,C)}if(I.indexOf("G")>-1){F(k);k.fillStyle="rgba(110, 255, 110, "+a+")";E(k,j)}if(I.indexOf("B")>-1){F(e);e.fillStyle="rgba(110, 110, 255, "+a+")";E(e,c)}if(I.indexOf("L")>-1){F(q);q.fillStyle="rgba(170, 170, 170, "+a+")";q.globalAlpha=0.67;q.globalCompositeOperation="source-over";E(q,p)}q.beginPath();q.moveTo(((K*0.25)|0)+0.5,L-z);q.lineTo(((K*0.25)|0)+0.5,L+z);q.moveTo((K>>1)-z,L);q.lineTo((K>>1)+z,L);q.moveTo((K>>1)+0.5,L-z);q.lineTo((K>>1)+0.5,L+z);q.moveTo(((K*0.75)|0)+0.5,L-z);q.lineTo(((K*0.75)|0)+0.5,L+z);q.strokeStyle="rgba(170, 170, 170, 0.75)";q.globalCompositeOperation="source-over";q.stroke();function E(g,b){var h=0,l=0;g.beginPath();g.moveTo(0,0);while(h<256){g.lineTo(l,b[h++]*H);l+=J}g.lineTo(l,0);g.fill()}function F(b){b.setTransform(1,0,0,-1,0,B.tHeight);b.globalCompositeOperation="lighten";b.globalAlpha=1}return{r:C,g:j,b:c,luma:p,step:J,max:A,scale:H}};Scope._waveform=function(e,B){var E=B.rCtx,j=B.gCtx,d=B.bCtx,o=B.lCtx,z=B.lumR,v=B.lumG,u=B.lumB,I=B.show,J=B.sWidth,m=256,t=(I.indexOf("L")>-1),D=new Uint8ClampedArray(256*J),i=new Uint8ClampedArray(256*J),a=new Uint8ClampedArray(256*J),n=t?new Uint8ClampedArray(256*J):null,A=J*0.025,q=J*0.0125,H,k,c,s,K,L,C;for(L=0;L<m;L++){for(K=0;K<J;K++){C=(L*J+K)<<2;H=e[C++];k=e[C++];c=e[C++];D[H*J+K]++;i[k*J+K]++;a[c*J+K]++;if(t){s=(H*z+k*v+c*u+0.5)|0;n[s*J+K]++}}}if(I==="RGB"){F(o,D,i,a,n)}else{if(I==="R"){G(E,D,7829503)}else{if(I==="G"){G(j,i,7864183)}else{if(I==="B"){G(d,a,16742263)}else{if(t){G(o,n,16777215)}}}}}o.beginPath();f(o,((m*0.25)|0)+0.5,A*0.5);f(o,(m>>1)+0.5,A);f(o,((m*0.75)|0)+0.5,A*0.5);o.moveTo(0,m-1);o.lineTo(J,m-1);o.stroke();function f(b,h,g){b.moveTo(0,h);b.lineTo(g,h);b.moveTo((J>>1)-g,h);b.lineTo((J>>1)+g,h);b.moveTo(J-g,h);b.lineTo(J,h);b.moveTo((J>>1)+0.5,h-q);b.lineTo((J>>1)+0.5,h+q)}function F(w,U,N,h){var P=w.createImageData(J,m),M=new Uint32Array(P.data.buffer),Q=B.intensity,X,W,S,T;for(X=0;X<256;X++){S=X*J;T=((m-1)-X)*J;for(W=0;W<J;W++){var R=S+W,V=Math.min(255,U[R]*Q),O=Math.min(255,N[R]*Q),l=Math.min(255,h[R]*Q);M[T+W]=4278190080|(l<<16)|(O<<8)|V}}w.putImageData(P,0,0)}function G(l,b,h){var w=l.createImageData(J,m),r=new Uint32Array(w.data.buffer),M=B.intensity;for(var S=0,R,P,Q;S<256;S++){P=S*J;Q=(m-S)*J;for(R=0;R<J;R++){var N=P+R,O=Q+R,g=Math.min(255,b[N]*M);r[O]=(g<<24)|h}}l.putImageData(w,0,0)}};Scope._parade=function(e,k){var d=k.lCtx,q=k.sWidth,j=256,m=new Uint8ClampedArray(256*q),f=new Uint8ClampedArray(256*q),a=new Uint8ClampedArray(256*q),o,i,c,s,t,l;for(t=0;t<j;t++){for(s=0;s<q;s++){l=(t*q+s)<<2;o=e[l++];i=e[l++];c=e[l++];m[o*q+s]++;f[i*q+s]++;a[c*q+s]++}}n(d,m,f,a);function n(v,L,A,h){var N=(q+1)*3,C=v.createImageData(N,256),w=new Uint32Array(C.data.buffer),D=k.intensity,G=q,F=N*0.025,E=N*0.0125,O,P,J,K;for(P=0;P<256;P++){J=P*q;K=((j-1)-P)*N;for(O=0;O<q;O++){var H=J+O,I=K+O,M=Math.min(255,L[H]*D)<<24,B=Math.min(255,A[H]*D)<<24,u=Math.min(255,h[H]*D)<<24;w[I]=M|7829503;w[I+G+1]=B|7864183;w[I+(G<<1)+2]=u|16742263}}v.putImageData(C,0,0);v.beginPath();z(((j*0.25)|0)+0.5,F*0.5);z((j>>1)+0.5,F);z(((j*0.75)|0)+0.5,F*0.5);v.moveTo(0,j-1);v.lineTo(N,j-1);v.stroke();function z(b,g){v.moveTo(0,b);v.lineTo(g,b);v.moveTo(q-g,b);v.lineTo(q+g,b);v.moveTo(q*2+1-g,b);v.lineTo(q*2+1+g,b);v.moveTo(N-g,b);v.lineTo(N,b);v.moveTo(q+0.5,b-E);v.lineTo(q+0.5,b+E);v.moveTo(q*2+1.5,b-E);v.lineTo(q*2+1.5,b+E)}}};Scope._vector=function(e,l){var f=Math.min(l.tWidth,l.tHeight)>>1,o=f*0.5,q=(o*0.9)|0,d=l.rCtx,v=Scope._vector._tmp,u=new Uint8ClampedArray(f*f),k=e.byteLength,j,n,h,a,m;if(!v){Scope._vector._tmp=v=c(f)}for(j=0;j<k;j++){n=e[j++];h=e[j++];a=e[j++];m=t(n,h,a,o,o,q);u[m.y*f+m.x]++}s(v,d,u);function s(g,r,b){var x,y,w=g.createImageData(f,f),i=new Uint32Array(w.data.buffer);for(x=0;x<i.length;x++){y=Math.min(255,b[x]*l.intensity)|0;i[x]=4278190080|y*65793}g.putImageData(w,0,0);if(!Scope._graticule){Scope._createGraticule(o<<1)}r.globalCompositeOperation="source-over";r.clearRect(0,0,f<<1,f<<1);r.drawImage(g.canvas,0,0,f,f,0,0,f<<1,f<<1);r.drawImage(Scope._graticule,0,0)}function t(D,w,i,G,H,E){D/=255;w/=255;i/=255;var B=Math.max(D,w,i),C=Math.min(D,w,i),p=B-C,z,F,A=(B+C)*0.5;if(p){if(D===B){z=(w-i)/p}else{if(w===B){z=2+(i-D)/p}else{z=4+(D-w)/p}}F=(A<0.5?p/(2-B-C):p/(B+C));F*=E;z*=1.047197551196598;if(z<0){z+=Math.PI*2}}else{z=F=0}z+=0.2373647782712288;return{x:(G-F*Math.sin(z))|0,y:(H-F*Math.cos(z))|0}}function c(i){var b=document.createElement("canvas"),g=b.getContext("2d",{alpha:!1});b.width=b.height=i;return g}};Scope._vector._tmp=null;Scope._graticule=null;Scope._createGraticule=function(g){var j=g*2,b=document.createElement("canvas"),e=b.getContext("2d"),a=g*0.1,c="rgba(255,255,170,0.5)",h=["G","YL","R","MG","B","CY"],f;b.width=b.height=j;Scope._graticule=b;e.save();e.globalAlpha=1;e.setTransform(1,0,0,1,g+0.5,g+0.5);e.strokeStyle=c;e.arc(0,0,g*0.9,0,2*Math.PI);e.stroke();e.beginPath();e.arc(0,0,g*0.3,0,2*Math.PI);e.globalAlpha=0.25;e.stroke();e.beginPath();e.arc(0,0,g*0.54,0,2*Math.PI);e.globalAlpha=0.5;e.strokeStyle="#f00";e.stroke();e.strokeStyle=c;e.globalAlpha=1;d(e,0.75);e.rotate(-(90-57)/180*Math.PI);d(e,0.33);e.rotate((90-57)/180*Math.PI);e.beginPath();e.rotate(-0.2373647782712288);e.save();e.lineWidth=3;e.rotate(-0.5*Math.PI);for(f=359;f>=0;f--){e.beginPath();e.arc(0,0,g-2,0,0.0175);e.rotate(0.017453);e.strokeStyle="hsl("+f+", 90%, 33%)";e.stroke()}e.restore();e.fillStyle="rgba(255, 255, 255, 0.7)";e.beginPath();for(f=0;f<360;f+=5){e.rotate(5/180*Math.PI);e.moveTo(((f-1)%6===0)?g-14:g-9,0);e.lineTo(g-4,0)}e.stroke();e.font=((g*0.075)|0)+"px sans-serif";e.textBaseline="middle";e.textAlign="center";e.beginPath();for(f=0;f<6;f++){e.clearRect(-a,g*0.85,a*2,a);e.clearRect(-a*0.75,g*0.49,a*1.5,a);e.rect(-a*0.5,g*0.85,a,a);e.rect(-a*0.5,g*0.49,a,a);e.rotate(1.047197551196598);e.save();e.translate(0,g*0.75);e.rotate(-1.047197551196598*f-0.8098327729253688);e.fillText(h[f],0,0);e.restore()}e.strokeStyle=c;e.stroke();e.rotate(-0.28);e.fillText("Q",g*0.83,0);e.rotate(0.35);e.globalAlpha=0.5;e.font=((g*0.05)|0)+"px sans-serif";e.fillText("50%",g*0.38,0);e.fillText("75%",g*0.61,0);e.fillText("100%",g*0.8,0);e.save();e.setTransform(1,0,0,1,g+0.5,g+0.5);e.globalCompositeOperation="destination-over";e.rotate(-Math.PI-0.2373647782712288);for(f=5;f>=0;f--){e.rotate(1/3*Math.PI);e.beginPath();e.moveTo(0,0);e.lineTo(g*0.9,0);e.arc(0,0,g*0.9,0,1/3*Math.PI);e.fillStyle="hsla("+((f+1)*60)+",100%, 80%, 0.1)";e.fill()}e.globalCompositeOperation="source-over";e.beginPath();for(f=0;f<6;f++){e.rotate(1/3*Math.PI);e.moveTo(0,0);e.lineTo(g*0.55,0)}e.strokeStyle="rgba(70, 70, 70, 0.33)";e.stroke();e.restore();e.restore();function d(i,k){i.beginPath();i.moveTo(0,g*k);i.lineTo(0,g*0.95);i.moveTo(0,-g*k);i.lineTo(0,-g*0.95);i.moveTo(g*k,0);i.lineTo(g*0.95,0);i.moveTo(-g*k,0);i.lineTo(-g*0.95,0);i.stroke()}};