diff options
| author | Micha White <botahamec@outlook.com> | 2024-01-01 19:41:32 -0500 |
|---|---|---|
| committer | Micha White <botahamec@outlook.com> | 2024-01-01 19:41:32 -0500 |
| commit | 488c7ed94b0662222fa0d825ab81b60b0b1e5d6c (patch) | |
| tree | 83d7c6b2d7a0de9bedffbc5760396f965b6d10cb /packer | |
| parent | 82a737798b5694e353971a00f46d117743d2a03e (diff) | |
Created a texture packer
Diffstat (limited to 'packer')
| -rw-r--r-- | packer/Cargo.toml | 3 | ||||
| -rw-r--r-- | packer/packed.png | bin | 0 -> 944566 bytes | |||
| -rw-r--r-- | packer/src/bin/benchmark.rs | 26 | ||||
| -rw-r--r-- | packer/src/bin/res/bunny.ff | bin | 0 -> 8208 bytes | |||
| -rw-r--r-- | packer/src/bin/res/bunny.png | bin | 0 -> 438 bytes | |||
| -rw-r--r-- | packer/src/bin/res/gator.bmp | bin | 0 -> 750054 bytes | |||
| -rw-r--r-- | packer/src/bin/res/gator.ff | bin | 0 -> 2000016 bytes | |||
| -rw-r--r-- | packer/src/bin/res/ghost.ico | bin | 0 -> 67646 bytes | |||
| -rw-r--r-- | packer/src/lib.rs | 4 |
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 Binary files differnew file mode 100644 index 0000000..1a913a2 --- /dev/null +++ b/packer/packed.png 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 Binary files differnew file mode 100644 index 0000000..64c5a69 --- /dev/null +++ b/packer/src/bin/res/bunny.ff diff --git a/packer/src/bin/res/bunny.png b/packer/src/bin/res/bunny.png Binary files differnew file mode 100644 index 0000000..87ba72d --- /dev/null +++ b/packer/src/bin/res/bunny.png diff --git a/packer/src/bin/res/gator.bmp b/packer/src/bin/res/gator.bmp Binary files differnew file mode 100644 index 0000000..e752b56 --- /dev/null +++ b/packer/src/bin/res/gator.bmp diff --git a/packer/src/bin/res/gator.ff b/packer/src/bin/res/gator.ff Binary files differnew file mode 100644 index 0000000..aac1bcb --- /dev/null +++ b/packer/src/bin/res/gator.ff diff --git a/packer/src/bin/res/ghost.ico b/packer/src/bin/res/ghost.ico Binary files differnew file mode 100644 index 0000000..102de00 --- /dev/null +++ b/packer/src/bin/res/ghost.ico 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 { |
