RGBMLoader.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. ( function () {
  2. class RGBMLoader extends THREE.DataTextureLoader {
  3. loadCubemap( urls, onLoad, onProgress, onError ) {
  4. const texture = new THREE.CubeTexture();
  5. let loaded = 0;
  6. const scope = this;
  7. function loadTexture( i ) {
  8. scope.load( urls[ i ], function ( image ) {
  9. texture.images[ i ] = image;
  10. loaded ++;
  11. if ( loaded === 6 ) {
  12. texture.needsUpdate = true;
  13. if ( onLoad ) onLoad( texture );
  14. }
  15. }, undefined, onError );
  16. }
  17. for ( let i = 0; i < urls.length; ++ i ) {
  18. loadTexture( i );
  19. }
  20. texture.encoding = THREE.RGBM7Encoding;
  21. texture.format = THREE.RGBAFormat;
  22. texture.minFilter = THREE.LinearFilter;
  23. texture.generateMipmaps = false;
  24. return texture;
  25. }
  26. parse( buffer ) {
  27. const img = UPNG.decode( buffer );
  28. const rgba = UPNG.toRGBA8( img )[ 0 ];
  29. return {
  30. width: img.width,
  31. height: img.height,
  32. data: new Uint8Array( rgba ),
  33. format: THREE.RGBAFormat,
  34. type: THREE.UnsignedByteType,
  35. flipY: true,
  36. encoding: THREE.RGBM7Encoding
  37. };
  38. }
  39. } // from https://github.com/photopea/UPNG.js (MIT License)
  40. var UPNG = {};
  41. UPNG.toRGBA8 = function ( out ) {
  42. var w = out.width,
  43. h = out.height;
  44. if ( out.tabs.acTL == null ) return [ UPNG.toRGBA8.decodeImage( out.data, w, h, out ).buffer ];
  45. var frms = [];
  46. if ( out.frames[ 0 ].data == null ) out.frames[ 0 ].data = out.data;
  47. var len = w * h * 4,
  48. img = new Uint8Array( len ),
  49. empty = new Uint8Array( len ),
  50. prev = new Uint8Array( len );
  51. for ( var i = 0; i < out.frames.length; i ++ ) {
  52. var frm = out.frames[ i ];
  53. var fx = frm.rect.x,
  54. fy = frm.rect.y,
  55. fw = frm.rect.width,
  56. fh = frm.rect.height;
  57. var fdata = UPNG.toRGBA8.decodeImage( frm.data, fw, fh, out );
  58. if ( i != 0 ) for ( var j = 0; j < len; j ++ ) prev[ j ] = img[ j ];
  59. if ( frm.blend == 0 ) UPNG._copyTile( fdata, fw, fh, img, w, h, fx, fy, 0 ); else if ( frm.blend == 1 ) UPNG._copyTile( fdata, fw, fh, img, w, h, fx, fy, 1 );
  60. frms.push( img.buffer.slice( 0 ) );
  61. if ( frm.dispose == 0 ) {} else if ( frm.dispose == 1 ) UPNG._copyTile( empty, fw, fh, img, w, h, fx, fy, 0 ); else if ( frm.dispose == 2 ) for ( var j = 0; j < len; j ++ ) img[ j ] = prev[ j ];
  62. }
  63. return frms;
  64. };
  65. UPNG.toRGBA8.decodeImage = function ( data, w, h, out ) {
  66. var area = w * h,
  67. bpp = UPNG.decode._getBPP( out );
  68. var bpl = Math.ceil( w * bpp / 8 ); // bytes per line
  69. var bf = new Uint8Array( area * 4 ),
  70. bf32 = new Uint32Array( bf.buffer );
  71. var ctype = out.ctype,
  72. depth = out.depth;
  73. var rs = UPNG._bin.readUshort;
  74. if ( ctype == 6 ) {
  75. // RGB + alpha
  76. var qarea = area << 2;
  77. if ( depth == 8 ) for ( var i = 0; i < qarea; i += 4 ) {
  78. bf[ i ] = data[ i ];
  79. bf[ i + 1 ] = data[ i + 1 ];
  80. bf[ i + 2 ] = data[ i + 2 ];
  81. bf[ i + 3 ] = data[ i + 3 ];
  82. }
  83. if ( depth == 16 ) for ( var i = 0; i < qarea; i ++ ) {
  84. bf[ i ] = data[ i << 1 ];
  85. }
  86. } else if ( ctype == 2 ) {
  87. // RGB
  88. var ts = out.tabs[ 'tRNS' ];
  89. if ( ts == null ) {
  90. if ( depth == 8 ) for ( var i = 0; i < area; i ++ ) {
  91. var ti = i * 3;
  92. bf32[ i ] = 255 << 24 | data[ ti + 2 ] << 16 | data[ ti + 1 ] << 8 | data[ ti ];
  93. }
  94. if ( depth == 16 ) for ( var i = 0; i < area; i ++ ) {
  95. var ti = i * 6;
  96. bf32[ i ] = 255 << 24 | data[ ti + 4 ] << 16 | data[ ti + 2 ] << 8 | data[ ti ];
  97. }
  98. } else {
  99. var tr = ts[ 0 ],
  100. tg = ts[ 1 ],
  101. tb = ts[ 2 ];
  102. if ( depth == 8 ) for ( var i = 0; i < area; i ++ ) {
  103. var qi = i << 2,
  104. ti = i * 3;
  105. bf32[ i ] = 255 << 24 | data[ ti + 2 ] << 16 | data[ ti + 1 ] << 8 | data[ ti ];
  106. if ( data[ ti ] == tr && data[ ti + 1 ] == tg && data[ ti + 2 ] == tb ) bf[ qi + 3 ] = 0;
  107. }
  108. if ( depth == 16 ) for ( var i = 0; i < area; i ++ ) {
  109. var qi = i << 2,
  110. ti = i * 6;
  111. bf32[ i ] = 255 << 24 | data[ ti + 4 ] << 16 | data[ ti + 2 ] << 8 | data[ ti ];
  112. if ( rs( data, ti ) == tr && rs( data, ti + 2 ) == tg && rs( data, ti + 4 ) == tb ) bf[ qi + 3 ] = 0;
  113. }
  114. }
  115. } else if ( ctype == 3 ) {
  116. // palette
  117. var p = out.tabs[ 'PLTE' ],
  118. ap = out.tabs[ 'tRNS' ],
  119. tl = ap ? ap.length : 0; //console.log(p, ap);
  120. if ( depth == 1 ) for ( var y = 0; y < h; y ++ ) {
  121. var s0 = y * bpl,
  122. t0 = y * w;
  123. for ( var i = 0; i < w; i ++ ) {
  124. var qi = t0 + i << 2,
  125. j = data[ s0 + ( i >> 3 ) ] >> 7 - ( ( i & 7 ) << 0 ) & 1,
  126. cj = 3 * j;
  127. bf[ qi ] = p[ cj ];
  128. bf[ qi + 1 ] = p[ cj + 1 ];
  129. bf[ qi + 2 ] = p[ cj + 2 ];
  130. bf[ qi + 3 ] = j < tl ? ap[ j ] : 255;
  131. }
  132. }
  133. if ( depth == 2 ) for ( var y = 0; y < h; y ++ ) {
  134. var s0 = y * bpl,
  135. t0 = y * w;
  136. for ( var i = 0; i < w; i ++ ) {
  137. var qi = t0 + i << 2,
  138. j = data[ s0 + ( i >> 2 ) ] >> 6 - ( ( i & 3 ) << 1 ) & 3,
  139. cj = 3 * j;
  140. bf[ qi ] = p[ cj ];
  141. bf[ qi + 1 ] = p[ cj + 1 ];
  142. bf[ qi + 2 ] = p[ cj + 2 ];
  143. bf[ qi + 3 ] = j < tl ? ap[ j ] : 255;
  144. }
  145. }
  146. if ( depth == 4 ) for ( var y = 0; y < h; y ++ ) {
  147. var s0 = y * bpl,
  148. t0 = y * w;
  149. for ( var i = 0; i < w; i ++ ) {
  150. var qi = t0 + i << 2,
  151. j = data[ s0 + ( i >> 1 ) ] >> 4 - ( ( i & 1 ) << 2 ) & 15,
  152. cj = 3 * j;
  153. bf[ qi ] = p[ cj ];
  154. bf[ qi + 1 ] = p[ cj + 1 ];
  155. bf[ qi + 2 ] = p[ cj + 2 ];
  156. bf[ qi + 3 ] = j < tl ? ap[ j ] : 255;
  157. }
  158. }
  159. if ( depth == 8 ) for ( var i = 0; i < area; i ++ ) {
  160. var qi = i << 2,
  161. j = data[ i ],
  162. cj = 3 * j;
  163. bf[ qi ] = p[ cj ];
  164. bf[ qi + 1 ] = p[ cj + 1 ];
  165. bf[ qi + 2 ] = p[ cj + 2 ];
  166. bf[ qi + 3 ] = j < tl ? ap[ j ] : 255;
  167. }
  168. } else if ( ctype == 4 ) {
  169. // gray + alpha
  170. if ( depth == 8 ) for ( var i = 0; i < area; i ++ ) {
  171. var qi = i << 2,
  172. di = i << 1,
  173. gr = data[ di ];
  174. bf[ qi ] = gr;
  175. bf[ qi + 1 ] = gr;
  176. bf[ qi + 2 ] = gr;
  177. bf[ qi + 3 ] = data[ di + 1 ];
  178. }
  179. if ( depth == 16 ) for ( var i = 0; i < area; i ++ ) {
  180. var qi = i << 2,
  181. di = i << 2,
  182. gr = data[ di ];
  183. bf[ qi ] = gr;
  184. bf[ qi + 1 ] = gr;
  185. bf[ qi + 2 ] = gr;
  186. bf[ qi + 3 ] = data[ di + 2 ];
  187. }
  188. } else if ( ctype == 0 ) {
  189. // gray
  190. var tr = out.tabs[ 'tRNS' ] ? out.tabs[ 'tRNS' ] : - 1;
  191. for ( var y = 0; y < h; y ++ ) {
  192. var off = y * bpl,
  193. to = y * w;
  194. if ( depth == 1 ) for ( var x = 0; x < w; x ++ ) {
  195. var gr = 255 * ( data[ off + ( x >>> 3 ) ] >>> 7 - ( x & 7 ) & 1 ),
  196. al = gr == tr * 255 ? 0 : 255;
  197. bf32[ to + x ] = al << 24 | gr << 16 | gr << 8 | gr;
  198. } else if ( depth == 2 ) for ( var x = 0; x < w; x ++ ) {
  199. var gr = 85 * ( data[ off + ( x >>> 2 ) ] >>> 6 - ( ( x & 3 ) << 1 ) & 3 ),
  200. al = gr == tr * 85 ? 0 : 255;
  201. bf32[ to + x ] = al << 24 | gr << 16 | gr << 8 | gr;
  202. } else if ( depth == 4 ) for ( var x = 0; x < w; x ++ ) {
  203. var gr = 17 * ( data[ off + ( x >>> 1 ) ] >>> 4 - ( ( x & 1 ) << 2 ) & 15 ),
  204. al = gr == tr * 17 ? 0 : 255;
  205. bf32[ to + x ] = al << 24 | gr << 16 | gr << 8 | gr;
  206. } else if ( depth == 8 ) for ( var x = 0; x < w; x ++ ) {
  207. var gr = data[ off + x ],
  208. al = gr == tr ? 0 : 255;
  209. bf32[ to + x ] = al << 24 | gr << 16 | gr << 8 | gr;
  210. } else if ( depth == 16 ) for ( var x = 0; x < w; x ++ ) {
  211. var gr = data[ off + ( x << 1 ) ],
  212. al = rs( data, off + ( x << 1 ) ) == tr ? 0 : 255;
  213. bf32[ to + x ] = al << 24 | gr << 16 | gr << 8 | gr;
  214. }
  215. }
  216. } //console.log(Date.now()-time);
  217. return bf;
  218. };
  219. UPNG.decode = function ( buff ) {
  220. var data = new Uint8Array( buff ),
  221. offset = 8,
  222. bin = UPNG._bin,
  223. rUs = bin.readUshort,
  224. rUi = bin.readUint;
  225. var out = {
  226. tabs: {},
  227. frames: []
  228. };
  229. var dd = new Uint8Array( data.length ),
  230. doff = 0; // put all IDAT data into it
  231. var fd,
  232. foff = 0; // frames
  233. var mgck = [ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a ];
  234. for ( var i = 0; i < 8; i ++ ) if ( data[ i ] != mgck[ i ] ) throw 'The input is not a PNG file!';
  235. while ( offset < data.length ) {
  236. var len = bin.readUint( data, offset );
  237. offset += 4;
  238. var type = bin.readASCII( data, offset, 4 );
  239. offset += 4; //console.log(type,len);
  240. if ( type == 'IHDR' ) {
  241. UPNG.decode._IHDR( data, offset, out );
  242. } else if ( type == 'CgBI' ) {
  243. out.tabs[ type ] = data.slice( offset, offset + 4 );
  244. } else if ( type == 'IDAT' ) {
  245. for ( var i = 0; i < len; i ++ ) dd[ doff + i ] = data[ offset + i ];
  246. doff += len;
  247. } else if ( type == 'acTL' ) {
  248. out.tabs[ type ] = {
  249. num_frames: rUi( data, offset ),
  250. num_plays: rUi( data, offset + 4 )
  251. };
  252. fd = new Uint8Array( data.length );
  253. } else if ( type == 'fcTL' ) {
  254. if ( foff != 0 ) {
  255. var fr = out.frames[ out.frames.length - 1 ];
  256. fr.data = UPNG.decode._decompress( out, fd.slice( 0, foff ), fr.rect.width, fr.rect.height );
  257. foff = 0;
  258. }
  259. var rct = {
  260. x: rUi( data, offset + 12 ),
  261. y: rUi( data, offset + 16 ),
  262. width: rUi( data, offset + 4 ),
  263. height: rUi( data, offset + 8 )
  264. };
  265. var del = rUs( data, offset + 22 );
  266. del = rUs( data, offset + 20 ) / ( del == 0 ? 100 : del );
  267. var frm = {
  268. rect: rct,
  269. delay: Math.round( del * 1000 ),
  270. dispose: data[ offset + 24 ],
  271. blend: data[ offset + 25 ]
  272. }; //console.log(frm);
  273. out.frames.push( frm );
  274. } else if ( type == 'fdAT' ) {
  275. for ( var i = 0; i < len - 4; i ++ ) fd[ foff + i ] = data[ offset + i + 4 ];
  276. foff += len - 4;
  277. } else if ( type == 'pHYs' ) {
  278. out.tabs[ type ] = [ bin.readUint( data, offset ), bin.readUint( data, offset + 4 ), data[ offset + 8 ] ];
  279. } else if ( type == 'cHRM' ) {
  280. out.tabs[ type ] = [];
  281. for ( var i = 0; i < 8; i ++ ) out.tabs[ type ].push( bin.readUint( data, offset + i * 4 ) );
  282. } else if ( type == 'tEXt' || type == 'zTXt' ) {
  283. if ( out.tabs[ type ] == null ) out.tabs[ type ] = {};
  284. var nz = bin.nextZero( data, offset );
  285. var keyw = bin.readASCII( data, offset, nz - offset );
  286. var text,
  287. tl = offset + len - nz - 1;
  288. if ( type == 'tEXt' ) text = bin.readASCII( data, nz + 1, tl ); else {
  289. var bfr = UPNG.decode._inflate( data.slice( nz + 2, nz + 2 + tl ) );
  290. text = bin.readUTF8( bfr, 0, bfr.length );
  291. }
  292. out.tabs[ type ][ keyw ] = text;
  293. } else if ( type == 'iTXt' ) {
  294. if ( out.tabs[ type ] == null ) out.tabs[ type ] = {};
  295. var nz = 0,
  296. off = offset;
  297. nz = bin.nextZero( data, off );
  298. var keyw = bin.readASCII( data, off, nz - off );
  299. off = nz + 1;
  300. var cflag = data[ off ];
  301. off += 2;
  302. nz = bin.nextZero( data, off );
  303. bin.readASCII( data, off, nz - off );
  304. off = nz + 1;
  305. nz = bin.nextZero( data, off );
  306. bin.readUTF8( data, off, nz - off );
  307. off = nz + 1;
  308. var text,
  309. tl = len - ( off - offset );
  310. if ( cflag == 0 ) text = bin.readUTF8( data, off, tl ); else {
  311. var bfr = UPNG.decode._inflate( data.slice( off, off + tl ) );
  312. text = bin.readUTF8( bfr, 0, bfr.length );
  313. }
  314. out.tabs[ type ][ keyw ] = text;
  315. } else if ( type == 'PLTE' ) {
  316. out.tabs[ type ] = bin.readBytes( data, offset, len );
  317. } else if ( type == 'hIST' ) {
  318. var pl = out.tabs[ 'PLTE' ].length / 3;
  319. out.tabs[ type ] = [];
  320. for ( var i = 0; i < pl; i ++ ) out.tabs[ type ].push( rUs( data, offset + i * 2 ) );
  321. } else if ( type == 'tRNS' ) {
  322. if ( out.ctype == 3 ) out.tabs[ type ] = bin.readBytes( data, offset, len ); else if ( out.ctype == 0 ) out.tabs[ type ] = rUs( data, offset ); else if ( out.ctype == 2 ) out.tabs[ type ] = [ rUs( data, offset ), rUs( data, offset + 2 ), rUs( data, offset + 4 ) ]; //else console.log("tRNS for unsupported color type",out.ctype, len);
  323. } else if ( type == 'gAMA' ) out.tabs[ type ] = bin.readUint( data, offset ) / 100000; else if ( type == 'sRGB' ) out.tabs[ type ] = data[ offset ]; else if ( type == 'bKGD' ) {
  324. if ( out.ctype == 0 || out.ctype == 4 ) out.tabs[ type ] = [ rUs( data, offset ) ]; else if ( out.ctype == 2 || out.ctype == 6 ) out.tabs[ type ] = [ rUs( data, offset ), rUs( data, offset + 2 ), rUs( data, offset + 4 ) ]; else if ( out.ctype == 3 ) out.tabs[ type ] = data[ offset ];
  325. } else if ( type == 'IEND' ) {
  326. break;
  327. } //else { console.log("unknown chunk type", type, len); out.tabs[type]=data.slice(offset,offset+len); }
  328. offset += len;
  329. bin.readUint( data, offset );
  330. offset += 4;
  331. }
  332. if ( foff != 0 ) {
  333. var fr = out.frames[ out.frames.length - 1 ];
  334. fr.data = UPNG.decode._decompress( out, fd.slice( 0, foff ), fr.rect.width, fr.rect.height );
  335. }
  336. out.data = UPNG.decode._decompress( out, dd, out.width, out.height );
  337. delete out.compress;
  338. delete out.interlace;
  339. delete out.filter;
  340. return out;
  341. };
  342. UPNG.decode._decompress = function ( out, dd, w, h ) {
  343. var bpp = UPNG.decode._getBPP( out ),
  344. bpl = Math.ceil( w * bpp / 8 ),
  345. buff = new Uint8Array( ( bpl + 1 + out.interlace ) * h );
  346. if ( out.tabs[ 'CgBI' ] ) dd = UPNG.inflateRaw( dd, buff ); else dd = UPNG.decode._inflate( dd, buff );
  347. if ( out.interlace == 0 ) dd = UPNG.decode._filterZero( dd, out, 0, w, h ); else if ( out.interlace == 1 ) dd = UPNG.decode._readInterlace( dd, out );
  348. return dd;
  349. };
  350. UPNG.decode._inflate = function ( data, buff ) {
  351. var out = UPNG[ 'inflateRaw' ]( new Uint8Array( data.buffer, 2, data.length - 6 ), buff );
  352. return out;
  353. };
  354. UPNG.inflateRaw = function () {
  355. var H = {};
  356. H.H = {};
  357. H.H.N = function ( N, W ) {
  358. var R = Uint8Array,
  359. i = 0,
  360. m = 0,
  361. J = 0,
  362. h = 0,
  363. Q = 0,
  364. X = 0,
  365. u = 0,
  366. w = 0,
  367. d = 0,
  368. v,
  369. C;
  370. if ( N[ 0 ] == 3 && N[ 1 ] == 0 ) return W ? W : new R( 0 );
  371. var V = H.H,
  372. n = V.b,
  373. A = V.e,
  374. l = V.R,
  375. M = V.n,
  376. I = V.A,
  377. e = V.Z,
  378. b = V.m,
  379. Z = W == null;
  380. if ( Z ) W = new R( N.length >>> 2 << 5 );
  381. while ( i == 0 ) {
  382. i = n( N, d, 1 );
  383. m = n( N, d + 1, 2 );
  384. d += 3;
  385. if ( m == 0 ) {
  386. if ( ( d & 7 ) != 0 ) d += 8 - ( d & 7 );
  387. var D = ( d >>> 3 ) + 4,
  388. q = N[ D - 4 ] | N[ D - 3 ] << 8;
  389. if ( Z ) W = H.H.W( W, w + q );
  390. W.set( new R( N.buffer, N.byteOffset + D, q ), w );
  391. d = D + q << 3;
  392. w += q;
  393. continue;
  394. }
  395. if ( Z ) W = H.H.W( W, w + ( 1 << 17 ) );
  396. if ( m == 1 ) {
  397. v = b.J;
  398. C = b.h;
  399. X = ( 1 << 9 ) - 1;
  400. u = ( 1 << 5 ) - 1;
  401. }
  402. if ( m == 2 ) {
  403. J = A( N, d, 5 ) + 257;
  404. h = A( N, d + 5, 5 ) + 1;
  405. Q = A( N, d + 10, 4 ) + 4;
  406. d += 14;
  407. var j = 1;
  408. for ( var c = 0; c < 38; c += 2 ) {
  409. b.Q[ c ] = 0;
  410. b.Q[ c + 1 ] = 0;
  411. }
  412. for ( var c = 0; c < Q; c ++ ) {
  413. var K = A( N, d + c * 3, 3 );
  414. b.Q[ ( b.X[ c ] << 1 ) + 1 ] = K;
  415. if ( K > j ) j = K;
  416. }
  417. d += 3 * Q;
  418. M( b.Q, j );
  419. I( b.Q, j, b.u );
  420. v = b.w;
  421. C = b.d;
  422. d = l( b.u, ( 1 << j ) - 1, J + h, N, d, b.v );
  423. var r = V.V( b.v, 0, J, b.C );
  424. X = ( 1 << r ) - 1;
  425. var S = V.V( b.v, J, h, b.D );
  426. u = ( 1 << S ) - 1;
  427. M( b.C, r );
  428. I( b.C, r, v );
  429. M( b.D, S );
  430. I( b.D, S, C );
  431. }
  432. while ( ! 0 ) {
  433. var T = v[ e( N, d ) & X ];
  434. d += T & 15;
  435. var p = T >>> 4;
  436. if ( p >>> 8 == 0 ) {
  437. W[ w ++ ] = p;
  438. } else if ( p == 256 ) {
  439. break;
  440. } else {
  441. var z = w + p - 254;
  442. if ( p > 264 ) {
  443. var _ = b.q[ p - 257 ];
  444. z = w + ( _ >>> 3 ) + A( N, d, _ & 7 );
  445. d += _ & 7;
  446. }
  447. var $ = C[ e( N, d ) & u ];
  448. d += $ & 15;
  449. var s = $ >>> 4,
  450. Y = b.c[ s ],
  451. a = ( Y >>> 4 ) + n( N, d, Y & 15 );
  452. d += Y & 15;
  453. while ( w < z ) {
  454. W[ w ] = W[ w ++ - a ];
  455. W[ w ] = W[ w ++ - a ];
  456. W[ w ] = W[ w ++ - a ];
  457. W[ w ] = W[ w ++ - a ];
  458. }
  459. w = z;
  460. }
  461. }
  462. }
  463. return W.length == w ? W : W.slice( 0, w );
  464. };
  465. H.H.W = function ( N, W ) {
  466. var R = N.length;
  467. if ( W <= R ) return N;
  468. var V = new Uint8Array( R << 1 );
  469. V.set( N, 0 );
  470. return V;
  471. };
  472. H.H.R = function ( N, W, R, V, n, A ) {
  473. var l = H.H.e,
  474. M = H.H.Z,
  475. I = 0;
  476. while ( I < R ) {
  477. var e = N[ M( V, n ) & W ];
  478. n += e & 15;
  479. var b = e >>> 4;
  480. if ( b <= 15 ) {
  481. A[ I ] = b;
  482. I ++;
  483. } else {
  484. var Z = 0,
  485. m = 0;
  486. if ( b == 16 ) {
  487. m = 3 + l( V, n, 2 );
  488. n += 2;
  489. Z = A[ I - 1 ];
  490. } else if ( b == 17 ) {
  491. m = 3 + l( V, n, 3 );
  492. n += 3;
  493. } else if ( b == 18 ) {
  494. m = 11 + l( V, n, 7 );
  495. n += 7;
  496. }
  497. var J = I + m;
  498. while ( I < J ) {
  499. A[ I ] = Z;
  500. I ++;
  501. }
  502. }
  503. }
  504. return n;
  505. };
  506. H.H.V = function ( N, W, R, V ) {
  507. var n = 0,
  508. A = 0,
  509. l = V.length >>> 1;
  510. while ( A < R ) {
  511. var M = N[ A + W ];
  512. V[ A << 1 ] = 0;
  513. V[ ( A << 1 ) + 1 ] = M;
  514. if ( M > n ) n = M;
  515. A ++;
  516. }
  517. while ( A < l ) {
  518. V[ A << 1 ] = 0;
  519. V[ ( A << 1 ) + 1 ] = 0;
  520. A ++;
  521. }
  522. return n;
  523. };
  524. H.H.n = function ( N, W ) {
  525. var R = H.H.m,
  526. V = N.length,
  527. n,
  528. A,
  529. l,
  530. M,
  531. I,
  532. e = R.j;
  533. for ( var M = 0; M <= W; M ++ ) e[ M ] = 0;
  534. for ( M = 1; M < V; M += 2 ) e[ N[ M ] ] ++;
  535. var b = R.K;
  536. n = 0;
  537. e[ 0 ] = 0;
  538. for ( A = 1; A <= W; A ++ ) {
  539. n = n + e[ A - 1 ] << 1;
  540. b[ A ] = n;
  541. }
  542. for ( l = 0; l < V; l += 2 ) {
  543. I = N[ l + 1 ];
  544. if ( I != 0 ) {
  545. N[ l ] = b[ I ];
  546. b[ I ] ++;
  547. }
  548. }
  549. };
  550. H.H.A = function ( N, W, R ) {
  551. var V = N.length,
  552. n = H.H.m,
  553. A = n.r;
  554. for ( var l = 0; l < V; l += 2 ) if ( N[ l + 1 ] != 0 ) {
  555. var M = l >> 1,
  556. I = N[ l + 1 ],
  557. e = M << 4 | I,
  558. b = W - I,
  559. Z = N[ l ] << b,
  560. m = Z + ( 1 << b );
  561. while ( Z != m ) {
  562. var J = A[ Z ] >>> 15 - W;
  563. R[ J ] = e;
  564. Z ++;
  565. }
  566. }
  567. };
  568. H.H.l = function ( N, W ) {
  569. var R = H.H.m.r,
  570. V = 15 - W;
  571. for ( var n = 0; n < N.length; n += 2 ) {
  572. var A = N[ n ] << W - N[ n + 1 ];
  573. N[ n ] = R[ A ] >>> V;
  574. }
  575. };
  576. H.H.M = function ( N, W, R ) {
  577. R = R << ( W & 7 );
  578. var V = W >>> 3;
  579. N[ V ] |= R;
  580. N[ V + 1 ] |= R >>> 8;
  581. };
  582. H.H.I = function ( N, W, R ) {
  583. R = R << ( W & 7 );
  584. var V = W >>> 3;
  585. N[ V ] |= R;
  586. N[ V + 1 ] |= R >>> 8;
  587. N[ V + 2 ] |= R >>> 16;
  588. };
  589. H.H.e = function ( N, W, R ) {
  590. return ( N[ W >>> 3 ] | N[ ( W >>> 3 ) + 1 ] << 8 ) >>> ( W & 7 ) & ( 1 << R ) - 1;
  591. };
  592. H.H.b = function ( N, W, R ) {
  593. return ( N[ W >>> 3 ] | N[ ( W >>> 3 ) + 1 ] << 8 | N[ ( W >>> 3 ) + 2 ] << 16 ) >>> ( W & 7 ) & ( 1 << R ) - 1;
  594. };
  595. H.H.Z = function ( N, W ) {
  596. return ( N[ W >>> 3 ] | N[ ( W >>> 3 ) + 1 ] << 8 | N[ ( W >>> 3 ) + 2 ] << 16 ) >>> ( W & 7 );
  597. };
  598. H.H.i = function ( N, W ) {
  599. return ( N[ W >>> 3 ] | N[ ( W >>> 3 ) + 1 ] << 8 | N[ ( W >>> 3 ) + 2 ] << 16 | N[ ( W >>> 3 ) + 3 ] << 24 ) >>> ( W & 7 );
  600. };
  601. H.H.m = function () {
  602. var N = Uint16Array,
  603. W = Uint32Array;
  604. return {
  605. K: new N( 16 ),
  606. j: new N( 16 ),
  607. X: [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ],
  608. S: [ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 999, 999, 999 ],
  609. T: [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0, 0 ],
  610. q: new N( 32 ),
  611. p: [ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 65535, 65535 ],
  612. z: [ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 0, 0 ],
  613. c: new W( 32 ),
  614. J: new N( 512 ),
  615. _: [],
  616. h: new N( 32 ),
  617. $: [],
  618. w: new N( 32768 ),
  619. C: [],
  620. v: [],
  621. d: new N( 32768 ),
  622. D: [],
  623. u: new N( 512 ),
  624. Q: [],
  625. r: new N( 1 << 15 ),
  626. s: new W( 286 ),
  627. Y: new W( 30 ),
  628. a: new W( 19 ),
  629. t: new W( 15e3 ),
  630. k: new N( 1 << 16 ),
  631. g: new N( 1 << 15 )
  632. };
  633. }();
  634. ( function () {
  635. var N = H.H.m,
  636. W = 1 << 15;
  637. for ( var R = 0; R < W; R ++ ) {
  638. var V = R;
  639. V = ( V & 2863311530 ) >>> 1 | ( V & 1431655765 ) << 1;
  640. V = ( V & 3435973836 ) >>> 2 | ( V & 858993459 ) << 2;
  641. V = ( V & 4042322160 ) >>> 4 | ( V & 252645135 ) << 4;
  642. V = ( V & 4278255360 ) >>> 8 | ( V & 16711935 ) << 8;
  643. N.r[ R ] = ( V >>> 16 | V << 16 ) >>> 17;
  644. }
  645. function n( A, l, M ) {
  646. while ( l -- != 0 ) A.push( 0, M );
  647. }
  648. for ( var R = 0; R < 32; R ++ ) {
  649. N.q[ R ] = N.S[ R ] << 3 | N.T[ R ];
  650. N.c[ R ] = N.p[ R ] << 4 | N.z[ R ];
  651. }
  652. n( N._, 144, 8 );
  653. n( N._, 255 - 143, 9 );
  654. n( N._, 279 - 255, 7 );
  655. n( N._, 287 - 279, 8 );
  656. H.H.n( N._, 9 );
  657. H.H.A( N._, 9, N.J );
  658. H.H.l( N._, 9 );
  659. n( N.$, 32, 5 );
  660. H.H.n( N.$, 5 );
  661. H.H.A( N.$, 5, N.h );
  662. H.H.l( N.$, 5 );
  663. n( N.Q, 19, 0 );
  664. n( N.C, 286, 0 );
  665. n( N.D, 30, 0 );
  666. n( N.v, 320, 0 );
  667. } )();
  668. return H.H.N;
  669. }();
  670. UPNG.decode._readInterlace = function ( data, out ) {
  671. var w = out.width,
  672. h = out.height;
  673. var bpp = UPNG.decode._getBPP( out ),
  674. cbpp = bpp >> 3,
  675. bpl = Math.ceil( w * bpp / 8 );
  676. var img = new Uint8Array( h * bpl );
  677. var di = 0;
  678. var starting_row = [ 0, 0, 4, 0, 2, 0, 1 ];
  679. var starting_col = [ 0, 4, 0, 2, 0, 1, 0 ];
  680. var row_increment = [ 8, 8, 8, 4, 4, 2, 2 ];
  681. var col_increment = [ 8, 8, 4, 4, 2, 2, 1 ];
  682. var pass = 0;
  683. while ( pass < 7 ) {
  684. var ri = row_increment[ pass ],
  685. ci = col_increment[ pass ];
  686. var sw = 0,
  687. sh = 0;
  688. var cr = starting_row[ pass ];
  689. while ( cr < h ) {
  690. cr += ri;
  691. sh ++;
  692. }
  693. var cc = starting_col[ pass ];
  694. while ( cc < w ) {
  695. cc += ci;
  696. sw ++;
  697. }
  698. var bpll = Math.ceil( sw * bpp / 8 );
  699. UPNG.decode._filterZero( data, out, di, sw, sh );
  700. var y = 0,
  701. row = starting_row[ pass ];
  702. while ( row < h ) {
  703. var col = starting_col[ pass ];
  704. var cdi = di + y * bpll << 3;
  705. while ( col < w ) {
  706. if ( bpp == 1 ) {
  707. var val = data[ cdi >> 3 ];
  708. val = val >> 7 - ( cdi & 7 ) & 1;
  709. img[ row * bpl + ( col >> 3 ) ] |= val << 7 - ( ( col & 7 ) << 0 );
  710. }
  711. if ( bpp == 2 ) {
  712. var val = data[ cdi >> 3 ];
  713. val = val >> 6 - ( cdi & 7 ) & 3;
  714. img[ row * bpl + ( col >> 2 ) ] |= val << 6 - ( ( col & 3 ) << 1 );
  715. }
  716. if ( bpp == 4 ) {
  717. var val = data[ cdi >> 3 ];
  718. val = val >> 4 - ( cdi & 7 ) & 15;
  719. img[ row * bpl + ( col >> 1 ) ] |= val << 4 - ( ( col & 1 ) << 2 );
  720. }
  721. if ( bpp >= 8 ) {
  722. var ii = row * bpl + col * cbpp;
  723. for ( var j = 0; j < cbpp; j ++ ) img[ ii + j ] = data[ ( cdi >> 3 ) + j ];
  724. }
  725. cdi += bpp;
  726. col += ci;
  727. }
  728. y ++;
  729. row += ri;
  730. }
  731. if ( sw * sh != 0 ) di += sh * ( 1 + bpll );
  732. pass = pass + 1;
  733. }
  734. return img;
  735. };
  736. UPNG.decode._getBPP = function ( out ) {
  737. var noc = [ 1, null, 3, 1, 2, null, 4 ][ out.ctype ];
  738. return noc * out.depth;
  739. };
  740. UPNG.decode._filterZero = function ( data, out, off, w, h ) {
  741. var bpp = UPNG.decode._getBPP( out ),
  742. bpl = Math.ceil( w * bpp / 8 ),
  743. paeth = UPNG.decode._paeth;
  744. bpp = Math.ceil( bpp / 8 );
  745. var i,
  746. di,
  747. type = data[ off ],
  748. x = 0;
  749. if ( type > 1 ) data[ off ] = [ 0, 0, 1 ][ type - 2 ];
  750. if ( type == 3 ) for ( x = bpp; x < bpl; x ++ ) data[ x + 1 ] = data[ x + 1 ] + ( data[ x + 1 - bpp ] >>> 1 ) & 255;
  751. for ( var y = 0; y < h; y ++ ) {
  752. i = off + y * bpl;
  753. di = i + y + 1;
  754. type = data[ di - 1 ];
  755. x = 0;
  756. if ( type == 0 ) for ( ; x < bpl; x ++ ) data[ i + x ] = data[ di + x ]; else if ( type == 1 ) {
  757. for ( ; x < bpp; x ++ ) data[ i + x ] = data[ di + x ];
  758. for ( ; x < bpl; x ++ ) data[ i + x ] = data[ di + x ] + data[ i + x - bpp ];
  759. } else if ( type == 2 ) {
  760. for ( ; x < bpl; x ++ ) data[ i + x ] = data[ di + x ] + data[ i + x - bpl ];
  761. } else if ( type == 3 ) {
  762. for ( ; x < bpp; x ++ ) data[ i + x ] = data[ di + x ] + ( data[ i + x - bpl ] >>> 1 );
  763. for ( ; x < bpl; x ++ ) data[ i + x ] = data[ di + x ] + ( data[ i + x - bpl ] + data[ i + x - bpp ] >>> 1 );
  764. } else {
  765. for ( ; x < bpp; x ++ ) data[ i + x ] = data[ di + x ] + paeth( 0, data[ i + x - bpl ], 0 );
  766. for ( ; x < bpl; x ++ ) data[ i + x ] = data[ di + x ] + paeth( data[ i + x - bpp ], data[ i + x - bpl ], data[ i + x - bpp - bpl ] );
  767. }
  768. }
  769. return data;
  770. };
  771. UPNG.decode._paeth = function ( a, b, c ) {
  772. var p = a + b - c,
  773. pa = p - a,
  774. pb = p - b,
  775. pc = p - c;
  776. if ( pa * pa <= pb * pb && pa * pa <= pc * pc ) return a; else if ( pb * pb <= pc * pc ) return b;
  777. return c;
  778. };
  779. UPNG.decode._IHDR = function ( data, offset, out ) {
  780. var bin = UPNG._bin;
  781. out.width = bin.readUint( data, offset );
  782. offset += 4;
  783. out.height = bin.readUint( data, offset );
  784. offset += 4;
  785. out.depth = data[ offset ];
  786. offset ++;
  787. out.ctype = data[ offset ];
  788. offset ++;
  789. out.compress = data[ offset ];
  790. offset ++;
  791. out.filter = data[ offset ];
  792. offset ++;
  793. out.interlace = data[ offset ];
  794. offset ++;
  795. };
  796. UPNG._bin = {
  797. nextZero: function ( data, p ) {
  798. while ( data[ p ] != 0 ) p ++;
  799. return p;
  800. },
  801. readUshort: function ( buff, p ) {
  802. return buff[ p ] << 8 | buff[ p + 1 ];
  803. },
  804. writeUshort: function ( buff, p, n ) {
  805. buff[ p ] = n >> 8 & 255;
  806. buff[ p + 1 ] = n & 255;
  807. },
  808. readUint: function ( buff, p ) {
  809. return buff[ p ] * ( 256 * 256 * 256 ) + ( buff[ p + 1 ] << 16 | buff[ p + 2 ] << 8 | buff[ p + 3 ] );
  810. },
  811. writeUint: function ( buff, p, n ) {
  812. buff[ p ] = n >> 24 & 255;
  813. buff[ p + 1 ] = n >> 16 & 255;
  814. buff[ p + 2 ] = n >> 8 & 255;
  815. buff[ p + 3 ] = n & 255;
  816. },
  817. readASCII: function ( buff, p, l ) {
  818. var s = '';
  819. for ( var i = 0; i < l; i ++ ) s += String.fromCharCode( buff[ p + i ] );
  820. return s;
  821. },
  822. writeASCII: function ( data, p, s ) {
  823. for ( var i = 0; i < s.length; i ++ ) data[ p + i ] = s.charCodeAt( i );
  824. },
  825. readBytes: function ( buff, p, l ) {
  826. var arr = [];
  827. for ( var i = 0; i < l; i ++ ) arr.push( buff[ p + i ] );
  828. return arr;
  829. },
  830. pad: function ( n ) {
  831. return n.length < 2 ? '0' + n : n;
  832. },
  833. readUTF8: function ( buff, p, l ) {
  834. var s = '',
  835. ns;
  836. for ( var i = 0; i < l; i ++ ) s += '%' + UPNG._bin.pad( buff[ p + i ].toString( 16 ) );
  837. try {
  838. ns = decodeURIComponent( s );
  839. } catch ( e ) {
  840. return UPNG._bin.readASCII( buff, p, l );
  841. }
  842. return ns;
  843. }
  844. };
  845. UPNG._copyTile = function ( sb, sw, sh, tb, tw, th, xoff, yoff, mode ) {
  846. var w = Math.min( sw, tw ),
  847. h = Math.min( sh, th );
  848. var si = 0,
  849. ti = 0;
  850. for ( var y = 0; y < h; y ++ ) for ( var x = 0; x < w; x ++ ) {
  851. if ( xoff >= 0 && yoff >= 0 ) {
  852. si = y * sw + x << 2;
  853. ti = ( yoff + y ) * tw + xoff + x << 2;
  854. } else {
  855. si = ( - yoff + y ) * sw - xoff + x << 2;
  856. ti = y * tw + x << 2;
  857. }
  858. if ( mode == 0 ) {
  859. tb[ ti ] = sb[ si ];
  860. tb[ ti + 1 ] = sb[ si + 1 ];
  861. tb[ ti + 2 ] = sb[ si + 2 ];
  862. tb[ ti + 3 ] = sb[ si + 3 ];
  863. } else if ( mode == 1 ) {
  864. var fa = sb[ si + 3 ] * ( 1 / 255 ),
  865. fr = sb[ si ] * fa,
  866. fg = sb[ si + 1 ] * fa,
  867. fb = sb[ si + 2 ] * fa;
  868. var ba = tb[ ti + 3 ] * ( 1 / 255 ),
  869. br = tb[ ti ] * ba,
  870. bg = tb[ ti + 1 ] * ba,
  871. bb = tb[ ti + 2 ] * ba;
  872. var ifa = 1 - fa,
  873. oa = fa + ba * ifa,
  874. ioa = oa == 0 ? 0 : 1 / oa;
  875. tb[ ti + 3 ] = 255 * oa;
  876. tb[ ti + 0 ] = ( fr + br * ifa ) * ioa;
  877. tb[ ti + 1 ] = ( fg + bg * ifa ) * ioa;
  878. tb[ ti + 2 ] = ( fb + bb * ifa ) * ioa;
  879. } else if ( mode == 2 ) {
  880. // copy only differences, otherwise zero
  881. var fa = sb[ si + 3 ],
  882. fr = sb[ si ],
  883. fg = sb[ si + 1 ],
  884. fb = sb[ si + 2 ];
  885. var ba = tb[ ti + 3 ],
  886. br = tb[ ti ],
  887. bg = tb[ ti + 1 ],
  888. bb = tb[ ti + 2 ];
  889. if ( fa == ba && fr == br && fg == bg && fb == bb ) {
  890. tb[ ti ] = 0;
  891. tb[ ti + 1 ] = 0;
  892. tb[ ti + 2 ] = 0;
  893. tb[ ti + 3 ] = 0;
  894. } else {
  895. tb[ ti ] = fr;
  896. tb[ ti + 1 ] = fg;
  897. tb[ ti + 2 ] = fb;
  898. tb[ ti + 3 ] = fa;
  899. }
  900. } else if ( mode == 3 ) {
  901. // check if can be blended
  902. var fa = sb[ si + 3 ],
  903. fr = sb[ si ],
  904. fg = sb[ si + 1 ],
  905. fb = sb[ si + 2 ];
  906. var ba = tb[ ti + 3 ],
  907. br = tb[ ti ],
  908. bg = tb[ ti + 1 ],
  909. bb = tb[ ti + 2 ];
  910. if ( fa == ba && fr == br && fg == bg && fb == bb ) continue; //if(fa!=255 && ba!=0) return false;
  911. if ( fa < 220 && ba > 20 ) return false;
  912. }
  913. }
  914. return true;
  915. };
  916. THREE.RGBMLoader = RGBMLoader;
  917. } )();