/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["./CIMSymbolHelper","./Rect","./SDFHelper","./utils"],(function(e,t,r,a){"use strict";const n=512;return function(){function i(e){this._resourceManager=e}var s=i.prototype;return s.dispose=function(){this._rasterizationCanvas=null},s.rasterizeJSONResource=function(a,n,i){if(this._rasterizationCanvas||(this._rasterizationCanvas=document.createElement("canvas")),"simple-fill"===a.type||"esriSFS"===a.type){const[t,r,i]=e.SymbolHelper.rasterizeSimpleFill(this._rasterizationCanvas,a.style,n);return{size:[r,i],image:new Uint32Array(t.buffer),sdf:!1,simplePattern:!0,anchorX:0,anchorY:0}}if("simple-line"===a.type||"esriSLS"===a.type||"line"===a.type&&a.dashTemplate){let t,r;if("simple-line"===a.type||"esriSLS"===a.type)switch(t=e.slsDashToTemplateArray(a.style,a.cap),a.cap){case"butt":r="Butt";break;case"square":r="Square";break;default:r="Round"}else t=a.dashTemplate,r=a.cim.capStyle;const[n,i,s]=e.SymbolHelper.rasterizeSimpleLine(t,r);return{size:[i,s],image:new Uint32Array(n.buffer),sdf:!0,simplePattern:!0,anchorX:0,anchorY:0}}let s,o,l;if("simple-marker"===a.type||"esriSMS"===a.type||"line-marker"===a.type?(s=e.CIMSymbolHelper.fromSimpleMarker(a),l=r.getSDFInfo(s)):a.cim&&"CIMHatchFill"===a.cim.type?(s=e.CIMSymbolHelper.fromCIMHatchFill(a.cim),o=new t(s.frame.xmin,-s.frame.ymax,s.frame.xmax-s.frame.xmin,s.frame.ymax-s.frame.ymin)):a.cim.markerPlacement&&"CIMMarkerPlacementInsidePolygon"===a.cim.markerPlacement.type?(s=e.CIMSymbolHelper.fromCIMInsidePolygon(a.cim),o=new t(s.frame.xmin,-s.frame.ymax,s.frame.xmax-s.frame.xmin,s.frame.ymax-s.frame.ymin)):(s=a.cim,l=r.getSDFInfo(s)),l&&!i){const[e,t,a]=r.buildSDF(l);return e?{size:[t,a],image:new Uint32Array(e.buffer),sdf:!0,simplePattern:!0,anchorX:0,anchorY:0}:null}const[m,c,f,p,u]=e.CIMSymbolHelper.rasterize(this._rasterizationCanvas,s,o,this._resourceManager,!i);return m?{size:[c,f],image:new Uint32Array(m.buffer),sdf:!1,simplePattern:!1,anchorX:p,anchorY:u}:null},s.rasterizeImageResource=function(e,t,r,i){this._rasterizationCanvas||(this._rasterizationCanvas=document.createElement("canvas")),this._rasterizationCanvas.width=e,this._rasterizationCanvas.height=t;const s=this._rasterizationCanvas.getContext("2d");r instanceof ImageData?s.putImageData(r,0,0):(r.setAttribute("width",`${e}px`),r.setAttribute("height",`${t}px`),s.drawImage(r,0,0,e,t));const o=s.getImageData(0,0,e,t),l=new Uint8Array(o.data);if(i)for(const a of i)if(a&&a.oldColor&&4===a.oldColor.length&&a.newColor&&4===a.newColor.length){const[e,t,r,n]=a.oldColor,[i,s,o,m]=a.newColor;if(e===i&&t===s&&r===o&&n===m)continue;for(let a=0;a<l.length;a+=4)e===l[a]&&t===l[a+1]&&r===l[a+2]&&n===l[a+3]&&(l[a]=i,l[a+1]=s,l[a+2]=o,l[a+3]=m)}let m;for(let a=0;a<l.length;a+=4)m=l[a+3]/255,l[a]=l[a]*m,l[a+1]=l[a+1]*m,l[a+2]=l[a+2]*m;let c=l,f=e,p=t;const u=n;if(f>=u||p>=u){const r=f/p;r>1?(f=u,p=Math.round(u/r)):(p=u,f=Math.round(u*r)),c=new Uint8Array(4*f*p);const n=new Uint8ClampedArray(c.buffer);a.resampleHermite(l,e,t,n,f,p,!1)}return{size:[f,p],image:new Uint32Array(c.buffer),sdf:!1,simplePattern:!1,anchorX:0,anchorY:0}},i}()}));
