EXRLoader.js 54 KB

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