import{V as h}from"./index-6jOCfdig.js";class b{static ConvertPanoramaToCubemap(e,a,t,n,o=!1,s=!0){if(!e)throw"ConvertPanoramaToCubemap: input cannot be null";let l=0;if(e.length!=a*t*3){if(e.length!=a*t*4)throw"ConvertPanoramaToCubemap: input size is wrong";l=4}else l=3;const w=this.CreateCubemapTexture(n,this.FACE_FRONT,e,a,t,o,s,l),f=this.CreateCubemapTexture(n,this.FACE_BACK,e,a,t,o,s,l),C=this.CreateCubemapTexture(n,this.FACE_LEFT,e,a,t,o,s,l),u=this.CreateCubemapTexture(n,this.FACE_RIGHT,e,a,t,o,s,l),c=this.CreateCubemapTexture(n,this.FACE_UP,e,a,t,o,s,l),i=this.CreateCubemapTexture(n,this.FACE_DOWN,e,a,t,o,s,l);return{front:w,back:f,left:C,right:u,up:c,down:i,size:n,type:1,format:4,gammaSpace:!1}}static CreateCubemapTexture(e,a,t,n,o,s,l,w){const f=new ArrayBuffer(e*e*4*3),C=new Float32Array(f),u=s?Math.max(1,Math.round(n/4/e)):1,c=1/u,i=c*c,d=a[1].subtract(a[0]).scale(c/e),R=a[3].subtract(a[2]).scale(c/e),m=1/e;let F=0;for(let M=0;M<e;M++)for(let E=0;E<u;E++){let T=a[0],B=a[2];for(let g=0;g<e;g++)for(let _=0;_<u;_++){const x=B.subtract(T).scale(F).add(T);x.normalize();const P=this.CalcProjectionSpherical(x,t,n,o,w,l);C[M*e*3+g*3+0]+=P.r*i,C[M*e*3+g*3+1]+=P.g*i,C[M*e*3+g*3+2]+=P.b*i,T=T.add(d),B=B.add(R)}F+=m*c}return C}static CalcProjectionSpherical(e,a,t,n,o,s){let l=Math.atan2(e.z,e.x);const w=Math.acos(e.y);for(;l<-Math.PI;)l+=2*Math.PI;for(;l>Math.PI;)l-=2*Math.PI;let f=l/Math.PI;const C=w/Math.PI;f=f*.5+.5;let u=Math.round(f*t);u<0?u=0:u>=t&&(u=t-1);let c=Math.round(C*n);c<0?c=0:c>=n&&(c=n-1);const i=s?n-c-1:c,d=a[i*t*o+u*o+0],R=a[i*t*o+u*o+1],m=a[i*t*o+u*o+2];return{r:d,g:R,b:m}}}b.FACE_LEFT=[new h(-1,-1,-1),new h(1,-1,-1),new h(-1,1,-1),new h(1,1,-1)];b.FACE_RIGHT=[new h(1,-1,1),new h(-1,-1,1),new h(1,1,1),new h(-1,1,1)];b.FACE_FRONT=[new h(1,-1,-1),new h(1,-1,1),new h(1,1,-1),new h(1,1,1)];b.FACE_BACK=[new h(-1,-1,1),new h(-1,-1,-1),new h(-1,1,1),new h(-1,1,-1)];b.FACE_DOWN=[new h(1,1,-1),new h(1,1,1),new h(-1,1,-1),new h(-1,1,1)];b.FACE_UP=[new h(-1,-1,-1),new h(-1,-1,1),new h(1,-1,-1),new h(1,-1,1)];function D(r,e){return e>1023?r*Math.pow(2,1023)*Math.pow(2,e-1023):e<-1074?r*Math.pow(2,-1074)*Math.pow(2,e+1074):r*Math.pow(2,e)}function L(r,e,a,t,n,o){n>0?(n=D(1,n-136),r[o+0]=e*n,r[o+1]=a*n,r[o+2]=t*n):(r[o+0]=0,r[o+1]=0,r[o+2]=0)}function A(r,e){let a="",t="";for(let n=e;n<r.length-e&&(t=String.fromCharCode(r[n]),t!=`
`);n++)a+=t;return a}function G(r){let e=0,a=0,t=A(r,0);if(t[0]!="#"||t[1]!="?")throw"Bad HDR Format.";let n=!1,o=!1,s=0;do s+=t.length+1,t=A(r,s),t=="FORMAT=32-bit_rle_rgbe"?o=!0:t.length==0&&(n=!0);while(!n);if(!o)throw"HDR Bad header format, unsupported FORMAT";s+=t.length+1,t=A(r,s);const w=/^-Y (.*) \+X (.*)$/g.exec(t);if(!w||w.length<3)throw"HDR Bad header format, no size";if(a=parseInt(w[2]),e=parseInt(w[1]),a<8||a>32767)throw"HDR Bad header format, unsupported size";return s+=t.length+1,{height:e,width:a,dataPosition:s}}function j(r,e,a=!1){const t=new Uint8Array(r),n=G(t),o=O(t,n);return b.ConvertPanoramaToCubemap(o,n.width,n.height,e,a)}function O(r,e){return N(r,e)}function N(r,e){let a=e.height;const t=e.width;let n,o,s,l,w,f=e.dataPosition,C=0,u=0,c=0;const i=new ArrayBuffer(t*4),d=new Uint8Array(i),R=new ArrayBuffer(e.width*e.height*4*3),m=new Float32Array(R);for(;a>0;){if(n=r[f++],o=r[f++],s=r[f++],l=r[f++],n!=2||o!=2||s&128||e.width<8||e.width>32767)return U(r,e);if((s<<8|l)!=t)throw"HDR Bad header format, wrong scan line width";for(C=0,c=0;c<4;c++)for(u=(c+1)*t;C<u;)if(n=r[f++],o=r[f++],n>128){if(w=n-128,w==0||w>u-C)throw"HDR Bad Format, bad scanline data (run)";for(;w-- >0;)d[C++]=o}else{if(w=n,w==0||w>u-C)throw"HDR Bad Format, bad scanline data (non-run)";if(d[C++]=o,--w>0)for(let F=0;F<w;F++)d[C++]=r[f++]}for(c=0;c<t;c++)n=d[c],o=d[c+t],s=d[c+2*t],l=d[c+3*t],L(m,n,o,s,l,(e.height-a)*t*3+c*3);a--}return m}function U(r,e){let a=e.height;const t=e.width;let n,o,s,l,w,f=e.dataPosition;const C=new ArrayBuffer(e.width*e.height*4*3),u=new Float32Array(C);for(;a>0;){for(w=0;w<e.width;w++)n=r[f++],o=r[f++],s=r[f++],l=r[f++],L(u,n,o,s,l,(e.height-a)*t*3+w*3);a--}return u}export{j as G,G as R,O as a};
