Skip to content

Commit

Permalink
Feature: Native -> PNG (#13)
Browse files Browse the repository at this point in the history
* Rename Image to PNGImage

* Update PNG Image methods + cmd line

* fmt

* Add NativeImage and conversions

* Add writing PNG and parsing TLUT

* Fix bugs and add tests

* Add CI png exports

* fmt

* Passing tests

* Remove debugging output

* Add ia4 from rgb8 png conversion

* Add more tests and fixes

* Add CI4 test + fixes

* Update some comments

* Small fixes

* Fix fmt
  • Loading branch information
dcvz authored Jul 18, 2023
1 parent f82f0a9 commit 677b38c
Show file tree
Hide file tree
Showing 18 changed files with 639 additions and 43 deletions.
86 changes: 86 additions & 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 @@ -10,3 +10,4 @@ png = "0.17.9"
clap = { version = "4.1.12", features = ["derive"] }
anyhow = "1.0.72"
byteorder = "1.4.3"
num_enum = "0.6.1"
17 changes: 14 additions & 3 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ impl Color {
Color { r, g, b, a: 0xFF }
}

/// Creates a new color from a 32-bit RGBA pixel.
#[inline]
pub fn from_u32(pixel: u32) -> Color {
let r = ((pixel >> 24) & 0xFF) as u8;
let g = ((pixel >> 16) & 0xFF) as u8;
let b = ((pixel >> 8) & 0xFF) as u8;
let a = (pixel & 0xFF) as u8;

Color { r, g, b, a }
}

/// Converts a 16-bit RGBA pixel to a 32-bit RGBA color.
#[inline]
pub fn from_u16(pixel: u16) -> Color {
Expand Down Expand Up @@ -62,15 +73,15 @@ impl Color {
/// Converts a 32-bit RGBA color to a 16-bit RGBA pixel and
/// returns the two 8-bit components.
#[inline]
pub fn rgba16(self) -> (u8, u8) {
pub fn rgba16(self) -> [u8; 2] {
let pixel = self.to_u16();
((pixel >> 8) as u8, (pixel & 0xFF) as u8)
[(pixel >> 8) as u8, (pixel & 0xFF) as u8]
}

/// Converts the rgb components to a single intensity value.
/// This is used for grayscale images.
#[inline]
pub fn rgb_to_intensity(&self) -> u8 {
(self.r as f32 * 0.2126 + self.g as f32 * 0.7152 + 0.0722 * self.b as f32) as u8
(self.r as f32 * 0.2126 + self.g as f32 * 0.7152 + 0.0722 * self.b as f32).round() as u8
}
}
1 change: 1 addition & 0 deletions src/image/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod native_image;
pub mod png_image;
Loading

0 comments on commit 677b38c

Please sign in to comment.