EXRLoader.js 55 KB

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