@@ -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
7983const 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;
0 commit comments