Files
image/image/formats.sakura
T
2026-10-28 08:26:26 -05:00

99 lines
3.5 KiB
Plaintext

#! Contains the formats, and how bytes, or a file's name, say which one they're
#! in.
module image;
#! The formats an image is read from, the first two of which it's written to.
pub enum format: u8 {
png, #! PNG, of any bit depth and color type, interlaced or not.
jpeg, #! JPEG, baseline or progressive, gray, YCbCr, RGB, CMYK, or YCCK.
svg, #! SVG, drawn at the size asked for, and never written.
}
#! The size an SVG is drawn at. Left as it is, that's the size the SVG gives
#! itself, by its `width` and `height`, or its `viewBox` without them. A width or
#! height asked for alone sets the other to keep the SVG's shape, and `scale`
#! multiplies whichever size is chosen. Raster formats are read at their own size
#! whatever this says.
pub struct svg_size {
pub var width: u32 = 0;
pub var height: u32 = 0;
pub var scale: f64 = 1;
}
#! Which format bytes are in, by how they begin: PNG's signature, JPEG's start of
#! image, or text whose first element is `<svg`.
#! @param bytes The bytes.
#! @returns The format, or `nothing` where they're none of those.
pub def format_of(bytes: span<u8>): maybe<format> {
if bytes.length >= 8 && bytes[0] == 0x89 && bytes[1] == 'P' && bytes[2] == 'N' && bytes[3] == 'G'
&& bytes[4] == 0x0D && bytes[5] == 0x0A && bytes[6] == 0x1A && bytes[7] == 0x0A {
return format::png;
}
if bytes.length >= 3 && bytes[0] == 0xFF && bytes[1] == 0xD8 && bytes[2] == 0xFF {
return format::jpeg;
}
if looks_like_svg(bytes) {
return format::svg;
}
return nothing {};
}
#! Which format a file's extension names, whatever the case of its letters:
#! `png`, `jpg`, `jpeg`, `jpe`, `jfif`, or `svg`.
#! @param extension The extension, without its `.`.
#! @returns The format, or `nothing` where it names none.
pub def format_named(extension: strview): maybe<format> {
val lowered := extension.lower();
val name := lowered.view();
if name == "png" {
return format::png;
}
if name == "jpg" || name == "jpeg" || name == "jpe" || name == "jfif" {
return format::jpeg;
}
if name == "svg" {
return format::svg;
}
return nothing {};
}
# Whether bytes are text, after a byte order mark and whitespace, whose markup
# reaches an `svg` element before anything else: only declarations, comments,
# instructions, and the document type may come first.
def looks_like_svg(bytes: span<u8>): bool {
val text: strview = bytes;
var at: u64 = text.starts_with("\xEF\xBB\xBF") ? 3 : 0;
while at < text.length {
while at < text.length && text[at].is_space {
at++;
}
if at >= text.length || text[at] != '<' {
return false;
}
val rest := text.substr(at, text.length - at);
if rest.starts_with("<?") || rest.starts_with("<!") {
# Past it: a comment to `-->`, and the rest to the `>` closing them, past
# any internal subset of a document type, which `[` begins before it.
var closing: strview = rest.starts_with("<!--") ? "-->" : ">";
val end := rest.find(closing) else { return false; };
if rest.starts_with("<!DOCTYPE") && rest.substr(0, end).find("[") is some(_) {
closing = "]>";
}
val past := rest.find(closing) else { return false; };
at += past + closing.length;
continue;
}
# The first element: `svg`, with or without a prefix.
var end: u64 = 1;
while end < rest.length && !rest[end].is_space && rest[end] != '>' && rest[end] != '/' {
end++;
}
val name := rest.substr(1, end - 1);
return name == "svg" || name.ends_with(":svg");
}
return false;
}