RGBMLoader.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. import {
  2. DataTextureLoader,
  3. UnsignedByteType,
  4. RGBAFormat,
  5. LinearFilter,
  6. CubeTexture,
  7. RGBM7Encoding
  8. } from '../../../build/three.module.js';
  9. class RGBMLoader extends DataTextureLoader {
  10. loadCubemap( urls, onLoad, onProgress, onError ) {
  11. const texture = new CubeTexture();
  12. let loaded = 0;
  13. const scope = this;
  14. function loadTexture( i ) {
  15. scope.load( urls[ i ], function ( image ) {
  16. texture.images[ i ] = image;
  17. loaded ++;
  18. if ( loaded === 6 ) {
  19. texture.needsUpdate = true;
  20. if ( onLoad ) onLoad( texture );
  21. }
  22. }, undefined, onError );
  23. }
  24. for ( let i = 0; i < urls.length; ++ i ) {
  25. loadTexture( i );
  26. }
  27. texture.encoding = RGBM7Encoding;
  28. texture.format = RGBAFormat;
  29. texture.minFilter = LinearFilter;
  30. texture.generateMipmaps = false;
  31. return texture;
  32. }
  33. parse( buffer ) {
  34. const img = UPNG.decode( buffer );
  35. const rgba = UPNG.toRGBA8( img )[ 0 ];
  36. return {
  37. width: img.width,
  38. height: img.height,
  39. data: new Uint8Array( rgba ),
  40. format: RGBAFormat,
  41. type: UnsignedByteType,
  42. flipY: true,
  43. encoding: RGBM7Encoding
  44. };
  45. }
  46. }
  47. // from https://github.com/photopea/UPNG.js (MIT License)
  48. var UPNG = {};
  49. UPNG.toRGBA8 = function ( out ) {
  50. var w = out.width, h = out.height;
  51. if ( out.tabs.acTL == null ) return [ UPNG.toRGBA8.decodeImage( out.data, w, h, out ).buffer ];
  52. var frms = [];
  53. if ( out.frames[ 0 ].data == null ) out.frames[ 0 ].data = out.data;
  54. var len = w * h * 4, img = new Uint8Array( len ), empty = new Uint8Array( len ), prev = new Uint8Array( len );
  55. for ( var i = 0; i < out.frames.length; i ++ ) {
  56. var frm = out.frames[ i ];
  57. var fx = frm.rect.x, fy = frm.rect.y, fw = frm.rect.width, fh = frm.rect.height;
  58. var fdata = UPNG.toRGBA8.decodeImage( frm.data, fw, fh, out );
  59. if ( i != 0 ) for ( var j = 0; j < len; j ++ ) prev[ j ] = img[ j ];
  60. if ( frm.blend == 0 ) UPNG._copyTile( fdata, fw, fh, img, w, h, fx, fy, 0 );
  61. else if ( frm.blend == 1 ) UPNG._copyTile( fdata, fw, fh, img, w, h, fx, fy, 1 );
  62. frms.push( img.buffer.slice( 0 ) );
  63. if ( frm.dispose == 0 ) {} else if ( frm.dispose == 1 ) UPNG._copyTile( empty, fw, fh, img, w, h, fx, fy, 0 );
  64. else if ( frm.dispose == 2 ) for ( var j = 0; j < len; j ++ ) img[ j ] = prev[ j ];
  65. }
  66. return frms;
  67. };
  68. UPNG.toRGBA8.decodeImage = function ( data, w, h, out ) {
  69. var area = w * h, bpp = UPNG.decode._getBPP( out );
  70. var bpl = Math.ceil( w * bpp / 8 ); // bytes per line
  71. var bf = new Uint8Array( area * 4 ), bf32 = new Uint32Array( bf.buffer );
  72. var ctype = out.ctype, depth = out.depth;
  73. var rs = UPNG._bin.readUshort;
  74. if ( ctype == 6 ) { // RGB + alpha
  75. var qarea = area << 2;
  76. if ( depth == 8 ) for ( var i = 0; i < qarea; i += 4 ) {
  77. bf[ i ] = data[ i ]; bf[ i + 1 ] = data[ i + 1 ]; bf[ i + 2 ] = data[ i + 2 ]; bf[ i + 3 ] = data[ i + 3 ];
  78. }
  79. if ( depth == 16 ) for ( var i = 0; i < qarea; i ++ ) {
  80. bf[ i ] = data[ i << 1 ];
  81. }
  82. } else if ( ctype == 2 ) { // RGB
  83. var ts = out.tabs[ 'tRNS' ];
  84. if ( ts == null ) {
  85. if ( depth == 8 ) for ( var i = 0; i < area; i ++ ) {
  86. var ti = i * 3; bf32[ i ] = ( 255 << 24 ) | ( data[ ti + 2 ] << 16 ) | ( data[ ti + 1 ] << 8 ) | data[ ti ];
  87. }
  88. if ( depth == 16 ) for ( var i = 0; i < area; i ++ ) {
  89. var ti = i * 6; bf32[ i ] = ( 255 << 24 ) | ( data[ ti + 4 ] << 16 ) | ( data[ ti + 2 ] << 8 ) | data[ ti ];
  90. }
  91. } else {
  92. var tr = ts[ 0 ], tg = ts[ 1 ], tb = ts[ 2 ];
  93. if ( depth == 8 ) for ( var i = 0; i < area; i ++ ) {
  94. var qi = i << 2, ti = i * 3; bf32[ i ] = ( 255 << 24 ) | ( data[ ti + 2 ] << 16 ) | ( data[ ti + 1 ] << 8 ) | data[ ti ];
  95. if ( data[ ti ] == tr && data[ ti + 1 ] == tg && data[ ti + 2 ] == tb ) bf[ qi + 3 ] = 0;
  96. }
  97. if ( depth == 16 ) for ( var i = 0; i < area; i ++ ) {
  98. var qi = i << 2, ti = i * 6; bf32[ i ] = ( 255 << 24 ) | ( data[ ti + 4 ] << 16 ) | ( data[ ti + 2 ] << 8 ) | data[ ti ];
  99. if ( rs( data, ti ) == tr && rs( data, ti + 2 ) == tg && rs( data, ti + 4 ) == tb ) bf[ qi + 3 ] = 0;
  100. }
  101. }
  102. } else if ( ctype == 3 ) { // palette
  103. var p = out.tabs[ 'PLTE' ], ap = out.tabs[ 'tRNS' ], tl = ap ? ap.length : 0;
  104. //console.log(p, ap);
  105. if ( depth == 1 ) for ( var y = 0; y < h; y ++ ) {
  106. var s0 = y * bpl, t0 = y * w;
  107. for ( var i = 0; i < w; i ++ ) {
  108. var qi = ( t0 + i ) << 2, j = ( ( data[ s0 + ( i >> 3 ) ] >> ( 7 - ( ( i & 7 ) << 0 ) ) ) & 1 ), cj = 3 * j; bf[ qi ] = p[ cj ]; bf[ qi + 1 ] = p[ cj + 1 ]; bf[ qi + 2 ] = p[ cj + 2 ]; bf[ qi + 3 ] = ( j < tl ) ? ap[ j ] : 255;
  109. }
  110. }
  111. if ( depth == 2 ) for ( var y = 0; y < h; y ++ ) {
  112. var s0 = y * bpl, t0 = y * w;
  113. for ( var i = 0; i < w; i ++ ) {
  114. var qi = ( t0 + i ) << 2, j = ( ( data[ s0 + ( i >> 2 ) ] >> ( 6 - ( ( i & 3 ) << 1 ) ) ) & 3 ), cj = 3 * j; bf[ qi ] = p[ cj ]; bf[ qi + 1 ] = p[ cj + 1 ]; bf[ qi + 2 ] = p[ cj + 2 ]; bf[ qi + 3 ] = ( j < tl ) ? ap[ j ] : 255;
  115. }
  116. }
  117. if ( depth == 4 ) for ( var y = 0; y < h; y ++ ) {
  118. var s0 = y * bpl, t0 = y * w;
  119. for ( var i = 0; i < w; i ++ ) {
  120. var qi = ( t0 + i ) << 2, j = ( ( data[ s0 + ( i >> 1 ) ] >> ( 4 - ( ( i & 1 ) << 2 ) ) ) & 15 ), cj = 3 * j; bf[ qi ] = p[ cj ]; bf[ qi + 1 ] = p[ cj + 1 ]; bf[ qi + 2 ] = p[ cj + 2 ]; bf[ qi + 3 ] = ( j < tl ) ? ap[ j ] : 255;
  121. }
  122. }
  123. if ( depth == 8 ) for ( var i = 0; i < area; i ++ ) {
  124. var qi = i << 2, j = data[ i ], cj = 3 * j; bf[ qi ] = p[ cj ]; bf[ qi + 1 ] = p[ cj + 1 ]; bf[ qi + 2 ] = p[ cj + 2 ]; bf[ qi + 3 ] = ( j < tl ) ? ap[ j ] : 255;
  125. }
  126. } else if ( ctype == 4 ) { // gray + alpha
  127. if ( depth == 8 ) for ( var i = 0; i < area; i ++ ) {
  128. var qi = i << 2, di = i << 1, gr = data[ di ]; bf[ qi ] = gr; bf[ qi + 1 ] = gr; bf[ qi + 2 ] = gr; bf[ qi + 3 ] = data[ di + 1 ];
  129. }
  130. if ( depth == 16 ) for ( var i = 0; i < area; i ++ ) {
  131. var qi = i << 2, di = i << 2, gr = data[ di ]; bf[ qi ] = gr; bf[ qi + 1 ] = gr; bf[ qi + 2 ] = gr; bf[ qi + 3 ] = data[ di + 2 ];
  132. }
  133. } else if ( ctype == 0 ) { // gray
  134. var tr = out.tabs[ 'tRNS' ] ? out.tabs[ 'tRNS' ] : - 1;
  135. for ( var y = 0; y < h; y ++ ) {
  136. var off = y * bpl, to = y * w;
  137. if ( depth == 1 ) for ( var x = 0; x < w; x ++ ) {
  138. var gr = 255 * ( ( data[ off + ( x >>> 3 ) ] >>> ( 7 - ( ( x & 7 ) ) ) ) & 1 ), al = ( gr == tr * 255 ) ? 0 : 255; bf32[ to + x ] = ( al << 24 ) | ( gr << 16 ) | ( gr << 8 ) | gr;
  139. }
  140. else if ( depth == 2 ) for ( var x = 0; x < w; x ++ ) {
  141. var gr = 85 * ( ( data[ off + ( x >>> 2 ) ] >>> ( 6 - ( ( x & 3 ) << 1 ) ) ) & 3 ), al = ( gr == tr * 85 ) ? 0 : 255; bf32[ to + x ] = ( al << 24 ) | ( gr << 16 ) | ( gr << 8 ) | gr;
  142. }
  143. else if ( depth == 4 ) for ( var x = 0; x < w; x ++ ) {
  144. var gr = 17 * ( ( data[ off + ( x >>> 1 ) ] >>> ( 4 - ( ( x & 1 ) << 2 ) ) ) & 15 ), al = ( gr == tr * 17 ) ? 0 : 255; bf32[ to + x ] = ( al << 24 ) | ( gr << 16 ) | ( gr << 8 ) | gr;
  145. }
  146. else if ( depth == 8 ) for ( var x = 0; x < w; x ++ ) {
  147. var gr = data[ off + x ], al = ( gr == tr ) ? 0 : 255; bf32[ to + x ] = ( al << 24 ) | ( gr << 16 ) | ( gr << 8 ) | gr;
  148. }
  149. else if ( depth == 16 ) for ( var x = 0; x < w; x ++ ) {
  150. var gr = data[ off + ( x << 1 ) ], al = ( rs( data, off + ( x << 1 ) ) == tr ) ? 0 : 255; bf32[ to + x ] = ( al << 24 ) | ( gr << 16 ) | ( gr << 8 ) | gr;
  151. }
  152. }
  153. }
  154. //console.log(Date.now()-time);
  155. return bf;
  156. };
  157. UPNG.decode = function ( buff ) {
  158. var data = new Uint8Array( buff ), offset = 8, bin = UPNG._bin, rUs = bin.readUshort, rUi = bin.readUint;
  159. var out = { tabs: {}, frames: [] };
  160. var dd = new Uint8Array( data.length ), doff = 0; // put all IDAT data into it
  161. var fd, foff = 0; // frames
  162. var mgck = [ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a ];
  163. for ( var i = 0; i < 8; i ++ ) if ( data[ i ] != mgck[ i ] ) throw 'The input is not a PNG file!';
  164. while ( offset < data.length ) {
  165. var len = bin.readUint( data, offset ); offset += 4;
  166. var type = bin.readASCII( data, offset, 4 ); offset += 4;
  167. //console.log(type,len);
  168. if ( type == 'IHDR' ) {
  169. UPNG.decode._IHDR( data, offset, out );
  170. } else if ( type == 'CgBI' ) {
  171. out.tabs[ type ] = data.slice( offset, offset + 4 );
  172. } else if ( type == 'IDAT' ) {
  173. for ( var i = 0; i < len; i ++ ) dd[ doff + i ] = data[ offset + i ];
  174. doff += len;
  175. } else if ( type == 'acTL' ) {
  176. out.tabs[ type ] = { num_frames: rUi( data, offset ), num_plays: rUi( data, offset + 4 ) };
  177. fd = new Uint8Array( data.length );
  178. } else if ( type == 'fcTL' ) {
  179. if ( foff != 0 ) {
  180. var fr = out.frames[ out.frames.length - 1 ];
  181. fr.data = UPNG.decode._decompress( out, fd.slice( 0, foff ), fr.rect.width, fr.rect.height ); foff = 0;
  182. }
  183. var rct = { x: rUi( data, offset + 12 ), y: rUi( data, offset + 16 ), width: rUi( data, offset + 4 ), height: rUi( data, offset + 8 ) };
  184. var del = rUs( data, offset + 22 ); del = rUs( data, offset + 20 ) / ( del == 0 ? 100 : del );
  185. var frm = { rect: rct, delay: Math.round( del * 1000 ), dispose: data[ offset + 24 ], blend: data[ offset + 25 ] };
  186. //console.log(frm);
  187. out.frames.push( frm );
  188. } else if ( type == 'fdAT' ) {
  189. for ( var i = 0; i < len - 4; i ++ ) fd[ foff + i ] = data[ offset + i + 4 ];
  190. foff += len - 4;
  191. } else if ( type == 'pHYs' ) {
  192. out.tabs[ type ] = [ bin.readUint( data, offset ), bin.readUint( data, offset + 4 ), data[ offset + 8 ] ];
  193. } else if ( type == 'cHRM' ) {
  194. out.tabs[ type ] = [];
  195. for ( var i = 0; i < 8; i ++ ) out.tabs[ type ].push( bin.readUint( data, offset + i * 4 ) );
  196. } else if ( type == 'tEXt' || type == 'zTXt' ) {
  197. if ( out.tabs[ type ] == null ) out.tabs[ type ] = {};
  198. var nz = bin.nextZero( data, offset );
  199. var keyw = bin.readASCII( data, offset, nz - offset );
  200. var text, tl = offset + len - nz - 1;
  201. if ( type == 'tEXt' ) text = bin.readASCII( data, nz + 1, tl );
  202. else {
  203. var bfr = UPNG.decode._inflate( data.slice( nz + 2, nz + 2 + tl ) );
  204. text = bin.readUTF8( bfr, 0, bfr.length );
  205. }
  206. out.tabs[ type ][ keyw ] = text;
  207. } else if ( type == 'iTXt' ) {
  208. if ( out.tabs[ type ] == null ) out.tabs[ type ] = {};
  209. var nz = 0, off = offset;
  210. nz = bin.nextZero( data, off );
  211. var keyw = bin.readASCII( data, off, nz - off ); off = nz + 1;
  212. var cflag = data[ off ]; off += 2;
  213. nz = bin.nextZero( data, off );
  214. bin.readASCII( data, off, nz - off ); off = nz + 1;
  215. nz = bin.nextZero( data, off );
  216. bin.readUTF8( data, off, nz - off ); off = nz + 1;
  217. var text, tl = len - ( off - offset );
  218. if ( cflag == 0 ) text = bin.readUTF8( data, off, tl );
  219. else {
  220. var bfr = UPNG.decode._inflate( data.slice( off, off + tl ) );
  221. text = bin.readUTF8( bfr, 0, bfr.length );
  222. }
  223. out.tabs[ type ][ keyw ] = text;
  224. } else if ( type == 'PLTE' ) {
  225. out.tabs[ type ] = bin.readBytes( data, offset, len );
  226. } else if ( type == 'hIST' ) {
  227. var pl = out.tabs[ 'PLTE' ].length / 3;
  228. out.tabs[ type ] = []; for ( var i = 0; i < pl; i ++ ) out.tabs[ type ].push( rUs( data, offset + i * 2 ) );
  229. } else if ( type == 'tRNS' ) {
  230. if ( out.ctype == 3 ) out.tabs[ type ] = bin.readBytes( data, offset, len );
  231. else if ( out.ctype == 0 ) out.tabs[ type ] = rUs( data, offset );
  232. else if ( out.ctype == 2 ) out.tabs[ type ] = [ rUs( data, offset ), rUs( data, offset + 2 ), rUs( data, offset + 4 ) ];
  233. //else console.log("tRNS for unsupported color type",out.ctype, len);
  234. } else if ( type == 'gAMA' ) out.tabs[ type ] = bin.readUint( data, offset ) / 100000;
  235. else if ( type == 'sRGB' ) out.tabs[ type ] = data[ offset ];
  236. else if ( type == 'bKGD' ) {
  237. if ( out.ctype == 0 || out.ctype == 4 ) out.tabs[ type ] = [ rUs( data, offset ) ];
  238. else if ( out.ctype == 2 || out.ctype == 6 ) out.tabs[ type ] = [ rUs( data, offset ), rUs( data, offset + 2 ), rUs( data, offset + 4 ) ];
  239. else if ( out.ctype == 3 ) out.tabs[ type ] = data[ offset ];
  240. } else if ( type == 'IEND' ) {
  241. break;
  242. }
  243. //else { console.log("unknown chunk type", type, len); out.tabs[type]=data.slice(offset,offset+len); }
  244. offset += len;
  245. bin.readUint( data, offset ); offset += 4;
  246. }
  247. if ( foff != 0 ) {
  248. var fr = out.frames[ out.frames.length - 1 ];
  249. fr.data = UPNG.decode._decompress( out, fd.slice( 0, foff ), fr.rect.width, fr.rect.height );
  250. }
  251. out.data = UPNG.decode._decompress( out, dd, out.width, out.height );
  252. delete out.compress; delete out.interlace; delete out.filter;
  253. return out;
  254. };
  255. UPNG.decode._decompress = function ( out, dd, w, h ) {
  256. var bpp = UPNG.decode._getBPP( out ), bpl = Math.ceil( w * bpp / 8 ), buff = new Uint8Array( ( bpl + 1 + out.interlace ) * h );
  257. if ( out.tabs[ 'CgBI' ] ) dd = UPNG.inflateRaw( dd, buff );
  258. else dd = UPNG.decode._inflate( dd, buff );
  259. if ( out.interlace == 0 ) dd = UPNG.decode._filterZero( dd, out, 0, w, h );
  260. else if ( out.interlace == 1 ) dd = UPNG.decode._readInterlace( dd, out );
  261. return dd;
  262. };
  263. UPNG.decode._inflate = function ( data, buff ) {
  264. var out = UPNG[ 'inflateRaw' ]( new Uint8Array( data.buffer, 2, data.length - 6 ), buff ); return out;
  265. };
  266. UPNG.inflateRaw = function () {
  267. var H = {}; H.H = {}; H.H.N = function ( N, W ) {
  268. var R = Uint8Array, i = 0, m = 0, J = 0, h = 0, Q = 0, X = 0, u = 0, w = 0, d = 0, v, C;
  269. if ( N[ 0 ] == 3 && N[ 1 ] == 0 ) return W ? W : new R( 0 ); var V = H.H, n = V.b, A = V.e, l = V.R, M = V.n, I = V.A, e = V.Z, b = V.m, Z = W == null;
  270. if ( Z )W = new R( N.length >>> 2 << 5 ); while ( i == 0 ) {
  271. i = n( N, d, 1 ); m = n( N, d + 1, 2 ); d += 3; if ( m == 0 ) {
  272. if ( ( d & 7 ) != 0 )d += 8 - ( d & 7 );
  273. var D = ( d >>> 3 ) + 4, q = N[ D - 4 ] | N[ D - 3 ] << 8; if ( Z )W = H.H.W( W, w + q ); W.set( new R( N.buffer, N.byteOffset + D, q ), w ); d = D + q << 3;
  274. w += q; continue
  275. ;
  276. }
  277. if ( Z )W = H.H.W( W, w + ( 1 << 17 ) ); if ( m == 1 ) {
  278. v = b.J; C = b.h; X = ( 1 << 9 ) - 1; u = ( 1 << 5 ) - 1;
  279. }
  280. if ( m == 2 ) {
  281. J = A( N, d, 5 ) + 257;
  282. h = A( N, d + 5, 5 ) + 1; Q = A( N, d + 10, 4 ) + 4; d += 14; var j = 1; for ( var c = 0; c < 38; c += 2 ) {
  283. b.Q[ c ] = 0; b.Q[ c + 1 ] = 0;
  284. }
  285. for ( var c = 0;
  286. c < Q; c ++ ) {
  287. var K = A( N, d + c * 3, 3 ); b.Q[ ( b.X[ c ] << 1 ) + 1 ] = K; if ( K > j )j = K
  288. ;
  289. }
  290. d += 3 * Q; M( b.Q, j ); I( b.Q, j, b.u ); v = b.w; C = b.d;
  291. d = l( b.u, ( 1 << j ) - 1, J + h, N, d, b.v ); var r = V.V( b.v, 0, J, b.C ); X = ( 1 << r ) - 1; var S = V.V( b.v, J, h, b.D ); u = ( 1 << S ) - 1; M( b.C, r );
  292. I( b.C, r, v ); M( b.D, S ); I( b.D, S, C )
  293. ;
  294. }
  295. while ( ! 0 ) {
  296. var T = v[ e( N, d ) & X ]; d += T & 15; var p = T >>> 4; if ( p >>> 8 == 0 ) {
  297. W[ w ++ ] = p;
  298. } else if ( p == 256 ) {
  299. break;
  300. } else {
  301. var z = w + p - 254;
  302. if ( p > 264 ) {
  303. var _ = b.q[ p - 257 ]; z = w + ( _ >>> 3 ) + A( N, d, _ & 7 ); d += _ & 7;
  304. }
  305. var $ = C[ e( N, d ) & u ]; d += $ & 15; var s = $ >>> 4, Y = b.c[ s ], a = ( Y >>> 4 ) + n( N, d, Y & 15 );
  306. d += Y & 15; while ( w < z ) {
  307. W[ w ] = W[ w ++ - a ]; W[ w ] = W[ w ++ - a ]; W[ w ] = W[ w ++ - a ]; W[ w ] = W[ w ++ - a ];
  308. }
  309. w = z
  310. ;
  311. }
  312. }
  313. }
  314. return W.length == w ? W : W.slice( 0, w )
  315. ;
  316. };
  317. H.H.W = function ( N, W ) {
  318. var R = N.length; if ( W <= R ) return N; var V = new Uint8Array( R << 1 ); V.set( N, 0 ); return V;
  319. };
  320. H.H.R = function ( N, W, R, V, n, A ) {
  321. var l = H.H.e, M = H.H.Z, I = 0; while ( I < R ) {
  322. var e = N[ M( V, n ) & W ]; n += e & 15; var b = e >>> 4;
  323. if ( b <= 15 ) {
  324. A[ I ] = b; I ++;
  325. } else {
  326. var Z = 0, m = 0; if ( b == 16 ) {
  327. m = 3 + l( V, n, 2 ); n += 2; Z = A[ I - 1 ];
  328. } else if ( b == 17 ) {
  329. m = 3 + l( V, n, 3 );
  330. n += 3
  331. ;
  332. } else if ( b == 18 ) {
  333. m = 11 + l( V, n, 7 ); n += 7;
  334. }
  335. var J = I + m; while ( I < J ) {
  336. A[ I ] = Z; I ++;
  337. }
  338. }
  339. }
  340. return n
  341. ;
  342. };
  343. H.H.V = function ( N, W, R, V ) {
  344. var n = 0, A = 0, l = V.length >>> 1;
  345. while ( A < R ) {
  346. var M = N[ A + W ]; V[ A << 1 ] = 0; V[ ( A << 1 ) + 1 ] = M; if ( M > n )n = M; A ++;
  347. }
  348. while ( A < l ) {
  349. V[ A << 1 ] = 0; V[ ( A << 1 ) + 1 ] = 0; A ++;
  350. }
  351. return n
  352. ;
  353. };
  354. H.H.n = function ( N, W ) {
  355. var R = H.H.m, V = N.length, n, A, l, M, I, e = R.j; for ( var M = 0; M <= W; M ++ )e[ M ] = 0; for ( M = 1; M < V; M += 2 )e[ N[ M ] ] ++;
  356. var b = R.K; n = 0; e[ 0 ] = 0; for ( A = 1; A <= W; A ++ ) {
  357. n = n + e[ A - 1 ] << 1; b[ A ] = n;
  358. }
  359. for ( l = 0; l < V; l += 2 ) {
  360. I = N[ l + 1 ]; if ( I != 0 ) {
  361. N[ l ] = b[ I ];
  362. b[ I ] ++
  363. ;
  364. }
  365. }
  366. };
  367. H.H.A = function ( N, W, R ) {
  368. var V = N.length, n = H.H.m, A = n.r; for ( var l = 0; l < V; l += 2 ) if ( N[ l + 1 ] != 0 ) {
  369. var M = l >> 1, I = N[ l + 1 ], e = M << 4 | I, b = W - I, Z = N[ l ] << b, m = Z + ( 1 << b );
  370. while ( Z != m ) {
  371. var J = A[ Z ] >>> 15 - W; R[ J ] = e; Z ++;
  372. }
  373. }
  374. };
  375. H.H.l = function ( N, W ) {
  376. var R = H.H.m.r, V = 15 - W; for ( var n = 0; n < N.length;
  377. n += 2 ) {
  378. var A = N[ n ] << W - N[ n + 1 ]; N[ n ] = R[ A ] >>> V;
  379. }
  380. };
  381. H.H.M = function ( N, W, R ) {
  382. R = R << ( W & 7 ); var V = W >>> 3; N[ V ] |= R; N[ V + 1 ] |= R >>> 8;
  383. };
  384. H.H.I = function ( N, W, R ) {
  385. R = R << ( W & 7 ); var V = W >>> 3; N[ V ] |= R; N[ V + 1 ] |= R >>> 8; N[ V + 2 ] |= R >>> 16;
  386. };
  387. H.H.e = function ( N, W, R ) {
  388. return ( N[ W >>> 3 ] | N[ ( W >>> 3 ) + 1 ] << 8 ) >>> ( W & 7 ) & ( 1 << R ) - 1;
  389. };
  390. H.H.b = function ( N, W, R ) {
  391. return ( N[ W >>> 3 ] | N[ ( W >>> 3 ) + 1 ] << 8 | N[ ( W >>> 3 ) + 2 ] << 16 ) >>> ( W & 7 ) & ( 1 << R ) - 1;
  392. };
  393. H.H.Z = function ( N, W ) {
  394. return ( N[ W >>> 3 ] | N[ ( W >>> 3 ) + 1 ] << 8 | N[ ( W >>> 3 ) + 2 ] << 16 ) >>> ( W & 7 );
  395. };
  396. H.H.i = function ( N, W ) {
  397. return ( N[ W >>> 3 ] | N[ ( W >>> 3 ) + 1 ] << 8 | N[ ( W >>> 3 ) + 2 ] << 16 | N[ ( W >>> 3 ) + 3 ] << 24 ) >>> ( W & 7 );
  398. };
  399. H.H.m = function () {
  400. var N = Uint16Array, W = Uint32Array;
  401. return { K: new N( 16 ), j: new N( 16 ), X: [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ], 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 ], 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 ], q: new N( 32 ), 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 ], 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 ], c: new W( 32 ), J: new N( 512 ), _: [], h: new N( 32 ), $: [], w: new N( 32768 ), C: [], v: [], d: new N( 32768 ), D: [], u: new N( 512 ), Q: [], r: new N( 1 << 15 ), s: new W( 286 ), Y: new W( 30 ), a: new W( 19 ), t: new W( 15e3 ), k: new N( 1 << 16 ), g: new N( 1 << 15 ) }
  402. ;
  403. }();
  404. ( function () {
  405. var N = H.H.m, W = 1 << 15; for ( var R = 0; R < W; R ++ ) {
  406. var V = R; V = ( V & 2863311530 ) >>> 1 | ( V & 1431655765 ) << 1;
  407. V = ( V & 3435973836 ) >>> 2 | ( V & 858993459 ) << 2; V = ( V & 4042322160 ) >>> 4 | ( V & 252645135 ) << 4; V = ( V & 4278255360 ) >>> 8 | ( V & 16711935 ) << 8;
  408. N.r[ R ] = ( V >>> 16 | V << 16 ) >>> 17
  409. ;
  410. }
  411. function n( A, l, M ) {
  412. while ( l -- != 0 )A.push( 0, M )
  413. ;
  414. }
  415. for ( var R = 0; R < 32; R ++ ) {
  416. N.q[ R ] = N.S[ R ] << 3 | N.T[ R ];
  417. N.c[ R ] = N.p[ R ] << 4 | N.z[ R ]
  418. ;
  419. }
  420. n( N._, 144, 8 ); n( N._, 255 - 143, 9 ); n( N._, 279 - 255, 7 ); n( N._, 287 - 279, 8 ); H.H.n( N._, 9 );
  421. H.H.A( N._, 9, N.J ); H.H.l( N._, 9 ); n( N.$, 32, 5 ); H.H.n( N.$, 5 ); H.H.A( N.$, 5, N.h ); H.H.l( N.$, 5 ); n( N.Q, 19, 0 ); n( N.C, 286, 0 );
  422. n( N.D, 30, 0 ); n( N.v, 320, 0 )
  423. ;
  424. }() );
  425. return H.H.N
  426. ;
  427. }();
  428. UPNG.decode._readInterlace = function ( data, out ) {
  429. var w = out.width, h = out.height;
  430. var bpp = UPNG.decode._getBPP( out ), cbpp = bpp >> 3, bpl = Math.ceil( w * bpp / 8 );
  431. var img = new Uint8Array( h * bpl );
  432. var di = 0;
  433. var starting_row = [ 0, 0, 4, 0, 2, 0, 1 ];
  434. var starting_col = [ 0, 4, 0, 2, 0, 1, 0 ];
  435. var row_increment = [ 8, 8, 8, 4, 4, 2, 2 ];
  436. var col_increment = [ 8, 8, 4, 4, 2, 2, 1 ];
  437. var pass = 0;
  438. while ( pass < 7 ) {
  439. var ri = row_increment[ pass ], ci = col_increment[ pass ];
  440. var sw = 0, sh = 0;
  441. var cr = starting_row[ pass ]; while ( cr < h ) {
  442. cr += ri; sh ++;
  443. }
  444. var cc = starting_col[ pass ]; while ( cc < w ) {
  445. cc += ci; sw ++;
  446. }
  447. var bpll = Math.ceil( sw * bpp / 8 );
  448. UPNG.decode._filterZero( data, out, di, sw, sh );
  449. var y = 0, row = starting_row[ pass ];
  450. while ( row < h ) {
  451. var col = starting_col[ pass ];
  452. var cdi = ( di + y * bpll ) << 3;
  453. while ( col < w ) {
  454. if ( bpp == 1 ) {
  455. var val = data[ cdi >> 3 ]; val = ( val >> ( 7 - ( cdi & 7 ) ) ) & 1;
  456. img[ row * bpl + ( col >> 3 ) ] |= ( val << ( 7 - ( ( col & 7 ) << 0 ) ) );
  457. }
  458. if ( bpp == 2 ) {
  459. var val = data[ cdi >> 3 ]; val = ( val >> ( 6 - ( cdi & 7 ) ) ) & 3;
  460. img[ row * bpl + ( col >> 2 ) ] |= ( val << ( 6 - ( ( col & 3 ) << 1 ) ) );
  461. }
  462. if ( bpp == 4 ) {
  463. var val = data[ cdi >> 3 ]; val = ( val >> ( 4 - ( cdi & 7 ) ) ) & 15;
  464. img[ row * bpl + ( col >> 1 ) ] |= ( val << ( 4 - ( ( col & 1 ) << 2 ) ) );
  465. }
  466. if ( bpp >= 8 ) {
  467. var ii = row * bpl + col * cbpp;
  468. for ( var j = 0; j < cbpp; j ++ ) img[ ii + j ] = data[ ( cdi >> 3 ) + j ];
  469. }
  470. cdi += bpp; col += ci;
  471. }
  472. y ++; row += ri;
  473. }
  474. if ( sw * sh != 0 ) di += sh * ( 1 + bpll );
  475. pass = pass + 1;
  476. }
  477. return img;
  478. };
  479. UPNG.decode._getBPP = function ( out ) {
  480. var noc = [ 1, null, 3, 1, 2, null, 4 ][ out.ctype ];
  481. return noc * out.depth;
  482. };
  483. UPNG.decode._filterZero = function ( data, out, off, w, h ) {
  484. var bpp = UPNG.decode._getBPP( out ), bpl = Math.ceil( w * bpp / 8 ), paeth = UPNG.decode._paeth;
  485. bpp = Math.ceil( bpp / 8 );
  486. var i, di, type = data[ off ], x = 0;
  487. if ( type > 1 ) data[ off ] = [ 0, 0, 1 ][ type - 2 ];
  488. if ( type == 3 ) for ( x = bpp; x < bpl; x ++ ) data[ x + 1 ] = ( data[ x + 1 ] + ( data[ x + 1 - bpp ] >>> 1 ) ) & 255;
  489. for ( var y = 0; y < h; y ++ ) {
  490. i = off + y * bpl; di = i + y + 1;
  491. type = data[ di - 1 ]; x = 0;
  492. if ( type == 0 ) for ( ; x < bpl; x ++ ) data[ i + x ] = data[ di + x ];
  493. else if ( type == 1 ) {
  494. for ( ; x < bpp; x ++ ) data[ i + x ] = data[ di + x ];
  495. for ( ; x < bpl; x ++ ) data[ i + x ] = ( data[ di + x ] + data[ i + x - bpp ] );
  496. } else if ( type == 2 ) {
  497. for ( ; x < bpl; x ++ ) data[ i + x ] = ( data[ di + x ] + data[ i + x - bpl ] );
  498. } else if ( type == 3 ) {
  499. for ( ; x < bpp; x ++ ) data[ i + x ] = ( data[ di + x ] + ( data[ i + x - bpl ] >>> 1 ) );
  500. for ( ; x < bpl; x ++ ) data[ i + x ] = ( data[ di + x ] + ( ( data[ i + x - bpl ] + data[ i + x - bpp ] ) >>> 1 ) );
  501. } else {
  502. for ( ; x < bpp; x ++ ) data[ i + x ] = ( data[ di + x ] + paeth( 0, data[ i + x - bpl ], 0 ) );
  503. for ( ; x < bpl; x ++ ) data[ i + x ] = ( data[ di + x ] + paeth( data[ i + x - bpp ], data[ i + x - bpl ], data[ i + x - bpp - bpl ] ) );
  504. }
  505. }
  506. return data;
  507. };
  508. UPNG.decode._paeth = function ( a, b, c ) {
  509. var p = a + b - c, pa = ( p - a ), pb = ( p - b ), pc = ( p - c );
  510. if ( pa * pa <= pb * pb && pa * pa <= pc * pc ) return a;
  511. else if ( pb * pb <= pc * pc ) return b;
  512. return c;
  513. };
  514. UPNG.decode._IHDR = function ( data, offset, out ) {
  515. var bin = UPNG._bin;
  516. out.width = bin.readUint( data, offset ); offset += 4;
  517. out.height = bin.readUint( data, offset ); offset += 4;
  518. out.depth = data[ offset ]; offset ++;
  519. out.ctype = data[ offset ]; offset ++;
  520. out.compress = data[ offset ]; offset ++;
  521. out.filter = data[ offset ]; offset ++;
  522. out.interlace = data[ offset ]; offset ++;
  523. };
  524. UPNG._bin = {
  525. nextZero: function ( data, p ) {
  526. while ( data[ p ] != 0 ) p ++; return p;
  527. },
  528. readUshort: function ( buff, p ) {
  529. return ( buff[ p ] << 8 ) | buff[ p + 1 ];
  530. },
  531. writeUshort: function ( buff, p, n ) {
  532. buff[ p ] = ( n >> 8 ) & 255; buff[ p + 1 ] = n & 255;
  533. },
  534. readUint: function ( buff, p ) {
  535. return ( buff[ p ] * ( 256 * 256 * 256 ) ) + ( ( buff[ p + 1 ] << 16 ) | ( buff[ p + 2 ] << 8 ) | buff[ p + 3 ] );
  536. },
  537. writeUint: function ( buff, p, n ) {
  538. buff[ p ] = ( n >> 24 ) & 255; buff[ p + 1 ] = ( n >> 16 ) & 255; buff[ p + 2 ] = ( n >> 8 ) & 255; buff[ p + 3 ] = n & 255;
  539. },
  540. readASCII: function ( buff, p, l ) {
  541. var s = ''; for ( var i = 0; i < l; i ++ ) s += String.fromCharCode( buff[ p + i ] ); return s;
  542. },
  543. writeASCII: function ( data, p, s ) {
  544. for ( var i = 0; i < s.length; i ++ ) data[ p + i ] = s.charCodeAt( i );
  545. },
  546. readBytes: function ( buff, p, l ) {
  547. var arr = []; for ( var i = 0; i < l; i ++ ) arr.push( buff[ p + i ] ); return arr;
  548. },
  549. pad: function ( n ) {
  550. return n.length < 2 ? '0' + n : n;
  551. },
  552. readUTF8: function ( buff, p, l ) {
  553. var s = '', ns;
  554. for ( var i = 0; i < l; i ++ ) s += '%' + UPNG._bin.pad( buff[ p + i ].toString( 16 ) );
  555. try {
  556. ns = decodeURIComponent( s );
  557. } catch ( e ) {
  558. return UPNG._bin.readASCII( buff, p, l );
  559. }
  560. return ns;
  561. }
  562. };
  563. UPNG._copyTile = function ( sb, sw, sh, tb, tw, th, xoff, yoff, mode ) {
  564. var w = Math.min( sw, tw ), h = Math.min( sh, th );
  565. var si = 0, ti = 0;
  566. for ( var y = 0; y < h; y ++ )
  567. for ( var x = 0; x < w; x ++ ) {
  568. if ( xoff >= 0 && yoff >= 0 ) {
  569. si = ( y * sw + x ) << 2; ti = ( ( yoff + y ) * tw + xoff + x ) << 2;
  570. } else {
  571. si = ( ( - yoff + y ) * sw - xoff + x ) << 2; ti = ( y * tw + x ) << 2;
  572. }
  573. if ( mode == 0 ) {
  574. tb[ ti ] = sb[ si ]; tb[ ti + 1 ] = sb[ si + 1 ]; tb[ ti + 2 ] = sb[ si + 2 ]; tb[ ti + 3 ] = sb[ si + 3 ];
  575. } else if ( mode == 1 ) {
  576. var fa = sb[ si + 3 ] * ( 1 / 255 ), fr = sb[ si ] * fa, fg = sb[ si + 1 ] * fa, fb = sb[ si + 2 ] * fa;
  577. var ba = tb[ ti + 3 ] * ( 1 / 255 ), br = tb[ ti ] * ba, bg = tb[ ti + 1 ] * ba, bb = tb[ ti + 2 ] * ba;
  578. var ifa = 1 - fa, oa = fa + ba * ifa, ioa = ( oa == 0 ? 0 : 1 / oa );
  579. tb[ ti + 3 ] = 255 * oa;
  580. tb[ ti + 0 ] = ( fr + br * ifa ) * ioa;
  581. tb[ ti + 1 ] = ( fg + bg * ifa ) * ioa;
  582. tb[ ti + 2 ] = ( fb + bb * ifa ) * ioa;
  583. } else if ( mode == 2 ) { // copy only differences, otherwise zero
  584. var fa = sb[ si + 3 ], fr = sb[ si ], fg = sb[ si + 1 ], fb = sb[ si + 2 ];
  585. var ba = tb[ ti + 3 ], br = tb[ ti ], bg = tb[ ti + 1 ], bb = tb[ ti + 2 ];
  586. if ( fa == ba && fr == br && fg == bg && fb == bb ) {
  587. tb[ ti ] = 0; tb[ ti + 1 ] = 0; tb[ ti + 2 ] = 0; tb[ ti + 3 ] = 0;
  588. } else {
  589. tb[ ti ] = fr; tb[ ti + 1 ] = fg; tb[ ti + 2 ] = fb; tb[ ti + 3 ] = fa;
  590. }
  591. } else if ( mode == 3 ) { // check if can be blended
  592. var fa = sb[ si + 3 ], fr = sb[ si ], fg = sb[ si + 1 ], fb = sb[ si + 2 ];
  593. var ba = tb[ ti + 3 ], br = tb[ ti ], bg = tb[ ti + 1 ], bb = tb[ ti + 2 ];
  594. if ( fa == ba && fr == br && fg == bg && fb == bb ) continue;
  595. //if(fa!=255 && ba!=0) return false;
  596. if ( fa < 220 && ba > 20 ) return false;
  597. }
  598. }
  599. return true;
  600. };
  601. export { RGBMLoader };