lib: Remove dependency on std

This commit is contained in:
Jan Solanti
2018-08-29 15:50:10 +09:00
parent 5b4fef6c42
commit 6edb7b043a
11 changed files with 54 additions and 56 deletions
+10 -10
View File
@@ -21,7 +21,7 @@
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
use std::u32;
use core::{u8, u32};
use ::f32_to_i32_clamped;
@@ -53,7 +53,7 @@ pub fn compress_alpha_dxt3(
tmp[i] = quant1 | (quant2 << 4)
}
block.clone_from_slice(&tmp);
block.copy_from_slice(&tmp);
}
pub fn decompress_alpha_dxt3(rgba: &mut [[u8; 4]; 16], bytes: &[u8]) {
@@ -75,7 +75,7 @@ pub fn decompress_alpha_dxt3(rgba: &mut [[u8; 4]; 16], bytes: &[u8]) {
fn fix_range(min: &mut u8, max: &mut u8, steps: u8) {
if (*max-*min) < steps {
*max = (*min as i32 + steps as i32).min(255) as u8;
*max = (*min as i32 + steps as i32).min(u8::MAX as i32) as u8;
}
if (*max-*min) < steps {
*min = (*max as i32 - steps as i32).max(0) as u8;
@@ -147,10 +147,10 @@ fn write_alpha_block(
// store in 3 bytes
let mut tmp = &mut buf[2+i*3..5+i*3];
for j in 0..tmp.len() {
tmp[j] = ((value >> 8*j) & 0xff) as u8;
tmp[j] = ((value >> 8*j) & 0xFF) as u8;
}
}
block.clone_from_slice(&buf);
block.copy_from_slice(&buf);
}
fn write_alpha_block5(
@@ -210,9 +210,9 @@ pub fn compress_alpha_dxt5(
block: &mut [u8]
) {
// get range for 5-alpha and 7-alpha interpolation
let mut min5 = 255u8;
let mut min5 = u8::MAX;
let mut max5 = 0u8;
let mut min7 = 255u8;
let mut min7 = u8::MAX;
let mut max7 = 0u8;
for i in 0..rgba.len() {
@@ -230,7 +230,7 @@ pub fn compress_alpha_dxt5(
if value != 0 {
min5 = min5.min(value);
}
if value != 255 {
if value != u8::MAX {
max5 = max5.max(value);
}
}
@@ -255,7 +255,7 @@ pub fn compress_alpha_dxt5(
codes5[1+i as usize] = (((5 - i)*min5 as i32 + i*max5 as i32)/5) as u8;
}
codes5[6] = 0;
codes5[7] = 255;
codes5[7] = u8::MAX;
// set up the 7-alpha codebook
let mut codes7 = [0u8; 8];
@@ -296,7 +296,7 @@ pub fn decompress_alpha_dxt5(rgba: &mut [[u8; 4]; 16], bytes: &[u8]) {
codes[1+i as usize] = (((5 - i)*alpha0 + i*alpha1)/5) as u8
}
codes[6] = 0;
codes[7] = 255;
codes[7] = u8::MAX;
} else {
// use 7-alpha codebook
for i in 1..7i32 {
+7 -7
View File
@@ -21,9 +21,9 @@
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
use std::mem;
use core::mem;
use byteorder::{ByteOrder, LittleEndian, WriteBytesExt};
use byteorder::{ByteOrder, LittleEndian};
use math::Vec3;
use ::f32_to_i32_clamped;
@@ -46,8 +46,8 @@ fn write_block(
block: &mut [u8]
) {
// write endpoints
<&mut [u8]>::write_u16::<LittleEndian>(&mut &mut block[0..2], a).unwrap();
<&mut [u8]>::write_u16::<LittleEndian>(&mut &mut block[2..4], b).unwrap();
LittleEndian::write_u16(&mut &mut block[0..2], a);
LittleEndian::write_u16(&mut &mut block[2..4], b);
// write 2-bit LUT indices
let mut packed = [0u8; 4];
@@ -58,7 +58,7 @@ fn write_block(
| (indices[4*i] & 0x03);
}
block[4..].clone_from_slice(&packed);
block[4..].copy_from_slice(&packed);
}
@@ -140,8 +140,8 @@ pub fn decompress_colour(bytes: &[u8], is_dxt1: bool) -> [[u8; 4]; 16] {
// unpack endpoints
let a = LittleEndian::read_u16(&bytes[0..1]);
let b = LittleEndian::read_u16(&bytes[2..3]);
codes[0..4].clone_from_slice(&unpack_565(a)[..]);
codes[4..8].clone_from_slice(&unpack_565(b)[..]);
codes[0..4].copy_from_slice(&unpack_565(a)[..]);
codes[4..8].copy_from_slice(&unpack_565(b)[..]);
// generate intermediate values
for i in 0..4 {
+5 -8
View File
@@ -21,8 +21,8 @@
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
use std::f32;
use std::cmp::Ordering;
use core::f32;
use core::cmp::Ordering;
use ::{ColourWeights, Format};
use ::colourblock::*;
@@ -85,9 +85,9 @@ impl<'a> ClusterFit<'a> {
let values = self.colourset.points();
// build list of dot products
let mut dps = [0f32; 16];
let mut dps = [(0usize, 0f32); 16];
for i in 0..count {
dps[i] = values[i].dot(axis);
dps[i] = (i, values[i].dot(axis));
}
// sort fn for floats - NaN & Inf are pushed to the end of the list
@@ -100,12 +100,9 @@ impl<'a> ClusterFit<'a> {
}
}
// number dot products
let mut dps = dps.iter()
.take(count)
.enumerate().collect::<Vec<(usize, &f32)>>();
// sort numbered list based on dot product value
dps.sort_by(|a, b| fcmp(&a.1, &b.1));
// this is our ordering now
for (a, b) in self.order[iteration].iter_mut().zip(dps.iter()) {
*a = b.0 as u8;
+1 -1
View File
@@ -57,6 +57,6 @@ impl<'a, T> ColourFit<'a> for T where T: ColourFitImpl<'a> {
self.compress4();
}
block.clone_from_slice(self.best_compressed());
block.copy_from_slice(self.best_compressed());
}
}
+1 -1
View File
@@ -21,7 +21,7 @@
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
use std::f32;
use core::f32;
use ::{ColourWeights, Format};
use ::colourblock::*;
+1 -1
View File
@@ -21,7 +21,7 @@
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
use std::u32;
use core::u32;
use ::{Format, f32_to_i32_clamped};
use ::colourblock::*;
+22 -20
View File
@@ -24,10 +24,13 @@
//! A pure Rust DXT1/3/5 compressor and decompressor based on Simon Brown's
//! **libsquish**
#![no_std]
extern crate byteorder;
use std::str::FromStr;
use std::fmt;
use core::str::FromStr;
use core::fmt;
mod alpha;
mod colourblock;
@@ -54,7 +57,7 @@ pub enum ParseFormatError {
impl fmt::Display for ParseFormatError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "InvalidFormat")
write!(f, "Not a valid")
}
}
@@ -73,7 +76,7 @@ impl FromStr for Format {
/// Defines a compression algorithm
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum CompressionAlgorithm {
pub enum Algorithm {
/// Fast, low quality
RangeFit,
@@ -84,9 +87,9 @@ pub enum CompressionAlgorithm {
IterativeClusterFit,
}
impl Default for CompressionAlgorithm {
impl Default for Algorithm {
fn default() -> Self {
CompressionAlgorithm::ClusterFit
Algorithm::ClusterFit
}
}
@@ -94,17 +97,15 @@ impl Default for CompressionAlgorithm {
pub type ColourWeights = [f32; 3];
/// Uniform weights for each colour channel
#[allow(unused)]
pub const COLOUR_WEIGHTS_UNIFORM: ColourWeights = [1.0, 1.0, 1.0];
/// Weights based on the perceived brightness of each colour channel
#[allow(unused)]
pub const COLOUR_WEIGHTS_PERCEPTUAL: ColourWeights = [0.2126, 0.7152, 0.0722];
#[derive(Clone, Copy)]
pub struct CompressorParams {
pub struct Params {
/// The compression algorithm to be used
pub algorithm: CompressionAlgorithm,
pub algorithm: Algorithm,
/// Weigh the relative importance of each colour channel when fitting
/// (defaults to perceptual weights)
@@ -117,10 +118,10 @@ pub struct CompressorParams {
pub weigh_colour_by_alpha: bool,
}
impl Default for CompressorParams {
impl Default for Params {
fn default() -> Self {
CompressorParams {
algorithm: CompressionAlgorithm::default(),
Params {
algorithm: Algorithm::default(),
weights: COLOUR_WEIGHTS_PERCEPTUAL,
weigh_colour_by_alpha: false,
}
@@ -138,8 +139,9 @@ pub fn decompress(
width: usize,
height: usize,
format: Format,
) -> Vec<u8> {
vec![]
output: &mut [u8]
) {
}
/// Returns how many bytes a 4x4 block of pixels will take after compression,
@@ -183,10 +185,10 @@ fn compress_block_masked(
rgba: [[u8; 4]; 16],
mask: u32,
format: Format,
params: CompressorParams,
params: Params,
output: &mut [u8]
) {
use CompressionAlgorithm as Algo;
use Algorithm as Algo;
// compress alpha separately if necessary
if format == Format::Dxt3 {
@@ -204,7 +206,7 @@ fn compress_block_masked(
);
let colour_offset = if format == Format::Dxt1 { 0 } else { 8 };
let mut colour_block = &mut output[colour_offset..colour_offset+8];
let colour_block = &mut output[colour_offset..colour_offset+8];
// compress with appropriate compression algorithm
if colours.count() == 1 {
@@ -253,7 +255,7 @@ pub fn compress(
width: usize,
height: usize,
format: Format,
params: CompressorParams,
params: Params,
output: &mut [u8]
) {
assert!(output.len() >= compute_compressed_size(width, height, format));
@@ -282,7 +284,7 @@ pub fn compress(
// copy pixel value
let src_index = 4 * (width*sy + sx);
&mut source_rgba[index]
.clone_from_slice(&rgba[src_index..src_index+4]);
.copy_from_slice(&rgba[src_index..src_index+4]);
// enable pixel
mask |= 1 << index;
+1 -1
View File
@@ -21,7 +21,7 @@
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
use std::f32;
use core::f32;
mod vec3;
pub use self::vec3::*;
+3 -3
View File
@@ -21,8 +21,8 @@
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
use std::iter::Sum;
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
use core::iter::Sum;
/// A 3-dimensional vector type
#[derive(Copy, Clone, PartialEq)]
@@ -433,4 +433,4 @@ impl Sum<Vec3> for Vec3 {
fn sum<I: Iterator<Item=Vec3>>(iter: I) -> Self {
iter.fold(Vec3::new(0.0, 0.0, 0.0), |a, b| a + b)
}
}
}
+1 -2
View File
@@ -21,7 +21,7 @@
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
use core::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
use super::Vec3;
@@ -433,4 +433,3 @@ impl MulAssign<f32> for Vec4 {
self.w *= other;
}
}
+2 -2
View File
@@ -32,7 +32,7 @@ use std::fs::File;
use std::path::PathBuf;
use ddsfile::{AlphaMode, Dds, D3D10ResourceDimension, DxgiFormat};
use squish::{Format, CompressorParams};
use squish::{Format, Params};
use structopt::StructOpt;
mod image;
@@ -88,7 +88,7 @@ fn main() {
image.width,
image.height,
format,
CompressorParams::default(),
Params::default(),
&mut buf
);