diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index abc9f37..c7d9381 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -9,18 +9,18 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - rust: ["1.34.2", stable, beta, nightly] - command: [build, test] + # alloc crate requires Rust 1.36 + rust: ["1.56", stable, beta, nightly] + features: ["default", "num-traits"] steps: - uses: actions/checkout@v2 - run: rustup default ${{ matrix.rust }} - name: build run: > - cargo build --verbose + cargo build --no-default-features --features=${{ matrix.features }} --verbose - name: test - if: ${{ matrix.rust != '1.34.2' }} run: > - cargo test --tests --benches + cargo test --no-default-features --features=${{ matrix.features }} --tests --benches # TODO: add criterion benchmarks? rustfmt: runs-on: ubuntu-latest @@ -36,3 +36,16 @@ jobs: with: command: fmt args: -- --check + + no_std_check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + toolchain: nightly + override: true + - name: build + run: | + cargo install cargo-no-std-check + cargo no-std-check --no-default-features --features=num-traits diff --git a/Cargo.toml b/Cargo.toml index 727e42c..0880d7f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,6 @@ [package] +# NOTE: 2018 is needed else "maybe a missing crate `core`" +edition = "2021" name = "color_quant" license = "MIT" version = "1.1.0" @@ -6,3 +8,11 @@ authors = ["nwin "] readme = "README.md" description = "Color quantization library to reduce n colors to 256 colors." repository = "https://github.com/image-rs/color_quant.git" +rust-version = "1.56" + +[dependencies] +num-traits = { version = "0.2", default-features = false, optional = true } + +[features] +default = ["std"] +std = [] \ No newline at end of file diff --git a/README.md b/README.md index 0644ad6..85b7b08 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,7 @@ quantization algorithm by Anthony Dekker. let indixes: Vec = data.chunks(4).map(|pix| nq.index_of(pix) as u8).collect(); let color_map = nq.color_map_rgba(); +#### no_std + +You should be able to use this crate in `no_std` environment e.g.: +`color_quant = { version = "1.1", default-features = false, features = ['num-traits'] }` \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 13c08cb..6aa2bed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,10 +69,20 @@ that this copyright notice remain intact. //! let color_map = nq.color_map_rgba(); //! ``` +#![cfg_attr(not(feature = "std"), no_std)] + +#[macro_use] +extern crate alloc; + +#[cfg(all(feature = "num-traits", not(feature = "std")))] +#[allow(unused_imports)] +use num_traits::float::FloatCore; + mod math; use crate::math::clamp; -use std::cmp::{max, min}; +use alloc::vec::Vec; +use core::cmp::{max, min}; const CHANNELS: usize = 4; @@ -279,7 +289,7 @@ impl NeuQuant { /// for frequently chosen neurons, freq[i] is high and bias[i] is negative /// bias[i] = gamma*((1/self.netsize)-freq[i]) fn contest(&mut self, b: f64, g: f64, r: f64, a: f64) -> i32 { - use std::f64; + use core::f64; let mut bestd = f64::MAX; let mut bestbiasd: f64 = bestd; @@ -411,7 +421,7 @@ impl NeuQuant { q = self.colormap[smallpos]; // swap p (i) and q (smallpos) entries if i != smallpos { - ::std::mem::swap(&mut p, &mut q); + ::core::mem::swap(&mut p, &mut q); self.colormap[i] = p; self.colormap[smallpos] = q; } @@ -433,7 +443,7 @@ impl NeuQuant { } /// Search for best matching color fn search_netindex(&self, b: u8, g: u8, r: u8, a: u8) -> usize { - let mut best_dist = std::i32::MAX; + let mut best_dist = core::i32::MAX; let first_guess = self.netindex[g as usize]; let mut best_pos = first_guess; let mut i = best_pos;