/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../chunks/_rollupPluginBabelHelpers","../../request","../../geometry/Point","../../geometry/projection","../../layers/support/rasterFunctions/rasterProjectionHelper","../2d/engine/Bitmap","../2d/engine/webgl/VertexStream","../2d/engine/webgl/shaders/MaterialPrograms","../webgl/enums","../webgl/FramebufferObject","../webgl/rasterUtils","../webgl/RenderingContext","../webgl/Texture"],(function(e,t,r,i,a,n,o,s,p,c,h,m,x,d){"use strict";let u=function(){function e(e){if(e)this._ownsRctx=!1,this._rctx=e;else{this._ownsRctx=!0;const e=document.createElement("canvas").getContext("webgl");e.getExtension("OES_texture_float"),this._rctx=new x.RenderingContext(e,{})}const t={applyProjection:!0,bilinear:!1,bicubic:!1},r=p.createProgramTemplate("raster/reproject","raster/reproject",new Map([["a_position",0]]),t);this._program=this._rctx.programCache.acquire(r.shaders.vertexShader,r.shaders.fragmentShader,r.attributes),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),this._quad=new s(this._rctx,[0,0,1,0,0,1,1,1])}var u=e.prototype;return u.reprojectTexture=function(e,t,r=!1){const o=a.project(e.extent,t),s=new i({x:(e.extent.xmax-e.extent.xmin)/e.texture.descriptor.width,y:(e.extent.ymax-e.extent.ymin)/e.texture.descriptor.height,spatialReference:e.extent.spatialReference}),{x:p,y:x}=n.projectResolution(s,t,e.extent);let u=(p+x)/2;const g=Math.round((o.xmax-o.xmin)/u),l=Math.round((o.ymax-o.ymin)/u);u=(o.width/g+o.height/l)/2;const _=new i({x:u,y:u,spatialReference:o.spatialReference}),f=n.getProjectionOffsetGrid({projectedExtent:o,srcBufferExtent:e.extent,pixelSize:_,hasWrapAround:!0,spacing:[16,16]}),T=m.createTransformTexture(this._rctx,f),w=new d.Texture(this._rctx,{width:g,height:l,pixelFormat:c.PixelFormat.RGBA,dataType:c.PixelType.UNSIGNED_BYTE,wrapMode:c.TextureWrapMode.CLAMP_TO_EDGE,samplingMode:c.TextureSamplingMode.LINEAR,hasMipmap:!1}),b=new h.FramebufferObject(this._rctx,{colorTarget:c.TargetType.TEXTURE,depthStencilTarget:c.DepthStencilTargetType.NONE,width:g,height:l},w);if(this._rctx.bindFramebuffer(b),this._rctx.setViewport(0,0,g,l),this._rctx.useProgram(this._program),this._rctx.bindTexture(e.texture,0),this._rctx.bindTexture(T,1),this._quad.bind(),this._program.setUniform2f("u_srcImageSize",e.texture.descriptor.width,e.texture.descriptor.height),this._program.setUniform2fv("u_transformSpacing",f.spacing),this._program.setUniform2fv("u_transformGridSize",f.size),this._program.setUniform2f("u_targetImageSize",g,l),this._quad.draw(),this._quad.unbind(),this._rctx.useProgram(null),this._rctx.bindFramebuffer(null),T.dispose(),r){const e=new ImageData(b.descriptor.width,b.descriptor.height);return b.readPixels(0,0,b.descriptor.width,b.descriptor.height,c.PixelFormat.RGBA,c.PixelType.UNSIGNED_BYTE,e.data),b.detachColorTexture(c.ColorAttachment.COLOR_ATTACHMENT0),b.dispose(),{texture:w,extent:o,imageData:e}}return b.detachColorTexture(c.ColorAttachment.COLOR_ATTACHMENT0),b.dispose(),{texture:w,extent:o}},u.reprojectBitmapData=function(e,t){const r=o.isImageSource(e.bitmapData)?o.rasterize(e.bitmapData):e.bitmapData,i=new d.Texture(this._rctx,{width:e.bitmapData.width,height:e.bitmapData.height,pixelFormat:c.PixelFormat.RGBA,dataType:c.PixelType.UNSIGNED_BYTE,wrapMode:c.TextureWrapMode.CLAMP_TO_EDGE,samplingMode:c.TextureSamplingMode.LINEAR,hasMipmap:!1},r),a=this.reprojectTexture({texture:i,extent:e.extent},t,!0);a.texture.dispose();const n=document.createElement("canvas");n.width=a.imageData.width,n.height=a.imageData.height;return n.getContext("2d").putImageData(a.imageData,0,0),{bitmapData:n,extent:a.extent}},u.loadAndReprojectBitmapData=function(){var e=t._asyncToGenerator((function*(e,t,i){const a=(yield r(e,{responseType:"image"})).data,n=document.createElement("canvas");n.width=a.width,n.height=a.height;const o=n.getContext("2d");o.drawImage(a,0,0);const s=o.getImageData(0,0,n.width,n.height),p=this.reprojectBitmapData({bitmapData:s,extent:t},i);return{bitmapData:p.bitmapData,extent:p.extent}}));function i(t,r,i){return e.apply(this,arguments)}return i}(),u.destroy=function(){this._quad.dispose(),this._program.dispose(),this._ownsRctx&&this._rctx.dispose()},e}();e.ImageReprojector=u,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
