Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 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,15 @@ 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 no-std-check --no-default-features --features=num-traits
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
[package]
# NOTE: 2018 is needed else "maybe a missing crate `core`"
edition = "2021"
name = "color_quant"
license = "MIT"
version = "1.1.0"
authors = ["nwin <[email protected]>"]
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 = []
num-traits = ["dep: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;

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