Skip to content

Commit

Permalink
This commit introduces the usage of the strum and strum_macros cr…
Browse files Browse the repository at this point in the history
…ates. The purpose of these crates is to enable the usage of getting the variant counts and an iterator for the enumeration.

Some documentation was added for enums and their functions.

Added a basic README.md

Bumped versions in Cargo.toml
  • Loading branch information
jpburnett committed Apr 2, 2024
1 parent ed785bc commit 744682e
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 31 deletions.
85 changes: 59 additions & 26 deletions Cargo.lock

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

10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ repository = "https://github.com/decompals/pigment64"
license = "MIT"

[dependencies]
png = "0.17.10"
clap = { version = "4.4.13", features = ["derive"] }
anyhow = "1.0.79"
png = "0.17.13"
clap = { version = "4.5.4", features = ["derive"] }
anyhow = "1.0.81"
byteorder = "1.5.0"
num_enum = "0.7.1"
num_enum = "0.7.2"
strum = "0.26.2"
strum_macros = "0.26.2"
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# pigment64
pigment64 is a library written in Rust for decoding N64 images in to pngs.

## Formats
The library supports the following image formats:
- I1, I4, I8, Ia4, Ia8, Ia16, Ci4, Ci8, RGBA16, RGBA32
62 changes: 61 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ pub use image::png_image::PNGImage;

mod utils;

use strum:: {
EnumCount, IntoEnumIterator
};
use strum_macros::{EnumCount as EnumCountMacro, EnumIter};

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, TryFromPrimitive)]
#[repr(u8)]
pub enum ImageSize {
Expand Down Expand Up @@ -43,7 +48,14 @@ pub enum ImageFormat {
I = 4,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, TryFromPrimitive)]
/// Represents the type of image.
///
/// This enum is used to specify the type of image, which determines the size and format of the
/// image data.
/// Each variant corresponds to a specific image type, such as indexed color (Ci), grayscale (I),
/// grayscale with alpha (Ia), or red-green-blue-alpha (RGBA).
///
#[derive(Copy, Clone, Debug, PartialEq, EnumCountMacro, EnumIter, Eq, Hash, TryFromPrimitive)]
#[repr(u8)]
pub enum ImageType {
I1,
Expand All @@ -59,6 +71,14 @@ pub enum ImageType {
}

impl ImageType {
/// Returns the size of the image type.
///
/// This function returns the size of the image type, which represents the number of bits used
/// to store each pixel. The size is determined based on the image type variant.
///
/// # Returns
///
/// - `ImageSize` - The size of the image type.
pub fn get_size(&self) -> ImageSize {
match self {
ImageType::Ci4 => ImageSize::Bits4,
Expand All @@ -74,6 +94,14 @@ impl ImageType {
}
}

/// Returns the format of the image type.
///
/// This method returns the format of the image type, which represents the color model used by
/// the image. The format is determined based on the image type variant.
///
/// # Returns
///
/// - `ImageFormat` - The format of the image type.
pub fn get_format(&self) -> ImageFormat {
match self {
ImageType::Ci4 => ImageFormat::Ci,
Expand All @@ -88,6 +116,38 @@ impl ImageType {
ImageType::Rgba32 => ImageFormat::Rgba,
}
}

/// Returns the number of variants in the `ImageType` enum.
///
/// # Examples
///
/// ```
/// use pigment64::ImageType;
///
/// // Get the count of ImageType variants
/// let count = ImageType::get_image_type_count();
/// println!("Number of ImageType variants: {}", count);
/// ```
pub fn get_count() -> usize {
ImageType::COUNT
}

/// Returns an iterator over all possible variants of `ImageType`, converted to their formatted
/// string representations.
///
/// # Examples
/// ```
/// use pigment64::ImageType;
///
/// // Iterate over the image formats
/// for image_format_type in ImageType::format_iter() {
/// println!("{}", image_format_type);
/// }
/// ```
pub fn get_iter() -> impl Iterator<Item = ImageType> {
ImageType::iter().into_iter()
}

}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive)]
Expand Down
22 changes: 22 additions & 0 deletions tests/native_image.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use pigment64::image::native_image::parse_tlut;
use pigment64::{create_palette_from_png, ImageSize, ImageType, NativeImage, PNGImage, TextureLUT};
use strum::{EnumCount, IntoEnumIterator};

#[test]
fn ci4() -> Result<()> {
Expand Down Expand Up @@ -162,3 +163,24 @@ fn rgba32() -> Result<()> {
assert_eq!(output_bytes, original_bytes);
Ok(())
}

#[test]
fn test_format_names() {

let mut image_iter = ImageType::iter();
assert_eq!(Some(ImageType::I1), image_iter.next());
assert_eq!(Some(ImageType::I4), image_iter.next());
assert_eq!(Some(ImageType::I8), image_iter.next());
assert_eq!(Some(ImageType::Ia4), image_iter.next());
assert_eq!(Some(ImageType::Ia8), image_iter.next());
assert_eq!(Some(ImageType::Ia16), image_iter.next());
assert_eq!(Some(ImageType::Ci4), image_iter.next());
assert_eq!(Some(ImageType::Ci8), image_iter.next());
assert_eq!(Some(ImageType::Rgba16), image_iter.next());
assert_eq!(Some(ImageType::Rgba32), image_iter.next());
assert_eq!(None, image_iter.next());

// Test the Correct number of items
assert_eq!(10, ImageType::COUNT);
assert_eq!(ImageType::iter().count(), ImageType::COUNT);
}

0 comments on commit 744682e

Please sign in to comment.