/** * DepthEstimationService — Browser-native depth estimation singleton. * * Uses Transformers.js with Depth Anything V2 Small (24.8M params) for * monocular depth estimation. Supports WebGPU (preferred), WASM, and CPU * backends with automatic detection. * * Model caching via IndexedDB ensures ~100MB model is downloaded only once. * Multiple HoloScript scenes share a single cached model instance. * * @see W.148: Browser-native depth estimation is production-ready * @see W.154: Browser ML model caching is mandatory for production * @see W.155: Depth-to-normal derivation in WGSL costs zero additional inference */ export type DepthBackend = 'webgpu' | 'wasm' | 'cpu'; export interface DepthEstimationConfig { /** Model ID on Hugging Face. Default: 'depth-anything/Depth-Anything-V2-Small-hf' */ modelId?: string; /** Preferred compute backend. Auto-detected if not specified. */ backend?: DepthBackend; /** Maximum resolution for depth inference (width or height). Default: 512 */ maxResolution?: number; /** Enable IndexedDB model caching. Default: true */ enableCache?: boolean; /** Progress callback during model download (0-1) */ onProgress?: (progress: number) => void; } export interface DepthResult { /** Depth map as Float32Array (0=near, 1=far), row-major */ depthMap: Float32Array; /** Normal map derived via Sobel filter on depth (RGB Float32, row-major) */ normalMap: Float32Array; /** Width of the output maps */ width: number; /** Height of the output maps */ height: number; /** Which backend was actually used */ backend: DepthBackend; /** Inference time in milliseconds */ inferenceMs: number; } export interface DepthSequenceConfig extends DepthEstimationConfig { /** Temporal smoothing alpha (0-1). Default: 0.8 (80% new, 20% history) */ temporalAlpha?: number; } export declare function detectBestBackend(): Promise; /** * Derive normal map from depth map using Sobel filter. * Zero additional inference cost — runs as pure computation on depth data. * * @see W.155: Depth-to-normal derivation costs zero additional inference */ export declare function depthToNormalMap(depthMap: Float32Array, width: number, height: number): Float32Array; export declare class ModelCache { private db; open(): Promise; get(key: string): Promise; set(key: string, value: ArrayBuffer): Promise; has(key: string): Promise; close(): void; } /** * Temporal smoothing for frame-to-frame depth consistency. * Uses exponential moving average (EMA) to eliminate flickering * in per-frame depth estimation. * * @see W.150: GIF temporal coherence requires EMA smoothing * @see P.150.01: Temporal Depth Smoothing pattern */ export declare class TemporalSmoother { private previousDepth; private readonly alpha; constructor(alpha?: number); /** * Smooth the current depth map against the previous frame. * Returns a new smoothed array (does not mutate input). */ smooth(currentDepth: Float32Array): Float32Array; reset(): void; } /** * GIF disposal methods that affect frame composition. * @see G.149.01: GIF Disposal Methods Break Frame Extraction */ export declare enum GIFDisposalMethod { Unspecified = 0, DoNotDispose = 1, RestoreBackground = 2, RestorePrevious = 3 } export interface GIFFrame { /** Full RGBA pixel data (composited, not raw) */ data: Uint8ClampedArray; /** Frame width */ width: number; /** Frame height */ height: number; /** Display delay in milliseconds */ delayMs: number; /** Frame index */ index: number; } export interface GIFDecomposerConfig { /** Maximum number of frames to extract. Default: 500 */ maxFrames?: number; /** Target width for resizing (preserves aspect ratio). Default: original size */ targetWidth?: number; } /** * GIF frame decomposer that correctly handles all 4 disposal methods. * Composites partial frames into full RGBA canvases. * * Uses OffscreenCanvas where available, falls back to a pixel-level compositor. * For browser use, expects gifuct-js or similar library to provide raw frame data. * * @see G.149.01: Never assume GIF frames are independent full images */ export declare class GIFDecomposer { private readonly config; constructor(config?: GIFDecomposerConfig); /** * Decompose raw GIF frame data into full composited RGBA frames. * Each frame in the input array should have: data (Uint8ClampedArray), * width, height, left, top, disposalMethod, delayMs. */ decompose(rawFrames: Array<{ data: Uint8ClampedArray; width: number; height: number; left: number; top: number; disposalMethod: number; delayMs: number; }>, gifWidth: number, gifHeight: number): GIFFrame[]; } /** * Singleton service for browser-native depth estimation. * * Manages model lifecycle, caching, and inference. All HoloScript scenes * share a single instance to avoid redundant model downloads. * * Usage: * ```typescript * const service = DepthEstimationService.getInstance(); * await service.initialize({ onProgress: p => console.log(`${p*100}%`) }); * const result = await service.estimateDepth(imageData); * ``` */ export declare class DepthEstimationService { private static instance; private pipeline; private RawImage; private config; private modelCache; private _initialized; private _initializing; private _backend; private constructor(); static getInstance(config?: DepthEstimationConfig): DepthEstimationService; static resetInstance(): void; get initialized(): boolean; get backend(): DepthBackend; /** * Initialize the depth estimation pipeline. * Downloads model on first call, loads from IndexedDB cache on subsequent calls. * Safe to call multiple times — returns cached promise if already initializing. */ initialize(config?: Partial): Promise; private _doInitialize; /** * Estimate depth from a single image. * Returns both depth map and derived normal map. * * @param imageData - ImageData, HTMLImageElement, HTMLCanvasElement, or image URL */ estimateDepth(imageData: ImageData | { width: number; height: number; data: Uint8ClampedArray; }): Promise; /** * Estimate depth for a sequence of frames with temporal smoothing. * Maintains coherence across frames via EMA filter. * * @see P.150.01: Temporal Depth Smoothing pattern */ estimateDepthSequence(frames: Array<{ width: number; height: number; data: Uint8ClampedArray; }>, config?: DepthSequenceConfig): Promise; /** * Run real Depth Anything V2 inference via Transformers.js pipeline. * Converts RGBA pixel data to a RawImage, runs the pipeline, and * extracts the depth tensor as a normalized Float32Array. */ private _runPipelineInference; /** * Fallback depth generation using luminance as a proxy. * Used when Transformers.js is not installed or pipeline fails. */ private _generatePlaceholderDepth; dispose(): void; } //# sourceMappingURL=DepthEstimationService.d.ts.map