dwv
Version:
DICOM Web Viewer.
2 lines • 17.2 kB
JavaScript
var t,s=s||{};s.lossless=s.lossless||{},s.lossless.ComponentSpec=s.lossless.ComponentSpec||function(){this.hSamp=0,this.quantTableSel=0,this.vSamp=0},s.lossless.DataStream=s.lossless.DataStream||function(t,s,e){this.buffer=new Uint8Array(t,s,e),this.index=0},s.lossless.DataStream.prototype.get16=function(){var t=(this.buffer[this.index]<<8)+this.buffer[this.index+1];return this.index+=2,t},s.lossless.DataStream.prototype.get8=function(){var t=this.buffer[this.index];return this.index+=1,t},s.lossless.Decoder=s.lossless.Decoder||function(t,e){this.buffer=t,this.frame=new s.lossless.FrameHeader,this.huffTable=new s.lossless.HuffmanTable,this.quantTable=new s.lossless.QuantizationTable,this.scan=new s.lossless.ScanHeader,this.DU=s.lossless.Utils.createArray(10,4,64),this.HuffTab=s.lossless.Utils.createArray(4,2,12800),this.IDCT_Source=[],this.nBlock=[],this.acTab=s.lossless.Utils.createArray(10,1),this.dcTab=s.lossless.Utils.createArray(10,1),this.qTab=s.lossless.Utils.createArray(10,1),this.marker=0,this.markerIndex=0,this.numComp=0,this.restartInterval=0,this.selection=0,this.xDim=0,this.yDim=0,this.xLoc=0,this.yLoc=0,this.numBytes=0,this.outputData=null,this.restarting=!1,this.mask=0,void 0!==e&&(this.numBytes=e)},s.lossless.Decoder.IDCT_P=[0,5,40,16,45,2,7,42,21,56,8,61,18,47,1,4,41,23,58,13,32,24,37,10,63,17,44,3,6,43,20,57,15,34,29,48,53,26,39,9,60,19,46,22,59,12,33,31,50,55,25,36,11,62,14,35,28,49,52,27,38,30,51,54],s.lossless.Decoder.TABLE=[0,1,5,6,14,15,27,28,2,4,7,13,16,26,29,42,3,8,12,17,25,30,41,43,9,11,18,24,31,40,44,53,10,19,23,32,39,45,52,54,20,22,33,38,46,51,55,60,21,34,37,47,50,56,59,61,35,36,48,49,57,58,62,63],s.lossless.Decoder.MAX_HUFFMAN_SUBTREE=50,s.lossless.Decoder.MSB=2147483648,s.lossless.Decoder.RESTART_MARKER_BEGIN=65488,s.lossless.Decoder.RESTART_MARKER_END=65495,s.lossless.Decoder.prototype.decompress=function(t,s,e){return this.decode(t,s,e).buffer},s.lossless.Decoder.prototype.decode=function(t,e,r,i){var o,a,n,h,l=0,c=[],u=[],f=[];if(void 0!==t&&(this.buffer=t),void 0!==i&&(this.numBytes=i),this.stream=new s.lossless.DataStream(this.buffer,e,r),this.buffer=null,this.xLoc=0,this.yLoc=0,65496!==(o=this.stream.get16()))throw new Error("Not a JPEG file");for(o=this.stream.get16();o>>4!=4092||65476===o;){switch(o){case 65476:this.huffTable.read(this.stream,this.HuffTab);break;case 65484:throw new Error("Program doesn't support arithmetic coding. (format throw new IOException)");case 65499:this.quantTable.read(this.stream,s.lossless.Decoder.TABLE);break;case 65501:this.restartInterval=this.readNumber();break;case 65504:case 65505:case 65506:case 65507:case 65508:case 65509:case 65510:case 65511:case 65512:case 65513:case 65514:case 65515:case 65516:case 65517:case 65518:case 65519:this.readApp();break;case 65534:this.readComment();break;default:if(o>>8!=255)throw new Error("ERROR: format throw new IOException! (decode)")}o=this.stream.get16()}if(o<65472||o>65479)throw new Error("ERROR: could not handle arithmetic code!");this.frame.read(this.stream),o=this.stream.get16();do{for(;65498!==o;){switch(o){case 65476:this.huffTable.read(this.stream,this.HuffTab);break;case 65484:throw new Error("Program doesn't support arithmetic coding. (format throw new IOException)");case 65499:this.quantTable.read(this.stream,s.lossless.Decoder.TABLE);break;case 65501:this.restartInterval=this.readNumber();break;case 65504:case 65505:case 65506:case 65507:case 65508:case 65509:case 65510:case 65511:case 65512:case 65513:case 65514:case 65515:case 65516:case 65517:case 65518:case 65519:this.readApp();break;case 65534:this.readComment();break;default:if(o>>8!=255)throw new Error("ERROR: format throw new IOException! (Parser.decode)")}o=this.stream.get16()}switch(this.precision=this.frame.precision,this.components=this.frame.components,this.numBytes||(this.numBytes=parseInt(Math.ceil(this.precision/8))),1==this.numBytes?this.mask=255:this.mask=65535,this.scan.read(this.stream),this.numComp=this.scan.numComp,this.selection=this.scan.selection,1===this.numBytes?3===this.numComp?(this.getter=this.getValueRGB,this.setter=this.setValueRGB,this.output=this.outputRGB):(this.getter=this.getValue8,this.setter=this.setValue8,this.output=this.outputSingle):(this.getter=this.getValue16,this.setter=this.setValue16,this.output=this.outputSingle),this.selection){case 2:this.selector=this.select2;break;case 3:this.selector=this.select3;break;case 4:this.selector=this.select4;break;case 5:this.selector=this.select5;break;case 6:this.selector=this.select6;break;case 7:this.selector=this.select7;break;default:this.selector=this.select1}for(this.scanComps=this.scan.components,this.quantTables=this.quantTable.quantTables,a=0;a<this.numComp;a+=1)n=this.scanComps[a].scanCompSel,this.qTab[a]=this.quantTables[this.components[n].quantTableSel],this.nBlock[a]=this.components[n].vSamp*this.components[n].hSamp,this.dcTab[a]=this.HuffTab[this.scanComps[a].dcTabSel][0],this.acTab[a]=this.HuffTab[this.scanComps[a].acTabSel][1];for(this.xDim=this.frame.dimX,this.yDim=this.frame.dimY,1==this.numBytes?this.outputData=new Uint8Array(new ArrayBuffer(this.xDim*this.yDim*this.numBytes*this.numComp)):this.outputData=new Uint16Array(new ArrayBuffer(this.xDim*this.yDim*this.numBytes*this.numComp)),l+=1;;){for(u[0]=0,f[0]=0,a=0;a<10;a+=1)c[a]=1<<this.precision-1;if(0===this.restartInterval){for(o=this.decodeUnit(c,u,f);0===o&&this.xLoc<this.xDim&&this.yLoc<this.yDim;)this.output(c),o=this.decodeUnit(c,u,f);break}for(h=0;h<this.restartInterval&&(this.restarting=0==h,o=this.decodeUnit(c,u,f),this.output(c),0===o);h+=1);if(0===o&&(0!==this.markerIndex?(o=65280|this.marker,this.markerIndex=0):o=this.stream.get16()),!(o>=s.lossless.Decoder.RESTART_MARKER_BEGIN&&o<=s.lossless.Decoder.RESTART_MARKER_END))break}65500===o&&1===l&&(this.readNumber(),o=this.stream.get16())}while(65497!==o&&this.xLoc<this.xDim&&this.yLoc<this.yDim&&0===l);return this.outputData},s.lossless.Decoder.prototype.decodeUnit=function(t,s,e){return 1==this.numComp?this.decodeSingle(t,s,e):3==this.numComp?this.decodeRGB(t,s,e):-1},s.lossless.Decoder.prototype.select1=function(t){return this.getPreviousX(t)},s.lossless.Decoder.prototype.select2=function(t){return this.getPreviousY(t)},s.lossless.Decoder.prototype.select3=function(t){return this.getPreviousXY(t)},s.lossless.Decoder.prototype.select4=function(t){return this.getPreviousX(t)+this.getPreviousY(t)-this.getPreviousXY(t)},s.lossless.Decoder.prototype.select5=function(t){return this.getPreviousX(t)+(this.getPreviousY(t)-this.getPreviousXY(t)>>1)},s.lossless.Decoder.prototype.select6=function(t){return this.getPreviousY(t)+(this.getPreviousX(t)-this.getPreviousXY(t)>>1)},s.lossless.Decoder.prototype.select7=function(t){return(this.getPreviousX(t)+this.getPreviousY(t))/2},s.lossless.Decoder.prototype.decodeRGB=function(t,e,r){var i,o,a,n,h,l,c,u;for(t[0]=this.selector(0),t[1]=this.selector(1),t[2]=this.selector(2),h=0;h<this.numComp;h+=1)for(n=this.qTab[h],o=this.acTab[h],a=this.dcTab[h],l=0;l<this.nBlock[h];l+=1){for(c=0;c<this.IDCT_Source.length;c+=1)this.IDCT_Source[c]=0;if((i=this.getHuffmanValue(a,e,r))>=65280)return i;for(t[h]=this.IDCT_Source[0]=t[h]+this.getn(r,i,e,r),this.IDCT_Source[0]*=n[0],u=1;u<64;u+=1){if((i=this.getHuffmanValue(o,e,r))>=65280)return i;if(u+=i>>4,15&i)this.IDCT_Source[s.lossless.Decoder.IDCT_P[u]]=this.getn(r,15&i,e,r)*n[u];else if(!(i>>4))break}}return 0},s.lossless.Decoder.prototype.decodeSingle=function(t,e,r){var i,o,a,n;for(this.restarting?(this.restarting=!1,t[0]=1<<this.frame.precision-1):t[0]=this.selector(),o=0;o<this.nBlock[0];o+=1){if((i=this.getHuffmanValue(this.dcTab[0],e,r))>=65280)return i;if((n=(a=this.getn(t,i,e,r))>>8)>=s.lossless.Decoder.RESTART_MARKER_BEGIN&&n<=s.lossless.Decoder.RESTART_MARKER_END)return n;t[0]+=a}return 0},s.lossless.Decoder.prototype.getHuffmanValue=function(t,e,r){var i,o;if(r[0]<8?(e[0]<<=8,255===(o=this.stream.get8())&&(this.marker=this.stream.get8(),0!==this.marker&&(this.markerIndex=9)),e[0]|=o):r[0]-=8,(i=t[e[0]>>r[0]])&s.lossless.Decoder.MSB){if(0!==this.markerIndex)return this.markerIndex=0,65280|this.marker;e[0]&=65535>>16-r[0],e[0]<<=8,255===(o=this.stream.get8())&&(this.marker=this.stream.get8(),0!==this.marker&&(this.markerIndex=9)),e[0]|=o,i=t[256*(255&i)+(e[0]>>r[0])],r[0]+=8}if(r[0]+=8-(i>>8),r[0]<0)throw new Error("index="+r[0]+" temp="+e[0]+" code="+i+" in HuffmanValue()");return r[0]<this.markerIndex?(this.markerIndex=0,65280|this.marker):(e[0]&=65535>>16-r[0],255&i)},s.lossless.Decoder.prototype.getn=function(t,s,e,r){var i,o;if(0===s)return 0;if(16===s)return t[0]>=0?-32768:32768;if(r[0]-=s,r[0]>=0){if(r[0]<this.markerIndex&&!this.isLastPixel())return this.markerIndex=0,(65280|this.marker)<<8;i=e[0]>>r[0],e[0]&=65535>>16-r[0]}else{if(e[0]<<=8,255===(o=this.stream.get8())&&(this.marker=this.stream.get8(),0!==this.marker&&(this.markerIndex=9)),e[0]|=o,r[0]+=8,r[0]<0){if(0!==this.markerIndex)return this.markerIndex=0,(65280|this.marker)<<8;e[0]<<=8,255===(o=this.stream.get8())&&(this.marker=this.stream.get8(),0!==this.marker&&(this.markerIndex=9)),e[0]|=o,r[0]+=8}if(r[0]<0)throw new Error("index="+r[0]+" in getn()");if(r[0]<this.markerIndex)return this.markerIndex=0,(65280|this.marker)<<8;i=e[0]>>r[0],e[0]&=65535>>16-r[0]}return i<1<<s-1&&(i+=1+(-1<<s)),i},s.lossless.Decoder.prototype.getPreviousX=function(t){return this.xLoc>0?this.getter(this.yLoc*this.xDim+this.xLoc-1,t):this.yLoc>0?this.getPreviousY(t):1<<this.frame.precision-1},s.lossless.Decoder.prototype.getPreviousXY=function(t){return this.xLoc>0&&this.yLoc>0?this.getter((this.yLoc-1)*this.xDim+this.xLoc-1,t):this.getPreviousY(t)},s.lossless.Decoder.prototype.getPreviousY=function(t){return this.yLoc>0?this.getter((this.yLoc-1)*this.xDim+this.xLoc,t):this.getPreviousX(t)},s.lossless.Decoder.prototype.isLastPixel=function(){return this.xLoc===this.xDim-1&&this.yLoc===this.yDim-1},s.lossless.Decoder.prototype.outputSingle=function(t){this.xLoc<this.xDim&&this.yLoc<this.yDim&&(this.setter(this.yLoc*this.xDim+this.xLoc,this.mask&t[0]),this.xLoc+=1,this.xLoc>=this.xDim&&(this.yLoc+=1,this.xLoc=0))},s.lossless.Decoder.prototype.outputRGB=function(t){var s=this.yLoc*this.xDim+this.xLoc;this.xLoc<this.xDim&&this.yLoc<this.yDim&&(this.setter(s,t[0],0),this.setter(s,t[1],1),this.setter(s,t[2],2),this.xLoc+=1,this.xLoc>=this.xDim&&(this.yLoc+=1,this.xLoc=0))},s.lossless.Decoder.prototype.setValue8=function(t,s){this.outputData[t]=s},s.lossless.Decoder.prototype.getValue8=function(t){return this.outputData[t]},t=new ArrayBuffer(2),new DataView(t).setInt16(0,256,!0),256===new Int16Array(t)[0]?(s.lossless.Decoder.prototype.setValue16=s.lossless.Decoder.prototype.setValue8,s.lossless.Decoder.prototype.getValue16=s.lossless.Decoder.prototype.getValue8):(s.lossless.Decoder.prototype.setValue16=function(t,s){this.outputData[t]=(255&s)<<8|s>>8&255},s.lossless.Decoder.prototype.getValue16=function(t){var s=this.outputData[t];return(255&s)<<8|s>>8&255}),s.lossless.Decoder.prototype.setValueRGB=function(t,s,e){this.outputData[3*t+e]=s},s.lossless.Decoder.prototype.getValueRGB=function(t,s){return this.outputData[3*t+s]},s.lossless.Decoder.prototype.readApp=function(){var t=0,s=this.stream.get16();for(t+=2;t<s;)this.stream.get8(),t+=1;return s},s.lossless.Decoder.prototype.readComment=function(){var t,s="",e=0;for(t=this.stream.get16(),e+=2;e<t;)s+=this.stream.get8(),e+=1;return s},s.lossless.Decoder.prototype.readNumber=function(){if(4!==this.stream.get16())throw new Error("ERROR: Define number format throw new IOException [Ld!=4]");return this.stream.get16()},s.lossless.FrameHeader=s.lossless.FrameHeader||function(){this.components=[],this.dimX=0,this.dimY=0,this.numComp=0,this.precision=0},s.lossless.FrameHeader.prototype.read=function(t){var e,r,i,o,a=0;for(e=t.get16(),a+=2,this.precision=t.get8(),a+=1,this.dimY=t.get16(),a+=2,this.dimX=t.get16(),a+=2,this.numComp=t.get8(),a+=1,r=1;r<=this.numComp;r+=1){if(a>e)throw new Error("ERROR: frame format error");if(i=t.get8(),(a+=1)>=e)throw new Error("ERROR: frame format error [c>=Lf]");o=t.get8(),a+=1,this.components[i]||(this.components[i]=new s.lossless.ComponentSpec),this.components[i].hSamp=o>>4,this.components[i].vSamp=15&o,this.components[i].quantTableSel=t.get8(),a+=1}if(a!==e)throw new Error("ERROR: frame format error [Lf!=count]");return 1},s.lossless.HuffmanTable=s.lossless.HuffmanTable||function(){this.l=s.lossless.Utils.createArray(4,2,16),this.th=[],this.v=s.lossless.Utils.createArray(4,2,16,200),this.tc=s.lossless.Utils.createArray(4,2),this.tc[0][0]=0,this.tc[1][0]=0,this.tc[2][0]=0,this.tc[3][0]=0,this.tc[0][1]=0,this.tc[1][1]=0,this.tc[2][1]=0,this.tc[3][1]=0,this.th[0]=0,this.th[1]=0,this.th[2]=0,this.th[3]=0},s.lossless.HuffmanTable.MSB=2147483648,s.lossless.HuffmanTable.prototype.read=function(t,s){var e,r,i,o,a,n,h=0;for(e=t.get16(),h+=2;h<e;){if(h+=1,(i=15&(r=t.get8()))>3)throw new Error("ERROR: Huffman table ID > 3");if((o=r>>4)>2)throw new Error("ERROR: Huffman table [Table class > 2 ]");for(this.th[i]=1,this.tc[i][o]=1,a=0;a<16;a+=1)this.l[i][o][a]=t.get8(),h+=1;for(a=0;a<16;a+=1)for(n=0;n<this.l[i][o][a];n+=1){if(h>e)throw new Error("ERROR: Huffman table format error [count>Lh]");this.v[i][o][a][n]=t.get8(),h+=1}}if(h!==e)throw new Error("ERROR: Huffman table format error [count!=Lf]");for(a=0;a<4;a+=1)for(n=0;n<2;n+=1)0!==this.tc[a][n]&&this.buildHuffTable(s[a][n],this.l[a][n],this.v[a][n]);return 1},s.lossless.HuffmanTable.prototype.buildHuffTable=function(t,e,r){var i,o,a,n,h;for(o=0,a=0;a<8;a+=1)for(n=0;n<e[a];n+=1)for(h=0;h<256>>a+1;h+=1)t[o]=r[a][n]|a+1<<8,o+=1;for(a=1;o<256;a+=1,o+=1)t[o]=a|s.lossless.HuffmanTable.MSB;for(i=1,o=0,a=8;a<16;a+=1)for(n=0;n<e[a];n+=1){for(h=0;h<256>>a-7;h+=1)t[256*i+o]=r[a][n]|a+1<<8,o+=1;if(o>=256){if(o>256)throw new Error("ERROR: Huffman table error(1)!");o=0,i+=1}}},s.lossless.QuantizationTable=s.lossless.QuantizationTable||function(){this.precision=[],this.tq=[],this.quantTables=s.lossless.Utils.createArray(4,64),this.tq[0]=0,this.tq[1]=0,this.tq[2]=0,this.tq[3]=0},s.lossless.QuantizationTable.enhanceQuantizationTable=function(t,s){var e;for(e=0;e<8;e+=1)t[s[0+e]]*=90,t[s[32+e]]*=90,t[s[16+e]]*=118,t[s[48+e]]*=49,t[s[40+e]]*=71,t[s[8+e]]*=126,t[s[56+e]]*=25,t[s[24+e]]*=106;for(e=0;e<8;e+=1)t[s[0+8*e]]*=90,t[s[4+8*e]]*=90,t[s[2+8*e]]*=118,t[s[6+8*e]]*=49,t[s[5+8*e]]*=71,t[s[1+8*e]]*=126,t[s[7+8*e]]*=25,t[s[3+8*e]]*=106;for(e=0;e<64;e+=1)t[e]>>=6},s.lossless.QuantizationTable.prototype.read=function(t,e){var r,i,o,a,n=0;for(r=t.get16(),n+=2;n<r;){if(n+=1,(o=15&(i=t.get8()))>3)throw new Error("ERROR: Quantization table ID > 3");if(this.precision[o]=i>>4,0===this.precision[o])this.precision[o]=8;else{if(1!==this.precision[o])throw new Error("ERROR: Quantization table precision error");this.precision[o]=16}if(this.tq[o]=1,8===this.precision[o]){for(a=0;a<64;a+=1){if(n>r)throw new Error("ERROR: Quantization table format error");this.quantTables[o][a]=t.get8(),n+=1}s.lossless.QuantizationTable.enhanceQuantizationTable(this.quantTables[o],e)}else{for(a=0;a<64;a+=1){if(n>r)throw new Error("ERROR: Quantization table format error");this.quantTables[o][a]=t.get16(),n+=2}s.lossless.QuantizationTable.enhanceQuantizationTable(this.quantTables[o],e)}}if(n!==r)throw new Error("ERROR: Quantization table error [count!=Lq]");return 1},s.lossless.ScanComponent=s.lossless.ScanComponent||function(){this.acTabSel=0,this.dcTabSel=0,this.scanCompSel=0},s.lossless.ScanHeader=s.lossless.ScanHeader||function(){this.ah=0,this.al=0,this.numComp=0,this.selection=0,this.spectralEnd=0,this.components=[]},s.lossless.ScanHeader.prototype.read=function(t){var e,r,i,o=0;for(e=t.get16(),o+=2,this.numComp=t.get8(),o+=1,r=0;r<this.numComp;r+=1){if(this.components[r]=new s.lossless.ScanComponent,o>e)throw new Error("ERROR: scan header format error");this.components[r].scanCompSel=t.get8(),o+=1,i=t.get8(),o+=1,this.components[r].dcTabSel=i>>4,this.components[r].acTabSel=15&i}if(this.selection=t.get8(),o+=1,this.spectralEnd=t.get8(),o+=1,i=t.get8(),this.ah=i>>4,this.al=15&i,(o+=1)!==e)throw new Error("ERROR: scan header format error [count!=Ns]");return 1},s.lossless.Utils=s.lossless.Utils||{},s.lossless.Utils.createArray=function(t){var e=new Array(t||0),r=t;if(arguments.length>1)for(var i=Array.prototype.slice.call(arguments,1);r--;)e[t-1-r]=s.lossless.Utils.createArray.apply(this,i);return e},s.lossless.Utils.makeCRCTable=function(){for(var t,s=[],e=0;e<256;e++){t=e;for(var r=0;r<8;r++)t=1&t?3988292384^t>>>1:t>>>1;s[e]=t}return s},s.lossless.Utils.crc32=function(t){for(var e=new Uint8Array(t.buffer),r=s.lossless.Utils.crcTable||(s.lossless.Utils.crcTable=s.lossless.Utils.makeCRCTable()),i=-1,o=0;o<e.length;o++)i=i>>>8^r[255&(i^e[o])];return~i>>>0},self.addEventListener("message",(function(t){const e=t.data.meta.bitsAllocated/8,r=new Uint8Array(t.data.buffer),i=(new s.lossless.Decoder).decode(r.buffer,0,r.buffer.byteLength,e);let o=null;8===t.data.meta.bitsAllocated?o=t.data.meta.isSigned?new Int8Array(i.buffer):new Uint8Array(i.buffer):16===t.data.meta.bitsAllocated&&(o=t.data.meta.isSigned?new Int16Array(i.buffer):new Uint16Array(i.buffer)),self.postMessage([o])}),!1);
//# sourceMappingURL=jpegloss.worker.min.js.map