show-first/js/plugins/ApngCore.js
2024-08-08 11:24:10 -05:00

859 lines
29 KiB
JavaScript

/*:
* @target MZ
* @plugindesc APNG形式の画像を扱うためのモジュールです。
* @author F_
*
* @help
*
* APNG形式の画像を扱うためのモジュールです。
*
* このプラグインは下記のライブラリを使用しています。
*
*
* -------------------------------------------------------
*
* apng-js (https://github.com/davidmz/apng-js)
*
*
* The MIT License (MIT)
*
* Copyright (c) 2016 David Mzareulyan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
var APNG = (() => {
var H = Object.create;
var T = Object.defineProperty;
var W = Object.getOwnPropertyDescriptor;
var X = Object.getOwnPropertyNames;
var K = Object.getPrototypeOf,
z = Object.prototype.hasOwnProperty;
var J = (h, n) => () => (n || h((n = { exports: {} }).exports, n), n.exports),
Q = (h, n) => {
for (var r in n) T(h, r, { get: n[r], enumerable: !0 });
},
M = (h, n, r, f) => {
if ((n && typeof n == "object") || typeof n == "function")
for (let c of X(n))
!z.call(h, c) &&
c !== r &&
T(h, c, {
get: () => n[c],
enumerable: !(f = W(n, c)) || f.enumerable,
});
return h;
};
var Z = (h, n, r) => (
(r = h != null ? H(K(h)) : {}),
M(
n || !h || !h.__esModule
? T(r, "default", { value: h, enumerable: !0 })
: r,
h
)
),
$ = (h) => M(T({}, "__esModule", { value: !0 }), h);
var S = J((R, I) => {
(function (n, r) {
typeof R == "object" && typeof I == "object"
? (I.exports = r())
: typeof define == "function" && define.amd
? define([], r)
: typeof R == "object"
? (R["apng-js"] = r())
: (n["apng-js"] = r());
})(R, function () {
return (function (h) {
var n = {};
function r(f) {
if (n[f]) return n[f].exports;
var c = (n[f] = { exports: {}, id: f, loaded: !1 });
return (
h[f].call(c.exports, c, c.exports, r), (c.loaded = !0), c.exports
);
}
return (r.m = h), (r.c = n), (r.p = ""), r(0);
})([
function (h, n, r) {
"use strict";
Object.defineProperty(n, "__esModule", { value: !0 }),
(n.isNotPNG = s),
(n.isNotAPNG = o),
(n.default = t);
var f = r(1),
c = N(f),
p = r(2);
function N(v) {
return v && v.__esModule ? v : { default: v };
}
var e = new Error("Not a PNG"),
a = new Error("Not an animated PNG");
function s(v) {
return v === e;
}
function o(v) {
return v === a;
}
var u = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]);
function t(v) {
var d = new Uint8Array(v);
if (
Array.prototype.some.call(u, function (O, w) {
return O !== d[w];
})
)
return e;
var y = !1;
if (
(l(d, function (O) {
return !(y = O === "acTL");
}),
!y)
)
return a;
var b = [],
E = [],
L = null,
m = null,
j = 0,
F = new p.APNG();
if (
(l(d, function (O, w, g, A) {
var k = new DataView(w.buffer);
switch (O) {
case "IHDR":
(L = w.subarray(g + 8, g + 8 + A)),
(F.width = k.getUint32(g + 8)),
(F.height = k.getUint32(g + 12));
break;
case "acTL":
F.numPlays = k.getUint32(g + 8 + 4);
break;
case "fcTL":
m && (F.frames.push(m), j++),
(m = new p.Frame()),
(m.width = k.getUint32(g + 8 + 4)),
(m.height = k.getUint32(g + 8 + 8)),
(m.left = k.getUint32(g + 8 + 12)),
(m.top = k.getUint32(g + 8 + 16));
var Y = k.getUint16(g + 8 + 20),
G = k.getUint16(g + 8 + 22);
G === 0 && (G = 100),
(m.delay = (1e3 * Y) / G),
m.delay <= 10 && (m.delay = 100),
(F.playTime += m.delay),
(m.disposeOp = k.getUint8(g + 8 + 24)),
(m.blendOp = k.getUint8(g + 8 + 25)),
(m.dataParts = []),
j === 0 && m.disposeOp === 2 && (m.disposeOp = 1);
break;
case "fdAT":
m && m.dataParts.push(w.subarray(g + 8 + 4, g + 8 + A));
break;
case "IDAT":
m && m.dataParts.push(w.subarray(g + 8, g + 8 + A));
break;
case "IEND":
E.push(P(w, g, 12 + A));
break;
default:
b.push(P(w, g, 12 + A));
}
}),
m && F.frames.push(m),
F.frames.length == 0)
)
return a;
var q = new Blob(b),
V = new Blob(E);
return (
F.frames.forEach(function (O) {
var w = [];
w.push(u),
L.set(U(O.width), 0),
L.set(U(O.height), 4),
w.push(D("IHDR", L)),
w.push(q),
O.dataParts.forEach(function (g) {
return w.push(D("IDAT", g));
}),
w.push(V),
(O.imageData = new Blob(w, { type: "image/png" })),
delete O.dataParts,
(w = null);
}),
F
);
}
function l(v, d) {
var y = new DataView(v.buffer),
b = 8,
E = void 0,
L = void 0,
m = void 0;
do
(L = y.getUint32(b)),
(E = i(v, b + 4, 4)),
(m = d(E, v, b, L)),
(b += 12 + L);
while (m !== !1 && E != "IEND" && b < v.length);
}
function i(v, d, y) {
var b = Array.prototype.slice.call(v.subarray(d, d + y));
return String.fromCharCode.apply(String, b);
}
function _(v) {
for (var d = new Uint8Array(v.length), y = 0; y < v.length; y++)
d[y] = v.charCodeAt(y);
return d;
}
function P(v, d, y) {
var b = new Uint8Array(y);
return b.set(v.subarray(d, d + y)), b;
}
var D = function (d, y) {
var b = d.length + y.length,
E = new Uint8Array(b + 8),
L = new DataView(E.buffer);
L.setUint32(0, y.length), E.set(_(d), 4), E.set(y, 8);
var m = (0, c.default)(E, 4, b);
return L.setUint32(b + 4, m), E;
},
U = function (d) {
return new Uint8Array([
(d >>> 24) & 255,
(d >>> 16) & 255,
(d >>> 8) & 255,
d & 255,
]);
};
},
function (h, n) {
"use strict";
Object.defineProperty(n, "__esModule", { value: !0 }),
(n.default = function (N) {
for (
var e =
arguments.length > 1 && arguments[1] !== void 0
? arguments[1]
: 0,
a =
arguments.length > 2 && arguments[2] !== void 0
? arguments[2]
: N.length - e,
s = -1,
o = e,
u = e + a;
o < u;
o++
)
s = (s >>> 8) ^ r[(s ^ N[o]) & 255];
return s ^ -1;
});
for (var r = new Uint32Array(256), f = 0; f < 256; f++) {
for (var c = f, p = 0; p < 8; p++)
c = c & 1 ? 3988292384 ^ (c >>> 1) : c >>> 1;
r[f] = c;
}
},
function (h, n, r) {
"use strict";
Object.defineProperty(n, "__esModule", { value: !0 }),
(n.Frame = n.APNG = void 0);
var f = (function () {
function o(u, t) {
for (var l = 0; l < t.length; l++) {
var i = t[l];
(i.enumerable = i.enumerable || !1),
(i.configurable = !0),
"value" in i && (i.writable = !0),
Object.defineProperty(u, i.key, i);
}
}
return function (u, t, l) {
return t && o(u.prototype, t), l && o(u, l), u;
};
})(),
c = r(3),
p = N(c);
function N(o) {
return o && o.__esModule ? o : { default: o };
}
function e(o, u) {
if (!(o instanceof u))
throw new TypeError("Cannot call a class as a function");
}
var a = (n.APNG = (function () {
function o() {
e(this, o),
(this.width = 0),
(this.height = 0),
(this.numPlays = 0),
(this.playTime = 0),
(this.frames = []);
}
return (
f(o, [
{
key: "createImages",
value: function () {
return Promise.all(
this.frames.map(function (t) {
return t.createImage();
})
);
},
},
{
key: "getPlayer",
value: function (t) {
var l = this,
i =
arguments.length > 1 && arguments[1] !== void 0
? arguments[1]
: !1;
return this.createImages().then(function () {
return new p.default(l, t, i);
});
},
},
]),
o
);
})()),
s = (n.Frame = (function () {
function o() {
e(this, o),
(this.left = 0),
(this.top = 0),
(this.width = 0),
(this.height = 0),
(this.delay = 0),
(this.disposeOp = 0),
(this.blendOp = 0),
(this.imageData = null),
(this.imageElement = null);
}
return (
f(o, [
{
key: "createImage",
value: function () {
var t = this;
return this.imageElement
? Promise.resolve()
: new Promise(function (l, i) {
var _ = URL.createObjectURL(t.imageData);
(t.imageElement = document.createElement("img")),
(t.imageElement.onload = function () {
URL.revokeObjectURL(_), l();
}),
(t.imageElement.onerror = function () {
URL.revokeObjectURL(_),
(t.imageElement = null),
i(new Error("Image creation error"));
}),
(t.imageElement.src = _);
});
},
},
]),
o
);
})());
},
function (h, n, r) {
"use strict";
Object.defineProperty(n, "__esModule", { value: !0 });
var f = (function () {
function u(t, l) {
for (var i = 0; i < l.length; i++) {
var _ = l[i];
(_.enumerable = _.enumerable || !1),
(_.configurable = !0),
"value" in _ && (_.writable = !0),
Object.defineProperty(t, _.key, _);
}
}
return function (t, l, i) {
return l && u(t.prototype, l), i && u(t, i), t;
};
})(),
c = r(4),
p = N(c);
function N(u) {
return u && u.__esModule ? u : { default: u };
}
function e(u, t) {
if (!(u instanceof t))
throw new TypeError("Cannot call a class as a function");
}
function a(u, t) {
if (!u)
throw new ReferenceError(
"this hasn't been initialised - super() hasn't been called"
);
return t && (typeof t == "object" || typeof t == "function")
? t
: u;
}
function s(u, t) {
if (typeof t != "function" && t !== null)
throw new TypeError(
"Super expression must either be null or a function, not " +
typeof t
);
(u.prototype = Object.create(t && t.prototype, {
constructor: {
value: u,
enumerable: !1,
writable: !0,
configurable: !0,
},
})),
t &&
(Object.setPrototypeOf
? Object.setPrototypeOf(u, t)
: (u.__proto__ = t));
}
var o = (function (u) {
s(t, u);
function t(l, i, _) {
e(this, t);
var P = a(
this,
(t.__proto__ || Object.getPrototypeOf(t)).call(this)
);
return (
(P.playbackRate = 1),
(P._currentFrameNumber = 0),
(P._ended = !1),
(P._paused = !0),
(P._numPlays = 0),
(P._apng = l),
(P.context = i),
P.stop(),
_ && P.play(),
P
);
}
return (
f(t, [
{
key: "renderNextFrame",
value: function () {
(this._currentFrameNumber =
(this._currentFrameNumber + 1) %
this._apng.frames.length),
this._currentFrameNumber ===
this._apng.frames.length - 1 &&
(this._numPlays++,
this._apng.numPlays !== 0 &&
this._numPlays >= this._apng.numPlays &&
((this._ended = !0), (this._paused = !0))),
this._prevFrame && this._prevFrame.disposeOp == 1
? this.context.clearRect(
this._prevFrame.left,
this._prevFrame.top,
this._prevFrame.width,
this._prevFrame.height
)
: this._prevFrame &&
this._prevFrame.disposeOp == 2 &&
this.context.putImageData(
this._prevFrameData,
this._prevFrame.left,
this._prevFrame.top
);
var i = this.currentFrame;
(this._prevFrame = i),
(this._prevFrameData = null),
i.disposeOp == 2 &&
(this._prevFrameData = this.context.getImageData(
i.left,
i.top,
i.width,
i.height
)),
i.blendOp == 0 &&
this.context.clearRect(
i.left,
i.top,
i.width,
i.height
),
this.context.drawImage(i.imageElement, i.left, i.top),
this.emit("frame", this._currentFrameNumber),
this._ended && this.emit("end");
},
},
{
key: "play",
value: function () {
var i = this;
this.emit("play"),
this._ended && this.stop(),
(this._paused = !1);
var _ =
performance.now() +
this.currentFrame.delay / this.playbackRate,
P = function D(U) {
if (!(i._ended || i._paused)) {
if (U >= _) {
for (; U - _ >= i._apng.playTime / i.playbackRate; )
(_ += i._apng.playTime / i.playbackRate),
i._numPlays++;
do
i.renderNextFrame(),
(_ += i.currentFrame.delay / i.playbackRate);
while (!i._ended && U > _);
}
requestAnimationFrame(D);
}
};
requestAnimationFrame(P);
},
},
{
key: "pause",
value: function () {
this._paused || (this.emit("pause"), (this._paused = !0));
},
},
{
key: "stop",
value: function () {
this.emit("stop"),
(this._numPlays = 0),
(this._ended = !1),
(this._paused = !0),
(this._currentFrameNumber = -1),
this.context.clearRect(
0,
0,
this._apng.width,
this._apng.height
),
this.renderNextFrame();
},
},
{
key: "currentFrameNumber",
get: function () {
return this._currentFrameNumber;
},
},
{
key: "currentFrame",
get: function () {
return this._apng.frames[this._currentFrameNumber];
},
},
{
key: "paused",
get: function () {
return this._paused;
},
},
{
key: "ended",
get: function () {
return this._ended;
},
},
]),
t
);
})(p.default);
n.default = o;
},
function (h, n) {
function r() {
(this._events = this._events || {}),
(this._maxListeners = this._maxListeners || void 0);
}
(h.exports = r),
(r.EventEmitter = r),
(r.prototype._events = void 0),
(r.prototype._maxListeners = void 0),
(r.defaultMaxListeners = 10),
(r.prototype.setMaxListeners = function (e) {
if (!c(e) || e < 0 || isNaN(e))
throw TypeError("n must be a positive number");
return (this._maxListeners = e), this;
}),
(r.prototype.emit = function (e) {
var a, s, o, u, t, l;
if (
(this._events || (this._events = {}),
e === "error" &&
(!this._events.error ||
(p(this._events.error) && !this._events.error.length)))
) {
if (((a = arguments[1]), a instanceof Error)) throw a;
var i = new Error(
'Uncaught, unspecified "error" event. (' + a + ")"
);
throw ((i.context = a), i);
}
if (((s = this._events[e]), N(s))) return !1;
if (f(s))
switch (arguments.length) {
case 1:
s.call(this);
break;
case 2:
s.call(this, arguments[1]);
break;
case 3:
s.call(this, arguments[1], arguments[2]);
break;
default:
(u = Array.prototype.slice.call(arguments, 1)),
s.apply(this, u);
}
else if (p(s))
for (
u = Array.prototype.slice.call(arguments, 1),
l = s.slice(),
o = l.length,
t = 0;
t < o;
t++
)
l[t].apply(this, u);
return !0;
}),
(r.prototype.addListener = function (e, a) {
var s;
if (!f(a)) throw TypeError("listener must be a function");
return (
this._events || (this._events = {}),
this._events.newListener &&
this.emit("newListener", e, f(a.listener) ? a.listener : a),
this._events[e]
? p(this._events[e])
? this._events[e].push(a)
: (this._events[e] = [this._events[e], a])
: (this._events[e] = a),
p(this._events[e]) &&
!this._events[e].warned &&
(N(this._maxListeners)
? (s = r.defaultMaxListeners)
: (s = this._maxListeners),
s &&
s > 0 &&
this._events[e].length > s &&
((this._events[e].warned = !0),
console.error(
"(node) warning: possible EventEmitter memory leak detected. %d listeners added. Use emitter.setMaxListeners() to increase limit.",
this._events[e].length
),
typeof console.trace == "function" && console.trace())),
this
);
}),
(r.prototype.on = r.prototype.addListener),
(r.prototype.once = function (e, a) {
if (!f(a)) throw TypeError("listener must be a function");
var s = !1;
function o() {
this.removeListener(e, o),
s || ((s = !0), a.apply(this, arguments));
}
return (o.listener = a), this.on(e, o), this;
}),
(r.prototype.removeListener = function (e, a) {
var s, o, u, t;
if (!f(a)) throw TypeError("listener must be a function");
if (!this._events || !this._events[e]) return this;
if (
((s = this._events[e]),
(u = s.length),
(o = -1),
s === a || (f(s.listener) && s.listener === a))
)
delete this._events[e],
this._events.removeListener &&
this.emit("removeListener", e, a);
else if (p(s)) {
for (t = u; t-- > 0; )
if (s[t] === a || (s[t].listener && s[t].listener === a)) {
o = t;
break;
}
if (o < 0) return this;
s.length === 1
? ((s.length = 0), delete this._events[e])
: s.splice(o, 1),
this._events.removeListener &&
this.emit("removeListener", e, a);
}
return this;
}),
(r.prototype.removeAllListeners = function (e) {
var a, s;
if (!this._events) return this;
if (!this._events.removeListener)
return (
arguments.length === 0
? (this._events = {})
: this._events[e] && delete this._events[e],
this
);
if (arguments.length === 0) {
for (a in this._events)
a !== "removeListener" && this.removeAllListeners(a);
return (
this.removeAllListeners("removeListener"),
(this._events = {}),
this
);
}
if (((s = this._events[e]), f(s))) this.removeListener(e, s);
else if (s)
for (; s.length; ) this.removeListener(e, s[s.length - 1]);
return delete this._events[e], this;
}),
(r.prototype.listeners = function (e) {
var a;
return (
!this._events || !this._events[e]
? (a = [])
: f(this._events[e])
? (a = [this._events[e]])
: (a = this._events[e].slice()),
a
);
}),
(r.prototype.listenerCount = function (e) {
if (this._events) {
var a = this._events[e];
if (f(a)) return 1;
if (a) return a.length;
}
return 0;
}),
(r.listenerCount = function (e, a) {
return e.listenerCount(a);
});
function f(e) {
return typeof e == "function";
}
function c(e) {
return typeof e == "number";
}
function p(e) {
return typeof e == "object" && e !== null;
}
function N(e) {
return e === void 0;
}
},
]);
});
});
var oe = {};
Q(oe, {
BLEND_OP_OVER: () => re,
BLEND_OP_SOURCE: () => C,
DISPOSE_OP_BACKGROUND: () => B,
DISPOSE_OP_NONE: () => ee,
DISPOSE_OP_PREVIOUS: () => te,
EMPTY: () => ne,
parse: () => ie,
});
var x = Z(S()),
ee = 0,
B = 1,
te = 2,
C = 0,
re = 1,
ne = { width: 1, height: 1, numPlays: 1, duration: 0, frames: [] };
async function ie(h) {
let n = (0, x.default)(h);
if ((0, x.isNotPNG)(n)) throw n;
return (0, x.isNotAPNG)(n) ? await ae(h) : await se(n);
}
async function ae(h) {
let n = new Blob([h], { type: "image/png" }),
r = await ue(n);
return {
width: r.width,
height: r.height,
numPlays: 1,
duration: 0,
frames: [
{
width: r.width,
height: r.height,
offsetX: 0,
offsetY: 0,
delay: 0,
disposeOp: B,
blendOp: C,
image: r,
},
],
};
}
async function se(h) {
await h.createImages();
let { width: n, height: r, numPlays: f, playTime: c } = h,
p = h.frames.map((N) => {
let {
left: e,
top: a,
width: s,
height: o,
delay: u,
disposeOp: t,
blendOp: l,
imageElement: i,
} = N;
return {
width: s,
height: o,
offsetX: e,
offsetY: a,
delay: u,
disposeOp: t,
blendOp: l,
image: i,
};
});
return { width: n, height: r, numPlays: f, duration: c, frames: p };
}
function ue(h) {
let n = URL.createObjectURL(h);
return new Promise((r, f) => {
let c = new Image();
c.addEventListener("load", () => {
URL.revokeObjectURL(n), r(c);
}),
c.addEventListener("error", (p) => {
URL.revokeObjectURL(n),
f(p.error instanceof Error ? p.error : new Error(p.message));
}),
(c.src = n);
});
}
return $(oe);
})();