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
impl Image
Sourcepub fn new(bytes: impl Into<Vec<u8>>) -> Result<Self, InvalidImage>
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::Formatif the image is not PNG or JPEG, without theimagefeature.InvalidImage::Headerif the PNG or JPEG header is not complete, without theimagefeature.InvalidImage::Sizeif a side is out of the limits, without theimagefeature. With the feature, if the long side is more than 5.6 times the short side.InvalidImage::Bytesif the image has more than 16 MiB, without theimagefeature.InvalidImage::Orientationif the EXIF orientation turns or mirrors the image, without theimagefeature.InvalidImage::Referenceif a different image has the same reference.InvalidImage::Decodeif the image needs a conversion, and does not decode, with theimagefeature. Also if its pixels need more than 512 MiB of memory. An image that passes without the feature is not decoded.
Sourcepub fn crop(&self, region: PixelBox) -> Result<Image, InvalidImage>
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::Regionif 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::Decodeif 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.
Trait Implementations§
Source§impl TryFrom<&Image> for DynamicImage
Available on crate feature image only.
impl TryFrom<&Image> for DynamicImage
image only.Source§impl TryFrom<DynamicImage> for Image
Available on crate feature image only.
impl TryFrom<DynamicImage> for Image
image only.