diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 31000a2..4969144 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -10,13 +10,29 @@ env: CARGO_TERM_COLOR: always jobs: - build: - + rustfmt-check: runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Build - run: cargo build --verbose - - name: Run tests - run: cargo test --verbose + - uses: actions/checkout@v2 + - name: Run cargo fmt + run: cargo fmt --all -- --check + - name: Run cargo clippy + run: cargo clippy --all -- -D warnings + macos-check: + runs-on: macos-latest + steps: + - uses: actions/checkout@v2 + - name: Test + run: cargo test --all-features + ubuntu-check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Test + run: cargo test --all-features + windows-check: + runs-on: windows-latest + steps: + - uses: actions/checkout@v2 + - name: Test + run: cargo test --all-features diff --git a/.gitignore b/.gitignore index ea8c4bf..3a8cabc 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +.idea diff --git a/src/lib.rs b/src/lib.rs index 600dfc4..5ae7663 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ +use png::{BitDepth, ColorType}; use std::io::prelude::*; -use png::{ColorType, BitDepth}; // TODO: make this an option or sompthin, also ask clover #[inline] @@ -19,8 +19,8 @@ fn pack_color(r: u8, g: u8, b: u8, a: u8) -> (u8, u8) { let g = (g >> 3) as u16; let b = (b >> 3) as u16; let a = (a > 127) as u16; - - let s = (r << 11) | (g << 6) | ( b << 1) | a; + + let s = (r << 11) | (g << 6) | (b << 1) | a; ((s >> 8) as u8, s as u8) } @@ -62,29 +62,42 @@ impl Image { for y in 0..self.height { for x in 0..self.width { let old_index = (y * self.width + x) as usize * self.color_type.samples(); - let new_index = ((self.height - y - 1) * self.width + (self.width - x - 1)) as usize * self.color_type.samples(); - flipped_bytes[new_index..new_index + self.color_type.samples()].copy_from_slice(&self.data[old_index..old_index + self.color_type.samples()]); + let new_index = ((self.height - y - 1) * self.width + (self.width - x - 1)) + as usize + * self.color_type.samples(); + flipped_bytes[new_index..new_index + self.color_type.samples()] + .copy_from_slice( + &self.data[old_index..old_index + self.color_type.samples()], + ); } } - }, + } (true, false) => { for y in 0..self.height { for x in 0..self.width { let old_index = (y * self.width + x) as usize * self.color_type.samples(); - let new_index = (y * self.width + (self.width - x - 1)) as usize * self.color_type.samples(); - flipped_bytes[new_index..new_index + self.color_type.samples()].copy_from_slice(&self.data[old_index..old_index + self.color_type.samples()]); + let new_index = (y * self.width + (self.width - x - 1)) as usize + * self.color_type.samples(); + flipped_bytes[new_index..new_index + self.color_type.samples()] + .copy_from_slice( + &self.data[old_index..old_index + self.color_type.samples()], + ); } } - }, + } (false, true) => { for y in 0..self.height { for x in 0..self.width { let old_index = (y * self.width + x) as usize * self.color_type.samples(); - let new_index = ((self.height - y - 1) * self.width + x) as usize * self.color_type.samples(); - flipped_bytes[new_index..new_index + self.color_type.samples()].copy_from_slice(&self.data[old_index..old_index + self.color_type.samples()]); + let new_index = ((self.height - y - 1) * self.width + x) as usize + * self.color_type.samples(); + flipped_bytes[new_index..new_index + self.color_type.samples()] + .copy_from_slice( + &self.data[old_index..old_index + self.color_type.samples()], + ); } } - }, + } (false, false) => { flipped_bytes.copy_from_slice(&self.data); } @@ -168,22 +181,25 @@ impl Image { match self.bit_depth { BitDepth::Four => self.data.to_vec(), - BitDepth::Eight => self.data + BitDepth::Eight => self + .data .chunks_exact(2) .map(|chunk| chunk[0] << 4 | chunk[1]) .collect(), - _ => panic!("unsupported bit depth: {:?}", self.bit_depth) + _ => panic!("unsupported bit depth: {:?}", self.bit_depth), } } pub fn as_i4(&self) -> Vec { match (self.color_type, self.bit_depth) { (ColorType::Grayscale, BitDepth::Four) => self.data.to_vec(), - (ColorType::Grayscale, BitDepth::Eight) => self.data + (ColorType::Grayscale, BitDepth::Eight) => self + .data .chunks_exact(2) .map(|chunk| u8_to_u4(chunk[0]) << 4 | u8_to_u4(chunk[1])) .collect(), - (ColorType::Rgba, BitDepth::Eight) => self.data + (ColorType::Rgba, BitDepth::Eight) => self + .data .chunks_exact(8) .map(|chunk| { let i1 = rgb_to_intensity(chunk[0], chunk[1], chunk[2]); @@ -191,7 +207,8 @@ impl Image { u8_to_u4(i1) << 4 | u8_to_u4(i2) }) .collect(), - (ColorType::Rgb, BitDepth::Eight) => self.data + (ColorType::Rgb, BitDepth::Eight) => self + .data .chunks_exact(6) .map(|chunk| { let i1 = rgb_to_intensity(chunk[0], chunk[1], chunk[2]); @@ -199,32 +216,36 @@ impl Image { u8_to_u4(i1) << 4 | u8_to_u4(i2) }) .collect(), - p => panic!("unsupported format {:?}", p) + p => panic!("unsupported format {:?}", p), } } pub fn as_i8(&self) -> Vec { match (self.color_type, self.bit_depth) { (ColorType::Grayscale, BitDepth::Eight) => self.data.to_vec(), - (ColorType::Grayscale, BitDepth::Four) => self.data + (ColorType::Grayscale, BitDepth::Four) => self + .data .chunks_exact(2) .map(|chunk| chunk[0] << 4 | chunk[1]) .collect(), - (ColorType::Rgba, BitDepth::Eight) => self.data + (ColorType::Rgba, BitDepth::Eight) => self + .data .chunks_exact(4) .map(|chunk| rgb_to_intensity(chunk[0], chunk[1], chunk[2])) .collect(), - (ColorType::Rgb, BitDepth::Eight) => self.data + (ColorType::Rgb, BitDepth::Eight) => self + .data .chunks_exact(3) .map(|chunk| rgb_to_intensity(chunk[0], chunk[1], chunk[2])) .collect(), - p => panic!("unsupported format {:?}", p) + p => panic!("unsupported format {:?}", p), } } pub fn as_ia4(&self) -> Vec { match (self.color_type, self.bit_depth) { - (ColorType::GrayscaleAlpha, BitDepth::Eight) => self.data + (ColorType::GrayscaleAlpha, BitDepth::Eight) => self + .data .chunks_exact(4) .map(|chunk| { let intensity = (chunk[0] >> 5) << 1; @@ -238,44 +259,46 @@ impl Image { high << 4 | (low & 0xF) }) .collect(), - p => panic!("unsupported format {:?}", p) + p => panic!("unsupported format {:?}", p), } } pub fn as_ia8(&self) -> Vec { match (self.color_type, self.bit_depth) { - (ColorType::GrayscaleAlpha, BitDepth::Eight) => self.data + (ColorType::GrayscaleAlpha, BitDepth::Eight) => self + .data .chunks_exact(2) .map(|chunk| chunk[0] << 4 | (chunk[1] & 0x0F)) .collect(), - p => panic!("unsupported format {:?}", p) + p => panic!("unsupported format {:?}", p), } } pub fn as_ia16(&self) -> Vec { match (self.color_type, self.bit_depth) { (ColorType::GrayscaleAlpha, BitDepth::Eight) => self.data.to_vec(), - p => panic!("unsupported format {:?}", p) + p => panic!("unsupported format {:?}", p), } } pub fn as_rgba16(&self) -> Vec { match (self.color_type, self.bit_depth) { - (ColorType::Rgba, BitDepth::Eight) => self.data + (ColorType::Rgba, BitDepth::Eight) => self + .data .chunks_exact(4) .flat_map(|chunk| { let (first, second) = pack_color(chunk[0], chunk[1], chunk[2], chunk[3]); [first, second].into_iter() }) .collect(), - p => panic!("unsupported format {:?}", p) + p => panic!("unsupported format {:?}", p), } } pub fn as_rgba32(&self) -> Vec { match (self.color_type, self.bit_depth) { (ColorType::Rgba, BitDepth::Eight) => self.data.to_vec(), - p => panic!("unsupported format {:?}", p) + p => panic!("unsupported format {:?}", p), } } } diff --git a/src/main.rs b/src/main.rs index c2d4e39..78147fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,19 +94,19 @@ macro_rules! write_buf_as_raw_array { }; } -fn write_buf_as_u8(output_file: &mut Box, bin: &Vec) { +fn write_buf_as_u8(output_file: &mut Box, bin: &[u8]) { write_buf_as_raw_array!(output_file, bin, u8); } -fn write_buf_as_u16(output_file: &mut Box, bin: &Vec) { +fn write_buf_as_u16(output_file: &mut Box, bin: &[u8]) { write_buf_as_raw_array!(output_file, bin, u16); } -fn write_buf_as_u32(output_file: &mut Box, bin: &Vec) { +fn write_buf_as_u32(output_file: &mut Box, bin: &[u8]) { write_buf_as_raw_array!(output_file, bin, u32); } -fn write_buf_as_u64(output_file: &mut Box, bin: &Vec) { +fn write_buf_as_u64(output_file: &mut Box, bin: &[u8]) { write_buf_as_raw_array!(output_file, bin, u64); } diff --git a/tests/test.rs b/tests/test.rs index c723612..0c50ad3 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,5 +1,5 @@ -use std::io::Cursor; use pigment::*; +use std::io::Cursor; // TODO: convert input into all permutations of color type and bit depth // to test all codepaths