@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 13.6 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */
import t from"../../../core/Error.js";import{assertIsSome as e}from"../../../core/maybe.js";import a from"./ImageCanvasDecoder.js";import i from"./JpgPlus.js";import{load as s,decode as r}from"./Lerc.js";import{decode as n}from"./Lzw.js";import{convertNoDataToMask as o}from"./pixelRangeUtils.js";import{Z as h}from"../../../chunks/Zlib.js";import{load as c,decode as l}from"./Qb3.js";import p from"./Raw.js";import{decodeTileOrStrip as d,decode as u}from"./TiffDecoder.js";import{isPlatformLittleEndian as f}from"./utils.js";import m from"../../support/PixelBlock.js";import w from"../../support/SimpleBandStatistics.js";var g=function(t){var e,a,i,s,r,n;function o(t){var e,a,i,s,r,n,o,h,c,l,p,d,u;for(this.data=t,this.pos=8,this.palette=[],this.imgData=[],this.transparency={},this.animation=null,this.text={},r=null;;){switch(e=this.readUInt32(),h=function(){var t,e;for(e=[],t=0;t<4;++t)e.push(String.fromCharCode(this.data[this.pos++]));return e}.call(this).join(""),h){case"IHDR":this.width=this.readUInt32(),this.height=this.readUInt32(),this.bits=this.data[this.pos++],this.colorType=this.data[this.pos++],this.compressionMethod=this.data[this.pos++],this.filterMethod=this.data[this.pos++],this.interlaceMethod=this.data[this.pos++];break;case"acTL":this.animation={numFrames:this.readUInt32(),numPlays:this.readUInt32()||1/0,frames:[]};break;case"PLTE":this.palette=this.read(e);break;case"fcTL":r&&this.animation.frames.push(r),this.pos+=4,r={width:this.readUInt32(),height:this.readUInt32(),xOffset:this.readUInt32(),yOffset:this.readUInt32()},s=this.readUInt16(),i=this.readUInt16()||100,r.delay=1e3*s/i,r.disposeOp=this.data[this.pos++],r.blendOp=this.data[this.pos++],r.data=[];break;case"IDAT":case"fdAT":for("fdAT"===h&&(this.pos+=4,e-=4),t=(null!=r?r.data:void 0)||this.imgData,p=0;0<=e?p<e:p>e;0<=e?++p:--p)t.push(this.data[this.pos++]);break;case"tRNS":switch(this.transparency={},this.colorType){case 3:if(this.transparency.indexed=this.read(e),(c=255-this.transparency.indexed.length)>0)for(d=0;0<=c?d<c:d>c;0<=c?++d:--d)this.transparency.indexed.push(255);break;case 0:this.transparency.grayscale=this.read(e)[0];break;case 2:this.transparency.rgb=this.read(e)}break;case"tEXt":n=(l=this.read(e)).indexOf(0),o=String.fromCharCode.apply(String,l.slice(0,n)),this.text[o]=String.fromCharCode.apply(String,l.slice(n+1));break;case"IEND":return r&&this.animation.frames.push(r),this.colors=function(){switch(this.colorType){case 0:case 3:case 4:return 1;case 2:case 6:return 3}}.call(this),this.hasAlphaChannel=4===(u=this.colorType)||6===u,a=this.colors+(this.hasAlphaChannel?1:0),this.pixelBitlength=this.bits*a,this.colorSpace=function(){switch(this.colors){case 1:return"DeviceGray";case 3:return"DeviceRGB"}}.call(this),void(this.imgData=new Uint8Array(this.imgData));default:this.pos+=e}if(this.pos+=4,this.pos>this.data.length)throw new Error("Incomplete or corrupt PNG file")}}return o.load=function(t,e,a){var i;return"function"==typeof e&&(a=e),(i=new XMLHttpRequest).open("GET",t,!0),i.responseType="arraybuffer",i.onload=function(){var t;return t=new o(new Uint8Array(i.response||i.mozResponseArrayBuffer)),"function"==typeof(null!=e?e.getContext:void 0)&&t.render(e),"function"==typeof a?a(t):void 0},i.send(null)},a=1,i=2,e=0,o.prototype.read=function(t){var e,a;for(a=[],e=0;0<=t?e<t:e>t;0<=t?++e:--e)a.push(this.data[this.pos++]);return a},o.prototype.readUInt32=function(){return this.data[this.pos++]<<24|this.data[this.pos++]<<16|this.data[this.pos++]<<8|this.data[this.pos++]},o.prototype.readUInt16=function(){return this.data[this.pos++]<<8|this.data[this.pos++]},o.prototype.decodePixels=function(t){var e,a,i,s,r,n,o,c,l,p,d,u,f,m,w,g,y,x,b,k,T,C,I;if(null==t&&(t=this.imgData),0===t.length)return new Uint8Array(0);for(t=(t=new h(t)).getBytes(),g=(u=this.pixelBitlength/8)*this.width,f=new Uint8Array(g*this.height),n=t.length,w=0,m=0,a=0;m<n;){switch(t[m++]){case 0:for(s=b=0;b<g;s=b+=1)f[a++]=t[m++];break;case 1:for(s=k=0;k<g;s=k+=1)e=t[m++],r=s<u?0:f[a-u],f[a++]=(e+r)%256;break;case 2:for(s=T=0;T<g;s=T+=1)e=t[m++],i=(s-s%u)/u,y=w&&f[(w-1)*g+i*u+s%u],f[a++]=(y+e)%256;break;case 3:for(s=C=0;C<g;s=C+=1)e=t[m++],i=(s-s%u)/u,r=s<u?0:f[a-u],y=w&&f[(w-1)*g+i*u+s%u],f[a++]=(e+Math.floor((r+y)/2))%256;break;case 4:for(s=I=0;I<g;s=I+=1)e=t[m++],i=(s-s%u)/u,r=s<u?0:f[a-u],0===w?y=x=0:(y=f[(w-1)*g+i*u+s%u],x=i&&f[(w-1)*g+(i-1)*u+s%u]),o=r+y-x,c=Math.abs(o-r),p=Math.abs(o-y),d=Math.abs(o-x),l=c<=p&&c<=d?r:p<=d?y:x,f[a++]=(e+l)%256;break;default:throw new Error("Invalid filter algorithm: "+t[m-1])}w++}return f},o.prototype.decodePalette=function(){var t,e,a,i,s,r,n,o,h;for(a=this.palette,r=this.transparency.indexed||[],s=new Uint8Array((r.length||0)+a.length),i=0,a.length,t=0,e=n=0,o=a.length;n<o;e=n+=3)s[i++]=a[e],s[i++]=a[e+1],s[i++]=a[e+2],s[i++]=null!=(h=r[t++])?h:255;return s},o.prototype.copyToImageData=function(t,e){var a,i,s,r,n,o,h,c,l,p,d;if(i=this.colors,l=null,a=this.hasAlphaChannel,this.palette.length&&(l=null!=(d=this._decodedPalette)?d:this._decodedPalette=this.decodePalette(),i=4,a=!0),c=(s=t.data||t).length,n=l||e,r=o=0,1===i)for(;r<c;)h=l?4*e[r/4]:o,p=n[h++],s[r++]=p,s[r++]=p,s[r++]=p,s[r++]=a?n[h++]:this.transparency.grayscale&&this.transparency.grayscale===p?0:255,o=h;else for(;r<c;)h=l?4*e[r/4]:o,s[r++]=n[h++],s[r++]=n[h++],s[r++]=n[h++],s[r++]=a?n[h++]:this.transparency.rgb&&this.transparency.rgb[1]===n[h-3]&&this.transparency.rgb[3]===n[h-2]&&this.transparency.rgb[5]===n[h-1]?0:255,o=h},o.prototype.decode=function(){var t;return t=new Uint8Array(this.width*this.height*4),this.copyToImageData(t,this.decodePixels()),t},r=t.document&&t.document.createElement("canvas"),n=r&&r.getContext("2d"),s=function(t){var e;return n.width=t.width,n.height=t.height,n.clearRect(0,0,t.width,t.height),n.putImageData(t,0,0),(e=new Image).src=r.toDataURL(),e},o.prototype.decodeFrames=function(t){var e,a,i,r,n,o,h,c;if(this.animation){for(c=[],a=n=0,o=(h=this.animation.frames).length;n<o;a=++n)e=h[a],i=t.createImageData(e.width,e.height),r=this.decodePixels(new Uint8Array(e.data)),this.copyToImageData(i,r),e.imageData=i,c.push(e.image=s(i));return c}},o.prototype.renderFrame=function(t,s){var r,n,o;return r=(n=this.animation.frames)[s],o=n[s-1],0===s&&t.clearRect(0,0,this.width,this.height),(null!=o?o.disposeOp:void 0)===a?t.clearRect(o.xOffset,o.yOffset,o.width,o.height):(null!=o?o.disposeOp:void 0)===i&&t.putImageData(o.imageData,o.xOffset,o.yOffset),r.blendOp===e&&t.clearRect(r.xOffset,r.yOffset,r.width,r.height),t.drawImage(r.image,r.xOffset,r.yOffset)},o.prototype.animate=function(t){var e,a,i,s,r,n,o=this;return a=0,n=this.animation,s=n.numFrames,i=n.frames,r=n.numPlays,(e=function(){var n,h;if(n=a++%s,h=i[n],o.renderFrame(t,n),s>1&&a/s<r)return o.animation._timeout=setTimeout(e,h.delay)})()},o.prototype.stopAnimation=function(){var t;return clearTimeout(null!=(t=this.animation)?t._timeout:void 0)},o.prototype.render=function(t){var e,a;return t._png&&t._png.stopAnimation(),t._png=this,t.width=this.width,t.height=this.height,e=t.getContext("2d"),this.animation?(this.decodeFrames(e),this.animate(e)):(a=e.createImageData(this.width,this.height),this.copyToImageData(a,this.decodePixels()),e.putImageData(a,0,0))},o}(self);const y=new Set(["jpg","png","bmp","gif"]);async function x(e,a){if(!f)throw new t("rasterCoded:decode","lerc decoder is not supported on big endian platform");await s();const{offset:i}=a,{width:n,height:o,pixelType:h,statistics:c,depthCount:l,noDataValues:p,bandMasks:d,pixels:u,mask:g}=r(e,{inputOffset:i,returnInterleaved:a.returnInterleaved}),y=new m({width:n,height:o,pixelType:h.toLowerCase(),pixels:u,mask:g,statistics:c.map(({minValue:t,maxValue:e})=>new w(t,e)),bandMasks:d,depthCount:l,noDataValues:p});return c?.length||y.updateStatistics(),y}async function b(e,a){await c();const i=l(e);if(!i)throw new t("rasterCodec:decode","failed to decode the input data.");const{width:s,height:r,pixels:n,pixelType:o}=i,h=new m({width:s,height:r,pixelType:o,pixels:n});return h.updateStatistics(),h}async function k(t,a){const i=await u(t,{...a,noDataValue:a.tiffNoDataValue,matchAllNoData:a.matchAllNoData});e(i);const s=new m({width:i.width,height:i.height,pixels:i.pixels,pixelType:i.pixelType.toLowerCase(),mask:i.mask,bandMasks:i.bandMasks,statistics:null});return s.updateStatistics(),s}async function T(t,e){const a=await d(t,e.customOptions),i=new m({width:a.width,height:a.height,pixels:a.pixels,pixelType:a.pixelType.toLowerCase(),mask:a.mask,statistics:null});return i.updateStatistics(),i}function C(t,e){const a=e.pixelType||"u8",i=m.getPixelArrayConstructor(a),s="u8"===a?t:new i(t.buffer),r=[],n=e.planes||1;if(1===n)r.push(s);else for(let h=0;h<n;h++){const a=(e.width||1)*(e.height||t.length),o=new i(a);for(let t=0;t<a;t++)o[t]=s[t*n+h];r.push(o)}const o=new m({width:e.width||1,height:e.height||t.length,pixels:r,pixelType:a,statistics:null});return o.updateStatistics(),o}function I(t,e){return C(new h(new Uint8Array(t)).getBytes(),e)}function A(t,e){return C(n(t,e.offset,e.eof,!e.isInputBigEndian),e)}function v(t,e,a){const{pixelTypeCtor:i}=P(e.pixelType),s=(0,p.decode)(t,{width:e.width,height:e.height,pixelType:i,format:a}),r=new m({width:e.width,height:e.height,pixels:s.pixels,pixelType:e.pixelType,mask:s.mask,statistics:null});return r.updateStatistics(),r}function D(t,e){const a=i.decode(t,e.hasNoZlibMask??void 0),s=new m({width:a.width,height:a.height,pixels:a.pixels,pixelType:"u8",mask:a.mask,statistics:null});return s.updateStatistics(),s}function U(t,e){const a=new Uint8Array(t),i=new g(a),{width:s,height:r}=e,n=s*r,o=i.decode();let h,c=0,l=0;const p=new Uint8Array(n);for(c=0;c<n;c++)p[c]=o[4*c+3];const d=new m({width:s,height:r,pixels:[],pixelType:"u8",mask:p,statistics:[]});for(c=0;c<3;c++){for(h=new Uint8Array(n),l=0;l<n;l++)h[l]=o[4*l+c];d.addData({pixels:h})}return d.updateStatistics(),d}async function S(t,e,i,s){const r=new a,n={applyJpegMask:!1,format:e,...i},o=await r.decode(t,n,s),h=new m(o);return h.updateStatistics(),h}function j(e){if(null==e)throw new t("rasterCodec:decode","parameter encodeddata is required.");const a=new Uint8Array(e,0,10);let i="";return 255===a[0]&&216===a[1]?i="jpg":137===a[0]&&80===a[1]&&78===a[2]&&71===a[3]?i="png":67===a[0]&&110===a[1]&&116===a[2]&&90===a[3]&&73===a[4]&&109===a[5]&&97===a[6]&&103===a[7]&&101===a[8]&&32===a[9]?i="lerc":76===a[0]&&101===a[1]&&114===a[2]&&99===a[3]&&50===a[4]&&32===a[5]?i="lerc2":73===a[0]&&73===a[1]&&42===a[2]&&0===a[3]||77===a[0]&&77===a[1]&&0===a[2]&&42===a[3]||73===a[0]&&73===a[1]&&43===a[2]&&0===a[3]||77===a[0]&&77===a[1]&&0===a[2]&&43===a[3]?i="tiff":71===a[0]&&73===a[1]&&70===a[2]?i="gif":66===a[0]&&77===a[1]?i="bmp":81===a[0]&&66===a[1]&&51===a[2]?i="qb3":String.fromCharCode.apply(null,a).toLowerCase().includes("error")&&(i="error"),i}function O(e){let a=null;switch(e){case"lerc":case"lerc2":a=x;break;case"jpg":a=D;break;case"png":a=U;break;case"bsq":case"bip":a=(t,a)=>v(t,a,e);break;case"tiff":a=k;break;case"deflate":a=I;break;case"lzw":a=A;break;case"qb3":a=b;break;case"error":a=()=>{throw new t("rasterCodec:decode","input data contains error")};break;default:a=()=>{throw new t("rasterCodec:decode","unsupported raster format")}}return a}function P(t){let e=null,a=null;switch(t?t.toLowerCase():"f32"){case"u1":case"u2":case"u4":case"u8":a=255,e=Uint8Array;break;case"u16":a=a||65535,e=Uint16Array;break;case"u32":a=a||2**32-1,e=Uint32Array;break;case"s8":a=a||-128,e=Int8Array;break;case"s16":a=a||-32768,e=Int16Array;break;case"s32":a=a||0-2**31,e=Int32Array;break;default:e=Float32Array}return{pixelTypeCtor:e,noDataValue:a}}function M(t,e=1){if(!t)return;const{pixels:a,width:i,height:s,mask:r}=t;if(!a||0===a.length)return;const n=i-1,o=s-1,h=[];let c=null;const l=m.getPixelArrayConstructor(t.pixelType);if(0===e){for(const t of a){const e=new l(n*o);for(let a=0;a<o;a++){const s=a*i;for(let i=0;i<n;i++)e[a*n+i]=t[s+i]}h.push(e)}if(null!=r){c=new Uint8Array(n*o);for(let t=0;t<o;t++){const e=t*i;for(let a=0;a<n;a++)c[t*n+a]=r[e+a]}}}else{for(const t of a){const e=new l(n*o);for(let a=0;a<o;a++){const s=a*i;for(let r=0;r<n;r++)e[a*n+r]=(t[s+r]+t[s+r+1]+t[s+i+r]+t[s+i+r+1])/4}h.push(e)}if(r){c=new Uint8Array(n*o);for(let t=0;t<o;t++){const e=t*i;for(let a=0;a<n;a++)c[t*n+a]=Math.min.apply(null,[r[e+a],r[e+a+1],r[e+i+a],r[e+i+a+1]])}}}t.width=n,t.height=o,t.mask=c,t.pixels=h}function L(t){let e=j(t);return"lerc2"===e?e="lerc":"error"===e&&(e=""),e}async function R(e,a={},i){if(null==e)throw new t("rasterCodec:decode","missing encodeddata parameter.");let s=a.format?.toLowerCase();if(!("bsq"!==s&&"bip"!==s||null!=a.width&&null!=a.height))throw new t("rasterCodec:decode","requires width and height in options parameter.");if("tiff"===s&&a.customOptions)return T(e,a);if((!s||"bsq"!==s&&"bip"!==s&&"deflate"!==s&&"lzw"!==s)&&(s=j(e)),a.useCanvas&&y.has(s))return S(e,s,a,i);const r=O(s);let n;a.isPoint&&(null!=(a={...a}).width&&a.width++,null!=a.height&&a.height++);const{offsets:h}=a;if(h&&h.length>1){const t=h.map((t,i)=>r(e.slice(t,h[i+1]),a)),i=await Promise.all(t);n=i[0],n.pixels=i.map(t=>t.pixels[0]);const s=i.map(t=>t.mask);i.some(t=>null!=t)&&(s.forEach((t,e)=>{null==t&&(s[e]=new Uint8Array(n.width*n.height).fill(255))}),n.bandMasks=s,n.mask=m.combineBandMasks(s)),n.updateStatistics()}else n=await r(e,{...a,offset:h?.[0]??a.offset});return"jpg"!==s&&null!=a.noDataValue&&1===n.depthCount&&o(n,a.noDataValue,{customFloatTolerance:a.tolerance}),a.isPoint&&M(n),n}export{R as decode,L as getFormat};