summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--packer/Cargo.toml3
-rw-r--r--packer/packed.pngbin0 -> 944566 bytes
-rw-r--r--packer/src/bin/benchmark.rs26
-rw-r--r--packer/src/bin/res/bunny.ffbin0 -> 8208 bytes
-rw-r--r--packer/src/bin/res/bunny.pngbin0 -> 438 bytes
-rw-r--r--packer/src/bin/res/gator.bmpbin0 -> 750054 bytes
-rw-r--r--packer/src/bin/res/gator.ffbin0 -> 2000016 bytes
-rw-r--r--packer/src/bin/res/ghost.icobin0 -> 67646 bytes
-rw-r--r--packer/src/lib.rs4
9 files changed, 32 insertions, 1 deletions
diff --git a/packer/Cargo.toml b/packer/Cargo.toml
index d370392..0b1beb8 100644
--- a/packer/Cargo.toml
+++ b/packer/Cargo.toml
@@ -8,3 +8,6 @@ edition = "2021"
[dependencies]
image = "0.24"
exun = "0.2"
+
+[[bin]]
+name = "benchmark"
diff --git a/packer/packed.png b/packer/packed.png
new file mode 100644
index 0000000..1a913a2
--- /dev/null
+++ b/packer/packed.png
Binary files differ
diff --git a/packer/src/bin/benchmark.rs b/packer/src/bin/benchmark.rs
new file mode 100644
index 0000000..bb83992
--- /dev/null
+++ b/packer/src/bin/benchmark.rs
@@ -0,0 +1,26 @@
+use std::{fs::File, sync::Arc, time::Instant};
+
+use image::{io::Reader as ImageReader, ImageOutputFormat};
+use packer::RectanglePacker;
+
+fn main() -> Result<(), exun::RawUnexpected> {
+ let img1 = ImageReader::open("src/bin/res/gator.bmp")?.decode()?;
+ let img2 = ImageReader::open("src/bin/res/bunny.ff")?.decode()?;
+ let img3 = ImageReader::open("src/bin/res/ghost.ico")?.decode()?;
+
+ let start = Instant::now();
+ let mut packer = RectanglePacker::new();
+ packer.add_texture("gator".into(), Arc::new(img1.to_rgb8()));
+ packer.add_texture("bunny".into(), Arc::new(img2.to_rgb8()));
+ packer.add_texture("ghost".into(), Arc::new(img3.to_rgb8()));
+ println!("{} milliseconds", start.elapsed().as_secs_f32() * 1000.0);
+
+ let start = Instant::now();
+ let packed = packer.output();
+ println!("{} milliseconds", start.elapsed().as_secs_f32() * 1000.0);
+
+ let mut file = File::create("packed.png")?;
+ packed?.0.write_to(&mut file, ImageOutputFormat::Bmp)?;
+
+ Ok(())
+}
diff --git a/packer/src/bin/res/bunny.ff b/packer/src/bin/res/bunny.ff
new file mode 100644
index 0000000..64c5a69
--- /dev/null
+++ b/packer/src/bin/res/bunny.ff
Binary files differ
diff --git a/packer/src/bin/res/bunny.png b/packer/src/bin/res/bunny.png
new file mode 100644
index 0000000..87ba72d
--- /dev/null
+++ b/packer/src/bin/res/bunny.png
Binary files differ
diff --git a/packer/src/bin/res/gator.bmp b/packer/src/bin/res/gator.bmp
new file mode 100644
index 0000000..e752b56
--- /dev/null
+++ b/packer/src/bin/res/gator.bmp
Binary files differ
diff --git a/packer/src/bin/res/gator.ff b/packer/src/bin/res/gator.ff
new file mode 100644
index 0000000..aac1bcb
--- /dev/null
+++ b/packer/src/bin/res/gator.ff
Binary files differ
diff --git a/packer/src/bin/res/ghost.ico b/packer/src/bin/res/ghost.ico
new file mode 100644
index 0000000..102de00
--- /dev/null
+++ b/packer/src/bin/res/ghost.ico
Binary files differ
diff --git a/packer/src/lib.rs b/packer/src/lib.rs
index 06c66ae..328f90e 100644
--- a/packer/src/lib.rs
+++ b/packer/src/lib.rs
@@ -1,5 +1,6 @@
+use std::collections::HashMap;
+use std::ops::Deref;
use std::sync::Arc;
-use std::{collections::HashMap, ops::Deref};
use exun::RawUnexpected;
use image::{GenericImage, RgbImage};
@@ -92,6 +93,7 @@ impl RectanglePacker {
let mut rectangles = Vec::with_capacity(self.textures.len());
self.textures.sort();
+ self.textures.reverse();
for texture in &self.textures {
// loop to the next row if we've gone off the edge
if (x_position + texture.0.width()) > image_width {