@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 7.98 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.19/LICENSE.txt */
import{__decorate as t}from"tslib";import e from"../../core/Error.js";import{JSONSupport as s}from"../../core/JSONSupport.js";import{clone as i}from"../../core/lang.js";import l from"../../core/Logger.js";import{property as r,cast as n,subclass as o}from"../../core/accessorSupport/decorators.js";import{getPixelValueRange as a}from"../raster/formats/pixelRangeUtils.js";import h from"./SimpleBandStatistics.js";var p;let c=class extends s{static{p=this}static createEmptyBand(t,e){return new(p.getPixelArrayConstructor(t))(e)}static combineBandMasks(t){if(t.length<2)return t[0];const e=t[0].length,s=new Uint8Array(e).fill(255);for(let i=0;i<t.length;i++){const l=t[i];for(let t=0;t<e;t++)l[t]||(s[t]=0)}return s}static getPixelArrayConstructor(t){let e;switch(t){case"u1":case"u2":case"u4":case"u8":e=Uint8Array;break;case"u16":e=Uint16Array;break;case"u32":e=Uint32Array;break;case"s8":e=Int8Array;break;case"s16":e=Int16Array;break;case"s32":e=Int32Array;break;case"f32":case"c64":case"c128":case"unknown":e=Float32Array;break;case"f64":e=Float64Array}return e}constructor(t){super(t),this.width=null,this.height=null,this.pixelType="f32",this.validPixelCount=null,this.mask=null,this.maskIsAlpha=!1,this.premultiplyAlpha=!1,this.statistics=null,this.depthCount=1}castPixelType(t){if(!t)return"f32";let e=t.toLowerCase();return["u1","u2","u4"].includes(e)?e="u8":["unknown","u8","s8","u16","s16","u32","s32","f32","f64"].includes(e)||(e="f32"),e}getPlaneCount(){return this.pixels?.length}addData(t){if(!t.pixels||t.pixels.length!==this.width*this.height)throw new e("pixelblock:invalid-or-missing-pixels","add data requires valid pixels array that has same length defined by pixel block width * height");this.pixels||(this.pixels=[]),this.statistics||(this.statistics=[]),this.pixels.push(t.pixels),this.statistics.push(t.statistics??new h)}getAsRGBA(){const t=new ArrayBuffer(this.width*this.height*4);switch(this.pixelType){case"s8":case"s16":case"u16":case"s32":case"u32":case"f32":case"f64":this._fillFromNon8Bit(t);break;default:this._fillFrom8Bit(t)}return new Uint8ClampedArray(t)}getAsRGBAFloat(){const t=new Float32Array(this.width*this.height*4);return this._fillFrom32Bit(t),t}updateStatistics(){if(!this.pixels)return;this.statistics=this.pixels.map(t=>u(t,this.mask));const t=this.mask;let e=0;if(null!=t)for(let s=0;s<t.length;s++)t[s]&&e++;else e=this.width*this.height;this.validPixelCount=e}clamp(t){if(!t||"f64"===t||"f32"===t||!this.pixels)return;const[e,s]=a(t),i=this.pixels,l=this.width*this.height,r=i.length;let n,o,h;const c=[];for(let a=0;a<r;a++){h=p.createEmptyBand(t,l),n=i[a];for(let t=0;t<l;t++)o=n[t],h[t]=o>s?s:o<e?e:o;c.push(h)}this.pixels=c,this.pixelType=t}extractBands(t){const{pixels:e,statistics:s}=this;if(null==t||0===t.length||!e||0===e.length)return this;const i=e.length,l=t.some(t=>t>=e.length),r=i===t.length&&!t.some((t,e)=>t!==e);if(l||r)return this;const n=this.bandMasks?.length===i?t.map(t=>this.bandMasks[t]):void 0;let{mask:o,validPixelCount:a}=this;const{width:h,height:c}=this;return n?.length&&(o=p.combineBandMasks(n),a=o.filter(t=>!!t).length),new p({pixelType:this.pixelType,width:h,height:c,mask:o,bandMasks:n,validPixelCount:a,maskIsAlpha:this.maskIsAlpha,pixels:t.map(t=>e[t]),statistics:s&&t.map(t=>s[t])})}clone(){const t=new p({width:this.width,height:this.height,pixelType:this.pixelType,maskIsAlpha:this.maskIsAlpha,validPixelCount:this.validPixelCount,premultiplyAlpha:this.premultiplyAlpha,depthCount:this.depthCount});let e;null!=this.mask&&(t.mask=new Uint8Array(this.mask)),this.noDataValues&&(t.noDataValues=[...this.noDataValues]),this.bandMasks&&(t.bandMasks=this.bandMasks.map(t=>new Uint8Array(t)));const s=p.getPixelArrayConstructor(this.pixelType);if(this.pixels&&this.pixels.length>0){t.pixels=[];const i=!!this.pixels[0].slice;for(e=0;e<this.pixels.length;e++)t.pixels[e]=i?this.pixels[e].slice():new s(this.pixels[e])}if(this.statistics)for(t.statistics=[],e=0;e<this.statistics.length;e++)t.statistics[e]=i(this.statistics[e]);return t}getTransferableObject(){const{pixels:t,bandMasks:e,mask:s}=this;this.pixels=[],this.bandMasks=void 0,this.mask=void 0;const i=this.toJSON();this.pixels=t,this.bandMasks=e,this.mask=s,i.pixels=t?[...t]:t,i.bandMasks=e?[...e]:e,i.mask=s;const l=[];return[...t??[],s,...e??[]].filter(t=>null!=t&&ArrayBuffer.isView(t)).forEach(t=>{t&&!l.includes(t.buffer)&&l.push(t.buffer)}),{pixelBlock:i,transferList:l}}_fillFrom8Bit(t){const{mask:e,maskIsAlpha:s,premultiplyAlpha:i,pixels:r}=this;if(!t||!r?.length)return void l.getLogger(this).error("getAsRGBA()","Unable to convert to RGBA. The input pixel block is empty.");let n,o,a,h;n=o=a=r[0],r.length>=3?(o=r[1],a=r[2]):2===r.length&&(o=r[1]);const p=new Uint32Array(t),c=this.width*this.height;if(n.length===c)if(null!=e&&e.length===c)if(s)for(h=0;h<c;h++){const t=e[h];if(t){const e=t/255;p[h]=i?t<<24|a[h]*e<<16|o[h]*e<<8|n[h]*e:t<<24|a[h]<<16|o[h]<<8|n[h]}}else for(h=0;h<c;h++)e[h]&&(p[h]=255<<24|a[h]<<16|o[h]<<8|n[h]);else for(h=0;h<c;h++)p[h]=255<<24|a[h]<<16|o[h]<<8|n[h];else l.getLogger(this).error("getAsRGBA()","Unable to convert to RGBA. The pixelblock is invalid.")}_fillFromNon8Bit(t){const{pixels:e,mask:s,statistics:i}=this;if(!t||!e?.length)return void l.getLogger(this).error("getAsRGBA()","Unable to convert to RGBA. The input pixel block is empty.");const r=this.pixelType;let n=1,o=0,a=1;if(i&&i.length>0){for(const t of i)if(null!=t.minValue&&(o=Math.min(o,t.minValue)),null!=t.maxValue&&null!=t.minValue){const e=t.maxValue-t.minValue;a=Math.max(a,e)}n=255/a}else{let t=255;"s8"===r?(o=-128,t=127):"u16"===r?t=65535:"s16"===r?(o=-32768,t=32767):"u32"===r?t=4294967295:"s32"===r?(o=-2147483648,t=2147483647):"f32"===r?(o=-34e38,t=34e38):"f64"===r&&(o=-Number.MAX_VALUE,t=Number.MAX_VALUE),n=255/(t-o)}const h=new Uint32Array(t),p=this.width*this.height;let c,u,f,g,d;if(c=u=f=e[0],c.length!==p)return l.getLogger(this).error("getAsRGBA()","Unable to convert to RGBA. The pixelblock is invalid.");if(e.length>=2)if(u=e[1],e.length>=3&&(f=e[2]),null!=s&&s.length===p)for(g=0;g<p;g++)s[g]&&(h[g]=255<<24|(f[g]-o)*n<<16|(u[g]-o)*n<<8|(c[g]-o)*n);else for(g=0;g<p;g++)h[g]=255<<24|(f[g]-o)*n<<16|(u[g]-o)*n<<8|(c[g]-o)*n;else if(null!=s&&s.length===p)for(g=0;g<p;g++)d=(c[g]-o)*n,s[g]&&(h[g]=255<<24|d<<16|d<<8|d);else for(g=0;g<p;g++)d=(c[g]-o)*n,h[g]=255<<24|d<<16|d<<8|d}_fillFrom32Bit(t){const{pixels:e,mask:s}=this;if(!t||!e?.length)return l.getLogger(this).error("getAsRGBAFloat()","Unable to convert to RGBA. The input pixel block is empty.");let i,r,n,o;i=r=n=e[0],e.length>=3?(r=e[1],n=e[2]):2===e.length&&(r=e[1]);const a=this.width*this.height;if(i.length!==a)return l.getLogger(this).error("getAsRGBAFloat()","Unable to convert to RGBA. The pixelblock is invalid.");let h=0;if(null!=s&&s.length===a)for(o=0;o<a;o++)t[h++]=i[o],t[h++]=r[o],t[h++]=n[o],t[h++]=1&s[o];else for(o=0;o<a;o++)t[h++]=i[o],t[h++]=r[o],t[h++]=n[o],t[h++]=1}};function u(t,e){let s=1/0,i=-1/0;const l=t.length;let r,n=0;if(null!=e)for(r=0;r<l;r++)e[r]&&(n=t[r],s=n<s?n:s,i=n>i?n:i);else for(r=0;r<l;r++)n=t[r],s=n<s?n:s,i=n>i?n:i;return new h(s,i)}t([r({json:{write:!0}})],c.prototype,"width",void 0),t([r({json:{write:!0}})],c.prototype,"height",void 0),t([r({json:{write:!0}})],c.prototype,"pixelType",void 0),t([n("pixelType")],c.prototype,"castPixelType",null),t([r({json:{write:!0}})],c.prototype,"validPixelCount",void 0),t([r({json:{write:!0}})],c.prototype,"mask",void 0),t([r({json:{write:!0}})],c.prototype,"maskIsAlpha",void 0),t([r({json:{write:!0}})],c.prototype,"pixels",void 0),t([r()],c.prototype,"premultiplyAlpha",void 0),t([r({json:{write:!0}})],c.prototype,"statistics",void 0),t([r({json:{write:!0}})],c.prototype,"depthCount",void 0),t([r({json:{write:!0}})],c.prototype,"noDataValues",void 0),t([r({json:{write:!0}})],c.prototype,"bandMasks",void 0),c=p=t([o("esri.layers.support.PixelBlock")],c);export{c as default};