[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
+76
View File
@@ -0,0 +1,76 @@
//! pictor-raster: the image side of Pictor's Toolbox.
//!
//! Decode with caps ([`probe`], [`decode`]), encode ([`encode`]), [`crop`], [`rotate`], [`resize`], batch
//! presets ([`apply_preset`]) and "compress to under N MB" ([`compress_to`]). Formats: PNG, JPG, WebP, BMP,
//! GIF, TIFF and ICO both ways through the `image` crate; HEIC/HEIF (and AVIF) in only, through the Windows
//! Imaging Component and the codecs Microsoft ships in the Store (see [`heif_support`]). No HEVC code is bundled.
//!
//! Every input is treated as hostile: sizes are read from headers and checked against [`Limits`] before any
//! pixel buffer exists, third-party decoder panics are caught, and every error is a [`RasterError`] whose
//! `Display` is a plain-English sentence.
mod decode;
mod encode;
mod error;
mod format;
mod isobmff;
mod ops;
mod preset;
#[cfg(windows)]
mod wic;
pub use decode::{Probe, decode, probe};
pub use encode::{EncodeOptions, encode};
pub use error::{RasterError, Result};
pub use format::Format;
pub use image::DynamicImage;
pub use ops::{Fit, Rect, Rotation, crop, resize, rotate};
pub use preset::{Compressed, Preset, apply_preset, compress_to, has_transparency};
use serde::{Deserialize, Serialize};
/// Caps applied to every decode. The worker process adds its own memory and time caps on top.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct Limits {
/// Largest width × height accepted (default 128 million: a 16000 × 8000 panorama).
pub max_pixels: u64,
/// Largest input file in bytes (default 512 MiB).
pub max_input_bytes: u64,
/// Largest decoded buffer, and the budget handed to the decoders (default 1 GiB).
pub max_alloc: u64,
}
impl Default for Limits {
fn default() -> Self {
Self { max_pixels: 128_000_000, max_input_bytes: 512 * 1024 * 1024, max_alloc: 1024 * 1024 * 1024 }
}
}
/// Whether this machine can decode HEIC photos.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum HeifSupport {
/// The HEIF container codec and an HEVC decoder are installed.
Ready,
/// "HEIF Image Extensions" is installed, "HEVC Video Extensions" is not.
NeedsHevc,
/// An HEVC decoder exists but "HEIF Image Extensions" is missing.
NeedsHeif,
/// Neither is installed.
NeedsHeifAndHevc,
/// Not Windows: HEIC is never decoded here.
Unavailable,
}
/// Probe the OS codecs without a file: the WIC HEIF decoder is instantiated by its container GUID, and
/// Media Foundation is asked (`MFTEnumEx`) for a decoder that takes HEVC video. Never fails.
pub fn heif_support() -> HeifSupport {
#[cfg(windows)]
{
wic::heif_support()
}
#[cfg(not(windows))]
{
HeifSupport::Unavailable
}
}