Skip to content

Commit 0c9c839

Browse files
author
Aurelia Molzer
authored
Merge pull request #24 from bushrat011899/no_std
Add `no_std` support
2 parents 0a8f644 + f7c8168 commit 0c9c839

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

.github/workflows/rust.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
runs-on: ubuntu-latest
1010
strategy:
1111
matrix:
12-
rust: ["1.34.2", stable, beta, nightly]
12+
rust: ["1.36.0", stable, beta, nightly]
1313
command: [build, test]
1414
steps:
1515
- uses: actions/checkout@v2
@@ -18,7 +18,7 @@ jobs:
1818
run: >
1919
cargo build --verbose
2020
- name: test
21-
if: ${{ matrix.rust != '1.34.2' }}
21+
if: ${{ matrix.rust != '1.36.0' }}
2222
run: >
2323
cargo test --tests --benches
2424
# TODO: add criterion benchmarks?

src/lib.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,15 @@ that this copyright notice remain intact.
7070
//! ```
7171
7272
#![forbid(unsafe_code)]
73+
#![no_std]
7374

74-
mod math;
75-
use crate::math::clamp;
75+
extern crate alloc;
76+
77+
use alloc::{vec, vec::Vec};
78+
use core::cmp::{max, min};
7679

77-
use std::cmp::{max, min};
80+
mod math;
81+
use crate::math::{abs, clamp_round};
7882

7983
const CHANNELS: usize = 4;
8084

@@ -291,7 +295,7 @@ impl NeuQuant {
291295
/// for frequently chosen neurons, freq[i] is high and bias[i] is negative
292296
/// bias[i] = gamma*((1/self.netsize)-freq[i])
293297
fn contest(&mut self, b: f64, g: f64, r: f64, a: f64) -> i32 {
294-
use std::f64;
298+
use core::f64;
295299

296300
let mut bestd = f64::MAX;
297301
let mut bestbiasd: f64 = bestd;
@@ -302,11 +306,11 @@ impl NeuQuant {
302306
let bestbiasd_biased = bestbiasd + self.bias[i];
303307
let mut dist;
304308
let n = &self.network[i];
305-
dist = (n.b - b).abs();
306-
dist += (n.r - r).abs();
309+
dist = abs(n.b - b);
310+
dist += abs(n.r - r);
307311
if dist < bestd || dist < bestbiasd_biased {
308-
dist += (n.g - g).abs();
309-
dist += (n.a - a).abs();
312+
dist += abs(n.g - g);
313+
dist += abs(n.a - a);
310314
if dist < bestd {
311315
bestd = dist;
312316
bestpos = i as i32;
@@ -394,10 +398,10 @@ impl NeuQuant {
394398
/// initializes the color map
395399
fn build_colormap(&mut self) {
396400
for i in 0usize..self.netsize {
397-
self.colormap[i].b = clamp(self.network[i].b.round() as i32);
398-
self.colormap[i].g = clamp(self.network[i].g.round() as i32);
399-
self.colormap[i].r = clamp(self.network[i].r.round() as i32);
400-
self.colormap[i].a = clamp(self.network[i].a.round() as i32);
401+
self.colormap[i].b = clamp_round(self.network[i].b);
402+
self.colormap[i].g = clamp_round(self.network[i].g);
403+
self.colormap[i].r = clamp_round(self.network[i].r);
404+
self.colormap[i].a = clamp_round(self.network[i].a);
401405
}
402406
}
403407

@@ -423,7 +427,7 @@ impl NeuQuant {
423427
q = self.colormap[smallpos];
424428
// swap p (i) and q (smallpos) entries
425429
if i != smallpos {
426-
::std::mem::swap(&mut p, &mut q);
430+
::core::mem::swap(&mut p, &mut q);
427431
self.colormap[i] = p;
428432
self.colormap[smallpos] = q;
429433
}
@@ -445,7 +449,7 @@ impl NeuQuant {
445449
}
446450
/// Search for best matching color
447451
fn search_netindex(&self, b: u8, g: u8, r: u8, a: u8) -> usize {
448-
let mut best_dist = std::i32::MAX;
452+
let mut best_dist = core::i32::MAX;
449453
let first_guess = self.netindex[g as usize];
450454
let mut best_pos = first_guess;
451455
let mut i = best_pos;

src/math.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
1+
//! These implementations are based on `num-traits`' [`FloatCore`].
2+
//! They have been adapted to the particular needs of `color_quant` and refined
3+
//! through [feedback].
4+
//!
5+
//! [`FloatCore`]: https://docs.rs/num-traits/0.2.19/num_traits/float/trait.FloatCore.html
6+
//! [feedback]: https://github.com/image-rs/color_quant/pull/24#discussion_r2083587462
7+
18
#[inline]
2-
pub(crate) fn clamp(a: i32) -> i32 {
3-
if a < 0 {
9+
pub(crate) fn abs(a: f64) -> f64 {
10+
if a.is_sign_positive() {
11+
a
12+
} else if a.is_sign_negative() {
13+
-a
14+
} else {
15+
core::f64::NAN
16+
}
17+
}
18+
19+
#[inline]
20+
pub(crate) fn clamp_round(a: f64) -> i32 {
21+
if a < 0. {
422
0
5-
} else if a > 255 {
23+
} else if a > 255. {
624
255
725
} else {
8-
a
26+
(a + 0.5) as i32
927
}
1028
}

0 commit comments

Comments
 (0)