Skip to content

Commit

Permalink
Merge pull request #8 from TianyiShi2001/remove-clamp-macro
Browse files Browse the repository at this point in the history
replace the clamp macro with the clamp function
  • Loading branch information
HeroicKatora authored Oct 9, 2020
2 parents 3a1d0c1 + 40a4daf commit cda22e6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,15 @@ that this copyright notice remain intact.
//! let indixes: Vec<u8> = data.chunks(4).map(|pix| nq.index_of(pix) as u8).collect();
//! let color_map = nq.color_map_rgba();
//! ```
//!
mod math;
use crate::math::clamp;

use std::cmp::{
max,
min
};

macro_rules! clamp(
($x:expr) => (match $x {
x if x < 0 => 0,
x if x > 255 => 255,
x => x
})
);

const CHANNELS: usize = 4;

const RADIUS_DEC: i32 = 30; // factor of 1/30 each cycle
Expand Down Expand Up @@ -337,10 +332,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(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);
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/math.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[inline]
pub(crate) fn clamp(a: i32) -> i32
{
if a < 0 {
0
} else if a > 255 {
255
} else {
a
}
}

0 comments on commit cda22e6

Please sign in to comment.