1
0
mirror of https://github.com/kettek/png-js synced 2025-03-14 06:24:30 -07:00
png-js/dist/png-js.cjs.min.js

2 lines
5.6 KiB
JavaScript
Raw Normal View History

2020-01-14 19:46:10 -08:00
"use strict";function _interopDefault(t){return t&&"object"==typeof t&&"default"in t?t.default:t}Object.defineProperty(exports,"__esModule",{value:!0});var fs=_interopDefault(require("fs")),zlib=_interopDefault(require("zlib")),util=require("util"),range=function(t,e,i){for(var a=[],r=t<e,s=i?r?e+1:e-1:e,h=t;r?h<s:h>s;r?h++:h--)a.push(h);return a},classCallCheck=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},createClass=function(){function t(t,e){for(var i=0;i<e.length;i++){var a=e[i];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(t,a.key,a)}}return function(e,i,a){return i&&t(e.prototype,i),a&&t(e,a),e}}(),toConsumableArray=function(t){if(Array.isArray(t)){for(var e=0,i=Array(t.length);e<t.length;e++)i[e]=t[e];return i}return Array.from(t)},inflateAsync=util.promisify(zlib.inflate),readFileAsync=util.promisify(fs.readFile),IndexedPNG=function(){function t(e){classCallCheck(this,t),this.data=e,this.pos=8,this.palette=[],this.imgData=[],this.transparency={},this.text={},this.process()}return createClass(t,[{key:"process",value:function(){for(var t=this,e=void 0;;){var i,a=this.readUInt32();switch(function(){var i=[];for(e=0;e<4;e++)i.push(String.fromCharCode(t.data[t.pos++]));return i}().join("")){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(a);break;case"IDAT":for(e=0,i=a;e<i;e++)this.imgData.push(this.data[this.pos++]);break;case"tRNS":switch(this.transparency={},this.colorType){case 3:this.transparency.indexed=this.read(a);var r,s,h=this.transparency.indexed.length-1;if(h>0)for(e=0,r=0<=(s=h);r?e<s:e>s;r?e++:e--)this.transparency.indexed.push(255);break;case 0:this.transparency.grayscale=this.read(a)[0];break;case 2:this.transparency.rgb=this.read(a)}break;case"tEXt":var o=this.read(a),n=o.indexOf(0),l=String.fromCharCode.apply(String,toConsumableArray(Array.from(o.slice(0,n)||[])));this.text[l]=String.fromCharCode.apply(String,toConsumableArray(Array.from(o.slice(n+1)||[])));break;case"IEND":this.colors=function(){switch(t.colorType){case 0:case 3:case 4:return 1;case 2:case 6:return 3}}(),this.hasAlphaChannel=[4,6].includes(this.colorType);var c=this.colors+(this.hasAlphaChannel?1:0);return this.pixelBitlength=this.bits*c,this.colorSpace=function(){switch(t.colors){case 1:return"DeviceGray";case 3:return"DeviceRGB"}}(),void(this.imgData=Buffer.from(this.imgData));default:this.pos+=a}if(this.pos+=4,this.pos>this.data.length)throw new Error("Incomplete or corrupt IndexedPNG file")}}},{key:"read",value:function(t){var e=this;return range(0,t,!1).map(function(t){return e.data[e.pos++]})}},{key:"readUInt32",value:function(){return this.data[this.pos++]<<24|this.data[this.pos++]<<16|this.data[this.pos++]<<8|this.data[this.pos++]}},{key:"readUInt16",value:function(){return this.data[this.pos++]<<8|this.data[this.pos++]}},{key:"decodePixels",value:async function(){var t=void 0;try{t=await inflateAsync(this.imgData)}catch(t){throw t}for(var e=this.pixelBitlength/8,i=e*this.width,a=Buffer.allocUnsafe(i*this.height),r=t.length,s=0,h=0,o=0;h<r;){var n,l,c,d,p,u,f,y,v,w;switch(t[h++]){case 0:for(c=0,u=i;c<u;c++)a[o++]=t[h++];break;case 1:for(c=0,f=i;c<f;c++)n=t[h++],d=c<e?0:a[o-e],a[o++]=(n+d)%256;break;case 2:for(c=0,y=i;c<y;c++)n=t[h++],l=(c-c%e)/e,p=s&&a[(s-1)*i+l*e+c%e],a[o++]=(p+n)%256;break;case 3:for(c=0,v=i;c<v;c++)n=t[h++],l=(c-c%e)/e,d=c<e?0:a[o-e],p=s&&a[(s-1)*i+l*e+c%e],a[o++]=(n+Math.floor((d+p)/2))%256;break;case 4:for(c=0,w=i;c<w;c++){var g,b;n=t[h++],l=(c-c%e)/e,d=c<e?0:a[o-e],0===s?p=b=0:(p=a[(s-1)*i+l*e+c%e],b=l&&a[(s-1)*i+(l-1)*e+c%e]);var x=d+p-b,m=Math.abs(x-d),k=Math.abs(x-p),C=Math.abs(x-b);g=m<=k&&m<=C?d:k<=C?p:b,a[o++]=(n+g)%256}break;default:throw new Error("Invalid filter algorithm: "+t[h-1])}s++}return a}},{key:"decodePalette",value:function(){for(