aboutsummaryrefslogtreecommitdiffstats
path: root/src/image-thumbnails.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/image-thumbnails.js')
-rw-r--r--src/image-thumbnails.js29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/image-thumbnails.js b/src/image-thumbnails.js
index 2686dac..80e6915 100644
--- a/src/image-thumbnails.js
+++ b/src/image-thumbnails.js
@@ -96,3 +96,32 @@ export function makeImageThumbnails(blob, { width = 0, tileSize = 0 } = {}) {
|| typeof createImageBitmap === 'undefined' || queue.length >= MAX_QUEUE) return Promise.resolve(null);
return new Promise((resolve) => { queue.push({ input: { blob, width, tileSize }, resolve }); drain(); });
}
+
+// A GIF with no loop instruction (the NETSCAPE2.0 application extension)
+// plays once and stops on its last frame: every browser reads it that way.
+// Plenty of GIFs are made without one (a gifs.nostr.build reply GIF froze
+// after a second, Adam, 2026-11-19), and people expect a GIF to loop. The
+// extension, "loop forever", goes right after the global colour table.
+// Returns the patched bytes, or null when the file loops already or isn't
+// a GIF.
+export function loopGif(bytes) {
+ const b = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
+ if (b.length < 13 || b[0] !== 0x47 || b[1] !== 0x49 || b[2] !== 0x46) return null; // "GIF"
+ const tag = [0x4e, 0x45, 0x54, 0x53, 0x43, 0x41, 0x50, 0x45, 0x32, 0x2e, 0x30]; // NETSCAPE2.0
+ const anim = [0x41, 0x4e, 0x49, 0x4d, 0x45, 0x58, 0x54, 0x53, 0x31, 0x2e, 0x30]; // ANIMEXTS1.0
+ outer: for (let i = 13; i < b.length - 11; i++) {
+ if (b[i] !== 0x4e && b[i] !== 0x41) continue;
+ const t = b[i] === 0x4e ? tag : anim;
+ for (let j = 1; j < 11; j++) if (b[i + j] !== t[j]) continue outer;
+ return null;
+ }
+ const packed = b[10];
+ const at = 13 + (packed & 0x80 ? 3 * 2 ** ((packed & 7) + 1) : 0);
+ if (at > b.length) return null;
+ const ext = new Uint8Array([0x21, 0xff, 0x0b, ...tag, 0x03, 0x01, 0x00, 0x00, 0x00]);
+ const out = new Uint8Array(b.length + ext.length);
+ out.set(b.subarray(0, at), 0);
+ out.set(ext, at);
+ out.set(b.subarray(at), at + ext.length);
+ return out;
+}