diff --git a/packages/baize-compress-image/CHANGELOG.md b/packages/baize-compress-image/CHANGELOG.md new file mode 100644 index 0000000..7fd3eb0 --- /dev/null +++ b/packages/baize-compress-image/CHANGELOG.md @@ -0,0 +1,7 @@ +# baize-compress-image + +## 0.0.4 + +### Patch Changes + +- 添加 png 和 webp 图片的压缩适配 diff --git a/packages/baize-compress-image/lib/UPNG.min.js b/packages/baize-compress-image/lib/UPNG.min.js new file mode 100644 index 0000000..6ab1bf0 --- /dev/null +++ b/packages/baize-compress-image/lib/UPNG.min.js @@ -0,0 +1,915 @@ +/** + * Minified by jsDelivr using Terser v5.3.5. + * Original file: /npm/upng-js@2.1.0/UPNG.js + * + * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files + */ +!(function () { + var e = {}; + "object" == typeof module ? (module.exports = e) : (self.UPNG = e), + (function (e, r) { + (e.toRGBA8 = function (r) { + var t = r.width, + n = r.height; + if (null == r.tabs.acTL) return [e.toRGBA8.decodeImage(r.data, t, n, r).buffer]; + var a = []; + null == r.frames[0].data && (r.frames[0].data = r.data); + for (var i, f = new Uint8Array(t * n * 4), o = 0; o < r.frames.length; o++) { + var s = r.frames[o], + l = s.rect.x, + c = s.rect.y, + u = s.rect.width, + d = s.rect.height, + h = e.toRGBA8.decodeImage(s.data, u, d, r); + if ( + (0 == o + ? (i = h) + : 0 == s.blend + ? e._copyTile(h, u, d, i, t, n, l, c, 0) + : 1 == s.blend && e._copyTile(h, u, d, i, t, n, l, c, 1), + a.push(i.buffer), + (i = i.slice(0)), + 0 == s.dispose) + ); + else if (1 == s.dispose) e._copyTile(f, u, d, i, t, n, l, c, 0); + else if (2 == s.dispose) { + for (var v = o - 1; 2 == r.frames[v].dispose; ) v--; + i = new Uint8Array(a[v]).slice(0); + } + } + return a; + }), + (e.toRGBA8.decodeImage = function (r, t, n, a) { + var i = t * n, + f = e.decode._getBPP(a), + o = Math.ceil((t * f) / 8), + s = new Uint8Array(4 * i), + l = new Uint32Array(s.buffer), + c = a.ctype, + u = a.depth, + d = e._bin.readUshort; + if (6 == c) { + var h = i << 2; + if (8 == u) for (var v = 0; v < h; v++) s[v] = r[v]; + if (16 == u) for (v = 0; v < h; v++) s[v] = r[v << 1]; + } else if (2 == c) { + var p = a.tabs.tRNS, + b = -1, + g = -1, + y = -1; + if ((p && ((b = p[0]), (g = p[1]), (y = p[2])), 8 == u)) + for (v = 0; v < i; v++) { + var m = 3 * v; + (s[(M = v << 2)] = r[m]), + (s[M + 1] = r[m + 1]), + (s[M + 2] = r[m + 2]), + (s[M + 3] = 255), + -1 != b && r[m] == b && r[m + 1] == g && r[m + 2] == y && (s[M + 3] = 0); + } + if (16 == u) + for (v = 0; v < i; v++) { + m = 6 * v; + (s[(M = v << 2)] = r[m]), + (s[M + 1] = r[m + 2]), + (s[M + 2] = r[m + 4]), + (s[M + 3] = 255), + -1 != b && d(r, m) == b && d(r, m + 2) == g && d(r, m + 4) == y && (s[M + 3] = 0); + } + } else if (3 == c) { + var w = a.tabs.PLTE, + A = a.tabs.tRNS, + U = A ? A.length : 0; + if (1 == u) + for (var _ = 0; _ < n; _++) { + var I = _ * o, + q = _ * t; + for (v = 0; v < t; v++) { + var M = (q + v) << 2, + T = 3 * (R = (r[I + (v >> 3)] >> (7 - ((7 & v) << 0))) & 1); + (s[M] = w[T]), (s[M + 1] = w[T + 1]), (s[M + 2] = w[T + 2]), (s[M + 3] = R < U ? A[R] : 255); + } + } + if (2 == u) + for (_ = 0; _ < n; _++) + for (I = _ * o, q = _ * t, v = 0; v < t; v++) { + (M = (q + v) << 2), (T = 3 * (R = (r[I + (v >> 2)] >> (6 - ((3 & v) << 1))) & 3)); + (s[M] = w[T]), (s[M + 1] = w[T + 1]), (s[M + 2] = w[T + 2]), (s[M + 3] = R < U ? A[R] : 255); + } + if (4 == u) + for (_ = 0; _ < n; _++) + for (I = _ * o, q = _ * t, v = 0; v < t; v++) { + (M = (q + v) << 2), (T = 3 * (R = (r[I + (v >> 1)] >> (4 - ((1 & v) << 2))) & 15)); + (s[M] = w[T]), (s[M + 1] = w[T + 1]), (s[M + 2] = w[T + 2]), (s[M + 3] = R < U ? A[R] : 255); + } + if (8 == u) + for (v = 0; v < i; v++) { + var R; + (M = v << 2), (T = 3 * (R = r[v])); + (s[M] = w[T]), (s[M + 1] = w[T + 1]), (s[M + 2] = w[T + 2]), (s[M + 3] = R < U ? A[R] : 255); + } + } else if (4 == c) { + if (8 == u) + for (v = 0; v < i; v++) { + M = v << 2; + var N = r[(z = v << 1)]; + (s[M] = N), (s[M + 1] = N), (s[M + 2] = N), (s[M + 3] = r[z + 1]); + } + if (16 == u) + for (v = 0; v < i; v++) { + var z; + (M = v << 2), (N = r[(z = v << 2)]); + (s[M] = N), (s[M + 1] = N), (s[M + 2] = N), (s[M + 3] = r[z + 2]); + } + } else if (0 == c) { + b = a.tabs.tRNS ? a.tabs.tRNS : -1; + if (1 == u) + for (v = 0; v < i; v++) { + var P = (N = 255 * ((r[v >> 3] >> (7 - (7 & v))) & 1)) == 255 * b ? 0 : 255; + l[v] = (P << 24) | (N << 16) | (N << 8) | N; + } + if (2 == u) + for (v = 0; v < i; v++) { + P = (N = 85 * ((r[v >> 2] >> (6 - ((3 & v) << 1))) & 3)) == 85 * b ? 0 : 255; + l[v] = (P << 24) | (N << 16) | (N << 8) | N; + } + if (4 == u) + for (v = 0; v < i; v++) { + P = (N = 17 * ((r[v >> 1] >> (4 - ((1 & v) << 2))) & 15)) == 17 * b ? 0 : 255; + l[v] = (P << 24) | (N << 16) | (N << 8) | N; + } + if (8 == u) + for (v = 0; v < i; v++) { + P = (N = r[v]) == b ? 0 : 255; + l[v] = (P << 24) | (N << 16) | (N << 8) | N; + } + if (16 == u) + for (v = 0; v < i; v++) { + (N = r[v << 1]), (P = d(r, v << 1) == b ? 0 : 255); + l[v] = (P << 24) | (N << 16) | (N << 8) | N; + } + } + return s; + }), + (e.decode = function (r) { + for ( + var t, + n = new Uint8Array(r), + a = 8, + i = e._bin, + f = i.readUshort, + o = i.readUint, + s = { tabs: {}, frames: [] }, + l = new Uint8Array(n.length), + c = 0, + u = 0, + d = [137, 80, 78, 71, 13, 10, 26, 10], + h = 0; + h < 8; + h++ + ) + if (n[h] != d[h]) throw "The input is not a PNG file!"; + for (; a < n.length; ) { + var v = i.readUint(n, a); + a += 4; + var p = i.readASCII(n, a, 4); + if (((a += 4), "IHDR" == p)) e.decode._IHDR(n, a, s); + else if ("IDAT" == p) { + for (h = 0; h < v; h++) l[c + h] = n[a + h]; + c += v; + } else if ("acTL" == p) + (s.tabs[p] = { num_frames: o(n, a), num_plays: o(n, a + 4) }), (t = new Uint8Array(n.length)); + else if ("fcTL" == p) { + if (0 != u) + ((I = s.frames[s.frames.length - 1]).data = e.decode._decompress( + s, + t.slice(0, u), + I.rect.width, + I.rect.height, + )), + (u = 0); + var b = { x: o(n, a + 12), y: o(n, a + 16), width: o(n, a + 4), height: o(n, a + 8) }, + g = f(n, a + 22); + g = f(n, a + 20) / (0 == g ? 100 : g); + var y = { rect: b, delay: Math.round(1e3 * g), dispose: n[a + 24], blend: n[a + 25] }; + s.frames.push(y); + } else if ("fdAT" == p) { + for (h = 0; h < v - 4; h++) t[u + h] = n[a + h + 4]; + u += v - 4; + } else if ("pHYs" == p) s.tabs[p] = [i.readUint(n, a), i.readUint(n, a + 4), n[a + 8]]; + else if ("cHRM" == p) { + s.tabs[p] = []; + for (h = 0; h < 8; h++) s.tabs[p].push(i.readUint(n, a + 4 * h)); + } else if ("tEXt" == p) { + null == s.tabs[p] && (s.tabs[p] = {}); + var m = i.nextZero(n, a), + w = i.readASCII(n, a, m - a), + A = i.readASCII(n, m + 1, a + v - m - 1); + s.tabs[p][w] = A; + } else if ("iTXt" == p) { + null == s.tabs[p] && (s.tabs[p] = {}); + m = 0; + var U = a; + m = i.nextZero(n, U); + (w = i.readASCII(n, U, m - U)), n[(U = m + 1)], n[U + 1]; + (U += 2), (m = i.nextZero(n, U)); + i.readASCII(n, U, m - U); + (U = m + 1), (m = i.nextZero(n, U)); + i.readUTF8(n, U, m - U); + U = m + 1; + A = i.readUTF8(n, U, v - (U - a)); + s.tabs[p][w] = A; + } else if ("PLTE" == p) s.tabs[p] = i.readBytes(n, a, v); + else if ("hIST" == p) { + var _ = s.tabs.PLTE.length / 3; + s.tabs[p] = []; + for (h = 0; h < _; h++) s.tabs[p].push(f(n, a + 2 * h)); + } else if ("tRNS" == p) + 3 == s.ctype + ? (s.tabs[p] = i.readBytes(n, a, v)) + : 0 == s.ctype + ? (s.tabs[p] = f(n, a)) + : 2 == s.ctype && (s.tabs[p] = [f(n, a), f(n, a + 2), f(n, a + 4)]); + else if ("gAMA" == p) s.tabs[p] = i.readUint(n, a) / 1e5; + else if ("sRGB" == p) s.tabs[p] = n[a]; + else if ("bKGD" == p) + 0 == s.ctype || 4 == s.ctype + ? (s.tabs[p] = [f(n, a)]) + : 2 == s.ctype || 6 == s.ctype + ? (s.tabs[p] = [f(n, a), f(n, a + 2), f(n, a + 4)]) + : 3 == s.ctype && (s.tabs[p] = n[a]); + else if ("IEND" == p) { + var I; + if (0 != u) + ((I = s.frames[s.frames.length - 1]).data = e.decode._decompress( + s, + t.slice(0, u), + I.rect.width, + I.rect.height, + )), + (u = 0); + s.data = e.decode._decompress(s, l, s.width, s.height); + break; + } + a += v; + i.readUint(n, a); + a += 4; + } + return delete s.compress, delete s.interlace, delete s.filter, s; + }), + (e.decode._decompress = function (r, t, n, a) { + return ( + 0 == r.compress && (t = e.decode._inflate(t)), + 0 == r.interlace + ? (t = e.decode._filterZero(t, r, 0, n, a)) + : 1 == r.interlace && (t = e.decode._readInterlace(t, r)), + t + ); + }), + (e.decode._inflate = function (e) { + return r.inflate(e); + }), + (e.decode._readInterlace = function (r, t) { + for ( + var n = t.width, + a = t.height, + i = e.decode._getBPP(t), + f = i >> 3, + o = Math.ceil((n * i) / 8), + s = new Uint8Array(a * o), + l = 0, + c = [0, 0, 4, 0, 2, 0, 1], + u = [0, 4, 0, 2, 0, 1, 0], + d = [8, 8, 8, 4, 4, 2, 2], + h = [8, 8, 4, 4, 2, 2, 1], + v = 0; + v < 7; + + ) { + for (var p = d[v], b = h[v], g = 0, y = 0, m = c[v]; m < a; ) (m += p), y++; + for (var w = u[v]; w < n; ) (w += b), g++; + var A = Math.ceil((g * i) / 8); + e.decode._filterZero(r, t, l, g, y); + for (var U = 0, _ = c[v]; _ < a; ) { + for (var I = u[v], q = (l + U * A) << 3; I < n; ) { + var M; + if (1 == i) + (M = ((M = r[q >> 3]) >> (7 - (7 & q))) & 1), (s[_ * o + (I >> 3)] |= M << (7 - ((3 & I) << 0))); + if (2 == i) + (M = ((M = r[q >> 3]) >> (6 - (7 & q))) & 3), (s[_ * o + (I >> 2)] |= M << (6 - ((3 & I) << 1))); + if (4 == i) + (M = ((M = r[q >> 3]) >> (4 - (7 & q))) & 15), (s[_ * o + (I >> 1)] |= M << (4 - ((1 & I) << 2))); + if (i >= 8) for (var T = _ * o + I * f, R = 0; R < f; R++) s[T + R] = r[(q >> 3) + R]; + (q += i), (I += b); + } + U++, (_ += p); + } + g * y != 0 && (l += y * (1 + A)), (v += 1); + } + return s; + }), + (e.decode._getBPP = function (e) { + return [1, null, 3, 1, 2, null, 4][e.ctype] * e.depth; + }), + (e.decode._filterZero = function (r, t, n, a, i) { + var f = e.decode._getBPP(t), + o = Math.ceil((a * f) / 8), + s = e.decode._paeth; + f = Math.ceil(f / 8); + for (var l = 0; l < i; l++) { + var c = n + l * o, + u = c + l + 1, + d = r[u - 1]; + if (0 == d) for (var h = 0; h < o; h++) r[c + h] = r[u + h]; + else if (1 == d) { + for (h = 0; h < f; h++) r[c + h] = r[u + h]; + for (h = f; h < o; h++) r[c + h] = (r[u + h] + r[c + h - f]) & 255; + } else if (0 == l) { + for (h = 0; h < f; h++) r[c + h] = r[u + h]; + if (2 == d) for (h = f; h < o; h++) r[c + h] = 255 & r[u + h]; + if (3 == d) for (h = f; h < o; h++) r[c + h] = (r[u + h] + (r[c + h - f] >> 1)) & 255; + if (4 == d) for (h = f; h < o; h++) r[c + h] = (r[u + h] + s(r[c + h - f], 0, 0)) & 255; + } else { + if (2 == d) for (h = 0; h < o; h++) r[c + h] = (r[u + h] + r[c + h - o]) & 255; + if (3 == d) { + for (h = 0; h < f; h++) r[c + h] = (r[u + h] + (r[c + h - o] >> 1)) & 255; + for (h = f; h < o; h++) r[c + h] = (r[u + h] + ((r[c + h - o] + r[c + h - f]) >> 1)) & 255; + } + if (4 == d) { + for (h = 0; h < f; h++) r[c + h] = (r[u + h] + s(0, r[c + h - o], 0)) & 255; + for (h = f; h < o; h++) r[c + h] = (r[u + h] + s(r[c + h - f], r[c + h - o], r[c + h - f - o])) & 255; + } + } + } + return r; + }), + (e.decode._paeth = function (e, r, t) { + var n = e + r - t, + a = Math.abs(n - e), + i = Math.abs(n - r), + f = Math.abs(n - t); + return a <= i && a <= f ? e : i <= f ? r : t; + }), + (e.decode._IHDR = function (r, t, n) { + var a = e._bin; + (n.width = a.readUint(r, t)), + (t += 4), + (n.height = a.readUint(r, t)), + (t += 4), + (n.depth = r[t]), + t++, + (n.ctype = r[t]), + t++, + (n.compress = r[t]), + t++, + (n.filter = r[t]), + t++, + (n.interlace = r[t]), + t++; + }), + (e._bin = { + nextZero: function (e, r) { + for (; 0 != e[r]; ) r++; + return r; + }, + readUshort: function (e, r) { + return (e[r] << 8) | e[r + 1]; + }, + writeUshort: function (e, r, t) { + (e[r] = (t >> 8) & 255), (e[r + 1] = 255 & t); + }, + readUint: function (e, r) { + return 16777216 * e[r] + ((e[r + 1] << 16) | (e[r + 2] << 8) | e[r + 3]); + }, + writeUint: function (e, r, t) { + (e[r] = (t >> 24) & 255), (e[r + 1] = (t >> 16) & 255), (e[r + 2] = (t >> 8) & 255), (e[r + 3] = 255 & t); + }, + readASCII: function (e, r, t) { + for (var n = "", a = 0; a < t; a++) n += String.fromCharCode(e[r + a]); + return n; + }, + writeASCII: function (e, r, t) { + for (var n = 0; n < t.length; n++) e[r + n] = t.charCodeAt(n); + }, + readBytes: function (e, r, t) { + for (var n = [], a = 0; a < t; a++) n.push(e[r + a]); + return n; + }, + pad: function (e) { + return e.length < 2 ? "0" + e : e; + }, + readUTF8: function (r, t, n) { + for (var a, i = "", f = 0; f < n; f++) i += "%" + e._bin.pad(r[t + f].toString(16)); + try { + a = decodeURIComponent(i); + } catch (a) { + return e._bin.readASCII(r, t, n); + } + return a; + }, + }), + (e._copyTile = function (e, r, t, n, a, i, f, o, s) { + for (var l = Math.min(r, a), c = Math.min(t, i), u = 0, d = 0, h = 0; h < c; h++) + for (var v = 0; v < l; v++) + if ( + (f >= 0 && o >= 0 + ? ((u = (h * r + v) << 2), (d = ((o + h) * a + f + v) << 2)) + : ((u = ((-o + h) * r - f + v) << 2), (d = (h * a + v) << 2)), + 0 == s) + ) + (n[d] = e[u]), (n[d + 1] = e[u + 1]), (n[d + 2] = e[u + 2]), (n[d + 3] = e[u + 3]); + else if (1 == s) { + var p = e[u + 3] * (1 / 255), + b = e[u] * p, + g = e[u + 1] * p, + y = e[u + 2] * p, + m = n[d + 3] * (1 / 255), + w = n[d] * m, + A = n[d + 1] * m, + U = n[d + 2] * m, + _ = 1 - p, + I = p + m * _, + q = 0 == I ? 0 : 1 / I; + (n[d + 3] = 255 * I), + (n[d + 0] = (b + w * _) * q), + (n[d + 1] = (g + A * _) * q), + (n[d + 2] = (y + U * _) * q); + } else if (2 == s) { + (p = e[u + 3]), + (b = e[u]), + (g = e[u + 1]), + (y = e[u + 2]), + (m = n[d + 3]), + (w = n[d]), + (A = n[d + 1]), + (U = n[d + 2]); + p == m && b == w && g == A && y == U + ? ((n[d] = 0), (n[d + 1] = 0), (n[d + 2] = 0), (n[d + 3] = 0)) + : ((n[d] = b), (n[d + 1] = g), (n[d + 2] = y), (n[d + 3] = p)); + } else if (3 == s) { + (p = e[u + 3]), + (b = e[u]), + (g = e[u + 1]), + (y = e[u + 2]), + (m = n[d + 3]), + (w = n[d]), + (A = n[d + 1]), + (U = n[d + 2]); + if (p == m && b == w && g == A && y == U) continue; + if (p < 220 && m > 20) return !1; + } + return !0; + }), + (e.encode = function (r, t, n, a, i, f) { + null == a && (a = 0), null == f && (f = !1); + for ( + var o = new Uint8Array(r[0].byteLength * r.length + 100), s = [137, 80, 78, 71, 13, 10, 26, 10], l = 0; + l < 8; + l++ + ) + o[l] = s[l]; + var c = 8, + u = e._bin, + d = e.crc.crc, + h = u.writeUint, + v = u.writeUshort, + p = u.writeASCII, + b = e.encode.compressPNG(r, t, n, a, f); + h(o, c, 13), + p(o, (c += 4), "IHDR"), + h(o, (c += 4), t), + h(o, (c += 4), n), + (o[(c += 4)] = b.depth), + (o[++c] = b.ctype), + (o[++c] = 0), + (o[++c] = 0), + (o[++c] = 0), + h(o, ++c, d(o, c - 17, 17)), + h(o, (c += 4), 1), + p(o, (c += 4), "sRGB"), + (o[(c += 4)] = 1), + h(o, ++c, d(o, c - 5, 5)), + (c += 4); + var g = r.length > 1; + if ( + (g && + (h(o, c, 8), + p(o, (c += 4), "acTL"), + h(o, (c += 4), r.length), + h(o, (c += 4), 0), + h(o, (c += 4), d(o, c - 12, 12)), + (c += 4)), + 3 == b.ctype) + ) { + h(o, c, 3 * (M = b.plte.length)), p(o, (c += 4), "PLTE"), (c += 4); + for (l = 0; l < M; l++) { + var y = 3 * l, + m = b.plte[l], + w = 255 & m, + A = (m >> 8) & 255, + U = (m >> 16) & 255; + (o[c + y + 0] = w), (o[c + y + 1] = A), (o[c + y + 2] = U); + } + if ((h(o, (c += 3 * M), d(o, c - 3 * M - 4, 3 * M + 4)), (c += 4), b.gotAlpha)) { + h(o, c, M), p(o, (c += 4), "tRNS"), (c += 4); + for (l = 0; l < M; l++) o[c + l] = (b.plte[l] >> 24) & 255; + h(o, (c += M), d(o, c - M - 4, M + 4)), (c += 4); + } + } + for (var _ = 0, I = 0; I < b.frames.length; I++) { + var q = b.frames[I]; + g && + (h(o, c, 26), + p(o, (c += 4), "fcTL"), + h(o, (c += 4), _++), + h(o, (c += 4), q.rect.width), + h(o, (c += 4), q.rect.height), + h(o, (c += 4), q.rect.x), + h(o, (c += 4), q.rect.y), + v(o, (c += 4), i[I]), + v(o, (c += 2), 1e3), + (o[(c += 2)] = q.dispose), + (o[++c] = q.blend), + h(o, ++c, d(o, c - 30, 30)), + (c += 4)); + var M, + T = q.cimg; + h(o, c, (M = T.length) + (0 == I ? 0 : 4)); + var R = (c += 4); + p(o, c, 0 == I ? "IDAT" : "fdAT"), (c += 4), 0 != I && (h(o, c, _++), (c += 4)); + for (l = 0; l < M; l++) o[c + l] = T[l]; + h(o, (c += M), d(o, R, c - R)), (c += 4); + } + return h(o, c, 0), p(o, (c += 4), "IEND"), h(o, (c += 4), d(o, c - 4, 4)), (c += 4), o.buffer.slice(0, c); + }), + (e.encode.compressPNG = function (r, t, n, a, i) { + for (var f = e.encode.compress(r, t, n, a, !1, i), o = 0; o < r.length; o++) { + var s = f.frames[o], + l = (s.rect.width, s.rect.height), + c = s.bpl, + u = s.bpp, + d = new Uint8Array(l * c + l); + s.cimg = e.encode._filterZero(s.img, l, u, c, d); + } + return f; + }), + (e.encode.compress = function (r, t, n, a, i, f) { + null == f && (f = !1); + for (var o = 6, s = 8, l = 4, c = 255, u = 0; u < r.length; u++) + for (var d = new Uint8Array(r[u]), h = d.length, v = 0; v < h; v += 4) c &= d[v + 3]; + var p = 255 != c, + b = {}, + g = []; + if ((0 != r.length && ((b[0] = 0), g.push(0), 0 != a && a--), 0 != a)) { + var y = e.quantize(r, a, i); + r = y.bufs; + for (v = 0; v < y.plte.length; v++) { + null == b[(w = y.plte[v].est.rgba)] && ((b[w] = g.length), g.push(w)); + } + } else + for (u = 0; u < r.length; u++) { + var m = new Uint32Array(r[u]); + for (h = m.length, v = 0; v < h; v++) { + var w = m[v]; + if ( + (v < t || (w != m[v - 1] && w != m[v - t])) && + null == b[w] && + ((b[w] = g.length), g.push(w), g.length >= 300) + ) + break; + } + } + var A = !!p && i, + U = g.length; + U <= 256 && 0 == f && ((s = U <= 2 ? 1 : U <= 4 ? 2 : U <= 16 ? 4 : 8), i && (s = 8), (p = !0)); + var _ = []; + for (u = 0; u < r.length; u++) { + var I = new Uint8Array(r[u]), + q = new Uint32Array(I.buffer), + M = 0, + T = 0, + R = t, + N = n, + z = 0; + if (0 != u && !A) { + for (var P = i || 1 == u || 2 == _[_.length - 2].dispose ? 1 : 2, S = 0, L = 1e9, C = 0; C < P; C++) { + for ( + var D = new Uint8Array(r[u - 1 - C]), + B = new Uint32Array(r[u - 1 - C]), + x = t, + G = n, + Z = -1, + k = -1, + E = 0; + E < n; + E++ + ) + for (var H = 0; H < t; H++) { + q[(v = E * t + H)] != B[v] && + (H < x && (x = H), H > Z && (Z = H), E < G && (G = E), E > k && (k = E)); + } + var F = -1 == Z ? 1 : (Z - x + 1) * (k - G + 1); + F < L && + ((L = F), + (S = C), + -1 == Z ? ((M = T = 0), (R = N = 1)) : ((M = x), (T = G), (R = Z - x + 1), (N = k - G + 1))); + } + D = new Uint8Array(r[u - 1 - S]); + 1 == S && (_[_.length - 1].dispose = 2); + var V = new Uint8Array(R * N * 4); + new Uint32Array(V.buffer); + e._copyTile(D, t, n, V, R, N, -M, -T, 0), + e._copyTile(I, t, n, V, R, N, -M, -T, 3) + ? (e._copyTile(I, t, n, V, R, N, -M, -T, 2), (z = 1)) + : (e._copyTile(I, t, n, V, R, N, -M, -T, 0), (z = 0)), + (I = V), + (q = new Uint32Array(I.buffer)); + } + var X = 4 * R; + if (U <= 256 && 0 == f) { + X = Math.ceil((s * R) / 8); + for (V = new Uint8Array(X * N), E = 0; E < N; E++) { + v = E * X; + var j = E * R; + if (8 == s) for (H = 0; H < R; H++) V[v + H] = b[q[j + H]]; + else if (4 == s) for (H = 0; H < R; H++) V[v + (H >> 1)] |= b[q[j + H]] << (4 - 4 * (1 & H)); + else if (2 == s) for (H = 0; H < R; H++) V[v + (H >> 2)] |= b[q[j + H]] << (6 - 2 * (3 & H)); + else if (1 == s) for (H = 0; H < R; H++) V[v + (H >> 3)] |= b[q[j + H]] << (7 - 1 * (7 & H)); + } + (I = V), (o = 3), (l = 1); + } else if (0 == p && 1 == r.length) { + V = new Uint8Array(R * N * 3); + var K = R * N; + for (v = 0; v < K; v++) { + var Y = 3 * v, + J = 4 * v; + (V[Y] = I[J]), (V[Y + 1] = I[J + 1]), (V[Y + 2] = I[J + 2]); + } + (I = V), (o = 2), (l = 3), (X = 3 * R); + } + _.push({ rect: { x: M, y: T, width: R, height: N }, img: I, bpl: X, bpp: l, blend: z, dispose: A ? 1 : 0 }); + } + return { ctype: o, depth: s, plte: g, gotAlpha: p, frames: _ }; + }), + (e.encode._filterZero = function (t, n, a, i, f) { + for (var o = [], s = 0; s < 5; s++) + if (!(n * i > 5e5) || (2 != s && 3 != s && 4 != s)) { + for (var l = 0; l < n; l++) e.encode._filterLine(f, t, l, i, a, s); + if ((o.push(r.deflate(f)), 1 == a)) break; + } + for (var c, u = 1e9, d = 0; d < o.length; d++) o[d].length < u && ((c = d), (u = o[d].length)); + return o[c]; + }), + (e.encode._filterLine = function (r, t, n, a, i, f) { + var o = n * a, + s = o + n, + l = e.decode._paeth; + if (((r[s] = f), s++, 0 == f)) for (var c = 0; c < a; c++) r[s + c] = t[o + c]; + else if (1 == f) { + for (c = 0; c < i; c++) r[s + c] = t[o + c]; + for (c = i; c < a; c++) r[s + c] = (t[o + c] - t[o + c - i] + 256) & 255; + } else if (0 == n) { + for (c = 0; c < i; c++) r[s + c] = t[o + c]; + if (2 == f) for (c = i; c < a; c++) r[s + c] = t[o + c]; + if (3 == f) for (c = i; c < a; c++) r[s + c] = (t[o + c] - (t[o + c - i] >> 1) + 256) & 255; + if (4 == f) for (c = i; c < a; c++) r[s + c] = (t[o + c] - l(t[o + c - i], 0, 0) + 256) & 255; + } else { + if (2 == f) for (c = 0; c < a; c++) r[s + c] = (t[o + c] + 256 - t[o + c - a]) & 255; + if (3 == f) { + for (c = 0; c < i; c++) r[s + c] = (t[o + c] + 256 - (t[o + c - a] >> 1)) & 255; + for (c = i; c < a; c++) r[s + c] = (t[o + c] + 256 - ((t[o + c - a] + t[o + c - i]) >> 1)) & 255; + } + if (4 == f) { + for (c = 0; c < i; c++) r[s + c] = (t[o + c] + 256 - l(0, t[o + c - a], 0)) & 255; + for (c = i; c < a; c++) + r[s + c] = (t[o + c] + 256 - l(t[o + c - i], t[o + c - a], t[o + c - i - a])) & 255; + } + } + }), + (e.crc = { + table: (function () { + for (var e = new Uint32Array(256), r = 0; r < 256; r++) { + for (var t = r, n = 0; n < 8; n++) 1 & t ? (t = 3988292384 ^ (t >>> 1)) : (t >>>= 1); + e[r] = t; + } + return e; + })(), + update: function (r, t, n, a) { + for (var i = 0; i < a; i++) r = e.crc.table[255 & (r ^ t[n + i])] ^ (r >>> 8); + return r; + }, + crc: function (r, t, n) { + return 4294967295 ^ e.crc.update(4294967295, r, t, n); + }, + }), + (e.quantize = function (r, t, n) { + for (var a = [], i = 0, f = 0; f < r.length; f++) + a.push(e.encode.alphaMul(new Uint8Array(r[f]), n)), (i += r[f].byteLength); + var o = new Uint8Array(i), + s = new Uint32Array(o.buffer), + l = 0; + for (f = 0; f < a.length; f++) { + for (var c = a[f], u = c.length, d = 0; d < u; d++) o[l + d] = c[d]; + l += u; + } + var h = { i0: 0, i1: o.length, bst: null, est: null, tdst: 0, left: null, right: null }; + (h.bst = e.quantize.stats(o, h.i0, h.i1)), (h.est = e.quantize.estats(h.bst)); + for (var v = [h]; v.length < t; ) { + var p = 0, + b = 0; + for (f = 0; f < v.length; f++) v[f].est.L > p && ((p = v[f].est.L), (b = f)); + if (p < 0.001) break; + var g = v[b], + y = e.quantize.splitPixels(o, s, g.i0, g.i1, g.est.e, g.est.eMq255), + m = { i0: g.i0, i1: y, bst: null, est: null, tdst: 0, left: null, right: null }; + (m.bst = e.quantize.stats(o, m.i0, m.i1)), (m.est = e.quantize.estats(m.bst)); + var w = { i0: y, i1: g.i1, bst: null, est: null, tdst: 0, left: null, right: null }; + w.bst = { R: [], m: [], N: g.bst.N - m.bst.N }; + for (f = 0; f < 16; f++) w.bst.R[f] = g.bst.R[f] - m.bst.R[f]; + for (f = 0; f < 4; f++) w.bst.m[f] = g.bst.m[f] - m.bst.m[f]; + (w.est = e.quantize.estats(w.bst)), (g.left = m), (g.right = w), (v[b] = m), v.push(w); + } + v.sort(function (e, r) { + return r.bst.N - e.bst.N; + }); + for (var A = 0; A < a.length; A++) { + var U = e.quantize.planeDst, + _ = new Uint8Array(a[A].buffer), + I = new Uint32Array(a[A].buffer), + q = _.length; + for (f = 0; f < q; f += 4) { + for ( + var M = _[f] * (1 / 255), + T = _[f + 1] * (1 / 255), + R = _[f + 2] * (1 / 255), + N = _[f + 3] * (1 / 255), + z = h; + z.left; + + ) + z = U(z.est, M, T, R, N) <= 0 ? z.left : z.right; + I[f >> 2] = z.est.rgba; + } + a[A] = I.buffer; + } + return { bufs: a, plte: v }; + }), + (e.quantize.getNearest = function (r, t, n, a, i) { + if (null == r.left) return (r.tdst = e.quantize.dist(r.est.q, t, n, a, i)), r; + var f = e.quantize.planeDst(r.est, t, n, a, i), + o = r.left, + s = r.right; + f > 0 && ((o = r.right), (s = r.left)); + var l = e.quantize.getNearest(o, t, n, a, i); + if (l.tdst <= f * f) return l; + var c = e.quantize.getNearest(s, t, n, a, i); + return c.tdst < l.tdst ? c : l; + }), + (e.quantize.planeDst = function (e, r, t, n, a) { + var i = e.e; + return i[0] * r + i[1] * t + i[2] * n + i[3] * a - e.eMq; + }), + (e.quantize.dist = function (e, r, t, n, a) { + var i = r - e[0], + f = t - e[1], + o = n - e[2], + s = a - e[3]; + return i * i + f * f + o * o + s * s; + }), + (e.quantize.splitPixels = function (r, t, n, a, i, f) { + var o = e.quantize.vecDot; + a -= 4; + for (; n < a; ) { + for (; o(r, n, i) <= f; ) n += 4; + for (; o(r, a, i) > f; ) a -= 4; + if (n >= a) break; + var s = t[n >> 2]; + (t[n >> 2] = t[a >> 2]), (t[a >> 2] = s), (n += 4), (a -= 4); + } + for (; o(r, n, i) > f; ) n -= 4; + return n + 4; + }), + (e.quantize.vecDot = function (e, r, t) { + return e[r] * t[0] + e[r + 1] * t[1] + e[r + 2] * t[2] + e[r + 3] * t[3]; + }), + (e.quantize.stats = function (e, r, t) { + for ( + var n = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], a = [0, 0, 0, 0], i = (t - r) >> 2, f = r; + f < t; + f += 4 + ) { + var o = e[f] * (1 / 255), + s = e[f + 1] * (1 / 255), + l = e[f + 2] * (1 / 255), + c = e[f + 3] * (1 / 255); + (a[0] += o), + (a[1] += s), + (a[2] += l), + (a[3] += c), + (n[0] += o * o), + (n[1] += o * s), + (n[2] += o * l), + (n[3] += o * c), + (n[5] += s * s), + (n[6] += s * l), + (n[7] += s * c), + (n[10] += l * l), + (n[11] += l * c), + (n[15] += c * c); + } + return ( + (n[4] = n[1]), + (n[8] = n[2]), + (n[12] = n[3]), + (n[9] = n[6]), + (n[13] = n[7]), + (n[14] = n[11]), + { R: n, m: a, N: i } + ); + }), + (e.quantize.estats = function (r) { + var t = r.R, + n = r.m, + a = r.N, + i = n[0], + f = n[1], + o = n[2], + s = n[3], + l = 0 == a ? 0 : 1 / a, + c = [ + t[0] - i * i * l, + t[1] - i * f * l, + t[2] - i * o * l, + t[3] - i * s * l, + t[4] - f * i * l, + t[5] - f * f * l, + t[6] - f * o * l, + t[7] - f * s * l, + t[8] - o * i * l, + t[9] - o * f * l, + t[10] - o * o * l, + t[11] - o * s * l, + t[12] - s * i * l, + t[13] - s * f * l, + t[14] - s * o * l, + t[15] - s * s * l, + ], + u = c, + d = e.M4, + h = [0.5, 0.5, 0.5, 0.5], + v = 0, + p = 0; + if (0 != a) + for ( + var b = 0; + b < 10 && + ((h = d.multVec(u, h)), (p = Math.sqrt(d.dot(h, h))), (h = d.sml(1 / p, h)), !(Math.abs(p - v) < 1e-9)); + b++ + ) + v = p; + var g = [i * l, f * l, o * l, s * l], + y = d.dot(d.sml(255, g), h), + m = g[3] < 0.001 ? 0 : 1 / g[3]; + return { + Cov: c, + q: g, + e: h, + L: v, + eMq255: y, + eMq: d.dot(h, g), + rgba: + ((Math.round(255 * g[3]) << 24) | + (Math.round(255 * g[2] * m) << 16) | + (Math.round(255 * g[1] * m) << 8) | + (Math.round(255 * g[0] * m) << 0)) >>> + 0, + }; + }), + (e.M4 = { + multVec: function (e, r) { + return [ + e[0] * r[0] + e[1] * r[1] + e[2] * r[2] + e[3] * r[3], + e[4] * r[0] + e[5] * r[1] + e[6] * r[2] + e[7] * r[3], + e[8] * r[0] + e[9] * r[1] + e[10] * r[2] + e[11] * r[3], + e[12] * r[0] + e[13] * r[1] + e[14] * r[2] + e[15] * r[3], + ]; + }, + dot: function (e, r) { + return e[0] * r[0] + e[1] * r[1] + e[2] * r[2] + e[3] * r[3]; + }, + sml: function (e, r) { + return [e * r[0], e * r[1], e * r[2], e * r[3]]; + }, + }), + (e.encode.alphaMul = function (e, r) { + for (var t = new Uint8Array(e.length), n = e.length >> 2, a = 0; a < n; a++) { + var i = a << 2, + f = e[i + 3]; + r && (f = f < 128 ? 0 : 255); + var o = f * (1 / 255); + (t[i + 0] = e[i + 0] * o), (t[i + 1] = e[i + 1] * o), (t[i + 2] = e[i + 2] * o), (t[i + 3] = f); + } + return t; + }); + })(e, "function" == typeof require ? require("pako") : self.pako); +})(); +//# sourceMappingURL=/sm/0fd601ffe64756cda7aea202ef9150d5d05576b5d48a0c7799b9ccbfedbaf055.map diff --git a/packages/baize-compress-image/lib/main.ts b/packages/baize-compress-image/lib/main.ts index 197e23f..fef52b4 100644 --- a/packages/baize-compress-image/lib/main.ts +++ b/packages/baize-compress-image/lib/main.ts @@ -8,7 +8,7 @@ const store = localforage.createInstance(DEFAUTL_FORAGE_CONFIG); const worker = new Worker(); -export const compressImageWorker = async (file: File, quality = DEFAULT_QUALITY) => { +export const compressImageWorker = async (file: File, quality = DEFAULT_QUALITY): Promise => { return new Promise(async (resolve, reject) => { const id = nanoid(8); const taskData = { @@ -21,7 +21,7 @@ export const compressImageWorker = async (file: File, quality = DEFAULT_QUALITY) worker.onmessage = async (event) => { const message = JSON.parse(event.data); if (message.type === "compressImageSuccess") { - const result = await store.getItem(taskId); + const result = (await store.getItem(taskId)) as File; await store.removeItem(taskId); resolve(result); } else { diff --git a/packages/baize-compress-image/lib/pako.min.js b/packages/baize-compress-image/lib/pako.min.js new file mode 100644 index 0000000..2535eaf --- /dev/null +++ b/packages/baize-compress-image/lib/pako.min.js @@ -0,0 +1,2 @@ +/*! pako 2.1.0 https://github.com/nodeca/pako @license (MIT AND Zlib) */ +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).pako={})}(this,(function(t){"use strict";function e(t){let e=t.length;for(;--e>=0;)t[e]=0}const a=256,i=286,n=30,s=15,r=new Uint8Array([0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0]),o=new Uint8Array([0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13]),l=new Uint8Array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7]),h=new Uint8Array([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]),d=new Array(576);e(d);const _=new Array(60);e(_);const f=new Array(512);e(f);const c=new Array(256);e(c);const u=new Array(29);e(u);const w=new Array(n);function m(t,e,a,i,n){this.static_tree=t,this.extra_bits=e,this.extra_base=a,this.elems=i,this.max_length=n,this.has_stree=t&&t.length}let b,g,p;function k(t,e){this.dyn_tree=t,this.max_code=0,this.stat_desc=e}e(w);const v=t=>t<256?f[t]:f[256+(t>>>7)],y=(t,e)=>{t.pending_buf[t.pending++]=255&e,t.pending_buf[t.pending++]=e>>>8&255},x=(t,e,a)=>{t.bi_valid>16-a?(t.bi_buf|=e<>16-t.bi_valid,t.bi_valid+=a-16):(t.bi_buf|=e<{x(t,a[2*e],a[2*e+1])},A=(t,e)=>{let a=0;do{a|=1&t,t>>>=1,a<<=1}while(--e>0);return a>>>1},E=(t,e,a)=>{const i=new Array(16);let n,r,o=0;for(n=1;n<=s;n++)o=o+a[n-1]<<1,i[n]=o;for(r=0;r<=e;r++){let e=t[2*r+1];0!==e&&(t[2*r]=A(i[e]++,e))}},R=t=>{let e;for(e=0;e{t.bi_valid>8?y(t,t.bi_buf):t.bi_valid>0&&(t.pending_buf[t.pending++]=t.bi_buf),t.bi_buf=0,t.bi_valid=0},U=(t,e,a,i)=>{const n=2*e,s=2*a;return t[n]{const i=t.heap[a];let n=a<<1;for(;n<=t.heap_len&&(n{let n,s,l,h,d=0;if(0!==t.sym_next)do{n=255&t.pending_buf[t.sym_buf+d++],n+=(255&t.pending_buf[t.sym_buf+d++])<<8,s=t.pending_buf[t.sym_buf+d++],0===n?z(t,s,e):(l=c[s],z(t,l+a+1,e),h=r[l],0!==h&&(s-=u[l],x(t,s,h)),n--,l=v(n),z(t,l,i),h=o[l],0!==h&&(n-=w[l],x(t,n,h)))}while(d{const a=e.dyn_tree,i=e.stat_desc.static_tree,n=e.stat_desc.has_stree,r=e.stat_desc.elems;let o,l,h,d=-1;for(t.heap_len=0,t.heap_max=573,o=0;o>1;o>=1;o--)S(t,a,o);h=r;do{o=t.heap[1],t.heap[1]=t.heap[t.heap_len--],S(t,a,1),l=t.heap[1],t.heap[--t.heap_max]=o,t.heap[--t.heap_max]=l,a[2*h]=a[2*o]+a[2*l],t.depth[h]=(t.depth[o]>=t.depth[l]?t.depth[o]:t.depth[l])+1,a[2*o+1]=a[2*l+1]=h,t.heap[1]=h++,S(t,a,1)}while(t.heap_len>=2);t.heap[--t.heap_max]=t.heap[1],((t,e)=>{const a=e.dyn_tree,i=e.max_code,n=e.stat_desc.static_tree,r=e.stat_desc.has_stree,o=e.stat_desc.extra_bits,l=e.stat_desc.extra_base,h=e.stat_desc.max_length;let d,_,f,c,u,w,m=0;for(c=0;c<=s;c++)t.bl_count[c]=0;for(a[2*t.heap[t.heap_max]+1]=0,d=t.heap_max+1;d<573;d++)_=t.heap[d],c=a[2*a[2*_+1]+1]+1,c>h&&(c=h,m++),a[2*_+1]=c,_>i||(t.bl_count[c]++,u=0,_>=l&&(u=o[_-l]),w=a[2*_],t.opt_len+=w*(c+u),r&&(t.static_len+=w*(n[2*_+1]+u)));if(0!==m){do{for(c=h-1;0===t.bl_count[c];)c--;t.bl_count[c]--,t.bl_count[c+1]+=2,t.bl_count[h]--,m-=2}while(m>0);for(c=h;0!==c;c--)for(_=t.bl_count[c];0!==_;)f=t.heap[--d],f>i||(a[2*f+1]!==c&&(t.opt_len+=(c-a[2*f+1])*a[2*f],a[2*f+1]=c),_--)}})(t,e),E(a,d,t.bl_count)},O=(t,e,a)=>{let i,n,s=-1,r=e[1],o=0,l=7,h=4;for(0===r&&(l=138,h=3),e[2*(a+1)+1]=65535,i=0;i<=a;i++)n=r,r=e[2*(i+1)+1],++o{let i,n,s=-1,r=e[1],o=0,l=7,h=4;for(0===r&&(l=138,h=3),i=0;i<=a;i++)if(n=r,r=e[2*(i+1)+1],!(++o{x(t,0+(i?1:0),3),Z(t),y(t,a),y(t,~a),a&&t.pending_buf.set(t.window.subarray(e,e+a),t.pending),t.pending+=a};var N=(t,e,i,n)=>{let s,r,o=0;t.level>0?(2===t.strm.data_type&&(t.strm.data_type=(t=>{let e,i=4093624447;for(e=0;e<=31;e++,i>>>=1)if(1&i&&0!==t.dyn_ltree[2*e])return 0;if(0!==t.dyn_ltree[18]||0!==t.dyn_ltree[20]||0!==t.dyn_ltree[26])return 1;for(e=32;e{let e;for(O(t,t.dyn_ltree,t.l_desc.max_code),O(t,t.dyn_dtree,t.d_desc.max_code),T(t,t.bl_desc),e=18;e>=3&&0===t.bl_tree[2*h[e]+1];e--);return t.opt_len+=3*(e+1)+5+5+4,e})(t),s=t.opt_len+3+7>>>3,r=t.static_len+3+7>>>3,r<=s&&(s=r)):s=r=i+5,i+4<=s&&-1!==e?L(t,e,i,n):4===t.strategy||r===s?(x(t,2+(n?1:0),3),D(t,d,_)):(x(t,4+(n?1:0),3),((t,e,a,i)=>{let n;for(x(t,e-257,5),x(t,a-1,5),x(t,i-4,4),n=0;n{F||((()=>{let t,e,a,h,k;const v=new Array(16);for(a=0,h=0;h<28;h++)for(u[h]=a,t=0;t<1<>=7;h(t.pending_buf[t.sym_buf+t.sym_next++]=e,t.pending_buf[t.sym_buf+t.sym_next++]=e>>8,t.pending_buf[t.sym_buf+t.sym_next++]=i,0===e?t.dyn_ltree[2*i]++:(t.matches++,e--,t.dyn_ltree[2*(c[i]+a+1)]++,t.dyn_dtree[2*v(e)]++),t.sym_next===t.sym_end),_tr_align:t=>{x(t,2,3),z(t,256,d),(t=>{16===t.bi_valid?(y(t,t.bi_buf),t.bi_buf=0,t.bi_valid=0):t.bi_valid>=8&&(t.pending_buf[t.pending++]=255&t.bi_buf,t.bi_buf>>=8,t.bi_valid-=8)})(t)}};var C=(t,e,a,i)=>{let n=65535&t|0,s=t>>>16&65535|0,r=0;for(;0!==a;){r=a>2e3?2e3:a,a-=r;do{n=n+e[i++]|0,s=s+n|0}while(--r);n%=65521,s%=65521}return n|s<<16|0};const M=new Uint32Array((()=>{let t,e=[];for(var a=0;a<256;a++){t=a;for(var i=0;i<8;i++)t=1&t?3988292384^t>>>1:t>>>1;e[a]=t}return e})());var H=(t,e,a,i)=>{const n=M,s=i+a;t^=-1;for(let a=i;a>>8^n[255&(t^e[a])];return-1^t},j={2:"need dictionary",1:"stream end",0:"","-1":"file error","-2":"stream error","-3":"data error","-4":"insufficient memory","-5":"buffer error","-6":"incompatible version"},K={Z_NO_FLUSH:0,Z_PARTIAL_FLUSH:1,Z_SYNC_FLUSH:2,Z_FULL_FLUSH:3,Z_FINISH:4,Z_BLOCK:5,Z_TREES:6,Z_OK:0,Z_STREAM_END:1,Z_NEED_DICT:2,Z_ERRNO:-1,Z_STREAM_ERROR:-2,Z_DATA_ERROR:-3,Z_MEM_ERROR:-4,Z_BUF_ERROR:-5,Z_NO_COMPRESSION:0,Z_BEST_SPEED:1,Z_BEST_COMPRESSION:9,Z_DEFAULT_COMPRESSION:-1,Z_FILTERED:1,Z_HUFFMAN_ONLY:2,Z_RLE:3,Z_FIXED:4,Z_DEFAULT_STRATEGY:0,Z_BINARY:0,Z_TEXT:1,Z_UNKNOWN:2,Z_DEFLATED:8};const{_tr_init:P,_tr_stored_block:Y,_tr_flush_block:G,_tr_tally:X,_tr_align:W}=B,{Z_NO_FLUSH:q,Z_PARTIAL_FLUSH:J,Z_FULL_FLUSH:Q,Z_FINISH:V,Z_BLOCK:$,Z_OK:tt,Z_STREAM_END:et,Z_STREAM_ERROR:at,Z_DATA_ERROR:it,Z_BUF_ERROR:nt,Z_DEFAULT_COMPRESSION:st,Z_FILTERED:rt,Z_HUFFMAN_ONLY:ot,Z_RLE:lt,Z_FIXED:ht,Z_DEFAULT_STRATEGY:dt,Z_UNKNOWN:_t,Z_DEFLATED:ft}=K,ct=258,ut=262,wt=42,mt=113,bt=666,gt=(t,e)=>(t.msg=j[e],e),pt=t=>2*t-(t>4?9:0),kt=t=>{let e=t.length;for(;--e>=0;)t[e]=0},vt=t=>{let e,a,i,n=t.w_size;e=t.hash_size,i=e;do{a=t.head[--i],t.head[i]=a>=n?a-n:0}while(--e);e=n,i=e;do{a=t.prev[--i],t.prev[i]=a>=n?a-n:0}while(--e)};let yt=(t,e,a)=>(e<{const e=t.state;let a=e.pending;a>t.avail_out&&(a=t.avail_out),0!==a&&(t.output.set(e.pending_buf.subarray(e.pending_out,e.pending_out+a),t.next_out),t.next_out+=a,e.pending_out+=a,t.total_out+=a,t.avail_out-=a,e.pending-=a,0===e.pending&&(e.pending_out=0))},zt=(t,e)=>{G(t,t.block_start>=0?t.block_start:-1,t.strstart-t.block_start,e),t.block_start=t.strstart,xt(t.strm)},At=(t,e)=>{t.pending_buf[t.pending++]=e},Et=(t,e)=>{t.pending_buf[t.pending++]=e>>>8&255,t.pending_buf[t.pending++]=255&e},Rt=(t,e,a,i)=>{let n=t.avail_in;return n>i&&(n=i),0===n?0:(t.avail_in-=n,e.set(t.input.subarray(t.next_in,t.next_in+n),a),1===t.state.wrap?t.adler=C(t.adler,e,n,a):2===t.state.wrap&&(t.adler=H(t.adler,e,n,a)),t.next_in+=n,t.total_in+=n,n)},Zt=(t,e)=>{let a,i,n=t.max_chain_length,s=t.strstart,r=t.prev_length,o=t.nice_match;const l=t.strstart>t.w_size-ut?t.strstart-(t.w_size-ut):0,h=t.window,d=t.w_mask,_=t.prev,f=t.strstart+ct;let c=h[s+r-1],u=h[s+r];t.prev_length>=t.good_match&&(n>>=2),o>t.lookahead&&(o=t.lookahead);do{if(a=e,h[a+r]===u&&h[a+r-1]===c&&h[a]===h[s]&&h[++a]===h[s+1]){s+=2,a++;do{}while(h[++s]===h[++a]&&h[++s]===h[++a]&&h[++s]===h[++a]&&h[++s]===h[++a]&&h[++s]===h[++a]&&h[++s]===h[++a]&&h[++s]===h[++a]&&h[++s]===h[++a]&&sr){if(t.match_start=e,r=i,i>=o)break;c=h[s+r-1],u=h[s+r]}}}while((e=_[e&d])>l&&0!=--n);return r<=t.lookahead?r:t.lookahead},Ut=t=>{const e=t.w_size;let a,i,n;do{if(i=t.window_size-t.lookahead-t.strstart,t.strstart>=e+(e-ut)&&(t.window.set(t.window.subarray(e,e+e-i),0),t.match_start-=e,t.strstart-=e,t.block_start-=e,t.insert>t.strstart&&(t.insert=t.strstart),vt(t),i+=e),0===t.strm.avail_in)break;if(a=Rt(t.strm,t.window,t.strstart+t.lookahead,i),t.lookahead+=a,t.lookahead+t.insert>=3)for(n=t.strstart-t.insert,t.ins_h=t.window[n],t.ins_h=yt(t,t.ins_h,t.window[n+1]);t.insert&&(t.ins_h=yt(t,t.ins_h,t.window[n+3-1]),t.prev[n&t.w_mask]=t.head[t.ins_h],t.head[t.ins_h]=n,n++,t.insert--,!(t.lookahead+t.insert<3)););}while(t.lookahead{let a,i,n,s=t.pending_buf_size-5>t.w_size?t.w_size:t.pending_buf_size-5,r=0,o=t.strm.avail_in;do{if(a=65535,n=t.bi_valid+42>>3,t.strm.avail_outi+t.strm.avail_in&&(a=i+t.strm.avail_in),a>n&&(a=n),a>8,t.pending_buf[t.pending-2]=~a,t.pending_buf[t.pending-1]=~a>>8,xt(t.strm),i&&(i>a&&(i=a),t.strm.output.set(t.window.subarray(t.block_start,t.block_start+i),t.strm.next_out),t.strm.next_out+=i,t.strm.avail_out-=i,t.strm.total_out+=i,t.block_start+=i,a-=i),a&&(Rt(t.strm,t.strm.output,t.strm.next_out,a),t.strm.next_out+=a,t.strm.avail_out-=a,t.strm.total_out+=a)}while(0===r);return o-=t.strm.avail_in,o&&(o>=t.w_size?(t.matches=2,t.window.set(t.strm.input.subarray(t.strm.next_in-t.w_size,t.strm.next_in),0),t.strstart=t.w_size,t.insert=t.strstart):(t.window_size-t.strstart<=o&&(t.strstart-=t.w_size,t.window.set(t.window.subarray(t.w_size,t.w_size+t.strstart),0),t.matches<2&&t.matches++,t.insert>t.strstart&&(t.insert=t.strstart)),t.window.set(t.strm.input.subarray(t.strm.next_in-o,t.strm.next_in),t.strstart),t.strstart+=o,t.insert+=o>t.w_size-t.insert?t.w_size-t.insert:o),t.block_start=t.strstart),t.high_watern&&t.block_start>=t.w_size&&(t.block_start-=t.w_size,t.strstart-=t.w_size,t.window.set(t.window.subarray(t.w_size,t.w_size+t.strstart),0),t.matches<2&&t.matches++,n+=t.w_size,t.insert>t.strstart&&(t.insert=t.strstart)),n>t.strm.avail_in&&(n=t.strm.avail_in),n&&(Rt(t.strm,t.window,t.strstart,n),t.strstart+=n,t.insert+=n>t.w_size-t.insert?t.w_size-t.insert:n),t.high_water>3,n=t.pending_buf_size-n>65535?65535:t.pending_buf_size-n,s=n>t.w_size?t.w_size:n,i=t.strstart-t.block_start,(i>=s||(i||e===V)&&e!==q&&0===t.strm.avail_in&&i<=n)&&(a=i>n?n:i,r=e===V&&0===t.strm.avail_in&&a===i?1:0,Y(t,t.block_start,a,r),t.block_start+=a,xt(t.strm)),r?3:1)},Dt=(t,e)=>{let a,i;for(;;){if(t.lookahead=3&&(t.ins_h=yt(t,t.ins_h,t.window[t.strstart+3-1]),a=t.prev[t.strstart&t.w_mask]=t.head[t.ins_h],t.head[t.ins_h]=t.strstart),0!==a&&t.strstart-a<=t.w_size-ut&&(t.match_length=Zt(t,a)),t.match_length>=3)if(i=X(t,t.strstart-t.match_start,t.match_length-3),t.lookahead-=t.match_length,t.match_length<=t.max_lazy_match&&t.lookahead>=3){t.match_length--;do{t.strstart++,t.ins_h=yt(t,t.ins_h,t.window[t.strstart+3-1]),a=t.prev[t.strstart&t.w_mask]=t.head[t.ins_h],t.head[t.ins_h]=t.strstart}while(0!=--t.match_length);t.strstart++}else t.strstart+=t.match_length,t.match_length=0,t.ins_h=t.window[t.strstart],t.ins_h=yt(t,t.ins_h,t.window[t.strstart+1]);else i=X(t,0,t.window[t.strstart]),t.lookahead--,t.strstart++;if(i&&(zt(t,!1),0===t.strm.avail_out))return 1}return t.insert=t.strstart<2?t.strstart:2,e===V?(zt(t,!0),0===t.strm.avail_out?3:4):t.sym_next&&(zt(t,!1),0===t.strm.avail_out)?1:2},Tt=(t,e)=>{let a,i,n;for(;;){if(t.lookahead=3&&(t.ins_h=yt(t,t.ins_h,t.window[t.strstart+3-1]),a=t.prev[t.strstart&t.w_mask]=t.head[t.ins_h],t.head[t.ins_h]=t.strstart),t.prev_length=t.match_length,t.prev_match=t.match_start,t.match_length=2,0!==a&&t.prev_length4096)&&(t.match_length=2)),t.prev_length>=3&&t.match_length<=t.prev_length){n=t.strstart+t.lookahead-3,i=X(t,t.strstart-1-t.prev_match,t.prev_length-3),t.lookahead-=t.prev_length-1,t.prev_length-=2;do{++t.strstart<=n&&(t.ins_h=yt(t,t.ins_h,t.window[t.strstart+3-1]),a=t.prev[t.strstart&t.w_mask]=t.head[t.ins_h],t.head[t.ins_h]=t.strstart)}while(0!=--t.prev_length);if(t.match_available=0,t.match_length=2,t.strstart++,i&&(zt(t,!1),0===t.strm.avail_out))return 1}else if(t.match_available){if(i=X(t,0,t.window[t.strstart-1]),i&&zt(t,!1),t.strstart++,t.lookahead--,0===t.strm.avail_out)return 1}else t.match_available=1,t.strstart++,t.lookahead--}return t.match_available&&(i=X(t,0,t.window[t.strstart-1]),t.match_available=0),t.insert=t.strstart<2?t.strstart:2,e===V?(zt(t,!0),0===t.strm.avail_out?3:4):t.sym_next&&(zt(t,!1),0===t.strm.avail_out)?1:2};function Ot(t,e,a,i,n){this.good_length=t,this.max_lazy=e,this.nice_length=a,this.max_chain=i,this.func=n}const It=[new Ot(0,0,0,0,St),new Ot(4,4,8,4,Dt),new Ot(4,5,16,8,Dt),new Ot(4,6,32,32,Dt),new Ot(4,4,16,16,Tt),new Ot(8,16,32,32,Tt),new Ot(8,16,128,128,Tt),new Ot(8,32,128,256,Tt),new Ot(32,128,258,1024,Tt),new Ot(32,258,258,4096,Tt)];function Ft(){this.strm=null,this.status=0,this.pending_buf=null,this.pending_buf_size=0,this.pending_out=0,this.pending=0,this.wrap=0,this.gzhead=null,this.gzindex=0,this.method=ft,this.last_flush=-1,this.w_size=0,this.w_bits=0,this.w_mask=0,this.window=null,this.window_size=0,this.prev=null,this.head=null,this.ins_h=0,this.hash_size=0,this.hash_bits=0,this.hash_mask=0,this.hash_shift=0,this.block_start=0,this.match_length=0,this.prev_match=0,this.match_available=0,this.strstart=0,this.match_start=0,this.lookahead=0,this.prev_length=0,this.max_chain_length=0,this.max_lazy_match=0,this.level=0,this.strategy=0,this.good_match=0,this.nice_match=0,this.dyn_ltree=new Uint16Array(1146),this.dyn_dtree=new Uint16Array(122),this.bl_tree=new Uint16Array(78),kt(this.dyn_ltree),kt(this.dyn_dtree),kt(this.bl_tree),this.l_desc=null,this.d_desc=null,this.bl_desc=null,this.bl_count=new Uint16Array(16),this.heap=new Uint16Array(573),kt(this.heap),this.heap_len=0,this.heap_max=0,this.depth=new Uint16Array(573),kt(this.depth),this.sym_buf=0,this.lit_bufsize=0,this.sym_next=0,this.sym_end=0,this.opt_len=0,this.static_len=0,this.matches=0,this.insert=0,this.bi_buf=0,this.bi_valid=0}const Lt=t=>{if(!t)return 1;const e=t.state;return!e||e.strm!==t||e.status!==wt&&57!==e.status&&69!==e.status&&73!==e.status&&91!==e.status&&103!==e.status&&e.status!==mt&&e.status!==bt?1:0},Nt=t=>{if(Lt(t))return gt(t,at);t.total_in=t.total_out=0,t.data_type=_t;const e=t.state;return e.pending=0,e.pending_out=0,e.wrap<0&&(e.wrap=-e.wrap),e.status=2===e.wrap?57:e.wrap?wt:mt,t.adler=2===e.wrap?0:1,e.last_flush=-2,P(e),tt},Bt=t=>{const e=Nt(t);var a;return e===tt&&((a=t.state).window_size=2*a.w_size,kt(a.head),a.max_lazy_match=It[a.level].max_lazy,a.good_match=It[a.level].good_length,a.nice_match=It[a.level].nice_length,a.max_chain_length=It[a.level].max_chain,a.strstart=0,a.block_start=0,a.lookahead=0,a.insert=0,a.match_length=a.prev_length=2,a.match_available=0,a.ins_h=0),e},Ct=(t,e,a,i,n,s)=>{if(!t)return at;let r=1;if(e===st&&(e=6),i<0?(r=0,i=-i):i>15&&(r=2,i-=16),n<1||n>9||a!==ft||i<8||i>15||e<0||e>9||s<0||s>ht||8===i&&1!==r)return gt(t,at);8===i&&(i=9);const o=new Ft;return t.state=o,o.strm=t,o.status=wt,o.wrap=r,o.gzhead=null,o.w_bits=i,o.w_size=1<Ct(t,e,ft,15,8,dt),deflateInit2:Ct,deflateReset:Bt,deflateResetKeep:Nt,deflateSetHeader:(t,e)=>Lt(t)||2!==t.state.wrap?at:(t.state.gzhead=e,tt),deflate:(t,e)=>{if(Lt(t)||e>$||e<0)return t?gt(t,at):at;const a=t.state;if(!t.output||0!==t.avail_in&&!t.input||a.status===bt&&e!==V)return gt(t,0===t.avail_out?nt:at);const i=a.last_flush;if(a.last_flush=e,0!==a.pending){if(xt(t),0===t.avail_out)return a.last_flush=-1,tt}else if(0===t.avail_in&&pt(e)<=pt(i)&&e!==V)return gt(t,nt);if(a.status===bt&&0!==t.avail_in)return gt(t,nt);if(a.status===wt&&0===a.wrap&&(a.status=mt),a.status===wt){let e=ft+(a.w_bits-8<<4)<<8,i=-1;if(i=a.strategy>=ot||a.level<2?0:a.level<6?1:6===a.level?2:3,e|=i<<6,0!==a.strstart&&(e|=32),e+=31-e%31,Et(a,e),0!==a.strstart&&(Et(a,t.adler>>>16),Et(a,65535&t.adler)),t.adler=1,a.status=mt,xt(t),0!==a.pending)return a.last_flush=-1,tt}if(57===a.status)if(t.adler=0,At(a,31),At(a,139),At(a,8),a.gzhead)At(a,(a.gzhead.text?1:0)+(a.gzhead.hcrc?2:0)+(a.gzhead.extra?4:0)+(a.gzhead.name?8:0)+(a.gzhead.comment?16:0)),At(a,255&a.gzhead.time),At(a,a.gzhead.time>>8&255),At(a,a.gzhead.time>>16&255),At(a,a.gzhead.time>>24&255),At(a,9===a.level?2:a.strategy>=ot||a.level<2?4:0),At(a,255&a.gzhead.os),a.gzhead.extra&&a.gzhead.extra.length&&(At(a,255&a.gzhead.extra.length),At(a,a.gzhead.extra.length>>8&255)),a.gzhead.hcrc&&(t.adler=H(t.adler,a.pending_buf,a.pending,0)),a.gzindex=0,a.status=69;else if(At(a,0),At(a,0),At(a,0),At(a,0),At(a,0),At(a,9===a.level?2:a.strategy>=ot||a.level<2?4:0),At(a,3),a.status=mt,xt(t),0!==a.pending)return a.last_flush=-1,tt;if(69===a.status){if(a.gzhead.extra){let e=a.pending,i=(65535&a.gzhead.extra.length)-a.gzindex;for(;a.pending+i>a.pending_buf_size;){let n=a.pending_buf_size-a.pending;if(a.pending_buf.set(a.gzhead.extra.subarray(a.gzindex,a.gzindex+n),a.pending),a.pending=a.pending_buf_size,a.gzhead.hcrc&&a.pending>e&&(t.adler=H(t.adler,a.pending_buf,a.pending-e,e)),a.gzindex+=n,xt(t),0!==a.pending)return a.last_flush=-1,tt;e=0,i-=n}let n=new Uint8Array(a.gzhead.extra);a.pending_buf.set(n.subarray(a.gzindex,a.gzindex+i),a.pending),a.pending+=i,a.gzhead.hcrc&&a.pending>e&&(t.adler=H(t.adler,a.pending_buf,a.pending-e,e)),a.gzindex=0}a.status=73}if(73===a.status){if(a.gzhead.name){let e,i=a.pending;do{if(a.pending===a.pending_buf_size){if(a.gzhead.hcrc&&a.pending>i&&(t.adler=H(t.adler,a.pending_buf,a.pending-i,i)),xt(t),0!==a.pending)return a.last_flush=-1,tt;i=0}e=a.gzindexi&&(t.adler=H(t.adler,a.pending_buf,a.pending-i,i)),a.gzindex=0}a.status=91}if(91===a.status){if(a.gzhead.comment){let e,i=a.pending;do{if(a.pending===a.pending_buf_size){if(a.gzhead.hcrc&&a.pending>i&&(t.adler=H(t.adler,a.pending_buf,a.pending-i,i)),xt(t),0!==a.pending)return a.last_flush=-1,tt;i=0}e=a.gzindexi&&(t.adler=H(t.adler,a.pending_buf,a.pending-i,i))}a.status=103}if(103===a.status){if(a.gzhead.hcrc){if(a.pending+2>a.pending_buf_size&&(xt(t),0!==a.pending))return a.last_flush=-1,tt;At(a,255&t.adler),At(a,t.adler>>8&255),t.adler=0}if(a.status=mt,xt(t),0!==a.pending)return a.last_flush=-1,tt}if(0!==t.avail_in||0!==a.lookahead||e!==q&&a.status!==bt){let i=0===a.level?St(a,e):a.strategy===ot?((t,e)=>{let a;for(;;){if(0===t.lookahead&&(Ut(t),0===t.lookahead)){if(e===q)return 1;break}if(t.match_length=0,a=X(t,0,t.window[t.strstart]),t.lookahead--,t.strstart++,a&&(zt(t,!1),0===t.strm.avail_out))return 1}return t.insert=0,e===V?(zt(t,!0),0===t.strm.avail_out?3:4):t.sym_next&&(zt(t,!1),0===t.strm.avail_out)?1:2})(a,e):a.strategy===lt?((t,e)=>{let a,i,n,s;const r=t.window;for(;;){if(t.lookahead<=ct){if(Ut(t),t.lookahead<=ct&&e===q)return 1;if(0===t.lookahead)break}if(t.match_length=0,t.lookahead>=3&&t.strstart>0&&(n=t.strstart-1,i=r[n],i===r[++n]&&i===r[++n]&&i===r[++n])){s=t.strstart+ct;do{}while(i===r[++n]&&i===r[++n]&&i===r[++n]&&i===r[++n]&&i===r[++n]&&i===r[++n]&&i===r[++n]&&i===r[++n]&&nt.lookahead&&(t.match_length=t.lookahead)}if(t.match_length>=3?(a=X(t,1,t.match_length-3),t.lookahead-=t.match_length,t.strstart+=t.match_length,t.match_length=0):(a=X(t,0,t.window[t.strstart]),t.lookahead--,t.strstart++),a&&(zt(t,!1),0===t.strm.avail_out))return 1}return t.insert=0,e===V?(zt(t,!0),0===t.strm.avail_out?3:4):t.sym_next&&(zt(t,!1),0===t.strm.avail_out)?1:2})(a,e):It[a.level].func(a,e);if(3!==i&&4!==i||(a.status=bt),1===i||3===i)return 0===t.avail_out&&(a.last_flush=-1),tt;if(2===i&&(e===J?W(a):e!==$&&(Y(a,0,0,!1),e===Q&&(kt(a.head),0===a.lookahead&&(a.strstart=0,a.block_start=0,a.insert=0))),xt(t),0===t.avail_out))return a.last_flush=-1,tt}return e!==V?tt:a.wrap<=0?et:(2===a.wrap?(At(a,255&t.adler),At(a,t.adler>>8&255),At(a,t.adler>>16&255),At(a,t.adler>>24&255),At(a,255&t.total_in),At(a,t.total_in>>8&255),At(a,t.total_in>>16&255),At(a,t.total_in>>24&255)):(Et(a,t.adler>>>16),Et(a,65535&t.adler)),xt(t),a.wrap>0&&(a.wrap=-a.wrap),0!==a.pending?tt:et)},deflateEnd:t=>{if(Lt(t))return at;const e=t.state.status;return t.state=null,e===mt?gt(t,it):tt},deflateSetDictionary:(t,e)=>{let a=e.length;if(Lt(t))return at;const i=t.state,n=i.wrap;if(2===n||1===n&&i.status!==wt||i.lookahead)return at;if(1===n&&(t.adler=C(t.adler,e,a,0)),i.wrap=0,a>=i.w_size){0===n&&(kt(i.head),i.strstart=0,i.block_start=0,i.insert=0);let t=new Uint8Array(i.w_size);t.set(e.subarray(a-i.w_size,a),0),e=t,a=i.w_size}const s=t.avail_in,r=t.next_in,o=t.input;for(t.avail_in=a,t.next_in=0,t.input=e,Ut(i);i.lookahead>=3;){let t=i.strstart,e=i.lookahead-2;do{i.ins_h=yt(i,i.ins_h,i.window[t+3-1]),i.prev[t&i.w_mask]=i.head[i.ins_h],i.head[i.ins_h]=t,t++}while(--e);i.strstart=t,i.lookahead=2,Ut(i)}return i.strstart+=i.lookahead,i.block_start=i.strstart,i.insert=i.lookahead,i.lookahead=0,i.match_length=i.prev_length=2,i.match_available=0,t.next_in=r,t.input=o,t.avail_in=s,i.wrap=n,tt},deflateInfo:"pako deflate (from Nodeca project)"};const Ht=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);var jt=function(t){const e=Array.prototype.slice.call(arguments,1);for(;e.length;){const a=e.shift();if(a){if("object"!=typeof a)throw new TypeError(a+"must be non-object");for(const e in a)Ht(a,e)&&(t[e]=a[e])}}return t},Kt=t=>{let e=0;for(let a=0,i=t.length;a=252?6:t>=248?5:t>=240?4:t>=224?3:t>=192?2:1;Yt[254]=Yt[254]=1;var Gt=t=>{if("function"==typeof TextEncoder&&TextEncoder.prototype.encode)return(new TextEncoder).encode(t);let e,a,i,n,s,r=t.length,o=0;for(n=0;n>>6,e[s++]=128|63&a):a<65536?(e[s++]=224|a>>>12,e[s++]=128|a>>>6&63,e[s++]=128|63&a):(e[s++]=240|a>>>18,e[s++]=128|a>>>12&63,e[s++]=128|a>>>6&63,e[s++]=128|63&a);return e},Xt=(t,e)=>{const a=e||t.length;if("function"==typeof TextDecoder&&TextDecoder.prototype.decode)return(new TextDecoder).decode(t.subarray(0,e));let i,n;const s=new Array(2*a);for(n=0,i=0;i4)s[n++]=65533,i+=r-1;else{for(e&=2===r?31:3===r?15:7;r>1&&i1?s[n++]=65533:e<65536?s[n++]=e:(e-=65536,s[n++]=55296|e>>10&1023,s[n++]=56320|1023&e)}}return((t,e)=>{if(e<65534&&t.subarray&&Pt)return String.fromCharCode.apply(null,t.length===e?t:t.subarray(0,e));let a="";for(let i=0;i{(e=e||t.length)>t.length&&(e=t.length);let a=e-1;for(;a>=0&&128==(192&t[a]);)a--;return a<0||0===a?e:a+Yt[t[a]]>e?a:e};var qt=function(){this.input=null,this.next_in=0,this.avail_in=0,this.total_in=0,this.output=null,this.next_out=0,this.avail_out=0,this.total_out=0,this.msg="",this.state=null,this.data_type=2,this.adler=0};const Jt=Object.prototype.toString,{Z_NO_FLUSH:Qt,Z_SYNC_FLUSH:Vt,Z_FULL_FLUSH:$t,Z_FINISH:te,Z_OK:ee,Z_STREAM_END:ae,Z_DEFAULT_COMPRESSION:ie,Z_DEFAULT_STRATEGY:ne,Z_DEFLATED:se}=K;function re(t){this.options=jt({level:ie,method:se,chunkSize:16384,windowBits:15,memLevel:8,strategy:ne},t||{});let e=this.options;e.raw&&e.windowBits>0?e.windowBits=-e.windowBits:e.gzip&&e.windowBits>0&&e.windowBits<16&&(e.windowBits+=16),this.err=0,this.msg="",this.ended=!1,this.chunks=[],this.strm=new qt,this.strm.avail_out=0;let a=Mt.deflateInit2(this.strm,e.level,e.method,e.windowBits,e.memLevel,e.strategy);if(a!==ee)throw new Error(j[a]);if(e.header&&Mt.deflateSetHeader(this.strm,e.header),e.dictionary){let t;if(t="string"==typeof e.dictionary?Gt(e.dictionary):"[object ArrayBuffer]"===Jt.call(e.dictionary)?new Uint8Array(e.dictionary):e.dictionary,a=Mt.deflateSetDictionary(this.strm,t),a!==ee)throw new Error(j[a]);this._dict_set=!0}}function oe(t,e){const a=new re(e);if(a.push(t,!0),a.err)throw a.msg||j[a.err];return a.result}re.prototype.push=function(t,e){const a=this.strm,i=this.options.chunkSize;let n,s;if(this.ended)return!1;for(s=e===~~e?e:!0===e?te:Qt,"string"==typeof t?a.input=Gt(t):"[object ArrayBuffer]"===Jt.call(t)?a.input=new Uint8Array(t):a.input=t,a.next_in=0,a.avail_in=a.input.length;;)if(0===a.avail_out&&(a.output=new Uint8Array(i),a.next_out=0,a.avail_out=i),(s===Vt||s===$t)&&a.avail_out<=6)this.onData(a.output.subarray(0,a.next_out)),a.avail_out=0;else{if(n=Mt.deflate(a,s),n===ae)return a.next_out>0&&this.onData(a.output.subarray(0,a.next_out)),n=Mt.deflateEnd(this.strm),this.onEnd(n),this.ended=!0,n===ee;if(0!==a.avail_out){if(s>0&&a.next_out>0)this.onData(a.output.subarray(0,a.next_out)),a.avail_out=0;else if(0===a.avail_in)break}else this.onData(a.output)}return!0},re.prototype.onData=function(t){this.chunks.push(t)},re.prototype.onEnd=function(t){t===ee&&(this.result=Kt(this.chunks)),this.chunks=[],this.err=t,this.msg=this.strm.msg};var le={Deflate:re,deflate:oe,deflateRaw:function(t,e){return(e=e||{}).raw=!0,oe(t,e)},gzip:function(t,e){return(e=e||{}).gzip=!0,oe(t,e)},constants:K};const he=16209;var de=function(t,e){let a,i,n,s,r,o,l,h,d,_,f,c,u,w,m,b,g,p,k,v,y,x,z,A;const E=t.state;a=t.next_in,z=t.input,i=a+(t.avail_in-5),n=t.next_out,A=t.output,s=n-(e-t.avail_out),r=n+(t.avail_out-257),o=E.dmax,l=E.wsize,h=E.whave,d=E.wnext,_=E.window,f=E.hold,c=E.bits,u=E.lencode,w=E.distcode,m=(1<>>24,f>>>=p,c-=p,p=g>>>16&255,0===p)A[n++]=65535&g;else{if(!(16&p)){if(0==(64&p)){g=u[(65535&g)+(f&(1<>>=p,c-=p),c<15&&(f+=z[a++]<>>24,f>>>=p,c-=p,p=g>>>16&255,!(16&p)){if(0==(64&p)){g=w[(65535&g)+(f&(1<o){t.msg="invalid distance too far back",E.mode=he;break t}if(f>>>=p,c-=p,p=n-s,v>p){if(p=v-p,p>h&&E.sane){t.msg="invalid distance too far back",E.mode=he;break t}if(y=0,x=_,0===d){if(y+=l-p,p2;)A[n++]=x[y++],A[n++]=x[y++],A[n++]=x[y++],k-=3;k&&(A[n++]=x[y++],k>1&&(A[n++]=x[y++]))}else{y=n-v;do{A[n++]=A[y++],A[n++]=A[y++],A[n++]=A[y++],k-=3}while(k>2);k&&(A[n++]=A[y++],k>1&&(A[n++]=A[y++]))}break}}break}}while(a>3,a-=k,c-=k<<3,f&=(1<{const l=o.bits;let h,d,_,f,c,u,w=0,m=0,b=0,g=0,p=0,k=0,v=0,y=0,x=0,z=0,A=null;const E=new Uint16Array(16),R=new Uint16Array(16);let Z,U,S,D=null;for(w=0;w<=_e;w++)E[w]=0;for(m=0;m=1&&0===E[g];g--);if(p>g&&(p=g),0===g)return n[s++]=20971520,n[s++]=20971520,o.bits=1,0;for(b=1;b0&&(0===t||1!==g))return-1;for(R[1]=0,w=1;w<_e;w++)R[w+1]=R[w]+E[w];for(m=0;m852||2===t&&x>592)return 1;for(;;){Z=w-v,r[m]+1=u?(U=D[r[m]-u],S=A[r[m]-u]):(U=96,S=0),h=1<>v)+d]=Z<<24|U<<16|S|0}while(0!==d);for(h=1<>=1;if(0!==h?(z&=h-1,z+=h):z=0,m++,0==--E[w]){if(w===g)break;w=e[a+r[m]]}if(w>p&&(z&f)!==_){for(0===v&&(v=p),c+=b,k=w-v,y=1<852||2===t&&x>592)return 1;_=z&f,n[_]=p<<24|k<<16|c-s|0}}return 0!==z&&(n[c+z]=w-v<<24|64<<16|0),o.bits=p,0};const{Z_FINISH:be,Z_BLOCK:ge,Z_TREES:pe,Z_OK:ke,Z_STREAM_END:ve,Z_NEED_DICT:ye,Z_STREAM_ERROR:xe,Z_DATA_ERROR:ze,Z_MEM_ERROR:Ae,Z_BUF_ERROR:Ee,Z_DEFLATED:Re}=K,Ze=16180,Ue=16190,Se=16191,De=16192,Te=16194,Oe=16199,Ie=16200,Fe=16206,Le=16209,Ne=t=>(t>>>24&255)+(t>>>8&65280)+((65280&t)<<8)+((255&t)<<24);function Be(){this.strm=null,this.mode=0,this.last=!1,this.wrap=0,this.havedict=!1,this.flags=0,this.dmax=0,this.check=0,this.total=0,this.head=null,this.wbits=0,this.wsize=0,this.whave=0,this.wnext=0,this.window=null,this.hold=0,this.bits=0,this.length=0,this.offset=0,this.extra=0,this.lencode=null,this.distcode=null,this.lenbits=0,this.distbits=0,this.ncode=0,this.nlen=0,this.ndist=0,this.have=0,this.next=null,this.lens=new Uint16Array(320),this.work=new Uint16Array(288),this.lendyn=null,this.distdyn=null,this.sane=0,this.back=0,this.was=0}const Ce=t=>{if(!t)return 1;const e=t.state;return!e||e.strm!==t||e.mode16211?1:0},Me=t=>{if(Ce(t))return xe;const e=t.state;return t.total_in=t.total_out=e.total=0,t.msg="",e.wrap&&(t.adler=1&e.wrap),e.mode=Ze,e.last=0,e.havedict=0,e.flags=-1,e.dmax=32768,e.head=null,e.hold=0,e.bits=0,e.lencode=e.lendyn=new Int32Array(852),e.distcode=e.distdyn=new Int32Array(592),e.sane=1,e.back=-1,ke},He=t=>{if(Ce(t))return xe;const e=t.state;return e.wsize=0,e.whave=0,e.wnext=0,Me(t)},je=(t,e)=>{let a;if(Ce(t))return xe;const i=t.state;return e<0?(a=0,e=-e):(a=5+(e>>4),e<48&&(e&=15)),e&&(e<8||e>15)?xe:(null!==i.window&&i.wbits!==e&&(i.window=null),i.wrap=a,i.wbits=e,He(t))},Ke=(t,e)=>{if(!t)return xe;const a=new Be;t.state=a,a.strm=t,a.window=null,a.mode=Ze;const i=je(t,e);return i!==ke&&(t.state=null),i};let Pe,Ye,Ge=!0;const Xe=t=>{if(Ge){Pe=new Int32Array(512),Ye=new Int32Array(32);let e=0;for(;e<144;)t.lens[e++]=8;for(;e<256;)t.lens[e++]=9;for(;e<280;)t.lens[e++]=7;for(;e<288;)t.lens[e++]=8;for(me(1,t.lens,0,288,Pe,0,t.work,{bits:9}),e=0;e<32;)t.lens[e++]=5;me(2,t.lens,0,32,Ye,0,t.work,{bits:5}),Ge=!1}t.lencode=Pe,t.lenbits=9,t.distcode=Ye,t.distbits=5},We=(t,e,a,i)=>{let n;const s=t.state;return null===s.window&&(s.wsize=1<=s.wsize?(s.window.set(e.subarray(a-s.wsize,a),0),s.wnext=0,s.whave=s.wsize):(n=s.wsize-s.wnext,n>i&&(n=i),s.window.set(e.subarray(a-i,a-i+n),s.wnext),(i-=n)?(s.window.set(e.subarray(a-i,a),0),s.wnext=i,s.whave=s.wsize):(s.wnext+=n,s.wnext===s.wsize&&(s.wnext=0),s.whaveKe(t,15),inflateInit2:Ke,inflate:(t,e)=>{let a,i,n,s,r,o,l,h,d,_,f,c,u,w,m,b,g,p,k,v,y,x,z=0;const A=new Uint8Array(4);let E,R;const Z=new Uint8Array([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]);if(Ce(t)||!t.output||!t.input&&0!==t.avail_in)return xe;a=t.state,a.mode===Se&&(a.mode=De),r=t.next_out,n=t.output,l=t.avail_out,s=t.next_in,i=t.input,o=t.avail_in,h=a.hold,d=a.bits,_=o,f=l,x=ke;t:for(;;)switch(a.mode){case Ze:if(0===a.wrap){a.mode=De;break}for(;d<16;){if(0===o)break t;o--,h+=i[s++]<>>8&255,a.check=H(a.check,A,2,0),h=0,d=0,a.mode=16181;break}if(a.head&&(a.head.done=!1),!(1&a.wrap)||(((255&h)<<8)+(h>>8))%31){t.msg="incorrect header check",a.mode=Le;break}if((15&h)!==Re){t.msg="unknown compression method",a.mode=Le;break}if(h>>>=4,d-=4,y=8+(15&h),0===a.wbits&&(a.wbits=y),y>15||y>a.wbits){t.msg="invalid window size",a.mode=Le;break}a.dmax=1<>8&1),512&a.flags&&4&a.wrap&&(A[0]=255&h,A[1]=h>>>8&255,a.check=H(a.check,A,2,0)),h=0,d=0,a.mode=16182;case 16182:for(;d<32;){if(0===o)break t;o--,h+=i[s++]<>>8&255,A[2]=h>>>16&255,A[3]=h>>>24&255,a.check=H(a.check,A,4,0)),h=0,d=0,a.mode=16183;case 16183:for(;d<16;){if(0===o)break t;o--,h+=i[s++]<>8),512&a.flags&&4&a.wrap&&(A[0]=255&h,A[1]=h>>>8&255,a.check=H(a.check,A,2,0)),h=0,d=0,a.mode=16184;case 16184:if(1024&a.flags){for(;d<16;){if(0===o)break t;o--,h+=i[s++]<>>8&255,a.check=H(a.check,A,2,0)),h=0,d=0}else a.head&&(a.head.extra=null);a.mode=16185;case 16185:if(1024&a.flags&&(c=a.length,c>o&&(c=o),c&&(a.head&&(y=a.head.extra_len-a.length,a.head.extra||(a.head.extra=new Uint8Array(a.head.extra_len)),a.head.extra.set(i.subarray(s,s+c),y)),512&a.flags&&4&a.wrap&&(a.check=H(a.check,i,c,s)),o-=c,s+=c,a.length-=c),a.length))break t;a.length=0,a.mode=16186;case 16186:if(2048&a.flags){if(0===o)break t;c=0;do{y=i[s+c++],a.head&&y&&a.length<65536&&(a.head.name+=String.fromCharCode(y))}while(y&&c>9&1,a.head.done=!0),t.adler=a.check=0,a.mode=Se;break;case 16189:for(;d<32;){if(0===o)break t;o--,h+=i[s++]<>>=7&d,d-=7&d,a.mode=Fe;break}for(;d<3;){if(0===o)break t;o--,h+=i[s++]<>>=1,d-=1,3&h){case 0:a.mode=16193;break;case 1:if(Xe(a),a.mode=Oe,e===pe){h>>>=2,d-=2;break t}break;case 2:a.mode=16196;break;case 3:t.msg="invalid block type",a.mode=Le}h>>>=2,d-=2;break;case 16193:for(h>>>=7&d,d-=7&d;d<32;){if(0===o)break t;o--,h+=i[s++]<>>16^65535)){t.msg="invalid stored block lengths",a.mode=Le;break}if(a.length=65535&h,h=0,d=0,a.mode=Te,e===pe)break t;case Te:a.mode=16195;case 16195:if(c=a.length,c){if(c>o&&(c=o),c>l&&(c=l),0===c)break t;n.set(i.subarray(s,s+c),r),o-=c,s+=c,l-=c,r+=c,a.length-=c;break}a.mode=Se;break;case 16196:for(;d<14;){if(0===o)break t;o--,h+=i[s++]<>>=5,d-=5,a.ndist=1+(31&h),h>>>=5,d-=5,a.ncode=4+(15&h),h>>>=4,d-=4,a.nlen>286||a.ndist>30){t.msg="too many length or distance symbols",a.mode=Le;break}a.have=0,a.mode=16197;case 16197:for(;a.have>>=3,d-=3}for(;a.have<19;)a.lens[Z[a.have++]]=0;if(a.lencode=a.lendyn,a.lenbits=7,E={bits:a.lenbits},x=me(0,a.lens,0,19,a.lencode,0,a.work,E),a.lenbits=E.bits,x){t.msg="invalid code lengths set",a.mode=Le;break}a.have=0,a.mode=16198;case 16198:for(;a.have>>24,b=z>>>16&255,g=65535&z,!(m<=d);){if(0===o)break t;o--,h+=i[s++]<>>=m,d-=m,a.lens[a.have++]=g;else{if(16===g){for(R=m+2;d>>=m,d-=m,0===a.have){t.msg="invalid bit length repeat",a.mode=Le;break}y=a.lens[a.have-1],c=3+(3&h),h>>>=2,d-=2}else if(17===g){for(R=m+3;d>>=m,d-=m,y=0,c=3+(7&h),h>>>=3,d-=3}else{for(R=m+7;d>>=m,d-=m,y=0,c=11+(127&h),h>>>=7,d-=7}if(a.have+c>a.nlen+a.ndist){t.msg="invalid bit length repeat",a.mode=Le;break}for(;c--;)a.lens[a.have++]=y}}if(a.mode===Le)break;if(0===a.lens[256]){t.msg="invalid code -- missing end-of-block",a.mode=Le;break}if(a.lenbits=9,E={bits:a.lenbits},x=me(1,a.lens,0,a.nlen,a.lencode,0,a.work,E),a.lenbits=E.bits,x){t.msg="invalid literal/lengths set",a.mode=Le;break}if(a.distbits=6,a.distcode=a.distdyn,E={bits:a.distbits},x=me(2,a.lens,a.nlen,a.ndist,a.distcode,0,a.work,E),a.distbits=E.bits,x){t.msg="invalid distances set",a.mode=Le;break}if(a.mode=Oe,e===pe)break t;case Oe:a.mode=Ie;case Ie:if(o>=6&&l>=258){t.next_out=r,t.avail_out=l,t.next_in=s,t.avail_in=o,a.hold=h,a.bits=d,de(t,f),r=t.next_out,n=t.output,l=t.avail_out,s=t.next_in,i=t.input,o=t.avail_in,h=a.hold,d=a.bits,a.mode===Se&&(a.back=-1);break}for(a.back=0;z=a.lencode[h&(1<>>24,b=z>>>16&255,g=65535&z,!(m<=d);){if(0===o)break t;o--,h+=i[s++]<>p)],m=z>>>24,b=z>>>16&255,g=65535&z,!(p+m<=d);){if(0===o)break t;o--,h+=i[s++]<>>=p,d-=p,a.back+=p}if(h>>>=m,d-=m,a.back+=m,a.length=g,0===b){a.mode=16205;break}if(32&b){a.back=-1,a.mode=Se;break}if(64&b){t.msg="invalid literal/length code",a.mode=Le;break}a.extra=15&b,a.mode=16201;case 16201:if(a.extra){for(R=a.extra;d>>=a.extra,d-=a.extra,a.back+=a.extra}a.was=a.length,a.mode=16202;case 16202:for(;z=a.distcode[h&(1<>>24,b=z>>>16&255,g=65535&z,!(m<=d);){if(0===o)break t;o--,h+=i[s++]<>p)],m=z>>>24,b=z>>>16&255,g=65535&z,!(p+m<=d);){if(0===o)break t;o--,h+=i[s++]<>>=p,d-=p,a.back+=p}if(h>>>=m,d-=m,a.back+=m,64&b){t.msg="invalid distance code",a.mode=Le;break}a.offset=g,a.extra=15&b,a.mode=16203;case 16203:if(a.extra){for(R=a.extra;d>>=a.extra,d-=a.extra,a.back+=a.extra}if(a.offset>a.dmax){t.msg="invalid distance too far back",a.mode=Le;break}a.mode=16204;case 16204:if(0===l)break t;if(c=f-l,a.offset>c){if(c=a.offset-c,c>a.whave&&a.sane){t.msg="invalid distance too far back",a.mode=Le;break}c>a.wnext?(c-=a.wnext,u=a.wsize-c):u=a.wnext-c,c>a.length&&(c=a.length),w=a.window}else w=n,u=r-a.offset,c=a.length;c>l&&(c=l),l-=c,a.length-=c;do{n[r++]=w[u++]}while(--c);0===a.length&&(a.mode=Ie);break;case 16205:if(0===l)break t;n[r++]=a.length,l--,a.mode=Ie;break;case Fe:if(a.wrap){for(;d<32;){if(0===o)break t;o--,h|=i[s++]<{if(Ce(t))return xe;let e=t.state;return e.window&&(e.window=null),t.state=null,ke},inflateGetHeader:(t,e)=>{if(Ce(t))return xe;const a=t.state;return 0==(2&a.wrap)?xe:(a.head=e,e.done=!1,ke)},inflateSetDictionary:(t,e)=>{const a=e.length;let i,n,s;return Ce(t)?xe:(i=t.state,0!==i.wrap&&i.mode!==Ue?xe:i.mode===Ue&&(n=1,n=C(n,e,a,0),n!==i.check)?ze:(s=We(t,e,a,a),s?(i.mode=16210,Ae):(i.havedict=1,ke)))},inflateInfo:"pako inflate (from Nodeca project)"};var Je=function(){this.text=0,this.time=0,this.xflags=0,this.os=0,this.extra=null,this.extra_len=0,this.name="",this.comment="",this.hcrc=0,this.done=!1};const Qe=Object.prototype.toString,{Z_NO_FLUSH:Ve,Z_FINISH:$e,Z_OK:ta,Z_STREAM_END:ea,Z_NEED_DICT:aa,Z_STREAM_ERROR:ia,Z_DATA_ERROR:na,Z_MEM_ERROR:sa}=K;function ra(t){this.options=jt({chunkSize:65536,windowBits:15,to:""},t||{});const e=this.options;e.raw&&e.windowBits>=0&&e.windowBits<16&&(e.windowBits=-e.windowBits,0===e.windowBits&&(e.windowBits=-15)),!(e.windowBits>=0&&e.windowBits<16)||t&&t.windowBits||(e.windowBits+=32),e.windowBits>15&&e.windowBits<48&&0==(15&e.windowBits)&&(e.windowBits|=15),this.err=0,this.msg="",this.ended=!1,this.chunks=[],this.strm=new qt,this.strm.avail_out=0;let a=qe.inflateInit2(this.strm,e.windowBits);if(a!==ta)throw new Error(j[a]);if(this.header=new Je,qe.inflateGetHeader(this.strm,this.header),e.dictionary&&("string"==typeof e.dictionary?e.dictionary=Gt(e.dictionary):"[object ArrayBuffer]"===Qe.call(e.dictionary)&&(e.dictionary=new Uint8Array(e.dictionary)),e.raw&&(a=qe.inflateSetDictionary(this.strm,e.dictionary),a!==ta)))throw new Error(j[a])}function oa(t,e){const a=new ra(e);if(a.push(t),a.err)throw a.msg||j[a.err];return a.result}ra.prototype.push=function(t,e){const a=this.strm,i=this.options.chunkSize,n=this.options.dictionary;let s,r,o;if(this.ended)return!1;for(r=e===~~e?e:!0===e?$e:Ve,"[object ArrayBuffer]"===Qe.call(t)?a.input=new Uint8Array(t):a.input=t,a.next_in=0,a.avail_in=a.input.length;;){for(0===a.avail_out&&(a.output=new Uint8Array(i),a.next_out=0,a.avail_out=i),s=qe.inflate(a,r),s===aa&&n&&(s=qe.inflateSetDictionary(a,n),s===ta?s=qe.inflate(a,r):s===na&&(s=aa));a.avail_in>0&&s===ea&&a.state.wrap>0&&0!==t[a.next_in];)qe.inflateReset(a),s=qe.inflate(a,r);switch(s){case ia:case na:case aa:case sa:return this.onEnd(s),this.ended=!0,!1}if(o=a.avail_out,a.next_out&&(0===a.avail_out||s===ea))if("string"===this.options.to){let t=Wt(a.output,a.next_out),e=a.next_out-t,n=Xt(a.output,t);a.next_out=e,a.avail_out=i-e,e&&a.output.set(a.output.subarray(t,t+e),0),this.onData(n)}else this.onData(a.output.length===a.next_out?a.output:a.output.subarray(0,a.next_out));if(s!==ta||0!==o){if(s===ea)return s=qe.inflateEnd(this.strm),this.onEnd(s),this.ended=!0,!0;if(0===a.avail_in)break}}return!0},ra.prototype.onData=function(t){this.chunks.push(t)},ra.prototype.onEnd=function(t){t===ta&&("string"===this.options.to?this.result=this.chunks.join(""):this.result=Kt(this.chunks)),this.chunks=[],this.err=t,this.msg=this.strm.msg};var la={Inflate:ra,inflate:oa,inflateRaw:function(t,e){return(e=e||{}).raw=!0,oa(t,e)},ungzip:oa,constants:K};const{Deflate:ha,deflate:da,deflateRaw:_a,gzip:fa}=le,{Inflate:ca,inflate:ua,inflateRaw:wa,ungzip:ma}=la;var ba=ha,ga=da,pa=_a,ka=fa,va=ca,ya=ua,xa=wa,za=ma,Aa=K,Ea={Deflate:ba,deflate:ga,deflateRaw:pa,gzip:ka,Inflate:va,inflate:ya,inflateRaw:xa,ungzip:za,constants:Aa};t.Deflate=ba,t.Inflate=va,t.constants=Aa,t.default=Ea,t.deflate=ga,t.deflateRaw=pa,t.gzip=ka,t.inflate=ya,t.inflateRaw=xa,t.ungzip=za,Object.defineProperty(t,"__esModule",{value:!0})})); diff --git a/packages/baize-compress-image/lib/worker.ts b/packages/baize-compress-image/lib/worker.ts index 7025619..0f46ae5 100644 --- a/packages/baize-compress-image/lib/worker.ts +++ b/packages/baize-compress-image/lib/worker.ts @@ -1,9 +1,12 @@ // worker.ts import "./localforage.min.js"; +import "./pako.min.js"; +import "./UPNG.min.js"; import "localforage"; import { DEFAUTL_FORAGE_CONFIG, DEFAULT_QUALITY } from "./config.js"; declare const localforage: LocalForage; +declare const UPNG: any; interface TaskType { file: File; taskId: string; @@ -12,23 +15,39 @@ interface TaskType { const store = localforage.createInstance(DEFAUTL_FORAGE_CONFIG); -const compressJpegImage = async ({ img, quality }) => { +// 利用OffscreenCanvas压缩jpeg图片 +const compressJpegImage = async ({ file, quality }) => { + const img = file.slice(0, file.size, file.type); const offscreen = new OffscreenCanvas(100, 100); const ctx = offscreen.getContext("2d") as OffscreenCanvasRenderingContext2D; const imgData = await createImageBitmap(img); offscreen.width = imgData.width; offscreen.height = imgData.height; ctx.drawImage(imgData, 0, 0, offscreen.width, offscreen.height); - const res = await offscreen.convertToBlob({ type: "image/jpeg", quality }).then((blob) => { - const reader = new FileReader(); - reader.readAsDataURL(blob); - return new Promise((resolve) => { - reader.onloadend = () => { - resolve(reader.result); - }; - }); - }); - return res; + const blob = await offscreen.convertToBlob({ type: file.type, quality }); + const newFile = new File([blob], file.name, { type: file.type, lastModified: Date.now() }); + return newFile; +}; + +// 利用UPNG压缩png图片 +const compressPngImage = async ({ file, quality }) => { + const arrayBuffer = await file.arrayBuffer(); + const decoded = UPNG.decode(arrayBuffer); + const rgba8 = UPNG.toRGBA8(decoded); + const compressImg = UPNG.encode(rgba8, decoded.width, decoded.height, 256 * quality); + const compressFile = new File([compressImg], file.name, { type: file.type }); + return compressFile; +}; + +const compressImage = async ({ file, quality }) => { + const type = file.type.split("/")[1]; + if (type === "jpeg" || type === "jpg" || type === "webp") { + return await compressJpegImage({ file, quality }); + } else if (type === "png") { + return await compressPngImage({ file, quality }); + } else { + throw new Error("Unsupported image type"); + } }; self.onmessage = async (event) => { @@ -37,11 +56,16 @@ self.onmessage = async (event) => { const taskData = (await store.getItem(params.taskId)) as TaskType; await store.removeItem(params.taskId); const file = taskData.file; - const compressRes = await compressJpegImage({ - img: file.slice(0, file.size, file.type), + const compressRes = await compressImage({ + file: file, quality: taskData.quality || DEFAULT_QUALITY, }); - store.setItem(params.taskId, compressRes); + if (compressRes.size < file.size) { + // 压缩后大小小于原文件大小,则替换原文件 + store.setItem(params.taskId, compressRes); + } else { + store.setItem(params.taskId, file); + } self.postMessage( JSON.stringify({ type: "compressImageSuccess", diff --git a/packages/baize-compress-image/package.json b/packages/baize-compress-image/package.json index ef4b15e..0f65bc2 100644 --- a/packages/baize-compress-image/package.json +++ b/packages/baize-compress-image/package.json @@ -1,6 +1,6 @@ { "name": "baize-compress-image", - "version": "0.0.3", + "version": "0.0.4", "type": "module", "files": [ "dist", @@ -24,6 +24,7 @@ "devDependencies": { "@types/react": "^18.3.3", "@types/react-dom": "^18.3.0", + "@types/upng-js": "^2.1.5", "@vitejs/plugin-react": "^4.3.0", "cross-env": "^7.0.3", "react": "^18.3.1", @@ -33,7 +34,8 @@ "vite": "^5.2.10" }, "dependencies": { - "nanoid": "^5.0.7" + "nanoid": "^5.0.7", + "upng-js": "^2.1.0" }, "peerDependencies": { "localforage": ">=1.10.0" diff --git a/packages/baize-compress-image/src/main.tsx b/packages/baize-compress-image/src/main.tsx index f7a70fb..c63f924 100644 --- a/packages/baize-compress-image/src/main.tsx +++ b/packages/baize-compress-image/src/main.tsx @@ -1,24 +1,51 @@ -// import React from "react"; +import React, { useEffect, useState } from "react"; import ReactDOM from "react-dom/client"; import { compressImageWorker } from "../lib/main"; // import { compressImageWorker } from "../dist/index.js"; -import { useEffect } from "react"; function App() { useEffect(() => {}, []); - const handleFileChange = async (e: any) => { - const file = e.target.files[0]; + const [originalImageUrl, setOriginalImageUrl] = useState(""); + const [compressedImageUrl, setCompressedImageUrl] = useState(""); + + const handleFileChange = async (e: React.ChangeEvent) => { + const files = e.target.files; + if (!files || files.length === 0) { + return; + } + + const file = files[0]; console.time("compressImageWorker"); - const res = await compressImageWorker(file); - console.log("%c [ res ]-12", "font-size:13px; background:pink; color:#bf2c9f;", res); - console.timeEnd("compressImageWorker"); + try { + const compressedFile = await compressImageWorker(file); + console.log("%c [ res ]-12", "font-size:13px; background:pink; color:#bf2c9f;", compressedFile); + + const originalUrl = URL.createObjectURL(file); + setOriginalImageUrl(originalUrl); + + const compressedUrl = URL.createObjectURL(compressedFile); + setCompressedImageUrl(compressedUrl); + + console.timeEnd("compressImageWorker"); + } catch (error) { + console.error("Compression failed:", error); + } }; + return (
Hello localforage-worker!
- + +
+
+

原图

+ Original Image +
+
+

压缩图

+ Compressed Image
); diff --git a/packages/baize-compress-image/vite.config.ts b/packages/baize-compress-image/vite.config.ts index 3aef058..beac491 100644 --- a/packages/baize-compress-image/vite.config.ts +++ b/packages/baize-compress-image/vite.config.ts @@ -27,7 +27,7 @@ const libConfig = { inlineDynamicImports: true, }, }, - external: ["localforage"], + external: ["localforage", "upng-js"], }, }, }; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7051130..bd97aef 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -23,6 +23,9 @@ importers: nanoid: specifier: ^5.0.7 version: 5.0.7 + upng-js: + specifier: ^2.1.0 + version: 2.1.0 devDependencies: '@types/react': specifier: ^18.3.3 @@ -30,6 +33,9 @@ importers: '@types/react-dom': specifier: ^18.3.0 version: 18.3.0 + '@types/upng-js': + specifier: ^2.1.5 + version: 2.1.5 '@vitejs/plugin-react': specifier: ^4.3.0 version: 4.3.1(vite@5.2.11(@types/node@20.14.11)(less@4.2.0)(terser@5.31.3)) @@ -1196,6 +1202,9 @@ packages: '@types/stack-utils@2.0.3': resolution: {integrity: sha512-9aEbYZ3TbYMznPdcdr3SmIrLXwC/AKZXQeCf9Pgao5CKb8CyHuEX5jzWPTkvregvhRJHcpRO6BFoGW9ycaOkYw==} + '@types/upng-js@2.1.5': + resolution: {integrity: sha512-CzXg1lcCcWzrmYmke9BLbBPzb2DpdC1bXuXf0BtK3Bygvsozslei8S1bheDI1QUfZzZpMeQI5fywfnMj4CxocQ==} + '@types/webpack-sources@3.2.3': resolution: {integrity: sha512-4nZOdMwSPHZ4pTEZzSp0AsTM4K7Qmu40UKW4tJDiOVs20UzYF9l+qUe4s0ftfN0pin06n+5cWWDJXH+sbhAiDw==} @@ -2791,6 +2800,9 @@ packages: resolution: {integrity: sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==} engines: {node: '>=6'} + pako@1.0.11: + resolution: {integrity: sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==} + parse-json@5.2.0: resolution: {integrity: sha512-ayCKvm/phCGxOkYRSCM82iDwct8/EonSEgCSxWxD7ve6jHggsFl4fZVQBPRNgQoKiuV/odhFrGzQXZwbifC8Rg==} engines: {node: '>=8'} @@ -3493,6 +3505,9 @@ packages: peerDependencies: browserslist: '>= 4.21.0' + upng-js@2.1.0: + resolution: {integrity: sha512-d3xzZzpMP64YkjP5pr8gNyvBt7dLk/uGI67EctzDuVp4lCZyVMo0aJO6l/VDlgbInJYDY6cnClLoBp29eKWI6g==} + uri-js@4.4.1: resolution: {integrity: sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==} @@ -4866,6 +4881,8 @@ snapshots: '@types/stack-utils@2.0.3': {} + '@types/upng-js@2.1.5': {} + '@types/webpack-sources@3.2.3': dependencies: '@types/node': 20.14.11 @@ -6798,6 +6815,8 @@ snapshots: p-try@2.2.0: {} + pako@1.0.11: {} + parse-json@5.2.0: dependencies: '@babel/code-frame': 7.24.2 @@ -7540,6 +7559,10 @@ snapshots: escalade: 3.1.2 picocolors: 1.0.1 + upng-js@2.1.0: + dependencies: + pako: 1.0.11 + uri-js@4.4.1: dependencies: punycode: 2.3.1