pub struct Image { /* private fields */ }Expand description
A lazily evaluated image pipeline.
Cheap to clone: clones share graph nodes rather than pixels. Chaining builds graph structure and executes nothing (SPEC §Guarantees 3).
Implementations§
Source§impl Image
impl Image
Sourcepub fn from_raw(descriptor: ImageDescriptor, bytes: Vec<u8>) -> Result<Self>
pub fn from_raw(descriptor: ImageDescriptor, bytes: Vec<u8>) -> Result<Self>
Build an image from raw pixels already in memory.
bytes must be exactly the packed byte length of descriptor.
§Errors
Returns PixelsError::InvalidArgument if bytes is not exactly the
packed length descriptor implies.
Sourcepub fn from_raw_stream(
layout: RawFormat,
source: impl Source + Debug + 'static,
) -> Result<Self>
pub fn from_raw_stream( layout: RawFormat, source: impl Source + Debug + 'static, ) -> Result<Self>
Build an image by decoding a raw pixel stream.
The header parse is trivial for raw — the layout is the header — so
this reads no bytes from source. Pixels are pulled at the terminal.
§Errors
Returns PixelsError::InvalidArgument if the layout is not
representable on this platform.
Sourcepub fn open(path: impl AsRef<Path>) -> Result<Self>
pub fn open(path: impl AsRef<Path>) -> Result<Self>
Open an image file, identifying its format from its contents, with
default OpenOptions — so it is turned upright.
The path’s extension is ignored. Detection is by magic bytes only (SPEC §Formats), because a name is an attacker-controlled hint while the bytes are a fact.
§Errors
Returns PixelsError::Io if the file cannot be opened,
PixelsError::Unsupported if no built-in codec recognises it, and
PixelsError::Malformed if the header is invalid for the format its
magic bytes claim.
Sourcepub fn from_stream(source: impl Source + Debug + 'static) -> Result<Self>
pub fn from_stream(source: impl Source + Debug + 'static) -> Result<Self>
Build an image from a byte stream, identifying its format from the leading bytes.
Sniffing reads only the longest magic prefix any known codec needs, and
replays it to the decoder rather than seeking — a Source is
forward-only (ADR-0005), so a pipe or socket works here exactly as a
file does. A stream shorter than that prefix is not an error at this
stage: it simply matches nothing.
§Errors
Returns PixelsError::Unsupported if no built-in codec recognises
the stream, PixelsError::Io on read failure, or
PixelsError::Malformed if the header is invalid for the format its
magic bytes claim.
Sourcepub fn from_stream_with(
source: impl Source + Debug + 'static,
options: OpenOptions,
) -> Result<Self>
pub fn from_stream_with( source: impl Source + Debug + 'static, options: OpenOptions, ) -> Result<Self>
Sourcepub fn from_decoder(decoder: Box<dyn Decoder>, format: Format) -> Self
pub fn from_decoder(decoder: Box<dyn Decoder>, format: Format) -> Self
Build an image from any decoder whose header has already been parsed.
This is the extension point for codecs living outside this crate.
Pixels arrive as stored: Decoder::orientation is not applied here,
so pass it to Image::orient to turn the result upright.
Sourcepub fn from_producer(producer: Arc<dyn Producer>, format: Format) -> Self
pub fn from_producer(producer: Arc<dyn Producer>, format: Format) -> Self
Build an image from any pixel producer.
Sourcepub fn metadata(&self) -> Result<Metadata>
pub fn metadata(&self) -> Result<Metadata>
Header-only facts about this image: dimensions, format, pixel format.
Free — descriptors are resolved as the graph is built, so this decodes nothing (SPEC §Guarantees 3).
§Errors
Returns the first error captured while building the pipeline, if any.
Sourcepub fn descriptor(&self) -> Result<ImageDescriptor>
pub fn descriptor(&self) -> Result<ImageDescriptor>
The shape of this image at this point in the pipeline.
§Errors
Returns the first error captured while building the pipeline, if any.
Sourcepub fn crop(self, x: u32, y: u32, width: u32, height: u32) -> Self
pub fn crop(self, x: u32, y: u32, width: u32, height: u32) -> Self
Extract the rectangular window at (x, y) of size width × height.
A window outside the image is an error, surfaced at the terminal.
Sourcepub fn resize(self, width: u32, height: u32) -> Self
pub fn resize(self, width: u32, height: u32) -> Self
Resample to width by height with the default filter (Lanczos3).
Sourcepub fn resize_with(
self,
width: u32,
height: u32,
options: ResizeOptions,
) -> Self
pub fn resize_with( self, width: u32, height: u32, options: ResizeOptions, ) -> Self
Resample to width by height with explicit options.
Sourcepub fn thumbnail(self, width: u32, height: u32) -> Self
pub fn thumbnail(self, width: u32, height: u32) -> Self
Scale to fit inside width by height, preserving aspect ratio.
Sourcepub fn orient(self, orientation: Orientation) -> Self
pub fn orient(self, orientation: Orientation) -> Self
Apply orientation: the stored image becomes the upright one.
Image::open and Image::from_stream already do this with the
orientation the file declares; this is for pixels opened with
auto_orient off or through Image::from_decoder. It is a quarter
turn and a mirror at most, and both rescale, so an oriented JPEG
keeps its shrink-on-load fast path.
Sourcepub fn extract_channel(self, index: usize) -> Self
pub fn extract_channel(self, index: usize) -> Self
Extract one channel as a greyscale image.
Sourcepub fn flatten(self, red: u8, green: u8, blue: u8) -> Self
pub fn flatten(self, red: u8, green: u8, blue: u8) -> Self
Composite this image against an opaque background, discarding alpha.
Sourcepub fn composite(self, overlay: Self, x: i64, y: i64) -> Self
pub fn composite(self, overlay: Self, x: i64, y: i64) -> Self
Draw overlay over this image at (x, y).
This is the join point for two branches of a graph: both pipelines stay lazy, and neither is evaluated until a terminal pulls on the result.
Sourcepub fn composite_with(self, overlay: Self, x: i64, y: i64, blend: Blend) -> Self
pub fn composite_with(self, overlay: Self, x: i64, y: i64, blend: Blend) -> Self
Draw overlay over this image with an explicit blend mode.
Sourcepub fn animation(&self) -> Option<&Animation>
pub fn animation(&self) -> Option<&Animation>
The source file’s animation, if it has more than one frame.
The pipeline processes the first frame, so this describes what the
file holds rather than what will be written: frame count, loop count
and per-frame durations, for a caller to decide whether a still is
what it wants (see OpenOptions::animated).
Sourcepub fn icc_profile(&self) -> Option<&[u8]>
pub fn icc_profile(&self) -> Option<&[u8]>
The ICC profile this image’s pixels are in, if it is not sRGB.
A file’s embedded profile, unless the pixels were converted to sRGB
on open (see OpenOptions). It is written into the output where
the format has a place for one, so colours survive the round trip.
Sourcepub fn to_srgb(self) -> Self
pub fn to_srgb(self) -> Self
Convert the pixels from their ICC profile’s colour space to sRGB, and drop the profile.
Image::open already does this unless told not to
(OpenOptions::to_srgb). Matrix/TRC RGB and grey profiles convert,
relative colorimetric with out-of-gamut colours clipped, as lcms2
does; a profile that is sRGB in all but name is just dropped. Any
other profile (LUT-based, CMYK, one that does not match the pixels)
is kept, unconverted, so the output still carries it.
Sourcepub fn to_pixel_format(self, pixel: PixelFormat) -> Self
pub fn to_pixel_format(self, pixel: PixelFormat) -> Self
Convert to pixel: depth (8-bit, 16-bit, float) and layout (grey,
grey with alpha, RGB, RGBA). Grey widens to RGB by repetition and RGB
narrows to grey by BT.601 luma; alpha is added opaque or dropped
(use Image::flatten to composite against a colour instead).
Outputs need not ask for this: Image::output narrows to what the
format holds by itself.
Sourcepub fn with_icc_profile(self, profile: Option<Vec<u8>>) -> Self
pub fn with_icc_profile(self, profile: Option<Vec<u8>>) -> Self
Declare the ICC profile the pixels are in, or with None drop it and
call them sRGB. Only the label changes, never a pixel.
Sourcepub fn apply(self, op: Arc<dyn Op>) -> Self
pub fn apply(self, op: Arc<dyn Op>) -> Self
Chain an arbitrary op onto this pipeline.
The escape hatch for ops defined outside this crate. Errors are deferred to the terminal, like every other chaining method.
Sourcepub fn output(self, format: Format, options: EncodeOptions) -> Output
pub fn output(self, format: Format, options: EncodeOptions) -> Output
Choose the encoder and options for this pipeline’s output.
This is the single encode terminal, with format as data (ADR-0006):
requesting a format that is not yet implemented is a catchable
PixelsError::Unsupported, not a compile error.