pub struct ImageSurface<T> {
    surface: ImageSurface,
    data_ptr: NonNull<u8>,
    width: i32,
    height: i32,
    stride: isize,
    surface_type: SurfaceType,
    _state: PhantomData<T>,
}
Expand description

Wrapper for a Cairo image surface that enforces exclusive access when modifying it.

Shared access to cairo::ImageSurface is tricky since a read-only borrowed reference can still be cloned and then modified. We can’t simply use cairo::ImageSurface::data() because in the filter code we have surfaces referenced from multiple places and it would probably add more complexity to remove that and start passing around references.

This wrapper asserts the uniqueness of its image surface.

It uses the typestate pattern to ensure that the surface can be modified only when it is in the Exclusive state, while in the Shared state it only allows read-only access.

Fields§

§surface: ImageSurface§data_ptr: NonNull<u8>§width: i32§height: i32§stride: isize§surface_type: SurfaceType§_state: PhantomData<T>

Implementations§

source§

impl<T> ImageSurface<T>

source

pub fn width(&self) -> i32

Returns the surface width.

source

pub fn height(&self) -> i32

Returns the surface height.

source

pub fn stride(&self) -> isize

Returns the surface stride.

source§

impl ImageSurface<Shared>

source

pub fn wrap( surface: ImageSurface, surface_type: SurfaceType ) -> Result<SharedImageSurface, Error>

Creates a SharedImageSurface from a unique cairo::ImageSurface.

§Panics

Panics if the surface format isn’t ARgb32 and if the surface is not unique, that is, its reference count isn’t 1.

source

pub fn copy_from_surface(surface: &ImageSurface) -> Result<Self, Error>

Creates a SharedImageSurface copying from a cairo::ImageSurface, even if it does not have a reference count of 1.

source

pub fn empty( width: i32, height: i32, surface_type: SurfaceType ) -> Result<Self, Error>

Creates an empty SharedImageSurface of the given size and type.

source

pub fn into_image_surface(self) -> Result<ImageSurface, Error>

Converts this SharedImageSurface back into a Cairo image surface.

source

pub fn from_image( image: &DynamicImage, content_type: Option<&str>, mime_data: Option<Vec<u8>> ) -> Result<SharedImageSurface, Error>

source

fn is_alpha_only(&self) -> bool

Returns true if the surface contains meaningful data only in the alpha channel.

source

pub fn surface_type(&self) -> SurfaceType

Returns the type of this surface.

source

pub fn get_pixel(&self, x: u32, y: u32) -> Pixel

Retrieves the pixel value at the given coordinates.

source

pub fn get_pixel_by_offset(&self, offset: isize) -> Pixel

Retrieves the pixel value by offset into the pixel data array.

source

pub fn set_as_source_surface( &self, cr: &Context, x: f64, y: f64 ) -> Result<(), Error>

Calls set_source_surface() on the given Cairo context.

source

pub fn to_cairo_pattern(&self) -> SurfacePattern

Creates a Cairo surface pattern from the surface

source

fn copy_surface(&self, bounds: IRect) -> Result<ImageSurface, Error>

Returns a new cairo::ImageSurface with the same contents as the one stored in this SharedImageSurface within the given bounds.

source

pub fn scale_to( &self, width: i32, height: i32, bounds: IRect, x: f64, y: f64 ) -> Result<SharedImageSurface, Error>

Scales the given surface by x and y into a surface width×height in size, clipped by bounds.

source

pub fn scale( &self, bounds: IRect, x: f64, y: f64 ) -> Result<(SharedImageSurface, IRect), Error>

Returns a scaled version of a surface and bounds.

source

pub fn extract_alpha(&self, bounds: IRect) -> Result<SharedImageSurface, Error>

Returns a surface with black background and alpha channel matching this surface.

source

pub fn to_luminance_mask(&self) -> Result<SharedImageSurface, Error>

Returns a surface whose alpha channel for each pixel is equal to the luminance of that pixel’s unpremultiplied RGB values. The resulting surface’s RGB values are not meanignful; only the alpha channel has useful luminance data.

This is to get a mask suitable for use with cairo_mask_surface().

source

pub fn unpremultiply(&self, bounds: IRect) -> Result<SharedImageSurface, Error>

Returns a surface with pre-multiplication of color values undone.

HACK: this is storing unpremultiplied pixels in an ARGB32 image surface (which is supposed to be premultiplied pixels).

source

pub fn to_linear_rgb(&self, bounds: IRect) -> Result<SharedImageSurface, Error>

Converts the surface to the linear sRGB color space.

source

pub fn to_srgb(&self, bounds: IRect) -> Result<SharedImageSurface, Error>

Converts the surface to the sRGB color space.

source

pub fn convolve<R: Dim, C: Dim, S: Storage<f64, R, C>>( &self, bounds: IRect, target: (i32, i32), kernel: &Matrix<f64, R, C, S>, edge_mode: EdgeMode ) -> Result<SharedImageSurface, Error>

Performs a convolution.

Note that kernel is rotated 180 degrees.

The target parameter determines the position of the kernel relative to each pixel of the image. The value of (0, 0) indicates that the top left pixel of the (180-degrees-rotated) kernel corresponds to the current pixel, and the rest of the kernel is to the right and bottom of the pixel. The value of (cols / 2, rows / 2) centers a kernel with an odd number of rows and columns.

§Panics

Panics if kernel has zero rows or columns.

source

pub fn box_blur_loop<B: BlurDirection, A: IsAlphaOnly>( &self, output_surface: &mut ImageSurface, bounds: IRect, kernel_size: usize, target: usize )

Performs a horizontal or vertical box blur.

The target parameter determines the position of the kernel relative to each pixel of the image. The value of 0 indicates that the first pixel of the kernel corresponds to the current pixel, and the rest of the kernel is to the right or bottom of the pixel. The value of kernel_size / 2 centers a kernel with an odd size.

§Panics

Panics if kernel_size is 0 or if target >= kernel_size.

source

pub fn box_blur<B: BlurDirection>( &self, bounds: IRect, kernel_size: usize, target: usize ) -> Result<SharedImageSurface, Error>

Performs a horizontal or vertical box blur.

The target parameter determines the position of the kernel relative to each pixel of the image. The value of 0 indicates that the first pixel of the kernel corresponds to the current pixel, and the rest of the kernel is to the right or bottom of the pixel. The value of kernel_size / 2 centers a kernel with an odd size.

§Panics

Panics if kernel_size is 0 or if target >= kernel_size.

source

pub fn flood( &self, bounds: IRect, color: Color ) -> Result<SharedImageSurface, Error>

Fills the with a specified color.

source

pub fn offset( &self, bounds: Rect, dx: f64, dy: f64 ) -> Result<SharedImageSurface, Error>

Offsets the image of the specified amount.

source

pub fn paint_image( &self, bounds: Rect, image: &SharedImageSurface, rect: Option<Rect>, interpolation: Interpolation ) -> Result<SharedImageSurface, Error>

Returns a new surface of the same size, with the contents of the specified image, optionally transformed to match a given box

source

pub fn tile(&self, bounds: IRect) -> Result<SharedImageSurface, Error>

Creates a new surface with the size and content specified in bounds

§Panics

Panics if bounds is an empty rectangle, since SharedImageSurface cannot represent zero-sized images.

source

pub fn paint_image_tiled( &self, bounds: IRect, image: &SharedImageSurface, x: i32, y: i32 ) -> Result<SharedImageSurface, Error>

Returns a new surface of the same size, with the contents of the specified image repeated to fill the bounds and starting from the given position.

source

pub fn compose( &self, other: &SharedImageSurface, bounds: IRect, operator: Operator ) -> Result<SharedImageSurface, Error>

Performs the combination of two input surfaces using Porter-Duff compositing operators.

§Panics

Panics if the two surface types are not compatible.

source

pub fn compose_arithmetic( &self, other: &SharedImageSurface, bounds: IRect, k1: f64, k2: f64, k3: f64, k4: f64 ) -> Result<SharedImageSurface, Error>

Performs the combination of two input surfaces.

Each pixel of the resulting image is computed using the following formula: res = k1*i1*i2 + k2*i1 + k3*i2 + k4

§Panics

Panics if the two surface types are not compatible.

source

pub fn rows(&self) -> Rows<'_>

source§

impl ImageSurface<Exclusive>

source

pub fn new( width: i32, height: i32, surface_type: SurfaceType ) -> Result<ExclusiveImageSurface, Error>

source

pub fn share(self) -> Result<SharedImageSurface, Error>

source

pub fn data(&mut self) -> ImageSurfaceData<'_>

Raw access to the image data as a slice

source

pub fn modify( &mut self, draw_fn: &mut dyn FnMut(&mut ImageSurfaceData<'_>, usize) )

Modify the image data

source

pub fn draw( &mut self, draw_fn: &mut dyn FnMut(Context) -> Result<(), InternalRenderingError> ) -> Result<(), InternalRenderingError>

Draw on the surface using cairo

source

pub fn rows_mut(&mut self) -> RowsMut<'_>

Trait Implementations§

source§

impl<T: Clone> Clone for ImageSurface<T>

source§

fn clone(&self) -> ImageSurface<T>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for ImageSurface<T>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for ImageSurface<T>
where T: RefUnwindSafe,

§

impl<T> !Send for ImageSurface<T>

§

impl<T> !Sync for ImageSurface<T>

§

impl<T> Unpin for ImageSurface<T>
where T: Unpin,

§

impl<T> UnwindSafe for ImageSurface<T>
where T: UnwindSafe,

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> 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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

impl<T> Same for T

§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

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

§

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>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.