From 52b79f4b5ffce7679fa5fcceeb80d615ad0cdf16 Mon Sep 17 00:00:00 2001 From: stonedDiscord Date: Fri, 11 Mar 2022 18:15:55 +0100 Subject: add apng preanims --- webAO/utils/calculateApngLength.js | 26 ++++++++++++++++++++++++++ webAO/utils/calculatorHandler.js | 2 ++ webAO/utils/getAnimLength.js | 4 ++-- 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 webAO/utils/calculateApngLength.js (limited to 'webAO/utils') diff --git a/webAO/utils/calculateApngLength.js b/webAO/utils/calculateApngLength.js new file mode 100644 index 0000000..bc6b3fa --- /dev/null +++ b/webAO/utils/calculateApngLength.js @@ -0,0 +1,26 @@ + /** + * Adds up the chunk delays to find out how long a APNG is + * @param {data} apngFile the APNG data + */ +const calculateApngLength = (apngFile) => { + const d = new Uint8Array(apngFile); + // https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chunk + let duration = 0; + for (var i = 0; i < d.length; i++) { + // Find fcTL header (66 63 54 4C) + if (d[i] === 0x66 + && d[i + 1] === 0x63 + && d[i + 2] === 0x54 + && d[i + 3] === 0x4C) { + console.log("found apng header"); + // numerator and denominator + let delay = ((d[i + 21] / d[i + 23]) * 1000) + + // minimum is 100ms + duration += delay < 100 ? 100 : delay; + } + } + console.debug(duration); + return duration * 10; +}; +export default calculateApngLength; diff --git a/webAO/utils/calculatorHandler.js b/webAO/utils/calculatorHandler.js index 7686573..9072195 100644 --- a/webAO/utils/calculatorHandler.js +++ b/webAO/utils/calculatorHandler.js @@ -1,7 +1,9 @@ import calculateGifLength from './calculateGifLength'; import calculateWebpLength from './calculateWebpLength'; +import calculateApngLength from './calculateApngLength'; export default { '.gif': calculateGifLength, '.webp': calculateWebpLength, + '.apng': calculateApngLength, }; diff --git a/webAO/utils/getAnimLength.js b/webAO/utils/getAnimLength.js index 5b24e84..1441548 100644 --- a/webAO/utils/getAnimLength.js +++ b/webAO/utils/getAnimLength.js @@ -1,6 +1,6 @@ import calculatorHandler from './calculatorHandler'; import fileExists from './fileExists.js'; -import requestBuffer from '../services/request.js'; +import {requestBuffer} from '../services/request.js'; /** * Gets animation length. If the animation cannot be found, it will * silently fail and return 0 instead. @@ -8,7 +8,7 @@ import requestBuffer from '../services/request.js'; */ const getAnimLength = async (url) => { - const extensions = ['.gif', '.webp']; + const extensions = ['.gif', '.webp', '.apng']; for (const extension of extensions) { const urlWithExtension = url + extension; const exists = await fileExists(urlWithExtension); -- cgit