Skip to content

Commit

Permalink
Entropy: cache the discretized gradient.
Browse files Browse the repository at this point in the history
  • Loading branch information
siedentop authored and sharkdp committed Nov 14, 2021
1 parent 5b7c275 commit 83779cc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -28,4 +28,4 @@ winit_input_helper = "0.10"
anyhow = "1.0"
humansize = "1.1"
memmap2 = "0.5.0"
lazy_static = "1.4.0"

21 changes: 13 additions & 8 deletions src/style.rs
Expand Up @@ -3,8 +3,6 @@ use std::convert::TryInto;
use crate::datatype::{Datatype, Endianness};
use crate::view::View;

use lazy_static::lazy_static;

pub type Color = [u8; 4];

fn rgba_from_color(color: colorgrad::Color) -> Color {
Expand Down Expand Up @@ -201,22 +199,28 @@ pub struct Entropy {
window_size: usize,
window_size_f64: f64,
counts: [i32; 256],
/// Cache the gradient color
byte_color: [Color; 256],
}

impl Entropy {
pub fn with_window_size(window_size: usize) -> Entropy {
let gradient = colorgrad::magma();
let mut byte_color = [[0, 0, 0, 0]; 256];
for (byte, color) in byte_color.iter_mut().enumerate() {
let gradient_color = gradient.at((byte as f64) / 255.0f64);
*color = rgba_from_color(gradient_color);
}

Entropy {
window_size,
window_size_f64: window_size as f64,
counts: [0; 256],
byte_color,
}
}
}

lazy_static! {
static ref MAGMA: colorgrad::Gradient = colorgrad::magma();
}

impl Style for Entropy {
fn init(&mut self, _: &View) {}

Expand All @@ -237,8 +241,9 @@ impl Style for Entropy {
}
entropy *= 1.0f64 / 8.0f64;

let color = MAGMA.at(entropy);
rgba_from_color(color)
let discretized_entropy: usize = ((entropy * self.byte_color.len() as f64) as usize)
.clamp(0, self.byte_color.len() - 1);
self.byte_color[discretized_entropy]
} else {
[0, 0, 0, 0]
}
Expand Down

0 comments on commit 83779cc

Please sign in to comment.