ome-zarr.js
Version:
Some JavaScript utils for simple rendering of OME-Zarr images
1,304 lines (1,303 loc) • 44.2 kB
JavaScript
import { _ as Be } from "./__vite-browser-external-YbutIfMq.js";
function ve(o, d) {
for (var f = 0; f < d.length; f++) {
const i = d[f];
if (typeof i != "string" && !Array.isArray(i)) {
for (const n in i)
if (n !== "default" && !(n in o)) {
const s = Object.getOwnPropertyDescriptor(i, n);
s && Object.defineProperty(o, n, s.get ? s : {
enumerable: !0,
get: () => i[n]
});
}
}
}
return Object.freeze(Object.defineProperty(o, Symbol.toStringTag, { value: "Module" }));
}
function xe(o) {
if (Object.prototype.hasOwnProperty.call(o, "__esModule")) return o;
var d = o.default;
if (typeof d == "function") {
var f = function i() {
return this instanceof i ? Reflect.construct(d, arguments, this.constructor) : d.apply(this, arguments);
};
f.prototype = d.prototype;
} else f = {};
return Object.defineProperty(f, "__esModule", { value: !0 }), Object.keys(o).forEach(function(i) {
var n = Object.getOwnPropertyDescriptor(o, i);
Object.defineProperty(f, i, n.get ? n : {
enumerable: !0,
get: function() {
return o[i];
}
});
}), f;
}
var x = {};
const O = /* @__PURE__ */ xe(Be);
var M = { exports: {} }, Y = { exports: {} }, ee;
function Te() {
if (ee) return Y.exports;
ee = 1;
let o = O, d = O, f = Y.exports = function() {
d.call(this), this._buffers = [], this._buffered = 0, this._reads = [], this._paused = !1, this._encoding = "utf8", this.writable = !0;
};
return o.inherits(f, d), f.prototype.read = function(i, n) {
this._reads.push({
length: Math.abs(i),
// if length < 0 then at most this length
allowLess: i < 0,
func: n
}), process.nextTick(
(function() {
this._process(), this._paused && this._reads && this._reads.length > 0 && (this._paused = !1, this.emit("drain"));
}).bind(this)
);
}, f.prototype.write = function(i, n) {
if (!this.writable)
return this.emit("error", new Error("Stream not writable")), !1;
let s;
return Buffer.isBuffer(i) ? s = i : s = Buffer.from(i, n || this._encoding), this._buffers.push(s), this._buffered += s.length, this._process(), this._reads && this._reads.length === 0 && (this._paused = !0), this.writable && !this._paused;
}, f.prototype.end = function(i, n) {
i && this.write(i, n), this.writable = !1, this._buffers && (this._buffers.length === 0 ? this._end() : (this._buffers.push(null), this._process()));
}, f.prototype.destroySoon = f.prototype.end, f.prototype._end = function() {
this._reads.length > 0 && this.emit("error", new Error("Unexpected end of input")), this.destroy();
}, f.prototype.destroy = function() {
this._buffers && (this.writable = !1, this._reads = null, this._buffers = null, this.emit("close"));
}, f.prototype._processReadAllowingLess = function(i) {
this._reads.shift();
let n = this._buffers[0];
n.length > i.length ? (this._buffered -= i.length, this._buffers[0] = n.slice(i.length), i.func.call(this, n.slice(0, i.length))) : (this._buffered -= n.length, this._buffers.shift(), i.func.call(this, n));
}, f.prototype._processRead = function(i) {
this._reads.shift();
let n = 0, s = 0, e = Buffer.alloc(i.length);
for (; n < i.length; ) {
let r = this._buffers[s++], t = Math.min(r.length, i.length - n);
r.copy(e, n, 0, t), n += t, t !== r.length && (this._buffers[--s] = r.slice(t));
}
s > 0 && this._buffers.splice(0, s), this._buffered -= i.length, i.func.call(this, e);
}, f.prototype._process = function() {
try {
for (; this._buffered > 0 && this._reads && this._reads.length > 0; ) {
let i = this._reads[0];
if (i.allowLess)
this._processReadAllowingLess(i);
else if (this._buffered >= i.length)
this._processRead(i);
else
break;
}
this._buffers && !this.writable && this._end();
} catch (i) {
this.emit("error", i);
}
}, Y.exports;
}
var N = { exports: {} }, U = { exports: {} }, S = {}, te;
function Ce() {
if (te) return S;
te = 1;
let o = [
{
// pass 1 - 1px
x: [0],
y: [0]
},
{
// pass 2 - 1px
x: [4],
y: [0]
},
{
// pass 3 - 2px
x: [0, 4],
y: [4]
},
{
// pass 4 - 4px
x: [2, 6],
y: [0, 4]
},
{
// pass 5 - 8px
x: [0, 2, 4, 6],
y: [2, 6]
},
{
// pass 6 - 16px
x: [1, 3, 5, 7],
y: [0, 2, 4, 6]
},
{
// pass 7 - 32px
x: [0, 1, 2, 3, 4, 5, 6, 7],
y: [1, 3, 5, 7]
}
];
return S.getImagePasses = function(d, f) {
let i = [], n = d % 8, s = f % 8, e = (d - n) / 8, r = (f - s) / 8;
for (let t = 0; t < o.length; t++) {
let l = o[t], h = e * l.x.length, a = r * l.y.length;
for (let u = 0; u < l.x.length && l.x[u] < n; u++)
h++;
for (let u = 0; u < l.y.length && l.y[u] < s; u++)
a++;
h > 0 && a > 0 && i.push({ width: h, height: a, index: t });
}
return i;
}, S.getInterlaceIterator = function(d) {
return function(f, i, n) {
let s = f % o[n].x.length, e = (f - s) / o[n].x.length * 8 + o[n].x[s], r = i % o[n].y.length, t = (i - r) / o[n].y.length * 8 + o[n].y[r];
return e * 4 + t * d * 4;
};
}, S;
}
var q, re;
function Oe() {
return re || (re = 1, q = function(d, f, i) {
let n = d + f - i, s = Math.abs(n - d), e = Math.abs(n - f), r = Math.abs(n - i);
return s <= e && s <= r ? d : e <= r ? f : i;
}), q;
}
var ie;
function Re() {
if (ie) return U.exports;
ie = 1;
let o = Ce(), d = Oe();
function f(n, s, e) {
let r = n * s;
return e !== 8 && (r = Math.ceil(r / (8 / e))), r;
}
let i = U.exports = function(n, s) {
let e = n.width, r = n.height, t = n.interlace, l = n.bpp, h = n.depth;
if (this.read = s.read, this.write = s.write, this.complete = s.complete, this._imageIndex = 0, this._images = [], t) {
let a = o.getImagePasses(e, r);
for (let u = 0; u < a.length; u++)
this._images.push({
byteWidth: f(a[u].width, l, h),
height: a[u].height,
lineIndex: 0
});
} else
this._images.push({
byteWidth: f(e, l, h),
height: r,
lineIndex: 0
});
h === 8 ? this._xComparison = l : h === 16 ? this._xComparison = l * 2 : this._xComparison = 1;
};
return i.prototype.start = function() {
this.read(
this._images[this._imageIndex].byteWidth + 1,
this._reverseFilterLine.bind(this)
);
}, i.prototype._unFilterType1 = function(n, s, e) {
let r = this._xComparison, t = r - 1;
for (let l = 0; l < e; l++) {
let h = n[1 + l], a = l > t ? s[l - r] : 0;
s[l] = h + a;
}
}, i.prototype._unFilterType2 = function(n, s, e) {
let r = this._lastLine;
for (let t = 0; t < e; t++) {
let l = n[1 + t], h = r ? r[t] : 0;
s[t] = l + h;
}
}, i.prototype._unFilterType3 = function(n, s, e) {
let r = this._xComparison, t = r - 1, l = this._lastLine;
for (let h = 0; h < e; h++) {
let a = n[1 + h], u = l ? l[h] : 0, _ = h > t ? s[h - r] : 0, p = Math.floor((_ + u) / 2);
s[h] = a + p;
}
}, i.prototype._unFilterType4 = function(n, s, e) {
let r = this._xComparison, t = r - 1, l = this._lastLine;
for (let h = 0; h < e; h++) {
let a = n[1 + h], u = l ? l[h] : 0, _ = h > t ? s[h - r] : 0, p = h > t && l ? l[h - r] : 0, y = d(_, u, p);
s[h] = a + y;
}
}, i.prototype._reverseFilterLine = function(n) {
let s = n[0], e, r = this._images[this._imageIndex], t = r.byteWidth;
if (s === 0)
e = n.slice(1, t + 1);
else
switch (e = Buffer.alloc(t), s) {
case 1:
this._unFilterType1(n, e, t);
break;
case 2:
this._unFilterType2(n, e, t);
break;
case 3:
this._unFilterType3(n, e, t);
break;
case 4:
this._unFilterType4(n, e, t);
break;
default:
throw new Error("Unrecognised filter type - " + s);
}
this.write(e), r.lineIndex++, r.lineIndex >= r.height ? (this._lastLine = null, this._imageIndex++, r = this._images[this._imageIndex]) : this._lastLine = e, r ? this.read(r.byteWidth + 1, this._reverseFilterLine.bind(this)) : (this._lastLine = null, this.complete());
}, U.exports;
}
var ne;
function Me() {
if (ne) return N.exports;
ne = 1;
let o = O, d = Te(), f = Re(), i = N.exports = function(n) {
d.call(this);
let s = [], e = this;
this._filter = new f(n, {
read: this.read.bind(this),
write: function(r) {
s.push(r);
},
complete: function() {
e.emit("complete", Buffer.concat(s));
}
}), this._filter.start();
};
return o.inherits(i, d), N.exports;
}
var H = { exports: {} }, F, le;
function P() {
return le || (le = 1, F = {
PNG_SIGNATURE: [137, 80, 78, 71, 13, 10, 26, 10],
TYPE_IHDR: 1229472850,
TYPE_IEND: 1229278788,
TYPE_IDAT: 1229209940,
TYPE_PLTE: 1347179589,
TYPE_tRNS: 1951551059,
// eslint-disable-line camelcase
TYPE_gAMA: 1732332865,
// eslint-disable-line camelcase
// color-type bits
COLORTYPE_GRAYSCALE: 0,
COLORTYPE_PALETTE: 1,
COLORTYPE_COLOR: 2,
COLORTYPE_ALPHA: 4,
// e.g. grayscale and alpha
// color-type combinations
COLORTYPE_PALETTE_COLOR: 3,
COLORTYPE_COLOR_ALPHA: 6,
COLORTYPE_TO_BPP_MAP: {
0: 1,
2: 3,
3: 1,
4: 2,
6: 4
},
GAMMA_DIVISION: 1e5
}), F;
}
var D = { exports: {} }, se;
function ke() {
if (se) return D.exports;
se = 1;
let o = [];
(function() {
for (let f = 0; f < 256; f++) {
let i = f;
for (let n = 0; n < 8; n++)
i & 1 ? i = 3988292384 ^ i >>> 1 : i = i >>> 1;
o[f] = i;
}
})();
let d = D.exports = function() {
this._crc = -1;
};
return d.prototype.write = function(f) {
for (let i = 0; i < f.length; i++)
this._crc = o[(this._crc ^ f[i]) & 255] ^ this._crc >>> 8;
return !0;
}, d.prototype.crc32 = function() {
return this._crc ^ -1;
}, d.crc32 = function(f) {
let i = -1;
for (let n = 0; n < f.length; n++)
i = o[(i ^ f[n]) & 255] ^ i >>> 8;
return i ^ -1;
}, D.exports;
}
var he;
function Le() {
if (he) return H.exports;
he = 1;
let o = P(), d = ke(), f = H.exports = function(i, n) {
this._options = i, i.checkCRC = i.checkCRC !== !1, this._hasIHDR = !1, this._hasIEND = !1, this._emittedHeadersFinished = !1, this._palette = [], this._colorType = 0, this._chunks = {}, this._chunks[o.TYPE_IHDR] = this._handleIHDR.bind(this), this._chunks[o.TYPE_IEND] = this._handleIEND.bind(this), this._chunks[o.TYPE_IDAT] = this._handleIDAT.bind(this), this._chunks[o.TYPE_PLTE] = this._handlePLTE.bind(this), this._chunks[o.TYPE_tRNS] = this._handleTRNS.bind(this), this._chunks[o.TYPE_gAMA] = this._handleGAMA.bind(this), this.read = n.read, this.error = n.error, this.metadata = n.metadata, this.gamma = n.gamma, this.transColor = n.transColor, this.palette = n.palette, this.parsed = n.parsed, this.inflateData = n.inflateData, this.finished = n.finished, this.simpleTransparency = n.simpleTransparency, this.headersFinished = n.headersFinished || function() {
};
};
return f.prototype.start = function() {
this.read(o.PNG_SIGNATURE.length, this._parseSignature.bind(this));
}, f.prototype._parseSignature = function(i) {
let n = o.PNG_SIGNATURE;
for (let s = 0; s < n.length; s++)
if (i[s] !== n[s]) {
this.error(new Error("Invalid file signature"));
return;
}
this.read(8, this._parseChunkBegin.bind(this));
}, f.prototype._parseChunkBegin = function(i) {
let n = i.readUInt32BE(0), s = i.readUInt32BE(4), e = "";
for (let t = 4; t < 8; t++)
e += String.fromCharCode(i[t]);
let r = !!(i[4] & 32);
if (!this._hasIHDR && s !== o.TYPE_IHDR) {
this.error(new Error("Expected IHDR on beggining"));
return;
}
if (this._crc = new d(), this._crc.write(Buffer.from(e)), this._chunks[s])
return this._chunks[s](n);
if (!r) {
this.error(new Error("Unsupported critical chunk type " + e));
return;
}
this.read(n + 4, this._skipChunk.bind(this));
}, f.prototype._skipChunk = function() {
this.read(8, this._parseChunkBegin.bind(this));
}, f.prototype._handleChunkEnd = function() {
this.read(4, this._parseChunkEnd.bind(this));
}, f.prototype._parseChunkEnd = function(i) {
let n = i.readInt32BE(0), s = this._crc.crc32();
if (this._options.checkCRC && s !== n) {
this.error(new Error("Crc error - " + n + " - " + s));
return;
}
this._hasIEND || this.read(8, this._parseChunkBegin.bind(this));
}, f.prototype._handleIHDR = function(i) {
this.read(i, this._parseIHDR.bind(this));
}, f.prototype._parseIHDR = function(i) {
this._crc.write(i);
let n = i.readUInt32BE(0), s = i.readUInt32BE(4), e = i[8], r = i[9], t = i[10], l = i[11], h = i[12];
if (e !== 8 && e !== 4 && e !== 2 && e !== 1 && e !== 16) {
this.error(new Error("Unsupported bit depth " + e));
return;
}
if (!(r in o.COLORTYPE_TO_BPP_MAP)) {
this.error(new Error("Unsupported color type"));
return;
}
if (t !== 0) {
this.error(new Error("Unsupported compression method"));
return;
}
if (l !== 0) {
this.error(new Error("Unsupported filter method"));
return;
}
if (h !== 0 && h !== 1) {
this.error(new Error("Unsupported interlace method"));
return;
}
this._colorType = r;
let a = o.COLORTYPE_TO_BPP_MAP[this._colorType];
this._hasIHDR = !0, this.metadata({
width: n,
height: s,
depth: e,
interlace: !!h,
palette: !!(r & o.COLORTYPE_PALETTE),
color: !!(r & o.COLORTYPE_COLOR),
alpha: !!(r & o.COLORTYPE_ALPHA),
bpp: a,
colorType: r
}), this._handleChunkEnd();
}, f.prototype._handlePLTE = function(i) {
this.read(i, this._parsePLTE.bind(this));
}, f.prototype._parsePLTE = function(i) {
this._crc.write(i);
let n = Math.floor(i.length / 3);
for (let s = 0; s < n; s++)
this._palette.push([i[s * 3], i[s * 3 + 1], i[s * 3 + 2], 255]);
this.palette(this._palette), this._handleChunkEnd();
}, f.prototype._handleTRNS = function(i) {
this.simpleTransparency(), this.read(i, this._parseTRNS.bind(this));
}, f.prototype._parseTRNS = function(i) {
if (this._crc.write(i), this._colorType === o.COLORTYPE_PALETTE_COLOR) {
if (this._palette.length === 0) {
this.error(new Error("Transparency chunk must be after palette"));
return;
}
if (i.length > this._palette.length) {
this.error(new Error("More transparent colors than palette size"));
return;
}
for (let n = 0; n < i.length; n++)
this._palette[n][3] = i[n];
this.palette(this._palette);
}
this._colorType === o.COLORTYPE_GRAYSCALE && this.transColor([i.readUInt16BE(0)]), this._colorType === o.COLORTYPE_COLOR && this.transColor([
i.readUInt16BE(0),
i.readUInt16BE(2),
i.readUInt16BE(4)
]), this._handleChunkEnd();
}, f.prototype._handleGAMA = function(i) {
this.read(i, this._parseGAMA.bind(this));
}, f.prototype._parseGAMA = function(i) {
this._crc.write(i), this.gamma(i.readUInt32BE(0) / o.GAMMA_DIVISION), this._handleChunkEnd();
}, f.prototype._handleIDAT = function(i) {
this._emittedHeadersFinished || (this._emittedHeadersFinished = !0, this.headersFinished()), this.read(-i, this._parseIDAT.bind(this, i));
}, f.prototype._parseIDAT = function(i, n) {
if (this._crc.write(n), this._colorType === o.COLORTYPE_PALETTE_COLOR && this._palette.length === 0)
throw new Error("Expected palette not found");
this.inflateData(n);
let s = i - n.length;
s > 0 ? this._handleIDAT(s) : this._handleChunkEnd();
}, f.prototype._handleIEND = function(i) {
this.read(i, this._parseIEND.bind(this));
}, f.prototype._parseIEND = function(i) {
this._crc.write(i), this._hasIEND = !0, this._handleChunkEnd(), this.finished && this.finished();
}, H.exports;
}
var G = {}, ae;
function Ae() {
if (ae) return G;
ae = 1;
let o = Ce(), d = [
// 0 - dummy entry
function() {
},
// 1 - L
// 0: 0, 1: 0, 2: 0, 3: 0xff
function(e, r, t, l) {
if (l === r.length)
throw new Error("Ran out of data");
let h = r[l];
e[t] = h, e[t + 1] = h, e[t + 2] = h, e[t + 3] = 255;
},
// 2 - LA
// 0: 0, 1: 0, 2: 0, 3: 1
function(e, r, t, l) {
if (l + 1 >= r.length)
throw new Error("Ran out of data");
let h = r[l];
e[t] = h, e[t + 1] = h, e[t + 2] = h, e[t + 3] = r[l + 1];
},
// 3 - RGB
// 0: 0, 1: 1, 2: 2, 3: 0xff
function(e, r, t, l) {
if (l + 2 >= r.length)
throw new Error("Ran out of data");
e[t] = r[l], e[t + 1] = r[l + 1], e[t + 2] = r[l + 2], e[t + 3] = 255;
},
// 4 - RGBA
// 0: 0, 1: 1, 2: 2, 3: 3
function(e, r, t, l) {
if (l + 3 >= r.length)
throw new Error("Ran out of data");
e[t] = r[l], e[t + 1] = r[l + 1], e[t + 2] = r[l + 2], e[t + 3] = r[l + 3];
}
], f = [
// 0 - dummy entry
function() {
},
// 1 - L
// 0: 0, 1: 0, 2: 0, 3: 0xff
function(e, r, t, l) {
let h = r[0];
e[t] = h, e[t + 1] = h, e[t + 2] = h, e[t + 3] = l;
},
// 2 - LA
// 0: 0, 1: 0, 2: 0, 3: 1
function(e, r, t) {
let l = r[0];
e[t] = l, e[t + 1] = l, e[t + 2] = l, e[t + 3] = r[1];
},
// 3 - RGB
// 0: 0, 1: 1, 2: 2, 3: 0xff
function(e, r, t, l) {
e[t] = r[0], e[t + 1] = r[1], e[t + 2] = r[2], e[t + 3] = l;
},
// 4 - RGBA
// 0: 0, 1: 1, 2: 2, 3: 3
function(e, r, t) {
e[t] = r[0], e[t + 1] = r[1], e[t + 2] = r[2], e[t + 3] = r[3];
}
];
function i(e, r) {
let t = [], l = 0;
function h() {
if (l === e.length)
throw new Error("Ran out of data");
let a = e[l];
l++;
let u, _, p, y, m, g, c, b;
switch (r) {
default:
throw new Error("unrecognised depth");
case 16:
c = e[l], l++, t.push((a << 8) + c);
break;
case 4:
c = a & 15, b = a >> 4, t.push(b, c);
break;
case 2:
m = a & 3, g = a >> 2 & 3, c = a >> 4 & 3, b = a >> 6 & 3, t.push(b, c, g, m);
break;
case 1:
u = a & 1, _ = a >> 1 & 1, p = a >> 2 & 1, y = a >> 3 & 1, m = a >> 4 & 1, g = a >> 5 & 1, c = a >> 6 & 1, b = a >> 7 & 1, t.push(b, c, g, m, y, p, _, u);
break;
}
}
return {
get: function(a) {
for (; t.length < a; )
h();
let u = t.slice(0, a);
return t = t.slice(a), u;
},
resetAfterLine: function() {
t.length = 0;
},
end: function() {
if (l !== e.length)
throw new Error("extra data found");
}
};
}
function n(e, r, t, l, h, a) {
let u = e.width, _ = e.height, p = e.index;
for (let y = 0; y < _; y++)
for (let m = 0; m < u; m++) {
let g = t(m, y, p);
d[l](r, h, g, a), a += l;
}
return a;
}
function s(e, r, t, l, h, a) {
let u = e.width, _ = e.height, p = e.index;
for (let y = 0; y < _; y++) {
for (let m = 0; m < u; m++) {
let g = h.get(l), c = t(m, y, p);
f[l](r, g, c, a);
}
h.resetAfterLine();
}
}
return G.dataToBitMap = function(e, r) {
let t = r.width, l = r.height, h = r.depth, a = r.bpp, u = r.interlace, _;
h !== 8 && (_ = i(e, h));
let p;
h <= 8 ? p = Buffer.alloc(t * l * 4) : p = new Uint16Array(t * l * 4);
let y = Math.pow(2, h) - 1, m = 0, g, c;
if (u)
g = o.getImagePasses(t, l), c = o.getInterlaceIterator(t, l);
else {
let b = 0;
c = function() {
let E = b;
return b += 4, E;
}, g = [{ width: t, height: l }];
}
for (let b = 0; b < g.length; b++)
h === 8 ? m = n(
g[b],
p,
c,
a,
e,
m
) : s(
g[b],
p,
c,
a,
_,
y
);
if (h === 8) {
if (m !== e.length)
throw new Error("extra data found");
} else
_.end();
return p;
}, G;
}
var z, oe;
function Ie() {
if (oe) return z;
oe = 1;
function o(i, n, s, e, r) {
let t = 0;
for (let l = 0; l < e; l++)
for (let h = 0; h < s; h++) {
let a = r[i[t]];
if (!a)
throw new Error("index " + i[t] + " not in palette");
for (let u = 0; u < 4; u++)
n[t + u] = a[u];
t += 4;
}
}
function d(i, n, s, e, r) {
let t = 0;
for (let l = 0; l < e; l++)
for (let h = 0; h < s; h++) {
let a = !1;
if (r.length === 1 ? r[0] === i[t] && (a = !0) : r[0] === i[t] && r[1] === i[t + 1] && r[2] === i[t + 2] && (a = !0), a)
for (let u = 0; u < 4; u++)
n[t + u] = 0;
t += 4;
}
}
function f(i, n, s, e, r) {
let t = 255, l = Math.pow(2, r) - 1, h = 0;
for (let a = 0; a < e; a++)
for (let u = 0; u < s; u++) {
for (let _ = 0; _ < 4; _++)
n[h + _] = Math.floor(
i[h + _] * t / l + 0.5
);
h += 4;
}
}
return z = function(i, n, s = !1) {
let e = n.depth, r = n.width, t = n.height, l = n.colorType, h = n.transColor, a = n.palette, u = i;
return l === 3 ? o(i, u, r, t, a) : (h && d(i, u, r, t, h), e !== 8 && !s && (e === 16 && (u = Buffer.alloc(r * t * 4)), f(i, u, r, t, e))), u;
}, z;
}
var fe;
function Ye() {
if (fe) return M.exports;
fe = 1;
let o = O, d = O, f = Te(), i = Me(), n = Le(), s = Ae(), e = Ie(), r = M.exports = function(t) {
f.call(this), this._parser = new n(t, {
read: this.read.bind(this),
error: this._handleError.bind(this),
metadata: this._handleMetaData.bind(this),
gamma: this.emit.bind(this, "gamma"),
palette: this._handlePalette.bind(this),
transColor: this._handleTransColor.bind(this),
finished: this._finished.bind(this),
inflateData: this._inflateData.bind(this),
simpleTransparency: this._simpleTransparency.bind(this),
headersFinished: this._headersFinished.bind(this)
}), this._options = t, this.writable = !0, this._parser.start();
};
return o.inherits(r, f), r.prototype._handleError = function(t) {
this.emit("error", t), this.writable = !1, this.destroy(), this._inflate && this._inflate.destroy && this._inflate.destroy(), this._filter && (this._filter.destroy(), this._filter.on("error", function() {
})), this.errord = !0;
}, r.prototype._inflateData = function(t) {
if (!this._inflate)
if (this._bitmapInfo.interlace)
this._inflate = d.createInflate(), this._inflate.on("error", this.emit.bind(this, "error")), this._filter.on("complete", this._complete.bind(this)), this._inflate.pipe(this._filter);
else {
let h = ((this._bitmapInfo.width * this._bitmapInfo.bpp * this._bitmapInfo.depth + 7 >> 3) + 1) * this._bitmapInfo.height, a = Math.max(h, d.Z_MIN_CHUNK);
this._inflate = d.createInflate({ chunkSize: a });
let u = h, _ = this.emit.bind(this, "error");
this._inflate.on("error", function(y) {
u && _(y);
}), this._filter.on("complete", this._complete.bind(this));
let p = this._filter.write.bind(this._filter);
this._inflate.on("data", function(y) {
u && (y.length > u && (y = y.slice(0, u)), u -= y.length, p(y));
}), this._inflate.on("end", this._filter.end.bind(this._filter));
}
this._inflate.write(t);
}, r.prototype._handleMetaData = function(t) {
this._metaData = t, this._bitmapInfo = Object.create(t), this._filter = new i(this._bitmapInfo);
}, r.prototype._handleTransColor = function(t) {
this._bitmapInfo.transColor = t;
}, r.prototype._handlePalette = function(t) {
this._bitmapInfo.palette = t;
}, r.prototype._simpleTransparency = function() {
this._metaData.alpha = !0;
}, r.prototype._headersFinished = function() {
this.emit("metadata", this._metaData);
}, r.prototype._finished = function() {
this.errord || (this._inflate ? this._inflate.end() : this.emit("error", "No Inflate block"));
}, r.prototype._complete = function(t) {
if (this.errord)
return;
let l;
try {
let h = s.dataToBitMap(t, this._bitmapInfo);
l = e(
h,
this._bitmapInfo,
this._options.skipRescale
), h = null;
} catch (h) {
this._handleError(h);
return;
}
this.emit("parsed", l);
}, M.exports;
}
var j = { exports: {} }, Z = { exports: {} }, V, ue;
function Ne() {
if (ue) return V;
ue = 1;
let o = P();
return V = function(d, f, i, n) {
let s = [o.COLORTYPE_COLOR_ALPHA, o.COLORTYPE_ALPHA].indexOf(
n.colorType
) !== -1;
if (n.colorType === n.inputColorType) {
let y = (function() {
let m = new ArrayBuffer(2);
return new DataView(m).setInt16(
0,
256,
!0
/* littleEndian */
), new Int16Array(m)[0] !== 256;
})();
if (n.bitDepth === 8 || n.bitDepth === 16 && y)
return d;
}
let e = n.bitDepth !== 16 ? d : new Uint16Array(d.buffer), r = 255, t = o.COLORTYPE_TO_BPP_MAP[n.inputColorType];
t === 4 && !n.inputHasAlpha && (t = 3);
let l = o.COLORTYPE_TO_BPP_MAP[n.colorType];
n.bitDepth === 16 && (r = 65535, l *= 2);
let h = Buffer.alloc(f * i * l), a = 0, u = 0, _ = n.bgColor || {};
_.red === void 0 && (_.red = r), _.green === void 0 && (_.green = r), _.blue === void 0 && (_.blue = r);
function p() {
let y, m, g, c = r;
switch (n.inputColorType) {
case o.COLORTYPE_COLOR_ALPHA:
c = e[a + 3], y = e[a], m = e[a + 1], g = e[a + 2];
break;
case o.COLORTYPE_COLOR:
y = e[a], m = e[a + 1], g = e[a + 2];
break;
case o.COLORTYPE_ALPHA:
c = e[a + 1], y = e[a], m = y, g = y;
break;
case o.COLORTYPE_GRAYSCALE:
y = e[a], m = y, g = y;
break;
default:
throw new Error(
"input color type:" + n.inputColorType + " is not supported at present"
);
}
return n.inputHasAlpha && (s || (c /= r, y = Math.min(
Math.max(Math.round((1 - c) * _.red + c * y), 0),
r
), m = Math.min(
Math.max(Math.round((1 - c) * _.green + c * m), 0),
r
), g = Math.min(
Math.max(Math.round((1 - c) * _.blue + c * g), 0),
r
))), { red: y, green: m, blue: g, alpha: c };
}
for (let y = 0; y < i; y++)
for (let m = 0; m < f; m++) {
let g = p();
switch (n.colorType) {
case o.COLORTYPE_COLOR_ALPHA:
case o.COLORTYPE_COLOR:
n.bitDepth === 8 ? (h[u] = g.red, h[u + 1] = g.green, h[u + 2] = g.blue, s && (h[u + 3] = g.alpha)) : (h.writeUInt16BE(g.red, u), h.writeUInt16BE(g.green, u + 2), h.writeUInt16BE(g.blue, u + 4), s && h.writeUInt16BE(g.alpha, u + 6));
break;
case o.COLORTYPE_ALPHA:
case o.COLORTYPE_GRAYSCALE: {
let c = (g.red + g.green + g.blue) / 3;
n.bitDepth === 8 ? (h[u] = c, s && (h[u + 1] = g.alpha)) : (h.writeUInt16BE(c, u), s && h.writeUInt16BE(g.alpha, u + 2));
break;
}
default:
throw new Error("unrecognised color Type " + n.colorType);
}
a += t, u += l;
}
return h;
}, V;
}
var W, ce;
function Ue() {
if (ce) return W;
ce = 1;
let o = Oe();
function d(_, p, y, m, g) {
for (let c = 0; c < y; c++)
m[g + c] = _[p + c];
}
function f(_, p, y) {
let m = 0, g = p + y;
for (let c = p; c < g; c++)
m += Math.abs(_[c]);
return m;
}
function i(_, p, y, m, g, c) {
for (let b = 0; b < y; b++) {
let E = b >= c ? _[p + b - c] : 0, w = _[p + b] - E;
m[g + b] = w;
}
}
function n(_, p, y, m) {
let g = 0;
for (let c = 0; c < y; c++) {
let b = c >= m ? _[p + c - m] : 0, E = _[p + c] - b;
g += Math.abs(E);
}
return g;
}
function s(_, p, y, m, g) {
for (let c = 0; c < y; c++) {
let b = p > 0 ? _[p + c - y] : 0, E = _[p + c] - b;
m[g + c] = E;
}
}
function e(_, p, y) {
let m = 0, g = p + y;
for (let c = p; c < g; c++) {
let b = p > 0 ? _[c - y] : 0, E = _[c] - b;
m += Math.abs(E);
}
return m;
}
function r(_, p, y, m, g, c) {
for (let b = 0; b < y; b++) {
let E = b >= c ? _[p + b - c] : 0, w = p > 0 ? _[p + b - y] : 0, R = _[p + b] - (E + w >> 1);
m[g + b] = R;
}
}
function t(_, p, y, m) {
let g = 0;
for (let c = 0; c < y; c++) {
let b = c >= m ? _[p + c - m] : 0, E = p > 0 ? _[p + c - y] : 0, w = _[p + c] - (b + E >> 1);
g += Math.abs(w);
}
return g;
}
function l(_, p, y, m, g, c) {
for (let b = 0; b < y; b++) {
let E = b >= c ? _[p + b - c] : 0, w = p > 0 ? _[p + b - y] : 0, R = p > 0 && b >= c ? _[p + b - (y + c)] : 0, C = _[p + b] - o(E, w, R);
m[g + b] = C;
}
}
function h(_, p, y, m) {
let g = 0;
for (let c = 0; c < y; c++) {
let b = c >= m ? _[p + c - m] : 0, E = p > 0 ? _[p + c - y] : 0, w = p > 0 && c >= m ? _[p + c - (y + m)] : 0, R = _[p + c] - o(b, E, w);
g += Math.abs(R);
}
return g;
}
let a = {
0: d,
1: i,
2: s,
3: r,
4: l
}, u = {
0: f,
1: n,
2: e,
3: t,
4: h
};
return W = function(_, p, y, m, g) {
let c;
if (!("filterType" in m) || m.filterType === -1)
c = [0, 1, 2, 3, 4];
else if (typeof m.filterType == "number")
c = [m.filterType];
else
throw new Error("unrecognised filter types");
m.bitDepth === 16 && (g *= 2);
let b = p * g, E = 0, w = 0, R = Buffer.alloc((b + 1) * y), C = c[0];
for (let k = 0; k < y; k++) {
if (c.length > 1) {
let L = 1 / 0;
for (let A = 0; A < c.length; A++) {
let I = u[c[A]](_, w, b, g);
I < L && (C = c[A], L = I);
}
}
R[E] = C, E++, a[C](_, w, b, R, E, g), E += b, w += b;
}
return R;
}, W;
}
var pe;
function Pe() {
if (pe) return Z.exports;
pe = 1;
let o = P(), d = ke(), f = Ne(), i = Ue(), n = O, s = Z.exports = function(e) {
if (this._options = e, e.deflateChunkSize = e.deflateChunkSize || 32 * 1024, e.deflateLevel = e.deflateLevel != null ? e.deflateLevel : 9, e.deflateStrategy = e.deflateStrategy != null ? e.deflateStrategy : 3, e.inputHasAlpha = e.inputHasAlpha != null ? e.inputHasAlpha : !0, e.deflateFactory = e.deflateFactory || n.createDeflate, e.bitDepth = e.bitDepth || 8, e.colorType = typeof e.colorType == "number" ? e.colorType : o.COLORTYPE_COLOR_ALPHA, e.inputColorType = typeof e.inputColorType == "number" ? e.inputColorType : o.COLORTYPE_COLOR_ALPHA, [
o.COLORTYPE_GRAYSCALE,
o.COLORTYPE_COLOR,
o.COLORTYPE_COLOR_ALPHA,
o.COLORTYPE_ALPHA
].indexOf(e.colorType) === -1)
throw new Error(
"option color type:" + e.colorType + " is not supported at present"
);
if ([
o.COLORTYPE_GRAYSCALE,
o.COLORTYPE_COLOR,
o.COLORTYPE_COLOR_ALPHA,
o.COLORTYPE_ALPHA
].indexOf(e.inputColorType) === -1)
throw new Error(
"option input color type:" + e.inputColorType + " is not supported at present"
);
if (e.bitDepth !== 8 && e.bitDepth !== 16)
throw new Error(
"option bit depth:" + e.bitDepth + " is not supported at present"
);
};
return s.prototype.getDeflateOptions = function() {
return {
chunkSize: this._options.deflateChunkSize,
level: this._options.deflateLevel,
strategy: this._options.deflateStrategy
};
}, s.prototype.createDeflate = function() {
return this._options.deflateFactory(this.getDeflateOptions());
}, s.prototype.filterData = function(e, r, t) {
let l = f(e, r, t, this._options), h = o.COLORTYPE_TO_BPP_MAP[this._options.colorType];
return i(l, r, t, this._options, h);
}, s.prototype._packChunk = function(e, r) {
let t = r ? r.length : 0, l = Buffer.alloc(t + 12);
return l.writeUInt32BE(t, 0), l.writeUInt32BE(e, 4), r && r.copy(l, 8), l.writeInt32BE(
d.crc32(l.slice(4, l.length - 4)),
l.length - 4
), l;
}, s.prototype.packGAMA = function(e) {
let r = Buffer.alloc(4);
return r.writeUInt32BE(Math.floor(e * o.GAMMA_DIVISION), 0), this._packChunk(o.TYPE_gAMA, r);
}, s.prototype.packIHDR = function(e, r) {
let t = Buffer.alloc(13);
return t.writeUInt32BE(e, 0), t.writeUInt32BE(r, 4), t[8] = this._options.bitDepth, t[9] = this._options.colorType, t[10] = 0, t[11] = 0, t[12] = 0, this._packChunk(o.TYPE_IHDR, t);
}, s.prototype.packIDAT = function(e) {
return this._packChunk(o.TYPE_IDAT, e);
}, s.prototype.packIEND = function() {
return this._packChunk(o.TYPE_IEND, null);
}, Z.exports;
}
var _e;
function qe() {
if (_e) return j.exports;
_e = 1;
let o = O, d = O, f = P(), i = Pe(), n = j.exports = function(s) {
d.call(this);
let e = s || {};
this._packer = new i(e), this._deflate = this._packer.createDeflate(), this.readable = !0;
};
return o.inherits(n, d), n.prototype.pack = function(s, e, r, t) {
this.emit("data", Buffer.from(f.PNG_SIGNATURE)), this.emit("data", this._packer.packIHDR(e, r)), t && this.emit("data", this._packer.packGAMA(t));
let l = this._packer.filterData(s, e, r);
this._deflate.on("error", this.emit.bind(this, "error")), this._deflate.on(
"data",
(function(h) {
this.emit("data", this._packer.packIDAT(h));
}).bind(this)
), this._deflate.on(
"end",
(function() {
this.emit("data", this._packer.packIEND()), this.emit("end");
}).bind(this)
), this._deflate.end(l);
}, j.exports;
}
var B = {}, v = { exports: {} }, de;
function He() {
return de || (de = 1, (function(o, d) {
let f = O.ok, i = O, n = O, s = O.kMaxLength;
function e(a) {
if (!(this instanceof e))
return new e(a);
a && a.chunkSize < i.Z_MIN_CHUNK && (a.chunkSize = i.Z_MIN_CHUNK), i.Inflate.call(this, a), this._offset = this._offset === void 0 ? this._outOffset : this._offset, this._buffer = this._buffer || this._outBuffer, a && a.maxLength != null && (this._maxLength = a.maxLength);
}
function r(a) {
return new e(a);
}
function t(a, u) {
a._handle && (a._handle.close(), a._handle = null);
}
e.prototype._processChunk = function(a, u, _) {
if (typeof _ == "function")
return i.Inflate._processChunk.call(this, a, u, _);
let p = this, y = a && a.length, m = this._chunkSize - this._offset, g = this._maxLength, c = 0, b = [], E = 0, w;
this.on("error", function(L) {
w = L;
});
function R(L, A) {
if (p._hadError)
return;
let I = m - A;
if (f(I >= 0, "have should not go down"), I > 0) {
let T = p._buffer.slice(p._offset, p._offset + I);
if (p._offset += I, T.length > g && (T = T.slice(0, g)), b.push(T), E += T.length, g -= T.length, g === 0)
return !1;
}
return (A === 0 || p._offset >= p._chunkSize) && (m = p._chunkSize, p._offset = 0, p._buffer = Buffer.allocUnsafe(p._chunkSize)), A === 0 ? (c += y - L, y = L, !0) : !1;
}
f(this._handle, "zlib binding closed");
let C;
do
C = this._handle.writeSync(
u,
a,
// in
c,
// in_off
y,
// in_len
this._buffer,
// out
this._offset,
//out_off
m
), C = C || this._writeState;
while (!this._hadError && R(C[0], C[1]));
if (this._hadError)
throw w;
if (E >= s)
throw t(this), new RangeError(
"Cannot create final Buffer. It would be larger than 0x" + s.toString(16) + " bytes"
);
let k = Buffer.concat(b, E);
return t(this), k;
}, n.inherits(e, i.Inflate);
function l(a, u) {
if (typeof u == "string" && (u = Buffer.from(u)), !(u instanceof Buffer))
throw new TypeError("Not a string or buffer");
let _ = a._finishFlushFlag;
return _ == null && (_ = i.Z_FINISH), a._processChunk(u, _);
}
function h(a, u) {
return l(new e(u), a);
}
o.exports = d = h, d.Inflate = e, d.createInflate = r, d.inflateSync = h;
})(v, v.exports)), v.exports;
}
var K = { exports: {} }, ye;
function Se() {
if (ye) return K.exports;
ye = 1;
let o = K.exports = function(d) {
this._buffer = d, this._reads = [];
};
return o.prototype.read = function(d, f) {
this._reads.push({
length: Math.abs(d),
// if length < 0 then at most this length
allowLess: d < 0,
func: f
});
}, o.prototype.process = function() {
for (; this._reads.length > 0 && this._buffer.length; ) {
let d = this._reads[0];
if (this._buffer.length && (this._buffer.length >= d.length || d.allowLess)) {
this._reads.shift();
let f = this._buffer;
this._buffer = f.slice(d.length), d.func.call(this, f.slice(0, d.length));
} else
break;
}
if (this._reads.length > 0)
throw new Error("There are some read requests waitng on finished stream");
if (this._buffer.length > 0)
throw new Error("unrecognised content at end of stream");
}, K.exports;
}
var J = {}, ge;
function Fe() {
if (ge) return J;
ge = 1;
let o = Se(), d = Re();
return J.process = function(f, i) {
let n = [], s = new o(f);
return new d(i, {
read: s.read.bind(s),
write: function(r) {
n.push(r);
},
complete: function() {
}
}).start(), s.process(), Buffer.concat(n);
}, J;
}
var Q, me;
function De() {
if (me) return Q;
me = 1;
let o = !0, d = O, f = He();
d.deflateSync || (o = !1);
let i = Se(), n = Fe(), s = Le(), e = Ae(), r = Ie();
return Q = function(t, l) {
if (!o)
throw new Error(
"To use the sync capability of this library in old node versions, please pin pngjs to v2.3.0"
);
let h;
function a(T) {
h = T;
}
let u;
function _(T) {
u = T;
}
function p(T) {
u.transColor = T;
}
function y(T) {
u.palette = T;
}
function m() {
u.alpha = !0;
}
let g;
function c(T) {
g = T;
}
let b = [];
function E(T) {
b.push(T);
}
let w = new i(t);
if (new s(l, {
read: w.read.bind(w),
error: a,
metadata: _,
gamma: c,
palette: y,
transColor: p,
inflateData: E,
simpleTransparency: m
}).start(), w.process(), h)
throw h;
let C = Buffer.concat(b);
b.length = 0;
let k;
if (u.interlace)
k = d.inflateSync(C);
else {
let X = ((u.width * u.bpp * u.depth + 7 >> 3) + 1) * u.height;
k = f(C, {
chunkSize: X,
maxLength: X
});
}
if (C = null, !k || !k.length)
throw new Error("bad png - invalid inflate data response");
let L = n.process(k, u);
C = null;
let A = e.dataToBitMap(L, u);
L = null;
let I = r(
A,
u,
l.skipRescale
);
return u.data = I, u.gamma = g || 0, u;
}, Q;
}
var $, be;
function Ge() {
if (be) return $;
be = 1;
let o = !0, d = O;
d.deflateSync || (o = !1);
let f = P(), i = Pe();
return $ = function(n, s) {
if (!o)
throw new Error(
"To use the sync capability of this library in old node versions, please pin pngjs to v2.3.0"
);
let e = s || {}, r = new i(e), t = [];
t.push(Buffer.from(f.PNG_SIGNATURE)), t.push(r.packIHDR(n.width, n.height)), n.gamma && t.push(r.packGAMA(n.gamma));
let l = r.filterData(
n.data,
n.width,
n.height
), h = d.deflateSync(
l,
r.getDeflateOptions()
);
if (l = null, !h || !h.length)
throw new Error("bad png - invalid compressed data response");
return t.push(r.packIDAT(h)), t.push(r.packIEND()), Buffer.concat(t);
}, $;
}
var Ee;
function ze() {
if (Ee) return B;
Ee = 1;
let o = De(), d = Ge();
return B.read = function(f, i) {
return o(f, i || {});
}, B.write = function(f, i) {
return d(f, i);
}, B;
}
var we;
function je() {
if (we) return x;
we = 1;
let o = O, d = O, f = Ye(), i = qe(), n = ze(), s = x.PNG = function(e) {
d.call(this), e = e || {}, this.width = e.width | 0, this.height = e.height | 0, this.data = this.width > 0 && this.height > 0 ? Buffer.alloc(4 * this.width * this.height) : null, e.fill && this.data && this.data.fill(0), this.gamma = 0, this.readable = this.writable = !0, this._parser = new f(e), this._parser.on("error", this.emit.bind(this, "error")), this._parser.on("close", this._handleClose.bind(this)), this._parser.on("metadata", this._metadata.bind(this)), this._parser.on("gamma", this._gamma.bind(this)), this._parser.on(
"parsed",
(function(r) {
this.data = r, this.emit("parsed", r);
}).bind(this)
), this._packer = new i(e), this._packer.on("data", this.emit.bind(this, "data")), this._packer.on("end", this.emit.bind(this, "end")), this._parser.on("close", this._handleClose.bind(this)), this._packer.on("error", this.emit.bind(this, "error"));
};
return o.inherits(s, d), s.sync = n, s.prototype.pack = function() {
return !this.data || !this.data.length ? (this.emit("error", "No data provided"), this) : (process.nextTick(
(function() {
this._packer.pack(this.data, this.width, this.height, this.gamma);
}).bind(this)
), this);
}, s.prototype.parse = function(e, r) {
if (r) {
let t, l;
t = (function(h) {
this.removeListener("error", l), this.data = h, r(null, this);
}).bind(this), l = (function(h) {
this.removeListener("parsed", t), r(h, null);
}).bind(this), this.once("parsed", t), this.once("error", l);
}
return this.end(e), this;
}, s.prototype.write = function(e) {
return this._parser.write(e), !0;
}, s.prototype.end = function(e) {
this._parser.end(e);
}, s.prototype._metadata = function(e) {
this.width = e.width, this.height = e.height, this.emit("metadata", e);
}, s.prototype._gamma = function(e) {
this.gamma = e;
}, s.prototype._handleClose = function() {
!this._parser.writable && !this._packer.readable && this.emit("close");
}, s.bitblt = function(e, r, t, l, h, a, u, _) {
if (t |= 0, l |= 0, h |= 0, a |= 0, u |= 0, _ |= 0, t > e.width || l > e.height || t + h > e.width || l + a > e.height)
throw new Error("bitblt reading outside image");
if (u > r.width || _ > r.height || u + h > r.width || _ + a > r.height)
throw new Error("bitblt writing outside image");
for (let p = 0; p < a; p++)
e.data.copy(
r.data,
(_ + p) * r.width + u << 2,
(l + p) * e.width + t << 2,
(l + p) * e.width + t + h << 2
);
}, s.prototype.bitblt = function(e, r, t, l, h, a, u) {
return s.bitblt(this, e, r, t, l, h, a, u), this;
}, s.adjustGamma = function(e) {
if (e.gamma) {
for (let r = 0; r < e.height; r++)
for (let t = 0; t < e.width; t++) {
let l = e.width * r + t << 2;
for (let h = 0; h < 3; h++) {
let a = e.data[l + h] / 255;
a = Math.pow(a, 1 / 2.2 / e.gamma), e.data[l + h] = Math.round(a * 255);
}
}
e.gamma = 0;
}
}, s.prototype.adjustGamma = function() {
s.adjustGamma(this);
}, x;
}
var Ze = je();
const We = /* @__PURE__ */ ve({
__proto__: null
}, [Ze]);
export {
We as p
};