-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
1 lines (1 loc) · 3.63 KB
/
index.js
1
(function(){"use strict";function d(t){if(!(this instanceof d))return new d(t);if(!t)throw new TypeError("convolution matrix required");this.matrix=t,this.factor(1),this.bias(0)}d.prototype.width=function(t){return this.w=t,this},d.prototype.height=function(t){return this.h=t,this},d.prototype.factor=function(t){return this._factor=t,this},d.prototype.bias=function(t){return this._bias=t,this},d.prototype.apply=function(t,e){for(var r=t.data,a=e.data,i=this.w,n=this.h,h=this.matrix,f=h[0].length,o=h.length,s=Math.floor(o/2),p=this._factor,g=this._bias,v=0;v<n-1;v++)for(var l=0;l<i-1;l++){for(var u=(v*i+l)*4,c=0,b=0,y=0,m=0;m<f;++m)for(var w=0;w<o;++w){var _=((v+(m-s))*i+(l+(w-s)))*4;c+=r[_+0]*h[m][w],b+=r[_+1]*h[m][w],y+=r[_+2]*h[m][w]}a[u+0]=p*c+g,a[u+1]=p*b+g,a[u+2]=p*y+g,a[u+3]=r[u+3]}},d.prototype.canvas=function(t){var e=t.width,r=t.height,a=t.getContext("2d"),i=a.getImageData(0,0,e,r),n=a.createImageData(e,r);this.width(e),this.height(r),this.apply(i,n),a.putImageData(n,0,0)};function D(t,e){this.x=t,this.y=e}D.prototype.toString=function(){return"("+this.x+", "+this.y+")"};function F(t,e,r,a,i){this.x=t,this.y=e,this.w=r,this.h=a,this.intensity=i||0}F.prototype.midpoint=function(){return new D(this.x+this.w/2,this.y+this.h/2)},F.prototype.draw=function(t){var e=255*this.intensity|0,r=this.w/2*this.intensity;t.save(),t.beginPath(),t.fillStyle="rgba("+e+",0,0, .5)",t.arc(this.x+this.w/2,this.y+this.h/2,r,0,Math.PI*2,!1),t.fill(),t.restore()},F.prototype.drawFocus=function(t){var e=255*this.intensity|0,r=this.w/2*this.intensity;t.save(),t.beginPath(),t.fillStyle="rgba(0,0,"+e+", .5)",t.arc(this.x+this.w/2,this.y+this.h/2,r,0,Math.PI*2,!1),t.fill(),t.restore()};function P(t,e){e=e||{};var r=e.debug,a=t.width,i=t.height,n=t.getContext("2d"),h=n.getImageData(0,0,a,i),f=n.createImageData(a,i);d([[0,-1,0],[-1,4,-1],[0,-1,0]]).width(a).height(i).apply(h,f),r&&(t.width*=2,n.putImageData(f,0,0));var o=R(f.data,a,i);return r&&(L(o,n),n.translate(a,0),n.putImageData(h,a,0),L(o,n)),S(o).midpoint()}function L(t,e){for(var r=0;r<t.length;++r)t[r].draw(e);S(t).drawFocus(e)}function S(t){for(var e,r=t[0],a=1;a<t.length;++a)e=t[a],e.intensity>r.intensity&&(r=e);return r}function R(t,e,r){var a=[],i=20,n=e/2,h=r/2;function f(I,M){return I=Math.abs(I-n)/n,M=Math.abs(M-h)/h,I/=2,M/=2,1-(I+M)}for(var o=0;o<e;++o){for(var s=0;s<r;++s){for(var p=f(o+i/2,s+i/2)*.15,g=Math.min(e-o,i),v=Math.min(r-s,i),l=i*i,u=0,c=0;c<g;++c)for(var b=0;b<v;++b){var y=((s+b)*e+(o+c))*4,m=t[y+0],w=t[y+1],_=t[y+2],E=m+w+_,X=E/765;u+=X}p+=u/l*.85,a.push(new F(o,s,g,v,p*5)),s+=i}o+=i}return a}function C(t){const e=t.options.components["k-files-section"];t.component("k-files-section",{extends:e,data(){return{firstLoad:!0}},watch:{data:{immediate:!1,handler(r,a){if(this.isLoading)return!1;if(!a.length&&this.firstLoad)return this.firstLoad=!1,!1;let i=r.filter((n,h)=>!a.filter((o,s)=>o.filename==n.filename).length);if(!i.length)return!1;FileReader&&this.findFocus(i)}}},methods:{findFocus(r){r.forEach(a=>{let i=this;var n=new XMLHttpRequest;n.open("get",a.url),n.responseType="blob",n.onload=function(){var h=new FileReader;h.onload=function(){var f=document.createElement("canvas"),o=f.getContext("2d"),s=new Image;s.onload=function(){var p=s.width,g=s.height;f.width=p,f.height=g,o.drawImage(s,0,0);var v=P(f,{debug:!1}),l=v.x/p,u=v.y/g,l=(Math.round(l*100)/100).toFixed(2),u=(Math.round(u*100)/100).toFixed(2),c='{"x":'+l+',"y":'+u+"}";i.saveFocus(a,c)},s.src=h.result},h.readAsDataURL(n.response)},n.send()})},saveFocus(r,a){this.$api.post("autofocus/save-focus",{uri:r.parent,filename:r.filename,coords:a}).then(i=>{}).catch(i=>{console.log(i)})}}})}panel.plugin("sylvainjule/autofocus",{use:[C]})})();