Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for rgba encoded heightmaps #7

Merged
merged 2 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ edition = "2018"
brs = "0.2.0"
image = "0.23.4"
clap = "2.33.0"
byteorder = "1.3.4"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Compile or download from releases.
FLAGS:
--cull Automatically remove bottom level bricks
-h, --help Prints help information
--hdmap Using a high detail rgb color encoded heightmap
--old Use old unoptimized heightmap code
--snap Snap bricks to the brick grid
--tile Render bricks as tiles
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fn main() {
(@arg snap: --snap "Snap bricks to the brick grid")
(@arg img: -i --img "Make the heightmap flat and render an image")
(@arg old: --old "Use old unoptimized heightmap code")
(@arg hdmap: --hdmap "Using a high detail rgb color encoded heightmap")
)
.get_matches();

Expand Down Expand Up @@ -61,6 +62,7 @@ fn main() {
tile: matches.is_present("tile"),
snap: matches.is_present("snap"),
img: matches.is_present("img"),
hdmap: matches.is_present("hdmap"),
};

println!("Reading image files");
Expand All @@ -87,7 +89,7 @@ fn main() {
if options.img {
Box::new(HeightmapFlat::new(colormap.size()).unwrap())
} else {
match HeightmapPNG::new(heightmap_files) {
match HeightmapPNG::new(heightmap_files, options.hdmap) {
Ok(map) => Box::new(map),
Err(error) => {
return println!("Error reading colormap: {:?}", error);
Expand Down
25 changes: 17 additions & 8 deletions src/map.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
extern crate image;
extern crate byteorder;

use image::RgbImage;
use image::{RgbaImage, RgbImage};
use byteorder::{ByteOrder, BigEndian};
use std::result::Result;

use crate::util::to_linear_rgb;
Expand All @@ -19,15 +21,22 @@ pub trait Colormap {

// PNG based heightmaps
pub struct HeightmapPNG {
maps: Vec<RgbImage>,
maps: Vec<RgbaImage>,
rgba_encoded: bool,
}

// Heightmap lookup
impl Heightmap for HeightmapPNG {
fn at(&self, x: u32, y: u32) -> u32 {
self.maps
if self.rgba_encoded {
self.maps
.iter()
.fold(0, |sum, m| sum + m.get_pixel(x, y as u32).0[0] as u32)
.fold(0, |sum, m| sum + BigEndian::read_u32(&m.get_pixel(x, y).0))
} else {
self.maps
.iter()
.fold(0, |sum, m| sum + m.get_pixel(x, y).0[0] as u32)
}
}

fn size(&self) -> (u32, u32) {
Expand All @@ -37,16 +46,16 @@ impl Heightmap for HeightmapPNG {

// Heightmap image input
impl HeightmapPNG {
pub fn new(images: Vec<&str>) -> Result<Self, String> {
pub fn new(images: Vec<&str>, rgba_encoded:bool) -> Result<Self, String> {
if images.is_empty() {
return Err("HeightmapPNG requires at least one image".to_string());
}

// read in the maps
let mut maps: Vec<RgbImage> = vec![];
let mut maps: Vec<RgbaImage> = vec![];
for file in images {
if let Ok(img) = image::open(file) {
maps.push(img.to_rgb());
maps.push(img.to_rgba());
} else {
return Err(format!("Could not open PNG {}", file));
}
Expand All @@ -62,7 +71,7 @@ impl HeightmapPNG {
}

// return a reference to save on memory
Ok(HeightmapPNG { maps })
Ok(HeightmapPNG { maps, rgba_encoded })
}
}

Expand Down
1 change: 1 addition & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct GenOptions {
pub tile: bool,
pub snap: bool,
pub img: bool,
pub hdmap: bool,
}

// convert gamma to linear gamma
Expand Down