Skip to content

Commit

Permalink
Fix build errors with image dependency; Fixes #79
Browse files Browse the repository at this point in the history
  • Loading branch information
yoanlcq committed Nov 11, 2021
1 parent 542813a commit 88af112
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 30 deletions.
10 changes: 10 additions & 0 deletions examples/bin-with-optional-dependencies/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Generated by Cargo
# will have compiled files and executables
/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk
9 changes: 9 additions & 0 deletions examples/bin-with-optional-dependencies/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "bin-with-optional-dependencies"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
vek = { path = "../../", features = ["repr_simd", "image", "serde", "mint"] }
3 changes: 3 additions & 0 deletions examples/bin-with-optional-dependencies/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
84 changes: 54 additions & 30 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2134,48 +2134,60 @@ macro_rules! vec_impl_pixel_rgb {
where T: ColorComponent + Copy + Clone + Primitive
{
type Subpixel = T;
fn channel_count() -> u8 {
3
}

const CHANNEL_COUNT: u8 = 3;
const COLOR_MODEL: &'static str = "RGB";

// When I first introduced the optional dependency to the `image` crate, ColorType allowed specifying the bit depth procedurally.
// Now the bit depths are fixed; the only "really" supported T are now u8 and u16...
// For now, I choose to still "implement" this trait for other T (such as f32), for convenience and backwards compatibility, but you shouldn't use the COLOR_TYPE in that case.
// Feel free to open an issue about that.
// NOTE: this comment is duplicated in vec_impl_pixel_rgba!(), please update both instances if you change one
const COLOR_TYPE: ColorType = match mem::size_of::<T>() {
1 => ColorType::Rgb8, // This is wrong if T is a signed type, but the closest we can get
2 => ColorType::Rgb16, // This is wrong if T is a signed type, but the closest we can get
_ => ColorType::Rgb8, // This is wrong for literally any T
};

fn channels(&self) -> &[Self::Subpixel] {
self.as_slice()
}
fn channels_mut(&mut self) -> &mut [Self::Subpixel] {
self.as_mut_slice()
}
fn color_model() -> &'static str {
"RGB"
}
fn color_type() -> ColorType {
ColorType::RGB(Self::channel_count() * mem::size_of::<T>() as u8 * 8_u8)
}
fn channels4(&self) -> (Self::Subpixel, Self::Subpixel, Self::Subpixel, Self::Subpixel) {
(self.r, self.g, self.b, T::full())
}
fn from_channels(a: Self::Subpixel, b: Self::Subpixel, c: Self::Subpixel, _d: Self::Subpixel) -> Self {
Self::new(a, b, c)
}
fn from_slice(slice: &[Self::Subpixel]) -> &Self {
assert!(slice.len() >= Self::channel_count() as _);
assert!(slice.len() >= Self::CHANNEL_COUNT as _);
unsafe { &*(slice.as_ptr() as *const _ as *const Self) }
}
fn from_slice_mut(slice: &mut [Self::Subpixel]) -> &mut Self {
assert!(slice.len() >= Self::channel_count() as _);
assert!(slice.len() >= Self::CHANNEL_COUNT as _);
unsafe { &mut *(slice.as_mut_ptr() as *mut _ as *mut Self) }
}
fn to_rgb(&self) -> image::Rgb<Self::Subpixel> {
image::Rgb { data: [self.r, self.g, self.b] }
image::Rgb([self.r, self.g, self.b])
}
fn to_rgba(&self) -> image::Rgba<Self::Subpixel> {
image::Rgba { data: [self.r, self.g, self.b, T::full()] }
image::Rgba([self.r, self.g, self.b, T::full()])
}
fn to_bgr(&self) -> image::Bgr<Self::Subpixel> {
image::Bgr([self.b, self.g, self.r])
}
fn to_bgra(&self) -> image::Bgra<Self::Subpixel> {
image::Bgra([self.b, self.g, self.r, T::full()])
}
fn to_luma(&self) -> Luma<Self::Subpixel> {
let three = T::one() + T::one() + T::one();
let c = (self.r + self.g + self.b) / three;
Luma { data: [c] }
Luma([c])
}
fn to_luma_alpha(&self) -> LumaA<Self::Subpixel> {
LumaA { data: [self.to_luma().data[0], T::full()] }
LumaA([self.to_luma().0[0], T::full()])
}
fn map<F>(&self, mut f: F) -> Self where F: FnMut(Self::Subpixel) -> Self::Subpixel {
Self { r: f(self.r), g: f(self.g), b: f(self.b) }
Expand Down Expand Up @@ -2236,48 +2248,60 @@ macro_rules! vec_impl_pixel_rgba {
where T: ColorComponent + Copy + Clone + Primitive
{
type Subpixel = T;
fn channel_count() -> u8 {
4
}

const CHANNEL_COUNT: u8 = 4;
const COLOR_MODEL: &'static str = "RGBA";

// When I first introduced the optional dependency to the `image` crate, ColorType allowed specifying the bit depth procedurally.
// Now the bit depths are fixed; the only "really" supported T are now u8 and u16...
// For now, I choose to still "implement" this trait for other T (such as f32), for convenience and backwards compatibility, but you shouldn't use the COLOR_TYPE in that case.
// Feel free to open an issue about that.
// NOTE: this comment is duplicated in vec_impl_pixel_rgb!(), please update both instances if you change one
const COLOR_TYPE: ColorType = match mem::size_of::<T>() {
1 => ColorType::Rgba8, // This is wrong if T is a signed type, but the closest we can get
2 => ColorType::Rgba16, // This is wrong if T is a signed type, but the closest we can get
_ => ColorType::Rgba8, // This is wrong for literally any T
};

fn channels(&self) -> &[Self::Subpixel] {
self.as_slice()
}
fn channels_mut(&mut self) -> &mut [Self::Subpixel] {
self.as_mut_slice()
}
fn color_model() -> &'static str {
"RGBA"
}
fn color_type() -> ColorType {
ColorType::RGBA(Self::channel_count() * mem::size_of::<T>() as u8 * 8_u8)
}
fn channels4(&self) -> (Self::Subpixel, Self::Subpixel, Self::Subpixel, Self::Subpixel) {
(self.r, self.g, self.b, self.a)
}
fn from_channels(a: Self::Subpixel, b: Self::Subpixel, c: Self::Subpixel, d: Self::Subpixel) -> Self {
Self::new(a, b, c, d)
}
fn from_slice(slice: &[Self::Subpixel]) -> &Self {
assert!(slice.len() >= Self::channel_count() as _);
assert!(slice.len() >= Self::CHANNEL_COUNT as _);
unsafe { &*(slice.as_ptr() as *const _ as *const Self) }
}
fn from_slice_mut(slice: &mut [Self::Subpixel]) -> &mut Self {
assert!(slice.len() >= Self::channel_count() as _);
assert!(slice.len() >= Self::CHANNEL_COUNT as _);
unsafe { &mut *(slice.as_mut_ptr() as *mut _ as *mut Self) }
}
fn to_rgb(&self) -> image::Rgb<Self::Subpixel> {
image::Rgb { data: [self.r, self.g, self.b] }
image::Rgb([self.r, self.g, self.b])
}
fn to_rgba(&self) -> image::Rgba<Self::Subpixel> {
image::Rgba { data: [self.r, self.g, self.b, self.a] }
image::Rgba([self.r, self.g, self.b, self.a])
}
fn to_bgr(&self) -> image::Bgr<Self::Subpixel> {
image::Bgr([self.b, self.g, self.r])
}
fn to_bgra(&self) -> image::Bgra<Self::Subpixel> {
image::Bgra([self.b, self.g, self.r, self.a])
}
fn to_luma(&self) -> Luma<Self::Subpixel> {
let three = T::one() + T::one() + T::one();
let c = (self.r + self.g + self.b) / three;
Luma { data: [c] }
Luma([c])
}
fn to_luma_alpha(&self) -> LumaA<Self::Subpixel> {
LumaA { data: [self.to_luma().data[0], self.a] }
LumaA([self.to_luma().0[0], self.a])
}
fn map<F>(&self, mut f: F) -> Self where F: FnMut(Self::Subpixel) -> Self::Subpixel {
Self { r: f(self.r), g: f(self.g), b: f(self.b), a: f(self.a) }
Expand Down

0 comments on commit 88af112

Please sign in to comment.