'use client'; import { createPasskeyAccount } from '@/app/lib/auth-client'; import { useEffect, useRef, useState } from 'react'; import styles from './studio.module.css'; import PrintPanel from './PrintPanel'; type Model = { id: string; label: string; available: boolean }; type StudioStatus = { signedIn: boolean; ready: boolean; models: Model[]; feedbackAvailable?: boolean; signupAvailable?: boolean; maxUploadBytes: number; maxPixels: number }; type Action = 'generate' | 'revise' | 'feedback'; const INITIAL = { brightness: 100, contrast: 100, saturation: 100, rotate: 0 }; const MAX_PIXELS = 20_000_000; const MAX_FILE = 10_000_000; const RATIOS = { original: 0, square: 1, portrait: 0.75, landscape: 4 / 3 }; type Ratio = keyof typeof RATIOS; function loadImage(blob: Blob): Promise { const path = URL.createObjectURL(blob); return new Promise((resolve, reject) => { const image = new Image(); image.onload = () => { URL.revokeObjectURL(path); resolve(image); }; image.onerror = () => { URL.revokeObjectURL(path); reject(new Error('That file could not be read as an image.')); }; image.src = path; }); } function exportBitmap(canvas: HTMLCanvasElement, type: string, quality?: number) { return new Promise((resolve, reject) => canvas.toBlob(blob => blob ? resolve(blob) : reject(new Error('Could not export this image.')), type, quality)); } export default function Studio() { const [status, setStatus] = useState(); const [model, setModel] = useState(''); const [mode, setMode] = useState<'edit' | 'ai' | 'print'>('edit'); const [source, setSource] = useState(); const [adjust, setAdjust] = useState(INITIAL); const [ratio, setRatio] = useState('original'); const [prompt, setPrompt] = useState(''); const [feedback, setFeedback] = useState(''); const [requestingLink, setRequestingLink] = useState(false); const [message, setMessage] = useState(''); const [busy, setBusy] = useState(null); const [pixels, setPixels] = useState({ width: 0, height: 0 }); const [prior, setPrior] = useState(); const canvas = useRef(null); const file = useRef(null); useEffect(() => { const controller = new AbortController(); fetch('/api/studio', { cache: 'no-store', signal: controller.signal }) .then(r => { if (!r.ok) throw new Error(); return r.json() as Promise; }) .then(data => { setStatus(data); setModel(data.models.find(item => item.available)?.id ?? ''); }).catch(() => {}); return () => controller.abort(); }, []); useEffect(() => { const output = canvas.current; if (!source || !output) return; const sideways = adjust.rotate % 180 !== 0; const w = sideways ? source.naturalHeight : source.naturalWidth; const h = sideways ? source.naturalWidth : source.naturalHeight; const r = RATIOS[ratio] || w / h; const cw = Math.min(w, h * r); const ch = cw / r; const scale = Math.min(1, 3200 / Math.max(cw, ch)); output.width = Math.max(1, Math.floor(cw * scale)); output.height = Math.max(1, Math.floor(ch * scale)); const context = output.getContext('2d'); if (!context) return; context.save(); context.translate(output.width / 2, output.height / 2); context.rotate((adjust.rotate * Math.PI) / 180); context.filter = `brightness(${adjust.brightness}%) contrast(${adjust.contrast}%) saturate(${adjust.saturation}%)`; context.drawImage(source, -(source.naturalWidth * scale) / 2, -(source.naturalHeight * scale) / 2, source.naturalWidth * scale, source.naturalHeight * scale); context.restore(); setPixels({ width: output.width, height: output.height }); }, [source, adjust, ratio]); function apply(next: HTMLImageElement, previous?: HTMLImageElement) { setSource(next); setPrior(previous); setAdjust(INITIAL); setRatio('original'); setFeedback(''); } async function upload(blob?: File) { if (!blob || busy) return; setMessage(''); try { if (!['image/jpeg', 'image/png', 'image/webp'].includes(blob.type)) throw new Error('Choose a JPEG, PNG, or WebP image.'); if (blob.size > MAX_FILE) throw new Error('Choose an image under 10 MB.'); const image = await loadImage(blob); if (image.naturalWidth * image.naturalHeight > MAX_PIXELS) throw new Error('Choose an image under 20 megapixels.'); apply(image); } catch (error) { setMessage((error as Error).message); } finally { if (file.current) file.current.value = ''; } } async function download() { if (!canvas.current || !source) return; try { const result = await exportBitmap(canvas.current, 'image/png'); const url = URL.createObjectURL(result); const link = document.createElement('a'); link.href = url; link.download = 'lumen-and-gesso.png'; link.click(); window.setTimeout(() => URL.revokeObjectURL(url), 1000); } catch (error) { setMessage((error as Error).message); } } async function emailLink() { setRequestingLink(true); setMessage(''); try { const problem = await createPasskeyAccount(); if (problem) setMessage(problem); else window.location.reload(); } catch (error) { setMessage((error as Error).message); } finally { setRequestingLink(false); } } async function run(action: Action) { if (busy || !status?.ready || !status.signedIn || (action !== 'feedback' && !model)) return; setBusy(action); setMessage(''); if (action === 'feedback') setFeedback(''); try { const form = new FormData(); form.set('action', action); if (action !== 'feedback') form.set('model', model); form.set('prompt', prompt.trim()); if (source && canvas.current) { const rendered = await exportBitmap(canvas.current, 'image/jpeg', 0.90); if (rendered.size > status.maxUploadBytes || pixels.width * pixels.height > status.maxPixels) { throw new Error('This image is too large for the selected AI tools. Try cropping it first.'); } form.set('image', rendered, 'studio-reference.jpg'); } const response = await fetch('/api/studio', { method: 'POST', body: form }); if (!response.ok) { const failure = await response.json().catch(() => ({})) as { error?: string }; throw new Error(failure.error || 'The model could not complete this request. Your image is unchanged.'); } if (action === 'feedback') { const result = await response.json() as { feedback: string }; setFeedback(result.feedback); } else { const current = await loadImage(await response.blob()); apply(current, source); setMessage('Image ready. Keep editing, ask for feedback, or download it.'); } } catch (error) { setMessage((error as Error).message); } finally { setBusy(null); } } return (

THE PORTRAIT STUDIO

Make it yours.

Upload a photo, edit it live, and download a clean image. No watermark added by Lumen & Gesso. No waiting for approval.

Canvas{source ? `${pixels.width} × ${pixels.height}` : 'No image yet'}
e.preventDefault()} onDrop={e => { e.preventDefault(); void upload(e.dataTransfer.files[0]); }}> {source ? :
Your picture goes here.

Drop a photo or open one. JPEG, PNG or WebP.

}
void upload(e.target.files?.[0])} />
{mode === 'edit' ? ( <>

Fine-tune the image

Adjustments show instantly and stay in this browser. Save the PNG before closing the page.

{(['brightness', 'contrast', 'saturation'] as const).map(key => )}
{prior && }
) : mode === 'print' ? : ( <>

Create, revise or get feedback

{!status?.ready ?

AI generation isn’t available right now. The upload, adjustment, and download tools still work for free.

: !status.signedIn ?

Sign in with your passkey to use the AI tools. Save any edits before you leave this page.

:

Choose a model. If you submit a photo, it goes to the model to process your request.

} {status?.signupAvailable && !status.signedIn && }