jpeg-lossless-decoder-js
Advanced tools
Comparing version 2.0.6 to 2.0.7
{ | ||
"name": "jpeg-lossless-decoder-js", | ||
"version": "2.0.6", | ||
"version": "2.0.7", | ||
"description": "A JavaScript JPEG Lossless decoder.", | ||
@@ -12,2 +12,4 @@ "main": "src/main.js", | ||
"esbuild": "*", | ||
"browserify": "*", | ||
"uglify-js": "*", | ||
"jsdoc-to-markdown": "*" | ||
@@ -17,3 +19,4 @@ }, | ||
"test": "mocha tests", | ||
"build": "rm -rf build; mkdir build; esbuild src/main.js --bundle --global-name=jpeg --outfile=build/lossless.js; esbuild src/main.js --bundle --global-name=jpeg --minify --outfile=build/lossless-min.js", | ||
"esbuild": "rm -rf build; mkdir build; esbuild src/main.js --bundle --global-name=jpeg --outfile=build/lossless.js; esbuild src/main.js --bundle --global-name=jpeg --minify --outfile=build/lossless-min.js", | ||
"build": "rm -rf build; mkdir build; browserify --standalone jpeg src/main.js -o build/lossless.js; uglifyjs build/lossless.js -o build/lossless-min.js", | ||
"release": "rm release/current/*.js; cp build/*.js release/current/.", | ||
@@ -20,0 +23,0 @@ "doc": "rm -rf build; mkdir build; ./node_modules/.bin/jsdoc2md src/*.js > build/docs.md" |
@@ -27,3 +27,3 @@ JPEGLosslessDecoderJS | ||
### Install | ||
Get a packaged source file: | ||
Get a packaged source file from the [release folder](https://github.com/rii-mango/JPEGLosslessDecoderJS/tree/master/release): | ||
@@ -45,9 +45,8 @@ * [lossless.js](https://raw.githubusercontent.com/rii-mango/JPEGLosslessDecoderJS/master/release/current/lossless.js) | ||
### Building | ||
See the [release folder](https://github.com/rii-mango/JPEGLosslessDecoderJS/tree/master/release) for the latest builds or build it yourself using: | ||
``` | ||
npm run build | ||
``` | ||
This will output lossless.js and lossless-min.js to build/. | ||
This will output lossless.js and lossless-min.js to build/ | ||
### Acknowledgments | ||
This decoder was originally written by Helmut Dersch for Java. I added support for selection values 2 through 7, contributed bug fixes and ported to JavaScript. |
@@ -1,1 +0,1 @@ | ||
"use strict";var jpeg=(()=>{var u=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(s,t)=>(typeof require<"u"?require:s)[t]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw new Error('Dynamic require of "'+e+'" is not supported')});var y=(e,s)=>()=>(s||e((s={exports:{}}).exports,s),s.exports);var U=y((te,S)=>{"use strict";var T=T||{};T.lossless=T.lossless||{};T.lossless.ComponentSpec=T.lossless.ComponentSpec||function(){this.hSamp=0,this.quantTableSel=0,this.vSamp=0};var X=typeof S;X!=="undefined"&&S.exports&&(S.exports=T.lossless.ComponentSpec)});var E=y((re,v)=>{"use strict";var g=g||{};g.lossless=g.lossless||{};g.lossless.DataStream=g.lossless.DataStream||function(e,s,t){s===void 0&&t===void 0?this.buffer=new Uint8Array(e):this.buffer=new Uint8Array(e,s,t),this.index=0};g.lossless.DataStream.prototype.get16=function(){var e=(this.buffer[this.index]<<8)+this.buffer[this.index+1];return this.index+=2,e};g.lossless.DataStream.prototype.get8=function(){var e=this.buffer[this.index];return this.index+=1,e};var Y=typeof v;Y!=="undefined"&&v.exports&&(v.exports=g.lossless.DataStream)});var w=y((ie,C)=>{"use strict";var p=p||{};p.lossless=p.lossless||{};p.lossless.Utils=p.lossless.Utils||{};p.lossless.Utils.createArray=function(e){var s=new Array(e||0),t=e;if(arguments.length>1)for(var r=Array.prototype.slice.call(arguments,1);t--;)s[e-1-t]=p.lossless.Utils.createArray.apply(this,r);return s};p.lossless.Utils.makeCRCTable=function(){for(var e,s=[],t=0;t<256;t++){e=t;for(var r=0;r<8;r++)e=e&1?3988292384^e>>>1:e>>>1;s[t]=e}return s};p.lossless.Utils.crc32=function(e){for(var s=new Uint8Array(e.buffer),t=p.lossless.Utils.crcTable||(p.lossless.Utils.crcTable=p.lossless.Utils.makeCRCTable()),r=-1,i=0;i<s.length;i++)r=r>>>8^t[(r^s[i])&255];return(r^-1)>>>0};var O=typeof C;O!=="undefined"&&C.exports&&(C.exports=p.lossless.Utils)});var P=y((oe,k)=>{"use strict";var f=f||{};f.lossless=f.lossless||{};f.lossless.DataStream=f.lossless.DataStream||(typeof u<"u"?E():null);f.lossless.Utils=f.lossless.Utils||(typeof u<"u"?w():null);f.lossless.HuffmanTable=f.lossless.HuffmanTable||function(){this.l=f.lossless.Utils.createArray(4,2,16),this.th=[],this.v=f.lossless.Utils.createArray(4,2,16,200),this.tc=f.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};f.lossless.HuffmanTable.MSB=2147483648;f.lossless.HuffmanTable.prototype.read=function(e,s){var t=0,r,i,l,a,n,h;for(r=e.get16(),t+=2;t<r;){if(i=e.get8(),t+=1,l=i&15,l>3)throw new Error("ERROR: Huffman table ID > 3");if(a=i>>4,a>2)throw new Error("ERROR: Huffman table [Table class > 2 ]");for(this.th[l]=1,this.tc[l][a]=1,n=0;n<16;n+=1)this.l[l][a][n]=e.get8(),t+=1;for(n=0;n<16;n+=1)for(h=0;h<this.l[l][a][n];h+=1){if(t>r)throw new Error("ERROR: Huffman table format error [count>Lh]");this.v[l][a][n][h]=e.get8(),t+=1}}if(t!==r)throw new Error("ERROR: Huffman table format error [count!=Lf]");for(n=0;n<4;n+=1)for(h=0;h<2;h+=1)this.tc[n][h]!==0&&this.buildHuffTable(s[n][h],this.l[n][h],this.v[n][h]);return 1};f.lossless.HuffmanTable.prototype.buildHuffTable=function(e,s,t){var r,i,l,a,n,h;for(i=256,l=0,a=0;a<8;a+=1)for(n=0;n<s[a];n+=1)for(h=0;h<i>>a+1;h+=1)e[l]=t[a][n]|a+1<<8,l+=1;for(a=1;l<256;a+=1,l+=1)e[l]=a|f.lossless.HuffmanTable.MSB;for(r=1,l=0,a=8;a<16;a+=1)for(n=0;n<s[a];n+=1){for(h=0;h<i>>a-7;h+=1)e[r*256+l]=t[a][n]|a+1<<8,l+=1;if(l>=256){if(l>256)throw new Error("ERROR: Huffman table error(1)!");l=0,r+=1}}};var j=typeof k;j!=="undefined"&&k.exports&&(k.exports=f.lossless.HuffmanTable)});var _=y((ne,q)=>{"use strict";var m=m||{};m.lossless=m.lossless||{};m.lossless.DataStream=m.lossless.DataStream||(typeof u<"u"?E():null);m.lossless.Utils=m.lossless.Utils||(typeof u<"u"?w():null);m.lossless.QuantizationTable=m.lossless.QuantizationTable||function(){this.precision=[],this.tq=[],this.quantTables=m.lossless.Utils.createArray(4,64),this.tq[0]=0,this.tq[1]=0,this.tq[2]=0,this.tq[3]=0};m.lossless.QuantizationTable.enhanceQuantizationTable=function(e,s){var t;for(t=0;t<8;t+=1)e[s[0*8+t]]*=90,e[s[4*8+t]]*=90,e[s[2*8+t]]*=118,e[s[6*8+t]]*=49,e[s[5*8+t]]*=71,e[s[1*8+t]]*=126,e[s[7*8+t]]*=25,e[s[3*8+t]]*=106;for(t=0;t<8;t+=1)e[s[0+8*t]]*=90,e[s[4+8*t]]*=90,e[s[2+8*t]]*=118,e[s[6+8*t]]*=49,e[s[5+8*t]]*=71,e[s[1+8*t]]*=126,e[s[7+8*t]]*=25,e[s[3+8*t]]*=106;for(t=0;t<64;t+=1)e[t]>>=6};m.lossless.QuantizationTable.prototype.read=function(e,s){var t=0,r,i,l,a;for(r=e.get16(),t+=2;t<r;){if(i=e.get8(),t+=1,l=i&15,l>3)throw new Error("ERROR: Quantization table ID > 3");if(this.precision[l]=i>>4,this.precision[l]===0)this.precision[l]=8;else if(this.precision[l]===1)this.precision[l]=16;else throw new Error("ERROR: Quantization table precision error");if(this.tq[l]=1,this.precision[l]===8){for(a=0;a<64;a+=1){if(t>r)throw new Error("ERROR: Quantization table format error");this.quantTables[l][a]=e.get8(),t+=1}m.lossless.QuantizationTable.enhanceQuantizationTable(this.quantTables[l],s)}else{for(a=0;a<64;a+=1){if(t>r)throw new Error("ERROR: Quantization table format error");this.quantTables[l][a]=e.get16(),t+=2}m.lossless.QuantizationTable.enhanceQuantizationTable(this.quantTables[l],s)}}if(t!==r)throw new Error("ERROR: Quantization table error [count!=Lq]");return 1};var M=typeof q;M!=="undefined"&&q.exports&&(q.exports=m.lossless.QuantizationTable)});var V=y((he,H)=>{"use strict";var b=b||{};b.lossless=b.lossless||{};b.lossless.ScanComponent=b.lossless.ScanComponent||function(){this.acTabSel=0,this.dcTabSel=0,this.scanCompSel=0};var G=typeof H;G!=="undefined"&&H.exports&&(H.exports=b.lossless.ScanComponent)});var z=y((ue,L)=>{"use strict";var d=d||{};d.lossless=d.lossless||{};d.lossless.DataStream=d.lossless.DataStream||(typeof u<"u"?E():null);d.lossless.ScanComponent=d.lossless.ScanComponent||(typeof u<"u"?V():null);d.lossless.ScanHeader=d.lossless.ScanHeader||function(){this.ah=0,this.al=0,this.numComp=0,this.selection=0,this.spectralEnd=0,this.components=[]};d.lossless.ScanHeader.prototype.read=function(e){var s=0,t,r,i;for(t=e.get16(),s+=2,this.numComp=e.get8(),s+=1,r=0;r<this.numComp;r+=1){if(this.components[r]=new d.lossless.ScanComponent,s>t)throw new Error("ERROR: scan header format error");this.components[r].scanCompSel=e.get8(),s+=1,i=e.get8(),s+=1,this.components[r].dcTabSel=i>>4,this.components[r].acTabSel=i&15}if(this.selection=e.get8(),s+=1,this.spectralEnd=e.get8(),s+=1,i=e.get8(),this.ah=i>>4,this.al=i&15,s+=1,s!==t)throw new Error("ERROR: scan header format error [count!=Ns]");return 1};var K=typeof L;K!=="undefined"&&L.exports&&(L.exports=d.lossless.ScanHeader)});var Q=y((fe,A)=>{"use strict";var F=F||{};F.lossless=F.lossless||{};F.lossless.ComponentSpec=F.lossless.ComponentSpec||(typeof u<"u"?U():null);F.lossless.DataStream=F.lossless.DataStream||(typeof u<"u"?E():null);F.lossless.FrameHeader=F.lossless.FrameHeader||function(){this.components=[],this.dimX=0,this.dimY=0,this.numComp=0,this.precision=0};F.lossless.FrameHeader.prototype.read=function(e){var s=0,t,r,i,l;for(t=e.get16(),s+=2,this.precision=e.get8(),s+=1,this.dimY=e.get16(),s+=2,this.dimX=e.get16(),s+=2,this.numComp=e.get8(),s+=1,r=1;r<=this.numComp;r+=1){if(s>t)throw new Error("ERROR: frame format error");if(i=e.get8(),s+=1,s>=t)throw new Error("ERROR: frame format error [c>=Lf]");l=e.get8(),s+=1,this.components[i]||(this.components[i]=new F.lossless.ComponentSpec),this.components[i].hSamp=l>>4,this.components[i].vSamp=l&15,this.components[i].quantTableSel=e.get8(),s+=1}if(s!==t)throw new Error("ERROR: frame format error [Lf!=count]");return 1};var J=typeof A;J!=="undefined"&&A.exports&&(A.exports=F.lossless.FrameHeader)});var N=y((pe,I)=>{"use strict";var o=o||{};o.lossless=o.lossless||{};o.lossless.DataStream=o.lossless.DataStream||(typeof u<"u"?E():null);o.lossless.HuffmanTable=o.lossless.HuffmanTable||(typeof u<"u"?P():null);o.lossless.QuantizationTable=o.lossless.QuantizationTable||(typeof u<"u"?_():null);o.lossless.ScanHeader=o.lossless.ScanHeader||(typeof u<"u"?z():null);o.lossless.FrameHeader=o.lossless.FrameHeader||(typeof u<"u"?Q():null);o.lossless.Utils=o.lossless.Utils||(typeof u<"u"?w():null);o.lossless.Decoder=o.lossless.Decoder||function(e,s){this.buffer=e,this.frame=new o.lossless.FrameHeader,this.huffTable=new o.lossless.HuffmanTable,this.quantTable=new o.lossless.QuantizationTable,this.scan=new o.lossless.ScanHeader,this.DU=o.lossless.Utils.createArray(10,4,64),this.HuffTab=o.lossless.Utils.createArray(4,2,50*256),this.IDCT_Source=[],this.nBlock=[],this.acTab=o.lossless.Utils.createArray(10,1),this.dcTab=o.lossless.Utils.createArray(10,1),this.qTab=o.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,typeof s<"u"&&(this.numBytes=s)};o.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];o.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];o.lossless.Decoder.MAX_HUFFMAN_SUBTREE=50;o.lossless.Decoder.MSB=2147483648;o.lossless.Decoder.RESTART_MARKER_BEGIN=65488;o.lossless.Decoder.RESTART_MARKER_END=65495;o.lossless.Decoder.prototype.decompress=function(e,s,t){return this.decode(e,s,t).buffer};o.lossless.Decoder.prototype.decode=function(e,s,t,r){var i,l=0,a=[],n,h,D=[],x=[],R;if(typeof e<"u"&&(this.buffer=e),typeof r<"u"&&(this.numBytes=r),this.stream=new o.lossless.DataStream(this.buffer,s,t),this.buffer=null,this.xLoc=0,this.yLoc=0,i=this.stream.get16(),i!==65496)throw new Error("Not a JPEG file");for(i=this.stream.get16();i>>4!==4092||i===65476;){switch(i){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,o.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(i>>8!==255)throw new Error("ERROR: format throw new IOException! (decode)")}i=this.stream.get16()}if(i<65472||i>65479)throw new Error("ERROR: could not handle arithmetic code!");this.frame.read(this.stream),i=this.stream.get16();do{for(;i!==65498;){switch(i){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,o.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(i>>8!==255)throw new Error("ERROR: format throw new IOException! (Parser.decode)")}i=this.stream.get16()}switch(this.precision=this.frame.precision,this.components=this.frame.components,this.numBytes||(this.numBytes=parseInt(Math.ceil(this.precision/8))),this.numBytes==1?this.mask=255:this.mask=65535,this.scan.read(this.stream),this.numComp=this.scan.numComp,this.selection=this.scan.selection,this.numBytes===1?this.numComp===3?(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;break}for(this.scanComps=this.scan.components,this.quantTables=this.quantTable.quantTables,n=0;n<this.numComp;n+=1)h=this.scanComps[n].scanCompSel,this.qTab[n]=this.quantTables[this.components[h].quantTableSel],this.nBlock[n]=this.components[h].vSamp*this.components[h].hSamp,this.dcTab[n]=this.HuffTab[this.scanComps[n].dcTabSel][0],this.acTab[n]=this.HuffTab[this.scanComps[n].acTabSel][1];for(this.xDim=this.frame.dimX,this.yDim=this.frame.dimY,this.numBytes==1?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(D[0]=0,x[0]=0,n=0;n<10;n+=1)a[n]=1<<this.precision-1;if(this.restartInterval===0){for(i=this.decodeUnit(a,D,x);i===0&&this.xLoc<this.xDim&&this.yLoc<this.yDim;)this.output(a),i=this.decodeUnit(a,D,x);break}for(R=0;R<this.restartInterval&&(this.restarting=R==0,i=this.decodeUnit(a,D,x),this.output(a),i===0);R+=1);if(i===0&&(this.markerIndex!==0?(i=65280|this.marker,this.markerIndex=0):i=this.stream.get16()),!(i>=o.lossless.Decoder.RESTART_MARKER_BEGIN&&i<=o.lossless.Decoder.RESTART_MARKER_END))break}i===65500&&l===1&&(this.readNumber(),i=this.stream.get16())}while(i!==65497&&this.xLoc<this.xDim&&this.yLoc<this.yDim&&l===0);return this.outputData};o.lossless.Decoder.prototype.decodeUnit=function(e,s,t){return this.numComp==1?this.decodeSingle(e,s,t):this.numComp==3?this.decodeRGB(e,s,t):-1};o.lossless.Decoder.prototype.select1=function(e){return this.getPreviousX(e)};o.lossless.Decoder.prototype.select2=function(e){return this.getPreviousY(e)};o.lossless.Decoder.prototype.select3=function(e){return this.getPreviousXY(e)};o.lossless.Decoder.prototype.select4=function(e){return this.getPreviousX(e)+this.getPreviousY(e)-this.getPreviousXY(e)};o.lossless.Decoder.prototype.select5=function(e){return this.getPreviousX(e)+(this.getPreviousY(e)-this.getPreviousXY(e)>>1)};o.lossless.Decoder.prototype.select6=function(e){return this.getPreviousY(e)+(this.getPreviousX(e)-this.getPreviousXY(e)>>1)};o.lossless.Decoder.prototype.select7=function(e){return(this.getPreviousX(e)+this.getPreviousY(e))/2};o.lossless.Decoder.prototype.decodeRGB=function(e,s,t){var r,i,l,a,n,h,D,x;for(e[0]=this.selector(0),e[1]=this.selector(1),e[2]=this.selector(2),n=0;n<this.numComp;n+=1)for(a=this.qTab[n],i=this.acTab[n],l=this.dcTab[n],h=0;h<this.nBlock[n];h+=1){for(D=0;D<this.IDCT_Source.length;D+=1)this.IDCT_Source[D]=0;if(r=this.getHuffmanValue(l,s,t),r>=65280)return r;for(e[n]=this.IDCT_Source[0]=e[n]+this.getn(t,r,s,t),this.IDCT_Source[0]*=a[0],x=1;x<64;x+=1){if(r=this.getHuffmanValue(i,s,t),r>=65280)return r;if(x+=r>>4,r&15)this.IDCT_Source[o.lossless.Decoder.IDCT_P[x]]=this.getn(t,r&15,s,t)*a[x];else if(!(r>>4))break}}return 0};o.lossless.Decoder.prototype.decodeSingle=function(e,s,t){var r,i,l,a;for(this.restarting?(this.restarting=!1,e[0]=1<<this.frame.precision-1):e[0]=this.selector(),i=0;i<this.nBlock[0];i+=1){if(r=this.getHuffmanValue(this.dcTab[0],s,t),r>=65280)return r;if(l=this.getn(e,r,s,t),a=l>>8,a>=o.lossless.Decoder.RESTART_MARKER_BEGIN&&a<=o.lossless.Decoder.RESTART_MARKER_END)return a;e[0]+=l}return 0};o.lossless.Decoder.prototype.getHuffmanValue=function(e,s,t){var r,i,l;if(l=65535,t[0]<8?(s[0]<<=8,i=this.stream.get8(),i===255&&(this.marker=this.stream.get8(),this.marker!==0&&(this.markerIndex=9)),s[0]|=i):t[0]-=8,r=e[s[0]>>t[0]],r&o.lossless.Decoder.MSB){if(this.markerIndex!==0)return this.markerIndex=0,65280|this.marker;s[0]&=l>>16-t[0],s[0]<<=8,i=this.stream.get8(),i===255&&(this.marker=this.stream.get8(),this.marker!==0&&(this.markerIndex=9)),s[0]|=i,r=e[(r&255)*256+(s[0]>>t[0])],t[0]+=8}if(t[0]+=8-(r>>8),t[0]<0)throw new Error("index="+t[0]+" temp="+s[0]+" code="+r+" in HuffmanValue()");return t[0]<this.markerIndex?(this.markerIndex=0,65280|this.marker):(s[0]&=l>>16-t[0],r&255)};o.lossless.Decoder.prototype.getn=function(e,s,t,r){var i,l,a,n,h;if(l=1,a=-1,n=65535,s===0)return 0;if(s===16)return e[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=t[0]>>r[0],t[0]&=n>>16-r[0]}else{if(t[0]<<=8,h=this.stream.get8(),h===255&&(this.marker=this.stream.get8(),this.marker!==0&&(this.markerIndex=9)),t[0]|=h,r[0]+=8,r[0]<0){if(this.markerIndex!==0)return this.markerIndex=0,(65280|this.marker)<<8;t[0]<<=8,h=this.stream.get8(),h===255&&(this.marker=this.stream.get8(),this.marker!==0&&(this.markerIndex=9)),t[0]|=h,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=t[0]>>r[0],t[0]&=n>>16-r[0]}return i<l<<s-1&&(i+=(a<<s)+1),i};o.lossless.Decoder.prototype.getPreviousX=function(e){return this.xLoc>0?this.getter(this.yLoc*this.xDim+this.xLoc-1,e):this.yLoc>0?this.getPreviousY(e):1<<this.frame.precision-1};o.lossless.Decoder.prototype.getPreviousXY=function(e){return this.xLoc>0&&this.yLoc>0?this.getter((this.yLoc-1)*this.xDim+this.xLoc-1,e):this.getPreviousY(e)};o.lossless.Decoder.prototype.getPreviousY=function(e){return this.yLoc>0?this.getter((this.yLoc-1)*this.xDim+this.xLoc,e):this.getPreviousX(e)};o.lossless.Decoder.prototype.isLastPixel=function(){return this.xLoc===this.xDim-1&&this.yLoc===this.yDim-1};o.lossless.Decoder.prototype.outputSingle=function(e){this.xLoc<this.xDim&&this.yLoc<this.yDim&&(this.setter(this.yLoc*this.xDim+this.xLoc,this.mask&e[0]),this.xLoc+=1,this.xLoc>=this.xDim&&(this.yLoc+=1,this.xLoc=0))};o.lossless.Decoder.prototype.outputRGB=function(e){var s=this.yLoc*this.xDim+this.xLoc;this.xLoc<this.xDim&&this.yLoc<this.yDim&&(this.setter(s,e[0],0),this.setter(s,e[1],1),this.setter(s,e[2],2),this.xLoc+=1,this.xLoc>=this.xDim&&(this.yLoc+=1,this.xLoc=0))};o.lossless.Decoder.prototype.setValue8=function(e,s){this.outputData[e]=s};o.lossless.Decoder.prototype.getValue8=function(e){return this.outputData[e]};var W=function(){var e=new ArrayBuffer(2);return new DataView(e).setInt16(0,256,!0),new Int16Array(e)[0]===256}();W?(o.lossless.Decoder.prototype.setValue16=o.lossless.Decoder.prototype.setValue8,o.lossless.Decoder.prototype.getValue16=o.lossless.Decoder.prototype.getValue8):(o.lossless.Decoder.prototype.setValue16=function(e,s){this.outputData[e]=(s&255)<<8|s>>8&255},o.lossless.Decoder.prototype.getValue16=function(e){var s=this.outputData[e];return(s&255)<<8|s>>8&255});o.lossless.Decoder.prototype.setValueRGB=function(e,s,t){this.outputData[e*3+t]=s};o.lossless.Decoder.prototype.getValueRGB=function(e,s){return this.outputData[e*3+s]};o.lossless.Decoder.prototype.readApp=function(){var e=0,s=this.stream.get16();for(e+=2;e<s;)this.stream.get8(),e+=1;return s};o.lossless.Decoder.prototype.readComment=function(){var e="",s=0,t;for(t=this.stream.get16(),s+=2;s<t;)e+=this.stream.get8(),s+=1;return e};o.lossless.Decoder.prototype.readNumber=function(){var e=this.stream.get16();if(e!==4)throw new Error("ERROR: Define number format throw new IOException [Ld!=4]");return this.stream.get16()};var Z=typeof I;Z!=="undefined"&&I.exports&&(I.exports=o.lossless.Decoder)});var ee=y((Fe,B)=>{var c=c||{};c.lossless=c.lossless||{};c.lossless.ComponentSpec=c.lossless.ComponentSpec||(typeof u<"u"?U():null);c.lossless.DataStream=c.lossless.DataStream||(typeof u<"u"?E():null);c.lossless.Decoder=c.lossless.Decoder||(typeof u<"u"?N():null);c.lossless.FrameHeader=c.lossless.FrameHeader||(typeof u<"u"?Q():null);c.lossless.HuffmanTable=c.lossless.HuffmanTable||(typeof u<"u"?P():null);c.lossless.QuantizationTable=c.lossless.QuantizationTable||(typeof u<"u"?_():null);c.lossless.ScanComponent=c.lossless.ScanComponent||(typeof u<"u"?V():null);c.lossless.ScanHeader=c.lossless.ScanHeader||(typeof u<"u"?z():null);c.lossless.Utils=c.lossless.Utils||(typeof u<"u"?w():null);var $=typeof B;$!=="undefined"&&B.exports&&(B.exports=c)});return ee();})(); | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.jpeg=f()}})(function(){var define,module,exports;return function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r}()({1:[function(require,module,exports){"use strict";var jpeg=jpeg||{};jpeg.lossless=jpeg.lossless||{};jpeg.lossless.ComponentSpec=jpeg.lossless.ComponentSpec||function(){this.hSamp=0;this.quantTableSel=0;this.vSamp=0};var moduleType=typeof module;if(moduleType!=="undefined"&&module.exports){module.exports=jpeg.lossless.ComponentSpec}},{}],2:[function(require,module,exports){"use strict";var jpeg=jpeg||{};jpeg.lossless=jpeg.lossless||{};jpeg.lossless.DataStream=jpeg.lossless.DataStream||function(data,offset,length){if(offset===undefined&&length===undefined){this.buffer=new Uint8Array(data)}else{this.buffer=new Uint8Array(data,offset,length)}this.index=0};jpeg.lossless.DataStream.prototype.get16=function(){var value=(this.buffer[this.index]<<8)+this.buffer[this.index+1];this.index+=2;return value};jpeg.lossless.DataStream.prototype.get8=function(){var value=this.buffer[this.index];this.index+=1;return value};var moduleType=typeof module;if(moduleType!=="undefined"&&module.exports){module.exports=jpeg.lossless.DataStream}},{}],3:[function(require,module,exports){"use strict";var jpeg=jpeg||{};jpeg.lossless=jpeg.lossless||{};jpeg.lossless.DataStream=jpeg.lossless.DataStream||(typeof require!=="undefined"?require("./data-stream.js"):null);jpeg.lossless.HuffmanTable=jpeg.lossless.HuffmanTable||(typeof require!=="undefined"?require("./huffman-table.js"):null);jpeg.lossless.QuantizationTable=jpeg.lossless.QuantizationTable||(typeof require!=="undefined"?require("./quantization-table.js"):null);jpeg.lossless.ScanHeader=jpeg.lossless.ScanHeader||(typeof require!=="undefined"?require("./scan-header.js"):null);jpeg.lossless.FrameHeader=jpeg.lossless.FrameHeader||(typeof require!=="undefined"?require("./frame-header.js"):null);jpeg.lossless.Utils=jpeg.lossless.Utils||(typeof require!=="undefined"?require("./utils.js"):null);jpeg.lossless.Decoder=jpeg.lossless.Decoder||function(buffer,numBytes){this.buffer=buffer;this.frame=new jpeg.lossless.FrameHeader;this.huffTable=new jpeg.lossless.HuffmanTable;this.quantTable=new jpeg.lossless.QuantizationTable;this.scan=new jpeg.lossless.ScanHeader;this.DU=jpeg.lossless.Utils.createArray(10,4,64);this.HuffTab=jpeg.lossless.Utils.createArray(4,2,50*256);this.IDCT_Source=[];this.nBlock=[];this.acTab=jpeg.lossless.Utils.createArray(10,1);this.dcTab=jpeg.lossless.Utils.createArray(10,1);this.qTab=jpeg.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=false;this.mask=0;if(typeof numBytes!=="undefined"){this.numBytes=numBytes}};jpeg.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];jpeg.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];jpeg.lossless.Decoder.MAX_HUFFMAN_SUBTREE=50;jpeg.lossless.Decoder.MSB=2147483648;jpeg.lossless.Decoder.RESTART_MARKER_BEGIN=65488;jpeg.lossless.Decoder.RESTART_MARKER_END=65495;jpeg.lossless.Decoder.prototype.decompress=function(buffer,offset,length){return this.decode(buffer,offset,length).buffer};jpeg.lossless.Decoder.prototype.decode=function(buffer,offset,length,numBytes){var current,scanNum=0,pred=[],i,compN,temp=[],index=[],mcuNum;if(typeof buffer!=="undefined"){this.buffer=buffer}if(typeof numBytes!=="undefined"){this.numBytes=numBytes}this.stream=new jpeg.lossless.DataStream(this.buffer,offset,length);this.buffer=null;this.xLoc=0;this.yLoc=0;current=this.stream.get16();if(current!==65496){throw new Error("Not a JPEG file")}current=this.stream.get16();while(current>>4!==4092||current===65476){switch(current){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,jpeg.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(current>>8!==255){throw new Error("ERROR: format throw new IOException! (decode)")}}current=this.stream.get16()}if(current<65472||current>65479){throw new Error("ERROR: could not handle arithmetic code!")}this.frame.read(this.stream);current=this.stream.get16();do{while(current!==65498){switch(current){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,jpeg.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(current>>8!==255){throw new Error("ERROR: format throw new IOException! (Parser.decode)")}}current=this.stream.get16()}this.precision=this.frame.precision;this.components=this.frame.components;if(!this.numBytes){this.numBytes=parseInt(Math.ceil(this.precision/8))}if(this.numBytes==1){this.mask=255}else{this.mask=65535}this.scan.read(this.stream);this.numComp=this.scan.numComp;this.selection=this.scan.selection;if(this.numBytes===1){if(this.numComp===3){this.getter=this.getValueRGB;this.setter=this.setValueRGB;this.output=this.outputRGB}else{this.getter=this.getValue8;this.setter=this.setValue8;this.output=this.outputSingle}}else{this.getter=this.getValue16;this.setter=this.setValue16;this.output=this.outputSingle}switch(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;break}this.scanComps=this.scan.components;this.quantTables=this.quantTable.quantTables;for(i=0;i<this.numComp;i+=1){compN=this.scanComps[i].scanCompSel;this.qTab[i]=this.quantTables[this.components[compN].quantTableSel];this.nBlock[i]=this.components[compN].vSamp*this.components[compN].hSamp;this.dcTab[i]=this.HuffTab[this.scanComps[i].dcTabSel][0];this.acTab[i]=this.HuffTab[this.scanComps[i].acTabSel][1]}this.xDim=this.frame.dimX;this.yDim=this.frame.dimY;if(this.numBytes==1){this.outputData=new Uint8Array(new ArrayBuffer(this.xDim*this.yDim*this.numBytes*this.numComp))}else{this.outputData=new Uint16Array(new ArrayBuffer(this.xDim*this.yDim*this.numBytes*this.numComp))}scanNum+=1;while(true){temp[0]=0;index[0]=0;for(i=0;i<10;i+=1){pred[i]=1<<this.precision-1}if(this.restartInterval===0){current=this.decodeUnit(pred,temp,index);while(current===0&&(this.xLoc<this.xDim&&this.yLoc<this.yDim)){this.output(pred);current=this.decodeUnit(pred,temp,index)}break}for(mcuNum=0;mcuNum<this.restartInterval;mcuNum+=1){this.restarting=mcuNum==0;current=this.decodeUnit(pred,temp,index);this.output(pred);if(current!==0){break}}if(current===0){if(this.markerIndex!==0){current=65280|this.marker;this.markerIndex=0}else{current=this.stream.get16()}}if(!(current>=jpeg.lossless.Decoder.RESTART_MARKER_BEGIN&¤t<=jpeg.lossless.Decoder.RESTART_MARKER_END)){break}}if(current===65500&&scanNum===1){this.readNumber();current=this.stream.get16()}}while(current!==65497&&(this.xLoc<this.xDim&&this.yLoc<this.yDim)&&scanNum===0);return this.outputData};jpeg.lossless.Decoder.prototype.decodeUnit=function(prev,temp,index){if(this.numComp==1){return this.decodeSingle(prev,temp,index)}else if(this.numComp==3){return this.decodeRGB(prev,temp,index)}else{return-1}};jpeg.lossless.Decoder.prototype.select1=function(compOffset){return this.getPreviousX(compOffset)};jpeg.lossless.Decoder.prototype.select2=function(compOffset){return this.getPreviousY(compOffset)};jpeg.lossless.Decoder.prototype.select3=function(compOffset){return this.getPreviousXY(compOffset)};jpeg.lossless.Decoder.prototype.select4=function(compOffset){return this.getPreviousX(compOffset)+this.getPreviousY(compOffset)-this.getPreviousXY(compOffset)};jpeg.lossless.Decoder.prototype.select5=function(compOffset){return this.getPreviousX(compOffset)+(this.getPreviousY(compOffset)-this.getPreviousXY(compOffset)>>1)};jpeg.lossless.Decoder.prototype.select6=function(compOffset){return this.getPreviousY(compOffset)+(this.getPreviousX(compOffset)-this.getPreviousXY(compOffset)>>1)};jpeg.lossless.Decoder.prototype.select7=function(compOffset){return(this.getPreviousX(compOffset)+this.getPreviousY(compOffset))/2};jpeg.lossless.Decoder.prototype.decodeRGB=function(prev,temp,index){var value,actab,dctab,qtab,ctrC,i,k,j;prev[0]=this.selector(0);prev[1]=this.selector(1);prev[2]=this.selector(2);for(ctrC=0;ctrC<this.numComp;ctrC+=1){qtab=this.qTab[ctrC];actab=this.acTab[ctrC];dctab=this.dcTab[ctrC];for(i=0;i<this.nBlock[ctrC];i+=1){for(k=0;k<this.IDCT_Source.length;k+=1){this.IDCT_Source[k]=0}value=this.getHuffmanValue(dctab,temp,index);if(value>=65280){return value}prev[ctrC]=this.IDCT_Source[0]=prev[ctrC]+this.getn(index,value,temp,index);this.IDCT_Source[0]*=qtab[0];for(j=1;j<64;j+=1){value=this.getHuffmanValue(actab,temp,index);if(value>=65280){return value}j+=value>>4;if((value&15)===0){if(value>>4===0){break}}else{this.IDCT_Source[jpeg.lossless.Decoder.IDCT_P[j]]=this.getn(index,value&15,temp,index)*qtab[j]}}}}return 0};jpeg.lossless.Decoder.prototype.decodeSingle=function(prev,temp,index){var value,i,n,nRestart;if(this.restarting){this.restarting=false;prev[0]=1<<this.frame.precision-1}else{prev[0]=this.selector()}for(i=0;i<this.nBlock[0];i+=1){value=this.getHuffmanValue(this.dcTab[0],temp,index);if(value>=65280){return value}n=this.getn(prev,value,temp,index);nRestart=n>>8;if(nRestart>=jpeg.lossless.Decoder.RESTART_MARKER_BEGIN&&nRestart<=jpeg.lossless.Decoder.RESTART_MARKER_END){return nRestart}prev[0]+=n}return 0};jpeg.lossless.Decoder.prototype.getHuffmanValue=function(table,temp,index){var code,input,mask;mask=65535;if(index[0]<8){temp[0]<<=8;input=this.stream.get8();if(input===255){this.marker=this.stream.get8();if(this.marker!==0){this.markerIndex=9}}temp[0]|=input}else{index[0]-=8}code=table[temp[0]>>index[0]];if((code&jpeg.lossless.Decoder.MSB)!==0){if(this.markerIndex!==0){this.markerIndex=0;return 65280|this.marker}temp[0]&=mask>>16-index[0];temp[0]<<=8;input=this.stream.get8();if(input===255){this.marker=this.stream.get8();if(this.marker!==0){this.markerIndex=9}}temp[0]|=input;code=table[(code&255)*256+(temp[0]>>index[0])];index[0]+=8}index[0]+=8-(code>>8);if(index[0]<0){throw new Error("index="+index[0]+" temp="+temp[0]+" code="+code+" in HuffmanValue()")}if(index[0]<this.markerIndex){this.markerIndex=0;return 65280|this.marker}temp[0]&=mask>>16-index[0];return code&255};jpeg.lossless.Decoder.prototype.getn=function(PRED,n,temp,index){var result,one,n_one,mask,input;one=1;n_one=-1;mask=65535;if(n===0){return 0}if(n===16){if(PRED[0]>=0){return-32768}else{return 32768}}index[0]-=n;if(index[0]>=0){if(index[0]<this.markerIndex&&!this.isLastPixel()){this.markerIndex=0;return(65280|this.marker)<<8}result=temp[0]>>index[0];temp[0]&=mask>>16-index[0]}else{temp[0]<<=8;input=this.stream.get8();if(input===255){this.marker=this.stream.get8();if(this.marker!==0){this.markerIndex=9}}temp[0]|=input;index[0]+=8;if(index[0]<0){if(this.markerIndex!==0){this.markerIndex=0;return(65280|this.marker)<<8}temp[0]<<=8;input=this.stream.get8();if(input===255){this.marker=this.stream.get8();if(this.marker!==0){this.markerIndex=9}}temp[0]|=input;index[0]+=8}if(index[0]<0){throw new Error("index="+index[0]+" in getn()")}if(index[0]<this.markerIndex){this.markerIndex=0;return(65280|this.marker)<<8}result=temp[0]>>index[0];temp[0]&=mask>>16-index[0]}if(result<one<<n-1){result+=(n_one<<n)+1}return result};jpeg.lossless.Decoder.prototype.getPreviousX=function(compOffset){if(this.xLoc>0){return this.getter(this.yLoc*this.xDim+this.xLoc-1,compOffset)}else if(this.yLoc>0){return this.getPreviousY(compOffset)}else{return 1<<this.frame.precision-1}};jpeg.lossless.Decoder.prototype.getPreviousXY=function(compOffset){if(this.xLoc>0&&this.yLoc>0){return this.getter((this.yLoc-1)*this.xDim+this.xLoc-1,compOffset)}else{return this.getPreviousY(compOffset)}};jpeg.lossless.Decoder.prototype.getPreviousY=function(compOffset){if(this.yLoc>0){return this.getter((this.yLoc-1)*this.xDim+this.xLoc,compOffset)}else{return this.getPreviousX(compOffset)}};jpeg.lossless.Decoder.prototype.isLastPixel=function(){return this.xLoc===this.xDim-1&&this.yLoc===this.yDim-1};jpeg.lossless.Decoder.prototype.outputSingle=function(PRED){if(this.xLoc<this.xDim&&this.yLoc<this.yDim){this.setter(this.yLoc*this.xDim+this.xLoc,this.mask&PRED[0]);this.xLoc+=1;if(this.xLoc>=this.xDim){this.yLoc+=1;this.xLoc=0}}};jpeg.lossless.Decoder.prototype.outputRGB=function(PRED){var offset=this.yLoc*this.xDim+this.xLoc;if(this.xLoc<this.xDim&&this.yLoc<this.yDim){this.setter(offset,PRED[0],0);this.setter(offset,PRED[1],1);this.setter(offset,PRED[2],2);this.xLoc+=1;if(this.xLoc>=this.xDim){this.yLoc+=1;this.xLoc=0}}};jpeg.lossless.Decoder.prototype.setValue8=function(index,val){this.outputData[index]=val};jpeg.lossless.Decoder.prototype.getValue8=function(index){return this.outputData[index]};var littleEndian=function(){var buffer=new ArrayBuffer(2);new DataView(buffer).setInt16(0,256,true);return new Int16Array(buffer)[0]===256}();if(littleEndian){jpeg.lossless.Decoder.prototype.setValue16=jpeg.lossless.Decoder.prototype.setValue8;jpeg.lossless.Decoder.prototype.getValue16=jpeg.lossless.Decoder.prototype.getValue8}else{jpeg.lossless.Decoder.prototype.setValue16=function(index,val){this.outputData[index]=(val&255)<<8|val>>8&255};jpeg.lossless.Decoder.prototype.getValue16=function(index){var val=this.outputData[index];return(val&255)<<8|val>>8&255}}jpeg.lossless.Decoder.prototype.setValueRGB=function(index,val,compOffset){this.outputData[index*3+compOffset]=val};jpeg.lossless.Decoder.prototype.getValueRGB=function(index,compOffset){return this.outputData[index*3+compOffset]};jpeg.lossless.Decoder.prototype.readApp=function(){var count=0,length=this.stream.get16();count+=2;while(count<length){this.stream.get8();count+=1}return length};jpeg.lossless.Decoder.prototype.readComment=function(){var sb="",count=0,length;length=this.stream.get16();count+=2;while(count<length){sb+=this.stream.get8();count+=1}return sb};jpeg.lossless.Decoder.prototype.readNumber=function(){var Ld=this.stream.get16();if(Ld!==4){throw new Error("ERROR: Define number format throw new IOException [Ld!=4]")}return this.stream.get16()};var moduleType=typeof module;if(moduleType!=="undefined"&&module.exports){module.exports=jpeg.lossless.Decoder}},{"./data-stream.js":2,"./frame-header.js":4,"./huffman-table.js":5,"./quantization-table.js":7,"./scan-header.js":9,"./utils.js":10}],4:[function(require,module,exports){"use strict";var jpeg=jpeg||{};jpeg.lossless=jpeg.lossless||{};jpeg.lossless.ComponentSpec=jpeg.lossless.ComponentSpec||(typeof require!=="undefined"?require("./component-spec.js"):null);jpeg.lossless.DataStream=jpeg.lossless.DataStream||(typeof require!=="undefined"?require("./data-stream.js"):null);jpeg.lossless.FrameHeader=jpeg.lossless.FrameHeader||function(){this.components=[];this.dimX=0;this.dimY=0;this.numComp=0;this.precision=0};jpeg.lossless.FrameHeader.prototype.read=function(data){var count=0,length,i,c,temp;length=data.get16();count+=2;this.precision=data.get8();count+=1;this.dimY=data.get16();count+=2;this.dimX=data.get16();count+=2;this.numComp=data.get8();count+=1;for(i=1;i<=this.numComp;i+=1){if(count>length){throw new Error("ERROR: frame format error")}c=data.get8();count+=1;if(count>=length){throw new Error("ERROR: frame format error [c>=Lf]")}temp=data.get8();count+=1;if(!this.components[c]){this.components[c]=new jpeg.lossless.ComponentSpec}this.components[c].hSamp=temp>>4;this.components[c].vSamp=temp&15;this.components[c].quantTableSel=data.get8();count+=1}if(count!==length){throw new Error("ERROR: frame format error [Lf!=count]")}return 1};var moduleType=typeof module;if(moduleType!=="undefined"&&module.exports){module.exports=jpeg.lossless.FrameHeader}},{"./component-spec.js":1,"./data-stream.js":2}],5:[function(require,module,exports){"use strict";var jpeg=jpeg||{};jpeg.lossless=jpeg.lossless||{};jpeg.lossless.DataStream=jpeg.lossless.DataStream||(typeof require!=="undefined"?require("./data-stream.js"):null);jpeg.lossless.Utils=jpeg.lossless.Utils||(typeof require!=="undefined"?require("./utils.js"):null);jpeg.lossless.HuffmanTable=jpeg.lossless.HuffmanTable||function(){this.l=jpeg.lossless.Utils.createArray(4,2,16);this.th=[];this.v=jpeg.lossless.Utils.createArray(4,2,16,200);this.tc=jpeg.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};jpeg.lossless.HuffmanTable.MSB=2147483648;jpeg.lossless.HuffmanTable.prototype.read=function(data,HuffTab){var count=0,length,temp,t,c,i,j;length=data.get16();count+=2;while(count<length){temp=data.get8();count+=1;t=temp&15;if(t>3){throw new Error("ERROR: Huffman table ID > 3")}c=temp>>4;if(c>2){throw new Error("ERROR: Huffman table [Table class > 2 ]")}this.th[t]=1;this.tc[t][c]=1;for(i=0;i<16;i+=1){this.l[t][c][i]=data.get8();count+=1}for(i=0;i<16;i+=1){for(j=0;j<this.l[t][c][i];j+=1){if(count>length){throw new Error("ERROR: Huffman table format error [count>Lh]")}this.v[t][c][i][j]=data.get8();count+=1}}}if(count!==length){throw new Error("ERROR: Huffman table format error [count!=Lf]")}for(i=0;i<4;i+=1){for(j=0;j<2;j+=1){if(this.tc[i][j]!==0){this.buildHuffTable(HuffTab[i][j],this.l[i][j],this.v[i][j])}}}return 1};jpeg.lossless.HuffmanTable.prototype.buildHuffTable=function(tab,L,V){var currentTable,temp,k,i,j,n;temp=256;k=0;for(i=0;i<8;i+=1){for(j=0;j<L[i];j+=1){for(n=0;n<temp>>i+1;n+=1){tab[k]=V[i][j]|i+1<<8;k+=1}}}for(i=1;k<256;i+=1,k+=1){tab[k]=i|jpeg.lossless.HuffmanTable.MSB}currentTable=1;k=0;for(i=8;i<16;i+=1){for(j=0;j<L[i];j+=1){for(n=0;n<temp>>i-7;n+=1){tab[currentTable*256+k]=V[i][j]|i+1<<8;k+=1}if(k>=256){if(k>256){throw new Error("ERROR: Huffman table error(1)!")}k=0;currentTable+=1}}}};var moduleType=typeof module;if(moduleType!=="undefined"&&module.exports){module.exports=jpeg.lossless.HuffmanTable}},{"./data-stream.js":2,"./utils.js":10}],6:[function(require,module,exports){"use strict";var jpeg=jpeg||{};jpeg.lossless=jpeg.lossless||{};jpeg.lossless.ComponentSpec=jpeg.lossless.ComponentSpec||(typeof require!=="undefined"?require("./component-spec.js"):null);jpeg.lossless.DataStream=jpeg.lossless.DataStream||(typeof require!=="undefined"?require("./data-stream.js"):null);jpeg.lossless.Decoder=jpeg.lossless.Decoder||(typeof require!=="undefined"?require("./decoder.js"):null);jpeg.lossless.FrameHeader=jpeg.lossless.FrameHeader||(typeof require!=="undefined"?require("./frame-header.js"):null);jpeg.lossless.HuffmanTable=jpeg.lossless.HuffmanTable||(typeof require!=="undefined"?require("./huffman-table.js"):null);jpeg.lossless.QuantizationTable=jpeg.lossless.QuantizationTable||(typeof require!=="undefined"?require("./quantization-table.js"):null);jpeg.lossless.ScanComponent=jpeg.lossless.ScanComponent||(typeof require!=="undefined"?require("./scan-component.js"):null);jpeg.lossless.ScanHeader=jpeg.lossless.ScanHeader||(typeof require!=="undefined"?require("./scan-header.js"):null);jpeg.lossless.Utils=jpeg.lossless.Utils||(typeof require!=="undefined"?require("./utils.js"):null);var moduleType=typeof module;if(moduleType!=="undefined"&&module.exports){module.exports=jpeg}},{"./component-spec.js":1,"./data-stream.js":2,"./decoder.js":3,"./frame-header.js":4,"./huffman-table.js":5,"./quantization-table.js":7,"./scan-component.js":8,"./scan-header.js":9,"./utils.js":10}],7:[function(require,module,exports){"use strict";var jpeg=jpeg||{};jpeg.lossless=jpeg.lossless||{};jpeg.lossless.DataStream=jpeg.lossless.DataStream||(typeof require!=="undefined"?require("./data-stream.js"):null);jpeg.lossless.Utils=jpeg.lossless.Utils||(typeof require!=="undefined"?require("./utils.js"):null);jpeg.lossless.QuantizationTable=jpeg.lossless.QuantizationTable||function(){this.precision=[];this.tq=[];this.quantTables=jpeg.lossless.Utils.createArray(4,64);this.tq[0]=0;this.tq[1]=0;this.tq[2]=0;this.tq[3]=0};jpeg.lossless.QuantizationTable.enhanceQuantizationTable=function(qtab,table){var i;for(i=0;i<8;i+=1){qtab[table[0*8+i]]*=90;qtab[table[4*8+i]]*=90;qtab[table[2*8+i]]*=118;qtab[table[6*8+i]]*=49;qtab[table[5*8+i]]*=71;qtab[table[1*8+i]]*=126;qtab[table[7*8+i]]*=25;qtab[table[3*8+i]]*=106}for(i=0;i<8;i+=1){qtab[table[0+8*i]]*=90;qtab[table[4+8*i]]*=90;qtab[table[2+8*i]]*=118;qtab[table[6+8*i]]*=49;qtab[table[5+8*i]]*=71;qtab[table[1+8*i]]*=126;qtab[table[7+8*i]]*=25;qtab[table[3+8*i]]*=106}for(i=0;i<64;i+=1){qtab[i]>>=6}};jpeg.lossless.QuantizationTable.prototype.read=function(data,table){var count=0,length,temp,t,i;length=data.get16();count+=2;while(count<length){temp=data.get8();count+=1;t=temp&15;if(t>3){throw new Error("ERROR: Quantization table ID > 3")}this.precision[t]=temp>>4;if(this.precision[t]===0){this.precision[t]=8}else if(this.precision[t]===1){this.precision[t]=16}else{throw new Error("ERROR: Quantization table precision error")}this.tq[t]=1;if(this.precision[t]===8){for(i=0;i<64;i+=1){if(count>length){throw new Error("ERROR: Quantization table format error")}this.quantTables[t][i]=data.get8();count+=1}jpeg.lossless.QuantizationTable.enhanceQuantizationTable(this.quantTables[t],table)}else{for(i=0;i<64;i+=1){if(count>length){throw new Error("ERROR: Quantization table format error")}this.quantTables[t][i]=data.get16();count+=2}jpeg.lossless.QuantizationTable.enhanceQuantizationTable(this.quantTables[t],table)}}if(count!==length){throw new Error("ERROR: Quantization table error [count!=Lq]")}return 1};var moduleType=typeof module;if(moduleType!=="undefined"&&module.exports){module.exports=jpeg.lossless.QuantizationTable}},{"./data-stream.js":2,"./utils.js":10}],8:[function(require,module,exports){"use strict";var jpeg=jpeg||{};jpeg.lossless=jpeg.lossless||{};jpeg.lossless.ScanComponent=jpeg.lossless.ScanComponent||function(){this.acTabSel=0;this.dcTabSel=0;this.scanCompSel=0};var moduleType=typeof module;if(moduleType!=="undefined"&&module.exports){module.exports=jpeg.lossless.ScanComponent}},{}],9:[function(require,module,exports){"use strict";var jpeg=jpeg||{};jpeg.lossless=jpeg.lossless||{};jpeg.lossless.DataStream=jpeg.lossless.DataStream||(typeof require!=="undefined"?require("./data-stream.js"):null);jpeg.lossless.ScanComponent=jpeg.lossless.ScanComponent||(typeof require!=="undefined"?require("./scan-component.js"):null);jpeg.lossless.ScanHeader=jpeg.lossless.ScanHeader||function(){this.ah=0;this.al=0;this.numComp=0;this.selection=0;this.spectralEnd=0;this.components=[]};jpeg.lossless.ScanHeader.prototype.read=function(data){var count=0,length,i,temp;length=data.get16();count+=2;this.numComp=data.get8();count+=1;for(i=0;i<this.numComp;i+=1){this.components[i]=new jpeg.lossless.ScanComponent;if(count>length){throw new Error("ERROR: scan header format error")}this.components[i].scanCompSel=data.get8();count+=1;temp=data.get8();count+=1;this.components[i].dcTabSel=temp>>4;this.components[i].acTabSel=temp&15}this.selection=data.get8();count+=1;this.spectralEnd=data.get8();count+=1;temp=data.get8();this.ah=temp>>4;this.al=temp&15;count+=1;if(count!==length){throw new Error("ERROR: scan header format error [count!=Ns]")}return 1};var moduleType=typeof module;if(moduleType!=="undefined"&&module.exports){module.exports=jpeg.lossless.ScanHeader}},{"./data-stream.js":2,"./scan-component.js":8}],10:[function(require,module,exports){"use strict";var jpeg=jpeg||{};jpeg.lossless=jpeg.lossless||{};jpeg.lossless.Utils=jpeg.lossless.Utils||{};jpeg.lossless.Utils.createArray=function(length){var arr=new Array(length||0),i=length;if(arguments.length>1){var args=Array.prototype.slice.call(arguments,1);while(i--)arr[length-1-i]=jpeg.lossless.Utils.createArray.apply(this,args)}return arr};jpeg.lossless.Utils.makeCRCTable=function(){var c;var crcTable=[];for(var n=0;n<256;n++){c=n;for(var k=0;k<8;k++){c=c&1?3988292384^c>>>1:c>>>1}crcTable[n]=c}return crcTable};jpeg.lossless.Utils.crc32=function(dataView){var uint8view=new Uint8Array(dataView.buffer);var crcTable=jpeg.lossless.Utils.crcTable||(jpeg.lossless.Utils.crcTable=jpeg.lossless.Utils.makeCRCTable());var crc=0^-1;for(var i=0;i<uint8view.length;i++){crc=crc>>>8^crcTable[(crc^uint8view[i])&255]}return(crc^-1)>>>0};var moduleType=typeof module;if(moduleType!=="undefined"&&module.exports){module.exports=jpeg.lossless.Utils}},{}]},{},[6])(6)}); |
@@ -0,783 +1,431 @@ | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.jpeg = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | ||
/* | ||
* Copyright (C) 2015 Michael Martinez | ||
* Changes: Added support for selection values 2-7, fixed minor bugs & | ||
* warnings, split into multiple class files, and general clean up. | ||
* | ||
* 08-25-2015: Helmut Dersch agreed to a license change from LGPL to MIT. | ||
*/ | ||
/* | ||
* Copyright (C) Helmut Dersch | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
/*jslint browser: true, node: true */ | ||
/*global require, module */ | ||
"use strict"; | ||
var jpeg = (() => { | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, { | ||
get: (a, b) => (typeof require !== "undefined" ? require : a)[b] | ||
}) : x)(function(x) { | ||
if (typeof require !== "undefined") | ||
return require.apply(this, arguments); | ||
throw new Error('Dynamic require of "' + x + '" is not supported'); | ||
}); | ||
var __commonJS = (cb, mod) => function __require2() { | ||
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; | ||
}; | ||
// src/component-spec.js | ||
var require_component_spec = __commonJS({ | ||
"src/component-spec.js"(exports, module) { | ||
"use strict"; | ||
var jpeg = jpeg || {}; | ||
jpeg.lossless = jpeg.lossless || {}; | ||
jpeg.lossless.ComponentSpec = jpeg.lossless.ComponentSpec || function() { | ||
this.hSamp = 0; | ||
this.quantTableSel = 0; | ||
this.vSamp = 0; | ||
}; | ||
var moduleType = typeof module; | ||
if (moduleType !== "undefined" && module.exports) { | ||
module.exports = jpeg.lossless.ComponentSpec; | ||
} | ||
} | ||
}); | ||
/*** Imports ***/ | ||
var jpeg = jpeg || {}; | ||
jpeg.lossless = jpeg.lossless || {}; | ||
// src/data-stream.js | ||
var require_data_stream = __commonJS({ | ||
"src/data-stream.js"(exports, module) { | ||
"use strict"; | ||
var jpeg = jpeg || {}; | ||
jpeg.lossless = jpeg.lossless || {}; | ||
jpeg.lossless.DataStream = jpeg.lossless.DataStream || function(data, offset, length) { | ||
if (offset === void 0 && length === void 0) { | ||
this.buffer = new Uint8Array(data); | ||
} else { | ||
this.buffer = new Uint8Array(data, offset, length); | ||
} | ||
this.index = 0; | ||
}; | ||
jpeg.lossless.DataStream.prototype.get16 = function() { | ||
var value = (this.buffer[this.index] << 8) + this.buffer[this.index + 1]; | ||
this.index += 2; | ||
return value; | ||
}; | ||
jpeg.lossless.DataStream.prototype.get8 = function() { | ||
var value = this.buffer[this.index]; | ||
this.index += 1; | ||
return value; | ||
}; | ||
var moduleType = typeof module; | ||
if (moduleType !== "undefined" && module.exports) { | ||
module.exports = jpeg.lossless.DataStream; | ||
} | ||
} | ||
}); | ||
// src/utils.js | ||
var require_utils = __commonJS({ | ||
"src/utils.js"(exports, module) { | ||
"use strict"; | ||
var jpeg = jpeg || {}; | ||
jpeg.lossless = jpeg.lossless || {}; | ||
jpeg.lossless.Utils = jpeg.lossless.Utils || {}; | ||
jpeg.lossless.Utils.createArray = function(length) { | ||
var arr = new Array(length || 0), i = length; | ||
if (arguments.length > 1) { | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
while (i--) | ||
arr[length - 1 - i] = jpeg.lossless.Utils.createArray.apply(this, args); | ||
} | ||
return arr; | ||
}; | ||
jpeg.lossless.Utils.makeCRCTable = function() { | ||
var c; | ||
var crcTable = []; | ||
for (var n = 0; n < 256; n++) { | ||
c = n; | ||
for (var k = 0; k < 8; k++) { | ||
c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1; | ||
} | ||
crcTable[n] = c; | ||
} | ||
return crcTable; | ||
}; | ||
jpeg.lossless.Utils.crc32 = function(dataView) { | ||
var uint8view = new Uint8Array(dataView.buffer); | ||
var crcTable = jpeg.lossless.Utils.crcTable || (jpeg.lossless.Utils.crcTable = jpeg.lossless.Utils.makeCRCTable()); | ||
var crc = 0 ^ -1; | ||
for (var i = 0; i < uint8view.length; i++) { | ||
crc = crc >>> 8 ^ crcTable[(crc ^ uint8view[i]) & 255]; | ||
} | ||
return (crc ^ -1) >>> 0; | ||
}; | ||
var moduleType = typeof module; | ||
if (moduleType !== "undefined" && module.exports) { | ||
module.exports = jpeg.lossless.Utils; | ||
} | ||
} | ||
}); | ||
/*** Constructor ***/ | ||
jpeg.lossless.ComponentSpec = jpeg.lossless.ComponentSpec || function () { | ||
this.hSamp = 0; // Horizontal sampling factor | ||
this.quantTableSel = 0; // Quantization table destination selector | ||
this.vSamp = 0; // Vertical | ||
}; | ||
// src/huffman-table.js | ||
var require_huffman_table = __commonJS({ | ||
"src/huffman-table.js"(exports, module) { | ||
"use strict"; | ||
var jpeg = jpeg || {}; | ||
jpeg.lossless = jpeg.lossless || {}; | ||
jpeg.lossless.DataStream = jpeg.lossless.DataStream || (typeof __require !== "undefined" ? require_data_stream() : null); | ||
jpeg.lossless.Utils = jpeg.lossless.Utils || (typeof __require !== "undefined" ? require_utils() : null); | ||
jpeg.lossless.HuffmanTable = jpeg.lossless.HuffmanTable || function() { | ||
this.l = jpeg.lossless.Utils.createArray(4, 2, 16); | ||
this.th = []; | ||
this.v = jpeg.lossless.Utils.createArray(4, 2, 16, 200); | ||
this.tc = jpeg.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; | ||
}; | ||
jpeg.lossless.HuffmanTable.MSB = 2147483648; | ||
jpeg.lossless.HuffmanTable.prototype.read = function(data, HuffTab) { | ||
var count = 0, length, temp, t, c, i, j; | ||
length = data.get16(); | ||
count += 2; | ||
while (count < length) { | ||
temp = data.get8(); | ||
count += 1; | ||
t = temp & 15; | ||
if (t > 3) { | ||
throw new Error("ERROR: Huffman table ID > 3"); | ||
} | ||
c = temp >> 4; | ||
if (c > 2) { | ||
throw new Error("ERROR: Huffman table [Table class > 2 ]"); | ||
} | ||
this.th[t] = 1; | ||
this.tc[t][c] = 1; | ||
for (i = 0; i < 16; i += 1) { | ||
this.l[t][c][i] = data.get8(); | ||
count += 1; | ||
} | ||
for (i = 0; i < 16; i += 1) { | ||
for (j = 0; j < this.l[t][c][i]; j += 1) { | ||
if (count > length) { | ||
throw new Error("ERROR: Huffman table format error [count>Lh]"); | ||
} | ||
this.v[t][c][i][j] = data.get8(); | ||
count += 1; | ||
} | ||
} | ||
} | ||
if (count !== length) { | ||
throw new Error("ERROR: Huffman table format error [count!=Lf]"); | ||
} | ||
for (i = 0; i < 4; i += 1) { | ||
for (j = 0; j < 2; j += 1) { | ||
if (this.tc[i][j] !== 0) { | ||
this.buildHuffTable(HuffTab[i][j], this.l[i][j], this.v[i][j]); | ||
} | ||
} | ||
} | ||
return 1; | ||
}; | ||
jpeg.lossless.HuffmanTable.prototype.buildHuffTable = function(tab, L, V) { | ||
var currentTable, temp, k, i, j, n; | ||
temp = 256; | ||
k = 0; | ||
for (i = 0; i < 8; i += 1) { | ||
for (j = 0; j < L[i]; j += 1) { | ||
for (n = 0; n < temp >> i + 1; n += 1) { | ||
tab[k] = V[i][j] | i + 1 << 8; | ||
k += 1; | ||
} | ||
} | ||
} | ||
for (i = 1; k < 256; i += 1, k += 1) { | ||
tab[k] = i | jpeg.lossless.HuffmanTable.MSB; | ||
} | ||
currentTable = 1; | ||
k = 0; | ||
for (i = 8; i < 16; i += 1) { | ||
for (j = 0; j < L[i]; j += 1) { | ||
for (n = 0; n < temp >> i - 7; n += 1) { | ||
tab[currentTable * 256 + k] = V[i][j] | i + 1 << 8; | ||
k += 1; | ||
} | ||
if (k >= 256) { | ||
if (k > 256) { | ||
throw new Error("ERROR: Huffman table error(1)!"); | ||
} | ||
k = 0; | ||
currentTable += 1; | ||
} | ||
} | ||
} | ||
}; | ||
var moduleType = typeof module; | ||
if (moduleType !== "undefined" && module.exports) { | ||
module.exports = jpeg.lossless.HuffmanTable; | ||
} | ||
/*** Exports ***/ | ||
var moduleType = typeof module; | ||
if ((moduleType !== 'undefined') && module.exports) { | ||
module.exports = jpeg.lossless.ComponentSpec; | ||
} | ||
},{}],2:[function(require,module,exports){ | ||
/* | ||
* Copyright (C) 2015 Michael Martinez | ||
* Changes: Added support for selection values 2-7, fixed minor bugs & | ||
* warnings, split into multiple class files, and general clean up. | ||
* | ||
* 08-25-2015: Helmut Dersch agreed to a license change from LGPL to MIT. | ||
*/ | ||
/* | ||
* Copyright (C) Helmut Dersch | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
/*jslint browser: true, node: true */ | ||
/*global require, module */ | ||
"use strict"; | ||
/*** Imports ***/ | ||
var jpeg = jpeg || {}; | ||
jpeg.lossless = jpeg.lossless || {}; | ||
/*** Constructor ***/ | ||
jpeg.lossless.DataStream = jpeg.lossless.DataStream || function (data, offset, length) { | ||
if (offset === undefined && length === undefined) { // Old api | ||
this.buffer = new Uint8Array(data); | ||
} else { | ||
this.buffer = new Uint8Array(data, offset, length); | ||
} | ||
}); | ||
this.index = 0; | ||
}; | ||
// src/quantization-table.js | ||
var require_quantization_table = __commonJS({ | ||
"src/quantization-table.js"(exports, module) { | ||
"use strict"; | ||
var jpeg = jpeg || {}; | ||
jpeg.lossless = jpeg.lossless || {}; | ||
jpeg.lossless.DataStream = jpeg.lossless.DataStream || (typeof __require !== "undefined" ? require_data_stream() : null); | ||
jpeg.lossless.Utils = jpeg.lossless.Utils || (typeof __require !== "undefined" ? require_utils() : null); | ||
jpeg.lossless.QuantizationTable = jpeg.lossless.QuantizationTable || function() { | ||
this.precision = []; | ||
this.tq = []; | ||
this.quantTables = jpeg.lossless.Utils.createArray(4, 64); | ||
this.tq[0] = 0; | ||
this.tq[1] = 0; | ||
this.tq[2] = 0; | ||
this.tq[3] = 0; | ||
}; | ||
jpeg.lossless.QuantizationTable.enhanceQuantizationTable = function(qtab, table) { | ||
var i; | ||
for (i = 0; i < 8; i += 1) { | ||
qtab[table[0 * 8 + i]] *= 90; | ||
qtab[table[4 * 8 + i]] *= 90; | ||
qtab[table[2 * 8 + i]] *= 118; | ||
qtab[table[6 * 8 + i]] *= 49; | ||
qtab[table[5 * 8 + i]] *= 71; | ||
qtab[table[1 * 8 + i]] *= 126; | ||
qtab[table[7 * 8 + i]] *= 25; | ||
qtab[table[3 * 8 + i]] *= 106; | ||
} | ||
for (i = 0; i < 8; i += 1) { | ||
qtab[table[0 + 8 * i]] *= 90; | ||
qtab[table[4 + 8 * i]] *= 90; | ||
qtab[table[2 + 8 * i]] *= 118; | ||
qtab[table[6 + 8 * i]] *= 49; | ||
qtab[table[5 + 8 * i]] *= 71; | ||
qtab[table[1 + 8 * i]] *= 126; | ||
qtab[table[7 + 8 * i]] *= 25; | ||
qtab[table[3 + 8 * i]] *= 106; | ||
} | ||
for (i = 0; i < 64; i += 1) { | ||
qtab[i] >>= 6; | ||
} | ||
}; | ||
jpeg.lossless.QuantizationTable.prototype.read = function(data, table) { | ||
var count = 0, length, temp, t, i; | ||
length = data.get16(); | ||
count += 2; | ||
while (count < length) { | ||
temp = data.get8(); | ||
count += 1; | ||
t = temp & 15; | ||
if (t > 3) { | ||
throw new Error("ERROR: Quantization table ID > 3"); | ||
} | ||
this.precision[t] = temp >> 4; | ||
if (this.precision[t] === 0) { | ||
this.precision[t] = 8; | ||
} else if (this.precision[t] === 1) { | ||
this.precision[t] = 16; | ||
} else { | ||
throw new Error("ERROR: Quantization table precision error"); | ||
} | ||
this.tq[t] = 1; | ||
if (this.precision[t] === 8) { | ||
for (i = 0; i < 64; i += 1) { | ||
if (count > length) { | ||
throw new Error("ERROR: Quantization table format error"); | ||
} | ||
this.quantTables[t][i] = data.get8(); | ||
count += 1; | ||
} | ||
jpeg.lossless.QuantizationTable.enhanceQuantizationTable(this.quantTables[t], table); | ||
} else { | ||
for (i = 0; i < 64; i += 1) { | ||
if (count > length) { | ||
throw new Error("ERROR: Quantization table format error"); | ||
} | ||
this.quantTables[t][i] = data.get16(); | ||
count += 2; | ||
} | ||
jpeg.lossless.QuantizationTable.enhanceQuantizationTable(this.quantTables[t], table); | ||
} | ||
} | ||
if (count !== length) { | ||
throw new Error("ERROR: Quantization table error [count!=Lq]"); | ||
} | ||
return 1; | ||
}; | ||
var moduleType = typeof module; | ||
if (moduleType !== "undefined" && module.exports) { | ||
module.exports = jpeg.lossless.QuantizationTable; | ||
} | ||
jpeg.lossless.DataStream.prototype.get16 = function () { | ||
// var value = this.buffer.getUint16(this.index, false); | ||
var value = (this.buffer[this.index] << 8) + this.buffer[this.index + 1]; // DataView is big-endian by default | ||
this.index += 2; | ||
return value; | ||
}; | ||
jpeg.lossless.DataStream.prototype.get8 = function () { | ||
// var value = this.buffer.getUint8(this.index); | ||
var value = this.buffer[this.index]; | ||
this.index += 1; | ||
return value; | ||
}; | ||
/*** Exports ***/ | ||
var moduleType = typeof module; | ||
if ((moduleType !== 'undefined') && module.exports) { | ||
module.exports = jpeg.lossless.DataStream; | ||
} | ||
},{}],3:[function(require,module,exports){ | ||
/* | ||
* Copyright (C) 2015 Michael Martinez | ||
* Changes: Added support for selection values 2-7, fixed minor bugs & | ||
* warnings, split into multiple class files, and general clean up. | ||
* | ||
* 08-25-2015: Helmut Dersch agreed to a license change from LGPL to MIT. | ||
*/ | ||
/* | ||
* Copyright (C) Helmut Dersch | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
/*jslint browser: true, node: true */ | ||
/*global require, module */ | ||
"use strict"; | ||
/*** Imports ***/ | ||
var jpeg = jpeg || {}; | ||
jpeg.lossless = jpeg.lossless || {}; | ||
jpeg.lossless.DataStream = jpeg.lossless.DataStream || ((typeof require !== 'undefined') ? require('./data-stream.js') : null); | ||
jpeg.lossless.HuffmanTable = jpeg.lossless.HuffmanTable || ((typeof require !== 'undefined') ? require('./huffman-table.js') : null); | ||
jpeg.lossless.QuantizationTable = jpeg.lossless.QuantizationTable || ((typeof require !== 'undefined') ? require('./quantization-table.js') : null); | ||
jpeg.lossless.ScanHeader = jpeg.lossless.ScanHeader || ((typeof require !== 'undefined') ? require('./scan-header.js') : null); | ||
jpeg.lossless.FrameHeader = jpeg.lossless.FrameHeader || ((typeof require !== 'undefined') ? require('./frame-header.js') : null); | ||
jpeg.lossless.Utils = jpeg.lossless.Utils || ((typeof require !== 'undefined') ? require('./utils.js') : null); | ||
/*** Constructor ***/ | ||
/** | ||
* The Decoder constructor. | ||
* @property {number} xDim - size of x dimension | ||
* @property {number} yDim - size of y dimension | ||
* @property {number} numComp - number of components | ||
* @property {number} numBytes - number of bytes per component | ||
* @type {Function} | ||
*/ | ||
jpeg.lossless.Decoder = jpeg.lossless.Decoder || function (buffer, numBytes) { | ||
this.buffer = buffer; | ||
this.frame = new jpeg.lossless.FrameHeader(); | ||
this.huffTable = new jpeg.lossless.HuffmanTable(); | ||
this.quantTable = new jpeg.lossless.QuantizationTable(); | ||
this.scan = new jpeg.lossless.ScanHeader(); | ||
this.DU = jpeg.lossless.Utils.createArray(10, 4, 64); // at most 10 data units in a MCU, at most 4 data units in one component | ||
this.HuffTab = jpeg.lossless.Utils.createArray(4, 2, 50 * 256); | ||
this.IDCT_Source = []; | ||
this.nBlock = []; // number of blocks in the i-th Comp in a scan | ||
this.acTab = jpeg.lossless.Utils.createArray(10, 1); // ac HuffTab for the i-th Comp in a scan | ||
this.dcTab = jpeg.lossless.Utils.createArray(10, 1); // dc HuffTab for the i-th Comp in a scan | ||
this.qTab = jpeg.lossless.Utils.createArray(10, 1); // quantization table for the i-th Comp in a scan | ||
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 = false; | ||
this.mask = 0; | ||
if (typeof numBytes !== "undefined") { | ||
this.numBytes = numBytes; | ||
} | ||
}); | ||
}; | ||
// src/scan-component.js | ||
var require_scan_component = __commonJS({ | ||
"src/scan-component.js"(exports, module) { | ||
"use strict"; | ||
var jpeg = jpeg || {}; | ||
jpeg.lossless = jpeg.lossless || {}; | ||
jpeg.lossless.ScanComponent = jpeg.lossless.ScanComponent || function() { | ||
this.acTabSel = 0; | ||
this.dcTabSel = 0; | ||
this.scanCompSel = 0; | ||
}; | ||
var moduleType = typeof module; | ||
if (moduleType !== "undefined" && module.exports) { | ||
module.exports = jpeg.lossless.ScanComponent; | ||
} | ||
/*** Static Pseudo-constants ***/ | ||
jpeg.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]; | ||
jpeg.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]; | ||
jpeg.lossless.Decoder.MAX_HUFFMAN_SUBTREE = 50; | ||
jpeg.lossless.Decoder.MSB = 0x80000000; | ||
jpeg.lossless.Decoder.RESTART_MARKER_BEGIN = 0xFFD0; | ||
jpeg.lossless.Decoder.RESTART_MARKER_END = 0xFFD7; | ||
/*** Prototype Methods ***/ | ||
/** | ||
* Returns decompressed data. | ||
* @param {ArrayBuffer} buffer | ||
* @param {number} [offset] | ||
* @param {number} [length] | ||
* @returns {ArrayBufer} | ||
*/ | ||
jpeg.lossless.Decoder.prototype.decompress = function (buffer, offset, length) { | ||
return this.decode(buffer, offset, length).buffer; | ||
}; | ||
jpeg.lossless.Decoder.prototype.decode = function (buffer, offset, length, numBytes) { | ||
/*jslint bitwise: true */ | ||
var current, scanNum = 0, pred = [], i, compN, temp = [], index = [], mcuNum; | ||
if (typeof buffer !== "undefined") { | ||
this.buffer = buffer; | ||
} | ||
}); | ||
// src/scan-header.js | ||
var require_scan_header = __commonJS({ | ||
"src/scan-header.js"(exports, module) { | ||
"use strict"; | ||
var jpeg = jpeg || {}; | ||
jpeg.lossless = jpeg.lossless || {}; | ||
jpeg.lossless.DataStream = jpeg.lossless.DataStream || (typeof __require !== "undefined" ? require_data_stream() : null); | ||
jpeg.lossless.ScanComponent = jpeg.lossless.ScanComponent || (typeof __require !== "undefined" ? require_scan_component() : null); | ||
jpeg.lossless.ScanHeader = jpeg.lossless.ScanHeader || function() { | ||
this.ah = 0; | ||
this.al = 0; | ||
this.numComp = 0; | ||
this.selection = 0; | ||
this.spectralEnd = 0; | ||
this.components = []; | ||
}; | ||
jpeg.lossless.ScanHeader.prototype.read = function(data) { | ||
var count = 0, length, i, temp; | ||
length = data.get16(); | ||
count += 2; | ||
this.numComp = data.get8(); | ||
count += 1; | ||
for (i = 0; i < this.numComp; i += 1) { | ||
this.components[i] = new jpeg.lossless.ScanComponent(); | ||
if (count > length) { | ||
throw new Error("ERROR: scan header format error"); | ||
} | ||
this.components[i].scanCompSel = data.get8(); | ||
count += 1; | ||
temp = data.get8(); | ||
count += 1; | ||
this.components[i].dcTabSel = temp >> 4; | ||
this.components[i].acTabSel = temp & 15; | ||
} | ||
this.selection = data.get8(); | ||
count += 1; | ||
this.spectralEnd = data.get8(); | ||
count += 1; | ||
temp = data.get8(); | ||
this.ah = temp >> 4; | ||
this.al = temp & 15; | ||
count += 1; | ||
if (count !== length) { | ||
throw new Error("ERROR: scan header format error [count!=Ns]"); | ||
} | ||
return 1; | ||
}; | ||
var moduleType = typeof module; | ||
if (moduleType !== "undefined" && module.exports) { | ||
module.exports = jpeg.lossless.ScanHeader; | ||
} | ||
if (typeof numBytes !== "undefined") { | ||
this.numBytes = numBytes; | ||
} | ||
}); | ||
// src/frame-header.js | ||
var require_frame_header = __commonJS({ | ||
"src/frame-header.js"(exports, module) { | ||
"use strict"; | ||
var jpeg = jpeg || {}; | ||
jpeg.lossless = jpeg.lossless || {}; | ||
jpeg.lossless.ComponentSpec = jpeg.lossless.ComponentSpec || (typeof __require !== "undefined" ? require_component_spec() : null); | ||
jpeg.lossless.DataStream = jpeg.lossless.DataStream || (typeof __require !== "undefined" ? require_data_stream() : null); | ||
jpeg.lossless.FrameHeader = jpeg.lossless.FrameHeader || function() { | ||
this.components = []; | ||
this.dimX = 0; | ||
this.dimY = 0; | ||
this.numComp = 0; | ||
this.precision = 0; | ||
}; | ||
jpeg.lossless.FrameHeader.prototype.read = function(data) { | ||
var count = 0, length, i, c, temp; | ||
length = data.get16(); | ||
count += 2; | ||
this.precision = data.get8(); | ||
count += 1; | ||
this.dimY = data.get16(); | ||
count += 2; | ||
this.dimX = data.get16(); | ||
count += 2; | ||
this.numComp = data.get8(); | ||
count += 1; | ||
for (i = 1; i <= this.numComp; i += 1) { | ||
if (count > length) { | ||
throw new Error("ERROR: frame format error"); | ||
} | ||
c = data.get8(); | ||
count += 1; | ||
if (count >= length) { | ||
throw new Error("ERROR: frame format error [c>=Lf]"); | ||
} | ||
temp = data.get8(); | ||
count += 1; | ||
if (!this.components[c]) { | ||
this.components[c] = new jpeg.lossless.ComponentSpec(); | ||
} | ||
this.components[c].hSamp = temp >> 4; | ||
this.components[c].vSamp = temp & 15; | ||
this.components[c].quantTableSel = data.get8(); | ||
count += 1; | ||
} | ||
if (count !== length) { | ||
throw new Error("ERROR: frame format error [Lf!=count]"); | ||
} | ||
return 1; | ||
}; | ||
var moduleType = typeof module; | ||
if (moduleType !== "undefined" && module.exports) { | ||
module.exports = jpeg.lossless.FrameHeader; | ||
} | ||
this.stream = new jpeg.lossless.DataStream(this.buffer, offset, length); | ||
this.buffer = null; | ||
this.xLoc = 0; | ||
this.yLoc = 0; | ||
current = this.stream.get16(); | ||
if (current !== 0xFFD8) { // SOI | ||
throw new Error("Not a JPEG file"); | ||
} | ||
}); | ||
// src/decoder.js | ||
var require_decoder = __commonJS({ | ||
"src/decoder.js"(exports, module) { | ||
"use strict"; | ||
var jpeg = jpeg || {}; | ||
jpeg.lossless = jpeg.lossless || {}; | ||
jpeg.lossless.DataStream = jpeg.lossless.DataStream || (typeof __require !== "undefined" ? require_data_stream() : null); | ||
jpeg.lossless.HuffmanTable = jpeg.lossless.HuffmanTable || (typeof __require !== "undefined" ? require_huffman_table() : null); | ||
jpeg.lossless.QuantizationTable = jpeg.lossless.QuantizationTable || (typeof __require !== "undefined" ? require_quantization_table() : null); | ||
jpeg.lossless.ScanHeader = jpeg.lossless.ScanHeader || (typeof __require !== "undefined" ? require_scan_header() : null); | ||
jpeg.lossless.FrameHeader = jpeg.lossless.FrameHeader || (typeof __require !== "undefined" ? require_frame_header() : null); | ||
jpeg.lossless.Utils = jpeg.lossless.Utils || (typeof __require !== "undefined" ? require_utils() : null); | ||
jpeg.lossless.Decoder = jpeg.lossless.Decoder || function(buffer, numBytes) { | ||
this.buffer = buffer; | ||
this.frame = new jpeg.lossless.FrameHeader(); | ||
this.huffTable = new jpeg.lossless.HuffmanTable(); | ||
this.quantTable = new jpeg.lossless.QuantizationTable(); | ||
this.scan = new jpeg.lossless.ScanHeader(); | ||
this.DU = jpeg.lossless.Utils.createArray(10, 4, 64); | ||
this.HuffTab = jpeg.lossless.Utils.createArray(4, 2, 50 * 256); | ||
this.IDCT_Source = []; | ||
this.nBlock = []; | ||
this.acTab = jpeg.lossless.Utils.createArray(10, 1); | ||
this.dcTab = jpeg.lossless.Utils.createArray(10, 1); | ||
this.qTab = jpeg.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 = false; | ||
this.mask = 0; | ||
if (typeof numBytes !== "undefined") { | ||
this.numBytes = numBytes; | ||
} | ||
}; | ||
jpeg.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 | ||
]; | ||
jpeg.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 | ||
]; | ||
jpeg.lossless.Decoder.MAX_HUFFMAN_SUBTREE = 50; | ||
jpeg.lossless.Decoder.MSB = 2147483648; | ||
jpeg.lossless.Decoder.RESTART_MARKER_BEGIN = 65488; | ||
jpeg.lossless.Decoder.RESTART_MARKER_END = 65495; | ||
jpeg.lossless.Decoder.prototype.decompress = function(buffer, offset, length) { | ||
return this.decode(buffer, offset, length).buffer; | ||
}; | ||
jpeg.lossless.Decoder.prototype.decode = function(buffer, offset, length, numBytes) { | ||
var current, scanNum = 0, pred = [], i, compN, temp = [], index = [], mcuNum; | ||
if (typeof buffer !== "undefined") { | ||
this.buffer = buffer; | ||
} | ||
if (typeof numBytes !== "undefined") { | ||
this.numBytes = numBytes; | ||
} | ||
this.stream = new jpeg.lossless.DataStream(this.buffer, offset, length); | ||
this.buffer = null; | ||
this.xLoc = 0; | ||
this.yLoc = 0; | ||
current = this.stream.get16(); | ||
if (current !== 65496) { | ||
throw new Error("Not a JPEG file"); | ||
} | ||
current = this.stream.get16(); | ||
while (current >> 4 !== 4092 || current === 65476) { | ||
switch (current) { | ||
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, jpeg.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 (current >> 8 !== 255) { | ||
throw new Error("ERROR: format throw new IOException! (decode)"); | ||
} | ||
} | ||
current = this.stream.get16(); | ||
} | ||
if (current < 65472 || current > 65479) { | ||
throw new Error("ERROR: could not handle arithmetic code!"); | ||
} | ||
this.frame.read(this.stream); | ||
current = this.stream.get16(); | ||
do { | ||
while (current !== 65498) { | ||
switch (current) { | ||
case 65476: | ||
current = this.stream.get16(); | ||
while ((((current >> 4) !== 0x0FFC) || (current === 0xFFC4))) { // SOF 0~15 | ||
switch (current) { | ||
case 0xFFC4: // DHT | ||
this.huffTable.read(this.stream, this.HuffTab); | ||
break; | ||
case 65484: | ||
case 0xFFCC: // DAC | ||
throw new Error("Program doesn't support arithmetic coding. (format throw new IOException)"); | ||
case 65499: | ||
case 0xFFDB: | ||
this.quantTable.read(this.stream, jpeg.lossless.Decoder.TABLE); | ||
break; | ||
case 65501: | ||
case 0xFFDD: | ||
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: | ||
case 0xFFE0: | ||
case 0xFFE1: | ||
case 0xFFE2: | ||
case 0xFFE3: | ||
case 0xFFE4: | ||
case 0xFFE5: | ||
case 0xFFE6: | ||
case 0xFFE7: | ||
case 0xFFE8: | ||
case 0xFFE9: | ||
case 0xFFEA: | ||
case 0xFFEB: | ||
case 0xFFEC: | ||
case 0xFFED: | ||
case 0xFFEE: | ||
case 0xFFEF: | ||
this.readApp(); | ||
break; | ||
case 65534: | ||
case 0xFFFE: | ||
this.readComment(); | ||
break; | ||
default: | ||
if (current >> 8 !== 255) { | ||
throw new Error("ERROR: format throw new IOException! (Parser.decode)"); | ||
default: | ||
if ((current >> 8) !== 0xFF) { | ||
throw new Error("ERROR: format throw new IOException! (decode)"); | ||
} | ||
} | ||
current = this.stream.get16(); | ||
} | ||
if ((current < 0xFFC0) || (current > 0xFFC7)) { | ||
throw new Error("ERROR: could not handle arithmetic code!"); | ||
} | ||
this.frame.read(this.stream); | ||
current = this.stream.get16(); | ||
do { | ||
while (current !== 0x0FFDA) { // SOS | ||
switch (current) { | ||
case 0xFFC4: // DHT | ||
this.huffTable.read(this.stream, this.HuffTab); | ||
break; | ||
case 0xFFCC: // DAC | ||
throw new Error("Program doesn't support arithmetic coding. (format throw new IOException)"); | ||
case 0xFFDB: | ||
this.quantTable.read(this.stream, jpeg.lossless.Decoder.TABLE); | ||
break; | ||
case 0xFFDD: | ||
this.restartInterval = this.readNumber(); | ||
break; | ||
case 0xFFE0: | ||
case 0xFFE1: | ||
case 0xFFE2: | ||
case 0xFFE3: | ||
case 0xFFE4: | ||
case 0xFFE5: | ||
case 0xFFE6: | ||
case 0xFFE7: | ||
case 0xFFE8: | ||
case 0xFFE9: | ||
case 0xFFEA: | ||
case 0xFFEB: | ||
case 0xFFEC: | ||
case 0xFFED: | ||
case 0xFFEE: | ||
case 0xFFEF: | ||
this.readApp(); | ||
break; | ||
case 0xFFFE: | ||
this.readComment(); | ||
break; | ||
default: | ||
if ((current >> 8) !== 0xFF) { | ||
throw new Error("ERROR: format throw new IOException! (Parser.decode)"); | ||
} | ||
} | ||
current = this.stream.get16(); | ||
} | ||
this.precision = this.frame.precision; | ||
this.components = this.frame.components; | ||
if (!this.numBytes) { | ||
} | ||
this.precision = this.frame.precision; | ||
this.components = this.frame.components; | ||
if (!this.numBytes) { | ||
this.numBytes = parseInt(Math.ceil(this.precision / 8)); | ||
} | ||
if (this.numBytes == 1) { | ||
this.mask = 255; | ||
} else { | ||
this.mask = 65535; | ||
} | ||
this.scan.read(this.stream); | ||
this.numComp = this.scan.numComp; | ||
this.selection = this.scan.selection; | ||
if (this.numBytes === 1) { | ||
} | ||
if (this.numBytes == 1) { | ||
this.mask = 0xFF; | ||
} else { | ||
this.mask = 0xFFFF; | ||
} | ||
this.scan.read(this.stream); | ||
this.numComp = this.scan.numComp; | ||
this.selection = this.scan.selection; | ||
if (this.numBytes === 1) { | ||
if (this.numComp === 3) { | ||
this.getter = this.getValueRGB; | ||
this.setter = this.setValueRGB; | ||
this.output = this.outputRGB; | ||
this.getter = this.getValueRGB; | ||
this.setter = this.setValueRGB; | ||
this.output = this.outputRGB; | ||
} else { | ||
this.getter = this.getValue8; | ||
this.setter = this.setValue8; | ||
this.output = this.outputSingle; | ||
this.getter = this.getValue8; | ||
this.setter = this.setValue8; | ||
this.output = this.outputSingle; | ||
} | ||
} else { | ||
} else { | ||
this.getter = this.getValue16; | ||
this.setter = this.setValue16; | ||
this.output = this.outputSingle; | ||
} | ||
switch (this.selection) { | ||
} | ||
switch (this.selection) { | ||
case 2: | ||
this.selector = this.select2; | ||
break; | ||
this.selector = this.select2; | ||
break; | ||
case 3: | ||
this.selector = this.select3; | ||
break; | ||
this.selector = this.select3; | ||
break; | ||
case 4: | ||
this.selector = this.select4; | ||
break; | ||
this.selector = this.select4; | ||
break; | ||
case 5: | ||
this.selector = this.select5; | ||
break; | ||
this.selector = this.select5; | ||
break; | ||
case 6: | ||
this.selector = this.select6; | ||
break; | ||
this.selector = this.select6; | ||
break; | ||
case 7: | ||
this.selector = this.select7; | ||
break; | ||
this.selector = this.select7; | ||
break; | ||
default: | ||
this.selector = this.select1; | ||
break; | ||
} | ||
this.scanComps = this.scan.components; | ||
this.quantTables = this.quantTable.quantTables; | ||
for (i = 0; i < this.numComp; i += 1) { | ||
this.selector = this.select1; | ||
break; | ||
} | ||
this.scanComps = this.scan.components; | ||
this.quantTables = this.quantTable.quantTables; | ||
for (i = 0; i < this.numComp; i+=1) { | ||
compN = this.scanComps[i].scanCompSel; | ||
@@ -788,389 +436,1324 @@ this.qTab[i] = this.quantTables[this.components[compN].quantTableSel]; | ||
this.acTab[i] = this.HuffTab[this.scanComps[i].acTabSel][1]; | ||
} | ||
this.xDim = this.frame.dimX; | ||
this.yDim = this.frame.dimY; | ||
if (this.numBytes == 1) { | ||
} | ||
this.xDim = this.frame.dimX; | ||
this.yDim = this.frame.dimY; | ||
if (this.numBytes == 1) { | ||
this.outputData = new Uint8Array(new ArrayBuffer(this.xDim * this.yDim * this.numBytes * this.numComp)); | ||
} else { | ||
} else { | ||
this.outputData = new Uint16Array(new ArrayBuffer(this.xDim * this.yDim * this.numBytes * this.numComp)); | ||
} | ||
scanNum += 1; | ||
while (true) { | ||
} | ||
scanNum+=1; | ||
while (true) { // Decode one scan | ||
temp[0] = 0; | ||
index[0] = 0; | ||
for (i = 0; i < 10; i += 1) { | ||
pred[i] = 1 << this.precision - 1; | ||
for (i = 0; i < 10; i+=1) { | ||
pred[i] = (1 << (this.precision - 1)); | ||
} | ||
if (this.restartInterval === 0) { | ||
current = this.decodeUnit(pred, temp, index); | ||
while (current === 0 && (this.xLoc < this.xDim && this.yLoc < this.yDim)) { | ||
this.output(pred); | ||
current = this.decodeUnit(pred, temp, index); | ||
} | ||
break; | ||
while ((current === 0) && ((this.xLoc < this.xDim) && (this.yLoc < this.yDim))) { | ||
this.output(pred); | ||
current = this.decodeUnit(pred, temp, index); | ||
} | ||
break; //current=MARKER | ||
} | ||
for (mcuNum = 0; mcuNum < this.restartInterval; mcuNum += 1) { | ||
this.restarting = mcuNum == 0; | ||
current = this.decodeUnit(pred, temp, index); | ||
this.output(pred); | ||
if (current !== 0) { | ||
break; | ||
} | ||
for (mcuNum = 0; mcuNum < this.restartInterval; mcuNum+=1) { | ||
this.restarting = (mcuNum == 0); | ||
current = this.decodeUnit(pred, temp, index); | ||
this.output(pred); | ||
if (current !== 0) { | ||
break; | ||
} | ||
} | ||
if (current === 0) { | ||
if (this.markerIndex !== 0) { | ||
current = 65280 | this.marker; | ||
this.markerIndex = 0; | ||
} else { | ||
current = this.stream.get16(); | ||
} | ||
if (this.markerIndex !== 0) { | ||
current = (0xFF00 | this.marker); | ||
this.markerIndex = 0; | ||
} else { | ||
current = this.stream.get16(); | ||
} | ||
} | ||
if (!(current >= jpeg.lossless.Decoder.RESTART_MARKER_BEGIN && current <= jpeg.lossless.Decoder.RESTART_MARKER_END)) { | ||
break; | ||
if (!((current >= jpeg.lossless.Decoder.RESTART_MARKER_BEGIN) && | ||
(current <= jpeg.lossless.Decoder.RESTART_MARKER_END))) { | ||
break; //current=MARKER | ||
} | ||
} | ||
if (current === 65500 && scanNum === 1) { | ||
} | ||
if ((current === 0xFFDC) && (scanNum === 1)) { //DNL | ||
this.readNumber(); | ||
current = this.stream.get16(); | ||
} | ||
} while (current !== 65497 && (this.xLoc < this.xDim && this.yLoc < this.yDim) && scanNum === 0); | ||
return this.outputData; | ||
}; | ||
jpeg.lossless.Decoder.prototype.decodeUnit = function(prev, temp, index) { | ||
if (this.numComp == 1) { | ||
return this.decodeSingle(prev, temp, index); | ||
} else if (this.numComp == 3) { | ||
return this.decodeRGB(prev, temp, index); | ||
} else { | ||
return -1; | ||
} | ||
}; | ||
jpeg.lossless.Decoder.prototype.select1 = function(compOffset) { | ||
return this.getPreviousX(compOffset); | ||
}; | ||
jpeg.lossless.Decoder.prototype.select2 = function(compOffset) { | ||
return this.getPreviousY(compOffset); | ||
}; | ||
jpeg.lossless.Decoder.prototype.select3 = function(compOffset) { | ||
return this.getPreviousXY(compOffset); | ||
}; | ||
jpeg.lossless.Decoder.prototype.select4 = function(compOffset) { | ||
return this.getPreviousX(compOffset) + this.getPreviousY(compOffset) - this.getPreviousXY(compOffset); | ||
}; | ||
jpeg.lossless.Decoder.prototype.select5 = function(compOffset) { | ||
return this.getPreviousX(compOffset) + (this.getPreviousY(compOffset) - this.getPreviousXY(compOffset) >> 1); | ||
}; | ||
jpeg.lossless.Decoder.prototype.select6 = function(compOffset) { | ||
return this.getPreviousY(compOffset) + (this.getPreviousX(compOffset) - this.getPreviousXY(compOffset) >> 1); | ||
}; | ||
jpeg.lossless.Decoder.prototype.select7 = function(compOffset) { | ||
return (this.getPreviousX(compOffset) + this.getPreviousY(compOffset)) / 2; | ||
}; | ||
jpeg.lossless.Decoder.prototype.decodeRGB = function(prev, temp, index) { | ||
var value, actab, dctab, qtab, ctrC, i, k, j; | ||
prev[0] = this.selector(0); | ||
prev[1] = this.selector(1); | ||
prev[2] = this.selector(2); | ||
for (ctrC = 0; ctrC < this.numComp; ctrC += 1) { | ||
qtab = this.qTab[ctrC]; | ||
actab = this.acTab[ctrC]; | ||
dctab = this.dcTab[ctrC]; | ||
for (i = 0; i < this.nBlock[ctrC]; i += 1) { | ||
for (k = 0; k < this.IDCT_Source.length; k += 1) { | ||
this.IDCT_Source[k] = 0; | ||
} while ((current !== 0xFFD9) && ((this.xLoc < this.xDim) && (this.yLoc < this.yDim)) && (scanNum === 0)); | ||
return this.outputData; | ||
}; | ||
jpeg.lossless.Decoder.prototype.decodeUnit = function (prev, temp, index) { | ||
if (this.numComp == 1) { | ||
return this.decodeSingle(prev, temp, index); | ||
} else if (this.numComp == 3) { | ||
return this.decodeRGB(prev, temp, index); | ||
} else { | ||
return -1; | ||
} | ||
}; | ||
jpeg.lossless.Decoder.prototype.select1 = function (compOffset) { | ||
return this.getPreviousX(compOffset); | ||
}; | ||
jpeg.lossless.Decoder.prototype.select2 = function (compOffset) { | ||
return this.getPreviousY(compOffset); | ||
}; | ||
jpeg.lossless.Decoder.prototype.select3 = function (compOffset) { | ||
return this.getPreviousXY(compOffset); | ||
}; | ||
jpeg.lossless.Decoder.prototype.select4 = function (compOffset) { | ||
return (this.getPreviousX(compOffset) + this.getPreviousY(compOffset)) - this.getPreviousXY(compOffset); | ||
}; | ||
jpeg.lossless.Decoder.prototype.select5 = function (compOffset) { | ||
return this.getPreviousX(compOffset) + ((this.getPreviousY(compOffset) - this.getPreviousXY(compOffset)) >> 1); | ||
}; | ||
jpeg.lossless.Decoder.prototype.select6 = function (compOffset) { | ||
return this.getPreviousY(compOffset) + ((this.getPreviousX(compOffset) - this.getPreviousXY(compOffset)) >> 1); | ||
}; | ||
jpeg.lossless.Decoder.prototype.select7 = function (compOffset) { | ||
return ((this.getPreviousX(compOffset) + this.getPreviousY(compOffset)) / 2); | ||
}; | ||
jpeg.lossless.Decoder.prototype.decodeRGB = function (prev, temp, index) { | ||
/*jslint bitwise: true */ | ||
var value, actab, dctab, qtab, ctrC, i, k, j; | ||
prev[0] = this.selector(0); | ||
prev[1] = this.selector(1); | ||
prev[2] = this.selector(2); | ||
for (ctrC = 0; ctrC < this.numComp; ctrC+=1) { | ||
qtab = this.qTab[ctrC]; | ||
actab = this.acTab[ctrC]; | ||
dctab = this.dcTab[ctrC]; | ||
for (i = 0; i < this.nBlock[ctrC]; i+=1) { | ||
for (k = 0; k < this.IDCT_Source.length; k+=1) { | ||
this.IDCT_Source[k] = 0; | ||
} | ||
value = this.getHuffmanValue(dctab, temp, index); | ||
if (value >= 65280) { | ||
return value; | ||
if (value >= 0xFF00) { | ||
return value; | ||
} | ||
prev[ctrC] = this.IDCT_Source[0] = prev[ctrC] + this.getn(index, value, temp, index); | ||
this.IDCT_Source[0] *= qtab[0]; | ||
for (j = 1; j < 64; j += 1) { | ||
value = this.getHuffmanValue(actab, temp, index); | ||
if (value >= 65280) { | ||
return value; | ||
} | ||
j += value >> 4; | ||
if ((value & 15) === 0) { | ||
if (value >> 4 === 0) { | ||
break; | ||
for (j = 1; j < 64; j+=1) { | ||
value = this.getHuffmanValue(actab, temp, index); | ||
if (value >= 0xFF00) { | ||
return value; | ||
} | ||
} else { | ||
this.IDCT_Source[jpeg.lossless.Decoder.IDCT_P[j]] = this.getn(index, value & 15, temp, index) * qtab[j]; | ||
} | ||
j += (value >> 4); | ||
if ((value & 0x0F) === 0) { | ||
if ((value >> 4) === 0) { | ||
break; | ||
} | ||
} else { | ||
this.IDCT_Source[jpeg.lossless.Decoder.IDCT_P[j]] = this.getn(index, value & 0x0F, temp, index) * qtab[j]; | ||
} | ||
} | ||
} | ||
} | ||
return 0; | ||
}; | ||
jpeg.lossless.Decoder.prototype.decodeSingle = function(prev, temp, index) { | ||
var value, i, n, nRestart; | ||
if (this.restarting) { | ||
this.restarting = false; | ||
prev[0] = 1 << this.frame.precision - 1; | ||
} else { | ||
prev[0] = this.selector(); | ||
} | ||
return 0; | ||
}; | ||
jpeg.lossless.Decoder.prototype.decodeSingle = function (prev, temp, index) { | ||
/*jslint bitwise: true */ | ||
var value, i, n, nRestart; | ||
if (this.restarting) { | ||
this.restarting = false; | ||
prev[0] = (1 << (this.frame.precision - 1)); | ||
} else { | ||
prev[0] = this.selector(); | ||
} | ||
for (i = 0; i < this.nBlock[0]; i+=1) { | ||
value = this.getHuffmanValue(this.dcTab[0], temp, index); | ||
if (value >= 0xFF00) { | ||
return value; | ||
} | ||
for (i = 0; i < this.nBlock[0]; i += 1) { | ||
value = this.getHuffmanValue(this.dcTab[0], temp, index); | ||
if (value >= 65280) { | ||
return value; | ||
} | ||
n = this.getn(prev, value, temp, index); | ||
nRestart = n >> 8; | ||
if (nRestart >= jpeg.lossless.Decoder.RESTART_MARKER_BEGIN && nRestart <= jpeg.lossless.Decoder.RESTART_MARKER_END) { | ||
n = this.getn(prev, value, temp, index); | ||
nRestart = (n >> 8); | ||
if ((nRestart >= jpeg.lossless.Decoder.RESTART_MARKER_BEGIN) && (nRestart <= jpeg.lossless.Decoder.RESTART_MARKER_END)) { | ||
return nRestart; | ||
} | ||
prev[0] += n; | ||
} | ||
return 0; | ||
}; | ||
jpeg.lossless.Decoder.prototype.getHuffmanValue = function(table, temp, index) { | ||
var code, input, mask; | ||
mask = 65535; | ||
if (index[0] < 8) { | ||
temp[0] <<= 8; | ||
input = this.stream.get8(); | ||
if (input === 255) { | ||
prev[0] += n; | ||
} | ||
return 0; | ||
}; | ||
// Huffman table for fast search: (HuffTab) 8-bit Look up table 2-layer search architecture, 1st-layer represent 256 node (8 bits) if codeword-length > 8 | ||
// bits, then the entry of 1st-layer = (# of 2nd-layer table) | MSB and it is stored in the 2nd-layer Size of tables in each layer are 256. | ||
// HuffTab[*][*][0-256] is always the only 1st-layer table. | ||
// | ||
// An entry can be: (1) (# of 2nd-layer table) | MSB , for code length > 8 in 1st-layer (2) (Code length) << 8 | HuffVal | ||
// | ||
// HuffmanValue(table HuffTab[x][y] (ex) HuffmanValue(HuffTab[1][0],...) | ||
// ): | ||
// return: Huffman Value of table | ||
// 0xFF?? if it receives a MARKER | ||
// Parameter: table HuffTab[x][y] (ex) HuffmanValue(HuffTab[1][0],...) | ||
// temp temp storage for remainded bits | ||
// index index to bit of temp | ||
// in FILE pointer | ||
// Effect: | ||
// temp store new remainded bits | ||
// index change to new index | ||
// in change to new position | ||
// NOTE: | ||
// Initial by temp=0; index=0; | ||
// NOTE: (explain temp and index) | ||
// temp: is always in the form at calling time or returning time | ||
// | byte 4 | byte 3 | byte 2 | byte 1 | | ||
// | 0 | 0 | 00000000 | 00000??? | if not a MARKER | ||
// ^index=3 (from 0 to 15) | ||
// 321 | ||
// NOTE (marker and marker_index): | ||
// If get a MARKER from 'in', marker=the low-byte of the MARKER | ||
// and marker_index=9 | ||
// If marker_index=9 then index is always > 8, or HuffmanValue() | ||
// will not be called | ||
jpeg.lossless.Decoder.prototype.getHuffmanValue = function (table, temp, index) { | ||
/*jslint bitwise: true */ | ||
var code, input, mask; | ||
mask = 0xFFFF; | ||
if (index[0] < 8) { | ||
temp[0] <<= 8; | ||
input = this.stream.get8(); | ||
if (input === 0xFF) { | ||
this.marker = this.stream.get8(); | ||
if (this.marker !== 0) { | ||
this.markerIndex = 9; | ||
this.markerIndex = 9; | ||
} | ||
} | ||
temp[0] |= input; | ||
} else { | ||
index[0] -= 8; | ||
} | ||
code = table[temp[0] >> index[0]]; | ||
if ((code & jpeg.lossless.Decoder.MSB) !== 0) { | ||
if (this.markerIndex !== 0) { | ||
temp[0] |= input; | ||
} else { | ||
index[0] -= 8; | ||
} | ||
code = table[temp[0] >> index[0]]; | ||
if ((code & jpeg.lossless.Decoder.MSB) !== 0) { | ||
if (this.markerIndex !== 0) { | ||
this.markerIndex = 0; | ||
return 65280 | this.marker; | ||
} | ||
temp[0] &= mask >> 16 - index[0]; | ||
temp[0] <<= 8; | ||
input = this.stream.get8(); | ||
if (input === 255) { | ||
return 0xFF00 | this.marker; | ||
} | ||
temp[0] &= (mask >> (16 - index[0])); | ||
temp[0] <<= 8; | ||
input = this.stream.get8(); | ||
if (input === 0xFF) { | ||
this.marker = this.stream.get8(); | ||
if (this.marker !== 0) { | ||
this.markerIndex = 9; | ||
this.markerIndex = 9; | ||
} | ||
} | ||
temp[0] |= input; | ||
code = table[(code & 255) * 256 + (temp[0] >> index[0])]; | ||
index[0] += 8; | ||
} | ||
index[0] += 8 - (code >> 8); | ||
if (index[0] < 0) { | ||
throw new Error("index=" + index[0] + " temp=" + temp[0] + " code=" + code + " in HuffmanValue()"); | ||
} | ||
if (index[0] < this.markerIndex) { | ||
this.markerIndex = 0; | ||
return 65280 | this.marker; | ||
} | ||
temp[0] &= mask >> 16 - index[0]; | ||
return code & 255; | ||
}; | ||
jpeg.lossless.Decoder.prototype.getn = function(PRED, n, temp, index) { | ||
var result, one, n_one, mask, input; | ||
one = 1; | ||
n_one = -1; | ||
mask = 65535; | ||
if (n === 0) { | ||
return 0; | ||
} | ||
if (n === 16) { | ||
if (PRED[0] >= 0) { | ||
temp[0] |= input; | ||
code = table[((code & 0xFF) * 256) + (temp[0] >> index[0])]; | ||
index[0] += 8; | ||
} | ||
index[0] += 8 - (code >> 8); | ||
if (index[0] < 0) { | ||
throw new Error("index=" + index[0] + " temp=" + temp[0] + " code=" + code + " in HuffmanValue()"); | ||
} | ||
if (index[0] < this.markerIndex) { | ||
this.markerIndex = 0; | ||
return 0xFF00 | this.marker; | ||
} | ||
temp[0] &= (mask >> (16 - index[0])); | ||
return code & 0xFF; | ||
}; | ||
jpeg.lossless.Decoder.prototype.getn = function (PRED, n, temp, index) { | ||
/*jslint bitwise: true */ | ||
var result, one, n_one, mask, input; | ||
one = 1; | ||
n_one = -1; | ||
mask = 0xFFFF; | ||
if (n === 0) { | ||
return 0; | ||
} | ||
if (n === 16) { | ||
if (PRED[0] >= 0) { | ||
return -32768; | ||
} else { | ||
} else { | ||
return 32768; | ||
} | ||
} | ||
index[0] -= n; | ||
if (index[0] >= 0) { | ||
if (index[0] < this.markerIndex && !this.isLastPixel()) { | ||
} | ||
index[0] -= n; | ||
if (index[0] >= 0) { | ||
if ((index[0] < this.markerIndex) && !this.isLastPixel()) { // this was corrupting the last pixel in some cases | ||
this.markerIndex = 0; | ||
return (65280 | this.marker) << 8; | ||
} | ||
result = temp[0] >> index[0]; | ||
temp[0] &= mask >> 16 - index[0]; | ||
} else { | ||
temp[0] <<= 8; | ||
input = this.stream.get8(); | ||
if (input === 255) { | ||
return (0xFF00 | this.marker) << 8; | ||
} | ||
result = temp[0] >> index[0]; | ||
temp[0] &= (mask >> (16 - index[0])); | ||
} else { | ||
temp[0] <<= 8; | ||
input = this.stream.get8(); | ||
if (input === 0xFF) { | ||
this.marker = this.stream.get8(); | ||
if (this.marker !== 0) { | ||
this.markerIndex = 9; | ||
this.markerIndex = 9; | ||
} | ||
} | ||
temp[0] |= input; | ||
index[0] += 8; | ||
if (index[0] < 0) { | ||
} | ||
temp[0] |= input; | ||
index[0] += 8; | ||
if (index[0] < 0) { | ||
if (this.markerIndex !== 0) { | ||
this.markerIndex = 0; | ||
return (65280 | this.marker) << 8; | ||
this.markerIndex = 0; | ||
return (0xFF00 | this.marker) << 8; | ||
} | ||
temp[0] <<= 8; | ||
input = this.stream.get8(); | ||
if (input === 255) { | ||
this.marker = this.stream.get8(); | ||
if (this.marker !== 0) { | ||
this.markerIndex = 9; | ||
} | ||
if (input === 0xFF) { | ||
this.marker = this.stream.get8(); | ||
if (this.marker !== 0) { | ||
this.markerIndex = 9; | ||
} | ||
} | ||
temp[0] |= input; | ||
index[0] += 8; | ||
} | ||
if (index[0] < 0) { | ||
} | ||
if (index[0] < 0) { | ||
throw new Error("index=" + index[0] + " in getn()"); | ||
} | ||
if (index[0] < this.markerIndex) { | ||
} | ||
if (index[0] < this.markerIndex) { | ||
this.markerIndex = 0; | ||
return (65280 | this.marker) << 8; | ||
} | ||
result = temp[0] >> index[0]; | ||
temp[0] &= mask >> 16 - index[0]; | ||
return (0xFF00 | this.marker) << 8; | ||
} | ||
if (result < one << n - 1) { | ||
result += (n_one << n) + 1; | ||
result = temp[0] >> index[0]; | ||
temp[0] &= (mask >> (16 - index[0])); | ||
} | ||
if (result < (one << (n - 1))) { | ||
result += (n_one << n) + 1; | ||
} | ||
return result; | ||
}; | ||
jpeg.lossless.Decoder.prototype.getPreviousX = function (compOffset) { | ||
/*jslint bitwise: true */ | ||
if (this.xLoc > 0) { | ||
return this.getter((((this.yLoc * this.xDim) + this.xLoc) - 1), compOffset); | ||
} else if (this.yLoc > 0) { | ||
return this.getPreviousY(compOffset); | ||
} else { | ||
return (1 << (this.frame.precision - 1)); | ||
} | ||
}; | ||
jpeg.lossless.Decoder.prototype.getPreviousXY = function (compOffset) { | ||
/*jslint bitwise: true */ | ||
if ((this.xLoc > 0) && (this.yLoc > 0)) { | ||
return this.getter(((((this.yLoc - 1) * this.xDim) + this.xLoc) - 1), compOffset); | ||
} else { | ||
return this.getPreviousY(compOffset); | ||
} | ||
}; | ||
jpeg.lossless.Decoder.prototype.getPreviousY = function (compOffset) { | ||
/*jslint bitwise: true */ | ||
if (this.yLoc > 0) { | ||
return this.getter((((this.yLoc - 1) * this.xDim) + this.xLoc), compOffset); | ||
} else { | ||
return this.getPreviousX(compOffset); | ||
} | ||
}; | ||
jpeg.lossless.Decoder.prototype.isLastPixel = function () { | ||
return (this.xLoc === (this.xDim - 1)) && (this.yLoc === (this.yDim - 1)); | ||
}; | ||
jpeg.lossless.Decoder.prototype.outputSingle = function (PRED) { | ||
if ((this.xLoc < this.xDim) && (this.yLoc < this.yDim)) { | ||
this.setter((((this.yLoc * this.xDim) + this.xLoc)), this.mask & PRED[0]); | ||
this.xLoc+=1; | ||
if (this.xLoc >= this.xDim) { | ||
this.yLoc+=1; | ||
this.xLoc = 0; | ||
} | ||
return result; | ||
}; | ||
jpeg.lossless.Decoder.prototype.getPreviousX = function(compOffset) { | ||
if (this.xLoc > 0) { | ||
return this.getter(this.yLoc * this.xDim + this.xLoc - 1, compOffset); | ||
} else if (this.yLoc > 0) { | ||
return this.getPreviousY(compOffset); | ||
} else { | ||
return 1 << this.frame.precision - 1; | ||
} | ||
}; | ||
jpeg.lossless.Decoder.prototype.outputRGB = function (PRED) { | ||
var offset = ((this.yLoc * this.xDim) + this.xLoc); | ||
if ((this.xLoc < this.xDim) && (this.yLoc < this.yDim)) { | ||
this.setter(offset, PRED[0], 0); | ||
this.setter(offset, PRED[1], 1); | ||
this.setter(offset, PRED[2], 2); | ||
this.xLoc+=1; | ||
if (this.xLoc >= this.xDim) { | ||
this.yLoc+=1; | ||
this.xLoc = 0; | ||
} | ||
}; | ||
jpeg.lossless.Decoder.prototype.getPreviousXY = function(compOffset) { | ||
if (this.xLoc > 0 && this.yLoc > 0) { | ||
return this.getter((this.yLoc - 1) * this.xDim + this.xLoc - 1, compOffset); | ||
} else { | ||
return this.getPreviousY(compOffset); | ||
} | ||
}; | ||
jpeg.lossless.Decoder.prototype.setValue8 = function (index, val) { | ||
this.outputData[index] = val; | ||
}; | ||
jpeg.lossless.Decoder.prototype.getValue8 = function (index) { | ||
return this.outputData[index]; // mask should not be necessary because outputData is either Int8Array or Int16Array | ||
}; | ||
var littleEndian = (function() { | ||
var buffer = new ArrayBuffer(2); | ||
new DataView(buffer).setInt16(0, 256, true /* littleEndian */); | ||
// Int16Array uses the platform's endianness. | ||
return new Int16Array(buffer)[0] === 256; | ||
})(); | ||
if (littleEndian) { | ||
// just reading from an array is fine then. Int16Array will use platform endianness. | ||
jpeg.lossless.Decoder.prototype.setValue16 = jpeg.lossless.Decoder.prototype.setValue8; | ||
jpeg.lossless.Decoder.prototype.getValue16 = jpeg.lossless.Decoder.prototype.getValue8; | ||
} | ||
else { | ||
// If platform is big-endian, we will need to convert to little-endian | ||
jpeg.lossless.Decoder.prototype.setValue16 = function (index, val) { | ||
this.outputData[index] = ((val & 0xFF) << 8) | ((val >> 8) & 0xFF); | ||
}; | ||
jpeg.lossless.Decoder.prototype.getValue16 = function (index) { | ||
var val = this.outputData[index]; | ||
return ((val & 0xFF) << 8) | ((val >> 8) & 0xFF); | ||
}; | ||
} | ||
jpeg.lossless.Decoder.prototype.setValueRGB = function (index, val, compOffset) { | ||
// this.outputData.setUint8(index * 3 + compOffset, val); | ||
this.outputData[index * 3 + compOffset] = val; | ||
}; | ||
jpeg.lossless.Decoder.prototype.getValueRGB = function (index, compOffset) { | ||
// return this.outputData.getUint8(index * 3 + compOffset); | ||
return this.outputData[index * 3 + compOffset]; | ||
}; | ||
jpeg.lossless.Decoder.prototype.readApp = function() { | ||
var count = 0, length = this.stream.get16(); | ||
count += 2; | ||
while (count < length) { | ||
this.stream.get8(); | ||
count+=1; | ||
} | ||
return length; | ||
}; | ||
jpeg.lossless.Decoder.prototype.readComment = function () { | ||
var sb = "", count = 0, length; | ||
length = this.stream.get16(); | ||
count += 2; | ||
while (count < length) { | ||
sb += this.stream.get8(); | ||
count+=1; | ||
} | ||
return sb; | ||
}; | ||
jpeg.lossless.Decoder.prototype.readNumber = function() { | ||
var Ld = this.stream.get16(); | ||
if (Ld !== 4) { | ||
throw new Error("ERROR: Define number format throw new IOException [Ld!=4]"); | ||
} | ||
return this.stream.get16(); | ||
}; | ||
/*** Exports ***/ | ||
var moduleType = typeof module; | ||
if ((moduleType !== 'undefined') && module.exports) { | ||
module.exports = jpeg.lossless.Decoder; | ||
} | ||
},{"./data-stream.js":2,"./frame-header.js":4,"./huffman-table.js":5,"./quantization-table.js":7,"./scan-header.js":9,"./utils.js":10}],4:[function(require,module,exports){ | ||
/* | ||
* Copyright (C) 2015 Michael Martinez | ||
* Changes: Added support for selection values 2-7, fixed minor bugs & | ||
* warnings, split into multiple class files, and general clean up. | ||
* | ||
* 08-25-2015: Helmut Dersch agreed to a license change from LGPL to MIT. | ||
*/ | ||
/* | ||
* Copyright (C) Helmut Dersch | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
/*jslint browser: true, node: true */ | ||
/*global require, module */ | ||
"use strict"; | ||
/*** Imports ***/ | ||
var jpeg = jpeg || {}; | ||
jpeg.lossless = jpeg.lossless || {}; | ||
jpeg.lossless.ComponentSpec = jpeg.lossless.ComponentSpec || ((typeof require !== 'undefined') ? require('./component-spec.js') : null); | ||
jpeg.lossless.DataStream = jpeg.lossless.DataStream || ((typeof require !== 'undefined') ? require('./data-stream.js') : null); | ||
/*** Constructor ***/ | ||
jpeg.lossless.FrameHeader = jpeg.lossless.FrameHeader || function () { | ||
this.components = []; // Components | ||
this.dimX = 0; // Number of samples per line | ||
this.dimY = 0; // Number of lines | ||
this.numComp = 0; // Number of component in the frame | ||
this.precision = 0; // Sample Precision (from the original image) | ||
}; | ||
/*** Prototype Methods ***/ | ||
jpeg.lossless.FrameHeader.prototype.read = function (data) { | ||
/*jslint bitwise: true */ | ||
var count = 0, length, i, c, temp; | ||
length = data.get16(); | ||
count += 2; | ||
this.precision = data.get8(); | ||
count+=1; | ||
this.dimY = data.get16(); | ||
count += 2; | ||
this.dimX = data.get16(); | ||
count += 2; | ||
this.numComp = data.get8(); | ||
count+=1; | ||
for (i = 1; i <= this.numComp; i+=1) { | ||
if (count > length) { | ||
throw new Error("ERROR: frame format error"); | ||
} | ||
}; | ||
jpeg.lossless.Decoder.prototype.getPreviousY = function(compOffset) { | ||
if (this.yLoc > 0) { | ||
return this.getter((this.yLoc - 1) * this.xDim + this.xLoc, compOffset); | ||
} else { | ||
return this.getPreviousX(compOffset); | ||
c = data.get8(); | ||
count+=1; | ||
if (count >= length) { | ||
throw new Error("ERROR: frame format error [c>=Lf]"); | ||
} | ||
}; | ||
jpeg.lossless.Decoder.prototype.isLastPixel = function() { | ||
return this.xLoc === this.xDim - 1 && this.yLoc === this.yDim - 1; | ||
}; | ||
jpeg.lossless.Decoder.prototype.outputSingle = function(PRED) { | ||
if (this.xLoc < this.xDim && this.yLoc < this.yDim) { | ||
this.setter(this.yLoc * this.xDim + this.xLoc, this.mask & PRED[0]); | ||
this.xLoc += 1; | ||
if (this.xLoc >= this.xDim) { | ||
this.yLoc += 1; | ||
this.xLoc = 0; | ||
} | ||
temp = data.get8(); | ||
count+=1; | ||
if (!this.components[c]) { | ||
this.components[c] = new jpeg.lossless.ComponentSpec(); | ||
} | ||
}; | ||
jpeg.lossless.Decoder.prototype.outputRGB = function(PRED) { | ||
var offset = this.yLoc * this.xDim + this.xLoc; | ||
if (this.xLoc < this.xDim && this.yLoc < this.yDim) { | ||
this.setter(offset, PRED[0], 0); | ||
this.setter(offset, PRED[1], 1); | ||
this.setter(offset, PRED[2], 2); | ||
this.xLoc += 1; | ||
if (this.xLoc >= this.xDim) { | ||
this.yLoc += 1; | ||
this.xLoc = 0; | ||
} | ||
this.components[c].hSamp = temp >> 4; | ||
this.components[c].vSamp = temp & 0x0F; | ||
this.components[c].quantTableSel = data.get8(); | ||
count+=1; | ||
} | ||
if (count !== length) { | ||
throw new Error("ERROR: frame format error [Lf!=count]"); | ||
} | ||
return 1; | ||
}; | ||
/*** Exports ***/ | ||
var moduleType = typeof module; | ||
if ((moduleType !== 'undefined') && module.exports) { | ||
module.exports = jpeg.lossless.FrameHeader; | ||
} | ||
},{"./component-spec.js":1,"./data-stream.js":2}],5:[function(require,module,exports){ | ||
/* | ||
* Copyright (C) 2015 Michael Martinez | ||
* Changes: Added support for selection values 2-7, fixed minor bugs & | ||
* warnings, split into multiple class files, and general clean up. | ||
* | ||
* 08-25-2015: Helmut Dersch agreed to a license change from LGPL to MIT. | ||
*/ | ||
/* | ||
* Copyright (C) Helmut Dersch | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
/*jslint browser: true, node: true */ | ||
/*global require, module */ | ||
"use strict"; | ||
/*** Imports ***/ | ||
var jpeg = jpeg || {}; | ||
jpeg.lossless = jpeg.lossless || {}; | ||
jpeg.lossless.DataStream = jpeg.lossless.DataStream || ((typeof require !== 'undefined') ? require('./data-stream.js') : null); | ||
jpeg.lossless.Utils = jpeg.lossless.Utils || ((typeof require !== 'undefined') ? require('./utils.js') : null); | ||
/*** Constructor ***/ | ||
jpeg.lossless.HuffmanTable = jpeg.lossless.HuffmanTable || function () { | ||
this.l = jpeg.lossless.Utils.createArray(4, 2, 16); | ||
this.th = []; | ||
this.v = jpeg.lossless.Utils.createArray(4, 2, 16, 200); | ||
this.tc = jpeg.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; | ||
}; | ||
/*** Static Pseudo-constants ***/ | ||
jpeg.lossless.HuffmanTable.MSB = 0x80000000; | ||
/*** Prototype Methods ***/ | ||
jpeg.lossless.HuffmanTable.prototype.read = function(data, HuffTab) { | ||
/*jslint bitwise: true */ | ||
var count = 0, length, temp, t, c, i, j; | ||
length = data.get16(); | ||
count += 2; | ||
while (count < length) { | ||
temp = data.get8(); | ||
count+=1; | ||
t = temp & 0x0F; | ||
if (t > 3) { | ||
throw new Error("ERROR: Huffman table ID > 3"); | ||
} | ||
}; | ||
jpeg.lossless.Decoder.prototype.setValue8 = function(index, val) { | ||
this.outputData[index] = val; | ||
}; | ||
jpeg.lossless.Decoder.prototype.getValue8 = function(index) { | ||
return this.outputData[index]; | ||
}; | ||
var littleEndian = function() { | ||
var buffer = new ArrayBuffer(2); | ||
new DataView(buffer).setInt16( | ||
0, | ||
256, | ||
true | ||
/* littleEndian */ | ||
); | ||
return new Int16Array(buffer)[0] === 256; | ||
}(); | ||
if (littleEndian) { | ||
jpeg.lossless.Decoder.prototype.setValue16 = jpeg.lossless.Decoder.prototype.setValue8; | ||
jpeg.lossless.Decoder.prototype.getValue16 = jpeg.lossless.Decoder.prototype.getValue8; | ||
} else { | ||
jpeg.lossless.Decoder.prototype.setValue16 = function(index, val) { | ||
this.outputData[index] = (val & 255) << 8 | val >> 8 & 255; | ||
}; | ||
jpeg.lossless.Decoder.prototype.getValue16 = function(index) { | ||
var val = this.outputData[index]; | ||
return (val & 255) << 8 | val >> 8 & 255; | ||
}; | ||
} | ||
jpeg.lossless.Decoder.prototype.setValueRGB = function(index, val, compOffset) { | ||
this.outputData[index * 3 + compOffset] = val; | ||
}; | ||
jpeg.lossless.Decoder.prototype.getValueRGB = function(index, compOffset) { | ||
return this.outputData[index * 3 + compOffset]; | ||
}; | ||
jpeg.lossless.Decoder.prototype.readApp = function() { | ||
var count = 0, length = this.stream.get16(); | ||
count += 2; | ||
while (count < length) { | ||
this.stream.get8(); | ||
count += 1; | ||
c = temp >> 4; | ||
if (c > 2) { | ||
throw new Error("ERROR: Huffman table [Table class > 2 ]"); | ||
} | ||
return length; | ||
}; | ||
jpeg.lossless.Decoder.prototype.readComment = function() { | ||
var sb = "", count = 0, length; | ||
length = this.stream.get16(); | ||
count += 2; | ||
while (count < length) { | ||
sb += this.stream.get8(); | ||
count += 1; | ||
this.th[t] = 1; | ||
this.tc[t][c] = 1; | ||
for (i = 0; i < 16; i+=1) { | ||
this.l[t][c][i] = data.get8(); | ||
count+=1; | ||
} | ||
return sb; | ||
}; | ||
jpeg.lossless.Decoder.prototype.readNumber = function() { | ||
var Ld = this.stream.get16(); | ||
if (Ld !== 4) { | ||
throw new Error("ERROR: Define number format throw new IOException [Ld!=4]"); | ||
for (i = 0; i < 16; i+=1) { | ||
for (j = 0; j < this.l[t][c][i]; j+=1) { | ||
if (count > length) { | ||
throw new Error("ERROR: Huffman table format error [count>Lh]"); | ||
} | ||
this.v[t][c][i][j] = data.get8(); | ||
count+=1; | ||
} | ||
} | ||
return this.stream.get16(); | ||
}; | ||
var moduleType = typeof module; | ||
if (moduleType !== "undefined" && module.exports) { | ||
module.exports = jpeg.lossless.Decoder; | ||
} | ||
} | ||
}); | ||
// src/main.js | ||
var require_main = __commonJS({ | ||
"src/main.js"(exports, module) { | ||
var jpeg = jpeg || {}; | ||
jpeg.lossless = jpeg.lossless || {}; | ||
jpeg.lossless.ComponentSpec = jpeg.lossless.ComponentSpec || (typeof __require !== "undefined" ? require_component_spec() : null); | ||
jpeg.lossless.DataStream = jpeg.lossless.DataStream || (typeof __require !== "undefined" ? require_data_stream() : null); | ||
jpeg.lossless.Decoder = jpeg.lossless.Decoder || (typeof __require !== "undefined" ? require_decoder() : null); | ||
jpeg.lossless.FrameHeader = jpeg.lossless.FrameHeader || (typeof __require !== "undefined" ? require_frame_header() : null); | ||
jpeg.lossless.HuffmanTable = jpeg.lossless.HuffmanTable || (typeof __require !== "undefined" ? require_huffman_table() : null); | ||
jpeg.lossless.QuantizationTable = jpeg.lossless.QuantizationTable || (typeof __require !== "undefined" ? require_quantization_table() : null); | ||
jpeg.lossless.ScanComponent = jpeg.lossless.ScanComponent || (typeof __require !== "undefined" ? require_scan_component() : null); | ||
jpeg.lossless.ScanHeader = jpeg.lossless.ScanHeader || (typeof __require !== "undefined" ? require_scan_header() : null); | ||
jpeg.lossless.Utils = jpeg.lossless.Utils || (typeof __require !== "undefined" ? require_utils() : null); | ||
var moduleType = typeof module; | ||
if (moduleType !== "undefined" && module.exports) { | ||
module.exports = jpeg; | ||
} | ||
if (count !== length) { | ||
throw new Error("ERROR: Huffman table format error [count!=Lf]"); | ||
} | ||
}); | ||
return require_main(); | ||
})(); | ||
for (i = 0; i < 4; i+=1) { | ||
for (j = 0; j < 2; j+=1) { | ||
if (this.tc[i][j] !== 0) { | ||
this.buildHuffTable(HuffTab[i][j], this.l[i][j], this.v[i][j]); | ||
} | ||
} | ||
} | ||
return 1; | ||
}; | ||
// Build_HuffTab() | ||
// Parameter: t table ID | ||
// c table class ( 0 for DC, 1 for AC ) | ||
// L[i] # of codewords which length is i | ||
// V[i][j] Huffman Value (length=i) | ||
// Effect: | ||
// build up HuffTab[t][c] using L and V. | ||
jpeg.lossless.HuffmanTable.prototype.buildHuffTable = function(tab, L, V) { | ||
/*jslint bitwise: true */ | ||
var currentTable, temp, k, i, j, n; | ||
temp = 256; | ||
k = 0; | ||
for (i = 0; i < 8; i+=1) { // i+1 is Code length | ||
for (j = 0; j < L[i]; j+=1) { | ||
for (n = 0; n < (temp >> (i + 1)); n+=1) { | ||
tab[k] = V[i][j] | ((i + 1) << 8); | ||
k+=1; | ||
} | ||
} | ||
} | ||
for (i = 1; k < 256; i+=1, k+=1) { | ||
tab[k] = i | jpeg.lossless.HuffmanTable.MSB; | ||
} | ||
currentTable = 1; | ||
k = 0; | ||
for (i = 8; i < 16; i+=1) { // i+1 is Code length | ||
for (j = 0; j < L[i]; j+=1) { | ||
for (n = 0; n < (temp >> (i - 7)); n+=1) { | ||
tab[(currentTable * 256) + k] = V[i][j] | ((i + 1) << 8); | ||
k+=1; | ||
} | ||
if (k >= 256) { | ||
if (k > 256) { | ||
throw new Error("ERROR: Huffman table error(1)!"); | ||
} | ||
k = 0; | ||
currentTable+=1; | ||
} | ||
} | ||
} | ||
}; | ||
/*** Exports ***/ | ||
var moduleType = typeof module; | ||
if ((moduleType !== 'undefined') && module.exports) { | ||
module.exports = jpeg.lossless.HuffmanTable; | ||
} | ||
},{"./data-stream.js":2,"./utils.js":10}],6:[function(require,module,exports){ | ||
/*jslint browser: true, node: true */ | ||
/*global require, module */ | ||
"use strict"; | ||
/*** Imports ****/ | ||
/** | ||
* jpeg | ||
* @type {*|{}} | ||
*/ | ||
var jpeg = jpeg || {}; | ||
/** | ||
* jpeg.lossless | ||
* @type {*|{}} | ||
*/ | ||
jpeg.lossless = jpeg.lossless || {}; | ||
jpeg.lossless.ComponentSpec = jpeg.lossless.ComponentSpec || ((typeof require !== 'undefined') ? require('./component-spec.js') : null); | ||
jpeg.lossless.DataStream = jpeg.lossless.DataStream || ((typeof require !== 'undefined') ? require('./data-stream.js') : null); | ||
jpeg.lossless.Decoder = jpeg.lossless.Decoder || ((typeof require !== 'undefined') ? require('./decoder.js') : null); | ||
jpeg.lossless.FrameHeader = jpeg.lossless.FrameHeader || ((typeof require !== 'undefined') ? require('./frame-header.js') : null); | ||
jpeg.lossless.HuffmanTable = jpeg.lossless.HuffmanTable || ((typeof require !== 'undefined') ? require('./huffman-table.js') : null); | ||
jpeg.lossless.QuantizationTable = jpeg.lossless.QuantizationTable || ((typeof require !== 'undefined') ? require('./quantization-table.js') : null); | ||
jpeg.lossless.ScanComponent = jpeg.lossless.ScanComponent || ((typeof require !== 'undefined') ? require('./scan-component.js') : null); | ||
jpeg.lossless.ScanHeader = jpeg.lossless.ScanHeader || ((typeof require !== 'undefined') ? require('./scan-header.js') : null); | ||
jpeg.lossless.Utils = jpeg.lossless.Utils || ((typeof require !== 'undefined') ? require('./utils.js') : null); | ||
/*** Exports ***/ | ||
var moduleType = typeof module; | ||
if ((moduleType !== 'undefined') && module.exports) { | ||
module.exports = jpeg; | ||
} | ||
},{"./component-spec.js":1,"./data-stream.js":2,"./decoder.js":3,"./frame-header.js":4,"./huffman-table.js":5,"./quantization-table.js":7,"./scan-component.js":8,"./scan-header.js":9,"./utils.js":10}],7:[function(require,module,exports){ | ||
/* | ||
* Copyright (C) 2015 Michael Martinez | ||
* Changes: Added support for selection values 2-7, fixed minor bugs & | ||
* warnings, split into multiple class files, and general clean up. | ||
* | ||
* 08-25-2015: Helmut Dersch agreed to a license change from LGPL to MIT. | ||
*/ | ||
/* | ||
* Copyright (C) Helmut Dersch | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
/*jslint browser: true, node: true */ | ||
/*global require, module */ | ||
"use strict"; | ||
/*** Imports ***/ | ||
var jpeg = jpeg || {}; | ||
jpeg.lossless = jpeg.lossless || {}; | ||
jpeg.lossless.DataStream = jpeg.lossless.DataStream || ((typeof require !== 'undefined') ? require('./data-stream.js') : null); | ||
jpeg.lossless.Utils = jpeg.lossless.Utils || ((typeof require !== 'undefined') ? require('./utils.js') : null); | ||
/*** Constructor ***/ | ||
jpeg.lossless.QuantizationTable = jpeg.lossless.QuantizationTable || function () { | ||
this.precision = []; // Quantization precision 8 or 16 | ||
this.tq = []; // 1: this table is presented | ||
this.quantTables = jpeg.lossless.Utils.createArray(4, 64); // Tables | ||
this.tq[0] = 0; | ||
this.tq[1] = 0; | ||
this.tq[2] = 0; | ||
this.tq[3] = 0; | ||
}; | ||
/*** Static Methods ***/ | ||
jpeg.lossless.QuantizationTable.enhanceQuantizationTable = function(qtab, table) { | ||
/*jslint bitwise: true */ | ||
var i; | ||
for (i = 0; i < 8; i+=1) { | ||
qtab[table[(0 * 8) + i]] *= 90; | ||
qtab[table[(4 * 8) + i]] *= 90; | ||
qtab[table[(2 * 8) + i]] *= 118; | ||
qtab[table[(6 * 8) + i]] *= 49; | ||
qtab[table[(5 * 8) + i]] *= 71; | ||
qtab[table[(1 * 8) + i]] *= 126; | ||
qtab[table[(7 * 8) + i]] *= 25; | ||
qtab[table[(3 * 8) + i]] *= 106; | ||
} | ||
for (i = 0; i < 8; i+=1) { | ||
qtab[table[0 + (8 * i)]] *= 90; | ||
qtab[table[4 + (8 * i)]] *= 90; | ||
qtab[table[2 + (8 * i)]] *= 118; | ||
qtab[table[6 + (8 * i)]] *= 49; | ||
qtab[table[5 + (8 * i)]] *= 71; | ||
qtab[table[1 + (8 * i)]] *= 126; | ||
qtab[table[7 + (8 * i)]] *= 25; | ||
qtab[table[3 + (8 * i)]] *= 106; | ||
} | ||
for (i = 0; i < 64; i+=1) { | ||
qtab[i] >>= 6; | ||
} | ||
}; | ||
/*** Prototype Methods ***/ | ||
jpeg.lossless.QuantizationTable.prototype.read = function (data, table) { | ||
/*jslint bitwise: true */ | ||
var count = 0, length, temp, t, i; | ||
length = data.get16(); | ||
count += 2; | ||
while (count < length) { | ||
temp = data.get8(); | ||
count+=1; | ||
t = temp & 0x0F; | ||
if (t > 3) { | ||
throw new Error("ERROR: Quantization table ID > 3"); | ||
} | ||
this.precision[t] = temp >> 4; | ||
if (this.precision[t] === 0) { | ||
this.precision[t] = 8; | ||
} else if (this.precision[t] === 1) { | ||
this.precision[t] = 16; | ||
} else { | ||
throw new Error("ERROR: Quantization table precision error"); | ||
} | ||
this.tq[t] = 1; | ||
if (this.precision[t] === 8) { | ||
for (i = 0; i < 64; i+=1) { | ||
if (count > length) { | ||
throw new Error("ERROR: Quantization table format error"); | ||
} | ||
this.quantTables[t][i] = data.get8(); | ||
count+=1; | ||
} | ||
jpeg.lossless.QuantizationTable.enhanceQuantizationTable(this.quantTables[t], table); | ||
} else { | ||
for (i = 0; i < 64; i+=1) { | ||
if (count > length) { | ||
throw new Error("ERROR: Quantization table format error"); | ||
} | ||
this.quantTables[t][i] = data.get16(); | ||
count += 2; | ||
} | ||
jpeg.lossless.QuantizationTable.enhanceQuantizationTable(this.quantTables[t], table); | ||
} | ||
} | ||
if (count !== length) { | ||
throw new Error("ERROR: Quantization table error [count!=Lq]"); | ||
} | ||
return 1; | ||
}; | ||
/*** Exports ***/ | ||
var moduleType = typeof module; | ||
if ((moduleType !== 'undefined') && module.exports) { | ||
module.exports = jpeg.lossless.QuantizationTable; | ||
} | ||
},{"./data-stream.js":2,"./utils.js":10}],8:[function(require,module,exports){ | ||
/* | ||
* Copyright (C) 2015 Michael Martinez | ||
* Changes: Added support for selection values 2-7, fixed minor bugs & | ||
* warnings, split into multiple class files, and general clean up. | ||
* | ||
* 08-25-2015: Helmut Dersch agreed to a license change from LGPL to MIT. | ||
*/ | ||
/* | ||
* Copyright (C) Helmut Dersch | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
/*jslint browser: true, node: true */ | ||
/*global require, module */ | ||
"use strict"; | ||
/*** Imports ***/ | ||
var jpeg = jpeg || {}; | ||
jpeg.lossless = jpeg.lossless || {}; | ||
/*** Constructor ***/ | ||
jpeg.lossless.ScanComponent = jpeg.lossless.ScanComponent || function () { | ||
this.acTabSel = 0; // AC table selector | ||
this.dcTabSel = 0; // DC table selector | ||
this.scanCompSel = 0; // Scan component selector | ||
}; | ||
/*** Exports ***/ | ||
var moduleType = typeof module; | ||
if ((moduleType !== 'undefined') && module.exports) { | ||
module.exports = jpeg.lossless.ScanComponent; | ||
} | ||
},{}],9:[function(require,module,exports){ | ||
/* | ||
* Copyright (C) 2015 Michael Martinez | ||
* Changes: Added support for selection values 2-7, fixed minor bugs & | ||
* warnings, split into multiple class files, and general clean up. | ||
* | ||
* 08-25-2015: Helmut Dersch agreed to a license change from LGPL to MIT. | ||
*/ | ||
/* | ||
* Copyright (C) Helmut Dersch | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
/*jslint browser: true, node: true */ | ||
/*global require, module */ | ||
"use strict"; | ||
/*** Imports ***/ | ||
var jpeg = jpeg || {}; | ||
jpeg.lossless = jpeg.lossless || {}; | ||
jpeg.lossless.DataStream = jpeg.lossless.DataStream || ((typeof require !== 'undefined') ? require('./data-stream.js') : null); | ||
jpeg.lossless.ScanComponent = jpeg.lossless.ScanComponent || ((typeof require !== 'undefined') ? require('./scan-component.js') : null); | ||
/*** Constructor ***/ | ||
jpeg.lossless.ScanHeader = jpeg.lossless.ScanHeader || function () { | ||
this.ah = 0; | ||
this.al = 0; | ||
this.numComp = 0; // Number of components in the scan | ||
this.selection = 0; // Start of spectral or predictor selection | ||
this.spectralEnd = 0; // End of spectral selection | ||
this.components = []; | ||
}; | ||
/*** Prototype Methods ***/ | ||
jpeg.lossless.ScanHeader.prototype.read = function(data) { | ||
/*jslint bitwise: true */ | ||
var count = 0, length, i, temp; | ||
length = data.get16(); | ||
count += 2; | ||
this.numComp = data.get8(); | ||
count+=1; | ||
for (i = 0; i < this.numComp; i+=1) { | ||
this.components[i] = new jpeg.lossless.ScanComponent(); | ||
if (count > length) { | ||
throw new Error("ERROR: scan header format error"); | ||
} | ||
this.components[i].scanCompSel = data.get8(); | ||
count+=1; | ||
temp = data.get8(); | ||
count+=1; | ||
this.components[i].dcTabSel = (temp >> 4); | ||
this.components[i].acTabSel = (temp & 0x0F); | ||
} | ||
this.selection = data.get8(); | ||
count+=1; | ||
this.spectralEnd = data.get8(); | ||
count+=1; | ||
temp = data.get8(); | ||
this.ah = (temp >> 4); | ||
this.al = (temp & 0x0F); | ||
count+=1; | ||
if (count !== length) { | ||
throw new Error("ERROR: scan header format error [count!=Ns]"); | ||
} | ||
return 1; | ||
}; | ||
/*** Exports ***/ | ||
var moduleType = typeof module; | ||
if ((moduleType !== 'undefined') && module.exports) { | ||
module.exports = jpeg.lossless.ScanHeader; | ||
} | ||
},{"./data-stream.js":2,"./scan-component.js":8}],10:[function(require,module,exports){ | ||
/* | ||
* Copyright (C) 2015 Michael Martinez | ||
* Changes: Added support for selection values 2-7, fixed minor bugs & | ||
* warnings, split into multiple class files, and general clean up. | ||
* | ||
* 08-25-2015: Helmut Dersch agreed to a license change from LGPL to MIT. | ||
*/ | ||
/* | ||
* Copyright (C) Helmut Dersch | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
/*jslint browser: true, node: true */ | ||
/*global require, module */ | ||
"use strict"; | ||
/*** Imports ***/ | ||
var jpeg = jpeg || {}; | ||
jpeg.lossless = jpeg.lossless || {}; | ||
/*** Constructor ***/ | ||
jpeg.lossless.Utils = jpeg.lossless.Utils || {}; | ||
/*** Static methods ***/ | ||
// http://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript | ||
jpeg.lossless.Utils.createArray = function (length) { | ||
var arr = new Array(length || 0), | ||
i = length; | ||
if (arguments.length > 1) { | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
while(i--) arr[length-1 - i] = jpeg.lossless.Utils.createArray.apply(this, args); | ||
} | ||
return arr; | ||
}; | ||
// http://stackoverflow.com/questions/18638900/javascript-crc32 | ||
jpeg.lossless.Utils.makeCRCTable = function(){ | ||
var c; | ||
var crcTable = []; | ||
for(var n =0; n < 256; n++){ | ||
c = n; | ||
for(var k =0; k < 8; k++){ | ||
c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1)); | ||
} | ||
crcTable[n] = c; | ||
} | ||
return crcTable; | ||
}; | ||
jpeg.lossless.Utils.crc32 = function(dataView) { | ||
var uint8view = new Uint8Array(dataView.buffer); | ||
var crcTable = jpeg.lossless.Utils.crcTable || (jpeg.lossless.Utils.crcTable = jpeg.lossless.Utils.makeCRCTable()); | ||
var crc = 0 ^ (-1); | ||
for (var i = 0; i < uint8view.length; i++ ) { | ||
crc = (crc >>> 8) ^ crcTable[(crc ^ uint8view[i]) & 0xFF]; | ||
} | ||
return (crc ^ (-1)) >>> 0; | ||
}; | ||
/*** Exports ***/ | ||
var moduleType = typeof module; | ||
if ((moduleType !== 'undefined') && module.exports) { | ||
module.exports = jpeg.lossless.Utils; | ||
} | ||
},{}]},{},[6])(6) | ||
}); |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
6098757
3099
14
5
51