EXRLoader.js 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147
  1. ( function () {
  2. /**
  3. * OpenEXR loader currently supports uncompressed, ZIP(S), RLE, PIZ and DWA/B compression.
  4. * Supports reading as UnsignedByte, HalfFloat and Float type data texture.
  5. *
  6. * Referred to the original Industrial Light & Magic OpenEXR implementation and the TinyEXR / Syoyo Fujita
  7. * implementation, so I have preserved their copyright notices.
  8. */
  9. // /*
  10. // Copyright (c) 2014 - 2017, Syoyo Fujita
  11. // All rights reserved.
  12. // Redistribution and use in source and binary forms, with or without
  13. // modification, are permitted provided that the following conditions are met:
  14. // * Redistributions of source code must retain the above copyright
  15. // notice, this list of conditions and the following disclaimer.
  16. // * Redistributions in binary form must reproduce the above copyright
  17. // notice, this list of conditions and the following disclaimer in the
  18. // documentation and/or other materials provided with the distribution.
  19. // * Neither the name of the Syoyo Fujita nor the
  20. // names of its contributors may be used to endorse or promote products
  21. // derived from this software without specific prior written permission.
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  23. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. // DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  26. // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  29. // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. // */
  33. // // TinyEXR contains some OpenEXR code, which is licensed under ------------
  34. // ///////////////////////////////////////////////////////////////////////////
  35. // //
  36. // // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
  37. // // Digital Ltd. LLC
  38. // //
  39. // // All rights reserved.
  40. // //
  41. // // Redistribution and use in source and binary forms, with or without
  42. // // modification, are permitted provided that the following conditions are
  43. // // met:
  44. // // * Redistributions of source code must retain the above copyright
  45. // // notice, this list of conditions and the following disclaimer.
  46. // // * Redistributions in binary form must reproduce the above
  47. // // copyright notice, this list of conditions and the following disclaimer
  48. // // in the documentation and/or other materials provided with the
  49. // // distribution.
  50. // // * Neither the name of Industrial Light & Magic nor the names of
  51. // // its contributors may be used to endorse or promote products derived
  52. // // from this software without specific prior written permission.
  53. // //
  54. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  55. // // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  56. // // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  57. // // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  58. // // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  59. // // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  60. // // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  61. // // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  62. // // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  63. // // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  64. // // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  65. // //
  66. // ///////////////////////////////////////////////////////////////////////////
  67. // // End of OpenEXR license -------------------------------------------------
  68. class EXRLoader extends THREE.DataTextureLoader {
  69. constructor( manager ) {
  70. super( manager );
  71. this.type = THREE.HalfFloatType;
  72. }
  73. parse( buffer ) {
  74. const USHORT_RANGE = 1 << 16;
  75. const BITMAP_SIZE = USHORT_RANGE >> 3;
  76. const HUF_ENCBITS = 16; // literal (value) bit length
  77. const HUF_DECBITS = 14; // decoding bit size (>= 8)
  78. const HUF_ENCSIZE = ( 1 << HUF_ENCBITS ) + 1; // encoding table size
  79. const HUF_DECSIZE = 1 << HUF_DECBITS; // decoding table size
  80. const HUF_DECMASK = HUF_DECSIZE - 1;
  81. const NBITS = 16;
  82. const A_OFFSET = 1 << NBITS - 1;
  83. const MOD_MASK = ( 1 << NBITS ) - 1;
  84. const SHORT_ZEROCODE_RUN = 59;
  85. const LONG_ZEROCODE_RUN = 63;
  86. const SHORTEST_LONG_RUN = 2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN;
  87. const ULONG_SIZE = 8;
  88. const FLOAT32_SIZE = 4;
  89. const INT32_SIZE = 4;
  90. const INT16_SIZE = 2;
  91. const INT8_SIZE = 1;
  92. const STATIC_HUFFMAN = 0;
  93. const DEFLATE = 1;
  94. const UNKNOWN = 0;
  95. const LOSSY_DCT = 1;
  96. const RLE = 2;
  97. const logBase = Math.pow( 2.7182818, 2.2 );
  98. function reverseLutFromBitmap( bitmap, lut ) {
  99. let k = 0;
  100. for ( let i = 0; i < USHORT_RANGE; ++ i ) {
  101. if ( i == 0 || bitmap[ i >> 3 ] & 1 << ( i & 7 ) ) {
  102. lut[ k ++ ] = i;
  103. }
  104. }
  105. const n = k - 1;
  106. while ( k < USHORT_RANGE ) lut[ k ++ ] = 0;
  107. return n;
  108. }
  109. function hufClearDecTable( hdec ) {
  110. for ( let i = 0; i < HUF_DECSIZE; i ++ ) {
  111. hdec[ i ] = {};
  112. hdec[ i ].len = 0;
  113. hdec[ i ].lit = 0;
  114. hdec[ i ].p = null;
  115. }
  116. }
  117. const getBitsReturn = {
  118. l: 0,
  119. c: 0,
  120. lc: 0
  121. };
  122. function getBits( nBits, c, lc, uInt8Array, inOffset ) {
  123. while ( lc < nBits ) {
  124. c = c << 8 | parseUint8Array( uInt8Array, inOffset );
  125. lc += 8;
  126. }
  127. lc -= nBits;
  128. getBitsReturn.l = c >> lc & ( 1 << nBits ) - 1;
  129. getBitsReturn.c = c;
  130. getBitsReturn.lc = lc;
  131. }
  132. const hufTableBuffer = new Array( 59 );
  133. function hufCanonicalCodeTable( hcode ) {
  134. for ( let i = 0; i <= 58; ++ i ) hufTableBuffer[ i ] = 0;
  135. for ( let i = 0; i < HUF_ENCSIZE; ++ i ) hufTableBuffer[ hcode[ i ] ] += 1;
  136. let c = 0;
  137. for ( let i = 58; i > 0; -- i ) {
  138. const nc = c + hufTableBuffer[ i ] >> 1;
  139. hufTableBuffer[ i ] = c;
  140. c = nc;
  141. }
  142. for ( let i = 0; i < HUF_ENCSIZE; ++ i ) {
  143. const l = hcode[ i ];
  144. if ( l > 0 ) hcode[ i ] = l | hufTableBuffer[ l ] ++ << 6;
  145. }
  146. }
  147. function hufUnpackEncTable( uInt8Array, inOffset, ni, im, iM, hcode ) {
  148. const p = inOffset;
  149. let c = 0;
  150. let lc = 0;
  151. for ( ; im <= iM; im ++ ) {
  152. if ( p.value - inOffset.value > ni ) return false;
  153. getBits( 6, c, lc, uInt8Array, p );
  154. const l = getBitsReturn.l;
  155. c = getBitsReturn.c;
  156. lc = getBitsReturn.lc;
  157. hcode[ im ] = l;
  158. if ( l == LONG_ZEROCODE_RUN ) {
  159. if ( p.value - inOffset.value > ni ) {
  160. throw new Error( 'Something wrong with hufUnpackEncTable' );
  161. }
  162. getBits( 8, c, lc, uInt8Array, p );
  163. let zerun = getBitsReturn.l + SHORTEST_LONG_RUN;
  164. c = getBitsReturn.c;
  165. lc = getBitsReturn.lc;
  166. if ( im + zerun > iM + 1 ) {
  167. throw new Error( 'Something wrong with hufUnpackEncTable' );
  168. }
  169. while ( zerun -- ) hcode[ im ++ ] = 0;
  170. im --;
  171. } else if ( l >= SHORT_ZEROCODE_RUN ) {
  172. let zerun = l - SHORT_ZEROCODE_RUN + 2;
  173. if ( im + zerun > iM + 1 ) {
  174. throw new Error( 'Something wrong with hufUnpackEncTable' );
  175. }
  176. while ( zerun -- ) hcode[ im ++ ] = 0;
  177. im --;
  178. }
  179. }
  180. hufCanonicalCodeTable( hcode );
  181. }
  182. function hufLength( code ) {
  183. return code & 63;
  184. }
  185. function hufCode( code ) {
  186. return code >> 6;
  187. }
  188. function hufBuildDecTable( hcode, im, iM, hdecod ) {
  189. for ( ; im <= iM; im ++ ) {
  190. const c = hufCode( hcode[ im ] );
  191. const l = hufLength( hcode[ im ] );
  192. if ( c >> l ) {
  193. throw new Error( 'Invalid table entry' );
  194. }
  195. if ( l > HUF_DECBITS ) {
  196. const pl = hdecod[ c >> l - HUF_DECBITS ];
  197. if ( pl.len ) {
  198. throw new Error( 'Invalid table entry' );
  199. }
  200. pl.lit ++;
  201. if ( pl.p ) {
  202. const p = pl.p;
  203. pl.p = new Array( pl.lit );
  204. for ( let i = 0; i < pl.lit - 1; ++ i ) {
  205. pl.p[ i ] = p[ i ];
  206. }
  207. } else {
  208. pl.p = new Array( 1 );
  209. }
  210. pl.p[ pl.lit - 1 ] = im;
  211. } else if ( l ) {
  212. let plOffset = 0;
  213. for ( let i = 1 << HUF_DECBITS - l; i > 0; i -- ) {
  214. const pl = hdecod[ ( c << HUF_DECBITS - l ) + plOffset ];
  215. if ( pl.len || pl.p ) {
  216. throw new Error( 'Invalid table entry' );
  217. }
  218. pl.len = l;
  219. pl.lit = im;
  220. plOffset ++;
  221. }
  222. }
  223. }
  224. return true;
  225. }
  226. const getCharReturn = {
  227. c: 0,
  228. lc: 0
  229. };
  230. function getChar( c, lc, uInt8Array, inOffset ) {
  231. c = c << 8 | parseUint8Array( uInt8Array, inOffset );
  232. lc += 8;
  233. getCharReturn.c = c;
  234. getCharReturn.lc = lc;
  235. }
  236. const getCodeReturn = {
  237. c: 0,
  238. lc: 0
  239. };
  240. function getCode( po, rlc, c, lc, uInt8Array, inOffset, outBuffer, outBufferOffset, outBufferEndOffset ) {
  241. if ( po == rlc ) {
  242. if ( lc < 8 ) {
  243. getChar( c, lc, uInt8Array, inOffset );
  244. c = getCharReturn.c;
  245. lc = getCharReturn.lc;
  246. }
  247. lc -= 8;
  248. let cs = c >> lc;
  249. cs = new Uint8Array( [ cs ] )[ 0 ];
  250. if ( outBufferOffset.value + cs > outBufferEndOffset ) {
  251. return false;
  252. }
  253. const s = outBuffer[ outBufferOffset.value - 1 ];
  254. while ( cs -- > 0 ) {
  255. outBuffer[ outBufferOffset.value ++ ] = s;
  256. }
  257. } else if ( outBufferOffset.value < outBufferEndOffset ) {
  258. outBuffer[ outBufferOffset.value ++ ] = po;
  259. } else {
  260. return false;
  261. }
  262. getCodeReturn.c = c;
  263. getCodeReturn.lc = lc;
  264. }
  265. function UInt16( value ) {
  266. return value & 0xFFFF;
  267. }
  268. function Int16( value ) {
  269. const ref = UInt16( value );
  270. return ref > 0x7FFF ? ref - 0x10000 : ref;
  271. }
  272. const wdec14Return = {
  273. a: 0,
  274. b: 0
  275. };
  276. function wdec14( l, h ) {
  277. const ls = Int16( l );
  278. const hs = Int16( h );
  279. const hi = hs;
  280. const ai = ls + ( hi & 1 ) + ( hi >> 1 );
  281. const as = ai;
  282. const bs = ai - hi;
  283. wdec14Return.a = as;
  284. wdec14Return.b = bs;
  285. }
  286. function wdec16( l, h ) {
  287. const m = UInt16( l );
  288. const d = UInt16( h );
  289. const bb = m - ( d >> 1 ) & MOD_MASK;
  290. const aa = d + bb - A_OFFSET & MOD_MASK;
  291. wdec14Return.a = aa;
  292. wdec14Return.b = bb;
  293. }
  294. function wav2Decode( buffer, j, nx, ox, ny, oy, mx ) {
  295. const w14 = mx < 1 << 14;
  296. const n = nx > ny ? ny : nx;
  297. let p = 1;
  298. let p2;
  299. let py;
  300. while ( p <= n ) p <<= 1;
  301. p >>= 1;
  302. p2 = p;
  303. p >>= 1;
  304. while ( p >= 1 ) {
  305. py = 0;
  306. const ey = py + oy * ( ny - p2 );
  307. const oy1 = oy * p;
  308. const oy2 = oy * p2;
  309. const ox1 = ox * p;
  310. const ox2 = ox * p2;
  311. let i00, i01, i10, i11;
  312. for ( ; py <= ey; py += oy2 ) {
  313. let px = py;
  314. const ex = py + ox * ( nx - p2 );
  315. for ( ; px <= ex; px += ox2 ) {
  316. const p01 = px + ox1;
  317. const p10 = px + oy1;
  318. const p11 = p10 + ox1;
  319. if ( w14 ) {
  320. wdec14( buffer[ px + j ], buffer[ p10 + j ] );
  321. i00 = wdec14Return.a;
  322. i10 = wdec14Return.b;
  323. wdec14( buffer[ p01 + j ], buffer[ p11 + j ] );
  324. i01 = wdec14Return.a;
  325. i11 = wdec14Return.b;
  326. wdec14( i00, i01 );
  327. buffer[ px + j ] = wdec14Return.a;
  328. buffer[ p01 + j ] = wdec14Return.b;
  329. wdec14( i10, i11 );
  330. buffer[ p10 + j ] = wdec14Return.a;
  331. buffer[ p11 + j ] = wdec14Return.b;
  332. } else {
  333. wdec16( buffer[ px + j ], buffer[ p10 + j ] );
  334. i00 = wdec14Return.a;
  335. i10 = wdec14Return.b;
  336. wdec16( buffer[ p01 + j ], buffer[ p11 + j ] );
  337. i01 = wdec14Return.a;
  338. i11 = wdec14Return.b;
  339. wdec16( i00, i01 );
  340. buffer[ px + j ] = wdec14Return.a;
  341. buffer[ p01 + j ] = wdec14Return.b;
  342. wdec16( i10, i11 );
  343. buffer[ p10 + j ] = wdec14Return.a;
  344. buffer[ p11 + j ] = wdec14Return.b;
  345. }
  346. }
  347. if ( nx & p ) {
  348. const p10 = px + oy1;
  349. if ( w14 ) wdec14( buffer[ px + j ], buffer[ p10 + j ] ); else wdec16( buffer[ px + j ], buffer[ p10 + j ] );
  350. i00 = wdec14Return.a;
  351. buffer[ p10 + j ] = wdec14Return.b;
  352. buffer[ px + j ] = i00;
  353. }
  354. }
  355. if ( ny & p ) {
  356. let px = py;
  357. const ex = py + ox * ( nx - p2 );
  358. for ( ; px <= ex; px += ox2 ) {
  359. const p01 = px + ox1;
  360. if ( w14 ) wdec14( buffer[ px + j ], buffer[ p01 + j ] ); else wdec16( buffer[ px + j ], buffer[ p01 + j ] );
  361. i00 = wdec14Return.a;
  362. buffer[ p01 + j ] = wdec14Return.b;
  363. buffer[ px + j ] = i00;
  364. }
  365. }
  366. p2 = p;
  367. p >>= 1;
  368. }
  369. return py;
  370. }
  371. function hufDecode( encodingTable, decodingTable, uInt8Array, inOffset, ni, rlc, no, outBuffer, outOffset ) {
  372. let c = 0;
  373. let lc = 0;
  374. const outBufferEndOffset = no;
  375. const inOffsetEnd = Math.trunc( inOffset.value + ( ni + 7 ) / 8 );
  376. while ( inOffset.value < inOffsetEnd ) {
  377. getChar( c, lc, uInt8Array, inOffset );
  378. c = getCharReturn.c;
  379. lc = getCharReturn.lc;
  380. while ( lc >= HUF_DECBITS ) {
  381. const index = c >> lc - HUF_DECBITS & HUF_DECMASK;
  382. const pl = decodingTable[ index ];
  383. if ( pl.len ) {
  384. lc -= pl.len;
  385. getCode( pl.lit, rlc, c, lc, uInt8Array, inOffset, outBuffer, outOffset, outBufferEndOffset );
  386. c = getCodeReturn.c;
  387. lc = getCodeReturn.lc;
  388. } else {
  389. if ( ! pl.p ) {
  390. throw new Error( 'hufDecode issues' );
  391. }
  392. let j;
  393. for ( j = 0; j < pl.lit; j ++ ) {
  394. const l = hufLength( encodingTable[ pl.p[ j ] ] );
  395. while ( lc < l && inOffset.value < inOffsetEnd ) {
  396. getChar( c, lc, uInt8Array, inOffset );
  397. c = getCharReturn.c;
  398. lc = getCharReturn.lc;
  399. }
  400. if ( lc >= l ) {
  401. if ( hufCode( encodingTable[ pl.p[ j ] ] ) == ( c >> lc - l & ( 1 << l ) - 1 ) ) {
  402. lc -= l;
  403. getCode( pl.p[ j ], rlc, c, lc, uInt8Array, inOffset, outBuffer, outOffset, outBufferEndOffset );
  404. c = getCodeReturn.c;
  405. lc = getCodeReturn.lc;
  406. break;
  407. }
  408. }
  409. }
  410. if ( j == pl.lit ) {
  411. throw new Error( 'hufDecode issues' );
  412. }
  413. }
  414. }
  415. }
  416. const i = 8 - ni & 7;
  417. c >>= i;
  418. lc -= i;
  419. while ( lc > 0 ) {
  420. const pl = decodingTable[ c << HUF_DECBITS - lc & HUF_DECMASK ];
  421. if ( pl.len ) {
  422. lc -= pl.len;
  423. getCode( pl.lit, rlc, c, lc, uInt8Array, inOffset, outBuffer, outOffset, outBufferEndOffset );
  424. c = getCodeReturn.c;
  425. lc = getCodeReturn.lc;
  426. } else {
  427. throw new Error( 'hufDecode issues' );
  428. }
  429. }
  430. return true;
  431. }
  432. function hufUncompress( uInt8Array, inDataView, inOffset, nCompressed, outBuffer, nRaw ) {
  433. const outOffset = {
  434. value: 0
  435. };
  436. const initialInOffset = inOffset.value;
  437. const im = parseUint32( inDataView, inOffset );
  438. const iM = parseUint32( inDataView, inOffset );
  439. inOffset.value += 4;
  440. const nBits = parseUint32( inDataView, inOffset );
  441. inOffset.value += 4;
  442. if ( im < 0 || im >= HUF_ENCSIZE || iM < 0 || iM >= HUF_ENCSIZE ) {
  443. throw new Error( 'Something wrong with HUF_ENCSIZE' );
  444. }
  445. const freq = new Array( HUF_ENCSIZE );
  446. const hdec = new Array( HUF_DECSIZE );
  447. hufClearDecTable( hdec );
  448. const ni = nCompressed - ( inOffset.value - initialInOffset );
  449. hufUnpackEncTable( uInt8Array, inOffset, ni, im, iM, freq );
  450. if ( nBits > 8 * ( nCompressed - ( inOffset.value - initialInOffset ) ) ) {
  451. throw new Error( 'Something wrong with hufUncompress' );
  452. }
  453. hufBuildDecTable( freq, im, iM, hdec );
  454. hufDecode( freq, hdec, uInt8Array, inOffset, nBits, iM, nRaw, outBuffer, outOffset );
  455. }
  456. function applyLut( lut, data, nData ) {
  457. for ( let i = 0; i < nData; ++ i ) {
  458. data[ i ] = lut[ data[ i ] ];
  459. }
  460. }
  461. function predictor( source ) {
  462. for ( let t = 1; t < source.length; t ++ ) {
  463. const d = source[ t - 1 ] + source[ t ] - 128;
  464. source[ t ] = d;
  465. }
  466. }
  467. function interleaveScalar( source, out ) {
  468. let t1 = 0;
  469. let t2 = Math.floor( ( source.length + 1 ) / 2 );
  470. let s = 0;
  471. const stop = source.length - 1;
  472. while ( true ) {
  473. if ( s > stop ) break;
  474. out[ s ++ ] = source[ t1 ++ ];
  475. if ( s > stop ) break;
  476. out[ s ++ ] = source[ t2 ++ ];
  477. }
  478. }
  479. function decodeRunLength( source ) {
  480. let size = source.byteLength;
  481. const out = new Array();
  482. let p = 0;
  483. const reader = new DataView( source );
  484. while ( size > 0 ) {
  485. const l = reader.getInt8( p ++ );
  486. if ( l < 0 ) {
  487. const count = - l;
  488. size -= count + 1;
  489. for ( let i = 0; i < count; i ++ ) {
  490. out.push( reader.getUint8( p ++ ) );
  491. }
  492. } else {
  493. const count = l;
  494. size -= 2;
  495. const value = reader.getUint8( p ++ );
  496. for ( let i = 0; i < count + 1; i ++ ) {
  497. out.push( value );
  498. }
  499. }
  500. }
  501. return out;
  502. }
  503. function lossyDctDecode( cscSet, rowPtrs, channelData, acBuffer, dcBuffer, outBuffer ) {
  504. let dataView = new DataView( outBuffer.buffer );
  505. const width = channelData[ cscSet.idx[ 0 ] ].width;
  506. const height = channelData[ cscSet.idx[ 0 ] ].height;
  507. const numComp = 3;
  508. const numFullBlocksX = Math.floor( width / 8.0 );
  509. const numBlocksX = Math.ceil( width / 8.0 );
  510. const numBlocksY = Math.ceil( height / 8.0 );
  511. const leftoverX = width - ( numBlocksX - 1 ) * 8;
  512. const leftoverY = height - ( numBlocksY - 1 ) * 8;
  513. const currAcComp = {
  514. value: 0
  515. };
  516. const currDcComp = new Array( numComp );
  517. const dctData = new Array( numComp );
  518. const halfZigBlock = new Array( numComp );
  519. const rowBlock = new Array( numComp );
  520. const rowOffsets = new Array( numComp );
  521. for ( let comp = 0; comp < numComp; ++ comp ) {
  522. rowOffsets[ comp ] = rowPtrs[ cscSet.idx[ comp ] ];
  523. currDcComp[ comp ] = comp < 1 ? 0 : currDcComp[ comp - 1 ] + numBlocksX * numBlocksY;
  524. dctData[ comp ] = new Float32Array( 64 );
  525. halfZigBlock[ comp ] = new Uint16Array( 64 );
  526. rowBlock[ comp ] = new Uint16Array( numBlocksX * 64 );
  527. }
  528. for ( let blocky = 0; blocky < numBlocksY; ++ blocky ) {
  529. let maxY = 8;
  530. if ( blocky == numBlocksY - 1 ) maxY = leftoverY;
  531. let maxX = 8;
  532. for ( let blockx = 0; blockx < numBlocksX; ++ blockx ) {
  533. if ( blockx == numBlocksX - 1 ) maxX = leftoverX;
  534. for ( let comp = 0; comp < numComp; ++ comp ) {
  535. halfZigBlock[ comp ].fill( 0 ); // set block DC component
  536. halfZigBlock[ comp ][ 0 ] = dcBuffer[ currDcComp[ comp ] ++ ]; // set block AC components
  537. unRleAC( currAcComp, acBuffer, halfZigBlock[ comp ] ); // UnZigZag block to float
  538. unZigZag( halfZigBlock[ comp ], dctData[ comp ] ); // decode float dct
  539. dctInverse( dctData[ comp ] );
  540. }
  541. if ( numComp == 3 ) {
  542. csc709Inverse( dctData );
  543. }
  544. for ( let comp = 0; comp < numComp; ++ comp ) {
  545. convertToHalf( dctData[ comp ], rowBlock[ comp ], blockx * 64 );
  546. }
  547. } // blockx
  548. let offset = 0;
  549. for ( let comp = 0; comp < numComp; ++ comp ) {
  550. const type = channelData[ cscSet.idx[ comp ] ].type;
  551. for ( let y = 8 * blocky; y < 8 * blocky + maxY; ++ y ) {
  552. offset = rowOffsets[ comp ][ y ];
  553. for ( let blockx = 0; blockx < numFullBlocksX; ++ blockx ) {
  554. const src = blockx * 64 + ( y & 0x7 ) * 8;
  555. dataView.setUint16( offset + 0 * INT16_SIZE * type, rowBlock[ comp ][ src + 0 ], true );
  556. dataView.setUint16( offset + 1 * INT16_SIZE * type, rowBlock[ comp ][ src + 1 ], true );
  557. dataView.setUint16( offset + 2 * INT16_SIZE * type, rowBlock[ comp ][ src + 2 ], true );
  558. dataView.setUint16( offset + 3 * INT16_SIZE * type, rowBlock[ comp ][ src + 3 ], true );
  559. dataView.setUint16( offset + 4 * INT16_SIZE * type, rowBlock[ comp ][ src + 4 ], true );
  560. dataView.setUint16( offset + 5 * INT16_SIZE * type, rowBlock[ comp ][ src + 5 ], true );
  561. dataView.setUint16( offset + 6 * INT16_SIZE * type, rowBlock[ comp ][ src + 6 ], true );
  562. dataView.setUint16( offset + 7 * INT16_SIZE * type, rowBlock[ comp ][ src + 7 ], true );
  563. offset += 8 * INT16_SIZE * type;
  564. }
  565. } // handle partial X blocks
  566. if ( numFullBlocksX != numBlocksX ) {
  567. for ( let y = 8 * blocky; y < 8 * blocky + maxY; ++ y ) {
  568. const offset = rowOffsets[ comp ][ y ] + 8 * numFullBlocksX * INT16_SIZE * type;
  569. const src = numFullBlocksX * 64 + ( y & 0x7 ) * 8;
  570. for ( let x = 0; x < maxX; ++ x ) {
  571. dataView.setUint16( offset + x * INT16_SIZE * type, rowBlock[ comp ][ src + x ], true );
  572. }
  573. }
  574. }
  575. } // comp
  576. } // blocky
  577. const halfRow = new Uint16Array( width );
  578. dataView = new DataView( outBuffer.buffer ); // convert channels back to float, if needed
  579. for ( let comp = 0; comp < numComp; ++ comp ) {
  580. channelData[ cscSet.idx[ comp ] ].decoded = true;
  581. const type = channelData[ cscSet.idx[ comp ] ].type;
  582. if ( channelData[ comp ].type != 2 ) continue;
  583. for ( let y = 0; y < height; ++ y ) {
  584. const offset = rowOffsets[ comp ][ y ];
  585. for ( let x = 0; x < width; ++ x ) {
  586. halfRow[ x ] = dataView.getUint16( offset + x * INT16_SIZE * type, true );
  587. }
  588. for ( let x = 0; x < width; ++ x ) {
  589. dataView.setFloat32( offset + x * INT16_SIZE * type, decodeFloat16( halfRow[ x ] ), true );
  590. }
  591. }
  592. }
  593. }
  594. function unRleAC( currAcComp, acBuffer, halfZigBlock ) {
  595. let acValue;
  596. let dctComp = 1;
  597. while ( dctComp < 64 ) {
  598. acValue = acBuffer[ currAcComp.value ];
  599. if ( acValue == 0xff00 ) {
  600. dctComp = 64;
  601. } else if ( acValue >> 8 == 0xff ) {
  602. dctComp += acValue & 0xff;
  603. } else {
  604. halfZigBlock[ dctComp ] = acValue;
  605. dctComp ++;
  606. }
  607. currAcComp.value ++;
  608. }
  609. }
  610. function unZigZag( src, dst ) {
  611. dst[ 0 ] = decodeFloat16( src[ 0 ] );
  612. dst[ 1 ] = decodeFloat16( src[ 1 ] );
  613. dst[ 2 ] = decodeFloat16( src[ 5 ] );
  614. dst[ 3 ] = decodeFloat16( src[ 6 ] );
  615. dst[ 4 ] = decodeFloat16( src[ 14 ] );
  616. dst[ 5 ] = decodeFloat16( src[ 15 ] );
  617. dst[ 6 ] = decodeFloat16( src[ 27 ] );
  618. dst[ 7 ] = decodeFloat16( src[ 28 ] );
  619. dst[ 8 ] = decodeFloat16( src[ 2 ] );
  620. dst[ 9 ] = decodeFloat16( src[ 4 ] );
  621. dst[ 10 ] = decodeFloat16( src[ 7 ] );
  622. dst[ 11 ] = decodeFloat16( src[ 13 ] );
  623. dst[ 12 ] = decodeFloat16( src[ 16 ] );
  624. dst[ 13 ] = decodeFloat16( src[ 26 ] );
  625. dst[ 14 ] = decodeFloat16( src[ 29 ] );
  626. dst[ 15 ] = decodeFloat16( src[ 42 ] );
  627. dst[ 16 ] = decodeFloat16( src[ 3 ] );
  628. dst[ 17 ] = decodeFloat16( src[ 8 ] );
  629. dst[ 18 ] = decodeFloat16( src[ 12 ] );
  630. dst[ 19 ] = decodeFloat16( src[ 17 ] );
  631. dst[ 20 ] = decodeFloat16( src[ 25 ] );
  632. dst[ 21 ] = decodeFloat16( src[ 30 ] );
  633. dst[ 22 ] = decodeFloat16( src[ 41 ] );
  634. dst[ 23 ] = decodeFloat16( src[ 43 ] );
  635. dst[ 24 ] = decodeFloat16( src[ 9 ] );
  636. dst[ 25 ] = decodeFloat16( src[ 11 ] );
  637. dst[ 26 ] = decodeFloat16( src[ 18 ] );
  638. dst[ 27 ] = decodeFloat16( src[ 24 ] );
  639. dst[ 28 ] = decodeFloat16( src[ 31 ] );
  640. dst[ 29 ] = decodeFloat16( src[ 40 ] );
  641. dst[ 30 ] = decodeFloat16( src[ 44 ] );
  642. dst[ 31 ] = decodeFloat16( src[ 53 ] );
  643. dst[ 32 ] = decodeFloat16( src[ 10 ] );
  644. dst[ 33 ] = decodeFloat16( src[ 19 ] );
  645. dst[ 34 ] = decodeFloat16( src[ 23 ] );
  646. dst[ 35 ] = decodeFloat16( src[ 32 ] );
  647. dst[ 36 ] = decodeFloat16( src[ 39 ] );
  648. dst[ 37 ] = decodeFloat16( src[ 45 ] );
  649. dst[ 38 ] = decodeFloat16( src[ 52 ] );
  650. dst[ 39 ] = decodeFloat16( src[ 54 ] );
  651. dst[ 40 ] = decodeFloat16( src[ 20 ] );
  652. dst[ 41 ] = decodeFloat16( src[ 22 ] );
  653. dst[ 42 ] = decodeFloat16( src[ 33 ] );
  654. dst[ 43 ] = decodeFloat16( src[ 38 ] );
  655. dst[ 44 ] = decodeFloat16( src[ 46 ] );
  656. dst[ 45 ] = decodeFloat16( src[ 51 ] );
  657. dst[ 46 ] = decodeFloat16( src[ 55 ] );
  658. dst[ 47 ] = decodeFloat16( src[ 60 ] );
  659. dst[ 48 ] = decodeFloat16( src[ 21 ] );
  660. dst[ 49 ] = decodeFloat16( src[ 34 ] );
  661. dst[ 50 ] = decodeFloat16( src[ 37 ] );
  662. dst[ 51 ] = decodeFloat16( src[ 47 ] );
  663. dst[ 52 ] = decodeFloat16( src[ 50 ] );
  664. dst[ 53 ] = decodeFloat16( src[ 56 ] );
  665. dst[ 54 ] = decodeFloat16( src[ 59 ] );
  666. dst[ 55 ] = decodeFloat16( src[ 61 ] );
  667. dst[ 56 ] = decodeFloat16( src[ 35 ] );
  668. dst[ 57 ] = decodeFloat16( src[ 36 ] );
  669. dst[ 58 ] = decodeFloat16( src[ 48 ] );
  670. dst[ 59 ] = decodeFloat16( src[ 49 ] );
  671. dst[ 60 ] = decodeFloat16( src[ 57 ] );
  672. dst[ 61 ] = decodeFloat16( src[ 58 ] );
  673. dst[ 62 ] = decodeFloat16( src[ 62 ] );
  674. dst[ 63 ] = decodeFloat16( src[ 63 ] );
  675. }
  676. function dctInverse( data ) {
  677. const a = 0.5 * Math.cos( 3.14159 / 4.0 );
  678. const b = 0.5 * Math.cos( 3.14159 / 16.0 );
  679. const c = 0.5 * Math.cos( 3.14159 / 8.0 );
  680. const d = 0.5 * Math.cos( 3.0 * 3.14159 / 16.0 );
  681. const e = 0.5 * Math.cos( 5.0 * 3.14159 / 16.0 );
  682. const f = 0.5 * Math.cos( 3.0 * 3.14159 / 8.0 );
  683. const g = 0.5 * Math.cos( 7.0 * 3.14159 / 16.0 );
  684. const alpha = new Array( 4 );
  685. const beta = new Array( 4 );
  686. const theta = new Array( 4 );
  687. const gamma = new Array( 4 );
  688. for ( let row = 0; row < 8; ++ row ) {
  689. const rowPtr = row * 8;
  690. alpha[ 0 ] = c * data[ rowPtr + 2 ];
  691. alpha[ 1 ] = f * data[ rowPtr + 2 ];
  692. alpha[ 2 ] = c * data[ rowPtr + 6 ];
  693. alpha[ 3 ] = f * data[ rowPtr + 6 ];
  694. beta[ 0 ] = b * data[ rowPtr + 1 ] + d * data[ rowPtr + 3 ] + e * data[ rowPtr + 5 ] + g * data[ rowPtr + 7 ];
  695. beta[ 1 ] = d * data[ rowPtr + 1 ] - g * data[ rowPtr + 3 ] - b * data[ rowPtr + 5 ] - e * data[ rowPtr + 7 ];
  696. beta[ 2 ] = e * data[ rowPtr + 1 ] - b * data[ rowPtr + 3 ] + g * data[ rowPtr + 5 ] + d * data[ rowPtr + 7 ];
  697. beta[ 3 ] = g * data[ rowPtr + 1 ] - e * data[ rowPtr + 3 ] + d * data[ rowPtr + 5 ] - b * data[ rowPtr + 7 ];
  698. theta[ 0 ] = a * ( data[ rowPtr + 0 ] + data[ rowPtr + 4 ] );
  699. theta[ 3 ] = a * ( data[ rowPtr + 0 ] - data[ rowPtr + 4 ] );
  700. theta[ 1 ] = alpha[ 0 ] + alpha[ 3 ];
  701. theta[ 2 ] = alpha[ 1 ] - alpha[ 2 ];
  702. gamma[ 0 ] = theta[ 0 ] + theta[ 1 ];
  703. gamma[ 1 ] = theta[ 3 ] + theta[ 2 ];
  704. gamma[ 2 ] = theta[ 3 ] - theta[ 2 ];
  705. gamma[ 3 ] = theta[ 0 ] - theta[ 1 ];
  706. data[ rowPtr + 0 ] = gamma[ 0 ] + beta[ 0 ];
  707. data[ rowPtr + 1 ] = gamma[ 1 ] + beta[ 1 ];
  708. data[ rowPtr + 2 ] = gamma[ 2 ] + beta[ 2 ];
  709. data[ rowPtr + 3 ] = gamma[ 3 ] + beta[ 3 ];
  710. data[ rowPtr + 4 ] = gamma[ 3 ] - beta[ 3 ];
  711. data[ rowPtr + 5 ] = gamma[ 2 ] - beta[ 2 ];
  712. data[ rowPtr + 6 ] = gamma[ 1 ] - beta[ 1 ];
  713. data[ rowPtr + 7 ] = gamma[ 0 ] - beta[ 0 ];
  714. }
  715. for ( let column = 0; column < 8; ++ column ) {
  716. alpha[ 0 ] = c * data[ 16 + column ];
  717. alpha[ 1 ] = f * data[ 16 + column ];
  718. alpha[ 2 ] = c * data[ 48 + column ];
  719. alpha[ 3 ] = f * data[ 48 + column ];
  720. beta[ 0 ] = b * data[ 8 + column ] + d * data[ 24 + column ] + e * data[ 40 + column ] + g * data[ 56 + column ];
  721. beta[ 1 ] = d * data[ 8 + column ] - g * data[ 24 + column ] - b * data[ 40 + column ] - e * data[ 56 + column ];
  722. beta[ 2 ] = e * data[ 8 + column ] - b * data[ 24 + column ] + g * data[ 40 + column ] + d * data[ 56 + column ];
  723. beta[ 3 ] = g * data[ 8 + column ] - e * data[ 24 + column ] + d * data[ 40 + column ] - b * data[ 56 + column ];
  724. theta[ 0 ] = a * ( data[ column ] + data[ 32 + column ] );
  725. theta[ 3 ] = a * ( data[ column ] - data[ 32 + column ] );
  726. theta[ 1 ] = alpha[ 0 ] + alpha[ 3 ];
  727. theta[ 2 ] = alpha[ 1 ] - alpha[ 2 ];
  728. gamma[ 0 ] = theta[ 0 ] + theta[ 1 ];
  729. gamma[ 1 ] = theta[ 3 ] + theta[ 2 ];
  730. gamma[ 2 ] = theta[ 3 ] - theta[ 2 ];
  731. gamma[ 3 ] = theta[ 0 ] - theta[ 1 ];
  732. data[ 0 + column ] = gamma[ 0 ] + beta[ 0 ];
  733. data[ 8 + column ] = gamma[ 1 ] + beta[ 1 ];
  734. data[ 16 + column ] = gamma[ 2 ] + beta[ 2 ];
  735. data[ 24 + column ] = gamma[ 3 ] + beta[ 3 ];
  736. data[ 32 + column ] = gamma[ 3 ] - beta[ 3 ];
  737. data[ 40 + column ] = gamma[ 2 ] - beta[ 2 ];
  738. data[ 48 + column ] = gamma[ 1 ] - beta[ 1 ];
  739. data[ 56 + column ] = gamma[ 0 ] - beta[ 0 ];
  740. }
  741. }
  742. function csc709Inverse( data ) {
  743. for ( let i = 0; i < 64; ++ i ) {
  744. const y = data[ 0 ][ i ];
  745. const cb = data[ 1 ][ i ];
  746. const cr = data[ 2 ][ i ];
  747. data[ 0 ][ i ] = y + 1.5747 * cr;
  748. data[ 1 ][ i ] = y - 0.1873 * cb - 0.4682 * cr;
  749. data[ 2 ][ i ] = y + 1.8556 * cb;
  750. }
  751. }
  752. function convertToHalf( src, dst, idx ) {
  753. for ( let i = 0; i < 64; ++ i ) {
  754. dst[ idx + i ] = THREE.DataUtils.toHalfFloat( toLinear( src[ i ] ) );
  755. }
  756. }
  757. function toLinear( float ) {
  758. if ( float <= 1 ) {
  759. return Math.sign( float ) * Math.pow( Math.abs( float ), 2.2 );
  760. } else {
  761. return Math.sign( float ) * Math.pow( logBase, Math.abs( float ) - 1.0 );
  762. }
  763. }
  764. function uncompressRAW( info ) {
  765. return new DataView( info.array.buffer, info.offset.value, info.size );
  766. }
  767. function uncompressRLE( info ) {
  768. const compressed = info.viewer.buffer.slice( info.offset.value, info.offset.value + info.size );
  769. const rawBuffer = new Uint8Array( decodeRunLength( compressed ) );
  770. const tmpBuffer = new Uint8Array( rawBuffer.length );
  771. predictor( rawBuffer ); // revert predictor
  772. interleaveScalar( rawBuffer, tmpBuffer ); // interleave pixels
  773. return new DataView( tmpBuffer.buffer );
  774. }
  775. function uncompressZIP( info ) {
  776. const compressed = info.array.slice( info.offset.value, info.offset.value + info.size );
  777. if ( typeof fflate === 'undefined' ) {
  778. console.error( 'THREE.EXRLoader: External library fflate.min.js required.' );
  779. }
  780. const rawBuffer = fflate.unzlibSync( compressed ); // eslint-disable-line no-undef
  781. const tmpBuffer = new Uint8Array( rawBuffer.length );
  782. predictor( rawBuffer ); // revert predictor
  783. interleaveScalar( rawBuffer, tmpBuffer ); // interleave pixels
  784. return new DataView( tmpBuffer.buffer );
  785. }
  786. function uncompressPIZ( info ) {
  787. const inDataView = info.viewer;
  788. const inOffset = {
  789. value: info.offset.value
  790. };
  791. const outBuffer = new Uint16Array( info.width * info.scanlineBlockSize * ( info.channels * info.type ) );
  792. const bitmap = new Uint8Array( BITMAP_SIZE ); // Setup channel info
  793. let outBufferEnd = 0;
  794. const pizChannelData = new Array( info.channels );
  795. for ( let i = 0; i < info.channels; i ++ ) {
  796. pizChannelData[ i ] = {};
  797. pizChannelData[ i ][ 'start' ] = outBufferEnd;
  798. pizChannelData[ i ][ 'end' ] = pizChannelData[ i ][ 'start' ];
  799. pizChannelData[ i ][ 'nx' ] = info.width;
  800. pizChannelData[ i ][ 'ny' ] = info.lines;
  801. pizChannelData[ i ][ 'size' ] = info.type;
  802. outBufferEnd += pizChannelData[ i ].nx * pizChannelData[ i ].ny * pizChannelData[ i ].size;
  803. } // Read range compression data
  804. const minNonZero = parseUint16( inDataView, inOffset );
  805. const maxNonZero = parseUint16( inDataView, inOffset );
  806. if ( maxNonZero >= BITMAP_SIZE ) {
  807. throw new Error( 'Something is wrong with PIZ_COMPRESSION BITMAP_SIZE' );
  808. }
  809. if ( minNonZero <= maxNonZero ) {
  810. for ( let i = 0; i < maxNonZero - minNonZero + 1; i ++ ) {
  811. bitmap[ i + minNonZero ] = parseUint8( inDataView, inOffset );
  812. }
  813. } // Reverse LUT
  814. const lut = new Uint16Array( USHORT_RANGE );
  815. const maxValue = reverseLutFromBitmap( bitmap, lut );
  816. const length = parseUint32( inDataView, inOffset ); // Huffman decoding
  817. hufUncompress( info.array, inDataView, inOffset, length, outBuffer, outBufferEnd ); // Wavelet decoding
  818. for ( let i = 0; i < info.channels; ++ i ) {
  819. const cd = pizChannelData[ i ];
  820. for ( let j = 0; j < pizChannelData[ i ].size; ++ j ) {
  821. wav2Decode( outBuffer, cd.start + j, cd.nx, cd.size, cd.ny, cd.nx * cd.size, maxValue );
  822. }
  823. } // Expand the pixel data to their original range
  824. applyLut( lut, outBuffer, outBufferEnd ); // Rearrange the pixel data into the format expected by the caller.
  825. let tmpOffset = 0;
  826. const tmpBuffer = new Uint8Array( outBuffer.buffer.byteLength );
  827. for ( let y = 0; y < info.lines; y ++ ) {
  828. for ( let c = 0; c < info.channels; c ++ ) {
  829. const cd = pizChannelData[ c ];
  830. const n = cd.nx * cd.size;
  831. const cp = new Uint8Array( outBuffer.buffer, cd.end * INT16_SIZE, n * INT16_SIZE );
  832. tmpBuffer.set( cp, tmpOffset );
  833. tmpOffset += n * INT16_SIZE;
  834. cd.end += n;
  835. }
  836. }
  837. return new DataView( tmpBuffer.buffer );
  838. }
  839. function uncompressPXR( info ) {
  840. const compressed = info.array.slice( info.offset.value, info.offset.value + info.size );
  841. if ( typeof fflate === 'undefined' ) {
  842. console.error( 'THREE.EXRLoader: External library fflate.min.js required.' );
  843. }
  844. const rawBuffer = fflate.unzlibSync( compressed ); // eslint-disable-line no-undef
  845. const sz = info.lines * info.channels * info.width;
  846. const tmpBuffer = info.type == 1 ? new Uint16Array( sz ) : new Uint32Array( sz );
  847. let tmpBufferEnd = 0;
  848. let writePtr = 0;
  849. const ptr = new Array( 4 );
  850. for ( let y = 0; y < info.lines; y ++ ) {
  851. for ( let c = 0; c < info.channels; c ++ ) {
  852. let pixel = 0;
  853. switch ( info.type ) {
  854. case 1:
  855. ptr[ 0 ] = tmpBufferEnd;
  856. ptr[ 1 ] = ptr[ 0 ] + info.width;
  857. tmpBufferEnd = ptr[ 1 ] + info.width;
  858. for ( let j = 0; j < info.width; ++ j ) {
  859. const diff = rawBuffer[ ptr[ 0 ] ++ ] << 8 | rawBuffer[ ptr[ 1 ] ++ ];
  860. pixel += diff;
  861. tmpBuffer[ writePtr ] = pixel;
  862. writePtr ++;
  863. }
  864. break;
  865. case 2:
  866. ptr[ 0 ] = tmpBufferEnd;
  867. ptr[ 1 ] = ptr[ 0 ] + info.width;
  868. ptr[ 2 ] = ptr[ 1 ] + info.width;
  869. tmpBufferEnd = ptr[ 2 ] + info.width;
  870. for ( let j = 0; j < info.width; ++ j ) {
  871. const diff = rawBuffer[ ptr[ 0 ] ++ ] << 24 | rawBuffer[ ptr[ 1 ] ++ ] << 16 | rawBuffer[ ptr[ 2 ] ++ ] << 8;
  872. pixel += diff;
  873. tmpBuffer[ writePtr ] = pixel;
  874. writePtr ++;
  875. }
  876. break;
  877. }
  878. }
  879. }
  880. return new DataView( tmpBuffer.buffer );
  881. }
  882. function uncompressDWA( info ) {
  883. const inDataView = info.viewer;
  884. const inOffset = {
  885. value: info.offset.value
  886. };
  887. const outBuffer = new Uint8Array( info.width * info.lines * ( info.channels * info.type * INT16_SIZE ) ); // Read compression header information
  888. const dwaHeader = {
  889. version: parseInt64( inDataView, inOffset ),
  890. unknownUncompressedSize: parseInt64( inDataView, inOffset ),
  891. unknownCompressedSize: parseInt64( inDataView, inOffset ),
  892. acCompressedSize: parseInt64( inDataView, inOffset ),
  893. dcCompressedSize: parseInt64( inDataView, inOffset ),
  894. rleCompressedSize: parseInt64( inDataView, inOffset ),
  895. rleUncompressedSize: parseInt64( inDataView, inOffset ),
  896. rleRawSize: parseInt64( inDataView, inOffset ),
  897. totalAcUncompressedCount: parseInt64( inDataView, inOffset ),
  898. totalDcUncompressedCount: parseInt64( inDataView, inOffset ),
  899. acCompression: parseInt64( inDataView, inOffset )
  900. };
  901. if ( dwaHeader.version < 2 ) throw new Error( 'EXRLoader.parse: ' + EXRHeader.compression + ' version ' + dwaHeader.version + ' is unsupported' ); // Read channel ruleset information
  902. const channelRules = new Array();
  903. let ruleSize = parseUint16( inDataView, inOffset ) - INT16_SIZE;
  904. while ( ruleSize > 0 ) {
  905. const name = parseNullTerminatedString( inDataView.buffer, inOffset );
  906. const value = parseUint8( inDataView, inOffset );
  907. const compression = value >> 2 & 3;
  908. const csc = ( value >> 4 ) - 1;
  909. const index = new Int8Array( [ csc ] )[ 0 ];
  910. const type = parseUint8( inDataView, inOffset );
  911. channelRules.push( {
  912. name: name,
  913. index: index,
  914. type: type,
  915. compression: compression
  916. } );
  917. ruleSize -= name.length + 3;
  918. } // Classify channels
  919. const channels = EXRHeader.channels;
  920. const channelData = new Array( info.channels );
  921. for ( let i = 0; i < info.channels; ++ i ) {
  922. const cd = channelData[ i ] = {};
  923. const channel = channels[ i ];
  924. cd.name = channel.name;
  925. cd.compression = UNKNOWN;
  926. cd.decoded = false;
  927. cd.type = channel.pixelType;
  928. cd.pLinear = channel.pLinear;
  929. cd.width = info.width;
  930. cd.height = info.lines;
  931. }
  932. const cscSet = {
  933. idx: new Array( 3 )
  934. };
  935. for ( let offset = 0; offset < info.channels; ++ offset ) {
  936. const cd = channelData[ offset ];
  937. for ( let i = 0; i < channelRules.length; ++ i ) {
  938. const rule = channelRules[ i ];
  939. if ( cd.name == rule.name ) {
  940. cd.compression = rule.compression;
  941. if ( rule.index >= 0 ) {
  942. cscSet.idx[ rule.index ] = offset;
  943. }
  944. cd.offset = offset;
  945. }
  946. }
  947. }
  948. let acBuffer, dcBuffer, rleBuffer; // Read DCT - AC component data
  949. if ( dwaHeader.acCompressedSize > 0 ) {
  950. switch ( dwaHeader.acCompression ) {
  951. case STATIC_HUFFMAN:
  952. acBuffer = new Uint16Array( dwaHeader.totalAcUncompressedCount );
  953. hufUncompress( info.array, inDataView, inOffset, dwaHeader.acCompressedSize, acBuffer, dwaHeader.totalAcUncompressedCount );
  954. break;
  955. case DEFLATE:
  956. const compressed = info.array.slice( inOffset.value, inOffset.value + dwaHeader.totalAcUncompressedCount );
  957. const data = fflate.unzlibSync( compressed ); // eslint-disable-line no-undef
  958. acBuffer = new Uint16Array( data.buffer );
  959. inOffset.value += dwaHeader.totalAcUncompressedCount;
  960. break;
  961. }
  962. } // Read DCT - DC component data
  963. if ( dwaHeader.dcCompressedSize > 0 ) {
  964. const zlibInfo = {
  965. array: info.array,
  966. offset: inOffset,
  967. size: dwaHeader.dcCompressedSize
  968. };
  969. dcBuffer = new Uint16Array( uncompressZIP( zlibInfo ).buffer );
  970. inOffset.value += dwaHeader.dcCompressedSize;
  971. } // Read RLE compressed data
  972. if ( dwaHeader.rleRawSize > 0 ) {
  973. const compressed = info.array.slice( inOffset.value, inOffset.value + dwaHeader.rleCompressedSize );
  974. const data = fflate.unzlibSync( compressed ); // eslint-disable-line no-undef
  975. rleBuffer = decodeRunLength( data.buffer );
  976. inOffset.value += dwaHeader.rleCompressedSize;
  977. } // Prepare outbuffer data offset
  978. let outBufferEnd = 0;
  979. const rowOffsets = new Array( channelData.length );
  980. for ( let i = 0; i < rowOffsets.length; ++ i ) {
  981. rowOffsets[ i ] = new Array();
  982. }
  983. for ( let y = 0; y < info.lines; ++ y ) {
  984. for ( let chan = 0; chan < channelData.length; ++ chan ) {
  985. rowOffsets[ chan ].push( outBufferEnd );
  986. outBufferEnd += channelData[ chan ].width * info.type * INT16_SIZE;
  987. }
  988. } // Lossy DCT decode RGB channels
  989. lossyDctDecode( cscSet, rowOffsets, channelData, acBuffer, dcBuffer, outBuffer ); // Decode other channels
  990. for ( let i = 0; i < channelData.length; ++ i ) {
  991. const cd = channelData[ i ];
  992. if ( cd.decoded ) continue;
  993. switch ( cd.compression ) {
  994. case RLE:
  995. let row = 0;
  996. let rleOffset = 0;
  997. for ( let y = 0; y < info.lines; ++ y ) {
  998. let rowOffsetBytes = rowOffsets[ i ][ row ];
  999. for ( let x = 0; x < cd.width; ++ x ) {
  1000. for ( let byte = 0; byte < INT16_SIZE * cd.type; ++ byte ) {
  1001. outBuffer[ rowOffsetBytes ++ ] = rleBuffer[ rleOffset + byte * cd.width * cd.height ];
  1002. }
  1003. rleOffset ++;
  1004. }
  1005. row ++;
  1006. }
  1007. break;
  1008. case LOSSY_DCT: // skip
  1009. default:
  1010. throw new Error( 'EXRLoader.parse: unsupported channel compression' );
  1011. }
  1012. }
  1013. return new DataView( outBuffer.buffer );
  1014. }
  1015. function parseNullTerminatedString( buffer, offset ) {
  1016. const uintBuffer = new Uint8Array( buffer );
  1017. let endOffset = 0;
  1018. while ( uintBuffer[ offset.value + endOffset ] != 0 ) {
  1019. endOffset += 1;
  1020. }
  1021. const stringValue = new TextDecoder().decode( uintBuffer.slice( offset.value, offset.value + endOffset ) );
  1022. offset.value = offset.value + endOffset + 1;
  1023. return stringValue;
  1024. }
  1025. function parseFixedLengthString( buffer, offset, size ) {
  1026. const stringValue = new TextDecoder().decode( new Uint8Array( buffer ).slice( offset.value, offset.value + size ) );
  1027. offset.value = offset.value + size;
  1028. return stringValue;
  1029. }
  1030. function parseRational( dataView, offset ) {
  1031. const x = parseInt32( dataView, offset );
  1032. const y = parseUint32( dataView, offset );
  1033. return [ x, y ];
  1034. }
  1035. function parseTimecode( dataView, offset ) {
  1036. const x = parseUint32( dataView, offset );
  1037. const y = parseUint32( dataView, offset );
  1038. return [ x, y ];
  1039. }
  1040. function parseInt32( dataView, offset ) {
  1041. const Int32 = dataView.getInt32( offset.value, true );
  1042. offset.value = offset.value + INT32_SIZE;
  1043. return Int32;
  1044. }
  1045. function parseUint32( dataView, offset ) {
  1046. const Uint32 = dataView.getUint32( offset.value, true );
  1047. offset.value = offset.value + INT32_SIZE;
  1048. return Uint32;
  1049. }
  1050. function parseUint8Array( uInt8Array, offset ) {
  1051. const Uint8 = uInt8Array[ offset.value ];
  1052. offset.value = offset.value + INT8_SIZE;
  1053. return Uint8;
  1054. }
  1055. function parseUint8( dataView, offset ) {
  1056. const Uint8 = dataView.getUint8( offset.value );
  1057. offset.value = offset.value + INT8_SIZE;
  1058. return Uint8;
  1059. }
  1060. const parseInt64 = function ( dataView, offset ) {
  1061. let int;
  1062. if ( 'getBigInt64' in DataView.prototype ) {
  1063. int = Number( dataView.getBigInt64( offset.value, true ) );
  1064. } else {
  1065. int = dataView.getUint32( offset.value + 4, true ) + Number( dataView.getUint32( offset.value, true ) << 32 );
  1066. }
  1067. offset.value += ULONG_SIZE;
  1068. return int;
  1069. };
  1070. function parseFloat32( dataView, offset ) {
  1071. const float = dataView.getFloat32( offset.value, true );
  1072. offset.value += FLOAT32_SIZE;
  1073. return float;
  1074. }
  1075. function decodeFloat32( dataView, offset ) {
  1076. return THREE.DataUtils.toHalfFloat( parseFloat32( dataView, offset ) );
  1077. } // https://stackoverflow.com/questions/5678432/decompressing-half-precision-floats-in-javascript
  1078. function decodeFloat16( binary ) {
  1079. const exponent = ( binary & 0x7C00 ) >> 10,
  1080. fraction = binary & 0x03FF;
  1081. return ( binary >> 15 ? - 1 : 1 ) * ( exponent ? exponent === 0x1F ? fraction ? NaN : Infinity : Math.pow( 2, exponent - 15 ) * ( 1 + fraction / 0x400 ) : 6.103515625e-5 * ( fraction / 0x400 ) );
  1082. }
  1083. function parseUint16( dataView, offset ) {
  1084. const Uint16 = dataView.getUint16( offset.value, true );
  1085. offset.value += INT16_SIZE;
  1086. return Uint16;
  1087. }
  1088. function parseFloat16( buffer, offset ) {
  1089. return decodeFloat16( parseUint16( buffer, offset ) );
  1090. }
  1091. function parseChlist( dataView, buffer, offset, size ) {
  1092. const startOffset = offset.value;
  1093. const channels = [];
  1094. while ( offset.value < startOffset + size - 1 ) {
  1095. const name = parseNullTerminatedString( buffer, offset );
  1096. const pixelType = parseInt32( dataView, offset );
  1097. const pLinear = parseUint8( dataView, offset );
  1098. offset.value += 3; // reserved, three chars
  1099. const xSampling = parseInt32( dataView, offset );
  1100. const ySampling = parseInt32( dataView, offset );
  1101. channels.push( {
  1102. name: name,
  1103. pixelType: pixelType,
  1104. pLinear: pLinear,
  1105. xSampling: xSampling,
  1106. ySampling: ySampling
  1107. } );
  1108. }
  1109. offset.value += 1;
  1110. return channels;
  1111. }
  1112. function parseChromaticities( dataView, offset ) {
  1113. const redX = parseFloat32( dataView, offset );
  1114. const redY = parseFloat32( dataView, offset );
  1115. const greenX = parseFloat32( dataView, offset );
  1116. const greenY = parseFloat32( dataView, offset );
  1117. const blueX = parseFloat32( dataView, offset );
  1118. const blueY = parseFloat32( dataView, offset );
  1119. const whiteX = parseFloat32( dataView, offset );
  1120. const whiteY = parseFloat32( dataView, offset );
  1121. return {
  1122. redX: redX,
  1123. redY: redY,
  1124. greenX: greenX,
  1125. greenY: greenY,
  1126. blueX: blueX,
  1127. blueY: blueY,
  1128. whiteX: whiteX,
  1129. whiteY: whiteY
  1130. };
  1131. }
  1132. function parseCompression( dataView, offset ) {
  1133. const compressionCodes = [ 'NO_COMPRESSION', 'RLE_COMPRESSION', 'ZIPS_COMPRESSION', 'ZIP_COMPRESSION', 'PIZ_COMPRESSION', 'PXR24_COMPRESSION', 'B44_COMPRESSION', 'B44A_COMPRESSION', 'DWAA_COMPRESSION', 'DWAB_COMPRESSION' ];
  1134. const compression = parseUint8( dataView, offset );
  1135. return compressionCodes[ compression ];
  1136. }
  1137. function parseBox2i( dataView, offset ) {
  1138. const xMin = parseUint32( dataView, offset );
  1139. const yMin = parseUint32( dataView, offset );
  1140. const xMax = parseUint32( dataView, offset );
  1141. const yMax = parseUint32( dataView, offset );
  1142. return {
  1143. xMin: xMin,
  1144. yMin: yMin,
  1145. xMax: xMax,
  1146. yMax: yMax
  1147. };
  1148. }
  1149. function parseLineOrder( dataView, offset ) {
  1150. const lineOrders = [ 'INCREASING_Y' ];
  1151. const lineOrder = parseUint8( dataView, offset );
  1152. return lineOrders[ lineOrder ];
  1153. }
  1154. function parseV2f( dataView, offset ) {
  1155. const x = parseFloat32( dataView, offset );
  1156. const y = parseFloat32( dataView, offset );
  1157. return [ x, y ];
  1158. }
  1159. function parseV3f( dataView, offset ) {
  1160. const x = parseFloat32( dataView, offset );
  1161. const y = parseFloat32( dataView, offset );
  1162. const z = parseFloat32( dataView, offset );
  1163. return [ x, y, z ];
  1164. }
  1165. function parseValue( dataView, buffer, offset, type, size ) {
  1166. if ( type === 'string' || type === 'stringvector' || type === 'iccProfile' ) {
  1167. return parseFixedLengthString( buffer, offset, size );
  1168. } else if ( type === 'chlist' ) {
  1169. return parseChlist( dataView, buffer, offset, size );
  1170. } else if ( type === 'chromaticities' ) {
  1171. return parseChromaticities( dataView, offset );
  1172. } else if ( type === 'compression' ) {
  1173. return parseCompression( dataView, offset );
  1174. } else if ( type === 'box2i' ) {
  1175. return parseBox2i( dataView, offset );
  1176. } else if ( type === 'lineOrder' ) {
  1177. return parseLineOrder( dataView, offset );
  1178. } else if ( type === 'float' ) {
  1179. return parseFloat32( dataView, offset );
  1180. } else if ( type === 'v2f' ) {
  1181. return parseV2f( dataView, offset );
  1182. } else if ( type === 'v3f' ) {
  1183. return parseV3f( dataView, offset );
  1184. } else if ( type === 'int' ) {
  1185. return parseInt32( dataView, offset );
  1186. } else if ( type === 'rational' ) {
  1187. return parseRational( dataView, offset );
  1188. } else if ( type === 'timecode' ) {
  1189. return parseTimecode( dataView, offset );
  1190. } else if ( type === 'preview' ) {
  1191. offset.value += size;
  1192. return 'skipped';
  1193. } else {
  1194. offset.value += size;
  1195. return undefined;
  1196. }
  1197. }
  1198. function parseHeader( dataView, buffer, offset ) {
  1199. const EXRHeader = {};
  1200. if ( dataView.getUint32( 0, true ) != 20000630 ) {
  1201. // magic
  1202. throw new Error( 'THREE.EXRLoader: provided file doesn\'t appear to be in OpenEXR format.' );
  1203. }
  1204. EXRHeader.version = dataView.getUint8( 4 );
  1205. const spec = dataView.getUint8( 5 ); // fullMask
  1206. EXRHeader.spec = {
  1207. singleTile: !! ( spec & 2 ),
  1208. longName: !! ( spec & 4 ),
  1209. deepFormat: !! ( spec & 8 ),
  1210. multiPart: !! ( spec & 16 )
  1211. }; // start of header
  1212. offset.value = 8; // start at 8 - after pre-amble
  1213. let keepReading = true;
  1214. while ( keepReading ) {
  1215. const attributeName = parseNullTerminatedString( buffer, offset );
  1216. if ( attributeName == 0 ) {
  1217. keepReading = false;
  1218. } else {
  1219. const attributeType = parseNullTerminatedString( buffer, offset );
  1220. const attributeSize = parseUint32( dataView, offset );
  1221. const attributeValue = parseValue( dataView, buffer, offset, attributeType, attributeSize );
  1222. if ( attributeValue === undefined ) {
  1223. console.warn( `EXRLoader.parse: skipped unknown header attribute type \'${attributeType}\'.` );
  1224. } else {
  1225. EXRHeader[ attributeName ] = attributeValue;
  1226. }
  1227. }
  1228. }
  1229. if ( ( spec & ~ 0x04 ) != 0 ) {
  1230. // unsupported tiled, deep-image, multi-part
  1231. console.error( 'EXRHeader:', EXRHeader );
  1232. throw new Error( 'THREE.EXRLoader: provided file is currently unsupported.' );
  1233. }
  1234. return EXRHeader;
  1235. }
  1236. function setupDecoder( EXRHeader, dataView, uInt8Array, offset, outputType ) {
  1237. const EXRDecoder = {
  1238. size: 0,
  1239. viewer: dataView,
  1240. array: uInt8Array,
  1241. offset: offset,
  1242. width: EXRHeader.dataWindow.xMax - EXRHeader.dataWindow.xMin + 1,
  1243. height: EXRHeader.dataWindow.yMax - EXRHeader.dataWindow.yMin + 1,
  1244. channels: EXRHeader.channels.length,
  1245. bytesPerLine: null,
  1246. lines: null,
  1247. inputSize: null,
  1248. type: EXRHeader.channels[ 0 ].pixelType,
  1249. uncompress: null,
  1250. getter: null,
  1251. format: null,
  1252. encoding: null
  1253. };
  1254. switch ( EXRHeader.compression ) {
  1255. case 'NO_COMPRESSION':
  1256. EXRDecoder.lines = 1;
  1257. EXRDecoder.uncompress = uncompressRAW;
  1258. break;
  1259. case 'RLE_COMPRESSION':
  1260. EXRDecoder.lines = 1;
  1261. EXRDecoder.uncompress = uncompressRLE;
  1262. break;
  1263. case 'ZIPS_COMPRESSION':
  1264. EXRDecoder.lines = 1;
  1265. EXRDecoder.uncompress = uncompressZIP;
  1266. break;
  1267. case 'ZIP_COMPRESSION':
  1268. EXRDecoder.lines = 16;
  1269. EXRDecoder.uncompress = uncompressZIP;
  1270. break;
  1271. case 'PIZ_COMPRESSION':
  1272. EXRDecoder.lines = 32;
  1273. EXRDecoder.uncompress = uncompressPIZ;
  1274. break;
  1275. case 'PXR24_COMPRESSION':
  1276. EXRDecoder.lines = 16;
  1277. EXRDecoder.uncompress = uncompressPXR;
  1278. break;
  1279. case 'DWAA_COMPRESSION':
  1280. EXRDecoder.lines = 32;
  1281. EXRDecoder.uncompress = uncompressDWA;
  1282. break;
  1283. case 'DWAB_COMPRESSION':
  1284. EXRDecoder.lines = 256;
  1285. EXRDecoder.uncompress = uncompressDWA;
  1286. break;
  1287. default:
  1288. throw new Error( 'EXRLoader.parse: ' + EXRHeader.compression + ' is unsupported' );
  1289. }
  1290. EXRDecoder.scanlineBlockSize = EXRDecoder.lines;
  1291. if ( EXRDecoder.type == 1 ) {
  1292. // half
  1293. switch ( outputType ) {
  1294. case THREE.FloatType:
  1295. EXRDecoder.getter = parseFloat16;
  1296. EXRDecoder.inputSize = INT16_SIZE;
  1297. break;
  1298. case THREE.HalfFloatType:
  1299. EXRDecoder.getter = parseUint16;
  1300. EXRDecoder.inputSize = INT16_SIZE;
  1301. break;
  1302. }
  1303. } else if ( EXRDecoder.type == 2 ) {
  1304. // float
  1305. switch ( outputType ) {
  1306. case THREE.FloatType:
  1307. EXRDecoder.getter = parseFloat32;
  1308. EXRDecoder.inputSize = FLOAT32_SIZE;
  1309. break;
  1310. case THREE.HalfFloatType:
  1311. EXRDecoder.getter = decodeFloat32;
  1312. EXRDecoder.inputSize = FLOAT32_SIZE;
  1313. }
  1314. } else {
  1315. throw new Error( 'EXRLoader.parse: unsupported pixelType ' + EXRDecoder.type + ' for ' + EXRHeader.compression + '.' );
  1316. }
  1317. EXRDecoder.blockCount = ( EXRHeader.dataWindow.yMax + 1 ) / EXRDecoder.scanlineBlockSize;
  1318. for ( let i = 0; i < EXRDecoder.blockCount; i ++ ) parseInt64( dataView, offset ); // scanlineOffset
  1319. // we should be passed the scanline offset table, ready to start reading pixel data.
  1320. // RGB images will be converted to RGBA format, preventing software emulation in select devices.
  1321. EXRDecoder.outputChannels = EXRDecoder.channels == 3 ? 4 : EXRDecoder.channels;
  1322. const size = EXRDecoder.width * EXRDecoder.height * EXRDecoder.outputChannels;
  1323. switch ( outputType ) {
  1324. case THREE.FloatType:
  1325. EXRDecoder.byteArray = new Float32Array( size ); // Fill initially with 1s for the alpha value if the texture is not RGBA, RGB values will be overwritten
  1326. if ( EXRDecoder.channels < EXRDecoder.outputChannels ) EXRDecoder.byteArray.fill( 1, 0, size );
  1327. break;
  1328. case THREE.HalfFloatType:
  1329. EXRDecoder.byteArray = new Uint16Array( size );
  1330. if ( EXRDecoder.channels < EXRDecoder.outputChannels ) EXRDecoder.byteArray.fill( 0x3C00, 0, size ); // Uint16Array holds half float data, 0x3C00 is 1
  1331. break;
  1332. default:
  1333. console.error( 'THREE.EXRLoader: unsupported type: ', outputType );
  1334. break;
  1335. }
  1336. EXRDecoder.bytesPerLine = EXRDecoder.width * EXRDecoder.inputSize * EXRDecoder.channels;
  1337. if ( EXRDecoder.outputChannels == 4 ) {
  1338. EXRDecoder.format = THREE.RGBAFormat;
  1339. EXRDecoder.encoding = THREE.LinearEncoding;
  1340. } else {
  1341. EXRDecoder.format = THREE.RedFormat;
  1342. EXRDecoder.encoding = THREE.LinearEncoding;
  1343. }
  1344. return EXRDecoder;
  1345. } // start parsing file [START]
  1346. const bufferDataView = new DataView( buffer );
  1347. const uInt8Array = new Uint8Array( buffer );
  1348. const offset = {
  1349. value: 0
  1350. }; // get header information and validate format.
  1351. const EXRHeader = parseHeader( bufferDataView, buffer, offset ); // get input compression information and prepare decoding.
  1352. const EXRDecoder = setupDecoder( EXRHeader, bufferDataView, uInt8Array, offset, this.type );
  1353. const tmpOffset = {
  1354. value: 0
  1355. };
  1356. const channelOffsets = {
  1357. R: 0,
  1358. G: 1,
  1359. B: 2,
  1360. A: 3,
  1361. Y: 0
  1362. };
  1363. for ( let scanlineBlockIdx = 0; scanlineBlockIdx < EXRDecoder.height / EXRDecoder.scanlineBlockSize; scanlineBlockIdx ++ ) {
  1364. const line = parseUint32( bufferDataView, offset ); // line_no
  1365. EXRDecoder.size = parseUint32( bufferDataView, offset ); // data_len
  1366. EXRDecoder.lines = line + EXRDecoder.scanlineBlockSize > EXRDecoder.height ? EXRDecoder.height - line : EXRDecoder.scanlineBlockSize;
  1367. const isCompressed = EXRDecoder.size < EXRDecoder.lines * EXRDecoder.bytesPerLine;
  1368. const viewer = isCompressed ? EXRDecoder.uncompress( EXRDecoder ) : uncompressRAW( EXRDecoder );
  1369. offset.value += EXRDecoder.size;
  1370. for ( let line_y = 0; line_y < EXRDecoder.scanlineBlockSize; line_y ++ ) {
  1371. const true_y = line_y + scanlineBlockIdx * EXRDecoder.scanlineBlockSize;
  1372. if ( true_y >= EXRDecoder.height ) break;
  1373. for ( let channelID = 0; channelID < EXRDecoder.channels; channelID ++ ) {
  1374. const cOff = channelOffsets[ EXRHeader.channels[ channelID ].name ];
  1375. for ( let x = 0; x < EXRDecoder.width; x ++ ) {
  1376. tmpOffset.value = ( line_y * ( EXRDecoder.channels * EXRDecoder.width ) + channelID * EXRDecoder.width + x ) * EXRDecoder.inputSize;
  1377. const outIndex = ( EXRDecoder.height - 1 - true_y ) * ( EXRDecoder.width * EXRDecoder.outputChannels ) + x * EXRDecoder.outputChannels + cOff;
  1378. EXRDecoder.byteArray[ outIndex ] = EXRDecoder.getter( viewer, tmpOffset );
  1379. }
  1380. }
  1381. }
  1382. }
  1383. return {
  1384. header: EXRHeader,
  1385. width: EXRDecoder.width,
  1386. height: EXRDecoder.height,
  1387. data: EXRDecoder.byteArray,
  1388. format: EXRDecoder.format,
  1389. encoding: EXRDecoder.encoding,
  1390. type: this.type
  1391. };
  1392. }
  1393. setDataType( value ) {
  1394. this.type = value;
  1395. return this;
  1396. }
  1397. load( url, onLoad, onProgress, onError ) {
  1398. function onLoadCallback( texture, texData ) {
  1399. texture.encoding = texData.encoding;
  1400. texture.minFilter = THREE.LinearFilter;
  1401. texture.magFilter = THREE.LinearFilter;
  1402. texture.generateMipmaps = false;
  1403. texture.flipY = false;
  1404. if ( onLoad ) onLoad( texture, texData );
  1405. }
  1406. return super.load( url, onLoadCallback, onProgress, onError );
  1407. }
  1408. }
  1409. THREE.EXRLoader = EXRLoader;
  1410. } )();