Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
rust: ["1.34.2", stable, beta, nightly]
rust: ["1.36.0", stable, beta, nightly]
command: [build, test]
steps:
- uses: actions/checkout@v2
Expand All @@ -18,7 +18,7 @@ jobs:
run: >
cargo build --verbose
- name: test
if: ${{ matrix.rust != '1.34.2' }}
if: ${{ matrix.rust != '1.36.0' }}
run: >
cargo test --tests --benches
# TODO: add criterion benchmarks?
Expand Down
32 changes: 18 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@ that this copyright notice remain intact.
//! ```

#![forbid(unsafe_code)]
#![no_std]

mod math;
use crate::math::clamp;
extern crate alloc;

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

use std::cmp::{max, min};
mod math;
use crate::math::{abs, clamp, round};

const CHANNELS: usize = 4;

Expand Down Expand Up @@ -291,7 +295,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 All @@ -302,11 +306,11 @@ impl NeuQuant {
let bestbiasd_biased = bestbiasd + self.bias[i];
let mut dist;
let n = &self.network[i];
dist = (n.b - b).abs();
dist += (n.r - r).abs();
dist = abs(n.b - b);
dist += abs(n.r - r);
if dist < bestd || dist < bestbiasd_biased {
dist += (n.g - g).abs();
dist += (n.a - a).abs();
dist += abs(n.g - g);
dist += abs(n.a - a);
if dist < bestd {
bestd = dist;
bestpos = i as i32;
Expand Down Expand Up @@ -394,10 +398,10 @@ impl NeuQuant {
/// initializes the color map
fn build_colormap(&mut self) {
for i in 0usize..self.netsize {
self.colormap[i].b = clamp(self.network[i].b.round() as i32);
self.colormap[i].g = clamp(self.network[i].g.round() as i32);
self.colormap[i].r = clamp(self.network[i].r.round() as i32);
self.colormap[i].a = clamp(self.network[i].a.round() as i32);
self.colormap[i].b = clamp(round(self.network[i].b) as i32);
self.colormap[i].g = clamp(round(self.network[i].g) as i32);
self.colormap[i].r = clamp(round(self.network[i].r) as i32);
self.colormap[i].a = clamp(round(self.network[i].a) as i32);
}
}

Expand All @@ -423,7 +427,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 @@ -445,7 +449,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
42 changes: 42 additions & 0 deletions src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,45 @@ pub(crate) fn clamp(a: i32) -> i32 {
a
}
}

#[inline]
fn fract(a: f64) -> f64 {
if a == 0.0 {
0.
} else {
a % 1.
}
}

#[inline]
pub(crate) fn round(a: f64) -> f64 {
let one = 1.0;
let h = 0.5;
let f = fract(a);
if f.is_nan() || f == 0.0 {
a
} else if a > 0. {
if f < h {
a - f
} else {
a - f + one
}
} else if -f < h {
a - f
} else {
a - f - one
}
}

#[inline]
pub(crate) fn abs(a: f64) -> f64 {
if a.is_sign_positive() {
return a;
}

if a.is_sign_negative() {
return -a;
}

core::f64::NAN
}
Loading