Skip to content

Commit

Permalink
Get rid of image types
Browse files Browse the repository at this point in the history
They are not needed to accomplish the main task of this crate:
converting between various pixelformats using arbitrary buffers.

We now place the burden of choosing a suitable storage type onto
the caller. The caller can then iterate through the collection and
map each pixel individually (or multiple, in the case of macropixels).

Signed-off-by: Christopher N. Hesse <[email protected]>
  • Loading branch information
raymanfx committed Oct 28, 2023
1 parent 97fa0a0 commit 21f983c
Show file tree
Hide file tree
Showing 15 changed files with 10 additions and 896 deletions.
9 changes: 1 addition & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ jobs:
check:
name: check
runs-on: ubuntu-20.04
strategy:
matrix:
include:
- build: default
features: ""
- build: rayon
features: "--features rayon"
steps:
- name: Checkout repository
uses: actions/checkout@v2
Expand All @@ -34,7 +27,7 @@ jobs:
profile: minimal
override: true
- name: Check
run: cargo check --no-default-features ${{ matrix.features }}
run: cargo check

test:
name: test
Expand Down
5 changes: 0 additions & 5 deletions ffimage-yuv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@ path = "../ffimage"

[dependencies]
num-traits = "0.2.14"
itertools = "0.10.3"

[dev-dependencies]
criterion = "0.3.4"

[features]
default = ["rayon"]
rayon = ["ffimage/rayon"]

[[bench]]
name = "bench_main"
harness = false
92 changes: 0 additions & 92 deletions ffimage-yuv/src/yuv422.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use core::ops::{Deref, DerefMut};

use itertools::Itertools;
use num_traits::{AsPrimitive, FromPrimitive};

use ffimage::color::{Bgr, Rgb};
use ffimage::convert::MapPixels;
use ffimage::traits::Pixel;

use crate::yuv::*;
Expand Down Expand Up @@ -89,93 +84,6 @@ where
}
}

impl<T, const Y0: usize, const Y1: usize, const U: usize, const V: usize>
MapPixels<Yuv422<T, Y0, Y1, U, V>, Yuv<T>> for Yuv422<T, Y0, Y1, U, V>
where
T: Copy,
{
fn map_pixels<'a, I, O>(input: I, output: O)
where
I: IntoIterator<Item = &'a Yuv422<T, Y0, Y1, U, V>>,
O: IntoIterator<Item = &'a mut Yuv<T>>,
T: 'a,
{
input
.into_iter()
.zip(output.into_iter().tuples())
.for_each(|(t, (u1, u2))| {
let yuv = <[Yuv<T>; 2]>::from(*t);
*u1 = yuv[0];
*u2 = yuv[1];
})
}
}

impl<T: Default, const Y0: usize, const Y1: usize, const U: usize, const V: usize>
MapPixels<Yuv<T>, Yuv422<T, Y0, Y1, U, V>> for Yuv<T>
where
T: Copy,
{
fn map_pixels<'a, I, O>(input: I, output: O)
where
I: IntoIterator<Item = &'a Yuv<T>>,
O: IntoIterator<Item = &'a mut Yuv422<T, Y0, Y1, U, V>>,
T: 'a,
{
input
.into_iter()
.tuples()
.zip(output.into_iter())
.for_each(|((sp1, sp2), dp)| {
*dp = Yuv422::<T, Y0, Y1, U, V>::from([*sp1, *sp2]);
})
}
}

impl<T, const Y0: usize, const Y1: usize, const U: usize, const V: usize>
MapPixels<Yuv422<T, Y0, Y1, U, V>, Rgb<T>> for Yuv422<T, Y0, Y1, U, V>
where
T: Copy + Default + AsPrimitive<i32> + FromPrimitive,
{
fn map_pixels<'a, I, O>(input: I, output: O)
where
I: IntoIterator<Item = &'a Yuv422<T, Y0, Y1, U, V>>,
O: IntoIterator<Item = &'a mut Rgb<T>>,
T: 'a,
{
input
.into_iter()
.zip(output.into_iter().tuples())
.for_each(|(t, (u1, u2))| {
let yuv = <[Yuv<T>; 2]>::from(*t);
*u1 = Rgb::<T>::from(yuv[0]);
*u2 = Rgb::<T>::from(yuv[1]);
})
}
}

impl<T, const Y0: usize, const Y1: usize, const U: usize, const V: usize>
MapPixels<Yuv422<T, Y0, Y1, U, V>, Bgr<T>> for Yuv422<T, Y0, Y1, U, V>
where
T: Copy + Default + AsPrimitive<i32> + FromPrimitive,
{
fn map_pixels<'a, I, O>(input: I, output: O)
where
I: IntoIterator<Item = &'a Yuv422<T, Y0, Y1, U, V>>,
O: IntoIterator<Item = &'a mut Bgr<T>>,
T: 'a,
{
input
.into_iter()
.zip(output.into_iter().tuples())
.for_each(|(t, (u1, u2))| {
let yuv = <[Yuv<T>; 2]>::from(*t);
*u1 = Bgr::<T>::from(yuv[0]);
*u2 = Bgr::<T>::from(yuv[1]);
})
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
4 changes: 0 additions & 4 deletions ffimage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ repository= "https://github.com/raymanfx/ffimage"
cfg-if = "1.0.0"
num = "0.4.0"
num-traits = "0.2.14"
rayon = { version = "1.5.0", optional = true }

[dev-dependencies]
criterion = "0.3.4"

[features]
default = ["rayon"]

[[bench]]
name = "bench_main"
harness = false
39 changes: 0 additions & 39 deletions ffimage/src/convert.rs

This file was deleted.

21 changes: 0 additions & 21 deletions ffimage/src/error.rs

This file was deleted.

34 changes: 9 additions & 25 deletions ffimage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,21 @@
//! Here is a very brief example of RGB -> Grayscale conversion of existing memory:
//!
//! ```no_run
//! use ffimage::packed::{ImageView, ImageBuffer};
//! use ffimage::color::{Rgb, Gray};
//! use ffimage::convert::Convert;
//!
//! // This is our grayscale image memory.
//! // This is our RGB image memory.
//! // Usually, this will be allocated by a foreign function (e.g. kernel driver) and contain
//! // read-only memory.
//! let mem: [u8; 12] = [0; 12];
//!
//! // Create a statically typed view of the image, assuming it is RGB 24 bits per pixel.
//! // The u8 parameter denotes the internal storage type used by image pixels. In our case, each
//! // channel requires eight bits, which makes for a total of 3 * 8 = 24 bits per pixel.
//! // The length of the memory slice is validated and a None value is returned when constraints
//! // are violated.
//! let view = ImageView::<Rgb<u8>>::from_buf(&mem, 2, 2).unwrap();
//!
//! // Create a target buffer for the destination image.
//! // The dimensions should be equal to the source image, otherwise only as many pixels as the
//! // target buffer can hold will be converted.
//! let mut buf = ImageBuffer::<Gray<u8>>::new(2, 2, 0u8);
//!
//! // Perform the actual conversion.
//! // This cannot fail since the target buffer is resizable.
//! // If the pixel conversion between source and target image is not defined, the compiler will
//! // refuse to compile this line.
//! view.convert(&mut buf);
//! let rgb = vec![Rgb::<u8>([10, 10, 10]); 10];
//!
//! // Convert the pixels into Grayscale pixels by mapping each one individually.
//! let gray: Vec<Gray<u8>> = rgb
//! .iter()
//! .copied()
//! .map(|rgb| Gray::<u8>::from(rgb))
//! .collect();
//!```

pub mod error;
pub mod traits;

pub mod color;
pub mod convert;
pub mod packed;
28 changes: 0 additions & 28 deletions ffimage/src/packed/convert/gold.rs

This file was deleted.

7 changes: 0 additions & 7 deletions ffimage/src/packed/convert/mod.rs

This file was deleted.

35 changes: 0 additions & 35 deletions ffimage/src/packed/convert/rayon.rs

This file was deleted.

Loading

0 comments on commit 21f983c

Please sign in to comment.