RGBMLoader.js 28 KB

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