LogLuvLoader.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. ( function () {
  2. class LogLuvLoader extends THREE.DataTextureLoader {
  3. constructor( manager ) {
  4. super( manager );
  5. this.type = THREE.HalfFloatType;
  6. }
  7. parse( buffer ) {
  8. const ifds = UTIF.decode( buffer );
  9. UTIF.decodeImage( buffer, ifds[ 0 ] );
  10. const rgba = UTIF.toRGBA( ifds[ 0 ], this.type );
  11. return {
  12. width: ifds[ 0 ].width,
  13. height: ifds[ 0 ].height,
  14. data: rgba,
  15. format: THREE.RGBAFormat,
  16. type: this.type,
  17. flipY: true
  18. };
  19. }
  20. setDataType( value ) {
  21. this.type = value;
  22. return this;
  23. }
  24. } // from https://github.com/photopea/UTIF.js (MIT License)
  25. const UTIF = {};
  26. UTIF.decode = function ( buff, prm ) {
  27. if ( prm == null ) prm = {
  28. parseMN: true,
  29. debug: false
  30. }; // read MakerNote, debug
  31. var data = new Uint8Array( buff ),
  32. offset = 0;
  33. var id = UTIF._binBE.readASCII( data, offset, 2 );
  34. offset += 2;
  35. var bin = id == 'II' ? UTIF._binLE : UTIF._binBE;
  36. bin.readUshort( data, offset );
  37. offset += 2;
  38. var ifdo = bin.readUint( data, offset );
  39. offset += 4;
  40. var ifds = [];
  41. while ( true ) {
  42. var cnt = bin.readUshort( data, ifdo ),
  43. typ = bin.readUshort( data, ifdo + 4 );
  44. if ( cnt != 0 ) if ( typ < 1 || 13 < typ ) {
  45. console.log( 'error in TIFF' );
  46. break;
  47. }
  48. UTIF._readIFD( bin, data, ifdo, ifds, 0, prm );
  49. ifdo = bin.readUint( data, ifdo + 2 + cnt * 12 );
  50. if ( ifdo == 0 ) break;
  51. }
  52. return ifds;
  53. };
  54. UTIF.decodeImage = function ( buff, img, ifds ) {
  55. if ( img.data ) return;
  56. var data = new Uint8Array( buff );
  57. var id = UTIF._binBE.readASCII( data, 0, 2 );
  58. if ( img[ 't256' ] == null ) return; // No width => probably not an image
  59. img.isLE = id == 'II';
  60. img.width = img[ 't256' ][ 0 ]; //delete img["t256"];
  61. img.height = img[ 't257' ][ 0 ]; //delete img["t257"];
  62. var cmpr = img[ 't259' ] ? img[ 't259' ][ 0 ] : 1; //delete img["t259"];
  63. var fo = img[ 't266' ] ? img[ 't266' ][ 0 ] : 1; //delete img["t266"];
  64. if ( img[ 't284' ] && img[ 't284' ][ 0 ] == 2 ) console.log( 'PlanarConfiguration 2 should not be used!' );
  65. if ( cmpr == 7 && img[ 't258' ] && img[ 't258' ].length > 3 ) img[ 't258' ] = img[ 't258' ].slice( 0, 3 );
  66. var bipp; // bits per pixel
  67. if ( img[ 't258' ] ) bipp = Math.min( 32, img[ 't258' ][ 0 ] ) * img[ 't258' ].length; else bipp = img[ 't277' ] ? img[ 't277' ][ 0 ] : 1; // Some .NEF files have t258==14, even though they use 16 bits per pixel
  68. if ( cmpr == 1 && img[ 't279' ] != null && img[ 't278' ] && img[ 't262' ][ 0 ] == 32803 ) {
  69. bipp = Math.round( img[ 't279' ][ 0 ] * 8 / ( img.width * img[ 't278' ][ 0 ] ) );
  70. }
  71. var bipl = Math.ceil( img.width * bipp / 8 ) * 8;
  72. var soff = img[ 't273' ];
  73. if ( soff == null ) soff = img[ 't324' ];
  74. var bcnt = img[ 't279' ];
  75. if ( cmpr == 1 && soff.length == 1 ) bcnt = [ img.height * ( bipl >>> 3 ) ];
  76. if ( bcnt == null ) bcnt = img[ 't325' ]; //bcnt[0] = Math.min(bcnt[0], data.length); // Hasselblad, "RAW_HASSELBLAD_H3D39II.3FR"
  77. var bytes = new Uint8Array( img.height * ( bipl >>> 3 ) ),
  78. bilen = 0;
  79. if ( img[ 't322' ] != null ) {
  80. var tw = img[ 't322' ][ 0 ],
  81. th = img[ 't323' ][ 0 ];
  82. var tx = Math.floor( ( img.width + tw - 1 ) / tw );
  83. var ty = Math.floor( ( img.height + th - 1 ) / th );
  84. var tbuff = new Uint8Array( Math.ceil( tw * th * bipp / 8 ) | 0 );
  85. for ( var y = 0; y < ty; y ++ ) for ( var x = 0; x < tx; x ++ ) {
  86. var i = y * tx + x;
  87. for ( var j = 0; j < tbuff.length; j ++ ) tbuff[ j ] = 0;
  88. UTIF.decode._decompress( img, ifds, data, soff[ i ], bcnt[ i ], cmpr, tbuff, 0, fo ); // Might be required for 7 too. Need to check
  89. if ( cmpr == 6 ) bytes = tbuff; else UTIF._copyTile( tbuff, Math.ceil( tw * bipp / 8 ) | 0, th, bytes, Math.ceil( img.width * bipp / 8 ) | 0, img.height, Math.ceil( x * tw * bipp / 8 ) | 0, y * th );
  90. }
  91. bilen = bytes.length * 8;
  92. } else {
  93. var rps = img[ 't278' ] ? img[ 't278' ][ 0 ] : img.height;
  94. rps = Math.min( rps, img.height );
  95. for ( var i = 0; i < soff.length; i ++ ) {
  96. UTIF.decode._decompress( img, ifds, data, soff[ i ], bcnt[ i ], cmpr, bytes, Math.ceil( bilen / 8 ) | 0, fo );
  97. bilen += bipl * rps;
  98. }
  99. bilen = Math.min( bilen, bytes.length * 8 );
  100. }
  101. img.data = new Uint8Array( bytes.buffer, 0, Math.ceil( bilen / 8 ) | 0 );
  102. };
  103. UTIF.decode._decompress = function ( img, ifds, data, off, len, cmpr, tgt, toff ) {
  104. //console.log("compression", cmpr);
  105. //var time = Date.now();
  106. if ( cmpr == 34676 ) UTIF.decode._decodeLogLuv32( img, data, off, len, tgt, toff ); else console.log( 'Unsupported compression', cmpr ); //console.log(Date.now()-time);
  107. var bps = img[ 't258' ] ? Math.min( 32, img[ 't258' ][ 0 ] ) : 1;
  108. var noc = img[ 't277' ] ? img[ 't277' ][ 0 ] : 1,
  109. bpp = bps * noc >>> 3,
  110. h = img[ 't278' ] ? img[ 't278' ][ 0 ] : img.height,
  111. bpl = Math.ceil( bps * noc * img.width / 8 ); // convert to Little Endian /*
  112. if ( bps == 16 && ! img.isLE && img[ 't33422' ] == null ) // not DNG
  113. for ( var y = 0; y < h; y ++ ) {
  114. //console.log("fixing endianity");
  115. var roff = toff + y * bpl;
  116. for ( var x = 1; x < bpl; x += 2 ) {
  117. var t = tgt[ roff + x ];
  118. tgt[ roff + x ] = tgt[ roff + x - 1 ];
  119. tgt[ roff + x - 1 ] = t;
  120. }
  121. } //*/
  122. if ( img[ 't317' ] && img[ 't317' ][ 0 ] == 2 ) {
  123. for ( var y = 0; y < h; y ++ ) {
  124. var ntoff = toff + y * bpl;
  125. if ( bps == 16 ) for ( var j = bpp; j < bpl; j += 2 ) {
  126. var nv = ( tgt[ ntoff + j + 1 ] << 8 | tgt[ ntoff + j ] ) + ( tgt[ ntoff + j - bpp + 1 ] << 8 | tgt[ ntoff + j - bpp ] );
  127. tgt[ ntoff + j ] = nv & 255;
  128. tgt[ ntoff + j + 1 ] = nv >>> 8 & 255;
  129. } else if ( noc == 3 ) for ( var j = 3; j < bpl; j += 3 ) {
  130. tgt[ ntoff + j ] = tgt[ ntoff + j ] + tgt[ ntoff + j - 3 ] & 255;
  131. tgt[ ntoff + j + 1 ] = tgt[ ntoff + j + 1 ] + tgt[ ntoff + j - 2 ] & 255;
  132. tgt[ ntoff + j + 2 ] = tgt[ ntoff + j + 2 ] + tgt[ ntoff + j - 1 ] & 255;
  133. } else for ( var j = bpp; j < bpl; j ++ ) tgt[ ntoff + j ] = tgt[ ntoff + j ] + tgt[ ntoff + j - bpp ] & 255;
  134. }
  135. }
  136. };
  137. UTIF.decode._decodeLogLuv32 = function ( img, data, off, len, tgt, toff ) {
  138. var w = img.width,
  139. qw = w * 4;
  140. var io = 0,
  141. out = new Uint8Array( qw );
  142. while ( io < len ) {
  143. var oo = 0;
  144. while ( oo < qw ) {
  145. var c = data[ off + io ];
  146. io ++;
  147. if ( c < 128 ) {
  148. for ( var j = 0; j < c; j ++ ) out[ oo + j ] = data[ off + io + j ];
  149. oo += c;
  150. io += c;
  151. } else {
  152. c = c - 126;
  153. for ( var j = 0; j < c; j ++ ) out[ oo + j ] = data[ off + io ];
  154. oo += c;
  155. io ++;
  156. }
  157. }
  158. for ( var x = 0; x < w; x ++ ) {
  159. tgt[ toff + 0 ] = out[ x ];
  160. tgt[ toff + 1 ] = out[ x + w ];
  161. tgt[ toff + 2 ] = out[ x + w * 2 ];
  162. tgt[ toff + 4 ] = out[ x + w * 3 ];
  163. toff += 6;
  164. }
  165. }
  166. };
  167. UTIF.tags = {}; //UTIF.ttypes = { 256:3,257:3,258:3, 259:3, 262:3, 273:4, 274:3, 277:3,278:4,279:4, 282:5, 283:5, 284:3, 286:5,287:5, 296:3, 305:2, 306:2, 338:3, 513:4, 514:4, 34665:4 };
  168. // start at tag 250
  169. UTIF._types = function () {
  170. var main = new Array( 250 );
  171. main.fill( 0 );
  172. main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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] );
  173. var rest = {
  174. 33432: 2,
  175. 33434: 5,
  176. 33437: 5,
  177. 34665: 4,
  178. 34850: 3,
  179. 34853: 4,
  180. 34855: 3,
  181. 34864: 3,
  182. 34866: 4,
  183. 36864: 7,
  184. 36867: 2,
  185. 36868: 2,
  186. 37121: 7,
  187. 37377: 10,
  188. 37378: 5,
  189. 37380: 10,
  190. 37381: 5,
  191. 37383: 3,
  192. 37384: 3,
  193. 37385: 3,
  194. 37386: 5,
  195. 37510: 7,
  196. 37520: 2,
  197. 37521: 2,
  198. 37522: 2,
  199. 40960: 7,
  200. 40961: 3,
  201. 40962: 4,
  202. 40963: 4,
  203. 40965: 4,
  204. 41486: 5,
  205. 41487: 5,
  206. 41488: 3,
  207. 41985: 3,
  208. 41986: 3,
  209. 41987: 3,
  210. 41988: 5,
  211. 41989: 3,
  212. 41990: 3,
  213. 41993: 3,
  214. 41994: 3,
  215. 41995: 7,
  216. 41996: 3,
  217. 42032: 2,
  218. 42033: 2,
  219. 42034: 5,
  220. 42036: 2,
  221. 42037: 2,
  222. 59932: 7
  223. };
  224. return {
  225. basic: {
  226. main: main,
  227. rest: rest
  228. },
  229. gps: {
  230. main: [ 1, 2, 5, 2, 5, 1, 5, 5, 0, 9 ],
  231. rest: {
  232. 18: 2,
  233. 29: 2
  234. }
  235. }
  236. };
  237. }();
  238. UTIF._readIFD = function ( bin, data, offset, ifds, depth, prm ) {
  239. var cnt = bin.readUshort( data, offset );
  240. offset += 2;
  241. var ifd = {};
  242. if ( prm.debug ) console.log( ' '.repeat( depth ), ifds.length - 1, '>>>----------------' );
  243. for ( var i = 0; i < cnt; i ++ ) {
  244. var tag = bin.readUshort( data, offset );
  245. offset += 2;
  246. var type = bin.readUshort( data, offset );
  247. offset += 2;
  248. var num = bin.readUint( data, offset );
  249. offset += 4;
  250. var voff = bin.readUint( data, offset );
  251. offset += 4;
  252. var arr = []; //ifd["t"+tag+"-"+UTIF.tags[tag]] = arr;
  253. if ( type == 1 || type == 7 ) {
  254. arr = new Uint8Array( data.buffer, num < 5 ? offset - 4 : voff, num );
  255. }
  256. if ( type == 2 ) {
  257. var o0 = num < 5 ? offset - 4 : voff,
  258. c = data[ o0 ],
  259. len = Math.max( 0, Math.min( num - 1, data.length - o0 ) );
  260. if ( c < 128 || len == 0 ) arr.push( bin.readASCII( data, o0, len ) ); else arr = new Uint8Array( data.buffer, o0, len );
  261. }
  262. if ( type == 3 ) {
  263. for ( var j = 0; j < num; j ++ ) arr.push( bin.readUshort( data, ( num < 3 ? offset - 4 : voff ) + 2 * j ) );
  264. }
  265. if ( type == 4 || type == 13 ) {
  266. for ( var j = 0; j < num; j ++ ) arr.push( bin.readUint( data, ( num < 2 ? offset - 4 : voff ) + 4 * j ) );
  267. }
  268. if ( type == 5 || type == 10 ) {
  269. var ri = type == 5 ? bin.readUint : bin.readInt;
  270. for ( var j = 0; j < num; j ++ ) arr.push( [ ri( data, voff + j * 8 ), ri( data, voff + j * 8 + 4 ) ] );
  271. }
  272. if ( type == 8 ) {
  273. for ( var j = 0; j < num; j ++ ) arr.push( bin.readShort( data, ( num < 3 ? offset - 4 : voff ) + 2 * j ) );
  274. }
  275. if ( type == 9 ) {
  276. for ( var j = 0; j < num; j ++ ) arr.push( bin.readInt( data, ( num < 2 ? offset - 4 : voff ) + 4 * j ) );
  277. }
  278. if ( type == 11 ) {
  279. for ( var j = 0; j < num; j ++ ) arr.push( bin.readFloat( data, voff + j * 4 ) );
  280. }
  281. if ( type == 12 ) {
  282. for ( var j = 0; j < num; j ++ ) arr.push( bin.readDouble( data, voff + j * 8 ) );
  283. }
  284. if ( num != 0 && arr.length == 0 ) {
  285. console.log( tag, 'unknown TIFF tag type: ', type, 'num:', num );
  286. if ( i == 0 ) return;
  287. continue;
  288. }
  289. if ( prm.debug ) console.log( ' '.repeat( depth ), tag, type, UTIF.tags[ tag ], arr );
  290. ifd[ 't' + tag ] = arr;
  291. if ( tag == 330 && ifd[ 't272' ] && ifd[ 't272' ][ 0 ] == 'DSLR-A100' ) {} else if ( tag == 330 || tag == 34665 || tag == 34853 || tag == 50740 && bin.readUshort( data, bin.readUint( arr, 0 ) ) < 300 || tag == 61440 ) {
  292. var oarr = tag == 50740 ? [ bin.readUint( arr, 0 ) ] : arr;
  293. var subfd = [];
  294. for ( var j = 0; j < oarr.length; j ++ ) UTIF._readIFD( bin, data, oarr[ j ], subfd, depth + 1, prm );
  295. if ( tag == 330 ) ifd.subIFD = subfd;
  296. if ( tag == 34665 ) ifd.exifIFD = subfd[ 0 ];
  297. if ( tag == 34853 ) ifd.gpsiIFD = subfd[ 0 ]; //console.log("gps", subfd[0]); }
  298. if ( tag == 50740 ) ifd.dngPrvt = subfd[ 0 ];
  299. if ( tag == 61440 ) ifd.fujiIFD = subfd[ 0 ];
  300. }
  301. if ( tag == 37500 && prm.parseMN ) {
  302. var mn = arr; //console.log(bin.readASCII(mn,0,mn.length), mn);
  303. if ( bin.readASCII( mn, 0, 5 ) == 'Nikon' ) ifd.makerNote = UTIF[ 'decode' ]( mn.slice( 10 ).buffer )[ 0 ]; else if ( bin.readUshort( data, voff ) < 300 && bin.readUshort( data, voff + 4 ) <= 12 ) {
  304. var subsub = [];
  305. UTIF._readIFD( bin, data, voff, subsub, depth + 1, prm );
  306. ifd.makerNote = subsub[ 0 ];
  307. }
  308. }
  309. }
  310. ifds.push( ifd );
  311. if ( prm.debug ) console.log( ' '.repeat( depth ), '<<<---------------' );
  312. return offset;
  313. };
  314. UTIF.toRGBA = function ( out, type ) {
  315. const w = out.width,
  316. h = out.height,
  317. area = w * h,
  318. data = out.data;
  319. let img;
  320. switch ( type ) {
  321. case THREE.HalfFloatType:
  322. img = new Uint16Array( area * 4 );
  323. break;
  324. case THREE.FloatType:
  325. img = new Float32Array( area * 4 );
  326. break;
  327. default:
  328. console.error( 'THREE.LogLuvLoader: Unsupported texture data type:', type );
  329. }
  330. let intp = out[ 't262' ] ? out[ 't262' ][ 0 ] : 2;
  331. const bps = out[ 't258' ] ? Math.min( 32, out[ 't258' ][ 0 ] ) : 1;
  332. if ( out[ 't262' ] == null && bps == 1 ) intp = 0;
  333. if ( intp == 32845 ) {
  334. for ( let y = 0; y < h; y ++ ) {
  335. for ( let x = 0; x < w; x ++ ) {
  336. const si = ( y * w + x ) * 6,
  337. qi = ( y * w + x ) * 4;
  338. let L = data[ si + 1 ] << 8 | data[ si ];
  339. L = Math.pow( 2, ( L + 0.5 ) / 256 - 64 );
  340. const u = ( data[ si + 3 ] + 0.5 ) / 410;
  341. const v = ( data[ si + 5 ] + 0.5 ) / 410; // Luv to xyY
  342. const sX = 9 * u / ( 6 * u - 16 * v + 12 );
  343. const sY = 4 * v / ( 6 * u - 16 * v + 12 );
  344. const bY = L; // xyY to XYZ
  345. const X = sX * bY / sY,
  346. Y = bY,
  347. Z = ( 1 - sX - sY ) * bY / sY; // XYZ to linear RGB
  348. const r = 2.690 * X - 1.276 * Y - 0.414 * Z;
  349. const g = - 1.022 * X + 1.978 * Y + 0.044 * Z;
  350. const b = 0.061 * X - 0.224 * Y + 1.163 * Z;
  351. if ( type === THREE.HalfFloatType ) {
  352. img[ qi ] = THREE.DataUtils.toHalfFloat( Math.min( r, 65504 ) );
  353. img[ qi + 1 ] = THREE.DataUtils.toHalfFloat( Math.min( g, 65504 ) );
  354. img[ qi + 2 ] = THREE.DataUtils.toHalfFloat( Math.min( b, 65504 ) );
  355. img[ qi + 3 ] = THREE.DataUtils.toHalfFloat( 1 );
  356. } else {
  357. img[ qi ] = r;
  358. img[ qi + 1 ] = g;
  359. img[ qi + 2 ] = b;
  360. img[ qi + 3 ] = 1;
  361. }
  362. }
  363. }
  364. } else {
  365. console.log( 'Unsupported Photometric interpretation: ' + intp );
  366. }
  367. return img;
  368. };
  369. UTIF._binBE = {
  370. nextZero: function ( data, o ) {
  371. while ( data[ o ] != 0 ) o ++;
  372. return o;
  373. },
  374. readUshort: function ( buff, p ) {
  375. return buff[ p ] << 8 | buff[ p + 1 ];
  376. },
  377. readShort: function ( buff, p ) {
  378. var a = UTIF._binBE.ui8;
  379. a[ 0 ] = buff[ p + 1 ];
  380. a[ 1 ] = buff[ p + 0 ];
  381. return UTIF._binBE.i16[ 0 ];
  382. },
  383. readInt: function ( buff, p ) {
  384. var a = UTIF._binBE.ui8;
  385. a[ 0 ] = buff[ p + 3 ];
  386. a[ 1 ] = buff[ p + 2 ];
  387. a[ 2 ] = buff[ p + 1 ];
  388. a[ 3 ] = buff[ p + 0 ];
  389. return UTIF._binBE.i32[ 0 ];
  390. },
  391. readUint: function ( buff, p ) {
  392. var a = UTIF._binBE.ui8;
  393. a[ 0 ] = buff[ p + 3 ];
  394. a[ 1 ] = buff[ p + 2 ];
  395. a[ 2 ] = buff[ p + 1 ];
  396. a[ 3 ] = buff[ p + 0 ];
  397. return UTIF._binBE.ui32[ 0 ];
  398. },
  399. readASCII: function ( buff, p, l ) {
  400. var s = '';
  401. for ( var i = 0; i < l; i ++ ) s += String.fromCharCode( buff[ p + i ] );
  402. return s;
  403. },
  404. readFloat: function ( buff, p ) {
  405. var a = UTIF._binBE.ui8;
  406. for ( var i = 0; i < 4; i ++ ) a[ i ] = buff[ p + 3 - i ];
  407. return UTIF._binBE.fl32[ 0 ];
  408. },
  409. readDouble: function ( buff, p ) {
  410. var a = UTIF._binBE.ui8;
  411. for ( var i = 0; i < 8; i ++ ) a[ i ] = buff[ p + 7 - i ];
  412. return UTIF._binBE.fl64[ 0 ];
  413. },
  414. writeUshort: function ( buff, p, n ) {
  415. buff[ p ] = n >> 8 & 255;
  416. buff[ p + 1 ] = n & 255;
  417. },
  418. writeInt: function ( buff, p, n ) {
  419. var a = UTIF._binBE.ui8;
  420. UTIF._binBE.i32[ 0 ] = n;
  421. buff[ p + 3 ] = a[ 0 ];
  422. buff[ p + 2 ] = a[ 1 ];
  423. buff[ p + 1 ] = a[ 2 ];
  424. buff[ p + 0 ] = a[ 3 ];
  425. },
  426. writeUint: function ( buff, p, n ) {
  427. buff[ p ] = n >> 24 & 255;
  428. buff[ p + 1 ] = n >> 16 & 255;
  429. buff[ p + 2 ] = n >> 8 & 255;
  430. buff[ p + 3 ] = n >> 0 & 255;
  431. },
  432. writeASCII: function ( buff, p, s ) {
  433. for ( var i = 0; i < s.length; i ++ ) buff[ p + i ] = s.charCodeAt( i );
  434. },
  435. writeDouble: function ( buff, p, n ) {
  436. UTIF._binBE.fl64[ 0 ] = n;
  437. for ( var i = 0; i < 8; i ++ ) buff[ p + i ] = UTIF._binBE.ui8[ 7 - i ];
  438. }
  439. };
  440. UTIF._binBE.ui8 = new Uint8Array( 8 );
  441. UTIF._binBE.i16 = new Int16Array( UTIF._binBE.ui8.buffer );
  442. UTIF._binBE.i32 = new Int32Array( UTIF._binBE.ui8.buffer );
  443. UTIF._binBE.ui32 = new Uint32Array( UTIF._binBE.ui8.buffer );
  444. UTIF._binBE.fl32 = new Float32Array( UTIF._binBE.ui8.buffer );
  445. UTIF._binBE.fl64 = new Float64Array( UTIF._binBE.ui8.buffer );
  446. UTIF._binLE = {
  447. nextZero: UTIF._binBE.nextZero,
  448. readUshort: function ( buff, p ) {
  449. return buff[ p + 1 ] << 8 | buff[ p ];
  450. },
  451. readShort: function ( buff, p ) {
  452. var a = UTIF._binBE.ui8;
  453. a[ 0 ] = buff[ p + 0 ];
  454. a[ 1 ] = buff[ p + 1 ];
  455. return UTIF._binBE.i16[ 0 ];
  456. },
  457. readInt: function ( buff, p ) {
  458. var a = UTIF._binBE.ui8;
  459. a[ 0 ] = buff[ p + 0 ];
  460. a[ 1 ] = buff[ p + 1 ];
  461. a[ 2 ] = buff[ p + 2 ];
  462. a[ 3 ] = buff[ p + 3 ];
  463. return UTIF._binBE.i32[ 0 ];
  464. },
  465. readUint: function ( buff, p ) {
  466. var a = UTIF._binBE.ui8;
  467. a[ 0 ] = buff[ p + 0 ];
  468. a[ 1 ] = buff[ p + 1 ];
  469. a[ 2 ] = buff[ p + 2 ];
  470. a[ 3 ] = buff[ p + 3 ];
  471. return UTIF._binBE.ui32[ 0 ];
  472. },
  473. readASCII: UTIF._binBE.readASCII,
  474. readFloat: function ( buff, p ) {
  475. var a = UTIF._binBE.ui8;
  476. for ( var i = 0; i < 4; i ++ ) a[ i ] = buff[ p + i ];
  477. return UTIF._binBE.fl32[ 0 ];
  478. },
  479. readDouble: function ( buff, p ) {
  480. var a = UTIF._binBE.ui8;
  481. for ( var i = 0; i < 8; i ++ ) a[ i ] = buff[ p + i ];
  482. return UTIF._binBE.fl64[ 0 ];
  483. },
  484. writeUshort: function ( buff, p, n ) {
  485. buff[ p ] = n & 255;
  486. buff[ p + 1 ] = n >> 8 & 255;
  487. },
  488. writeInt: function ( buff, p, n ) {
  489. var a = UTIF._binBE.ui8;
  490. UTIF._binBE.i32[ 0 ] = n;
  491. buff[ p + 0 ] = a[ 0 ];
  492. buff[ p + 1 ] = a[ 1 ];
  493. buff[ p + 2 ] = a[ 2 ];
  494. buff[ p + 3 ] = a[ 3 ];
  495. },
  496. writeUint: function ( buff, p, n ) {
  497. buff[ p ] = n >>> 0 & 255;
  498. buff[ p + 1 ] = n >>> 8 & 255;
  499. buff[ p + 2 ] = n >>> 16 & 255;
  500. buff[ p + 3 ] = n >>> 24 & 255;
  501. },
  502. writeASCII: UTIF._binBE.writeASCII
  503. };
  504. UTIF._copyTile = function ( tb, tw, th, b, w, h, xoff, yoff ) {
  505. //log("copyTile", tw, th, w, h, xoff, yoff);
  506. var xlim = Math.min( tw, w - xoff );
  507. var ylim = Math.min( th, h - yoff );
  508. for ( var y = 0; y < ylim; y ++ ) {
  509. var tof = ( yoff + y ) * w + xoff;
  510. var sof = y * tw;
  511. for ( var x = 0; x < xlim; x ++ ) b[ tof + x ] = tb[ sof + x ];
  512. }
  513. };
  514. THREE.LogLuvLoader = LogLuvLoader;
  515. } )();