Skip to content
Open
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
16 changes: 8 additions & 8 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::ops::{Index, IndexMut};

use num_traits::{NumCast, ToPrimitive, Zero};

use crate::traits::{Enlargeable, Pixel, Primitive};
use crate::traits::{Pixel, Primitive, PrimitiveExt};

/// An enumeration over supported color types and bit depths
#[derive(Copy, PartialEq, Eq, Debug, Clone, Hash)]
Expand Down Expand Up @@ -411,11 +411,11 @@ define_colors! {
///
/// For the purpose of color conversion, as well as blending, the implementation of `Pixel`
/// assumes an `sRGB` color space of its data.
pub struct Rgb<T: Primitive Enlargeable>([T; 3, 0]) = "RGB";
pub struct Rgb<T: PrimitiveExt>([T; 3, 0]) = "RGB";
/// Grayscale colors.
pub struct Luma<T: Primitive>([T; 1, 0]) = "Y";
/// RGB colors + alpha channel
pub struct Rgba<T: Primitive Enlargeable>([T; 4, 1]) = "RGBA";
pub struct Rgba<T: PrimitiveExt>([T; 4, 1]) = "RGBA";
/// Grayscale colors + alpha channel
pub struct LumaA<T: Primitive>([T; 2, 1]) = "YA";
}
Expand Down Expand Up @@ -532,7 +532,7 @@ const SRGB_LUMA: [u32; 3] = [2126, 7152, 722];
const SRGB_LUMA_DIV: u32 = 10000;

#[inline]
fn rgb_to_luma<T: Primitive + Enlargeable>(rgb: &[T]) -> T {
fn rgb_to_luma<T: PrimitiveExt>(rgb: &[T]) -> T {
let l = <T::Larger as NumCast>::from(SRGB_LUMA[0]).unwrap() * rgb[0].to_larger()
+ <T::Larger as NumCast>::from(SRGB_LUMA[1]).unwrap() * rgb[1].to_larger()
+ <T::Larger as NumCast>::from(SRGB_LUMA[2]).unwrap() * rgb[2].to_larger();
Expand Down Expand Up @@ -560,7 +560,7 @@ where
}
}

impl<S: Primitive + Enlargeable, T: Primitive> FromColor<Rgb<S>> for Luma<T>
impl<S: PrimitiveExt, T: Primitive> FromColor<Rgb<S>> for Luma<T>
where
T: FromPrimitive<S>,
{
Expand All @@ -571,7 +571,7 @@ where
}
}

impl<S: Primitive + Enlargeable, T: Primitive> FromColor<Rgba<S>> for Luma<T>
impl<S: PrimitiveExt, T: Primitive> FromColor<Rgba<S>> for Luma<T>
where
T: FromPrimitive<S>,
{
Expand All @@ -597,7 +597,7 @@ where
}
}

impl<S: Primitive + Enlargeable, T: Primitive> FromColor<Rgb<S>> for LumaA<T>
impl<S: PrimitiveExt, T: Primitive> FromColor<Rgb<S>> for LumaA<T>
where
T: FromPrimitive<S>,
{
Expand All @@ -609,7 +609,7 @@ where
}
}

impl<S: Primitive + Enlargeable, T: Primitive> FromColor<Rgba<S>> for LumaA<T>
impl<S: Primitive + PrimitiveExt, T: Primitive> FromColor<Rgba<S>> for LumaA<T>
where
T: FromPrimitive<S>,
{
Expand Down
16 changes: 8 additions & 8 deletions src/imageops/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use num_traits::{NumCast, ToPrimitive, Zero};
use serde::{Deserialize, Serialize};

use crate::image::{GenericImage, GenericImageView};
use crate::traits::{Enlargeable, Pixel, Primitive};
use crate::traits::{Pixel, Primitive, PrimitiveExt};
use crate::utils::clamp;
use crate::{ImageBuffer, Rgba32FImage};

Expand Down Expand Up @@ -559,9 +559,9 @@ where
}

/// Local struct for keeping track of pixel sums for fast thumbnail averaging
struct ThumbnailSum<S: Primitive + Enlargeable>(S::Larger, S::Larger, S::Larger, S::Larger);
struct ThumbnailSum<S: PrimitiveExt>(S::Larger, S::Larger, S::Larger, S::Larger);

impl<S: Primitive + Enlargeable> ThumbnailSum<S> {
impl<S: PrimitiveExt> ThumbnailSum<S> {
fn zeroed() -> Self {
ThumbnailSum(
S::Larger::zero(),
Expand Down Expand Up @@ -601,7 +601,7 @@ pub fn thumbnail<I, P, S>(image: &I, new_width: u32, new_height: u32) -> ImageBu
where
I: GenericImageView<Pixel = P>,
P: Pixel<Subpixel = S> + 'static,
S: Primitive + Enlargeable + 'static,
S: PrimitiveExt + 'static,
{
let (width, height) = image.dimensions();
let mut out = ImageBuffer::new(new_width, new_height);
Expand Down Expand Up @@ -690,7 +690,7 @@ fn thumbnail_sample_block<I, P, S>(
where
I: GenericImageView<Pixel = P>,
P: Pixel<Subpixel = S>,
S: Primitive + Enlargeable,
S: PrimitiveExt,
{
let mut sum = ThumbnailSum::zeroed();

Expand Down Expand Up @@ -722,7 +722,7 @@ fn thumbnail_sample_fraction_horizontal<I, P, S>(
where
I: GenericImageView<Pixel = P>,
P: Pixel<Subpixel = S>,
S: Primitive + Enlargeable,
S: PrimitiveExt,
{
let fract = fraction_horizontal;

Expand Down Expand Up @@ -766,7 +766,7 @@ fn thumbnail_sample_fraction_vertical<I, P, S>(
where
I: GenericImageView<Pixel = P>,
P: Pixel<Subpixel = S>,
S: Primitive + Enlargeable,
S: PrimitiveExt,
{
let fract = fraction_vertical;

Expand Down Expand Up @@ -808,7 +808,7 @@ fn thumbnail_sample_fraction_both<I, P, S>(
where
I: GenericImageView<Pixel = P>,
P: Pixel<Subpixel = S>,
S: Primitive + Enlargeable,
S: PrimitiveExt,
{
#[allow(deprecated)]
let k_bl = image.get_pixel(left, bottom).channels4();
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub use crate::buffer_::{
pub use crate::flat::FlatSamples;

// Traits
pub use crate::traits::{EncodableLayout, Pixel, PixelWithColorType, Primitive};
pub use crate::traits::{EncodableLayout, Pixel, PixelWithColorType, Primitive, PrimitiveExt};

// Opening and loading images
pub use crate::dynimage::{
Expand Down
7 changes: 7 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ macro_rules! declare_primitive {
const DEFAULT_MAX_VALUE: Self = $to;
const DEFAULT_MIN_VALUE: Self = $from;
}
impl PrimitiveExt for $base {}
};
}

Expand All @@ -68,6 +69,12 @@ declare_primitive!(i64: (Self::MIN)..Self::MAX);
declare_primitive!(f32: (0.0)..1.0);
declare_primitive!(f64: (0.0)..1.0);

/// A sealed version of the `Primitive` trait.
///
/// In a future version, this trait will likely removed and the `Primitive` trait itself switched to
/// being sealed.
pub trait PrimitiveExt: Primitive + Enlargeable {}

/// An `Enlargable::Larger` value should be enough to calculate
/// the sum (average) of a few hundred or thousand Enlargeable values.
pub trait Enlargeable: Sized + Bounded + NumCast {
Expand Down
Loading