import type { MissCode } from "./contract.js"; import { type ImageLimits } from "./limits.js"; /** An image the model can take: a supported format, within the limits, as base64. pi-durable's `PreparedImage`. */ export type PreparedImage = { readonly data: string; readonly mimeType: string; /** Set when the image was resized: its size before and after, so coordinates can be mapped back. */ readonly resized?: { readonly from: { readonly width: number; readonly height: number; }; readonly to: { readonly width: number; readonly height: number; }; }; /** The original format, when it differs from `mimeType`. */ readonly convertedFrom?: string; }; /** A width and a height in pixels. */ export type PixelSize = { readonly width: number; readonly height: number; }; /** A rectangle in pixels: a size at an offset from the top left. */ export type PixelRect = PixelSize & { readonly left: number; readonly top: number; }; /** Decoded pixels: 8 bits a channel, rows from the top with no padding, opaque (3 channels) or with alpha (4). */ export type Raster = PixelSize & { readonly channels: 3 | 4; readonly data: Uint8Array; }; /** pi-durable's `ImageProcessor`: decodes, orients, resizes and re-encodes one image to fit `limits`, or gives * `undefined` when it cannot. A processor pi-durable accepts is accepted here, and the other way round. The members * after `prepare` are this package's: a processor that has `raster` and `encode` is asked for tiles and for the reasons * a fit fails; one that has only `prepare` is asked for one image and nothing else. */ export type ImageProcessor = { prepare(bytes: Uint8Array, mimeType: string, limits: ImageLimits): Promise; /** The MIME types it decodes. Absent: not stated, so every one is tried. */ readonly decodes?: readonly string[]; /** The stored size and EXIF orientation of an image whose header this package cannot read, without decoding it. */ probe?(bytes: Uint8Array): Promise<(PixelSize & { readonly orientation: number; }) | undefined>; /** The image turned upright by the EXIF `orientation` it is given (1 to 8: the one the core decided on, so no * processor reads another) and, when `size` is given, scaled to exactly that size (the upright one). Rejects with the * decoder's reason; never opens an image over `maxPixels`. */ raster?(bytes: Uint8Array, request: { readonly size?: PixelSize; readonly orientation: number; readonly maxPixels: number; }): Promise; /** A PNG, or a JPEG at `jpegQuality` (transparency flattened onto white), of the raster. */ encode?(raster: Raster, jpegQuality?: number): Promise; }; /** An image block as the model receives it, with the pixels it holds. */ export type ImageBlock = PixelSize & { readonly data: string; readonly mimeType: string; }; /** The limits one fit is held to: pi-durable's `ImageLimits`, the JPEG quality to start at and how many tiles one call * may carry. A route's `Limits.image` and `Limits.imagesPerCall`, side by side. */ export type FitLimits = ImageLimits & { readonly jpegQuality?: number; readonly imagesPerCall?: number; /** Each side of every block at least this many pixels: a smaller image is scaled up as far as the edge limits allow, * then padded with white at its right and bottom, its own pixels at the top left (`content` in the fit); a tile under * it is padded the same way, so a point read off a tile is still the tile's start plus its coordinate. */ readonly minEdge?: number; /** The MIME types a block may keep as they are (default: PNG, JPEG, GIF and WebP); any other is re-encoded. */ readonly formats?: readonly string[]; /** A progressive JPEG is re-encoded, so every JPEG sent is baseline. */ readonly baseline?: boolean; }; /** An image as the model receives it: its own bytes, one re-encoded block, or tiles in reading order; or why it cannot * be shown, as a miss code, the limit it broke and the reason, in the words of the text that stands in for it. `original` * is the size upright, after the EXIF orientation. */ export type ImageFit = { readonly fit: "as_is"; readonly original: PixelSize; readonly blocks: readonly [ImageBlock]; } | { readonly fit: "resized"; readonly original: PixelSize; readonly blocks: readonly [ImageBlock]; readonly turned: boolean; readonly convertedFrom?: string; readonly content?: PixelSize; } | { readonly fit: "tiled"; readonly original: PixelSize; readonly blocks: readonly ImageBlock[]; readonly scaled: PixelSize; readonly tile: PixelSize; readonly starts: readonly number[]; readonly vertical: boolean; readonly turned: boolean; } | { readonly fit: "refused"; readonly code: MissCode; readonly limit: string; readonly why: string; }; /** Where an image's blocks are cut: the raster they come from (the image upright and scaled so a fit, or the most tiles a * call carries, covers it) and each tile on it, top to bottom or left to right; no tiles means one block holds the whole * raster. An image is tiled when its long side is over `TILE_ASPECT` times its short side and fitting it whole would * keep less than `TILE_KEEP_BELOW` of it. A tile has the full short side and at most `TILE_MAX_EDGE` along the long one, * overlaps the one before by `TILE_OVERLAP`, and the last ends at the edge, so every tile has the full length. Pure: no * decoding; `tiles: false` never tiles. */ export declare function imageLayout(original: PixelSize, limits: FitLimits, tiles?: boolean): { readonly scaled: PixelSize; readonly tiles: readonly PixelRect[]; }; /** An image fitted to the limits of one block. Within them by its header, its own bytes come back and nothing is * decoded. Over a limit, or a format a model does not take, it is decoded, turned upright by its EXIF orientation and * either re-encoded at the size that fits inside the edge limits or, when it is tall or wide and a fit would shrink it * past reading, cut into overlapping tiles (`imageLayout`). Its bytes never go out unfitted: a processor that is missing * or fails, a header over the decode guard, a fit under the floor, an image no encoding of which fits, and a format the * processor does not decode are all `refused`, each with its miss code. A header that states no size is asked of the * processor's `probe`; with none, the image is refused, never passed on its bytes alone. `tiles: false` fits the image * whole whatever its shape, as pi-durable's `prepare` does. */ export declare function fitImage(processor: ImageProcessor, bytes: Uint8Array, mimeType: string, limits: FitLimits, options?: { readonly tiles?: boolean; }): Promise; /** The one sentence that says what the model was shown in place of the file, so coordinates read off it map back to the * file; empty for an image shown as it is. */ export declare function fitSentence(fit: ImageFit, limits: Pick): string; /** An `ImageProcessor` built from its operations: `prepare` is `fitImage` with `tiles: false`, pi-durable's contract of * one image or none. */ export declare function imageProcessor & Required>>(operations: T): T & ImageProcessor;