interface DecodedImage { width: number; height: number; data: Uint8ClampedArray; } interface IHeicDecoder { /** * Initializes the decoder (e.g., loading WebAssembly module). */ initialize(): Promise; /** * Decodes HEIC binary data into raw RGBA pixel data. * @param data The HEIC file as a Uint8Array. * @param onProgress Optional progress callback that receives the progress percentage. */ decode(data: Uint8Array, onProgress?: (percent: number) => void): Promise; /** * Cleans up allocated resources. */ free(): void; } type ImageFormat = 'jpeg' | 'jpg' | 'png' | 'svg' | 'webp'; type HeicInput = Blob | File | ArrayBuffer | Uint8Array; interface ResizeOptions { /** * Maximum width in pixels. The image is downscaled to fit within this * bound while preserving the aspect ratio. Images smaller than the bound * are never upscaled. */ maxWidth?: number; /** * Maximum height in pixels. The image is downscaled to fit within this * bound while preserving the aspect ratio. Images smaller than the bound * are never upscaled. */ maxHeight?: number; /** * Uniform scale factor applied to both dimensions (e.g. 0.5 halves the * image). Takes precedence over `maxWidth` and `maxHeight` when set. */ scale?: number; } interface ConvertOptions extends ResizeOptions { /** * Target format for the conversion. * @default 'jpeg' */ to?: ImageFormat; /** * Quality of the converted image (between 0.0 and 1.0). * Applicable for 'jpeg', 'jpg', and 'webp' formats. * @default 0.92 */ quality?: number; /** * Optional custom decoder implementation to inject. * If not provided, a default LibheifDecoder is used. */ decoder?: IHeicDecoder; /** * Optional progress callback that receives the progress percentage (0 to 100) during decoding. */ onProgress?: (percent: number) => void; } interface ConvertManyOptions extends Omit { /** * Maximum number of conversions running concurrently. * @default 4 */ concurrency?: number; /** * Optional progress callback that receives the item index and its * progress percentage (0 to 100) during decoding. */ onProgress?: (index: number, percent: number) => void; /** * Optional custom decoder implementation to inject. When provided, the * same instance is shared by all concurrent conversions, so it must be * safe for concurrent `decode()` calls. If not provided, a fresh * default LibheifDecoder is created per item. */ decoder?: IHeicDecoder; } interface LibheifDecoderOptions { /** * Custom function to locate the WASM file. * Useful when serving the WASM file from a custom route or CDN. */ locateFile?: (path: string, prefix: string) => string; /** * Raw WASM binary buffer. If provided, the library will use this buffer * directly instead of attempting to fetch the WASM file. */ wasmBinary?: ArrayBuffer; } declare class LibheifDecoder implements IHeicDecoder { private options?; private module; private decoderInstance; /** * Memoized module-loading promise so concurrent initialize()/decode() calls * on the same instance never instantiate (or mutate) the module twice. */ private initPromise; constructor(options?: LibheifDecoderOptions); /** * Initializes the WebAssembly module and instantiates the HEIC decoder. * Safe to call concurrently: the module is loaded at most once per instance. */ initialize(): Promise; /** * Decodes HEIC binary data into raw RGBA pixel data. * @param data The HEIC file contents as a Uint8Array. * @param onProgress Optional progress callback. */ decode(data: Uint8Array, onProgress?: (percent: number) => void): Promise; /** * Cleans up the WebAssembly decoder instance and resources. * Idempotent: safe to call multiple times. After free(), the instance must * be re-initialized (createHeicDecoderModule runs again on the next call). */ free(): void; } interface WorkerConvertOptions extends ConvertOptions { /** * URL of the worker script that performs the conversion. Should be a * compile-time constant (e.g. `new URL('./worker.js', import.meta.url)` * under a bundler); the script runs with the page's privileges. */ workerUrl: string | URL; /** * Maximum time in milliseconds to wait for the worker result before * rejecting with a timeout. Defaults to 60000 (60s). Set to 0 to * disable the timeout. */ timeoutMs?: number; /** * Worker script type. Use `'module'` when the script uses ES module * imports (e.g. `import { convertHeic } from ...`); `'classic'` * scripts must be pre-bundled. * @default 'classic' */ workerType?: 'classic' | 'module'; } /** * Converts a HEIC image inside a Web Worker so the main thread stays * responsive during the (potentially slow) WASM decode. * * The worker script is user-provided and must handle the following message * protocol: * * ```js * // converter.worker.js * import { convertHeic } from '@keeratita/heic-converter'; * * self.onmessage = async (event) => { * const { input, options } = event.data; * try { * const blob = await convertHeic(input, { * ...options, * onProgress: (percent) => self.postMessage({ type: 'progress', percent }), * }); * self.postMessage({ type: 'result', ok: true, blob }); * } catch (error) { * self.postMessage({ type: 'result', ok: false, error: error?.stack ?? error?.message ?? String(error) }); * } * }; * ``` * * ```ts * const jpegBlob = await convertHeicInWorker(heicBlob, { * workerUrl: new URL('./converter.worker.js', import.meta.url), * workerType: 'module', * to: 'jpeg', * }); * ``` * * Only `progress` and `result` messages are understood; any other message * type is ignored. Use `workerType: 'module'` when the script uses ES * module imports (as in the example above); `'classic'` scripts must be * pre-bundled, since static ES imports are not supported there. * * @param input HEIC image as a Blob, File, ArrayBuffer, or Uint8Array. * @param options Conversion options plus the worker script URL. * @returns A Promise resolving to the converted image as a Blob. */ declare function convertHeicInWorker(input: HeicInput, options: WorkerConvertOptions): Promise; /** * Releases any cached decoder resources. * * Decoders are now created and released per conversion, so there is no shared * instance to free. This function is kept for API compatibility. */ declare function freeSharedDecoder(): void; /** * Converts HEIC image data to a standard web format (JPEG, PNG, WebP, or SVG). * * @param input HEIC image as a Blob, File, ArrayBuffer, or Uint8Array. * @param options Conversion configuration options. * @returns A Promise resolving to the converted image as a Blob. */ declare function convertHeic(input: HeicInput, options?: ConvertOptions): Promise; /** * Converts multiple HEIC images to a standard web format. * * Conversions run with a bounded concurrency (default 4) and results are * returned in the same order as the inputs. If any conversion fails, the * returned promise rejects as soon as the failure is known (in-flight * conversions are allowed to finish in the background) with an error that * identifies the failing item index. * * @param inputs HEIC images as Blobs, Files, ArrayBuffers, or Uint8Arrays. * @param options Batch conversion options. * @returns A Promise resolving to the converted images as Blobs, in input order. */ declare function convertMany(inputs: HeicInput[], options?: ConvertManyOptions): Promise; export { type ConvertManyOptions, type ConvertOptions, type DecodedImage, type HeicInput, type IHeicDecoder, type ImageFormat, LibheifDecoder, type LibheifDecoderOptions, type ResizeOptions, type WorkerConvertOptions, convertHeic, convertHeicInWorker, convertMany, freeSharedDecoder };