EXRLoader.js 55 KB

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