/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{__decorate as t}from"tslib";import{uniform as e,define as r,UniformGroup as a}from"../../../../GraphShaderModule.js";import{step as s,Float as o,mix as n,Vec4 as i,clamp as l,Vec2 as c,texture2D as u,sqrt as p,max as m,min as d,Int as w,block as b}from"../../../../graph/glsl.js";import{mirror as g}from"../surface.js";import{BaseRasterProcessorShader as h}from"./BaseRasterProcessorShader.js";import{invertValue as y}from"./math.js";class f extends a{}t([e(c)],f.prototype,"clampRange",void 0);class _ extends h{constructor(){super(...arguments),this.type="FocalStatisticsShader",this.rows=3,this.cols=3,this.fill=!1}_process(t){const e=this._process1(t),r=s(new o(1),e.a);if(!this.fill)return this._clamp(e.rgb,r);const a=this._getPixel(t),i=n(e.rgb,a.rgb,a.a);return this._clamp(i,r)}_clamp(t,e){const{clampRange:r}=this.focalStatisticsConfig;return new i(l(t,r.x,r.y),1).multiply(e)}_process1(t){const{texture:e,srcImageSize:r}=this.config,{rows:a,cols:s}=this,n=new c(Math.floor(a/2),Math.floor(s/2)),l=new o(1).divide(r),w=this._getPixel(t),{statisticsType:b}=this,h="min"===b||"max"===b?new i(w.rgb,0):new i(0,0,0,0);switch(b){case"min":return this._stat(a,s,h,(r,a,s)=>{const p=new c(new o(a),new o(s)).subtract(n).multiply(l),m=u(e,g(t.add(p))),w=d(r.rgb,m.rgb);return new i(w,r.a.add(m.a))});case"max":return this._stat(a,s,h,(r,a,s)=>{const p=new c(new o(a),new o(s)).subtract(n).multiply(l),d=u(e,g(t.add(p))),w=m(r.rgb,d.rgb);return new i(w,r.a.add(d.a))});case"mean":{const r=this._stat(a,s,h,(r,a,s)=>{const p=new c(new o(a),new o(s)).subtract(n).multiply(l),m=u(e,g(t.add(p))),d=r.rgb.add(m.rgb.multiply(m.a));return new i(d,r.a.add(m.a))}),p=r.rgb.multiply(y(r.a));return new i(p,r.a)}case"stddev":{const r=this._stat(a,s,h,(r,a,s)=>{const p=new c(new o(a),new o(s)).subtract(n).multiply(l),m=u(e,g(t.add(p))),d=r.rgb.add(m.rgb.multiply(m.a));return new i(d,r.a.add(m.a))}),m=this._stat(a,s,h,(r,a,s)=>{const p=new c(new o(a),new o(s)).subtract(n).multiply(l),m=u(e,g(t.add(p))),d=r.rgb.add(m.a.multiply(m.rgb).multiply(m.rgb));return new i(d,r.a.add(m.a))}),d=y(m.a),w=p(m.subtract(r.multiply(r).multiply(d)).multiply(d));return new i(w.rgb,r.a)}default:return w}}_stat(t=3,e=3,r,a){const s=new w(0).setMutable().setDebugName("StatColIterator"),o=new w(0).setMutable().setDebugName("StatRowIterator"),n=r.setMutable().setDebugName("StatAccumulator"),l=a(n,s,o).setDebugName("StatPredicate"),c=b({iterX:s,iterY:o,accumulator:n},i,l,({out:r,iterX:a,iterY:s,accumulator:o,subgraph:n})=>`\n  for (${s} = 0; ${s} < ${t}; ${s}++) {\n    for (${a} = 0; ${a} < ${e}; ${a}++) {\n  \n    ${n.body}\n  \n    ${o} = ${n.varName};\n    }\n  }\n  ${r} = ${o};\n  `).setDebugName("statBody");return c}}t([r],_.prototype,"rows",void 0),t([r],_.prototype,"cols",void 0),t([r],_.prototype,"statisticsType",void 0),t([r],_.prototype,"fill",void 0),t([e(f)],_.prototype,"focalStatisticsConfig",void 0);export{_ as FocalStatisticsShader};
