- add colormap from string to rust code
This commit is contained in:
+26
-1
@@ -3,6 +3,8 @@ mod py;
|
||||
|
||||
use anyhow::{Result, anyhow};
|
||||
use chrono::Utc;
|
||||
use colorcet::ColorMap;
|
||||
use colorgrad::{Gradient, LinearGradient};
|
||||
use css_color::Srgb;
|
||||
use flate2::write::ZlibEncoder;
|
||||
use ndarray::{ArcArray2, AsArray, Ix2, s};
|
||||
@@ -709,11 +711,12 @@ impl IJTiffFile {
|
||||
}
|
||||
|
||||
/// set colors from css color names and #C01085
|
||||
pub fn set_colors(&mut self, colors: &[String]) -> Result<()> {
|
||||
pub fn set_colors<S: AsRef<str>>(&mut self, colors: &[S]) -> Result<()> {
|
||||
self.colors = Colors::Colors(
|
||||
colors
|
||||
.iter()
|
||||
.map(|c| {
|
||||
let c = c.as_ref();
|
||||
let lc = c.to_lowercase();
|
||||
let c = match lc.as_str() {
|
||||
"r" => "#ff0000",
|
||||
@@ -740,6 +743,28 @@ impl IJTiffFile {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_colormap<S: AsRef<str>>(&mut self, name: S) -> Result<()> {
|
||||
let name = name.as_ref();
|
||||
let colormap: LinearGradient = name.parse::<ColorMap>()?.try_into()?;
|
||||
let mut colormap = colormap
|
||||
.colors(256)
|
||||
.into_iter()
|
||||
.map(|c| {
|
||||
vec![
|
||||
(c.r * 255.0).round() as u8,
|
||||
(c.g * 255.0).round() as u8,
|
||||
(c.b * 255.0).round() as u8,
|
||||
]
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
if name.starts_with("glasbey") || name.ends_with("glasbey") {
|
||||
colormap[0] = vec![255, 255, 255];
|
||||
colormap[255] = vec![0, 0, 0];
|
||||
}
|
||||
self.colors = Colors::Colormap(colormap);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// to be saved in description tag (270)
|
||||
pub fn description(&self, c_size: usize, z_size: usize, t_size: usize) -> String {
|
||||
let mut desc: String = String::from("ImageJ=1.11a");
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use crate::{Colors, Compression, IJTiffFile, Tag};
|
||||
use ndarray::s;
|
||||
use num::{Complex, FromPrimitive, Rational32};
|
||||
use numpy::{PyArrayMethods, PyReadonlyArray2};
|
||||
use numpy::PyReadonlyArray2;
|
||||
use pyo3::exceptions::PyValueError;
|
||||
use pyo3::prelude::*;
|
||||
|
||||
@@ -221,14 +220,9 @@ impl PyIJTiffFile {
|
||||
}
|
||||
|
||||
#[setter]
|
||||
fn set_colormap(&mut self, colormap: PyReadonlyArray2<u8>) -> PyResult<()> {
|
||||
fn set_colormap(&mut self, colormap: &str) -> PyResult<()> {
|
||||
if let Some(ijtifffile) = &mut self.ijtifffile {
|
||||
let a = colormap.to_owned_array();
|
||||
ijtifffile.colors = Colors::Colormap(
|
||||
(0..a.shape()[0])
|
||||
.map(|i| Vec::from(a.slice(s![i, ..]).as_slice().unwrap()))
|
||||
.collect(),
|
||||
);
|
||||
ijtifffile.set_colormap(colormap)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user