/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{CIMSymbolHelper as e}from"./CIMSymbolHelper.js";import{rasterizeFillStyle as r,rasterizeDash1D as t,rasterizeGradient as a}from"./rasterizingUtils.js";import n from"./Rect.js";import{getSDFInfo as s,buildSDF as i}from"./SDFHelper.js";import{normalizeDashTemplate as o,resampleHermite as c}from"./utils.js";import{rasterizedVectorMarkerMinSize as l,patternFillRasterizationScale as h}from"../../views/2d/engine/webgl/definitions.js";const f=512;class d{constructor(e){this._resourceManager=e,this._cachedRasterizationCanvas=null}dispose(){this._cachedRasterizationCanvas=null}get _canvas(){return this._cachedRasterizationCanvas||(this._cachedRasterizationCanvas=document.createElement("canvas")),this._cachedRasterizationCanvas}rasterizeJSONResource(e){switch(e.type){case"dash":{const r=o(e.dashTemplate),[a,n,s]=t(r);return{size:[n,s],image:new Uint32Array(a.buffer),sdf:!0,simplePattern:!0,anchorX:0,anchorY:0}}case"fill-style":{const[t,a,n,s]=r(this._canvas,e,h);return{size:[a,n],image:new Uint32Array(t.buffer),sdf:!1,simplePattern:!0,anchorX:0,anchorY:0,rasterizationScale:s}}case"sdf":return u(e);case"CIMGradientFill":case"CIMGradientStroke":case"CIMHatchFill":case"CIMPictureMarker":case"CIMVectorMarker":return this._rasterizeCIMJSONResource(e)}}_rasterizeCIMJSONResource(r){switch(r.type){case"CIMGradientFill":case"CIMGradientStroke":{const[e,t,n]=a(this._canvas,r);return{size:[t,n],image:new Uint32Array(e.buffer),sdf:!1,simplePattern:!1,anchorX:0,anchorY:0,samplingMode:"Discrete"===r.gradientType||"CIMFixedColorRamp"===r.colorRamp.type?"Nearest":"Linear"}}case"CIMHatchFill":{const t=e.fromCIMHatchFill(r,h);return this._rasterizeCIMVectorMarker(t)}case"CIMPictureMarker":{const t=e.fromCIMInsidePolygon(r);return this._rasterizeCIMVectorMarker(t)}case"CIMVectorMarker":{if("CIMMarkerPlacementInsidePolygon"===r.markerPlacement?.type){const t=e.fromCIMInsidePolygon(r);return this._rasterizeCIMVectorMarker(t)}const t=s(r);return t&&!r.avoidSDFRasterization?u(t):this._rasterizeCIMVectorMarker(r,!1,l)}}}_rasterizeCIMVectorMarker(r,t=!0,a){const s=t?n.fromExtent(r.frame):null,[i,o,c,l,h]=e.rasterize(this._canvas,r,s,this._resourceManager,!0,a);return i?{size:[o,c],image:new Uint32Array(i.buffer),sdf:!1,simplePattern:!1,anchorX:l,anchorY:h}:null}rasterizeImageResource(e,r,t,a){this._canvas.width=e,this._canvas.height=r;const n=this._canvas.getContext("2d",{willReadFrequently:!0});t instanceof ImageData?n.putImageData(t,0,0):(t.setAttribute("width",`${e}px`),t.setAttribute("height",`${r}px`),n.drawImage(t,0,0,e,r));const s=n.getImageData(0,0,e,r),i=new Uint8Array(s.data);if(a)for(const c of a)if(c&&c.oldColor&&4===c.oldColor.length&&c.newColor&&4===c.newColor.length){const[e,r,t,a]=c.oldColor,[n,s,o,l]=c.newColor;if(e===n&&r===s&&t===o&&a===l)continue;for(let c=0;c<i.length;c+=4)e===i[c]&&r===i[c+1]&&t===i[c+2]&&a===i[c+3]&&(i[c]=n,i[c+1]=s,i[c+2]=o,i[c+3]=l)}let o;for(let c=0;c<i.length;c+=4)o=i[c+3]/255,i[c]=i[c]*o,i[c+1]=i[c+1]*o,i[c+2]=i[c+2]*o;let l=i,h=e,d=r;const u=f;if(h>=u||d>=u){const t=h/d;t>1?(h=u,d=Math.round(u/t)):(d=u,h=Math.round(u*t)),l=new Uint8Array(4*h*d);const a=new Uint8ClampedArray(l.buffer);c(i,e,r,a,h,d,!1)}return{size:[h,d],image:new Uint32Array(l.buffer),sdf:!1,simplePattern:!1,anchorX:0,anchorY:0}}}function u(e){if(!e)return null;const{data:r,width:t,height:a,sdfPaddingRatio:n,sdfDecodeCoeff:s}=i(e);return r?{size:[t,a],image:new Uint32Array(r.buffer),sdf:!0,simplePattern:!0,sdfPaddingRatio:n,sdfDecodeCoeff:s,anchorX:0,anchorY:0}:null}export{d as default};
