// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See http://js.arcgis.com/3.23/esri/copyright.txt for details.
//>>built
define("esri/layers/rasterFormats/LercCodec",[],function(){var M={defaultNoDataValue:-3.4027999387901484E38,decode:function(d,p){var y;p=p||{};var e=p.inputOffset||0,h=p.encodedMaskData||null===p.encodedMaskData,a={},b=new Uint8Array(d,e,10);a.fileIdentifierString=String.fromCharCode.apply(null,b);if("CntZImage"!=a.fileIdentifierString.trim())throw"Unexpected file identifier string: "+a.fileIdentifierString;e+=10;b=new DataView(d,e,24);a.fileVersion=b.getInt32(0,!0);a.imageType=b.getInt32(4,!0);a.height=
b.getUint32(8,!0);a.width=b.getUint32(12,!0);a.maxZError=b.getFloat64(16,!0);e+=24;if(!h)if(b=new DataView(d,e,16),a.mask={},a.mask.numBlocksY=b.getUint32(0,!0),a.mask.numBlocksX=b.getUint32(4,!0),a.mask.numBytes=b.getUint32(8,!0),a.mask.maxValue=b.getFloat32(12,!0),e+=16,0<a.mask.numBytes){var h=new Uint8Array(Math.ceil(a.width*a.height/8)),b=new DataView(d,e,a.mask.numBytes),k=b.getInt16(0,!0),z=2,v=0;do{if(0<k)for(;k--;)h[v++]=b.getUint8(z++);else for(var A=b.getUint8(z++),k=-k;k--;)h[v++]=A;k=
b.getInt16(z,!0);z+=2}while(z<a.mask.numBytes);if(-32768!==k||v<h.length)throw"Unexpected end of mask RLE encoding";a.mask.bitset=h;e+=a.mask.numBytes}else 0===(a.mask.numBytes|a.mask.numBlocksY|a.mask.maxValue)&&(h=new Uint8Array(Math.ceil(a.width*a.height/8)),a.mask.bitset=h);b=new DataView(d,e,16);a.pixels={};a.pixels.numBlocksY=b.getUint32(0,!0);a.pixels.numBlocksX=b.getUint32(4,!0);a.pixels.numBytes=b.getUint32(8,!0);a.pixels.maxValue=b.getFloat32(12,!0);e+=16;h=a.pixels.numBlocksX;b=a.pixels.numBlocksY;
h+=0<a.width%h?1:0;k=b+(0<a.height%b?1:0);a.pixels.blocks=Array(h*k);for(v=z=0;v<k;v++)for(A=0;A<h;A++){var g=0,b=new DataView(d,e,Math.min(10,d.byteLength-e)),f={};a.pixels.blocks[z++]=f;var c=b.getUint8(0);g++;f.encoding=c&63;if(3<f.encoding)throw"Invalid block encoding ("+f.encoding+")";if(2===f.encoding)e++;else{if(0!==c&&2!==c){c>>=6;f.offsetType=c;if(2===c)f.offset=b.getInt8(1),g++;else if(1===c)f.offset=b.getInt16(1,!0),g+=2;else if(0===c)f.offset=b.getFloat32(1,!0),g+=4;else throw"Invalid block offset type";
if(1===f.encoding)if(c=b.getUint8(g),g++,f.bitsPerPixel=c&63,c>>=6,f.numValidPixelsType=c,2===c)f.numValidPixels=b.getUint8(g),g++;else if(1===c)f.numValidPixels=b.getUint16(g,!0),g+=2;else if(0===c)f.numValidPixels=b.getUint32(g,!0),g+=4;else throw"Invalid valid pixel count type";}e+=g;if(3!=f.encoding)if(0===f.encoding){c=(a.pixels.numBytes-1)/4;if(c!==Math.floor(c))throw"uncompressed block has invalid length";b=new ArrayBuffer(4*c);g=new Uint8Array(b);g.set(new Uint8Array(d,e,4*c));b=new Float32Array(b);
f.rawData=b;e+=4*c}else 1===f.encoding&&(c=Math.ceil(f.numValidPixels*f.bitsPerPixel/8),b=new ArrayBuffer(4*Math.ceil(c/4)),g=new Uint8Array(b),g.set(new Uint8Array(d,e,c)),f.stuffedData=new Uint32Array(b),e+=c)}}a.eofOffset=e;var e=null!=p.noDataValue?p.noDataValue:M.defaultNoDataValue,k=p.encodedMaskData,F=p.returnMask,z=0,v=a.pixels.numBlocksX,A=a.pixels.numBlocksY,f=Math.floor(a.width/v),g=Math.floor(a.height/A),c=2*a.maxZError,b=Number.MAX_VALUE,w,k=k||(a.mask?a.mask.bitset:null),r,h=new (p.pixelType||
Float32Array)(a.width*a.height);F&&k&&(r=new Uint8Array(a.width*a.height));for(var F=new Float32Array(f*g),t,u,G=0;G<=A;G++){var H=G!==A?g:a.height%A;if(0!==H)for(var I=0;I<=v;I++){var C=I!==v?f:a.width%v;if(0!==C){var n=G*a.width*g+I*f,K=a.width-C,q=a.pixels.blocks[z],l,m;if(2>q.encoding){if(0===q.encoding)l=q.rawData;else{l=q.stuffedData;m=q.bitsPerPixel;w=q.numValidPixels;t=q.offset;u=c;var P=F,N=a.pixels.maxValue,L=(1<<m)-1,O=0,J=void 0,x=0,D=void 0,E=void 0,Q=Math.ceil((N-t)/u);l[l.length-1]<<=
8*(4*l.length-Math.ceil(m*w/8));for(J=0;J<w;J++)0===x&&(E=l[O++],x=32),x>=m?(D=E>>>x-m&L,x-=m):(x=m-x,D=(E&L)<<x&L,E=l[O++],x=32-x,D+=E>>>x),P[J]=D<Q?t+D*u:N;l=F}m=0}else y=2===q.encoding?0:q.offset;var B;if(k)for(u=0;u<H;u++){n&7&&(B=k[n>>3],B<<=n&7);for(t=0;t<C;t++)n&7||(B=k[n>>3]),B&128?(r&&(r[n]=1),w=2>q.encoding?l[m++]:y,b=b>w?w:b,h[n++]=w):(r&&(r[n]=0),h[n++]=e),B<<=1;n+=K}else if(2>q.encoding)for(u=0;u<H;u++){for(t=0;t<C;t++)w=l[m++],b=b>w?w:b,h[n++]=w;n+=K}else for(b=b>y?y:b,u=0;u<H;u++){for(t=
0;t<C;t++)h[n++]=y;n+=K}if(1===q.encoding&&m!==q.numValidPixels)throw"Block and Mask do not match";z++}}}y=r;r={width:a.width,height:a.height,pixelData:h,minValue:b,maxValue:a.pixels.maxValue,noDataValue:e};y&&(r.maskData=y);p.returnEncodedMask&&a.mask&&(r.encodedMaskData=a.mask.bitset?a.mask.bitset:null);if(p.returnFileInfo&&(r.fileInfo=R(a),p.computeUsedBitDepths)){y=r.fileInfo;B=a.pixels.numBlocksX*a.pixels.numBlocksY;l={};for(m=0;m<B;m++)e=a.pixels.blocks[m],0===e.encoding?l.float32=!0:1===e.encoding?
l[e.bitsPerPixel]=!0:l[0]=!0;a=Object.keys(l);y.bitDepths=a}return r}},R=function(d){return{fileIdentifierString:d.fileIdentifierString,fileVersion:d.fileVersion,imageType:d.imageType,height:d.height,width:d.width,maxZError:d.maxZError,eofOffset:d.eofOffset,mask:d.mask?{numBlocksX:d.mask.numBlocksX,numBlocksY:d.mask.numBlocksY,numBytes:d.mask.numBytes,maxValue:d.mask.maxValue}:null,pixels:{numBlocksX:d.pixels.numBlocksX,numBlocksY:d.pixels.numBlocksY,numBytes:d.pixels.numBytes,maxValue:d.pixels.maxValue,
noDataValue:this.noDataValue}}};return M});