// Project files (.atelier): a small binary container holding a JSON manifest and // PNG-encoded layer, mask and selection bitmaps. // // magic "ATLR" | u32 version | u32 manifestLength | manifest (UTF-8 JSON) | blobs... // // Blob references in the manifest are { offset, length } relative to the blob section. import { ImageDocument } from '../core/document.js'; import { Layer } from '../core/layer.js'; import { Selection } from '../core/selection.js'; import { normalizeEffects } from '../render/effects.js'; import { canvasToBlob, blobToImage, createCanvas } from '../core/util.js'; const MAGIC = 'ATLR'; const VERSION = 1; export const PROJECT_EXT = '.atelier'; export async function saveProject(doc, extra = {}) { const blobs = []; let offset = 0; const addCanvas = async (canvas) => { const blob = await canvasToBlob(canvas, 'image/png'); const buf = new Uint8Array(await blob.arrayBuffer()); const ref = { offset, length: buf.length }; blobs.push(buf); offset += buf.length; return ref; }; const layers = []; for (const l of doc.layers) { layers.push({ id: l.id, name: l.name, type: l.type, visible: l.visible, opacity: l.opacity, blendMode: l.blendMode, locked: l.locked, alphaLock: l.alphaLock, clip: l.clip, maskEnabled: l.maskEnabled, text: l.text ? { ...l.text } : null, effects: l.effects || null, image: await addCanvas(l.canvas), mask: l.mask ? await addCanvas(l.mask) : null, }); } const manifest = { app: 'Atelier', version: VERSION, name: doc.name, width: doc.width, height: doc.height, activeLayerId: doc.activeLayerId, selection: doc.selection ? await addCanvas(doc.selection.mask) : null, guides: doc.guides, guideLines: doc.guideLines, layers, ...extra, savedAt: new Date().toISOString(), }; const json = new TextEncoder().encode(JSON.stringify(manifest)); const header = new ArrayBuffer(12); const hv = new DataView(header); for (let i = 0; i < 4; i++) hv.setUint8(i, MAGIC.charCodeAt(i)); hv.setUint32(4, VERSION, true); hv.setUint32(8, json.length, true); return new Blob([header, json, ...blobs], { type: 'application/x-atelier' }); } export async function isProjectFile(file) { if (file.name?.toLowerCase().endsWith(PROJECT_EXT)) return true; const head = new Uint8Array(await file.slice(0, 4).arrayBuffer()); return String.fromCharCode(...head) === MAGIC; } export async function loadProject(blob) { const buf = await blob.arrayBuffer(); const v = new DataView(buf); const magic = String.fromCharCode(v.getUint8(0), v.getUint8(1), v.getUint8(2), v.getUint8(3)); if (magic !== MAGIC) throw new Error('Not an Atelier project file'); const version = v.getUint32(4, true); if (version > VERSION) throw new Error(`Project was saved by a newer version (${version})`); const jsonLen = v.getUint32(8, true); const manifest = JSON.parse(new TextDecoder().decode(new Uint8Array(buf, 12, jsonLen))); const base = 12 + jsonLen; const readCanvas = async (ref, w, h) => { if (!ref) return null; const img = await blobToImage(new Blob([new Uint8Array(buf, base + ref.offset, ref.length)], { type: 'image/png' })); const c = createCanvas(w, h); c.getContext('2d').drawImage(img, 0, 0); return c; }; const { width: w, height: h } = manifest; const doc = new ImageDocument({ width: w, height: h, name: manifest.name || 'Untitled', empty: true }); for (const ld of manifest.layers) { const canvas = await readCanvas(ld.image, w, h); const layer = new Layer(w, h, { name: ld.name, type: ld.type, canvas }); layer.id = ld.id || layer.id; layer.visible = ld.visible ?? true; layer.opacity = ld.opacity ?? 1; layer.blendMode = ld.blendMode || 'normal'; layer.locked = !!ld.locked; layer.alphaLock = !!ld.alphaLock; layer.clip = !!ld.clip; layer.maskEnabled = ld.maskEnabled ?? true; layer.text = ld.text ? { ...ld.text } : null; layer.effects = ld.effects ? normalizeEffects(ld.effects) : null; layer.mask = await readCanvas(ld.mask, w, h); if (layer.isText && layer.text) layer.renderText(); doc.layers.push(layer); } if (!doc.layers.length) doc.layers.push(new Layer(w, h, { name: 'Layer 1' })); doc.activeLayerId = doc.layerById(manifest.activeLayerId) ? manifest.activeLayerId : doc.layers[doc.layers.length - 1].id; if (manifest.selection) doc.selection = Selection.orNull(await readCanvas(manifest.selection, w, h)); if (manifest.guides) doc.guides = { ...doc.guides, ...manifest.guides }; if (Array.isArray(manifest.guideLines)) doc.guideLines = manifest.guideLines.filter((g) => (g.axis === 'x' || g.axis === 'y') && Number.isFinite(g.pos)); return { doc, manifest }; }