EXRLoader.js 65 KB

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