Skip to content

Commit

Permalink
Introduce strum and strum_macros (#19)
Browse files Browse the repository at this point in the history
* This commit introduces the usage of the `strum` and `strum_macros` crates. 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

* Address review comments

This commit removes the implemented functions. Also updates the README.md description.

* Run formatter

Didn't have auto-formatting on, this commit should fix that.
  • Loading branch information
jpburnett authored Apr 4, 2024
1 parent ed785bc commit ff903f0
Show file tree
Hide file tree
Showing 5 changed files with 128 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 converting image data between native and png formats.

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

mod utils;

use strum_macros::{EnumCount, EnumIter};

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, TryFromPrimitive)]
#[repr(u8)]
pub enum ImageSize {
Expand All @@ -21,6 +23,15 @@ pub enum ImageSize {
}

impl ImageSize {
/// Returns the size of the TLUT (Table Look-Up Table) based on the image size.
///
/// # Returns
///
/// The size of the TLUT as a `usize` value.
///
/// # Panics
///
/// This method will panic if the image size is invalid.
pub fn get_tlut_size(&self) -> usize {
match self {
ImageSize::Bits1 => 0b10,
Expand All @@ -43,7 +54,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, EnumCount, EnumIter, Eq, Hash, TryFromPrimitive)]
#[repr(u8)]
pub enum ImageType {
I1,
Expand All @@ -59,6 +77,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 +100,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 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_image_type_strum() {
// Test iterating over the ImageType enum
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 ff903f0

Please sign in to comment.