Files
Pictor-public/crates/raster/src/lib.rs
T

77 lines
2.7 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! 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
}
}