Skip to main content

Image

Struct Image 

Source
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

Source

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.

Source

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.

Source

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.

Source

pub fn open_with(path: impl AsRef<Path>, options: OpenOptions) -> Result<Self>

Open an image file with explicit OpenOptions.

§Errors

As Image::open.

Source

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.

Source

pub fn from_stream_with( source: impl Source + Debug + 'static, options: OpenOptions, ) -> Result<Self>

Build an image from a byte stream with explicit OpenOptions.

§Errors

As Image::from_stream.

Source

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.

Source

pub fn from_producer(producer: Arc<dyn Producer>, format: Format) -> Self

Build an image from any pixel producer.

Source

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.

Source

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.

Source

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.

Source

pub fn flip(self) -> Self

Mirror vertically: the top row becomes the bottom row.

Source

pub fn flop(self) -> Self

Mirror horizontally: the left column becomes the right column.

Source

pub fn resize(self, width: u32, height: u32) -> Self

Resample to width by height with the default filter (Lanczos3).

Source

pub fn resize_with( self, width: u32, height: u32, options: ResizeOptions, ) -> Self

Resample to width by height with explicit options.

Source

pub fn thumbnail(self, width: u32, height: u32) -> Self

Scale to fit inside width by height, preserving aspect ratio.

Source

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.

Source

pub fn rotate(self, degrees: i32) -> Self

Rotate by degrees, which must be a multiple of 90.

Source

pub fn modulate(self, options: Modulate) -> Self

Adjust brightness, saturation and hue.

Source

pub fn convolve(self, kernel: Kernel) -> Self

Convolve with kernel.

Source

pub fn blur(self, sigma: f32) -> Self

Blur with a Gaussian of the given sigma.

Source

pub fn sharpen(self, amount: f32) -> Self

Sharpen by amount, a 3x3 unsharp-style kernel.

Source

pub fn extract_channel(self, index: usize) -> Self

Extract one channel as a greyscale image.

Source

pub fn flatten(self, red: u8, green: u8, blue: u8) -> Self

Composite this image against an opaque background, discarding alpha.

Source

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.

Source

pub fn composite_with(self, overlay: Self, x: i64, y: i64, blend: Blend) -> Self

Draw overlay over this image with an explicit blend mode.

Source

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).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for Image

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Image

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Image

§

impl !UnwindSafe for Image

§

impl Freeze for Image

§

impl Send for Image

§

impl Sync for Image

§

impl Unpin for Image

§

impl UnsafeUnpin for Image

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.