dotfiles/vscode/.vscode/extensions/ms-toolsai.jupyter-2024.6.0-darwin-arm64/dist/node_modules/png-js.js
Errol Sancaktar 5f8db31398 alacritty
2024-07-15 17:06:13 -06:00

2 lines
3.9 KiB
JavaScript

var U=require("fs"),G=require("zlib");module.exports=class S{static decode(i,s){return U.readFile(i,function(a,h){return new S(h).decode(t=>s(t))})}static load(i){let s=U.readFileSync(i);return new S(s)}constructor(i){let s;for(this.data=i,this.pos=8,this.palette=[],this.imgData=[],this.transparency={},this.text={};;){let r=this.readUInt32(),l="";for(s=0;s<4;s++)l+=String.fromCharCode(this.data[this.pos++]);switch(l){case"IHDR":this.width=this.readUInt32(),this.height=this.readUInt32(),this.bits=this.data[this.pos++],this.colorType=this.data[this.pos++],this.compressionMethod=this.data[this.pos++],this.filterMethod=this.data[this.pos++],this.interlaceMethod=this.data[this.pos++];break;case"PLTE":this.palette=this.read(r);break;case"IDAT":for(s=0;s<r;s++)this.imgData.push(this.data[this.pos++]);break;case"tRNS":switch(this.transparency={},this.colorType){case 3:this.transparency.indexed=this.read(r);var a=255-this.transparency.indexed.length;if(a>0)for(s=0;s<a;s++)this.transparency.indexed.push(255);break;case 0:this.transparency.grayscale=this.read(r)[0];break;case 2:this.transparency.rgb=this.read(r);break}break;case"tEXt":var h=this.read(r),c=h.indexOf(0),t=String.fromCharCode.apply(String,h.slice(0,c));this.text[t]=String.fromCharCode.apply(String,h.slice(c+1));break;case"IEND":switch(this.colorType){case 0:case 3:case 4:this.colors=1;break;case 2:case 6:this.colors=3;break}this.hasAlphaChannel=[4,6].includes(this.colorType);var f=this.colors+(this.hasAlphaChannel?1:0);switch(this.pixelBitlength=this.bits*f,this.colors){case 1:this.colorSpace="DeviceGray";break;case 3:this.colorSpace="DeviceRGB";break}this.imgData=new Buffer(this.imgData);return;default:this.pos+=r}if(this.pos+=4,this.pos>this.data.length)throw new Error("Incomplete or corrupt PNG file")}}read(i){let s=new Array(i);for(let a=0;a<i;a++)s[a]=this.data[this.pos++];return s}readUInt32(){let i=this.data[this.pos++]<<24,s=this.data[this.pos++]<<16,a=this.data[this.pos++]<<8,h=this.data[this.pos++];return i|s|a|h}readUInt16(){let i=this.data[this.pos++]<<8,s=this.data[this.pos++];return i|s}decodePixels(i){return G.inflate(this.imgData,(s,a)=>{if(s)throw s;let{width:h,height:c}=this,t=this.pixelBitlength/8,f=new Buffer(h*c*t),{length:r}=a,l=0;function o(n,v,B,C,x=!1){let T=Math.ceil((h-n)/B),A=Math.ceil((c-v)/C),p=t*T,d=x?f:new Buffer(p*A),g=0,b=0;for(;g<A&&l<r;){var u,k,e,m,w;switch(a[l++]){case 0:for(e=0;e<p;e++)d[b++]=a[l++];break;case 1:for(e=0;e<p;e++)u=a[l++],m=e<t?0:d[b-t],d[b++]=(u+m)%256;break;case 2:for(e=0;e<p;e++)u=a[l++],k=(e-e%t)/t,w=g&&d[(g-1)*p+k*t+e%t],d[b++]=(w+u)%256;break;case 3:for(e=0;e<p;e++)u=a[l++],k=(e-e%t)/t,m=e<t?0:d[b-t],w=g&&d[(g-1)*p+k*t+e%t],d[b++]=(u+Math.floor((m+w)/2))%256;break;case 4:for(e=0;e<p;e++){var M,I;u=a[l++],k=(e-e%t)/t,m=e<t?0:d[b-t],g===0?w=I=0:(w=d[(g-1)*p+k*t+e%t],I=k&&d[(g-1)*p+(k-1)*t+e%t]);let y=m+w-I,P=Math.abs(y-m),D=Math.abs(y-w),E=Math.abs(y-I);P<=D&&P<=E?M=m:D<=E?M=w:M=I,d[b++]=(u+M)%256}break;default:throw new Error(`Invalid filter algorithm: ${a[l-1]}`)}if(!x){let y=((v+g*C)*h+n)*t,P=g*p;for(e=0;e<T;e++){for(let D=0;D<t;D++)f[y++]=d[P++];y+=(B-1)*t}}g++}}return this.interlaceMethod===1?(o(0,0,8,8),o(4,0,8,8),o(0,4,4,8),o(2,0,4,4),o(0,2,2,4),o(1,0,2,2),o(0,1,1,2)):o(0,0,1,1,!0),i(f)})}decodePalette(){let{palette:i}=this,{length:s}=i,a=this.transparency.indexed||[],h=new Buffer(a.length+s),c=0,t=0;for(let r=0;r<s;r+=3){var f;h[c++]=i[r],h[c++]=i[r+1],h[c++]=i[r+2],h[c++]=(f=a[t++])!=null?f:255}return h}copyToImageData(i,s){let a,h,{colors:c}=this,t=null,f=this.hasAlphaChannel;this.palette.length&&(t=this._decodedPalette||(this._decodedPalette=this.decodePalette()),c=4,f=!0);let r=i.data||i,{length:l}=r,o=t||s,n=a=0;if(c===1)for(;n<l;){h=t?s[n/4]*4:a;let v=o[h++];r[n++]=v,r[n++]=v,r[n++]=v,r[n++]=f?o[h++]:255,a=h}else for(;n<l;)h=t?s[n/4]*4:a,r[n++]=o[h++],r[n++]=o[h++],r[n++]=o[h++],r[n++]=f?o[h++]:255,a=h}decode(i){let s=new Buffer(this.width*this.height*4);return this.decodePixels(a=>(this.copyToImageData(s,a),i(s)))}};