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
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
[package]
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"

[dependencies]
# libm: "Uncomment if you wish to use `Float` and `Real` without `std`"
n-prat marked this conversation as resolved.
Show resolved Hide resolved
num-traits = { version = "0.2", default-features = false }

[features]
libm = ["num-traits/libm"]
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();
//! ```

// #![no_std]
#![cfg_attr(not(feature = "std"), no_std)]

#[macro_use]
extern crate alloc;

#[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