[raster] Toolbox: probe/decode with caps, convert, crop, rotate, resize, presets, compress to size, HEIC via WIC

This commit is contained in:
2026-11-16 13:30:42 +01:00
parent ede068f7c9
commit ce1e8785d9
16 changed files with 2394 additions and 1 deletions
+151
View File
@@ -0,0 +1,151 @@
use serde::{Deserialize, Serialize};
use crate::isobmff;
/// The image formats the Toolbox knows. `Heif` and `Avif` share the ISO-BMFF/HEIF container and are read
/// only through the Windows codec; neither can be written.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Format {
Png,
Jpeg,
WebP,
Bmp,
Gif,
Tiff,
Ico,
/// HEIF/HEIC (brands heic, heix, hevc, heim, heis, hevm, hevs, mif1, msf1).
Heif,
/// AVIF (brands avif, avis): HEIF container with AV1 data.
Avif,
}
impl Format {
/// Every format, in the order the Toolbox lists them.
pub const ALL: [Format; 9] = [
Format::Png,
Format::Jpeg,
Format::WebP,
Format::Bmp,
Format::Gif,
Format::Tiff,
Format::Ico,
Format::Heif,
Format::Avif,
];
/// Identify a format by its magic bytes only (never by the file name).
pub fn sniff(bytes: &[u8]) -> Option<Format> {
let starts = |sig: &[u8]| bytes.starts_with(sig);
if starts(b"\x89PNG\r\n\x1a\n") {
Some(Format::Png)
} else if starts(&[0xFF, 0xD8, 0xFF]) {
Some(Format::Jpeg)
} else if bytes.len() >= 12 && starts(b"RIFF") && &bytes[8..12] == b"WEBP" {
Some(Format::WebP)
} else if starts(b"GIF87a") || starts(b"GIF89a") {
Some(Format::Gif)
} else if starts(b"II*\0") || starts(b"MM\0*") || starts(b"II+\0") || starts(b"MM\0+") {
Some(Format::Tiff)
} else if bytes.len() >= 14 && starts(b"BM") {
Some(Format::Bmp)
} else if bytes.len() >= 6 && starts(&[0, 0, 1, 0]) && (bytes[4] != 0 || bytes[5] != 0) {
Some(Format::Ico)
} else {
match isobmff::brand(bytes)? {
isobmff::Brand::Heif => Some(Format::Heif),
isobmff::Brand::Avif => Some(Format::Avif),
}
}
}
/// The usual file extension, without the dot.
pub fn extension(self) -> &'static str {
match self {
Format::Png => "png",
Format::Jpeg => "jpg",
Format::WebP => "webp",
Format::Bmp => "bmp",
Format::Gif => "gif",
Format::Tiff => "tif",
Format::Ico => "ico",
Format::Heif => "heic",
Format::Avif => "avif",
}
}
/// The name shown to people.
pub fn name(self) -> &'static str {
match self {
Format::Png => "PNG",
Format::Jpeg => "JPG",
Format::WebP => "WebP",
Format::Bmp => "BMP",
Format::Gif => "GIF",
Format::Tiff => "TIFF",
Format::Ico => "ICO",
Format::Heif => "HEIC",
Format::Avif => "AVIF",
}
}
pub fn mime_type(self) -> &'static str {
match self {
Format::Png => "image/png",
Format::Jpeg => "image/jpeg",
Format::WebP => "image/webp",
Format::Bmp => "image/bmp",
Format::Gif => "image/gif",
Format::Tiff => "image/tiff",
Format::Ico => "image/x-icon",
Format::Heif => "image/heic",
Format::Avif => "image/avif",
}
}
/// Look a format up by an extension or a file name: `"jpg"`, `".JPEG"`, `"photo.heic"`.
pub fn from_name(name: &str) -> Option<Format> {
let ext = name.rsplit('.').next().unwrap_or(name).trim().to_ascii_lowercase();
Some(match ext.as_str() {
"png" => Format::Png,
"jpg" | "jpeg" | "jpe" | "jfif" => Format::Jpeg,
"webp" => Format::WebP,
"bmp" | "dib" => Format::Bmp,
"gif" => Format::Gif,
"tif" | "tiff" => Format::Tiff,
"ico" => Format::Ico,
"heic" | "heif" | "heics" | "heifs" | "hif" => Format::Heif,
"avif" => Format::Avif,
_ => return None,
})
}
/// HEIF container (HEIC or AVIF): decoded by the OS codec.
pub fn is_heif_family(self) -> bool {
matches!(self, Format::Heif | Format::Avif)
}
/// Whether `encode` can write this format.
pub fn can_encode(self) -> bool {
!self.is_heif_family()
}
pub(crate) fn to_image(self) -> Option<image::ImageFormat> {
Some(match self {
Format::Png => image::ImageFormat::Png,
Format::Jpeg => image::ImageFormat::Jpeg,
Format::WebP => image::ImageFormat::WebP,
Format::Bmp => image::ImageFormat::Bmp,
Format::Gif => image::ImageFormat::Gif,
Format::Tiff => image::ImageFormat::Tiff,
Format::Ico => image::ImageFormat::Ico,
Format::Heif | Format::Avif => return None,
})
}
}
impl std::fmt::Display for Format {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.name())
}
}