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

Add get_image function to IconState #20

Merged
merged 4 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions src/dirs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ bitflags! {
}
}

impl std::fmt::Display for Dirs {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{} ({:?})", self.bits(), self)
}
}

/// A list of every cardinal direction.
pub const CARDINAL_DIRS: [Dirs; 4] = [Dirs::NORTH, Dirs::SOUTH, Dirs::EAST, Dirs::WEST];

Expand Down
54 changes: 28 additions & 26 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
use std::io;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum DmiError {
#[error("IO error")]
Io(#[from] io::Error),
#[error("Image-processing error")]
Image(#[from] image::error::ImageError),
#[error("FromUtf8 error")]
FromUtf8(#[from] std::string::FromUtf8Error),
#[error("ParseInt error")]
ParseInt(#[from] std::num::ParseIntError),
#[error("ParseFloat error")]
ParseFloat(#[from] std::num::ParseFloatError),
#[error("Invalid chunk type (byte outside the range `A-Za-z`): {chunk_type:?}")]
InvalidChunkType { chunk_type: [u8; 4] },
#[error("CRC mismatch (stated {stated:?}, calculated {calculated:?})")]
CrcMismatch { stated: u32, calculated: u32 },
#[error("Dmi error: {0}")]
Generic(String),
#[error("Encoding error: {0}")]
Encoding(String),
#[error("Conversion error: {0}")]
Conversion(String),
}
use std::io;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum DmiError {
#[error("IO error")]
Io(#[from] io::Error),
#[error("Image-processing error")]
Image(#[from] image::error::ImageError),
#[error("FromUtf8 error")]
FromUtf8(#[from] std::string::FromUtf8Error),
#[error("ParseInt error")]
ParseInt(#[from] std::num::ParseIntError),
#[error("ParseFloat error")]
ParseFloat(#[from] std::num::ParseFloatError),
#[error("Invalid chunk type (byte outside the range `A-Za-z`): {chunk_type:?}")]
InvalidChunkType { chunk_type: [u8; 4] },
#[error("CRC mismatch (stated {stated:?}, calculated {calculated:?})")]
CrcMismatch { stated: u32, calculated: u32 },
#[error("Dmi error: {0}")]
Generic(String),
#[error("Dmi IconState error: {0}")]
IconState(String),
#[error("Encoding error: {0}")]
Encoding(String),
#[error("Conversion error: {0}")]
Conversion(String),
}
62 changes: 60 additions & 2 deletions src/icon.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::dirs::Dirs;
use crate::dirs::{Dirs, ALL_DIRS, ORDINAL_DIRS};
use crate::{error, ztxt, RawDmi};
use image::codecs::png;
use image::imageops;
use image::GenericImageView;
use image::{imageops, DynamicImage};
use std::collections::HashMap;
use std::io::prelude::*;
use std::io::Cursor;
Expand All @@ -29,6 +29,21 @@ pub const DIR_ORDERING: [Dirs; 8] = [
Dirs::NORTHWEST,
];

/// Given a Dir, gives its order with a DMI file (equivalent: DIR_ORDERING.iter().position(Dir))
pub fn dir_to_dmi_index(dir: &Dirs) -> Option<u32> {
match *dir {
Dirs::SOUTH => Some(0),
Dirs::NORTH => Some(1),
Dirs::EAST => Some(2),
Dirs::WEST => Some(3),
Dirs::SOUTHEAST => Some(4),
Dirs::SOUTHWEST => Some(5),
Dirs::NORTHEAST => Some(6),
Dirs::NORTHWEST => Some(7),
_ => None,
}
}

impl Icon {
pub fn load<R: Read>(reader: R) -> Result<Icon, error::DmiError> {
let raw_dmi = RawDmi::load(reader)?;
Expand Down Expand Up @@ -484,6 +499,49 @@ pub struct IconState {
pub unknown_settings: Option<HashMap<String, String>>,
}

impl IconState {
/// Gets a specific DynamicImage from `images`, given a dir and frame.
pub fn get_image(&self, dir: &Dirs, frame: u32) -> Result<&DynamicImage, error::DmiError> {
if self.frames < frame {
return Err(error::DmiError::IconState(format!(
"Specified frame \"{}\" is larger than the number of frames ({}) in icon_state \"{}\"",
frame, self.frames, self.name
itsmeow marked this conversation as resolved.
Show resolved Hide resolved
)));
}
if (self.dirs == 1 && *dir != Dirs::SOUTH)
|| (self.dirs == 4 && !ORDINAL_DIRS.contains(dir))
|| (self.dirs == 8 && !ALL_DIRS.contains(dir))
itsmeow marked this conversation as resolved.
Show resolved Hide resolved
{
return Err(error::DmiError::IconState(format!(
"Dir specified {} is not in the set of valid dirs ({} dirs) in icon_state \"{}\"",
itsmeow marked this conversation as resolved.
Show resolved Hide resolved
dir, self.dirs, self.name
)));
}

let image_idx = match dir_to_dmi_index(dir) {
Some(idx) => (idx + 1) * frame - 1,
None => {
return Err(error::DmiError::IconState(format!(
"Dir specified {} is not a valid dir within DMI ordering! (icon_state: {})",
dir, self.name
)));
}
};

match self.images.get(image_idx as usize) {
Some(image) => {
Ok(image)
},
None => {
Err(error::DmiError::IconState(format!(
"Out of bounds index {} in icon_state \"{}\" (images len: {} dirs: {}, frames: {} - dir: {}, frame: {})",
image_idx, self.name, self.images.len(), self.dirs, self.frames, dir, frame
)))
}
}
}
}

impl Default for IconState {
fn default() -> Self {
IconState {
Expand Down
Loading