import { type ImageEncodeOption } from "@valbuild/core"; /** * Every decision `encode` makes, with none of the pixels. * * The re-encode itself happens in two places that share nothing: the Studio * runs it on a `` before the upload leaves the browser * (`packages/ui/spa/utils/encodeImage.ts`), and the MCP image tool runs it on * `sharp` in the server process (`@valbuild/mcp`). What must NOT differ between * them is what `s.image({ encode })` means — which images are converted, how * far they are scaled, and when the original wins anyway. So the decisions live * here, once, and each side supplies only its own encoder. * * See `architecture/media.md`. */ export type EncodeSettings = { type: "webp"; quality: number; maxWidth: number; maxHeight: number; }; export declare const ENCODE_DEFAULTS: Omit; export declare const ENCODE_MIME_TYPE_OF: Record; /** * Formats an encoder cannot round-trip without losing something: SVG is vector, * an animated GIF keeps only its first frame, and AVIF is already smaller than * anything we would produce. */ export declare const NEVER_ENCODED: string[]; /** * The schema's own option first, then the gallery it references, then off. * * Mirrors how `ImageField` resolves `accept` and `directory`. A gallery-backed * field (`s.image(galleryVal)`) serializes with EMPTY options, so without the * gallery fallback it could never inherit what the gallery asked for — and * `s.image(galleryVal, { encode: false })` is how such a field opts back out. * * Nonsense numbers fall back to the defaults rather than through. `fitWithin` * reads a bound of 0 (or NaN, or a negative) as "everything already fits", so * an unusable `maxWidth` would otherwise SILENTLY disable the downscale it was * asking for, and a `quality` outside 0–1 is ignored by `canvas.toBlob` just as * quietly. Falling back is deliberately not throwing: a schema is evaluated by * the CLI's `node:vm` sandbox and by the Next server, so a typo'd quality would * take a whole project down over an upload-time optimisation. */ export declare function resolveEncodeSettings(fieldOption: ImageEncodeOption | undefined, galleryOption: ImageEncodeOption | undefined): EncodeSettings | null; /** * Dimensions scaled to fit inside the box, or null when the image already does. * * Null is the signal for "no downscale was needed", which the size comparison * in {@link chooseEncoded} turns on. */ export declare function fitWithin(width: number, height: number, maxWidth: number, maxHeight: number): { width: number; height: number; } | null; /** * Whether a schema that declares `accept` would take this mime type. * * The same rules as `ImageSchema.executeValidate`, which validates the STORED * mimeType against `accept` — so converting to a type `accept` forbids would * upload a file the schema reports as invalid the moment it lands. Delegated to * `@valbuild/core` rather than re-spelled, because a second reading of one * `accept` string is how a converted image passes here and fails there. */ export declare function isMimeTypeAccepted(mimeType: string, accept: string | undefined): boolean; /** Formats left alone: the lossy list, plus the target type when it already fits. */ export declare function isSkippedSource(sourceMimeType: string, targetMimeType: string, needsDownscale: boolean): boolean; /** * The types the Studio can write with WebAssembly when the canvas cannot. * * libwebp, compiled to WebAssembly (`@jsquash/webp`, loaded by * `packages/ui/spa/utils/webpWasm.ts`). One entry because `encode` has one * `type`; a second type is a second encoder to host, not a second string here. */ export declare const WASM_ENCODED_MIME_TYPES: string[]; /** * Whether to encode the canvas's pixels with WebAssembly, given what the * canvas's own encoder handed back. * * `canvas.toBlob` answers a type it cannot write with a PNG instead of an error, * and Safari -- the browser people take their photos in -- cannot write WebP. * The pixels on that canvas are already decoded, oriented and scaled, so the * only thing missing is the encoder, and libwebp in WebAssembly is that. * * Only then, and that is what keeps the WebAssembly lazy: a browser whose * canvas wrote the type is never asked to download an encoder, even when its * WebP then loses to the original on size -- a second encoder of the same * format would lose for the same reason. * * `null` -- no blob at all -- is not "cannot write this type" either. It is a * canvas that failed to serialize anything, under memory pressure for instance, * and says nothing about WebP: a Chromium tab that hits it is still a browser * that writes WebP, and must not start downloading an encoder because of it. */ export declare function shouldEncodeWithWasm(input: { targetMimeType: string; canvasOutputType: string | null; }): boolean; /** * What to scale an image down to when nothing could write the type asked for: * the source's own format, when it is one every canvas can write. * * The second fallback, after {@link shouldEncodeWithWasm}: it is what a browser * whose canvas cannot write WebP gets when the WebAssembly encoder could not be * loaded either (offline, a blocked host, a failed instantiation). Without it, * `chooseEncoded` would rightly refuse the canvas's PNG and upload the original * -- every photo at the camera's full size, the one case `maxWidth` exists * for. Scaling it down as a JPEG keeps the half of `encode` this browser can do. * * Only when a downscale is needed: re-encoding a JPEG that already fits, as a * JPEG, is a second generation of loss for a few percent. And never into a * type `accept` refuses, for the reason `isMimeTypeAccepted` gives. */ export declare function fallbackMimeType(sourceMimeType: string, needsDownscale: boolean, accept: string | undefined): string | null; /** * Whether to upload what the encoder produced, or the file that was picked. * * Two ways the encoded bytes lose. They are not actually the type we asked for * — `canvas.toBlob` answers an unsupported type with a PNG rather than with * null, so "not null" is not the same question as "is a webp". Or they are * simply bigger, which happens for small and flat images: measured, an 8x8 * solid PNG of 74 bytes becomes a 548 byte webp. A downscale overrides that, * since the original is then the wrong size whatever it weighs. */ export declare function chooseEncoded(input: { originalSize: number; encodedSize: number | null; encodedType: string | null; targetMimeType: string; needsDownscale: boolean; }): boolean; /** * `photo.png` -> `photo.webp`. The extension is re-derived downstream too, but * the name should not lie. */ export declare function withExtension(filename: string, ext: string): string;