UNPKG

cozy-iiif

Version:

A developer-friendly collection of abstractions and utilities built on top of @iiif/presentation-3 and @iiif/parser

122 lines (121 loc) 4.17 kB
import { h as e } from "../core-BHFaVC33.js"; import { t } from "../p-throttle-DZE1ty58.js"; //#region src/level-0/fetch-image-info.ts var n = (t) => ({ id: e(t, "id"), ...t }), r = (e) => fetch(e.serviceUrl).then((e) => e.json()).then(n), i = (() => { let e = { limit: 5, interval: 1e3 }, n = t(e); return { getInstance: () => n, getConfig: () => ({ ...e }), setConfig: (r) => { e = { ...e, ...r }, n = t(e); } }; })(), a = (e) => { if (!e) return; let t = i.getConfig(); (e.limit && e.limit !== t.limit || e.interval && e.interval !== t.interval) && i.setConfig({ ...t, ...e }); }, o = (e) => (e && a({ limit: e.callsPerSecond || 5, interval: 1e3 }), { loadImage: i.getInstance()((e) => new Promise((t, n) => { let r = new Image(); r.crossOrigin = "anonymous", r.onload = () => t(r), r.onerror = n, r.src = e; })) }), s = (e, t) => { let { x: n, y: r, w: i, h: a } = t, o = e.tiles[0].width, s = e.tiles[0].height || e.tiles[0].width; return `${e.id}/${n * o},${r * s},${i},${a}/${o},/0/default.jpg`; }, c = (e, t) => { let n = e.tiles[0].width, r = e.tiles[0].height || e.tiles[0].width, i = e.width, a = e.height, o = Math.floor(t.x / n), c = Math.floor(t.y / r), l = Math.ceil((t.x + t.w) / n), u = Math.ceil((t.y + t.h) / r), d = []; for (let t = c; t < u; t++) for (let c = o; c < l; c++) { if (c * n >= i || t * r >= a) continue; let o = Math.min(n, i - c * n), l = Math.min(r, a - t * r); d.push({ x: c, y: t, width: o, height: l, url: s(e, { x: c, y: t, w: o, h: l }) }); } return d; }, l = async (e, t) => { let n = await r(e), i = c(n, t), a = document.createElement("canvas"), s = a.getContext("2d"); if (!s) throw Error("Error initializing canvas context"); let l = n.tiles[0].width, u = n.tiles[0].height || n.tiles[0].width, d = (Math.ceil(t.w / l) + 1) * l, f = (Math.ceil(t.h / u) + 1) * u; a.width = d, a.height = f; let p = o({ callsPerSecond: 20 }); await Promise.all(i.map(async (e) => { let n = await p.loadImage(e.url), r = e.x * l - t.x, i = e.y * u - t.y; s.drawImage(n, r, i); })); let m = document.createElement("canvas"); m.width = t.w, m.height = t.h; let h = m.getContext("2d"); if (!h) throw Error("Error initializing canvas context"); return h.drawImage(a, 0, 0, t.w, t.h, 0, 0, t.w, t.h), new Promise((e, t) => { m.toBlob((n) => { n ? e(n) : t(/* @__PURE__ */ Error("Failed to create blob")); }, "image/jpeg", .95); }); }, u = (e, t) => { let n = e.tiles[0].scaleFactors.sort((e, t) => t - e); if (!t) return n[0]; let r = t.width ? e.width / t.width : Infinity, i = t.height ? e.height / t.height : Infinity, a = Math.min(r, i); for (let e of n) if (e <= a) return e; return n[n.length - 1]; }, d = (e, t) => { let n = u(e, t), r = Math.ceil(e.width / n), i = Math.ceil(e.height / n); if (t) { let n = e.width / e.height; t.width && r < t.width && (r = t.width, i = Math.ceil(r / n)), t.height && i < t.height && (i = t.height, r = Math.ceil(i * n)); } return { width: r, height: i }; }, f = (e, t) => { let n = u(e, t), r = e.tiles[0].width, i = e.tiles[0].height || e.tiles[0].width, a = Math.ceil(e.width / (r * n)), o = Math.ceil(e.height / (i * n)), s = []; for (let t = 0; t < o; t++) for (let o = 0; o < a; o++) { let a = Math.min(r, (e.width - o * r * n) / n), c = Math.min(i, (e.height - t * i * n) / n); a <= 0 || c <= 0 || s.push({ url: `${e.id}/${o * r * n},${t * i * n},${a * n},${c * n}/${Math.ceil(a)},/0/default.jpg`, width: Math.ceil(a), height: Math.ceil(c), x: o * r, y: t * i }); } return s; }, p = async (e, t) => { let n = await r(e), i = f(n, t), a = d(n, t), s = document.createElement("canvas"); s.width = a.width, s.height = a.height; let c = s.getContext("2d"); if (!c) throw Error("Error creating canvas context"); let l = o(); return await Promise.all(i.map(async (e) => { let t = await l.loadImage(e.url); c.drawImage(t, e.x, e.y); })), new Promise((e, t) => { s.toBlob((n) => { n ? e(n) : t(/* @__PURE__ */ Error("Failed to create blob")); }, "image/jpeg", .85); }); }; //#endregion export { l as cropRegion, p as getThumbnail };