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>
impl<'a, Pix: Pixel, T: 'a + Clone + Texture<Pixel = Pix>, K: Clone + Eq + Hash> TexturePacker<'a, T, K>
Sourcepub fn new_skyline(config: TexturePackerConfig) -> Self
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>
impl<'a, Pix: Pixel, T: Clone + Texture<Pixel = Pix>, K: Clone + Eq + Hash> TexturePacker<'a, T, K>
Sourcepub fn can_pack(&self, texture: &'a T) -> bool
pub fn can_pack(&self, texture: &'a T) -> bool
Check if the texture can be packed into this packer.
Sourcepub fn pack_ref(&mut self, key: K, texture: &'a T) -> Result<(), PackError>
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.
Sourcepub fn pack_own(&mut self, key: K, texture: T) -> Result<(), PackError>
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}
Sourcepub fn get_frames(&self) -> &HashMap<K, Frame<K>>
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}
Trait Implementations§
Source§impl<'a, Pix, T, K: Clone + Eq + Hash> Texture for TexturePacker<'a, T, K>
impl<'a, Pix, T, K: Clone + Eq + Hash> Texture for TexturePacker<'a, T, K>
Source§fn get_rotated(&self, x: u32, y: u32) -> Option<Self::Pixel>
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
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
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>
impl<'a, T, K> !UnwindSafe for TexturePacker<'a, T, K>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more