220 lines
6.2 KiB
JavaScript
220 lines
6.2 KiB
JavaScript
(function() {
|
|
var t = [
|
|
"0",
|
|
"1",
|
|
"2",
|
|
"3",
|
|
"4",
|
|
"5",
|
|
"6",
|
|
"7",
|
|
"8",
|
|
"9",
|
|
"A",
|
|
"B",
|
|
"C",
|
|
"D",
|
|
"E",
|
|
"F",
|
|
"G",
|
|
"H",
|
|
"I",
|
|
"J",
|
|
"K",
|
|
"L",
|
|
"M",
|
|
"N",
|
|
"O",
|
|
"P",
|
|
"Q",
|
|
"R",
|
|
"S",
|
|
"T",
|
|
"U",
|
|
"V",
|
|
"W",
|
|
"X",
|
|
"Y",
|
|
"Z",
|
|
"a",
|
|
"b",
|
|
"c",
|
|
"d",
|
|
"e",
|
|
"f",
|
|
"g",
|
|
"h",
|
|
"i",
|
|
"j",
|
|
"k",
|
|
"l",
|
|
"m",
|
|
"n",
|
|
"o",
|
|
"p",
|
|
"q",
|
|
"r",
|
|
"s",
|
|
"t",
|
|
"u",
|
|
"v",
|
|
"w",
|
|
"x",
|
|
"y",
|
|
"z",
|
|
"#",
|
|
"$",
|
|
"%",
|
|
"*",
|
|
"+",
|
|
",",
|
|
"-",
|
|
".",
|
|
":",
|
|
";",
|
|
"=",
|
|
"?",
|
|
"@",
|
|
"[",
|
|
"]",
|
|
"^",
|
|
"_",
|
|
"{",
|
|
"|",
|
|
"}",
|
|
"~"
|
|
], e = (e, a) => {
|
|
var i = "";
|
|
for (let h = 1; h <= a; h++) {
|
|
let r = Math.floor(e) / Math.pow(83, a - h) % 83;
|
|
i += t[Math.floor(r)];
|
|
}
|
|
return i;
|
|
}, a = (t) => {
|
|
let e = t / 255;
|
|
return e <= .04045 ? e / 12.92 : Math.pow((e + .055) / 1.055, 2.4);
|
|
}, i = (t) => {
|
|
let e = Math.max(0, Math.min(1, t));
|
|
return e <= .0031308 ? Math.trunc(12.92 * e * 255 + .5) : Math.trunc(255 * (1.055 * Math.pow(e, .4166666666666667) - .055) + .5);
|
|
}, h = (t, e) => ((t) => t < 0 ? -1 : 1)(t) * Math.pow(Math.abs(t), e), r = class extends Error {
|
|
constructor(t) {
|
|
super(t), this.name = "ValidationError", this.message = t;
|
|
}
|
|
}, o = (t, e, i, h) => {
|
|
let r = 0, o = 0, n = 0, l = 4 * e;
|
|
for (let g = 0; g < e; g++) {
|
|
let e = 4 * g;
|
|
for (let s = 0; s < i; s++) {
|
|
let i = e + s * l, m = h(g, s);
|
|
r += m * a(t[i]), o += m * a(t[i + 1]), n += m * a(t[i + 2]);
|
|
}
|
|
}
|
|
let s = 1 / (e * i);
|
|
return [
|
|
r * s,
|
|
o * s,
|
|
n * s
|
|
];
|
|
};
|
|
function n(t, e, a) {
|
|
const i = new OffscreenCanvas(e, a), h = i.getContext("2d");
|
|
if (!h) throw new Error("Failed to get 2d context from OffscreenCanvas");
|
|
return h.imageSmoothingEnabled = !0, h.imageSmoothingQuality = "high", h.drawImage(t, 0, 0, e, a), t.close(), i;
|
|
}
|
|
function l(t, e, a) {
|
|
if (!(.5 * t.width > e && .5 * t.height > a)) return n(t, e, a);
|
|
let i = new OffscreenCanvas(Math.floor(.5 * t.width), Math.floor(.5 * t.height)), h = i.getContext("2d");
|
|
for (h.imageSmoothingEnabled = !0, h.imageSmoothingQuality = "medium", h.drawImage(t, 0, 0, i.width, i.height), t.close(); .5 * i.width > e && .5 * i.height > a;) {
|
|
const t = new OffscreenCanvas(Math.floor(.5 * i.width), Math.floor(.5 * i.height)), e = t.getContext("2d");
|
|
e.imageSmoothingEnabled = !0, e.imageSmoothingQuality = "medium", e.drawImage(i, 0, 0, t.width, t.height), i.width = 0, i.height = 0, i = t;
|
|
}
|
|
const r = new OffscreenCanvas(e, a), o = r.getContext("2d");
|
|
return o.imageSmoothingEnabled = !0, o.imageSmoothingQuality = "high", o.drawImage(i, 0, 0, e, a), i.width = 0, i.height = 0, r;
|
|
}
|
|
self.onmessage = async (t) => {
|
|
if ("PROCESS_PHOTO" !== t.data?.type) return;
|
|
const { id: a, file: s, origWidth: g, origHeight: m, mainWidth: c, mainHeight: f, quality: d, thumbWidth: u, thumbHeight: w, thumbQuality: M, computeBlurhash: y } = t.data;
|
|
try {
|
|
const t = await async function(t, e, a, i) {
|
|
const h = i ? {
|
|
resizeWidth: e,
|
|
resizeHeight: a,
|
|
resizeQuality: "high"
|
|
} : {}, r = (t) => t.width === e && t.height === a;
|
|
try {
|
|
const i = await createImageBitmap(t, h);
|
|
return r(i) ? n(i, e, a) : l(i, e, a);
|
|
} catch (g) {
|
|
console.warn("Hardware resize from File failed, retrying via in-memory Blob:", g);
|
|
}
|
|
const o = await t.arrayBuffer(), s = new Blob([o], { type: t.type });
|
|
try {
|
|
const t = await createImageBitmap(s, h);
|
|
return r(t) ? n(t, e, a) : l(t, e, a);
|
|
} catch (m) {
|
|
console.warn("Hardware resize from Blob failed, falling back to stepped software scaling:", m);
|
|
}
|
|
return l(await createImageBitmap(s), e, a);
|
|
}(s, c, f, g !== c || m !== f), b = await async function(t, e, a) {
|
|
if (t.width === e && t.height === a) return t;
|
|
try {
|
|
const i = await createImageBitmap(t, {
|
|
resizeWidth: e,
|
|
resizeHeight: a,
|
|
resizeQuality: "high"
|
|
});
|
|
return i.width === e && i.height === a ? n(i, e, a) : l(i, e, a);
|
|
} catch (i) {
|
|
return console.warn("Native thumbnail resize failed, scaling from canvas:", i), l(await createImageBitmap(t), e, a);
|
|
}
|
|
}(t, u, w);
|
|
let x = null;
|
|
if (y) try {
|
|
x = function(t) {
|
|
const a = Math.min(1, 64 / Math.max(t.width, t.height)), n = Math.max(1, Math.round(t.width * a)), l = Math.max(1, Math.round(t.height * a)), s = new OffscreenCanvas(n, l).getContext("2d", { willReadFrequently: !0 });
|
|
return s.imageSmoothingEnabled = !0, s.imageSmoothingQuality = "high", s.drawImage(t, 0, 0, n, l), ((t, a, n) => {
|
|
if (a * n * 4 !== t.length) throw new r("Width and height must match the pixels array");
|
|
let l = [];
|
|
for (let e = 0; e < 3; e++) for (let i = 0; i < 4; i++) {
|
|
let h = 0 == i && 0 == e ? 1 : 2, r = o(t, a, n, (t, r) => h * Math.cos(Math.PI * i * t / a) * Math.cos(Math.PI * e * r / n));
|
|
l.push(r);
|
|
}
|
|
let s, g = l[0], m = l.slice(1), c = "";
|
|
if (c += e(21, 1), m.length > 0) {
|
|
let t = Math.max(...m.map((t) => Math.max(...t))), a = Math.floor(Math.max(0, Math.min(82, Math.floor(166 * t - .5))));
|
|
s = (a + 1) / 166, c += e(a, 1);
|
|
} else s = 1, c += e(0, 1);
|
|
return c += e(((t) => (i(t[0]) << 16) + (i(t[1]) << 8) + i(t[2]))(g), 4), m.forEach((t) => {
|
|
c += e(((t, e) => 19 * Math.floor(Math.max(0, Math.min(18, Math.floor(9 * h(t[0] / e, .5) + 9.5)))) * 19 + 19 * Math.floor(Math.max(0, Math.min(18, Math.floor(9 * h(t[1] / e, .5) + 9.5)))) + Math.floor(Math.max(0, Math.min(18, Math.floor(9 * h(t[2] / e, .5) + 9.5)))))(t, s), 2);
|
|
}), c;
|
|
})(s.getImageData(0, 0, n, l).data, n, l);
|
|
}(b);
|
|
} catch (p) {
|
|
console.warn("Could not generate blurhash (possible canvas fingerprinting protection):", p);
|
|
}
|
|
const v = `${t.width}x${t.height}`, B = `${b.width}x${b.height}`, [C, I] = await Promise.all([t.convertToBlob({
|
|
type: "image/jpeg",
|
|
quality: d
|
|
}), b.convertToBlob({
|
|
type: "image/jpeg",
|
|
quality: M
|
|
})]);
|
|
t.width = 0, t.height = 0, b !== t && (b.width = 0, b.height = 0), self.postMessage({
|
|
id: a,
|
|
success: !0,
|
|
mainBlob: C,
|
|
mainDim: v,
|
|
thumbBlob: I,
|
|
thumbDim: B,
|
|
blurhash: x
|
|
});
|
|
} catch (b) {
|
|
self.postMessage({
|
|
id: a,
|
|
success: !1,
|
|
error: b.message
|
|
});
|
|
}
|
|
};
|
|
})();
|