Skip to main content

Image

Struct Image 

Source
pub struct Image { /* private fields */ }
Expand description

A PNG or JPEG image that goes where text goes.

An image goes in a state, a question, or an option, through format!, json!, or a struct. It displays and serializes as its reference: s2-img- and 16 hex digits of its SHA-256. ask finds each reference in the text values of the request, and sends the bytes of its image. A reference in an object key, such as the name of an option, stays text. A clone shares the bytes.

image! makes an image that the compiler checks. Image::new makes one from data. With the image feature, TryFrom converts an image::DynamicImage into an image, and an &Image into a DynamicImage.

An image keeps its original: the image that the caller gave, upright. With the image feature, DynamicImage::try_from(&image) gives the place of the image in its original, with all of its detail.

§Examples

use serde_json::json;
use system2::prelude::*;
use system2::Image;

let screenshot = Image::new(std::fs::read("screenshot.png")?)?;
let state = json!({ "screenshot": screenshot, "message": "The app shows an error." });
let answer = noul!("Does the screenshot show the error?").ask(&state).await?;
let answer = noul!("Is there a dialog in {screenshot}?").ask("A screenshot.").await?;

Implementations§

Source§

impl Image

Source

pub fn new(bytes: impl Into<Vec<u8>>) -> Result<Self, InvalidImage>

Makes an image from bytes, and validates it.

Without the image feature, the image must be PNG or JPEG, with no EXIF orientation that turns or mirrors it. Each side must be 280 to 1568 px, and the image must have 16 MiB or less. An image that passes keeps its bytes, with no copy.

The image feature also takes WebP, and the first frame of a GIF, of each size and orientation. It turns the pixels upright, and scales the image into the limits. A JPEG stays a JPEG, and each other format becomes a PNG. An image that passes without the feature does not change.

§Errors
  • InvalidImage::Format if the image is not PNG or JPEG, without the image feature.
  • InvalidImage::Header if the PNG or JPEG header is not complete, without the image feature.
  • InvalidImage::Size if a side is out of the limits, without the image feature. With the feature, if the long side is more than 5.6 times the short side.
  • InvalidImage::Bytes if the image has more than 16 MiB, without the image feature.
  • InvalidImage::Orientation if the EXIF orientation turns or mirrors the image, without the image feature.
  • InvalidImage::Reference if a different image has the same reference.
  • InvalidImage::Decode if the image needs a conversion, and does not decode, with the image feature. Also if its pixels need more than 512 MiB of memory. An image that passes without the feature is not decoded.
Source

pub fn crop(&self, region: PixelBox) -> Result<Image, InvalidImage>

Cuts a region from the original of the image, with all of its detail.

The region is in the pixels of the original, as BboxAnswer::to_pixels gives it. The crop is a new image, which Image::new converts, and it shares the original. A BboxAnswer on the crop also gives the pixels of the original. crop decodes the original each time.

§Errors
  • InvalidImage::Region if the region is not in the original, or has no area. Also if its long side is more than 5.6 times its short side.
  • InvalidImage::Decode if the original does not decode, or the crop does not encode. Also if the pixels of the original need more than 512 MiB of memory.
Source

pub fn width(&self) -> u32

Returns the width that the API sees, in pixels.

Source

pub fn height(&self) -> u32

Returns the height that the API sees, in pixels.

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, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Image

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Serialize for Image

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl TryFrom<&Image> for DynamicImage

Available on crate feature image only.
Source§

type Error = InvalidImage

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

fn try_from(image: &Image) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<DynamicImage> for Image

Available on crate feature image only.
Source§

type Error = InvalidImage

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

fn try_from(dynamic_image: DynamicImage) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl Forget for Image

§

impl Freeze for Image

§

impl Move for Image

§

impl RefUnwindSafe for Image

§

impl Send for Image

§

impl Sync for Image

§

impl Unpin for Image

§

impl UnsafeUnpin for Image

§

impl UnwindSafe 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more