Skip to content

Commit

Permalink
add README and some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sn-blg committed Nov 3, 2021
1 parent ab668ff commit 393134e
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "img2h3m"
version = "0.1.0"
version = "0.2.0"
authors = ["sn-blg"]
edition = "2021"
description = "CLI utility for converting image to homm3 mini map"
description = "CLI utility for converting image to homm3 HotA minimap"
license = "MIT"

[dependencies]
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# img2h3m

CLI utility for converting image to **homm3 HotA** minimap.
6 changes: 3 additions & 3 deletions src/h3m/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl H3m {
Ok(())
}

pub fn size(&self) -> usize {
pub fn map_size(&self) -> usize {
self.info.map_size
}

Expand All @@ -95,7 +95,7 @@ impl H3m {
) -> H3mResult<()> {
const SURFACE_CELL_SIZE: usize = 7;

let land_length = self.size() * self.size();
let land_length = self.map_size() * self.map_size();

if index >= land_length {
return Err(H3mError::InvalidArgument);
Expand All @@ -122,7 +122,7 @@ impl H3m {
underground: bool,
surface: Surface,
) -> H3mResult<()> {
self.set_surface_by_index(row * self.size() + column, underground, surface)
self.set_surface_by_index(row * self.map_size() + column, underground, surface)
}

fn surface_picture_type(&self, surface: Surface) -> u8 {
Expand Down
25 changes: 3 additions & 22 deletions src/h3m/parsers/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,6 @@ use crate::h3m::result::*;
use byteorder::{ReadBytesExt, LE};
use std::io::{Read, Seek};

pub enum Size {
S = 36,
M = 72,
L = 108,
XL = 144,
H = 180,
XH = 216,
G = 252,
}

#[derive(PartialEq)]
enum Version {
RoE,
Expand All @@ -36,22 +26,13 @@ fn read_version<R: Read>(input: &mut R) -> H3mResult<Version> {
}
}

fn read_size<R: Read>(input: &mut R) -> H3mResult<Size> {
fn read_size<R: Read>(input: &mut R) -> H3mResult<usize> {
let size = input.read_u32::<LE>()?;
match size {
36 => Ok(Size::S),
72 => Ok(Size::M),
108 => Ok(Size::L),
144 => Ok(Size::XL),
180 => Ok(Size::H),
216 => Ok(Size::XH),
252 => Ok(Size::G),
_ => Err(H3mError::ParseError),
}
usize::try_from(size).or(Err(H3mError::ParseError))
}

pub struct H3mHeaderInfo {
pub map_size: Size,
pub map_size: usize,
pub has_underground: bool,
}

Expand Down
2 changes: 1 addition & 1 deletion src/h3m/parsers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub fn parse(raw_map: &[u8]) -> H3mResult<H3mInfo> {
let land_offset = raw_map.position();

Ok(H3mInfo {
map_size: header_info.map_size as usize,
map_size: header_info.map_size,
has_underground: header_info.has_underground,
land_offset: usize::try_from(land_offset).or(Err(H3mError::ParseError))?,
})
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
let mut h3m = H3m::load(input_map_file)?;

let img = ImageReader::open(&config.image_path)?.decode()?.into_rgb8();
let map_size = h3m.size();
let map_size = h3m.map_size();
let palette = make_palette();

for (row_id, row) in img.rows().take(map_size).enumerate() {
Expand Down

0 comments on commit 393134e

Please sign in to comment.