EXRLoader.js 54 KB

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