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