Commit ac4fc176 authored by Jonas Platte's avatar Jonas Platte

Add clamping, padding

parent e9755068
[package]
name = "texpackr"
version = "0.1.0"
authors = [
"Philipp Mandler <info@philipp-mandler.com>",
"Jonas Platte <jplatte+info@posteo.de>",
]
name = "texpackr"
version = "0.1.0"
[[bin]]
name = "texpackr"
path = "src/bin/main.rs"
required-features = ["binary"]
[dependencies]
gen-iter = { git = "https://gitlab.com/detox/gen-iter.git" }
image = "0.18.0"
serde = "1.0.33"
serde_derive = "1.0.33"
serde_json = "1.0.11"
image = "0.19.0"
serde = "1.0.77"
serde_derive = "1.0.77"
serde_json = "1.0.27"
[dependencies.gen-iter]
git = "https://gitlab.com/detox/gen-iter.git"
[dependencies.toml]
optional = true
version = "0.4.5"
version = "0.4.6"
[features]
binary = ["toml"]
[[bin]]
name = "texpackr"
path = "src/bin/main.rs"
required-features = ["binary"]
......@@ -9,21 +9,25 @@ extern crate serde_json;
mod patch_node;
use std::{
collections::HashMap,
ffi::OsStr,
fs::{self, File},
io::Write,
path::{Path, PathBuf},
collections::HashMap,
ffi::OsStr,
};
use image::{imageops, GenericImage, ImageBuffer};
use image::{imageops, GenericImage, ImageBuffer, SubImage};
use crate::patch_node::{Patch, PatchNode};
use crate::patch_node::PatchNode;
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct Config {
pub width: u32,
pub height: u32,
#[serde(default)]
pub padding: u32,
#[serde(default)]
pub clamping: u32,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
......@@ -50,6 +54,8 @@ pub fn create_atlas(
let files = add_dir(asset_path, &path, atlas_id, &config);
let total_padding = config.clamping + config.padding;
// collect image data
let mut image_files = Vec::new();
for name in files {
......@@ -68,25 +74,67 @@ pub fn create_atlas(
let mut tree = PatchNode::new(config.width, config.height);
for (name, size) in image_files {
tree.insert(size.0, size.1, name)
tree.insert(size.0 + 2 * total_padding, size.1 + 2 * total_padding, name)
.expect("Not enough space in atlas!");
}
let mut buf = ImageBuffer::new(config.width, config.height);
let store_map = tree.into_iter()
let store_map = tree
.into_iter()
.map(|patch| {
let Patch { pos, size, name } = patch;
let pos = (patch.pos.0 + total_padding, patch.pos.1 + total_padding);
let size = (
patch.size.0 - 2 * total_padding,
patch.size.1 - 2 * total_padding,
);
let name = patch.name;
let img_path = path.join(&name);
let img = image::open(&img_path).expect(&format!("Couldn't open {:?}", img_path));
imageops::replace(&mut buf, &img.to_rgba(), pos.0, pos.1);
let img = image::open(&img_path)
.expect(&format!("Couldn't open {:?}", img_path))
.to_rgba();
imageops::replace(&mut buf, &img, pos.0, pos.1);
if config.clamping > 0 {
let pixel_top_left = *buf.get_pixel(pos.0, pos.1);
let pixel_top_right = *buf.get_pixel(pos.0 + size.0 - 1, pos.1);
let pixel_bottom_left = *buf.get_pixel(pos.0, pos.1 + size.1 - 1);
let pixel_bottom_right = *buf.get_pixel(pos.0 + size.0 - 1, pos.1 + size.1 - 1);
for i in 1..=(config.clamping) {
// Sides
let row_top = SubImage::new(&mut buf, pos.0, pos.1, size.0, 1).to_image();
let row_bottom =
SubImage::new(&mut buf, pos.0, pos.1 + size.1 - 1, size.0, 1).to_image();
let col_left = SubImage::new(&mut buf, pos.0, pos.1, 1, size.1).to_image();
let col_right =
SubImage::new(&mut buf, pos.0 + size.0 - 1, pos.1, 1, size.1).to_image();
imageops::replace(&mut buf, &row_top, pos.0, pos.1 - i);
imageops::replace(&mut buf, &row_bottom, pos.0, pos.1 + size.1 - 1 + i);
imageops::replace(&mut buf, &col_left, pos.0 - i, pos.1);
imageops::replace(&mut buf, &col_right, pos.0 + size.1 - 1 + i, pos.1);
// Corners
for j in 1..=(config.clamping) {
buf.put_pixel(pos.0 - i, pos.1 - j, pixel_top_left);
buf.put_pixel(pos.0 + size.0 - 1 + i, pos.1 - j, pixel_top_right);
buf.put_pixel(pos.0 - i, pos.1 + size.1 - 1 + j, pixel_bottom_left);
buf.put_pixel(
pos.0 + size.0 - 1 + i,
pos.1 + size.1 - 1 + j,
pixel_bottom_right,
);
}
}
}
(
name.with_extension("").to_str().unwrap().to_owned(),
TileData { pos, size },
)
})
.collect::<HashMap<_, _>>();
}).collect::<HashMap<_, _>>();
// save atlas
buf.save(&dest_path.join(atlas_id).with_extension("png"))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment