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

no_std support #21

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 18 additions & 5 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
[package]
# NOTE: 2018 is needed else "maybe a missing crate `core`"
edition = "2021"
name = "color_quant"
license = "MIT"
version = "1.1.0"
authors = ["nwin <nwin@users.noreply.github.com>"]
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 = []
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ quantization algorithm by Anthony Dekker.
let indixes: Vec<u8> = 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'] }`
18 changes: 14 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
HeroicKatora marked this conversation as resolved.
Show resolved Hide resolved

mod math;
use crate::math::clamp;

use std::cmp::{max, min};
use alloc::vec::Vec;
use core::cmp::{max, min};

const CHANNELS: usize = 4;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down