Skip to content

Commit

Permalink
Remove unnecessary ColorCountsParallelRemap trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivordir committed Jan 6, 2025
1 parent f41407c commit 94634f4
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 110 deletions.
18 changes: 8 additions & 10 deletions src/api/image_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
#[cfg(all(feature = "colorspaces", feature = "threads"))]
use crate::colorspace::convert_color_slice_par;
#[cfg(feature = "threads")]
use crate::ColorCountsParallelRemap;
#[cfg(any(feature = "colorspaces", feature = "kmeans"))]
use crate::IndexedColorCounts;
use crate::{
dither::FloydSteinberg,
wu::{self, Binner3},
ColorComponents, ColorCounts, ColorCountsRemap, ColorSlice, ColorSpace, PalettePipeline,
PaletteSize, QuantizeMethod, SumPromotion, ZeroedIsZero,
ColorComponents, ColorCounts, ColorSlice, ColorSpace, PalettePipeline, PaletteSize,
QuantizeMethod, RemappableColorCounts, SumPromotion, ZeroedIsZero,
};
use num_traits::AsPrimitive;
use palette::Srgb;
Expand Down Expand Up @@ -249,7 +247,7 @@ impl<'a> TryFrom<&'a RgbImage> for ImagePipeline<'a> {
}
}

impl<'a> ImagePipeline<'a> {
impl ImagePipeline<'_> {
/// Runs the pipeline and returns the computed color palette.
#[must_use]
pub fn palette(&self) -> Vec<Srgb<u8>> {
Expand Down Expand Up @@ -377,7 +375,7 @@ impl<'a> ImagePipeline<'a> {
}

#[cfg(feature = "image")]
impl<'a> ImagePipeline<'a> {
impl ImagePipeline<'_> {
/// Runs the pipeline and returns the quantized image.
#[must_use]
pub fn quantized_rgbimage(&self) -> RgbImage {
Expand All @@ -401,7 +399,7 @@ impl<'a> ImagePipeline<'a> {
}

#[cfg(feature = "threads")]
impl<'a> ImagePipeline<'a> {
impl ImagePipeline<'_> {
/// Runs the pipeline in parallel and returns the computed color palette.
#[must_use]
pub fn palette_par(&self) -> Vec<Srgb<u8>> {
Expand Down Expand Up @@ -519,7 +517,7 @@ impl<'a> ImagePipeline<'a> {
}

#[cfg(all(feature = "threads", feature = "image"))]
impl<'a> ImagePipeline<'a> {
impl ImagePipeline<'_> {
/// Runs the pipeline in parallel and returns the quantized image.
#[must_use]
pub fn quantized_rgbimage_par(&self) -> RgbImage {
Expand All @@ -545,7 +543,7 @@ impl<'a> ImagePipeline<'a> {
/// Computes a color palette and the indices into it.
#[allow(clippy::needless_pass_by_value)]
fn indexed_palette<Color, Component, const B: usize>(
color_counts: &impl ColorCountsRemap<Color, Component, 3>,
color_counts: &impl RemappableColorCounts<Color, Component, 3>,
width: u32,
height: u32,
k: PaletteSize,
Expand Down Expand Up @@ -597,7 +595,7 @@ where
#[cfg(feature = "threads")]
#[allow(clippy::needless_pass_by_value)]
fn indexed_palette_par<Color, Component, const B: usize>(
color_counts: &(impl ColorCountsParallelRemap<Color, Component, 3> + Send + Sync),
color_counts: &(impl RemappableColorCounts<Color, Component, 3> + Send + Sync),
width: u32,
height: u32,
k: PaletteSize,
Expand Down
4 changes: 2 additions & 2 deletions src/api/palette_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl<'a> From<ImagePipeline<'a>> for PalettePipeline<'a> {
}
}

impl<'a> PalettePipeline<'a> {
impl PalettePipeline<'_> {
/// Runs the pipeline and returns the computed color palette.
#[must_use]
pub fn palette(&self) -> Vec<Srgb<u8>> {
Expand Down Expand Up @@ -288,7 +288,7 @@ impl<'a> PalettePipeline<'a> {
}

#[cfg(feature = "threads")]
impl<'a> PalettePipeline<'a> {
impl PalettePipeline<'_> {
/// Runs the pipeline in parallel and returns the computed color palette.
#[must_use]
pub fn palette_par(&self) -> Vec<Srgb<u8>> {
Expand Down
107 changes: 42 additions & 65 deletions src/color_counts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ where
/// As such, the length of this slice must also not be greater than [`MAX_PIXELS`](crate::MAX_PIXELS).
fn counts(&self) -> Option<&[u32]>;

/// A slice of indices into the color slice returned by `colors`.
/// This is used to retain the original color slice/image after deduplication.
///
/// The length of this slice must not be greater than [`MAX_PIXELS`](crate::MAX_PIXELS),
/// and each index must be valid index into the slice returned by `colors`.
fn indices(&self) -> Option<&[u32]>;

/// The slice returned by `colors` casted to a slice of component arrays.
fn color_components(&self) -> &[[Component; N]] {
self.colors().as_arrays()
Expand All @@ -68,8 +61,7 @@ where
}
}

impl<'a, Color, Component, const N: usize> ColorCounts<Color, Component, N>
for ColorSlice<'a, Color>
impl<Color, Component, const N: usize> ColorCounts<Color, Component, N> for ColorSlice<'_, Color>
where
Color: ColorComponents<Component, N>,
{
Expand All @@ -85,10 +77,6 @@ where
None
}

fn indices(&self) -> Option<&[u32]> {
None
}

fn num_colors(&self) -> u32 {
self.num_colors()
}
Expand All @@ -115,10 +103,6 @@ where
Some(&self.counts)
}

fn indices(&self) -> Option<&[u32]> {
None
}

fn total_count(&self) -> u32 {
self.total_count
}
Expand All @@ -137,92 +121,85 @@ where
Some(&self.counts)
}

fn indices(&self) -> Option<&[u32]> {
Some(&self.indices)
}

fn total_count(&self) -> u32 {
self.total_count
}
}

/// Types that allow reconstructing the original image/color slice from a `Vec` of indices
/// into a color palette.
pub trait ColorCountsRemap<Color, Component, const N: usize>:
pub trait RemappableColorCounts<Color, Component, const N: usize>:
ColorCounts<Color, Component, N>
where
Color: ColorComponents<Component, N>,
{
/// Uses the given `Vec` of indices to create indices for each pixel/color
/// in the original image/color slice.
/// A slice of indices into the color slice returned by `colors`.
/// This is a mapping from pixels to `colors`, and is used to retain
/// the original color slice/image after pixel deduplication.
///
/// The given `Vec` will have the same length as the color slice returned by `colors`.
/// For [`ColorSlice`]s, this returns `None`, indicating that the original pixels
/// and `colors` are the same.
///
/// The length of the returned `Vec` must not be greater than [`MAX_PIXELS`](crate::MAX_PIXELS).
fn map_indices(&self, indices: Vec<u8>) -> Vec<u8>;
/// The length of this slice must not be greater than [`MAX_PIXELS`](crate::MAX_PIXELS),
/// and each index must be valid index into the slice returned by `colors`.
fn indices(&self) -> Option<&[u32]>;
}

impl<'a, Color, Component, const N: usize> ColorCountsRemap<Color, Component, N>
for ColorSlice<'a, Color>
impl<Color, Component, const N: usize> RemappableColorCounts<Color, Component, N>
for ColorSlice<'_, Color>
where
Color: ColorComponents<Component, N>,
{
fn map_indices(&self, indices: Vec<u8>) -> Vec<u8> {
indices
fn indices(&self) -> Option<&[u32]> {
None
}
}

impl<Color, Component, const N: usize> ColorCountsRemap<Color, Component, N>
impl<Color, Component, const N: usize> RemappableColorCounts<Color, Component, N>
for IndexedColorCounts<Color, Component, N>
where
Color: ColorComponents<Component, N>,
{
fn map_indices(&self, indices: Vec<u8>) -> Vec<u8> {
let indices = indices.as_slice(); // faster for some reason???
self.indices.iter().map(|&i| indices[i as usize]).collect()
fn indices(&self) -> Option<&[u32]> {
Some(&self.indices)
}
}

/// Types that allow reconstructing the original image/color slice in parallel
/// from a `Vec` of indices into a color palette.
#[cfg(feature = "threads")]
pub trait ColorCountsParallelRemap<Color, Component, const N: usize>:
ColorCounts<Color, Component, N>
where
Color: ColorComponents<Component, N>,
{
/// Uses the given `Vec` of indices to create indices, in parallel, for each pixel/color
/// in the original image/color slice.
///
/// The given `Vec` will have the same length as the color slice returned by `colors`.
///
/// The length of the returned `Vec` must not be greater than [`MAX_PIXELS`](crate::MAX_PIXELS).
fn map_indices_par(&self, indices: Vec<u8>) -> Vec<u8>;
}

#[cfg(feature = "threads")]
impl<'a, Color, Component, const N: usize> ColorCountsParallelRemap<Color, Component, N>
for ColorSlice<'a, Color>
/// Given a mapping from `colors` to palette colors,
/// returns a mapping from pixels to palette colors.
pub(crate) fn remap_indices<Color, Component, const N: usize>(
color_counts: &impl RemappableColorCounts<Color, Component, N>,
palette_indices: Vec<u8>,
) -> Vec<u8>
where
Color: ColorComponents<Component, N>,
{
fn map_indices_par(&self, indices: Vec<u8>) -> Vec<u8> {
indices
if let Some(color_indices) = color_counts.indices() {
let indices = palette_indices.as_slice(); // faster for some reason???
color_indices.iter().map(|&i| indices[i as usize]).collect()
} else {
palette_indices
}
}

/// Given a mapping from `colors` to palette color index,
/// returns a mapping from pixels to palette color.
#[cfg(feature = "threads")]
impl<Color, Component, const N: usize> ColorCountsParallelRemap<Color, Component, N>
for IndexedColorCounts<Color, Component, N>
pub(crate) fn remap_indices_par<Color, Component, const N: usize>(
color_counts: &impl RemappableColorCounts<Color, Component, N>,
palette_indices: Vec<u8>,
) -> Vec<u8>
where
Color: ColorComponents<Component, N>,
{
fn map_indices_par(&self, indices: Vec<u8>) -> Vec<u8> {
let indices = indices.as_slice(); // faster for some reason???
self.indices
if let Some(color_indices) = color_counts.indices() {
let indices = palette_indices.as_slice(); // faster for some reason???
color_indices
.par_iter()
.map(|&i| indices[i as usize])
.collect()
} else {
palette_indices
}
}

Expand Down Expand Up @@ -669,8 +646,8 @@ mod sync_unsafe {
written: Vec<AtomicBool>,
}

unsafe impl<'a, T: Send + Sync> Send for SyncUnsafeSlice<'a, T> {}
unsafe impl<'a, T: Send + Sync> Sync for SyncUnsafeSlice<'a, T> {}
unsafe impl<T: Send + Sync> Send for SyncUnsafeSlice<'_, T> {}
unsafe impl<T: Send + Sync> Sync for SyncUnsafeSlice<'_, T> {}

impl<'a, T> SyncUnsafeSlice<'a, T> {
/// Creates a new [`SyncUnsafeSlice`] with the given slice.
Expand Down Expand Up @@ -701,7 +678,7 @@ mod sync_unsafe {
}
}

impl<'a, T: Copy> SyncUnsafeSlice<'a, T> {
impl<T: Copy> SyncUnsafeSlice<'_, T> {
/// Unsafely write the given slice to the given range.
///
/// # Safety
Expand Down
Loading

0 comments on commit 94634f4

Please sign in to comment.