// Read each animation at its authored times. For a numbered clip sample a single pass at 10 fps, // an editor rate whose 100 ms frames are also exactly representable by a GIF made from the folder. import assert from 'node:assert/strict'; import path from 'node:path'; import { sequenceAction } from '../src/actions.js'; import { animationGIF } from '../src/gif.js'; import { folderGIF } from '../src/frame-folder.js'; import { sceneGIF } from '../src/scene-export.js'; import { renderScene } from '../src/scene.js'; import { gifFrame, sha } from './example-delivery.mjs'; function moreThan256(data) { const colors = new Set(); for (let i = 0; i < data.length; i += 4) { colors.add(data[i + 3] < 128 ? -1 : (data[i] << 16) + (data[i + 1] << 8) + data[i + 2]); if (colors.size > 256) return true; } return false; } async function gif(delivery, name, make, frames, delays, size, loop) { let result; try { result = make(); } catch (error) { assert.match(error.message, /GIF frame holds 256/); assert.ok(frames.some(f => moreThan256(f.data)), name + ' should fit a GIF'); delivery.restrictions.set(name, 'A frame needs more than 256 distinct GIF palette entries.'); return; } const expected = frames.map((frame, i) => ({ ...gifFrame(frame), ms: delays[i] })); await delivery.image(name, result.data, { size, frames: expected, loop: loop ? 0 : null }); } export async function spriteGIFs(delivery, project, plays) { if (!plays) { assert.throws(() => animationGIF(project), /frames are (?:stills|tiles)/); delivery.restrictions.set('gif', 'A still has no animation to encode.'); return; } for (const [name, sequence] of Object.entries(project.animations)) { const frames = sequence.frames.map(i => project.frames[i]); let remainder = 0; const delays = frames.map(f => { const exact = f.duration + remainder, ms = Math.max(20, Math.round(exact / 10) * 10); remainder = exact < 20 ? 0 : exact - ms; return ms; }); await gif(delivery, 'gifs/' + name + '.gif', () => animationGIF(project, name), frames, delays, [project.width, project.height], sequence.loop); } } export async function sceneMovie(delivery, scene) { const frames = Array.from({ length: Math.max(1, Math.round(scene.duration / 100)) }, (_, i) => renderScene(scene, { time: i * 100 })); await gif(delivery, 'scene.gif', () => sceneGIF(scene, { fps: 10 }), frames, frames.map(() => 100), [scene.width, scene.height], true); } export async function numberedClip(delivery, document, { project, scene, plays = true }) { const fps = 10, make = () => sequenceAction(document, { fps }); if (!plays) { assert.throws(make, /frames are (?:stills|tiles)/); delivery.restrictions.set('sequence', 'A still has no animation to sample.'); return; } const clip = make(), width = (project ?? scene).width, height = (project ?? scene).height; const { info } = clip; let source; if (project) { const name = info.animation ?? info.effect ?? info.clip; const animation = project.animations[name]; assert.ok(animation, 'compiled animation for ' + name); source = animation.frames.map(i => project.frames[i]); } const duration = scene ? scene.duration : source.reduce((total, f) => total + f.duration, 0); assert.equal(info.frames, Math.ceil(duration * fps / 1000)); assert.deepEqual([info.width, info.height, info.fps.numerator, info.fps.denominator], [width, height, fps, 1]); const frames = []; for (let i = 0, pos = 0, end = source?.[0].duration; i < info.frames; i++) { const time = i * 100; while (source && pos + 1 < source.length && time >= end) end += source[++pos].duration; const frame = scene ? renderScene(scene, { time }) : source[pos]; frames.push(frame); const name = info.pattern.replace(/%0(\d+)d/, (_, digits) => String(i + 1).padStart(Number(digits), '0')); const bytes = clip.files.get(name); assert.ok(bytes, name); await delivery.image('sequence/' + name, bytes, { size: [width, height], frames: [{ rgba: sha(frame.data) }] }); } for (const [name, bytes] of clip.files) if (!name.endsWith('.png')) await delivery.put('sequence/' + name, bytes); assert.equal(clip.files.size, frames.length + 2); const dir = path.join(delivery.directory, 'sequence'); await gif(delivery, 'numbered.gif', () => folderGIF(dir, { fps }), frames, frames.map(() => 100), [width, height], true); }