EXRLoader.js 60 KB

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