Struct TexturePacker

Source
pub struct TexturePacker<'a, T: 'a + Clone, K: Clone + Eq + Hash> { /* private fields */ }
Expand description

Packs textures into a single texture atlas.

Implementations§

Source§

impl<'a, Pix: Pixel, T: 'a + Clone + Texture<Pixel = Pix>, K: Clone + Eq + Hash> TexturePacker<'a, T, K>

Source

pub fn new_skyline(config: TexturePackerConfig) -> Self

Create a new packer using the skyline packing algorithm.

Examples found in repository?
examples/packer-test.rs (line 31)
13fn main() {
14    fs::create_dir_all("target/output").unwrap();
15
16    //
17    // Perform texture packing
18    //
19    let config = TexturePackerConfig {
20        max_width: 400,
21        max_height: 400,
22        allow_rotation: false,
23        texture_outlines: true,
24        border_padding: 2,
25        force_max_dimensions: false,
26        ..Default::default()
27    };
28
29    // single atlas
30    {
31        let mut packer = TexturePacker::new_skyline(config);
32
33        for i in 1..11 {
34            let name = format!("{}.png", i);
35            let path = format!("examples/assets/{}", name);
36            let path = Path::new(&path);
37            let texture = ImageImporter::import_from_file(path)
38                .expect("Unable to import file. Run this example with --features=\"png\"");
39
40            packer.pack_own(name, texture).unwrap();
41        }
42
43        //
44        // Print the information
45        //
46        println!("Dimensions : {}x{}", packer.width(), packer.height());
47        for (name, frame) in packer.get_frames() {
48            println!("  {:7} : {:?}", name, frame.frame);
49        }
50
51        //
52        // Save the result
53        //
54        let exporter = ImageExporter::export(&packer, None).unwrap();
55        let mut file = File::create("target/output/skyline-packer-output.png").unwrap();
56        exporter
57            .write_to(&mut file, image::ImageFormat::Png)
58            .unwrap();
59
60        println!("Output texture stored in {:?}", file);
61    }
62
63    // multiple atlases
64    {
65        let mut packer = MultiTexturePacker::new_skyline(config);
66
67        for i in 1..11 {
68            let name = format!("{}.png", i);
69            let path = format!("examples/assets/{}", name);
70            let path = Path::new(&path);
71            let texture = ImageImporter::import_from_file(path).unwrap();
72
73            packer.pack_own(format!("A{}", i), texture.clone()).unwrap();
74            packer.pack_own(format!("B{}", i), texture).unwrap();
75        }
76
77        for (i, page) in packer.get_pages().iter().enumerate() {
78            //
79            // Print the information
80            //
81            println!("#{} | Dimensions : {}x{}", i, page.width(), page.height());
82            for (name, frame) in page.get_frames() {
83                println!("#{} |   {:7} : {:?}", i, name, frame.frame);
84            }
85
86            //
87            // Save the result
88            //
89            let exporter = ImageExporter::export(page, None).unwrap();
90            let mut file = File::create(&format!(
91                "target/output/skyline-multi-packer-output-{}.png",
92                i
93            ))
94            .unwrap();
95            exporter
96                .write_to(&mut file, image::ImageFormat::Png)
97                .unwrap();
98
99            println!("Multi output texture stored in {:?}", file);
100        }
101    }
102}
Source§

impl<'a, Pix: Pixel, T: Clone + Texture<Pixel = Pix>, K: Clone + Eq + Hash> TexturePacker<'a, T, K>

Source

pub fn can_pack(&self, texture: &'a T) -> bool

Check if the texture can be packed into this packer.

Source

pub fn pack_ref(&mut self, key: K, texture: &'a T) -> Result<(), PackError>

Pack the texture into this packer, taking a reference of the texture object.

Source

pub fn pack_own(&mut self, key: K, texture: T) -> Result<(), PackError>

Pack the texture into this packer, taking ownership of the texture object.

Examples found in repository?
examples/packer-test.rs (line 40)
13fn main() {
14    fs::create_dir_all("target/output").unwrap();
15
16    //
17    // Perform texture packing
18    //
19    let config = TexturePackerConfig {
20        max_width: 400,
21        max_height: 400,
22        allow_rotation: false,
23        texture_outlines: true,
24        border_padding: 2,
25        force_max_dimensions: false,
26        ..Default::default()
27    };
28
29    // single atlas
30    {
31        let mut packer = TexturePacker::new_skyline(config);
32
33        for i in 1..11 {
34            let name = format!("{}.png", i);
35            let path = format!("examples/assets/{}", name);
36            let path = Path::new(&path);
37            let texture = ImageImporter::import_from_file(path)
38                .expect("Unable to import file. Run this example with --features=\"png\"");
39
40            packer.pack_own(name, texture).unwrap();
41        }
42
43        //
44        // Print the information
45        //
46        println!("Dimensions : {}x{}", packer.width(), packer.height());
47        for (name, frame) in packer.get_frames() {
48            println!("  {:7} : {:?}", name, frame.frame);
49        }
50
51        //
52        // Save the result
53        //
54        let exporter = ImageExporter::export(&packer, None).unwrap();
55        let mut file = File::create("target/output/skyline-packer-output.png").unwrap();
56        exporter
57            .write_to(&mut file, image::ImageFormat::Png)
58            .unwrap();
59
60        println!("Output texture stored in {:?}", file);
61    }
62
63    // multiple atlases
64    {
65        let mut packer = MultiTexturePacker::new_skyline(config);
66
67        for i in 1..11 {
68            let name = format!("{}.png", i);
69            let path = format!("examples/assets/{}", name);
70            let path = Path::new(&path);
71            let texture = ImageImporter::import_from_file(path).unwrap();
72
73            packer.pack_own(format!("A{}", i), texture.clone()).unwrap();
74            packer.pack_own(format!("B{}", i), texture).unwrap();
75        }
76
77        for (i, page) in packer.get_pages().iter().enumerate() {
78            //
79            // Print the information
80            //
81            println!("#{} | Dimensions : {}x{}", i, page.width(), page.height());
82            for (name, frame) in page.get_frames() {
83                println!("#{} |   {:7} : {:?}", i, name, frame.frame);
84            }
85
86            //
87            // Save the result
88            //
89            let exporter = ImageExporter::export(page, None).unwrap();
90            let mut file = File::create(&format!(
91                "target/output/skyline-multi-packer-output-{}.png",
92                i
93            ))
94            .unwrap();
95            exporter
96                .write_to(&mut file, image::ImageFormat::Png)
97                .unwrap();
98
99            println!("Multi output texture stored in {:?}", file);
100        }
101    }
102}
Source

pub fn get_frames(&self) -> &HashMap<K, Frame<K>>

Get the backing mapping from strings to frames.

Examples found in repository?
examples/packer-test.rs (line 47)
13fn main() {
14    fs::create_dir_all("target/output").unwrap();
15
16    //
17    // Perform texture packing
18    //
19    let config = TexturePackerConfig {
20        max_width: 400,
21        max_height: 400,
22        allow_rotation: false,
23        texture_outlines: true,
24        border_padding: 2,
25        force_max_dimensions: false,
26        ..Default::default()
27    };
28
29    // single atlas
30    {
31        let mut packer = TexturePacker::new_skyline(config);
32
33        for i in 1..11 {
34            let name = format!("{}.png", i);
35            let path = format!("examples/assets/{}", name);
36            let path = Path::new(&path);
37            let texture = ImageImporter::import_from_file(path)
38                .expect("Unable to import file. Run this example with --features=\"png\"");
39
40            packer.pack_own(name, texture).unwrap();
41        }
42
43        //
44        // Print the information
45        //
46        println!("Dimensions : {}x{}", packer.width(), packer.height());
47        for (name, frame) in packer.get_frames() {
48            println!("  {:7} : {:?}", name, frame.frame);
49        }
50
51        //
52        // Save the result
53        //
54        let exporter = ImageExporter::export(&packer, None).unwrap();
55        let mut file = File::create("target/output/skyline-packer-output.png").unwrap();
56        exporter
57            .write_to(&mut file, image::ImageFormat::Png)
58            .unwrap();
59
60        println!("Output texture stored in {:?}", file);
61    }
62
63    // multiple atlases
64    {
65        let mut packer = MultiTexturePacker::new_skyline(config);
66
67        for i in 1..11 {
68            let name = format!("{}.png", i);
69            let path = format!("examples/assets/{}", name);
70            let path = Path::new(&path);
71            let texture = ImageImporter::import_from_file(path).unwrap();
72
73            packer.pack_own(format!("A{}", i), texture.clone()).unwrap();
74            packer.pack_own(format!("B{}", i), texture).unwrap();
75        }
76
77        for (i, page) in packer.get_pages().iter().enumerate() {
78            //
79            // Print the information
80            //
81            println!("#{} | Dimensions : {}x{}", i, page.width(), page.height());
82            for (name, frame) in page.get_frames() {
83                println!("#{} |   {:7} : {:?}", i, name, frame.frame);
84            }
85
86            //
87            // Save the result
88            //
89            let exporter = ImageExporter::export(page, None).unwrap();
90            let mut file = File::create(&format!(
91                "target/output/skyline-multi-packer-output-{}.png",
92                i
93            ))
94            .unwrap();
95            exporter
96                .write_to(&mut file, image::ImageFormat::Png)
97                .unwrap();
98
99            println!("Multi output texture stored in {:?}", file);
100        }
101    }
102}
Source

pub fn get_frame(&self, key: &K) -> Option<&Frame<K>>

Acquire a frame by its name.

Trait Implementations§

Source§

impl<'a, Pix, T, K: Clone + Eq + Hash> Texture for TexturePacker<'a, T, K>
where Pix: Pixel, T: Texture<Pixel = Pix> + Clone,

Source§

type Pixel = Pix

Pixel type of this texture.
Source§

fn width(&self) -> u32

Get the width of this texture.
Source§

fn height(&self) -> u32

Get the height of this texture.
Source§

fn get(&self, x: u32, y: u32) -> Option<Pix>

Get the pixel value at a specific coordinate.
Source§

fn set(&mut self, _x: u32, _y: u32, _val: Pix)

Set the pixel value at a specific coordinate.
Source§

fn get_rotated(&self, x: u32, y: u32) -> Option<Self::Pixel>

Get the pixel if it were transformed by a rotation.
Source§

fn is_column_transparent(&self, col: u32) -> bool

Check if a column of the texture is transparent.
Source§

fn is_row_transparent(&self, row: u32) -> bool

Check if a row of the texture is transparent.

Auto Trait Implementations§

§

impl<'a, T, K> Freeze for TexturePacker<'a, T, K>

§

impl<'a, T, K> !RefUnwindSafe for TexturePacker<'a, T, K>

§

impl<'a, T, K> !Send for TexturePacker<'a, T, K>

§

impl<'a, T, K> !Sync for TexturePacker<'a, T, K>

§

impl<'a, T, K> Unpin for TexturePacker<'a, T, K>
where K: Unpin, T: Unpin,

§

impl<'a, T, K> !UnwindSafe for TexturePacker<'a, T, K>

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