/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import t from"../../request.js";import e from"../../geometry/Point.js";import{execute as r}from"../../geometry/operators/projectOperator.js";import{projectResolution as i,getProjectionOffsetGrid as a,load as s}from"../../layers/raster/functions/rasterProjectionHelper.js";import{isImageSource as n,rasterize as o}from"../2d/engine/Bitmap.js";import m from"../2d/engine/webgl/VertexStream.js";import{createProgramTemplate as c}from"../2d/engine/webgl/shaders/MaterialPrograms.js";import{PixelType as p}from"../webgl/enums.js";import{FramebufferObject as h}from"../webgl/FramebufferObject.js";import{createTransformTexture as x}from"../webgl/rasterUtils.js";import{RenderingContext as d}from"../webgl/RenderingContext.js";import{RenderingContextOptions as u}from"../webgl/RenderingContextOptions.js";import g from"../webgl/Texture.js";import{TextureDescriptor as _}from"../webgl/TextureDescriptor.js";class f{static{this._instanceRefCount=0}constructor(t){if(this._ownsRctx=!1,t)this._ownsRctx=!1,this._rctx=t;else{if(f._instance)return f._instanceRefCount++,f._instance;f._instanceRefCount=1,f._instance=this,this._ownsRctx=!0;const t=document.createElement("canvas").getContext("webgl2");t.getExtension("OES_texture_float"),this._rctx=new d(t,new u)}this._quad=new m(this._rctx,[0,0,1,0,0,1,1,1]);const e=c("raster/reproject","raster/reproject",{applyProjection:!0,bilinear:!1,bicubic:!1});this._program=this._rctx.programCache.acquire(e.vertexShader,e.fragmentShader,this._quad.locations),this._rctx.useProgram(this._program),this._program.setUniform1f("u_opacity",1),this._program.setUniform1i("u_image",0),this._program.setUniform1i("u_flipY",0),this._program.setUniform1i("u_transformGrid",1)}reprojectTexture(t,s,n=!1){const o=r(t.extent,s),m=new e({x:(t.extent.xmax-t.extent.xmin)/t.texture.descriptor.width,y:(t.extent.ymax-t.extent.ymin)/t.texture.descriptor.height,spatialReference:t.extent.spatialReference}),{x:c,y:d}=i(m,s,t.extent);let u=(c+d)/2;const g=Math.round((o.xmax-o.xmin)/u),f=Math.round((o.ymax-o.ymin)/u);u=(o.width/g+o.height/f)/2;const l=new e({x:u,y:u,spatialReference:o.spatialReference}),w=a({projectedExtent:o,srcBufferExtent:t.extent,pixelSize:l,hasWrapAround:!0,spacing:[16,16]}),b=x(this._rctx,w),j=new _(g,f);j.wrapMode=33071;const D=new h(this._rctx,j);this._rctx.bindFramebuffer(D),this._rctx.setViewport(0,0,g,f),this._rctx.useProgram(this._program),this._rctx.bindTexture(t.texture,0),this._rctx.bindTexture(b,1),this._quad.bind();const{width:R=0,height:y=0}=t.texture.descriptor;if(this._program.setUniform2f("u_srcImageSize",R,y),this._program.setUniform2fv("u_transformSpacing",w.spacing),this._program.setUniform2fv("u_transformGridSize",w.size),this._program.setUniform2f("u_targetImageSize",g,f),this._quad.draw(),this._quad.unbind(),this._rctx.useProgram(null),this._rctx.bindFramebuffer(null),b.dispose(),n){const{width:t,height:e}=D,r=new ImageData(t??0,e??0);D.readPixels(0,0,t??0,e??0,6408,p.UNSIGNED_BYTE,r.data);const i=D.detachColorTexture();return D.dispose(),{texture:i,extent:o,imageData:r}}const C=D.detachColorTexture();return D.dispose(),{texture:C,extent:o}}reprojectBitmapData(t,e){const r=n(t.bitmapData)?o(t.bitmapData):t.bitmapData,i=new _(t.bitmapData.width,t.bitmapData.height);i.wrapMode=33071;const a=new g(this._rctx,i,r),s=this.reprojectTexture({texture:a,extent:t.extent},e,!0);s.texture.dispose();const m=document.createElement("canvas"),c=s.imageData;m.width=c.width,m.height=c.height;return m.getContext("2d").putImageData(c,0,0),{bitmapData:m,extent:s.extent}}async loadAndReprojectBitmapData(e,r,i){const[a]=await Promise.all([t(e,{responseType:"image"}).then(t=>t.data),s()]),n=document.createElement("canvas");n.width=a.width,n.height=a.height;const o=n.getContext("2d");o.drawImage(a,0,0);const m=o.getImageData(0,0,n.width,n.height);if(r.spatialReference.equals(i))return{bitmapData:m,extent:r};const c=this.reprojectBitmapData({bitmapData:m,extent:r},i);return{bitmapData:c.bitmapData,extent:c.extent}}destroy(){this._ownsRctx?(f._instanceRefCount--,0===f._instanceRefCount&&(this._quad.dispose(),this._program.dispose(),this._rctx.dispose(),f._instance=null)):(this._quad.dispose(),this._program.dispose())}}export{f as ImageReprojector};
