diff --git a/UTIF.js b/UTIF.js index 1bf3112..5e75d3b 100644 --- a/UTIF.js +++ b/UTIF.js @@ -60,6 +60,7 @@ UTIF.encodeImage = function(rgba, w, h, metadata) return data.buffer; } +// ToDo: BigTIFFF encode? UTIF.encode = function(ifds) { var LE = false; @@ -83,17 +84,37 @@ UTIF.encode = function(ifds) UTIF.decode = function(buff, prm) { if(prm==null) prm = {parseMN:true, debug:false}; // read MakerNote, debug - var data = new Uint8Array(buff), offset = 0; + var data = new Uint8Array(buff), offset = 0n; - var id = UTIF._binBE.readASCII(data, offset, 2); offset+=2; + var id = UTIF._binBE.readASCII(data, offset, 2n); offset+=2n; var bin = id=="II" ? UTIF._binLE : UTIF._binBE; - var num = bin.readUshort(data, offset); offset+=2; - - var ifdo = bin.readUint(data, offset); offset+=4; + var version_num = bin.readUshort(data, offset); offset+=2n; + console.log(version_num); + var isBigTIFF = (version_num==43n); + console.log(isBigTIFF); + + if (isBigTIFF) { + var always8 = bin.readUshort(data, offset); offset+=2n; + var always0 = bin.readUshort(data, offset); offset+=2n; + console.log('always8 always0 : ', always8, always0); + var ifdo = bin.readUlong(data, offset); offset+=8n; + console.log('ifdo : ', ifdo); + var ifds = []; + while(true) { + var noff = UTIF._readIFD_BigTIFF(bin, data, ifdo, ifds, 0, prm); + ifdo = bin.readUlong(data, noff); + console.log(ifdo); + if(ifdo==0 || noff==0) break; + } + return ifds; + } + var ifdo = BigInt(bin.readUint(data, offset)); offset+=4n; + console.log('ifdo : ',ifdo); var ifds = []; while(true) { var noff = UTIF._readIFD(bin, data, ifdo, ifds, 0, prm); - ifdo = bin.readUint(data, noff); + ifdo = BigInt(bin.readUint(data, noff)); + console.log(ifdo); if(ifdo==0 || noff==0) break; } return ifds; @@ -102,7 +123,7 @@ UTIF.decode = function(buff, prm) UTIF.decodeImage = function(buff, img, ifds) { var data = new Uint8Array(buff); - var id = UTIF._binBE.readASCII(data, 0, 2); + var id = UTIF._binBE.readASCII(data, 0n, 2n); if(img["t256"]==null) return; // No width => probably not an image img.isLE = id=="II"; @@ -820,8 +841,28 @@ UTIF.decode._writeBits = function(bits, tgt, boff) } UTIF.decode._decodeLZW = function(){var x={},y=function(L,F,i,W,_){for(var a=0;a<_;a+=4){i[W+a]=L[F+a]; -i[W+a+1]=L[F+a+1];i[W+a+2]=L[F+a+2];i[W+a+3]=L[F+a+3]}},c=function(L,F,i,W){if(!x.c){var _=new Uint32Array(65535),a=new Uint16Array(65535),Z=new Uint8Array(2e6); -for(var f=0;f<256;f++){Z[f<<2]=f;_[f]=f<<2;a[f]=1}x.c=[_,a,Z]}var o=x.c[0],z=x.c[1],Z=x.c[2],h=258,n=258<<2,k=9,C=F<<3,m=256,B=257,p=0,O=0,K=0; +i[W+a+1]=L[F+a+1];i[W+a+2]=L[F+a+2];i[W+a+3]=L[F+a+3]}},c=function(L,F,i,W){if(!x.c){ + var _=new Uint32Array(65535),a=new Uint16Array(65535),Z=new Uint8Array(2e6); +for(var f=0;f<256;f++){ + Z[f<<2]=f; + _[f]=f<<2; + a[f]=1 +} +x.c=[_,a,Z] +} +var o=x.c[0]; +var z=x.c[1]; +var Z=x.c[2]; +var h=258; +var n=258<<2; +var k=9; +// ?????? +var C=Number(F)<<3; +var m=256; +var B=257; +var p=0; +var O=0; +var K=0; while(!0){p=L[C>>>3]<<16|L[C+8>>>3]<<8|L[C+16>>>3];O=p>>24-(C&7)-k&(1<>>3]<<16|L[C+8>>>3]<<8|L[C+16>>>3];O=p>>24-(C&7)-k&(1<=h){o[h]=n;Z[o[h]]=J[0];z[h]=1;n=n+1+3&~3; @@ -848,35 +889,145 @@ UTIF._types = function() { } }(); +UTIF._readIFD_BigTIFF = function(bin, data, offset, ifds, depth, prm) +{ + var cnt = bin.readUlong(data, offset); offset+=8n; + var ifd = {}; + + if(prm.debug) log(" ".repeat(depth),ifds.length-1,">>>----------------"); + for(var i=0n; i>>----------------"); for(var i=0; i> 8)&255; buff[p+1] = n&255; }, - writeInt : function(buff, p, n) { var a=UTIF._binBE.ui8; UTIF._binBE.i32[0]=n; buff[p+3]=a[0]; buff[p+2]=a[1]; buff[p+1]=a[2]; buff[p+0]=a[3]; }, - writeUint : function(buff, p, n) { buff[p] = (n>>24)&255; buff[p+1] = (n>>16)&255; buff[p+2] = (n>>8)&255; buff[p+3] = (n>>0)&255; }, - writeASCII : function(buff, p, s) { for(var i = 0; i < s.length; i++) buff[p+i] = s.charCodeAt(i); }, + writeInt : function(buff, p, n) { var a=UTIF._binBE.ui8; UTIF._binBE.i32[0]=n; buff[p+3n]=a[0]; buff[p+2n]=a[1]; buff[p+1n]=a[2]; buff[p+0n]=a[3]; }, + writeUint : function(buff, p, n) { buff[p] = (n>>24)&255; buff[p+1n] = (n>>16)&255; buff[p+2n] = (n>>8)&255; buff[p+3n] = (n>>0)&255; }, + writeASCII : function(buff, p, s) { for(var i = 0n; i < s.length; i++) buff[p+i] = s.charCodeAt(i); }, writeDouble: function(buff, p, n) { UTIF._binBE.fl64[0] = n; - for (var i = 0; i < 8; i++) buff[p + i] = UTIF._binBE.ui8[7 - i]; + for (var i = 0n; i < 8n; i++) buff[p + i] = UTIF._binBE.ui8[7n - i]; } } UTIF._binBE.ui8 = new Uint8Array (8); UTIF._binBE.i16 = new Int16Array (UTIF._binBE.ui8.buffer); UTIF._binBE.i32 = new Int32Array (UTIF._binBE.ui8.buffer); UTIF._binBE.ui32 = new Uint32Array (UTIF._binBE.ui8.buffer); +UTIF._binBE.ui64 = new BigUint64Array (UTIF._binBE.ui8.buffer); UTIF._binBE.fl32 = new Float32Array(UTIF._binBE.ui8.buffer); UTIF._binBE.fl64 = new Float64Array(UTIF._binBE.ui8.buffer); UTIF._binLE = { nextZero : UTIF._binBE.nextZero, - readUshort : function(buff, p) { return (buff[p+1]<< 8) | buff[p]; }, - readShort : function(buff, p) { var a=UTIF._binBE.ui8; a[0]=buff[p+0]; a[1]=buff[p+1]; return UTIF._binBE. i16[0]; }, - readInt : function(buff, p) { var a=UTIF._binBE.ui8; a[0]=buff[p+0]; a[1]=buff[p+1]; a[2]=buff[p+2]; a[3]=buff[p+3]; return UTIF._binBE. i32[0]; }, - readUint : function(buff, p) { var a=UTIF._binBE.ui8; a[0]=buff[p+0]; a[1]=buff[p+1]; a[2]=buff[p+2]; a[3]=buff[p+3]; return UTIF._binBE.ui32[0]; }, + readUshort : function(buff, p) { return (buff[p+1n]<< 8) | buff[p]; }, + readShort : function(buff, p) { var a=UTIF._binBE.ui8; a[0]=buff[p+0n]; a[1]=buff[p+1n]; return UTIF._binBE. i16[0]; }, + readInt : function(buff, p) { var a=UTIF._binBE.ui8; a[0]=buff[p+0n]; a[1]=buff[p+1n]; a[2]=buff[p+2n]; a[3]=buff[p+3n]; return UTIF._binBE. i32[0]; }, + readUint : function(buff, p) { var a=UTIF._binBE.ui8; a[0]=buff[p+0n]; a[1]=buff[p+1n]; a[2]=buff[p+2n]; a[3]=buff[p+3n]; return UTIF._binBE.ui32[0]; }, + readUlong : function(buff, p) { + var a=UTIF._binBE.ui8; + a[0]=buff[p+0n]; + a[1]=buff[p+1n]; + a[2]=buff[p+2n]; + a[3]=buff[p+3n]; + a[4]=buff[p+4n]; + a[5]=buff[p+5n]; + a[6]=buff[p+6n]; + a[7]=buff[p+7n]; + // console.log(a); + return UTIF._binBE.ui64[0]; }, + readUchar : UTIF._binBE.readUchar, readASCII : UTIF._binBE.readASCII, - readFloat : function(buff, p) { var a=UTIF._binBE.ui8; for(var i=0;i<4;i++) a[i]=buff[p+ i]; return UTIF._binBE.fl32[0]; }, - readDouble : function(buff, p) { var a=UTIF._binBE.ui8; for(var i=0;i<8;i++) a[i]=buff[p+ i]; return UTIF._binBE.fl64[0]; }, + readFloat : function(buff, p) { var a=UTIF._binBE.ui8; for(var i=0n;i<4n;i++) a[i]=buff[p+ i]; return UTIF._binBE.fl32[0]; }, + readDouble : function(buff, p) { var a=UTIF._binBE.ui8; for(var i=0n;i<8n;i++) a[i]=buff[p+ i]; return UTIF._binBE.fl64[0]; }, - writeUshort: function(buff, p, n) { buff[p] = (n)&255; buff[p+1] = (n>>8)&255; }, - writeInt : function(buff, p, n) { var a=UTIF._binBE.ui8; UTIF._binBE.i32[0]=n; buff[p+0]=a[0]; buff[p+1]=a[1]; buff[p+2]=a[2]; buff[p+3]=a[3]; }, - writeUint : function(buff, p, n) { buff[p] = (n>>>0)&255; buff[p+1] = (n>>>8)&255; buff[p+2] = (n>>>16)&255; buff[p+3] = (n>>>24)&255; }, + writeUshort: function(buff, p, n) { buff[p] = (n)&255; buff[p+1n] = (n>>8)&255; }, + writeInt : function(buff, p, n) { var a=UTIF._binBE.ui8; UTIF._binBE.i32[0]=n; buff[p+0n]=a[0]; buff[p+1n]=a[1]; buff[p+2n]=a[2]; buff[p+3]=a[3]; }, + writeUint : function(buff, p, n) { buff[p] = (n>>>0)&255; buff[p+1n] = (n>>>8)&255; buff[p+2n] = (n>>>16)&255; buff[p+3n] = (n>>>24)&255; }, writeASCII : UTIF._binBE.writeASCII } UTIF._copyTile = function(tb, tw, th, b, w, h, xoff, yoff)