Skip to main content

AsepriteFile

Struct AsepriteFile 

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

An Aseprite .ase/.aseprite file.

§Creating a file from scratch

use aseprite::*;

let mut file = AsepriteFile::new(32, 32, ColorMode::Rgba);
let layer = file.add_layer("Background");
let frame = file.add_frame(100);
let pixels = Pixels::new(vec![0u8; 32 * 32 * 4], 32, 32, ColorMode::Rgba).unwrap();
file.set_cel(layer, frame, pixels, 0, 0).unwrap();

§Reading and writing

use aseprite::AsepriteFile;

let data = std::fs::read("sprite.aseprite").unwrap();
let file = AsepriteFile::from_reader(&data[..]).unwrap();
let mut output = Vec::new();
file.write_to(&mut output).unwrap();

Implementations§

Source§

impl AsepriteFile

Source

pub fn new(width: u16, height: u16, color_mode: ColorMode) -> Self

Creates an empty file with the given canvas dimensions and color mode.

Source

pub fn width(&self) -> u16

Returns the canvas width in pixels.

Source

pub fn height(&self) -> u16

Returns the canvas height in pixels.

Source

pub fn color_mode(&self) -> ColorMode

Returns the color mode.

Source

pub fn flags(&self) -> u32

Returns the raw header flags.

Source

pub fn pixel_ratio(&self) -> (u8, u8)

Returns the pixel aspect ratio as (width, height).

Source

pub fn grid(&self) -> &GridInfo

Returns the grid overlay settings.

Source

pub fn color_profile(&self) -> &Option<ColorProfile>

Returns the color profile, if set.

Source

pub fn palette(&self) -> &[Color]

Returns the color palette (empty if no palette chunk was present).

Source

pub fn layers(&self) -> &[Layer]

Returns all layers in document order.

Source

pub fn frames(&self) -> &[Frame]

Returns all frames.

Source

pub fn tags(&self) -> &[Tag]

Returns all animation tags.

Source

pub fn slices(&self) -> &[Slice]

Returns all slices.

Source

pub fn sprite_user_data(&self) -> &Option<UserData>

Returns the sprite-level user data, if set.

Source

pub fn transparent_index(&self) -> u8

Returns the transparent palette index (only meaningful for indexed color mode).

Source

pub fn legacy_masks(&self) -> &[LegacyMask]

Returns legacy mask chunks (deprecated in modern Aseprite, preserved for round-trip fidelity).

Source

pub fn tilesets(&self) -> &[Tileset]

Returns all tilesets.

Source

pub fn external_files(&self) -> &[ExternalFile]

Returns all external file references.

Source

pub fn cel(&self, layer: LayerRef, frame: usize) -> Option<&Cel>

Returns the raw cel at the given layer and frame, or None. May return CelKind::Linked; use resolve_cel to follow links.

Source

pub fn resolve_cel(&self, layer: LayerRef, frame: usize) -> Option<&Cel>

Returns the cel at the given layer and frame, following linked cels.

Unlike cel(), this never returns CelKind::Linked. Returns None if the cel doesn’t exist or the link target is missing.

Source

pub fn layer_ref(&self, index: usize) -> Option<LayerRef>

Returns a LayerRef handle for the given index, or None if the index is out of bounds or points to a group.

Source

pub fn group_ref(&self, index: usize) -> Option<GroupRef>

Returns a GroupRef handle for the given index, or None if the index is out of bounds or is not a group.

Source

pub fn add_layer(&mut self, name: &str) -> LayerRef

Adds a normal layer at the top level.

Source

pub fn add_layer_with(&mut self, name: &str, opts: LayerOptions) -> LayerRef

Adds a normal layer at the top level with custom options.

Source

pub fn add_group(&mut self, name: &str) -> GroupRef

Adds a group layer at the top level.

Source

pub fn add_group_with(&mut self, name: &str, opts: LayerOptions) -> GroupRef

Adds a group layer at the top level with custom options.

Source

pub fn add_layer_in(&mut self, name: &str, parent: GroupRef) -> LayerRef

Adds a normal layer inside a group.

Source

pub fn add_layer_in_with( &mut self, name: &str, parent: GroupRef, opts: LayerOptions, ) -> LayerRef

Adds a normal layer inside a group with custom options.

Source

pub fn add_group_in(&mut self, name: &str, parent: GroupRef) -> GroupRef

Adds a nested group inside a parent group.

Source

pub fn add_group_in_with( &mut self, name: &str, parent: GroupRef, opts: LayerOptions, ) -> GroupRef

Adds a nested group inside a parent group with custom options.

Source

pub fn add_tilemap_layer(&mut self, name: &str, tileset_index: u32) -> LayerRef

Adds a tilemap layer referencing the given tileset index.

Source

pub fn set_tilemap_cel( &mut self, layer: LayerRef, frame: usize, tiles: Vec<u32>, width: u16, height: u16, x: i16, y: i16, ) -> Result<(), AsepriteError>

Sets tilemap data for a tilemap layer/frame.

Source

pub fn add_frame(&mut self, duration_ms: u16) -> usize

Adds a frame with the given duration in milliseconds. Returns the frame index.

Source

pub fn set_cel( &mut self, layer: LayerRef, frame: usize, pixels: Pixels, x: i16, y: i16, ) -> Result<(), AsepriteError>

Sets compressed pixel data for a layer/frame. Returns an error if the frame index is out of bounds.

Source

pub fn set_cel_with( &mut self, layer: LayerRef, frame: usize, opts: CelOptions, ) -> Result<(), AsepriteError>

Sets pixel data for a layer/frame with custom opacity, position, and z-index.

Source

pub fn set_raw_cel( &mut self, layer: LayerRef, frame: usize, pixels: Pixels, x: i16, y: i16, ) -> Result<(), AsepriteError>

Sets uncompressed pixel data for a layer/frame.

Source

pub fn set_linked_cel( &mut self, layer: LayerRef, frame: usize, source_frame: usize, ) -> Result<(), AsepriteError>

Sets a linked cel pointing to another frame’s cel. Returns an error if either frame index is out of bounds.

Source

pub fn add_tag( &mut self, name: &str, frames: RangeInclusive<usize>, direction: LoopDirection, ) -> Result<usize, AsepriteError>

Adds an animation tag spanning the given frame range.

Source

pub fn add_tag_with( &mut self, name: &str, frames: RangeInclusive<usize>, direction: LoopDirection, repeat: u16, ) -> Result<usize, AsepriteError>

Adds an animation tag with a custom repeat count.

Source

pub fn set_palette(&mut self, colors: &[Color]) -> Result<(), AsepriteError>

Sets the color palette. Returns an error if more than 256 entries.

Source

pub fn set_transparent_index(&mut self, index: u8)

Sets the transparent palette index.

Source

pub fn set_color_profile(&mut self, profile: ColorProfile)

Sets the color profile.

Source

pub fn add_slice(&mut self, slice: Slice)

Adds a slice.

Source

pub fn set_sprite_user_data(&mut self, ud: UserData)

Sets the sprite-level user data.

Source

pub fn add_tileset(&mut self, tileset: Tileset)

Adds a tileset definition.

Source

pub fn add_external_file(&mut self, ef: ExternalFile)

Adds an external file reference.

Source

pub fn set_layer_user_data(&mut self, layer: LayerRef, ud: UserData)

Sets user data on a layer.

Source

pub fn set_group_user_data(&mut self, group: GroupRef, ud: UserData)

Sets user data on a group layer.

Source

pub fn set_cel_user_data(&mut self, layer: LayerRef, frame: usize, ud: UserData)

Sets user data on a cel.

Source

pub fn set_cel_extra(&mut self, layer: LayerRef, frame: usize, extra: CelExtra)

Sets extra precision bounds on a cel.

Source

pub fn set_tag_user_data(&mut self, tag_index: usize, ud: UserData)

Sets user data on a tag.

Source§

impl AsepriteFile

Source

pub fn from_reader<R: Read>(r: R) -> Result<Self, AsepriteError>

Parses an Aseprite file from any reader.

Source

pub fn write_to<W: Write>(&self, w: W) -> Result<(), AsepriteError>

Writes the file in Aseprite binary format to any writer.

Trait Implementations§

Source§

impl Clone for AsepriteFile

Source§

fn clone(&self) -> AsepriteFile

Returns a duplicate 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 Debug for AsepriteFile

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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

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.