@johnhenry/mallory-cli 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -2
- package/bin/mallory.mjs +51 -0
- package/package.json +51 -4
- package/src/a2a/client.js +511 -0
- package/src/a2a/demo-agent.js +396 -0
- package/src/a2a/hub.js +578 -0
- package/src/a2a/settings.js +88 -0
- package/src/a2a/task.js +358 -0
- package/src/acp/client.js +573 -0
- package/src/acp/demo-agent.js +332 -0
- package/src/acp/hub.js +632 -0
- package/src/acp/settings.js +78 -0
- package/src/actions/map.js +524 -0
- package/src/agent/cloud.js +77 -0
- package/src/agent/compact.js +84 -0
- package/src/agent/constants.js +39 -0
- package/src/agent/diff.js +101 -0
- package/src/agent/fork.js +159 -0
- package/src/agent/grounding.js +73 -0
- package/src/agent/guards.js +129 -0
- package/src/agent/local-transport.js +152 -0
- package/src/agent/loop.js +479 -0
- package/src/agent/park-store.js +77 -0
- package/src/agent/park.js +76 -0
- package/src/agent/ports.js +28 -0
- package/src/agent/providers.js +591 -0
- package/src/agent/restart.js +51 -0
- package/src/agent/router.js +380 -0
- package/src/agent/run.js +861 -0
- package/src/agent/tools.js +247 -0
- package/src/agent-library/engine.js +43 -0
- package/src/ai/bridge.js +155 -0
- package/src/ai/policy.js +131 -0
- package/src/aimatey/vendor.js +18 -0
- package/src/audio/container-duration.js +35 -0
- package/src/audio/instruments.js +776 -0
- package/src/audio/lanes.js +230 -0
- package/src/audio/patch.js +1410 -0
- package/src/audio/plan.js +276 -0
- package/src/audio/rack.js +1006 -0
- package/src/audio/sample-trim.js +293 -0
- package/src/audio/soundfont.js +243 -0
- package/src/audio/take-store.js +87 -0
- package/src/audio/takes.js +129 -0
- package/src/audio/wav.js +170 -0
- package/src/audio/zzfx.js +333 -0
- package/src/autonomy/bundle.js +207 -0
- package/src/autonomy/confine.js +61 -0
- package/src/autonomy/enforce.js +155 -0
- package/src/autonomy/file-kv.js +88 -0
- package/src/autonomy/ledger.js +203 -0
- package/src/autonomy/pins.js +50 -0
- package/src/autonomy/psl/index.js +32 -0
- package/src/autonomy/psl/suffixes.json +1 -0
- package/src/autonomy/record.js +28 -0
- package/src/autonomy/routines.js +107 -0
- package/src/autonomy/server.js +175 -0
- package/src/autonomy/signer-node.js +167 -0
- package/src/autonomy/store.js +43 -0
- package/src/autonomy/unattended.js +13 -0
- package/src/bytes/bytes.js +378 -0
- package/src/calc/syntax.js +125 -0
- package/src/calc/tex.js +28 -0
- package/src/calendar/vendor.js +2 -0
- package/src/clipboard.js +130 -0
- package/src/clsh/clsh.js +3417 -0
- package/src/codec/capnp.js +1804 -0
- package/src/codec/core.js +2043 -0
- package/src/codec/vendor/avro.js +136 -0
- package/src/codec/vendor/cbor.js +25 -0
- package/src/codec/vendor/flatbuffers.js +206 -0
- package/src/codec/vendor/msgpack.js +9 -0
- package/src/codec/vendor/protobuf.js +53 -0
- package/src/delegation/caps.js +60 -0
- package/src/delegation/config.js +32 -0
- package/src/device/model.js +246 -0
- package/src/device/recording.js +198 -0
- package/src/device/stream-model.js +151 -0
- package/src/documents/documents.js +1224 -0
- package/src/effects/catalog.js +63 -0
- package/src/effects/extras.js +133 -0
- package/src/effects/parse.js +339 -0
- package/src/embed/protocol.js +131 -0
- package/src/env/env.js +159 -0
- package/src/exact/vendor.js +45 -0
- package/src/example-meta.js +164 -0
- package/src/examples.js +94 -0
- package/src/export/canvas-image.js +217 -0
- package/src/export/expr-latex-lazy.js +8 -0
- package/src/export/formats.js +322 -0
- package/src/export/latex.js +183 -0
- package/src/export/pane.js +184 -0
- package/src/export/value.js +452 -0
- package/src/export/zip.js +214 -0
- package/src/ext/access.js +58 -0
- package/src/ext/btasks.js +225 -0
- package/src/ext/client.js +209 -0
- package/src/ext/tools.js +69 -0
- package/src/ext/view.js +105 -0
- package/src/ext/webmcp-tabs.js +299 -0
- package/src/files/archive.js +232 -0
- package/src/files/files.js +317 -0
- package/src/formats/json.js +24 -0
- package/src/formats/jupyter.js +188 -0
- package/src/formats/markdown.js +254 -0
- package/src/formats/n8n.js +276 -0
- package/src/formats/node-red.js +363 -0
- package/src/formats/registry.js +177 -0
- package/src/formats/rivet.js +259 -0
- package/src/formats/shared.js +98 -0
- package/src/formats/structure.js +182 -0
- package/src/formats/yaml.js +127 -0
- package/src/frame/frame.js +962 -0
- package/src/frame/vendor.js +1484 -0
- package/src/gate/audit.js +116 -0
- package/src/gate/gate.js +365 -0
- package/src/gate/policy.js +295 -0
- package/src/gate/scripted.js +110 -0
- package/src/gate/seams.js +32 -0
- package/src/gate/store.js +66 -0
- package/src/geometry/model.js +533 -0
- package/src/grant-key.js +149 -0
- package/src/graph/model.js +381 -0
- package/src/headless/a2a-push.js +123 -0
- package/src/headless/channels/config.js +142 -0
- package/src/headless/channels/connectors.js +283 -0
- package/src/headless/channels/index.js +257 -0
- package/src/headless/channels/mail.js +197 -0
- package/src/headless/channels/mailnet.js +223 -0
- package/src/headless/channels/platforms.js +194 -0
- package/src/headless/channels/replay.js +49 -0
- package/src/headless/cli.js +1397 -0
- package/src/headless/companion-client.js +182 -0
- package/src/headless/cost-cli.js +177 -0
- package/src/headless/deprecations-cli.js +102 -0
- package/src/headless/examples.js +48 -0
- package/src/headless/ffmpeg-exec.js +217 -0
- package/src/headless/gateways/config.js +213 -0
- package/src/headless/gateways/homeassistant.js +159 -0
- package/src/headless/gateways/index.js +121 -0
- package/src/headless/gateways/matter.js +150 -0
- package/src/headless/gateways/mqtt-client.js +177 -0
- package/src/headless/gateways/mqtt.js +70 -0
- package/src/headless/gateways/netway.js +226 -0
- package/src/headless/gateways/serverpod-worker.mjs +16 -0
- package/src/headless/gateways/serverpod.js +71 -0
- package/src/headless/gateways/util.js +27 -0
- package/src/headless/grant-cli.js +208 -0
- package/src/headless/guard.js +125 -0
- package/src/headless/guards.js +124 -0
- package/src/headless/machine-cli.js +853 -0
- package/src/headless/mcp-endpoint.js +287 -0
- package/src/headless/mcp-roots.js +38 -0
- package/src/headless/model-endpoint.js +88 -0
- package/src/headless/net.js +151 -0
- package/src/headless/node.js +263 -0
- package/src/headless/npm-tarball.js +29 -0
- package/src/headless/operator-page.js +112 -0
- package/src/headless/operator.js +242 -0
- package/src/headless/people.js +34 -0
- package/src/headless/pkg-cli.js +1103 -0
- package/src/headless/pkg-serve.js +138 -0
- package/src/headless/process-worker.js +88 -0
- package/src/headless/runner-remote.js +477 -0
- package/src/headless/sandbox-child.mjs +21 -0
- package/src/headless/scheduled.js +557 -0
- package/src/headless/serve-stream.js +97 -0
- package/src/headless/serve.js +2238 -0
- package/src/headless/short-links.js +158 -0
- package/src/headless/static-dirs.js +207 -0
- package/src/headless/trust-file.js +51 -0
- package/src/headless/watch.js +291 -0
- package/src/headless/ws-connect.js +65 -0
- package/src/headless/ws-connections.js +44 -0
- package/src/headless/ws-echo.js +150 -0
- package/src/headless/ws-proxy.js +354 -0
- package/src/headless/ws-ticket.js +48 -0
- package/src/hooks/config.js +50 -0
- package/src/hooks/engine.js +189 -0
- package/src/input/log.js +613 -0
- package/src/input/player.js +217 -0
- package/src/input/replay.js +209 -0
- package/src/jth/vendor.js +28 -0
- package/src/ledger/tx.js +94 -0
- package/src/machine/apply.js +193 -0
- package/src/machine/diff.js +168 -0
- package/src/machine/drive.js +296 -0
- package/src/machine/engine.js +478 -0
- package/src/machine/files-tool.js +76 -0
- package/src/machine/lint.js +139 -0
- package/src/machine/llmtm-trace.js +117 -0
- package/src/machine/llmtm.js +475 -0
- package/src/machine/otel.js +80 -0
- package/src/machine/pane-provider.js +51 -0
- package/src/machine/propose.js +159 -0
- package/src/machine/random.js +52 -0
- package/src/machine/risk.js +107 -0
- package/src/machine/session.js +1017 -0
- package/src/machine/shell-tool.js +45 -0
- package/src/machine/tape.js +349 -0
- package/src/machine/templates.js +104 -0
- package/src/machine/trace.js +664 -0
- package/src/machine/view.js +73 -0
- package/src/machine/vm-tool.js +47 -0
- package/src/machine/watchdog.js +46 -0
- package/src/mcp/app-args.js +51 -0
- package/src/mcp/app-run.js +56 -0
- package/src/mcp/apps.js +468 -0
- package/src/mcp/cache.js +232 -0
- package/src/mcp/client.js +507 -0
- package/src/mcp/demo-app.js +183 -0
- package/src/mcp/demo-server.js +602 -0
- package/src/mcp/elicitation.js +116 -0
- package/src/mcp/hub.js +1326 -0
- package/src/mcp/roots.js +139 -0
- package/src/mcp/sampling.js +105 -0
- package/src/mcp/settings.js +125 -0
- package/src/mcp/tape.js +292 -0
- package/src/mcp/uri-template.js +156 -0
- package/src/mcp/wsh-client.js +164 -0
- package/src/media/animated-codec.js +7 -0
- package/src/media/animated.js +623 -0
- package/src/media/compare-agent.js +70 -0
- package/src/media/compare-job.js +63 -0
- package/src/media/compare-page.js +296 -0
- package/src/media/compare-worker.js +14 -0
- package/src/media/compare.js +600 -0
- package/src/media/contract.js +62 -0
- package/src/media/decode-frame.js +57 -0
- package/src/media/decode-pool.js +188 -0
- package/src/media/ffmpeg-edit.js +344 -0
- package/src/media/ffmpeg-wasm-client.js +121 -0
- package/src/media/ffmpeg-wasm-session.js +79 -0
- package/src/media/ffmpeg-wasm.js +139 -0
- package/src/media/ffmpeg-worker.js +23 -0
- package/src/media/ffmpeg.js +903 -0
- package/src/media/helpers.js +419 -0
- package/src/media/keep.js +108 -0
- package/src/media/metadata-worker.js +16 -0
- package/src/media/metadata.js +2518 -0
- package/src/media/mime.js +102 -0
- package/src/media/page-decode.js +74 -0
- package/src/media/persist.js +114 -0
- package/src/media/png.js +226 -0
- package/src/media/sample-input.js +67 -0
- package/src/media/store.js +86 -0
- package/src/media/wav.js +109 -0
- package/src/midi/mappings.js +250 -0
- package/src/midi/message.js +125 -0
- package/src/model/action-hub.js +143 -0
- package/src/model/adapters.js +284 -0
- package/src/model/agent-runs.js +12 -0
- package/src/model/arrange.js +308 -0
- package/src/model/automation-clock.js +494 -0
- package/src/model/automation.js +653 -0
- package/src/model/batches.js +78 -0
- package/src/model/binary-view.js +195 -0
- package/src/model/breakpoints.js +392 -0
- package/src/model/calc-settings.js +65 -0
- package/src/model/calendar.js +254 -0
- package/src/model/canvas-id.js +5 -0
- package/src/model/canvas.js +5048 -0
- package/src/model/channel-names.js +29 -0
- package/src/model/channels.js +692 -0
- package/src/model/chart-clock.js +631 -0
- package/src/model/chart-code.js +69 -0
- package/src/model/codecs.js +60 -0
- package/src/model/companion-access.js +271 -0
- package/src/model/companion-pane.js +50 -0
- package/src/model/composite.js +324 -0
- package/src/model/context.js +380 -0
- package/src/model/controls.js +231 -0
- package/src/model/converter-presets.js +368 -0
- package/src/model/cost.js +580 -0
- package/src/model/curve-transform.js +122 -0
- package/src/model/cycles.js +176 -0
- package/src/model/debug-log.js +168 -0
- package/src/model/decision-clock.js +100 -0
- package/src/model/decisions.js +265 -0
- package/src/model/desk.js +87 -0
- package/src/model/device-hub.js +400 -0
- package/src/model/diff.js +445 -0
- package/src/model/emit-events.js +179 -0
- package/src/model/errors.js +238 -0
- package/src/model/event-presets.js +297 -0
- package/src/model/events.js +1303 -0
- package/src/model/explicit-setters.js +139 -0
- package/src/model/field-mapping.js +623 -0
- package/src/model/files-pane.js +263 -0
- package/src/model/flow-idle.js +89 -0
- package/src/model/foreach.js +549 -0
- package/src/model/frame-loop.js +71 -0
- package/src/model/frame-pressure.js +180 -0
- package/src/model/function-scaffold.js +1000 -0
- package/src/model/geometry-settings.js +54 -0
- package/src/model/goals.js +176 -0
- package/src/model/graph-settings.js +76 -0
- package/src/model/grid-widget.js +175 -0
- package/src/model/groups.js +332 -0
- package/src/model/hash.js +21 -0
- package/src/model/history.js +259 -0
- package/src/model/http-copy.js +88 -0
- package/src/model/http-send-companion.js +110 -0
- package/src/model/http-send.js +163 -0
- package/src/model/http-tap.js +521 -0
- package/src/model/http-tools.js +1138 -0
- package/src/model/import-lock.js +78 -0
- package/src/model/imports.js +258 -0
- package/src/model/infer-outputs.js +822 -0
- package/src/model/ink-connect.js +150 -0
- package/src/model/ink-diff.js +75 -0
- package/src/model/ink-gone.js +71 -0
- package/src/model/ink-route.js +120 -0
- package/src/model/ink-svg.js +206 -0
- package/src/model/ink.js +739 -0
- package/src/model/input-log-hub.js +456 -0
- package/src/model/input-split.js +69 -0
- package/src/model/json-schema-host.js +9 -0
- package/src/model/json-schema.js +368 -0
- package/src/model/jth.js +78 -0
- package/src/model/latex-expr.js +528 -0
- package/src/model/layout-history.js +120 -0
- package/src/model/lazy-map.js +88 -0
- package/src/model/ledger-pane.js +101 -0
- package/src/model/legacy-viaframe.js +90 -0
- package/src/model/lessons.js +448 -0
- package/src/model/links.js +981 -0
- package/src/model/live.js +411 -0
- package/src/model/math-widget.js +53 -0
- package/src/model/memory-settings.js +33 -0
- package/src/model/midi-controls.js +350 -0
- package/src/model/nl-time.js +408 -0
- package/src/model/notebook.js +137 -0
- package/src/model/pane-view-store.js +15 -0
- package/src/model/partial.js +115 -0
- package/src/model/pause-clock.js +53 -0
- package/src/model/peer-pane.js +32 -0
- package/src/model/persist-state.js +290 -0
- package/src/model/person-only.js +122 -0
- package/src/model/pin.js +209 -0
- package/src/model/plot-3d.js +213 -0
- package/src/model/plot-series.js +157 -0
- package/src/model/plot-settings.js +531 -0
- package/src/model/ports.js +312 -0
- package/src/model/presentation.js +222 -0
- package/src/model/preset-apply.js +316 -0
- package/src/model/preset-banks.js +335 -0
- package/src/model/preset-morph.js +263 -0
- package/src/model/preset-ops.js +205 -0
- package/src/model/preset-player.js +283 -0
- package/src/model/preset-states.js +172 -0
- package/src/model/profile-settings.js +109 -0
- package/src/model/promote.js +103 -0
- package/src/model/protected.js +10 -0
- package/src/model/proxy-log.js +230 -0
- package/src/model/proxy.js +367 -0
- package/src/model/quantity-widget.js +61 -0
- package/src/model/queue-book.js +359 -0
- package/src/model/quick-add.js +224 -0
- package/src/model/rate-limit.js +313 -0
- package/src/model/rate.js +95 -0
- package/src/model/recovery.js +216 -0
- package/src/model/rename-follow.js +113 -0
- package/src/model/repeater.js +1126 -0
- package/src/model/retention.js +664 -0
- package/src/model/route-edit.js +132 -0
- package/src/model/router.js +155 -0
- package/src/model/runaway.js +232 -0
- package/src/model/runlog.js +1033 -0
- package/src/model/runners/a2a.js +2 -0
- package/src/model/runners/acp.js +2 -0
- package/src/model/runners/actions.js +4 -0
- package/src/model/runners/agent.js +4 -0
- package/src/model/runners/audio.js +21 -0
- package/src/model/runners/automation.js +2 -0
- package/src/model/runners/calc.js +23 -0
- package/src/model/runners/catch.js +10 -0
- package/src/model/runners/codec.js +24 -0
- package/src/model/runners/companion-run.js +2 -0
- package/src/model/runners/device.js +7 -0
- package/src/model/runners/dom.js +13 -0
- package/src/model/runners/effects.js +2 -0
- package/src/model/runners/embed.js +11 -0
- package/src/model/runners/eval.js +2 -0
- package/src/model/runners/files.js +19 -0
- package/src/model/runners/geometry.js +23 -0
- package/src/model/runners/goals.js +18 -0
- package/src/model/runners/graph.js +23 -0
- package/src/model/runners/html.js +2 -0
- package/src/model/runners/http-proxy.js +185 -0
- package/src/model/runners/http-send.js +61 -0
- package/src/model/runners/http-static.js +83 -0
- package/src/model/runners/http-tap.js +26 -0
- package/src/model/runners/http.js +40 -0
- package/src/model/runners/import.js +2 -0
- package/src/model/runners/index.js +74 -0
- package/src/model/runners/input.js +14 -0
- package/src/model/runners/inputlog.js +7 -0
- package/src/model/runners/ledger.js +15 -0
- package/src/model/runners/machine.js +4 -0
- package/src/model/runners/mcp.js +2 -0
- package/src/model/runners/mcpapp.js +2 -0
- package/src/model/runners/media.js +18 -0
- package/src/model/runners/memory.js +28 -0
- package/src/model/runners/mesh-inspect.js +13 -0
- package/src/model/runners/peer.js +14 -0
- package/src/model/runners/plot.js +22 -0
- package/src/model/runners/render.js +14 -0
- package/src/model/runners/respond.js +2 -0
- package/src/model/runners/sample-soundfont.js +85 -0
- package/src/model/runners/sample.js +159 -0
- package/src/model/runners/scene.js +8 -0
- package/src/model/runners/score.js +24 -0
- package/src/model/runners/secret.js +10 -0
- package/src/model/runners/select.js +2 -0
- package/src/model/runners/shared.js +4 -0
- package/src/model/runners/shares.js +15 -0
- package/src/model/runners/shell.js +7 -0
- package/src/model/runners/skill.js +26 -0
- package/src/model/runners/state.js +2 -0
- package/src/model/runners/statechart.js +4 -0
- package/src/model/runners/subgraph.js +9 -0
- package/src/model/runners/tabs.js +25 -0
- package/src/model/runners/terminal.js +5 -0
- package/src/model/runners/text.js +4 -0
- package/src/model/runners/timer.js +7 -0
- package/src/model/runners/traffic.js +65 -0
- package/src/model/runners/trigger.js +2 -0
- package/src/model/runners/verdict.js +29 -0
- package/src/model/runners/vm.js +7 -0
- package/src/model/runners/wait.js +4 -0
- package/src/model/runners/ws-client.js +57 -0
- package/src/model/safe-regex.js +719 -0
- package/src/model/sample-settings.js +43 -0
- package/src/model/sample-source.js +47 -0
- package/src/model/saved-mapping.js +153 -0
- package/src/model/score-controllers.js +87 -0
- package/src/model/score-edit.js +399 -0
- package/src/model/score-instruments.js +300 -0
- package/src/model/score-library.js +407 -0
- package/src/model/score-markup.js +235 -0
- package/src/model/score-mix.js +248 -0
- package/src/model/score-patterns.js +276 -0
- package/src/model/score-selection.js +34 -0
- package/src/model/score-time.js +139 -0
- package/src/model/score-transforms.js +336 -0
- package/src/model/score-transport.js +192 -0
- package/src/model/selection.js +351 -0
- package/src/model/serial-controls.js +328 -0
- package/src/model/shares-pane.js +17 -0
- package/src/model/shell-pane.js +106 -0
- package/src/model/signature.js +1122 -0
- package/src/model/size-hints.js +106 -0
- package/src/model/size-ratio.js +12 -0
- package/src/model/skill-settings.js +23 -0
- package/src/model/snapping.js +466 -0
- package/src/model/split.js +375 -0
- package/src/model/spring-player.js +222 -0
- package/src/model/statecharts.js +1369 -0
- package/src/model/states-preset.js +208 -0
- package/src/model/static-route.js +100 -0
- package/src/model/step-words.js +45 -0
- package/src/model/subgraph.js +1042 -0
- package/src/model/switch.js +98 -0
- package/src/model/tabs-store.js +10 -0
- package/src/model/tensor-view.js +146 -0
- package/src/model/timer-clock.js +361 -0
- package/src/model/timers.js +422 -0
- package/src/model/timing.js +212 -0
- package/src/model/traffic-agent.js +111 -0
- package/src/model/traffic-store.js +20 -0
- package/src/model/traffic.js +665 -0
- package/src/model/transport.js +364 -0
- package/src/model/turns.js +86 -0
- package/src/model/types.js +425 -0
- package/src/model/value-path.js +138 -0
- package/src/model/versions.js +535 -0
- package/src/model/view-outputs.js +144 -0
- package/src/model/viewport-bookmarks.js +194 -0
- package/src/model/virtual-clock.js +85 -0
- package/src/model/vm-pane.js +258 -0
- package/src/model/wait-clock.js +715 -0
- package/src/model/wait-persist.js +248 -0
- package/src/model/waits.js +431 -0
- package/src/model/watch.js +80 -0
- package/src/model/webhook.js +496 -0
- package/src/model/when.js +48 -0
- package/src/model/widget-values.js +392 -0
- package/src/model/window-commands.js +278 -0
- package/src/model/windows.js +348 -0
- package/src/model/wire-inspect.js +343 -0
- package/src/model/wire-ops.js +324 -0
- package/src/model/wire-pipeline.js +378 -0
- package/src/model/wire-route.js +637 -0
- package/src/model/workspaces.js +122 -0
- package/src/model/ws-client.js +155 -0
- package/src/model/ws-demo.js +31 -0
- package/src/model/ws-frames.js +141 -0
- package/src/model/ws-hub.js +219 -0
- package/src/model/ws-served.js +13 -0
- package/src/model/xstate.js +374 -0
- package/src/narrative/narrative.js +1557 -0
- package/src/network/auth.js +224 -0
- package/src/network/csp-frame.js +197 -0
- package/src/network/declarative-shadow.js +27 -0
- package/src/network/frame-net-shim.js +962 -0
- package/src/network/hosts.js +157 -0
- package/src/network/policy.js +520 -0
- package/src/network/rtc-shim.js +418 -0
- package/src/network/secret-gate.js +78 -0
- package/src/network/secrets.js +261 -0
- package/src/network/sockets.js +287 -0
- package/src/network/store.js +186 -0
- package/src/network/streams.js +99 -0
- package/src/packages/asset-values.js +50 -0
- package/src/packages/assets.js +99 -0
- package/src/packages/deprecation.js +203 -0
- package/src/packages/fetch.js +152 -0
- package/src/packages/function-canvas.js +73 -0
- package/src/packages/module.js +120 -0
- package/src/packages/package.js +474 -0
- package/src/packages/published.js +27 -0
- package/src/packages/requires.js +83 -0
- package/src/packages/signing.js +145 -0
- package/src/packages/trust.js +107 -0
- package/src/panes/a2a.js +51 -0
- package/src/panes/acp.js +60 -0
- package/src/panes/actions.js +54 -0
- package/src/panes/agent.js +83 -0
- package/src/panes/audio.js +103 -0
- package/src/panes/automation.js +112 -0
- package/src/panes/calc.js +67 -0
- package/src/panes/catch.js +52 -0
- package/src/panes/codec.js +59 -0
- package/src/panes/colors.js +12 -0
- package/src/panes/companion.js +54 -0
- package/src/panes/decision.js +65 -0
- package/src/panes/device.js +56 -0
- package/src/panes/dom.js +37 -0
- package/src/panes/effects.js +118 -0
- package/src/panes/embed.js +30 -0
- package/src/panes/eval.js +186 -0
- package/src/panes/fields.js +299 -0
- package/src/panes/files.js +45 -0
- package/src/panes/gate.js +70 -0
- package/src/panes/geometry.js +64 -0
- package/src/panes/goals.js +75 -0
- package/src/panes/graph.js +66 -0
- package/src/panes/html.js +54 -0
- package/src/panes/http.js +82 -0
- package/src/panes/import.js +38 -0
- package/src/panes/input.js +124 -0
- package/src/panes/inputlog.js +66 -0
- package/src/panes/ledger.js +56 -0
- package/src/panes/machine.js +138 -0
- package/src/panes/mcp.js +85 -0
- package/src/panes/mcpapp.js +64 -0
- package/src/panes/media.js +38 -0
- package/src/panes/memory.js +46 -0
- package/src/panes/mesh-inspect.js +49 -0
- package/src/panes/note.js +38 -0
- package/src/panes/peer.js +44 -0
- package/src/panes/plot.js +196 -0
- package/src/panes/registry.js +475 -0
- package/src/panes/render.js +87 -0
- package/src/panes/respond.js +44 -0
- package/src/panes/router.js +84 -0
- package/src/panes/sample.js +75 -0
- package/src/panes/scene.js +55 -0
- package/src/panes/score.js +87 -0
- package/src/panes/secret.js +38 -0
- package/src/panes/select.js +67 -0
- package/src/panes/shader.js +166 -0
- package/src/panes/shared.js +33 -0
- package/src/panes/shares.js +43 -0
- package/src/panes/shell.js +47 -0
- package/src/panes/skill.js +44 -0
- package/src/panes/state.js +91 -0
- package/src/panes/statechart.js +63 -0
- package/src/panes/story.js +33 -0
- package/src/panes/subgraph.js +94 -0
- package/src/panes/tabs.js +52 -0
- package/src/panes/terminal.js +57 -0
- package/src/panes/text.js +36 -0
- package/src/panes/timer.js +55 -0
- package/src/panes/traffic.js +59 -0
- package/src/panes/verdict.js +62 -0
- package/src/panes/vm.js +46 -0
- package/src/panes/wait.js +61 -0
- package/src/panes/watch.js +57 -0
- package/src/panes/webhook.js +67 -0
- package/src/panes/ws-client.js +67 -0
- package/src/persist.js +281 -0
- package/src/plot/tex.js +19 -0
- package/src/plot/vendor.js +59 -0
- package/src/plot/view-vendor.js +5 -0
- package/src/privacy/levels.js +108 -0
- package/src/protocol/devtools.js +116 -0
- package/src/protocol/inspector.js +368 -0
- package/src/render/time.js +381 -0
- package/src/rooms/mesh-metrics.js +162 -0
- package/src/run/companion.js +355 -0
- package/src/run/describe.js +114 -0
- package/src/run/local-model.js +150 -0
- package/src/run/monitor.js +208 -0
- package/src/run/publish.js +360 -0
- package/src/run/respond-outcome.js +12 -0
- package/src/run/run.js +820 -0
- package/src/run/runner-pairing.js +243 -0
- package/src/run/values.js +122 -0
- package/src/runtime/compile.js +122 -0
- package/src/runtime/console-tag.js +64 -0
- package/src/runtime/frame-signals.js +126 -0
- package/src/runtime/iteration-push.js +19 -0
- package/src/runtime/keys.js +474 -0
- package/src/runtime/midi-input.js +170 -0
- package/src/runtime/person-waits.js +140 -0
- package/src/runtime/resource-timing.js +56 -0
- package/src/runtime/runtime.js +2150 -0
- package/src/runtime/sandbox-modules.js +163 -0
- package/src/runtime/spring.js +246 -0
- package/src/runtime/strict.js +215 -0
- package/src/runtime/transfer.js +75 -0
- package/src/runtime/tween.js +158 -0
- package/src/runtime/wasm-fuel.js +596 -0
- package/src/runtime/worker/00-prelude.js +88 -0
- package/src/runtime/worker/10-helpers.js +295 -0
- package/src/runtime/worker/20-io.js +645 -0
- package/src/runtime/worker/30-previews.js +210 -0
- package/src/runtime/worker/40-profile-lineage.js +278 -0
- package/src/runtime/worker/50-own-worker.js +233 -0
- package/src/runtime/worker/60-timers.js +172 -0
- package/src/runtime/worker/70-state-keyed.js +193 -0
- package/src/runtime/worker/80-each-compile-generators.js +619 -0
- package/src/runtime/worker/90-events-operators.js +655 -0
- package/src/runtime/worker/95-agents-batches.js +1820 -0
- package/src/runtime/worker/96-plot.js +47 -0
- package/src/runtime/worker/97-calc.js +28 -0
- package/src/runtime/worker/98-geometry.js +24 -0
- package/src/runtime/worker/99-graph.js +30 -0
- package/src/runtime/worker/99-shell.js +30 -0
- package/src/safety/config.js +57 -0
- package/src/safety/engine.js +324 -0
- package/src/scene/pixi-scene.js +806 -0
- package/src/scene/scene-assets.js +241 -0
- package/src/scene/scene-edit.js +319 -0
- package/src/secret/settings.js +41 -0
- package/src/security/multipart.js +257 -0
- package/src/security/redact.js +261 -0
- package/src/serial/broker.js +473 -0
- package/src/serial/config.js +185 -0
- package/src/serial/framers.js +389 -0
- package/src/serial/packets.js +64 -0
- package/src/shader/audio-texture.js +87 -0
- package/src/shader/channels.js +496 -0
- package/src/shader/common.js +90 -0
- package/src/shader/model.js +867 -0
- package/src/shader/passes.js +150 -0
- package/src/shader/shadertoy.js +198 -0
- package/src/shell/hub.js +287 -0
- package/src/sim/checkpoints.js +850 -0
- package/src/sim/compile.js +37 -0
- package/src/sim/fixed.js +377 -0
- package/src/sim/headless.js +205 -0
- package/src/sim/physics.js +399 -0
- package/src/smf-helper/vendor.js +2 -0
- package/src/sources/config.js +435 -0
- package/src/storage.js +153 -0
- package/src/table/table-geo.js +2344 -0
- package/src/table/table-lineage.js +808 -0
- package/src/table/table-quality.js +355 -0
- package/src/table/table-schema.js +392 -0
- package/src/table/table-sql.js +1650 -0
- package/src/table/table-stats.js +480 -0
- package/src/table/table.js +2102 -0
- package/src/tensor/tensor.js +1153 -0
- package/src/terminal/hub.js +715 -0
- package/src/terminal/screen.js +516 -0
- package/src/tests/capture.js +88 -0
- package/src/tests/compare.js +480 -0
- package/src/tests/dataset.js +170 -0
- package/src/tests/evaluate.js +286 -0
- package/src/tests/from-runs.js +99 -0
- package/src/tests/judge.js +134 -0
- package/src/tests/matchers.js +263 -0
- package/src/tests/matrix-spec.js +87 -0
- package/src/tests/matrix.js +397 -0
- package/src/tests/metrics.js +369 -0
- package/src/tests/no-value.js +42 -0
- package/src/tests/pane-check.js +45 -0
- package/src/tests/report.js +124 -0
- package/src/tests/reporters.js +140 -0
- package/src/tests/run.js +155 -0
- package/src/tilemap/tilemap.js +1122 -0
- package/src/units/units.js +3 -0
- package/src/util/bytes-text.js +21 -0
- package/src/util/num.js +10 -0
- package/src/util/regexp.js +2 -0
- package/src/util/text.js +38 -0
- package/src/verdict/engine.js +154 -0
- package/src/vm/guest-wsh.js +108 -0
- package/src/vm/hub.js +457 -0
- package/src/vm/image-files.js +4 -0
- package/src/vm/image.js +33 -0
- package/src/vm/image.lock.json +13 -0
- package/src/vm/link.js +266 -0
- package/src/vm/mount.js +212 -0
- package/src/vm/net.js +85 -0
- package/src/vm/pipe-ws.js +187 -0
- package/src/vm/v86-loader.js +11 -0
- package/src/webmcp/args.js +212 -0
- package/src/webmcp/hidden.js +218 -0
- package/src/webmcp/text.js +250 -0
- package/src/webmcp/tools/bookmark-tools.js +106 -0
- package/src/webmcp/tools/breakpoint-tools.js +48 -0
- package/src/webmcp/tools/canvases.js +208 -0
- package/src/webmcp/tools/context-tools.js +84 -0
- package/src/webmcp/tools/core.js +170 -0
- package/src/webmcp/tools/devices.js +41 -0
- package/src/webmcp/tools/edit.js +357 -0
- package/src/webmcp/tools/event-tools.js +145 -0
- package/src/webmcp/tools/image-tools.js +173 -0
- package/src/webmcp/tools/ink-tools.js +247 -0
- package/src/webmcp/tools/input-log-tools.js +163 -0
- package/src/webmcp/tools/live.js +166 -0
- package/src/webmcp/tools/machine-tools.js +74 -0
- package/src/webmcp/tools/media.js +161 -0
- package/src/webmcp/tools/midi-tools.js +32 -0
- package/src/webmcp/tools/preset-tools.js +184 -0
- package/src/webmcp/tools/quick-add-tools.js +95 -0
- package/src/webmcp/tools/reshape.js +152 -0
- package/src/webmcp/tools/run-tools.js +309 -0
- package/src/webmcp/tools/runs.js +101 -0
- package/src/webmcp/tools/scaffold-tools.js +161 -0
- package/src/webmcp/tools/serial-tools.js +36 -0
- package/src/webmcp/tools/shared.js +338 -0
- package/src/webmcp/tools/subgraphs.js +360 -0
- package/src/webmcp/tools/tests.js +170 -0
- package/src/webmcp/tools/traffic-tools.js +70 -0
- package/src/webmcp/tools/transport-tools.js +65 -0
- package/src/webmcp/tools/version-tools.js +126 -0
- package/src/webmcp/tools/versions.js +11 -0
- package/src/webmcp/tools/wire-tools.js +90 -0
- package/src/webmcp/tools/workspace-tools.js +93 -0
- package/src/webmcp/tools.js +1323 -0
- package/src/wsh/connect.js +347 -0
- package/src/wsh/demo-host.js +361 -0
- package/src/wsh/e2e.js +27 -0
- package/src/wsh/keys.js +154 -0
- package/src/wsh/known-hosts.js +39 -0
- package/src/wsh/private-key.js +95 -0
- package/src/wsh/reverse.js +34 -0
- package/src/wsh/settings.js +118 -0
- package/src/wsh/transfer.js +62 -0
- package/src/wsh/vendor.js +5 -0
- package/src/x402/challenge.js +171 -0
- package/src/x402/demo-server.js +127 -0
- package/src/x402/pay.js +125 -0
- package/src/x402/retry.js +95 -0
- package/src/x402/store.js +76 -0
|
@@ -0,0 +1,623 @@
|
|
|
1
|
+
// Turning animated WebP and APNG upright: every frame is decoded, rotated or
|
|
2
|
+
// flipped, and written again, with its timing, its disposal and blend modes and
|
|
3
|
+
// the loop count kept. Offline: no codec library, no CDN.
|
|
4
|
+
//
|
|
5
|
+
// APNG is done entirely here: the frames are PNG streams (src/media/png.js
|
|
6
|
+
// decodes and encodes them with the platform's zlib streams). Animated WebP
|
|
7
|
+
// needs a decoder for its frames (VP8 or VP8L): the platform's own
|
|
8
|
+
// (`createImageBitmap`, where the page and the Workers have it), and a small
|
|
9
|
+
// lossless encoder written here (a VP8L writer: subtract-green transform, Huffman
|
|
10
|
+
// codes, runs of the left or upper pixel), so the frames come out lossless
|
|
11
|
+
// whatever they were before (the file can grow when a frame was lossy).
|
|
12
|
+
//
|
|
13
|
+
// No imports and no DOM at load time: like the other media modules it also runs
|
|
14
|
+
// as a sandbox module, so what it needs from its neighbours is handed to
|
|
15
|
+
// `createAnimated` (the orientation transform from metadata.js, the PNG codec
|
|
16
|
+
// from png.js). Metadata (EXIF, XMP) is not handled here: the caller puts it
|
|
17
|
+
// into the bytes this returns.
|
|
18
|
+
|
|
19
|
+
export const ANIMATED_LIMITS = {
|
|
20
|
+
frames: 2000, // the most frames turned
|
|
21
|
+
canvasPixels: 32 * 1024 * 1024, // the most pixels in the canvas
|
|
22
|
+
totalPixels: 400 * 1024 * 1024, // the most pixels over every frame
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const be32 = (b, at) => ((b[at] << 24) | (b[at + 1] << 16) | (b[at + 2] << 8) | b[at + 3]) >>> 0;
|
|
26
|
+
const le32 = (b, at) => (b[at] | (b[at + 1] << 8) | (b[at + 2] << 16) | (b[at + 3] << 24)) >>> 0;
|
|
27
|
+
const le24 = (b, at) => b[at] | (b[at + 1] << 8) | (b[at + 2] << 16);
|
|
28
|
+
const ascii = (b, at, n) => String.fromCharCode(...b.subarray(at, at + n));
|
|
29
|
+
const latin1 = (s) => Uint8Array.from(s, (c) => c.charCodeAt(0));
|
|
30
|
+
const join = (parts) => {
|
|
31
|
+
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
32
|
+
let at = 0;
|
|
33
|
+
for (const p of parts) (out.set(p, at), (at += p.length));
|
|
34
|
+
return out;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
class Refusal extends Error {
|
|
38
|
+
constructor(reason, message) {
|
|
39
|
+
super(message);
|
|
40
|
+
this.reason = reason;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// ── PNG chunks ───────────────────────────────────────────────────────────────
|
|
45
|
+
|
|
46
|
+
let crcTable;
|
|
47
|
+
function crc32(bytes) {
|
|
48
|
+
if (!crcTable) {
|
|
49
|
+
crcTable = new Uint32Array(256);
|
|
50
|
+
for (let n = 0; n < 256; n++) {
|
|
51
|
+
let c = n;
|
|
52
|
+
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
53
|
+
crcTable[n] = c >>> 0;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
let crc = 0xffffffff;
|
|
57
|
+
for (let i = 0; i < bytes.length; i++) crc = crcTable[(crc ^ bytes[i]) & 0xff] ^ (crc >>> 8);
|
|
58
|
+
return (crc ^ 0xffffffff) >>> 0;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function pngChunk(type, data) {
|
|
62
|
+
const out = new Uint8Array(12 + data.length);
|
|
63
|
+
const v = new DataView(out.buffer);
|
|
64
|
+
v.setUint32(0, data.length);
|
|
65
|
+
out.set(latin1(type), 4);
|
|
66
|
+
out.set(data, 8);
|
|
67
|
+
v.setUint32(8 + data.length, crc32(out.subarray(4, 8 + data.length)));
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const PNG_SIGNATURE = Uint8Array.from([137, 80, 78, 71, 13, 10, 26, 10]);
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* An APNG's structure: `{ width, height, ihdr, plte, trns, keep: [chunks before the image data worth carrying],
|
|
75
|
+
* actl: { frames, plays }, defaultData: [IDAT payloads], defaultIsFrame, frames: [{ x, y, width, height, delayNum, delayDen, dispose, blend, data: [payloads] }] }`.
|
|
76
|
+
* Throws a Refusal for what it can't read.
|
|
77
|
+
*/
|
|
78
|
+
export function parseApng(b) {
|
|
79
|
+
if (b.length < 8 || PNG_SIGNATURE.some((v, i) => b[i] !== v)) throw new Refusal("malformed", "not a PNG");
|
|
80
|
+
const out = { width: 0, height: 0, ihdr: null, plte: null, trns: null, keep: [], actl: null, defaultData: [], defaultIsFrame: false, frames: [] };
|
|
81
|
+
let at = 8;
|
|
82
|
+
let pending = null; // an fcTL waiting for its image data
|
|
83
|
+
let sawIdat = false;
|
|
84
|
+
let current = null;
|
|
85
|
+
while (at + 12 <= b.length) {
|
|
86
|
+
const len = be32(b, at);
|
|
87
|
+
const type = ascii(b, at + 4, 4);
|
|
88
|
+
const end = at + 12 + len;
|
|
89
|
+
if (len > 0x7fffffff || end > b.length) throw new Refusal("malformed", `the PNG is cut short in a ${type} chunk`);
|
|
90
|
+
const data = b.subarray(at + 8, at + 8 + len);
|
|
91
|
+
if (type === "IHDR" && len === 13) {
|
|
92
|
+
out.ihdr = data;
|
|
93
|
+
out.width = be32(data, 0);
|
|
94
|
+
out.height = be32(data, 4);
|
|
95
|
+
} else if (type === "PLTE") out.plte = data;
|
|
96
|
+
else if (type === "tRNS") out.trns = data;
|
|
97
|
+
else if (type === "acTL" && len === 8) out.actl = { frames: be32(data, 0), plays: be32(data, 4) };
|
|
98
|
+
else if (type === "fcTL" && len === 26) {
|
|
99
|
+
pending = { x: be32(data, 12), y: be32(data, 16), width: be32(data, 4), height: be32(data, 8), delayNum: (data[20] << 8) | data[21], delayDen: (data[22] << 8) | data[23], dispose: data[24], blend: data[25], data: [] };
|
|
100
|
+
if (!sawIdat) out.defaultIsFrame = true;
|
|
101
|
+
current = pending;
|
|
102
|
+
out.frames.push(pending);
|
|
103
|
+
} else if (type === "IDAT") {
|
|
104
|
+
sawIdat = true;
|
|
105
|
+
out.defaultData.push(data);
|
|
106
|
+
if (out.defaultIsFrame && out.frames.length === 1) out.frames[0].data.push(data);
|
|
107
|
+
} else if (type === "fdAT" && len >= 4) {
|
|
108
|
+
if (!current) throw new Refusal("malformed", "an fdAT chunk comes before any frame control chunk");
|
|
109
|
+
current.data.push(data.subarray(4));
|
|
110
|
+
} else if (!sawIdat && ["sRGB", "gAMA", "cHRM", "iCCP", "pHYs", "cICP", "sBIT"].includes(type)) out.keep.push({ type, data });
|
|
111
|
+
at = end;
|
|
112
|
+
if (type === "IEND") break;
|
|
113
|
+
}
|
|
114
|
+
if (!out.ihdr) throw new Refusal("malformed", "the PNG has no header");
|
|
115
|
+
if (!out.actl) throw new Refusal("malformed", "the PNG has no animation control chunk");
|
|
116
|
+
if (!out.frames.length) throw new Refusal("malformed", "the animation has no frames");
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The parts of an animated WebP: `{ width, height, flags, anim: { background, loops }, iccp, frames: [{ x, y, width, height, duration, dispose, blend, chunks: [{ type, data }] }] }`. */
|
|
121
|
+
export function parseAnimatedWebp(b) {
|
|
122
|
+
if (b.length < 20 || ascii(b, 0, 4) !== "RIFF" || ascii(b, 8, 4) !== "WEBP") throw new Refusal("malformed", "not a WebP");
|
|
123
|
+
const limit = Math.min(b.length, le32(b, 4) + 8);
|
|
124
|
+
const out = { width: 0, height: 0, flags: 0, anim: null, iccp: null, frames: [] };
|
|
125
|
+
let at = 12;
|
|
126
|
+
while (at + 8 <= limit) {
|
|
127
|
+
const type = ascii(b, at, 4);
|
|
128
|
+
const size = le32(b, at + 4);
|
|
129
|
+
if (at + 8 + size > limit) throw new Refusal("malformed", `the WebP is cut short in a ${JSON.stringify(type)} chunk`);
|
|
130
|
+
const data = b.subarray(at + 8, at + 8 + size);
|
|
131
|
+
if (type === "VP8X" && size >= 10) {
|
|
132
|
+
out.flags = data[0];
|
|
133
|
+
out.width = 1 + le24(data, 4);
|
|
134
|
+
out.height = 1 + le24(data, 7);
|
|
135
|
+
} else if (type === "ICCP") out.iccp = data;
|
|
136
|
+
else if (type === "ANIM" && size >= 6) out.anim = { background: data.subarray(0, 4), loops: data[4] | (data[5] << 8) };
|
|
137
|
+
else if (type === "ANMF" && size >= 16) {
|
|
138
|
+
const frame = { x: 2 * le24(data, 0), y: 2 * le24(data, 3), width: 1 + le24(data, 6), height: 1 + le24(data, 9), duration: le24(data, 12), dispose: data[15] & 1, blend: (data[15] >> 1) & 1, chunks: [] };
|
|
139
|
+
let p = 16;
|
|
140
|
+
while (p + 8 <= data.length) {
|
|
141
|
+
const t = ascii(data, p, 4);
|
|
142
|
+
const s = le32(data, p + 4);
|
|
143
|
+
if (p + 8 + s > data.length) throw new Refusal("malformed", `a frame is cut short in a ${JSON.stringify(t)} chunk`);
|
|
144
|
+
frame.chunks.push({ type: t, data: data.subarray(p + 8, p + 8 + s) });
|
|
145
|
+
p += 8 + s + (s & 1);
|
|
146
|
+
}
|
|
147
|
+
out.frames.push(frame);
|
|
148
|
+
}
|
|
149
|
+
at += 8 + size + (size & 1);
|
|
150
|
+
}
|
|
151
|
+
if (!out.width) throw new Refusal("malformed", "the WebP has no VP8X header");
|
|
152
|
+
if (!out.frames.length) throw new Refusal("malformed", "the animation has no frames");
|
|
153
|
+
return out;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ── VP8L (WebP lossless) writer ──────────────────────────────────────────────
|
|
157
|
+
|
|
158
|
+
class Bits {
|
|
159
|
+
constructor() {
|
|
160
|
+
this.bytes = [];
|
|
161
|
+
this.acc = 0;
|
|
162
|
+
this.n = 0;
|
|
163
|
+
}
|
|
164
|
+
put(value, bits) {
|
|
165
|
+
// bits <= 24
|
|
166
|
+
this.acc += (value & (2 ** bits - 1)) * 2 ** this.n;
|
|
167
|
+
this.n += bits;
|
|
168
|
+
while (this.n >= 8) {
|
|
169
|
+
this.bytes.push(this.acc % 256);
|
|
170
|
+
this.acc = Math.floor(this.acc / 256);
|
|
171
|
+
this.n -= 8;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/** A prefix code's bits, most significant first. */
|
|
175
|
+
code(code, len) {
|
|
176
|
+
for (let i = len - 1; i >= 0; i--) this.put((code >> i) & 1, 1);
|
|
177
|
+
}
|
|
178
|
+
finish() {
|
|
179
|
+
if (this.n) this.bytes.push(this.acc % 256);
|
|
180
|
+
return Uint8Array.from(this.bytes);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Huffman code lengths for `counts` (0 for an unused symbol), none longer than `max`. */
|
|
185
|
+
function codeLengths(counts, max) {
|
|
186
|
+
let c = Array.from(counts);
|
|
187
|
+
for (;;) {
|
|
188
|
+
const lens = new Array(c.length).fill(0);
|
|
189
|
+
let nodes = [];
|
|
190
|
+
c.forEach((w, s) => w > 0 && nodes.push({ w, s, kids: null }));
|
|
191
|
+
if (nodes.length === 1) lens[nodes[0].s] = 1;
|
|
192
|
+
while (nodes.length > 1) {
|
|
193
|
+
nodes.sort((x, y) => x.w - y.w || (x.s ?? 1e9) - (y.s ?? 1e9));
|
|
194
|
+
const [x, y, ...rest] = nodes;
|
|
195
|
+
nodes = [...rest, { w: x.w + y.w, s: undefined, kids: [x, y] }];
|
|
196
|
+
}
|
|
197
|
+
const walk = (n, d) => (n.kids ? n.kids.forEach((k) => walk(k, d + 1)) : (lens[n.s] = d || 1));
|
|
198
|
+
if (nodes[0]) walk(nodes[0], 0);
|
|
199
|
+
if (Math.max(0, ...lens) <= max) return lens;
|
|
200
|
+
c = c.map((w) => (w ? (w + 1) >> 1 : 0));
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Canonical codes for lengths: `[{ code, len }]` by symbol. */
|
|
205
|
+
function canonical(lens) {
|
|
206
|
+
const count = new Array(16).fill(0);
|
|
207
|
+
for (const l of lens) if (l) count[l]++;
|
|
208
|
+
const next = new Array(16).fill(0);
|
|
209
|
+
let code = 0;
|
|
210
|
+
for (let l = 1; l < 16; l++) {
|
|
211
|
+
code = (code + count[l - 1]) << 1;
|
|
212
|
+
next[l] = code;
|
|
213
|
+
}
|
|
214
|
+
return lens.map((l) => (l ? { code: next[l]++, len: l } : { code: 0, len: 0 }));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const CODE_LENGTH_ORDER = [17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
|
|
218
|
+
|
|
219
|
+
/** Write one prefix code for the alphabet's `counts`; returns the table to encode symbols with. */
|
|
220
|
+
function writePrefixCode(w, counts) {
|
|
221
|
+
const used = [];
|
|
222
|
+
counts.forEach((c, s) => c > 0 && used.push(s));
|
|
223
|
+
if (used.length === 0) used.push(0);
|
|
224
|
+
if (used.length <= 2 && used.every((s) => s < 256)) {
|
|
225
|
+
// a simple code: one or two symbols below 256
|
|
226
|
+
w.put(1, 1);
|
|
227
|
+
w.put(used.length - 1, 1);
|
|
228
|
+
const first = used[0];
|
|
229
|
+
if (first < 2) (w.put(0, 1), w.put(first, 1));
|
|
230
|
+
else (w.put(1, 1), w.put(first, 8));
|
|
231
|
+
if (used.length === 2) w.put(used[1], 8);
|
|
232
|
+
const table = counts.map(() => ({ code: 0, len: 0 }));
|
|
233
|
+
if (used.length === 2) {
|
|
234
|
+
table[used[0]] = { code: 0, len: 1 };
|
|
235
|
+
table[used[1]] = { code: 1, len: 1 };
|
|
236
|
+
}
|
|
237
|
+
return table;
|
|
238
|
+
}
|
|
239
|
+
const padded = used.length === 1 ? counts.map((c, s) => (c || s === (used[0] === 0 ? 1 : 0) ? c || 1 : 0)) : counts;
|
|
240
|
+
const lens = codeLengths(padded, 15);
|
|
241
|
+
// the lengths as code-length symbols: literals 0..15, 17 and 18 for runs of zeros
|
|
242
|
+
const tokens = [];
|
|
243
|
+
for (let i = 0; i < lens.length; ) {
|
|
244
|
+
if (lens[i] === 0) {
|
|
245
|
+
let j = i;
|
|
246
|
+
while (j < lens.length && lens[j] === 0) j++;
|
|
247
|
+
let run = j - i;
|
|
248
|
+
i = j;
|
|
249
|
+
while (run >= 11) {
|
|
250
|
+
const n = Math.min(run, 138);
|
|
251
|
+
tokens.push({ sym: 18, extra: n - 11, bits: 7 });
|
|
252
|
+
run -= n;
|
|
253
|
+
}
|
|
254
|
+
if (run >= 3) (tokens.push({ sym: 17, extra: run - 3, bits: 3 }), (run = 0));
|
|
255
|
+
for (; run > 0; run--) tokens.push({ sym: 0 });
|
|
256
|
+
} else tokens.push({ sym: lens[i++] });
|
|
257
|
+
}
|
|
258
|
+
const clCounts = new Array(19).fill(0);
|
|
259
|
+
for (const t of tokens) clCounts[t.sym]++;
|
|
260
|
+
if (clCounts.filter(Boolean).length < 2) clCounts[clCounts[0] ? 1 : 0] = 1;
|
|
261
|
+
const clLens = codeLengths(clCounts, 7);
|
|
262
|
+
const clCodes = canonical(clLens);
|
|
263
|
+
w.put(0, 1);
|
|
264
|
+
let last = 18;
|
|
265
|
+
while (last > 3 && clLens[CODE_LENGTH_ORDER[last]] === 0) last--;
|
|
266
|
+
w.put(last + 1 - 4, 4);
|
|
267
|
+
for (let i = 0; i <= last; i++) w.put(clLens[CODE_LENGTH_ORDER[i]], 3);
|
|
268
|
+
w.put(0, 1); // every token is read (no maximum)
|
|
269
|
+
for (const t of tokens) {
|
|
270
|
+
w.code(clCodes[t.sym].code, clCodes[t.sym].len);
|
|
271
|
+
if (t.bits) w.put(t.extra, t.bits);
|
|
272
|
+
}
|
|
273
|
+
return canonical(lens);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** A length or distance code value (1 and up) as a prefix symbol and its extra bits. */
|
|
277
|
+
function prefixOf(value) {
|
|
278
|
+
if (value <= 4) return { sym: value - 1, bits: 0, extra: 0 };
|
|
279
|
+
const v = value - 1;
|
|
280
|
+
const high = 31 - Math.clz32(v);
|
|
281
|
+
const second = (v >> (high - 1)) & 1;
|
|
282
|
+
const bits = high - 1;
|
|
283
|
+
return { sym: 2 * high + second, bits, extra: v & (2 ** bits - 1) };
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* The VP8L bitstream (the payload of a "VP8L" chunk) for `width`×`height` straight-alpha RGBA pixels, lossless:
|
|
288
|
+
* a subtract-green transform, no color cache, Huffman codes made from the pixels' own counts, and back-references only to
|
|
289
|
+
* the pixel on the left (runs) or the one above.
|
|
290
|
+
*/
|
|
291
|
+
export function encodeVp8l(rgba, width, height) {
|
|
292
|
+
if (!(width >= 1 && height >= 1 && width <= 16384 && height <= 16384)) throw new Refusal("unsupported", "a WebP frame is at most 16384 pixels each way");
|
|
293
|
+
const n = width * height;
|
|
294
|
+
const px = new Uint32Array(n); // A R G B, after subtracting green from red and blue
|
|
295
|
+
let alpha = false;
|
|
296
|
+
for (let i = 0; i < n; i++) {
|
|
297
|
+
const r = rgba[i * 4];
|
|
298
|
+
const g = rgba[i * 4 + 1];
|
|
299
|
+
const b = rgba[i * 4 + 2];
|
|
300
|
+
const a = rgba[i * 4 + 3];
|
|
301
|
+
if (a !== 255) alpha = true;
|
|
302
|
+
px[i] = ((a << 24) | (((r - g) & 255) << 16) | (g << 8) | ((b - g) & 255)) >>> 0;
|
|
303
|
+
}
|
|
304
|
+
// tokens: a literal pixel index, or a copy of `len` pixels from the left (code 2) or from above (code 1)
|
|
305
|
+
const tokens = [];
|
|
306
|
+
const hist = [new Array(280).fill(0), new Array(256).fill(0), new Array(256).fill(0), new Array(256).fill(0), new Array(40).fill(0)];
|
|
307
|
+
for (let i = 0; i < n; ) {
|
|
308
|
+
let len = 0;
|
|
309
|
+
let dist = 0;
|
|
310
|
+
if (i > 0) {
|
|
311
|
+
let j = i;
|
|
312
|
+
while (j < n && j - i < 4096 && px[j] === px[j - 1]) j++;
|
|
313
|
+
if (j - i > len) ((len = j - i), (dist = 2));
|
|
314
|
+
}
|
|
315
|
+
if (i >= width) {
|
|
316
|
+
let j = i;
|
|
317
|
+
while (j < n && j - i < 4096 && px[j] === px[j - width]) j++;
|
|
318
|
+
if (j - i > len) ((len = j - i), (dist = 1));
|
|
319
|
+
}
|
|
320
|
+
if (len >= 3) {
|
|
321
|
+
const lp = prefixOf(len);
|
|
322
|
+
const dp = prefixOf(dist);
|
|
323
|
+
tokens.push({ len, lp, dp });
|
|
324
|
+
hist[0][256 + lp.sym]++;
|
|
325
|
+
hist[4][dp.sym]++;
|
|
326
|
+
i += len;
|
|
327
|
+
} else {
|
|
328
|
+
const p = px[i];
|
|
329
|
+
tokens.push({ i });
|
|
330
|
+
hist[0][(p >> 8) & 255]++;
|
|
331
|
+
hist[1][(p >> 16) & 255]++;
|
|
332
|
+
hist[2][p & 255]++;
|
|
333
|
+
hist[3][p >>> 24]++;
|
|
334
|
+
i++;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const w = new Bits();
|
|
338
|
+
w.put(0x2f, 8);
|
|
339
|
+
w.put(width - 1, 14);
|
|
340
|
+
w.put(height - 1, 14);
|
|
341
|
+
w.put(alpha ? 1 : 0, 1);
|
|
342
|
+
w.put(0, 3);
|
|
343
|
+
w.put(1, 1); // a transform:
|
|
344
|
+
w.put(2, 2); // subtract green
|
|
345
|
+
w.put(0, 1); // no more transforms
|
|
346
|
+
w.put(0, 1); // no color cache
|
|
347
|
+
w.put(0, 1); // one set of prefix codes for the whole image
|
|
348
|
+
const tables = hist.map((h) => writePrefixCode(w, h));
|
|
349
|
+
const [G, R, B, A, D] = tables;
|
|
350
|
+
for (const t of tokens) {
|
|
351
|
+
if (t.lp) {
|
|
352
|
+
const l = G[256 + t.lp.sym];
|
|
353
|
+
w.code(l.code, l.len);
|
|
354
|
+
if (t.lp.bits) w.put(t.lp.extra, t.lp.bits);
|
|
355
|
+
const d = D[t.dp.sym];
|
|
356
|
+
w.code(d.code, d.len);
|
|
357
|
+
if (t.dp.bits) w.put(t.dp.extra, t.dp.bits);
|
|
358
|
+
} else {
|
|
359
|
+
const p = px[t.i];
|
|
360
|
+
for (const [table, v] of [[G, (p >> 8) & 255], [R, (p >> 16) & 255], [B, p & 255], [A, p >>> 24]]) w.code(table[v].code, table[v].len);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return w.finish();
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// ── RIFF ─────────────────────────────────────────────────────────────────────
|
|
367
|
+
|
|
368
|
+
function riffChunk(type, data) {
|
|
369
|
+
const out = new Uint8Array(8 + data.length + (data.length & 1));
|
|
370
|
+
out.set(latin1(type), 0);
|
|
371
|
+
new DataView(out.buffer).setUint32(4, data.length, true);
|
|
372
|
+
out.set(data, 8);
|
|
373
|
+
return out;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function riffFile(chunks) {
|
|
377
|
+
const inner = join(chunks);
|
|
378
|
+
const out = new Uint8Array(12 + inner.length);
|
|
379
|
+
out.set(latin1("RIFF"), 0);
|
|
380
|
+
new DataView(out.buffer).setUint32(4, 4 + inner.length, true);
|
|
381
|
+
out.set(latin1("WEBP"), 8);
|
|
382
|
+
out.set(inner, 12);
|
|
383
|
+
return out;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const put24 = (v) => Uint8Array.from([v & 255, (v >> 8) & 255, (v >> 16) & 255]);
|
|
387
|
+
|
|
388
|
+
/** One animation frame as a file of its own, for a decoder that reads stills. */
|
|
389
|
+
export function stillWebpOf(frame) {
|
|
390
|
+
const parts = frame.chunks.filter((c) => c.type === "ALPH" || c.type === "VP8 " || c.type === "VP8L");
|
|
391
|
+
if (!parts.some((c) => c.type === "VP8 " || c.type === "VP8L")) throw new Refusal("malformed", "a frame holds no image data");
|
|
392
|
+
const chunks = [];
|
|
393
|
+
if (parts.some((c) => c.type === "ALPH")) chunks.push(riffChunk("VP8X", join([Uint8Array.of(0x10, 0, 0, 0), put24(frame.width - 1), put24(frame.height - 1)])));
|
|
394
|
+
for (const c of parts) chunks.push(riffChunk(c.type, c.data));
|
|
395
|
+
return riffFile(chunks);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// ── turning ──────────────────────────────────────────────────────────────────
|
|
399
|
+
|
|
400
|
+
/** The rectangle `r` ({ x, y, width, height }) of a `W`×`H` canvas, after the orientation transform `t`. */
|
|
401
|
+
function movedRect(t, r) {
|
|
402
|
+
const at = (x, y) => [t.a * x + t.c * y + t.e, t.b * x + t.d * y + t.f];
|
|
403
|
+
const [x0, y0] = at(r.x, r.y);
|
|
404
|
+
const [x1, y1] = at(r.x + r.width, r.y + r.height);
|
|
405
|
+
return { x: Math.min(x0, x1), y: Math.min(y0, y1), width: Math.abs(x1 - x0), height: Math.abs(y1 - y0) };
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* `createAnimated({ orientTransform, orientPixels, decodePng, encodePng, decodeImage? })`:
|
|
410
|
+
* `decodeImage(bytes)` makes `{ data, width, height }` (straight RGBA) of a still WebP's bytes; the default uses `createImageBitmap`
|
|
411
|
+
* and `OffscreenCanvas`, with no color conversion.
|
|
412
|
+
* @returns {{ available: (format) => boolean, upright: (bytes, format, orientation) => Promise<{ bytes, width, height, frames, notes }> }}
|
|
413
|
+
*/
|
|
414
|
+
export function createAnimated(deps) {
|
|
415
|
+
const { orientTransform, orientPixels, decodePng, encodePng } = deps;
|
|
416
|
+
|
|
417
|
+
async function defaultDecode(bytes) {
|
|
418
|
+
if (typeof createImageBitmap !== "function" || typeof OffscreenCanvas !== "function") throw new Refusal("unsupported", "decoding the frames of an animated WebP needs createImageBitmap and OffscreenCanvas, which this runtime does not have");
|
|
419
|
+
let bitmap;
|
|
420
|
+
try {
|
|
421
|
+
bitmap = await createImageBitmap(new Blob([bytes], { type: "image/webp" }), { colorSpaceConversion: "none", premultiplyAlpha: "none", imageOrientation: "none" });
|
|
422
|
+
} catch {
|
|
423
|
+
throw new Refusal("unsupported", "this browser can't decode the frames of this WebP");
|
|
424
|
+
}
|
|
425
|
+
try {
|
|
426
|
+
const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
|
|
427
|
+
const g = canvas.getContext("2d", { willReadFrequently: true });
|
|
428
|
+
g.drawImage(bitmap, 0, 0);
|
|
429
|
+
const d = g.getImageData(0, 0, bitmap.width, bitmap.height);
|
|
430
|
+
return { data: d.data, width: d.width, height: d.height };
|
|
431
|
+
} finally {
|
|
432
|
+
bitmap.close?.();
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
const decodeImage = deps.decodeImage ?? defaultDecode;
|
|
436
|
+
|
|
437
|
+
function checkSize(width, height, count) {
|
|
438
|
+
if (width * height > ANIMATED_LIMITS.canvasPixels) throw new Refusal("too-large", `the canvas is ${width}×${height}; animated images up to ${ANIMATED_LIMITS.canvasPixels} pixels are turned`);
|
|
439
|
+
if (count > ANIMATED_LIMITS.frames) throw new Refusal("too-large", `the image has ${count} frames; up to ${ANIMATED_LIMITS.frames} are turned`);
|
|
440
|
+
if (width * height * count > ANIMATED_LIMITS.totalPixels) throw new Refusal("too-large", `the image has ${count} frames of ${width}×${height}; up to ${ANIMATED_LIMITS.totalPixels} pixels over all frames are turned`);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
async function uprightApng(b, o) {
|
|
444
|
+
const p = parseApng(b);
|
|
445
|
+
checkSize(p.width, p.height, p.frames.length);
|
|
446
|
+
if (p.ihdr[12]) throw new Refusal("unsupported", "this animated PNG is interlaced, which is not read; it is unchanged");
|
|
447
|
+
const t = orientTransform(o, p.width, p.height);
|
|
448
|
+
const decodeData = async (w, h, data) => {
|
|
449
|
+
const ihdr = Uint8Array.from(p.ihdr);
|
|
450
|
+
new DataView(ihdr.buffer).setUint32(0, w);
|
|
451
|
+
new DataView(ihdr.buffer).setUint32(4, h);
|
|
452
|
+
const png = join([PNG_SIGNATURE, pngChunk("IHDR", ihdr), ...(p.plte ? [pngChunk("PLTE", p.plte)] : []), ...(p.trns ? [pngChunk("tRNS", p.trns)] : []), ...data.map((d) => pngChunk("IDAT", d)), pngChunk("IEND", new Uint8Array())]);
|
|
453
|
+
try {
|
|
454
|
+
return await decodePng(png);
|
|
455
|
+
} catch (e) {
|
|
456
|
+
throw new Refusal("unsupported", `a frame can't be decoded (${String(e?.message ?? e).replace(/^decodePng: /, "")}); the image is unchanged`);
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
// a frame's pixels, turned, as the IDAT data of a PNG of its new size
|
|
460
|
+
const turned = async (w, h, data) => {
|
|
461
|
+
const src = await decodeData(w, h, data);
|
|
462
|
+
const r = orientPixels(src.data, src.width, src.height, o);
|
|
463
|
+
const encoded = await encodePng({ data: r.data, width: r.width, height: r.height });
|
|
464
|
+
const idat = [];
|
|
465
|
+
for (let at = 8; at + 12 <= encoded.length; ) {
|
|
466
|
+
const len = be32(encoded, at);
|
|
467
|
+
if (ascii(encoded, at + 4, 4) === "IDAT") idat.push(encoded.subarray(at + 8, at + 8 + len));
|
|
468
|
+
at += 12 + len;
|
|
469
|
+
}
|
|
470
|
+
return { width: r.width, height: r.height, idat: join(idat) };
|
|
471
|
+
};
|
|
472
|
+
const ihdr = new Uint8Array(13);
|
|
473
|
+
new DataView(ihdr.buffer).setUint32(0, t.width);
|
|
474
|
+
new DataView(ihdr.buffer).setUint32(4, t.height);
|
|
475
|
+
ihdr.set([8, 6, 0, 0, 0], 8);
|
|
476
|
+
const swap = o >= 5 && o <= 8;
|
|
477
|
+
const parts = [PNG_SIGNATURE, pngChunk("IHDR", ihdr)];
|
|
478
|
+
for (const c of p.keep) {
|
|
479
|
+
if (c.type === "sBIT") continue; // describes the source's samples
|
|
480
|
+
if (c.type === "pHYs" && swap && c.data.length === 9) {
|
|
481
|
+
const d = Uint8Array.from(c.data);
|
|
482
|
+
d.set(c.data.subarray(4, 8), 0);
|
|
483
|
+
d.set(c.data.subarray(0, 4), 4);
|
|
484
|
+
parts.push(pngChunk("pHYs", d));
|
|
485
|
+
} else parts.push(pngChunk(c.type, c.data));
|
|
486
|
+
}
|
|
487
|
+
const actl = new Uint8Array(8);
|
|
488
|
+
new DataView(actl.buffer).setUint32(0, p.actl.frames);
|
|
489
|
+
new DataView(actl.buffer).setUint32(4, p.actl.plays);
|
|
490
|
+
parts.push(pngChunk("acTL", actl));
|
|
491
|
+
let seq = 0;
|
|
492
|
+
const fctl = (f, r) => {
|
|
493
|
+
const d = new Uint8Array(26);
|
|
494
|
+
const v = new DataView(d.buffer);
|
|
495
|
+
v.setUint32(0, seq++);
|
|
496
|
+
v.setUint32(4, r.width);
|
|
497
|
+
v.setUint32(8, r.height);
|
|
498
|
+
v.setUint32(12, r.x);
|
|
499
|
+
v.setUint32(16, r.y);
|
|
500
|
+
v.setUint16(20, f.delayNum);
|
|
501
|
+
v.setUint16(22, f.delayDen);
|
|
502
|
+
d[24] = f.dispose;
|
|
503
|
+
d[25] = f.blend;
|
|
504
|
+
return pngChunk("fcTL", d);
|
|
505
|
+
};
|
|
506
|
+
const frames = [];
|
|
507
|
+
for (const f of p.frames) {
|
|
508
|
+
const r = movedRect(t, f);
|
|
509
|
+
const img = await turned(f.width, f.height, f.data);
|
|
510
|
+
frames.push({ f, r, img });
|
|
511
|
+
}
|
|
512
|
+
// the default image: the first frame, or a still of its own that no animation shows
|
|
513
|
+
if (p.defaultIsFrame) {
|
|
514
|
+
const { f, r, img } = frames[0];
|
|
515
|
+
parts.push(fctl(f, { ...r, width: img.width, height: img.height }), pngChunk("IDAT", img.idat));
|
|
516
|
+
} else {
|
|
517
|
+
const still = await turned(p.width, p.height, p.defaultData);
|
|
518
|
+
parts.push(pngChunk("IDAT", still.idat));
|
|
519
|
+
}
|
|
520
|
+
frames.forEach(({ f, r, img }, i) => {
|
|
521
|
+
if (i === 0 && p.defaultIsFrame) return;
|
|
522
|
+
parts.push(fctl(f, { ...r, width: img.width, height: img.height }));
|
|
523
|
+
const d = new Uint8Array(4 + img.idat.length);
|
|
524
|
+
new DataView(d.buffer).setUint32(0, seq++);
|
|
525
|
+
d.set(img.idat, 4);
|
|
526
|
+
parts.push(pngChunk("fdAT", d));
|
|
527
|
+
});
|
|
528
|
+
parts.push(pngChunk("IEND", new Uint8Array()));
|
|
529
|
+
return { bytes: join(parts), width: t.width, height: t.height, frames: p.frames.length, keptColor: p.keep.some((c) => c.type === "iCCP") };
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
async function uprightWebp(b, o) {
|
|
533
|
+
const p = parseAnimatedWebp(b);
|
|
534
|
+
checkSize(p.width, p.height, p.frames.length);
|
|
535
|
+
const t = orientTransform(o, p.width, p.height);
|
|
536
|
+
const W = t.width;
|
|
537
|
+
const H = t.height;
|
|
538
|
+
const canvas = new Uint8Array(W * H * 4); // the animation as drawn so far, in the turned picture
|
|
539
|
+
const out = [];
|
|
540
|
+
let anyAlpha = Boolean(p.flags & 0x10);
|
|
541
|
+
for (let i = 0; i < p.frames.length; i++) {
|
|
542
|
+
const f = p.frames[i];
|
|
543
|
+
const src = await decodeImage(stillWebpOf(f));
|
|
544
|
+
if (src.width !== f.width || src.height !== f.height) throw new Refusal("malformed", `frame ${i + 1} says ${f.width}×${f.height} but decodes to ${src.width}×${src.height}`);
|
|
545
|
+
const r = orientPixels(src.data, src.width, src.height, o);
|
|
546
|
+
const at = movedRect(t, f);
|
|
547
|
+
// a WebP frame starts at an even position: when the turned one doesn't, it grows by a pixel, which is invisible
|
|
548
|
+
// where the frame is blended over what is there (the pixel is transparent) or the canvas under it is transparent
|
|
549
|
+
let x = at.x;
|
|
550
|
+
let y = at.y;
|
|
551
|
+
let w = r.width;
|
|
552
|
+
let h = r.height;
|
|
553
|
+
let data = r.data;
|
|
554
|
+
const grow = (left, top) => {
|
|
555
|
+
const nw = w + (left ? 1 : 0);
|
|
556
|
+
const nh = h + (top ? 1 : 0);
|
|
557
|
+
const next = new Uint8ClampedArray(nw * nh * 4);
|
|
558
|
+
for (let yy = 0; yy < nh; yy++) {
|
|
559
|
+
for (let xx = 0; xx < nw; xx++) {
|
|
560
|
+
const sx = xx - (left ? 1 : 0);
|
|
561
|
+
const sy = yy - (top ? 1 : 0);
|
|
562
|
+
const to = (yy * nw + xx) * 4;
|
|
563
|
+
if (sx >= 0 && sy >= 0) next.set(data.subarray((sy * w + sx) * 4, (sy * w + sx) * 4 + 4), to);
|
|
564
|
+
else {
|
|
565
|
+
const c = ((y - (top ? 1 : 0) + yy) * W + (x - (left ? 1 : 0) + xx)) * 4;
|
|
566
|
+
if (canvas[c + 3] !== 0) throw new Refusal("unsupported", `turning frame ${i + 1} would need a frame to start on an odd pixel, which a WebP can't do without redrawing what is under it; the image is unchanged`);
|
|
567
|
+
if (f.blend === 1) next.set(canvas.subarray(c, c + 4), to);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
x -= left ? 1 : 0;
|
|
572
|
+
y -= top ? 1 : 0;
|
|
573
|
+
w = nw;
|
|
574
|
+
h = nh;
|
|
575
|
+
data = next;
|
|
576
|
+
};
|
|
577
|
+
if (x % 2 || y % 2) grow(x % 2 === 1, y % 2 === 1);
|
|
578
|
+
// draw the frame into the model of the canvas (its true rectangle), then dispose
|
|
579
|
+
const rect = { x: at.x, y: at.y, width: r.width, height: r.height };
|
|
580
|
+
for (let yy = 0; yy < rect.height; yy++) {
|
|
581
|
+
for (let xx = 0; xx < rect.width; xx++) {
|
|
582
|
+
const s = (yy * rect.width + xx) * 4;
|
|
583
|
+
const c = ((rect.y + yy) * W + rect.x + xx) * 4;
|
|
584
|
+
const a = r.data[s + 3];
|
|
585
|
+
if (f.blend === 1 || a === 255) canvas.set(r.data.subarray(s, s + 4), c);
|
|
586
|
+
else if (a > 0) {
|
|
587
|
+
const ca = canvas[c + 3];
|
|
588
|
+
const oa = a + (ca * (255 - a)) / 255;
|
|
589
|
+
for (let k = 0; k < 3; k++) canvas[c + k] = (r.data[s + k] * a + (canvas[c + k] * ca * (255 - a)) / 255) / oa;
|
|
590
|
+
canvas[c + 3] = oa;
|
|
591
|
+
}
|
|
592
|
+
if (a !== 255) anyAlpha = true;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
const pixels = encodeVp8l(data, w, h);
|
|
596
|
+
const frameHead = join([put24(x / 2), put24(y / 2), put24(w - 1), put24(h - 1), put24(f.duration), Uint8Array.of((f.blend << 1) | f.dispose)]);
|
|
597
|
+
out.push(riffChunk("ANMF", join([frameHead, riffChunk("VP8L", pixels)])));
|
|
598
|
+
if (f.dispose === 1) {
|
|
599
|
+
for (let yy = 0; yy < rect.height; yy++) canvas.fill(0, ((rect.y + yy) * W + rect.x) * 4, ((rect.y + yy) * W + rect.x + rect.width) * 4);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
const anim = p.anim ?? { background: new Uint8Array(4), loops: 0 };
|
|
603
|
+
const animChunk = join([anim.background, Uint8Array.of(anim.loops & 255, anim.loops >> 8)]);
|
|
604
|
+
const flags = 0x02 | (anyAlpha ? 0x10 : 0) | (p.iccp ? 0x20 : 0);
|
|
605
|
+
const bytes = riffFile([riffChunk("VP8X", join([Uint8Array.of(flags, 0, 0, 0), put24(W - 1), put24(H - 1)])), ...(p.iccp ? [riffChunk("ICCP", p.iccp)] : []), riffChunk("ANIM", animChunk), ...out]);
|
|
606
|
+
return { bytes, width: W, height: H, frames: p.frames.length, keptColor: Boolean(p.iccp) };
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
return {
|
|
610
|
+
/**
|
|
611
|
+
* Turn an animated `format` ("png" or "webp") upright for EXIF orientation `o` (2 to 8): `{ ok: true, bytes, width, height, frames, keptColor }`,
|
|
612
|
+
* or `{ ok: false, reason, message }`. The bytes carry no EXIF or XMP; the caller adds them.
|
|
613
|
+
*/
|
|
614
|
+
async upright(bytes, format, o) {
|
|
615
|
+
try {
|
|
616
|
+
return { ok: true, ...(format === "png" ? await uprightApng(bytes, o) : await uprightWebp(bytes, o)) };
|
|
617
|
+
} catch (e) {
|
|
618
|
+
if (e instanceof Refusal) return { ok: false, reason: e.reason, message: e.message };
|
|
619
|
+
throw e;
|
|
620
|
+
}
|
|
621
|
+
},
|
|
622
|
+
};
|
|
623
|
+
}
|