/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{disposeMaybe as t}from"../../../core/maybe.js";import{C as e}from"../../../chunks/RasterColorizer.glsl.js";import{createTransformTexture as r,getCommonUniforms as s,getColormapUniforms as i,getStretchUniforms as o,getShadedReliefUniforms as a,createRasterTexture as l,createColormapTexture as h}from"../../webgl/rasterUtils.js";const n={bandCount:3,minOutput:0,maxOutput:1,minCutOff:[0,0,0],maxCutOff:[255,255,255],factor:[1/255,1/255,1/255],useGamma:!1,gamma:[1,1,1],gammaCorrection:[1,1,1],colormap:null,colormapOffset:null,stretchType:"none",type:"stretch"};class u{constructor(t,e,r=null,s=null){this.lij=t,this.type="raster-tile",this._memoryUsed=null,this._source=null,this._symbolizerParameters=null,this._bandIds=null,this._interpolation=null,this._dirty=!1,this._transformGrid=null,this.isRendereredSource=!1,this.symbolizerRenderer=null,this.rawPixelData=null,this.opacity=1,this.source=e,this.width=r||e.width,this.height=s||e.height}get source(){return this._source}set source(e){this._source=e,this._rasterTexture=t(this._rasterTexture),this._memoryUsed=null}get symbolizerParameters(){return this.isRendereredSource?{...n,maxCutOff:[1,1,1],factor:[1,1,1]}:this._symbolizerParameters||n}set symbolizerParameters(t){this._symbolizerParameters=t}get bandIds(){return this._bandIds}set bandIds(t){if(null!=t&&t.length>0){this._bandIds&&t.every((t,e)=>!!this._bandIds?.[e]&&t===this._bandIds[e])||(this._bandIds=t,this._dirty=!0)}else this._bandIds=null}get interpolation(){return this._interpolation||"nearest"}set interpolation(t){if(this._interpolation=t,null!=this._rasterTexture){const e=this._getRasterTextureInterpolation(t);this._rasterTexture.setSamplingMode("bilinear"===e?9729:9728)}}get transformGrid(){return this._transformGrid}set transformGrid(e){this._transformGrid=e,this._transformGridTexture=t(this._transformGridTexture),this._memoryUsed=null}bind(t){return!!(this.source&&this.source.pixels&&this.source.pixels.length>0)&&((null==this._rasterTexture||this._dirty)&&this._updateRasterTexture(t,this.bandIds),null!=this._rasterTexture&&(this._updateColormapTexture(t),this.transformGrid&&null==this._transformGridTexture&&(this._transformGridTexture=r(t,this.transformGrid))),!0)}getUniforms(t){const{symbolizerParameters:r,transformGrid:l,width:h,height:n,opacity:u}=this,m=s(l,[h,n],[this.source.width,this.source.height],u),d=i(r.colormap,r.colormapOffset),_="stretch"===this.symbolizerParameters.type?o(this.symbolizerParameters):null,c="hillshade"===this.symbolizerParameters.type?a(this.symbolizerParameters):null,p=t.emptyTexture;return new e(m,d,_||c,this._rasterTexture??p,this._transformGridTexture??p,this._colormapTexture??p)}get isBilinearWithStretchColorRamp(){const{symbolizerParameters:t}=this;return"bilinear"===this.interpolation&&null!=t.colormap&&"stretch"===t.type}get memoryUsage(){if(null==this._memoryUsed){const t=[this._rasterTexture,this._transformGridTexture,this._colormapTexture];this._memoryUsed=t.map(t=>null!=t?t.descriptor.width*t.descriptor.height*4:0).reduce((t,e)=>t+e,0)}return this._memoryUsed}release(){return this._rasterTexture=t(this._rasterTexture),this._transformGridTexture=t(this._transformGridTexture),this._colormapTexture=t(this._colormapTexture),this.source=null,this.transformGrid=null,this.rawPixelData=null,!0}_updateRasterTexture(e,r){const s=this.source?this.source.extractBands(r):null;if(!(s?.pixels&&s.pixels.length>0))return void(this._rasterTexture=t(this._rasterTexture));const i=null==r&&null==this.bandIds||null!=r&&null!=this.bandIds&&r.join("")===this.bandIds.join("");if(null!=this._rasterTexture&&i)return;this._rasterTexture=t(this._rasterTexture);const o=this._getRasterTextureInterpolation(this.interpolation);this._rasterTexture=l(e,s,o,this.isRendereredSource||this.hasStretchTypeNone())}hasStretchTypeNone(){return"stretchType"in this.symbolizerParameters&&"none"===this.symbolizerParameters.stretchType&&!this.symbolizerParameters.useGamma&&"u8"===this.source.pixelType}_getRasterTextureInterpolation(t){return"lut"===this.symbolizerParameters.type||"nearest"===t||"majority"===t||this.isBilinearWithStretchColorRamp?"nearest":"bilinear"}_updateColormapTexture(e){const r=this._colormap,s=this.symbolizerParameters.colormap;return s?r?s.length!==r.length||s.some((t,e)=>t!==r[e])?(this._colormapTexture=t(this._colormapTexture),this._colormapTexture=h(e,s),void(this._colormap=s)):void 0:(this._colormapTexture=h(e,s),void(this._colormap=s)):(this._colormapTexture=t(this._colormapTexture),void(this._colormap=null))}}export{u as RasterTile};
