Skip to content

Commit

Permalink
Fix cargo features conditional compliation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivordir committed Oct 24, 2023
1 parent a41132a commit ea01a90
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 45 deletions.
2 changes: 1 addition & 1 deletion examples/simplecli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ fn main() {
.seed(seed),
)
} else {
QuantizeMethod::Wu
QuantizeMethod::wu()
};

let colorspace = colorspace.into();
Expand Down
26 changes: 22 additions & 4 deletions src/api/colorspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ use crate::{
QuantizeMethod,
};

#[cfg(all(feature = "kmeans", feature = "colorspaces"))]
use crate::kmeans::Centroids;
#[cfg(feature = "kmeans")]
use crate::{kmeans::Centroids, KmeansOptions};
use crate::KmeansOptions;
#[cfg(feature = "colorspaces")]
use crate::{wu::FloatBinner, ColorSlice};

use std::marker::PhantomData;

#[cfg(feature = "colorspaces")]
use ::palette::{IntoColor, LinSrgb, Srgb};
#[cfg(all(feature = "threads", feature = "colorspaces"))]
Expand Down Expand Up @@ -149,28 +153,42 @@ where
#[cfg(feature = "colorspaces")]
impl QuantizeMethod<Srgb<u8>> {
/// Convert the colorspace
#[allow(unused_variables)]
pub(crate) fn convert_color_space_from_srgb<Color>(
self,
convert_to: impl Fn(Srgb<u8>) -> Color,
) -> QuantizeMethod<Color> {
match self {
QuantizeMethod::Wu => QuantizeMethod::Wu,
QuantizeMethod::Wu(_) => QuantizeMethod::Wu(PhantomData),
#[cfg(feature = "kmeans")]
QuantizeMethod::Kmeans(KmeansOptions {
sampling_factor,
initial_centroids,
seed,
#[cfg(feature = "threads")]
batch_size,
}) => QuantizeMethod::Kmeans(KmeansOptions {
initial_centroids: initial_centroids.map(|c| {
Centroids::new_unchecked(c.into_inner().into_iter().map(&convert_to).collect())
}),
sampling_factor,
seed,
#[cfg(feature = "threads")]
batch_size,
}),
}
}
}

impl<Color> QuantizeMethod<Color> {
/// Creates a new [`QuantizeMethod::Wu`].
#[must_use]
pub const fn wu() -> Self {
Self::Wu(PhantomData)
}

/// Creates a new [`QuantizeMethod::Kmeans`] with the default [`KmeansOptions`].
#[must_use]
#[cfg(feature = "kmeans")]
pub const fn kmeans() -> Self {
Self::Kmeans(KmeansOptions::new())
}
}
33 changes: 19 additions & 14 deletions src/api/image_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,38 @@
use super::num_samples;

use crate::{
dither::FloydSteinberg, wu, ColorComponents, ColorCounts, ColorCountsRemap, ColorSlice,
ColorSpace, IndexedColorCounts, PalettePipeline, PaletteSize, QuantizeMethod,
dither::FloydSteinberg,
wu::{self, Binner3},
ColorComponents, ColorCounts, ColorCountsRemap, ColorSlice, ColorSpace, PalettePipeline,
PaletteSize, QuantizeMethod, SumPromotion, ZeroedIsZero,
};

#[cfg(all(feature = "colorspaces", feature = "threads"))]
use crate::colorspace::convert_color_slice_par;
#[cfg(feature = "colorspaces")]
use crate::colorspace::{convert_color_slice, from_srgb, to_srgb};
#[cfg(feature = "image")]
use crate::AboveMaxLen;
#[cfg(feature = "threads")]
use crate::ColorCountsParallelRemap;
#[cfg(feature = "colorspaces")]
use crate::{
colorspace::{convert_color_slice, from_srgb, to_srgb},
wu::Binner3,
SumPromotion, ZeroedIsZero,
};
#[cfg(any(feature = "colorspaces", feature = "kmeans"))]
use crate::IndexedColorCounts;
#[cfg(feature = "kmeans")]
use crate::{
kmeans::{self, Centroids},
KmeansOptions,
};

use num_traits::AsPrimitive;
use palette::Srgb;

#[cfg(feature = "image")]
use image::RgbImage;
#[cfg(feature = "colorspaces")]
use num_traits::AsPrimitive;
#[cfg(feature = "image")]
use palette::cast::IntoComponents;
#[cfg(feature = "colorspaces")]
use palette::{Lab, Oklab};
#[cfg(feature = "threads")]
#[cfg(all(feature = "threads", feature = "image"))]
use rayon::prelude::*;

/// A builder struct to specify options to create a quantized image or an indexed palette from an image.
Expand Down Expand Up @@ -125,6 +124,7 @@ pub struct ImagePipeline<'a> {
/// The error diffusion factor to use when dithering.
pub(crate) dither_error_diffusion: f32,
/// Whether or not to deduplicate the input pixels/colors.
#[cfg(any(feature = "kmeans", feature = "colorspaces"))]
pub(crate) dedup_pixels: bool,
}

Expand All @@ -137,9 +137,10 @@ impl<'a> ImagePipeline<'a> {
dimensions: (width, height),
k: PaletteSize::default(),
colorspace: ColorSpace::Srgb,
quantize_method: QuantizeMethod::Wu,
quantize_method: QuantizeMethod::wu(),
dither: true,
dither_error_diffusion: FloydSteinberg::DEFAULT_ERROR_DIFFUSION,
#[cfg(any(feature = "kmeans", feature = "colorspaces"))]
dedup_pixels: true,
}
}
Expand Down Expand Up @@ -274,6 +275,7 @@ impl<'a> ImagePipeline<'a> {
colors,
k,
quantize_method,
#[cfg(feature = "kmeans")]
dedup_pixels,
dimensions,
..
Expand Down Expand Up @@ -414,6 +416,7 @@ impl<'a> ImagePipeline<'a> {
colors,
k,
quantize_method,
#[cfg(feature = "kmeans")]
dedup_pixels,
dimensions,
..
Expand Down Expand Up @@ -539,6 +542,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>,
width: u32,
Expand All @@ -556,7 +560,7 @@ where
f32: AsPrimitive<Component>,
{
let (palette, mut indices) = match method {
QuantizeMethod::Wu => {
QuantizeMethod::Wu(_) => {
let res = wu::indexed_palette(color_counts, k, binner);
(res.palette, res.indices)
}
Expand Down Expand Up @@ -595,6 +599,7 @@ where

/// Computes a color palette and the indices into it in parallel.
#[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),
width: u32,
Expand All @@ -612,7 +617,7 @@ where
f32: AsPrimitive<Component>,
{
let (palette, mut indices) = match method {
QuantizeMethod::Wu => {
QuantizeMethod::Wu(_) => {
let res = wu::indexed_palette_par(color_counts, k, binner);
(res.palette, res.indices)
}
Expand Down
14 changes: 7 additions & 7 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ pub use colorspace::ColorSpace;
pub use image_pipeline::ImagePipeline;
pub use palette_pipeline::PalettePipeline;

use crate::{ColorComponents, ColorCounts};

#[cfg(feature = "kmeans")]
use crate::kmeans::Centroids;
use crate::{kmeans::Centroids, ColorComponents, ColorCounts};

use std::marker::PhantomData;

/// A builder struct to specify the parameters for k-means.
///
Expand All @@ -33,10 +33,11 @@ pub struct KmeansOptions<Color> {
/// The seed value for the random number generator.
seed: u64,
/// The batch size for minibatch k-means.
#[cfg(feature = "threads")]
#[allow(unused)]
batch_size: u32,
}

#[cfg(feature = "kmeans")]
impl<Color> Default for KmeansOptions<Color> {
fn default() -> Self {
Self::new()
Expand All @@ -47,12 +48,11 @@ impl<Color> Default for KmeansOptions<Color> {
impl<Color> KmeansOptions<Color> {
/// Creates a new [`KmeansOptions`] with default values.
#[must_use]
pub fn new() -> Self {
pub const fn new() -> Self {
Self {
sampling_factor: 0.5,
initial_centroids: None,
seed: 0,
#[cfg(feature = "threads")]
batch_size: 4096,
}
}
Expand Down Expand Up @@ -129,7 +129,7 @@ pub enum QuantizeMethod<Color> {
/// This method is quick and gives good or at least decent results.
///
/// See the [`wu`](crate::wu) module for more details.
Wu,
Wu(PhantomData<Color>),
/// Color quantization using k-means clustering.
///
/// This method is slower than Wu's color quantizer but gives more accurate results.
Expand Down
44 changes: 29 additions & 15 deletions src/api/palette_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,31 @@
use super::num_samples;

use crate::{
wu, ColorComponents, ColorCounts, ColorSlice, ColorSpace, ImagePipeline, PaletteSize,
QuantizeMethod, UniqueColorCounts,
wu::{self, Binner3},
ColorComponents, ColorCounts, ColorSlice, ColorSpace, ImagePipeline, PaletteSize,
QuantizeMethod, SumPromotion, ZeroedIsZero,
};

#[cfg(all(feature = "colorspaces", feature = "threads"))]
use crate::colorspace::convert_color_slice_par;
#[cfg(feature = "colorspaces")]
use crate::colorspace::{convert_color_slice, from_srgb, to_srgb};
#[cfg(feature = "image")]
use crate::AboveMaxLen;
#[cfg(feature = "colorspaces")]
use crate::{
colorspace::{convert_color_slice, from_srgb, to_srgb},
wu::Binner3,
SumPromotion, ZeroedIsZero,
};
#[cfg(any(feature = "colorspaces", feature = "kmeans"))]
use crate::UniqueColorCounts;
#[cfg(feature = "kmeans")]
use crate::{
kmeans::{self, Centroids},
KmeansOptions,
};

use num_traits::AsPrimitive;
use palette::Srgb;

#[cfg(feature = "image")]
use image::RgbImage;
#[cfg(feature = "colorspaces")]
use num_traits::AsPrimitive;
#[cfg(feature = "colorspaces")]
use palette::{Lab, Oklab};

/// A builder struct to specify options to create a color palette for an image or slice of colors.
Expand Down Expand Up @@ -132,6 +130,7 @@ pub struct PalettePipeline<'a> {
/// The color quantization method to use.
pub(crate) quantize_method: QuantizeMethod<Srgb<u8>>,
/// Whether or not to deduplicate the input pixels/colors.
#[cfg(any(feature = "kmeans", feature = "colorspaces"))]
pub(crate) dedup_pixels: bool,
}

Expand All @@ -143,7 +142,8 @@ impl<'a> PalettePipeline<'a> {
colors,
k: PaletteSize::default(),
colorspace: ColorSpace::Srgb,
quantize_method: QuantizeMethod::Wu,
quantize_method: QuantizeMethod::wu(),
#[cfg(any(feature = "kmeans", feature = "colorspaces"))]
dedup_pixels: true,
}
}
Expand Down Expand Up @@ -214,6 +214,7 @@ impl<'a> From<ImagePipeline<'a>> for PalettePipeline<'a> {
k,
colorspace,
quantize_method,
#[cfg(any(feature = "kmeans", feature = "colorspaces"))]
dedup_pixels,
..
} = value;
Expand All @@ -223,6 +224,7 @@ impl<'a> From<ImagePipeline<'a>> for PalettePipeline<'a> {
k,
colorspace,
quantize_method,
#[cfg(any(feature = "kmeans", feature = "colorspaces"))]
dedup_pixels,
}
}
Expand All @@ -235,7 +237,12 @@ impl<'a> PalettePipeline<'a> {
match self.colorspace {
ColorSpace::Srgb => {
let Self {
colors, k, quantize_method, dedup_pixels, ..
colors,
k,
quantize_method,
#[cfg(feature = "kmeans")]
dedup_pixels,
..
} = self;

match quantize_method {
Expand Down Expand Up @@ -315,7 +322,12 @@ impl<'a> PalettePipeline<'a> {
match self.colorspace {
ColorSpace::Srgb => {
let Self {
colors, k, quantize_method, dedup_pixels, ..
colors,
k,
quantize_method,
#[cfg(feature = "kmeans")]
dedup_pixels,
..
} = self;

match quantize_method {
Expand Down Expand Up @@ -390,6 +402,7 @@ impl<'a> PalettePipeline<'a> {
}

/// Computes a color palette.
#[allow(clippy::needless_pass_by_value)]
fn palette<Color, Component, const B: usize>(
color_counts: &impl ColorCounts<Color, Component, 3>,
k: PaletteSize,
Expand All @@ -404,7 +417,7 @@ where
f32: AsPrimitive<Component>,
{
match method {
QuantizeMethod::Wu => wu::palette(color_counts, k, binner).palette,
QuantizeMethod::Wu(_) => wu::palette(color_counts, k, binner).palette,
#[cfg(feature = "kmeans")]
QuantizeMethod::Kmeans(KmeansOptions {
sampling_factor, initial_centroids, seed, ..
Expand All @@ -422,6 +435,7 @@ where

/// Computes a color palette in parallel.
#[cfg(feature = "threads")]
#[allow(clippy::needless_pass_by_value)]
fn palette_par<Color, Component, const B: usize>(
color_counts: &(impl ColorCounts<Color, Component, 3> + Send + Sync),
k: PaletteSize,
Expand All @@ -436,7 +450,7 @@ where
f32: AsPrimitive<Component>,
{
match method {
QuantizeMethod::Wu => wu::palette_par(color_counts, k, binner).palette,
QuantizeMethod::Wu(_) => wu::palette_par(color_counts, k, binner).palette,
#[cfg(feature = "kmeans")]
QuantizeMethod::Kmeans(KmeansOptions {
sampling_factor,
Expand Down
2 changes: 1 addition & 1 deletion src/color_counts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use palette::cast::{self, AsArrays};

#[cfg(feature = "image")]
use image::RgbImage;
#[cfg(any(feature = "image", feature = "colorspaces"))]
#[cfg(feature = "image")]
use palette::Srgb;
#[cfg(feature = "threads")]
use rayon::prelude::*;
Expand Down
Loading

0 comments on commit ea01a90

Please sign in to comment.