@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 7.73 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.33/esri/copyright.txt for details.
*/
import{_ as t}from"../../chunks/tslib.es6.js";import e from"../../core/Error.js";import s from"../../core/JSONSupport.js";import{clone as i}from"../../core/lang.js";import l from"../../core/Logger.js";import{property as r}from"../../core/accessorSupport/decorators/property.js";import{cast as o}from"../../core/accessorSupport/decorators/cast.js";import{subclass as n}from"../../core/accessorSupport/decorators/subclass.js";import{SimpleBandStatistics as a}from"./SimpleBandStatistics.js";import{getPixelValueRange as h}from"./rasterFormats/pixelRangeUtils.js";var p;let c=p=class extends s{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 a)}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]=h(t),i=this.pixels,l=this.width*this.height,r=i.length;let o,n,a;const c=[];for(let h=0;h<r;h++){a=p.createEmptyBand(t,l),o=i[h];for(let t=0;t<l;t++)n=o[t],a[t]=n>s?s:n<e?e:n;c.push(a)}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 o=this.bandMasks?.length===i?t.map((t=>this.bandMasks[t])):void 0;let{mask:n,validPixelCount:a}=this;const{width:h,height:c}=this;return o?.length&&(n=p.combineBandMasks(o),a=n.filter((t=>!!t)).length),new p({pixelType:this.pixelType,width:h,height:c,mask:n,bandMasks:o,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});let e;null!=this.mask&&(t.mask=new Uint8Array(this.mask)),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.premultiplyAlpha=this.premultiplyAlpha,t}_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 o,n,a,h;o=n=a=r[0],r.length>=3?(n=r[1],a=r[2]):2===r.length&&(n=r[1]);const p=new Uint32Array(t),c=this.width*this.height;if(o.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|n[h]*e<<8|o[h]*e:t<<24|a[h]<<16|n[h]<<8|o[h]}}else for(h=0;h<c;h++)e[h]&&(p[h]=255<<24|a[h]<<16|n[h]<<8|o[h]);else for(h=0;h<c;h++)p[h]=255<<24|a[h]<<16|n[h]<<8|o[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 o=1,n=0,a=1;if(i&&i.length>0){for(const t of i)if(null!=t.minValue&&(n=Math.min(n,t.minValue)),null!=t.maxValue&&null!=t.minValue){const e=t.maxValue-t.minValue;a=Math.max(a,e)}o=255/a}else{let t=255;"s8"===r?(n=-128,t=127):"u16"===r?t=65535:"s16"===r?(n=-32768,t=32767):"u32"===r?t=4294967295:"s32"===r?(n=-2147483648,t=2147483647):"f32"===r?(n=-34e38,t=34e38):"f64"===r&&(n=-Number.MAX_VALUE,t=Number.MAX_VALUE),o=255/(t-n)}const h=new Uint32Array(t),p=this.width*this.height;let c,u,g,f,m;if(c=u=g=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&&(g=e[2]),null!=s&&s.length===p)for(f=0;f<p;f++)s[f]&&(h[f]=255<<24|(g[f]-n)*o<<16|(u[f]-n)*o<<8|(c[f]-n)*o);else for(f=0;f<p;f++)h[f]=255<<24|(g[f]-n)*o<<16|(u[f]-n)*o<<8|(c[f]-n)*o;else if(null!=s&&s.length===p)for(f=0;f<p;f++)m=(c[f]-n)*o,s[f]&&(h[f]=255<<24|m<<16|m<<8|m);else for(f=0;f<p;f++)m=(c[f]-n)*o,h[f]=255<<24|m<<16|m<<8|m}_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,o,n;i=r=o=e[0],e.length>=3?(r=e[1],o=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(n=0;n<a;n++)t[h++]=i[n],t[h++]=r[n],t[h++]=o[n],t[h++]=1&s[n];else for(n=0;n<a;n++)t[h++]=i[n],t[h++]=r[n],t[h++]=o[n],t[h++]=1}};function u(t,e){let s=1/0,i=-1/0;const l=t.length;let r,o=0;if(null!=e)for(r=0;r<l;r++)e[r]&&(o=t[r],s=o<s?o:s,i=o>i?o:i);else for(r=0;r<l;r++)o=t[r],s=o<s?o:s,i=o>i?o:i;return new a(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([o("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([n("esri.layers.support.PixelBlock")],c);export{c as default};