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

[CI] Format Checks + Validate All Platforms #6

Merged
merged 4 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 24 additions & 8 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
ethteck marked this conversation as resolved.
Show resolved Hide resolved
- name: Run cargo clippy
run: cargo clippy --all -- -D warnings
ethteck marked this conversation as resolved.
Show resolved Hide resolved
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
.idea
83 changes: 53 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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)
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -168,63 +181,71 @@ 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<u8> {
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]);
let i2 = rgb_to_intensity(chunk[4], chunk[5], chunk[6]);
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]);
let i2 = rgb_to_intensity(chunk[3], chunk[4], chunk[5]);
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<u8> {
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<u8> {
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;
Expand All @@ -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<u8> {
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<u8> {
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<u8> {
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<u8> {
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),
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,19 @@ macro_rules! write_buf_as_raw_array {
};
}

fn write_buf_as_u8(output_file: &mut Box<dyn Write>, bin: &Vec<u8>) {
fn write_buf_as_u8(output_file: &mut Box<dyn Write>, bin: &[u8]) {
write_buf_as_raw_array!(output_file, bin, u8);
}

fn write_buf_as_u16(output_file: &mut Box<dyn Write>, bin: &Vec<u8>) {
fn write_buf_as_u16(output_file: &mut Box<dyn Write>, bin: &[u8]) {
write_buf_as_raw_array!(output_file, bin, u16);
}

fn write_buf_as_u32(output_file: &mut Box<dyn Write>, bin: &Vec<u8>) {
fn write_buf_as_u32(output_file: &mut Box<dyn Write>, bin: &[u8]) {
write_buf_as_raw_array!(output_file, bin, u32);
}

fn write_buf_as_u64(output_file: &mut Box<dyn Write>, bin: &Vec<u8>) {
fn write_buf_as_u64(output_file: &mut Box<dyn Write>, bin: &[u8]) {
write_buf_as_raw_array!(output_file, bin, u64);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down