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

Replace the Pixel trait with ArrayCast and cast functions and increase the MSRV to 1.51.0 #254

Merged
merged 1 commit into from
Aug 21, 2021

Conversation

Ogeon
Copy link
Owner

@Ogeon Ogeon commented Aug 14, 2021

This replaces the Pixel trait with a new system for casting that is based on fixed size arrays as a primitive representation. The initial implementation covers most of the use cases for Pixel and some additional features (casting Box<[T]> and Vec<T> for example). The missing feature is the implicit truncation Pixel did. The new system leaves it to the user to truncate collections where necessary.

I couldn't come up with a good trait system that would be universal enough, so I decided to start with this setup with free functions. It will at least give us the base to build something more convenient on.

See #227 for some background and an earlier solution idea.

Breaking Change

This removes the Pixel and RawPixel traits and replaces it with a system that is based on the ArrayCast marker trait and a number of primitive cast functions.

  • Single color casting is available through From, Into, AsRef, AsMut, TryFrom and TryInto.
  • Slices and Vec can currently only be cast via the functions in the cast module. Something more ergonomic may come in a later change.

The MSRV is bumped to 1.51.0 to get access to const generics.

@Ogeon Ogeon changed the title Replace the Pixel trait with ArrayCast and cast functions Replace the Pixel trait with ArrayCast and cast functions and increase the MSRV to 1.51.0 Aug 14, 2021
@github-actions
Copy link

Benchmark for 4717c56

Click to view benchmark
Test PR Benchmark Master Benchmark %
Cie family/lab to lch 3.1±0.09µs 3.3±0.10µs -6.06%
Cie family/lab to xyz 927.4±27.85ns 870.2±29.54ns +6.57%
Cie family/lch to lab 2.2±0.08µs 2.3±0.08µs -4.35%
Cie family/linsrgb to xyz 3.8±0.15µs 3.8±0.14µs 0.00%
Cie family/xyz to lab 10.5±0.41µs 10.0±0.46µs +5.00%
Cie family/xyz to yxy 735.4±21.79ns 734.9±51.24ns +0.07%
Cie family/yxy to xyz 630.2±21.64ns 613.9±24.48ns +2.66%
Matrix functions/matrix_inverse 11.2±0.63ns 11.8±0.47ns -5.08%
Matrix functions/multiply_3x3 9.8±0.35ns 10.1±0.37ns -2.97%
Matrix functions/multiply_rgb_to_xyz 4.4±0.16ns 4.3±0.14ns +2.33%
Matrix functions/multiply_xyz 4.3±0.15ns 4.5±0.17ns -4.44%
Matrix functions/multiply_xyz_to_rgb 4.3±0.14ns 4.4±0.14ns -2.27%
Matrix functions/rgb_to_xyz_matrix 23.1±0.74ns 22.9±0.81ns +0.87%
Rgb family/hsl to hsv 731.2±39.91ns 763.0±31.60ns -4.17%
Rgb family/hsl to linear hsl 8.7±0.30µs 8.9±0.23µs -2.25%
Rgb family/hsl to rgb 2.3±0.08µs 2.3±0.08µs 0.00%
Rgb family/hsv to hsl 1101.3±32.43ns 1122.2±36.96ns -1.86%
Rgb family/hsv to hwb 251.8±9.89ns 250.7±9.10ns +0.44%
Rgb family/hsv to linear hsv 8.2±0.27µs 8.2±0.37µs 0.00%
Rgb family/hsv to rgb 2.3±0.07µs 2.3±0.11µs 0.00%
Rgb family/hwb to hsv 553.3±18.51ns 576.5±18.95ns -4.02%
Rgb family/hwb to linear hwb 8.6±0.31µs 8.6±0.35µs 0.00%
Rgb family/linear hsl to hsl 9.4±0.33µs 8.8±0.51µs +6.82%
Rgb family/linear hsv to hsv 8.8±0.36µs 8.7±0.31µs +1.15%
Rgb family/linear hwb to hwb 9.7±0.37µs 9.9±0.32µs -2.02%
Rgb family/linsrgb to rgb 4.4±0.18µs 4.3±0.18µs +2.33%
Rgb family/linsrgb_f32 to rgb_u8 5.8±0.19µs 5.7±0.21µs +1.75%
Rgb family/rgb to hsl 763.5±22.02ns 775.6±32.41ns -1.56%
Rgb family/rgb to hsv 585.6±18.25ns 600.9±27.34ns -2.55%
Rgb family/rgb to linsrgb 4.5±0.19µs 4.5±0.16µs 0.00%
Rgb family/rgb_u8 to linsrgb_f32 5.0±0.16µs 5.1±0.17µs -1.96%
Rgb family/xyz to linsrgb 6.6±0.11µs 6.3±0.30µs +4.76%

@github-actions
Copy link

Benchmark for 18b8f21

Click to view benchmark
Test PR Benchmark Master Benchmark %
Cie family/lab to lch 3.1±0.19µs 2.9±0.18µs +6.90%
Cie family/lab to xyz 820.9±46.84ns 805.8±34.10ns +1.87%
Cie family/lch to lab 2.1±0.12µs 2.1±0.11µs 0.00%
Cie family/linsrgb to xyz 3.4±0.15µs 3.5±0.23µs -2.86%
Cie family/xyz to lab 9.2±0.57µs 9.2±0.50µs 0.00%
Cie family/xyz to yxy 650.6±30.82ns 663.2±34.63ns -1.90%
Cie family/yxy to xyz 571.3±31.99ns 563.5±25.71ns +1.38%
Matrix functions/matrix_inverse 10.9±0.65ns 11.1±0.61ns -1.80%
Matrix functions/multiply_3x3 9.0±0.45ns 8.8±0.44ns +2.27%
Matrix functions/multiply_rgb_to_xyz 4.1±0.23ns 4.1±0.24ns 0.00%
Matrix functions/multiply_xyz 4.1±0.29ns 4.1±0.22ns 0.00%
Matrix functions/multiply_xyz_to_rgb 4.0±0.24ns 4.0±0.20ns 0.00%
Matrix functions/rgb_to_xyz_matrix 20.9±1.08ns 21.7±1.29ns -3.69%
Rgb family/hsl to hsv 712.0±44.19ns 731.9±38.35ns -2.72%
Rgb family/hsl to linear hsl 7.9±0.33µs 7.9±0.46µs 0.00%
Rgb family/hsl to rgb 2.1±0.10µs 2.1±0.13µs 0.00%
Rgb family/hsv to hsl 1022.4±70.95ns 1030.6±47.62ns -0.80%
Rgb family/hsv to hwb 228.8±14.20ns 233.4±13.74ns -1.97%
Rgb family/hsv to linear hsv 7.5±0.30µs 7.7±0.40µs -2.60%
Rgb family/hsv to rgb 2.1±0.12µs 2.0±0.09µs +5.00%
Rgb family/hwb to hsv 516.3±26.14ns 535.4±20.94ns -3.57%
Rgb family/hwb to linear hwb 8.5±0.59µs 7.9±0.41µs +7.59%
Rgb family/linear hsl to hsl 8.6±0.38µs 8.5±0.50µs +1.18%
Rgb family/linear hsv to hsv 8.3±0.47µs 8.3±0.37µs 0.00%
Rgb family/linear hwb to hwb 9.1±0.55µs 8.7±0.49µs +4.60%
Rgb family/linsrgb to rgb 4.1±0.26µs 3.9±0.22µs +5.13%
Rgb family/linsrgb_f32 to rgb_u8 5.6±0.30µs 5.5±0.41µs +1.82%
Rgb family/rgb to hsl 741.8±42.56ns 744.9±46.04ns -0.42%
Rgb family/rgb to hsv 545.6±26.76ns 547.5±28.60ns -0.35%
Rgb family/rgb to linsrgb 4.3±0.22µs 4.2±0.20µs +2.38%
Rgb family/rgb_u8 to linsrgb_f32 4.8±0.24µs 4.6±0.32µs +4.35%
Rgb family/xyz to linsrgb 5.7±0.25µs 5.9±0.34µs -3.39%

@Ogeon
Copy link
Owner Author

Ogeon commented Aug 21, 2021

bors r+

@bors
Copy link
Contributor

bors bot commented Aug 21, 2021

Build succeeded:

@bors bors bot merged commit b0f8ae0 into master Aug 21, 2021
@bors bors bot deleted the new_pixel_trait branch August 21, 2021 13:27
bors bot added a commit that referenced this pull request Aug 22, 2021
255: Add unsigned integer casting to `cast` and make `Packed` general purpose r=Ogeon a=Ogeon

I have added a `UintCast` trait and casting functions, similar to what I did for arrays in #254, to make it less awkward when casting collections of unsigned integers. I have also made `Packed` a part of `cast` and made it work with any color space and any storage format (including arrays and other unsigned integers). It implements `ArrayCast` if it wraps an array and `UintCast` if it wraps an unsigned integer.

There's also a bunch of related QoL changes for `Rgb` and `Luma` (which has gotten `Packed` compatibility), as well as some code that has been moved around.

## Breaking Change

The `Packed` struct takes its storage format as a type parameter, is no longer specific to RGB, and has moved into `cast`. `RgbChannels` has been renamed to `ComponentOrder` and has been moved into `cast`.


Co-authored-by: Erik Hedvall <erikwhedvall@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant