import type { WorkerImagePage, PdfPixelFormat } from './protocol.js'; /** * Already-decoded image pixels. Provide these directly when the runtime cannot * decode the format on its own (e.g. Node, which has no `createImageBitmap`), * or to skip decoding when you already hold raw pixels. * */ export interface PdfRawImage { /** Tightly packed pixels, `width * height * 4` bytes. */ pixels: Uint8Array | ArrayBuffer; /** Pixel width of the bitmap. */ width: number; /** Pixel height of the bitmap. */ height: number; /** Byte order of {@link pixels}. Default `'rgba8888'` (what a canvas produces). */ format?: PdfPixelFormat; } /** * One image handed to {@link PdfDocument.createPagesFromImages}. Either encoded bytes * (a `Blob`, `Uint8Array`, or `ArrayBuffer`) that get decoded, or a * {@link PdfRawImage} of pixels that are used as-is. * */ export type PdfImageSource = Blob | Uint8Array | ArrayBuffer | PdfRawImage; /** * Decodes encoded image bytes to {@link PdfRawImage} pixels. Supply one via * {@link PdfCreatePagesFromImagesOptions.decode} on runtimes without a built-in * decoder (JPEG never needs one — PDFium decodes it natively). * */ export type PdfImageDecoderResult = PdfRawImage | Blob | Uint8Array | ArrayBuffer; /** * Decodes or converts encoded image bytes. Return {@link PdfRawImage} for * RGBA/BGRA pixels, or return encoded bytes/a `Blob` in a format the runtime * can decode (JPEG is accepted directly by PDFium). Return `null` to decline * the input and use the built-in browser decoder. * * pdfrx can import images as PDF pages and, through `@pdfrx/react`, as image * annotations. Encoded formats that the current browser cannot decode (HEIC is * a common example) cannot be imported by default. Applications can add support * for those formats without adding a pdfrx dependency on a particular codec: * provide this callback and convert the input to JPEG/PNG or decoded * RGBA8888/BGRA8888 pixels. * * @example Decode HEIC with an optional application dependency * ```tsx * import heic2any from 'heic2any'; * import type { PdfImageDecoder } from '@pdfrx/engine'; * import { PdfrxViewerApp } from '@pdfrx/react'; * * const decodeImage: PdfImageDecoder = async (bytes, mimeType) => { * if (!mimeType || !/^image\/hei[cf](?:-sequence)?$/.test(mimeType)) return null; * const result = await heic2any({ * blob: new Blob([bytes], { type: mimeType }), * toType: 'image/jpeg', * }); * return Array.isArray(result) ? result[0]! : result; * }; * * * ``` * */ export type PdfImageDecoder = (bytes: Uint8Array, mimeType?: string) => Promise | PdfImageDecoderResult | null; /** Options for {@link PdfDocument.createPagesFromImages}. */ export interface PdfCreatePagesFromImagesOptions { /** * Pixels-per-inch used to convert an image's pixel size into the page size in * points. Default `72` (1 pixel = 1 point). Ignored for any image whose page * size is fixed by {@link pageSize}. * */ dpi?: number; /** * Fixed page size in points (1/72 inch) applied to every page; the image is * scaled to fill it. When omitted, each page is sized from its own image via * {@link dpi}. * */ pageSize?: { width: number; height: number; }; /** * Decoder for non-JPEG formats. Falls back to `createImageBitmap` + * `OffscreenCanvas` when available (browsers, workers, Deno, Bun) and, failing * that, throws — so pass this (or pre-decoded {@link PdfRawImage}s) on Node. * */ decode?: PdfImageDecoder; } /** * True for a byte stream that begins with the JPEG SOI marker. * * @param bytes - The binary data to process. * @returns Whether the condition is satisfied. * */ export declare function isJpeg(bytes: Uint8Array): boolean; /** * Reads a JPEG's pixel dimensions from its `SOFn` marker without decoding the * image, so JPEG pages can be sized without a pixel decoder. Throws if the bytes * are not a JPEG whose size can be found. * @param bytes - The binary data to process. * @returns The updated result. * */ export declare function readJpegSize(bytes: Uint8Array): { width: number; height: number; }; /** * Whether the current runtime can decode encoded images without a user-supplied decoder. * * @returns Whether the condition is satisfied. * */ export declare function canDecodeImages(): boolean; /** * Converts every {@link PdfImageSource} into wire pages and gathers the * `ArrayBuffer`s to transfer to the worker. Decoding (when needed) happens here, * on the calling thread. * */ export declare function imageSourcesToWorkerPages(images: readonly PdfImageSource[], options: PdfCreatePagesFromImagesOptions): Promise<{ pages: WorkerImagePage[]; transfer: ArrayBuffer[]; }>; //# sourceMappingURL=image-source.d.ts.map