Skip to main content

Module image_ops

Module image_ops 

Source
Expand description

Image mechanics shared by tools and the wire layer: magic sniffing, header-only dimension reads, the downscale-to-fit pass, and the per-image / per-message byte budget that keeps request bodies under the gateway’s limit (the HTTP 413 cure — see apply_image_budget).

One source of truth for “is this bytes stream a picture, how big is it, and does it fit the wire”: the read_image tool sniffs and labels with it, and the engine budgets and shrinks with it. Numbers live here as named constants so a product can argue with them in one place.

Structs§

ImageBudgetError
Why the request never left the machine: the full size inventory of every picture it carried, plus the lines that crossed the budget. Surfaced as a run error — the request is refused WHOLE, never thinned.

Constants§

MAX_IMAGE_BASE64_BYTES
Per-image wire budget, in base64 characters (the on-the-wire size). ~2.6 MB of raw pixels — after the downscale pass only the most pathological screenshot can reach it, and such a one is refused with an inventory instead of silently thinned.
MAX_MESSAGE_IMAGES_BASE64_BYTES
Per-message wire budget for all of its pictures together, in base64 characters. A message keeps all of its pictures or none — this is the line where “all” stops being affordable and the send is refused.
MAX_WIRE_LONG_EDGE
Longest edge any picture may carry on the wire: large enough for a desktop screenshot’s text to stay legible to a vision model, small enough that two of them never tripped the gateway’s body limit (the HTTP 413 of session 20261030_862ddf5c).

Functions§

apply_image_budget
Prepare every picture for the wire and enforce the byte budget.
base64_len
On-the-wire size of a raw payload once base64-encoded, without doing the encode: padded standard base64 is ceil(n / 3) * 4 characters.
base64_prefix_dims
Read a picture’s pixel size from the HEAD of its base64 payload — a bounded probe, never a full decode. None when the head carries no decodable PNG/JPEG header: an exotic format, a corrupt payload, or not an image at all. Non-ASCII characters are skipped (a payload may be arbitrary text), so the probe never slices mid-character.
downscale_to_fit
Shrink a picture so its longest edge is MAX_WIRE_LONG_EDGE, re-encoding in the SAME format (a PNG screenshot stays lossless, a JPEG stays a JPEG). Returns the (possibly unchanged) bytes plus the original and final pixel sizes.
from_base64
Decode base64 with the standard alphabet.
header_dimensions
Header-only dimension read for PNG/JPEG — no pixel decode. None for bytes that are not a decodable PNG/JPEG stream.
sniff_mime
MIME sniff from magic bytes — the extension and any declared MIME are advisory at best (a .jpg that is really a PNG is a PNG; a text/html wearing image/png is not a picture at all).
to_base64
Encode to base64 (standard alphabet, padded).

Type Aliases§

Dims
A picture’s pixel size, (width, height).