From d3391b47a42b0d6b8d43c937c2d06a59355e2dba Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Wed, 16 Apr 2025 21:05:34 -0700 Subject: [PATCH] Add PrimitiveExt trait --- src/color.rs | 16 ++++++++-------- src/imageops/sample.rs | 16 ++++++++-------- src/lib.rs | 2 +- src/traits.rs | 7 +++++++ 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/color.rs b/src/color.rs index a83ca8c6b4..49262c148a 100644 --- a/src/color.rs +++ b/src/color.rs @@ -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)] @@ -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; 3, 0]) = "RGB"; + pub struct Rgb([T; 3, 0]) = "RGB"; /// Grayscale colors. pub struct Luma([T; 1, 0]) = "Y"; /// RGB colors + alpha channel - pub struct Rgba([T; 4, 1]) = "RGBA"; + pub struct Rgba([T; 4, 1]) = "RGBA"; /// Grayscale colors + alpha channel pub struct LumaA([T; 2, 1]) = "YA"; } @@ -532,7 +532,7 @@ const SRGB_LUMA: [u32; 3] = [2126, 7152, 722]; const SRGB_LUMA_DIV: u32 = 10000; #[inline] -fn rgb_to_luma(rgb: &[T]) -> T { +fn rgb_to_luma(rgb: &[T]) -> T { let l = ::from(SRGB_LUMA[0]).unwrap() * rgb[0].to_larger() + ::from(SRGB_LUMA[1]).unwrap() * rgb[1].to_larger() + ::from(SRGB_LUMA[2]).unwrap() * rgb[2].to_larger(); @@ -560,7 +560,7 @@ where } } -impl FromColor> for Luma +impl FromColor> for Luma where T: FromPrimitive, { @@ -571,7 +571,7 @@ where } } -impl FromColor> for Luma +impl FromColor> for Luma where T: FromPrimitive, { @@ -597,7 +597,7 @@ where } } -impl FromColor> for LumaA +impl FromColor> for LumaA where T: FromPrimitive, { @@ -609,7 +609,7 @@ where } } -impl FromColor> for LumaA +impl FromColor> for LumaA where T: FromPrimitive, { diff --git a/src/imageops/sample.rs b/src/imageops/sample.rs index dc79552fd8..097a3ca1e3 100644 --- a/src/imageops/sample.rs +++ b/src/imageops/sample.rs @@ -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}; @@ -559,9 +559,9 @@ where } /// Local struct for keeping track of pixel sums for fast thumbnail averaging -struct ThumbnailSum(S::Larger, S::Larger, S::Larger, S::Larger); +struct ThumbnailSum(S::Larger, S::Larger, S::Larger, S::Larger); -impl ThumbnailSum { +impl ThumbnailSum { fn zeroed() -> Self { ThumbnailSum( S::Larger::zero(), @@ -601,7 +601,7 @@ pub fn thumbnail(image: &I, new_width: u32, new_height: u32) -> ImageBu where I: GenericImageView, P: Pixel + 'static, - S: Primitive + Enlargeable + 'static, + S: PrimitiveExt + 'static, { let (width, height) = image.dimensions(); let mut out = ImageBuffer::new(new_width, new_height); @@ -690,7 +690,7 @@ fn thumbnail_sample_block( where I: GenericImageView, P: Pixel, - S: Primitive + Enlargeable, + S: PrimitiveExt, { let mut sum = ThumbnailSum::zeroed(); @@ -722,7 +722,7 @@ fn thumbnail_sample_fraction_horizontal( where I: GenericImageView, P: Pixel, - S: Primitive + Enlargeable, + S: PrimitiveExt, { let fract = fraction_horizontal; @@ -766,7 +766,7 @@ fn thumbnail_sample_fraction_vertical( where I: GenericImageView, P: Pixel, - S: Primitive + Enlargeable, + S: PrimitiveExt, { let fract = fraction_vertical; @@ -808,7 +808,7 @@ fn thumbnail_sample_fraction_both( where I: GenericImageView, P: Pixel, - S: Primitive + Enlargeable, + S: PrimitiveExt, { #[allow(deprecated)] let k_bl = image.get_pixel(left, bottom).channels4(); diff --git a/src/lib.rs b/src/lib.rs index b691d10a82..11a70907ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::{ diff --git a/src/traits.rs b/src/traits.rs index 325926d2ad..56ee994100 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -51,6 +51,7 @@ macro_rules! declare_primitive { const DEFAULT_MAX_VALUE: Self = $to; const DEFAULT_MIN_VALUE: Self = $from; } + impl PrimitiveExt for $base {} }; } @@ -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 {