use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};
use std::io::{Cursor, Read};
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::time::Instant;
use crabshell_ui::{
CaptureFrame, Color, Fit, IconPixels, IconSource, ImageFilters, ImageTransition, ImageView,
};
use resvg::usvg;
use tiny_skia::{FilterQuality, Pixmap, PixmapPaint, PixmapRef, Transform};
use crate::animation::Frames;
use crate::theme::Themes;
const CAPACITY: usize = 128;
const RECENT: u64 = 16;
const MAX_CACHED_BYTES: usize = 32 * 1024 * 1024;
const MAX_SVG_BYTES: u64 = 1024 * 1024;
const MAX_PNG_BYTES: u64 = 4 * 1024 * 1024;
const MAX_PNG_SIDE: u32 = 1024;
const MAX_PIXELS_SIDE: u32 = 512;
pub const DEFAULT_THEME: &str = "hicolor";
pub(crate) struct Icons {
theme: String,
themes: Themes,
cache: HashMap<Key, Entry>,
frame: u64,
failed: HashSet<Key>,
failed_masks: HashSet<PathBuf>,
rasterized: usize,
captures: HashMap<u64, Capture>,
playbacks: HashMap<u64, Playback>,
transitions: HashMap<u64, Shown>,
modified: HashMap<PathBuf, Option<std::time::SystemTime>>,
shows: HashMap<u64, (Key, u64)>,
window: u64,
}
#[derive(Default)]
struct Shown {
good: Option<PathBuf>,
leaving: Option<(PathBuf, Option<PathBuf>)>,
left: Option<Key>,
used: u64,
window: u64,
}
pub(crate) enum Drawn<'a> {
One(&'a Pixmap),
Two {
from: Option<&'a Pixmap>,
to: &'a Pixmap,
t: f32,
},
}
struct Playback {
key: Key,
frame: usize,
played: u32,
since: Option<Instant>,
due: Option<Instant>,
drawn: bool,
playing: bool,
finished: bool,
used: u64,
}
impl Playback {
fn new(key: Key) -> Playback {
Playback {
key,
frame: 0,
played: 0,
since: None,
due: None,
drawn: false,
playing: true,
finished: false,
used: 0,
}
}
}
struct Capture {
frame: Shared,
width: u32,
height: u32,
fit: Fit,
pixmap: Option<Pixmap>,
used: u64,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
enum Key {
Name {
theme: String,
name: String,
size: u32,
scale: u32,
side: u32,
tint: Option<Color>,
},
File {
path: PathBuf,
side: u32,
tint: Option<Color>,
},
Image {
path: PathBuf,
modified: Option<std::time::SystemTime>,
width: u32,
height: u32,
fit: Fit,
filters: FilterKey,
},
Pixels {
data: Shared,
width: u32,
height: u32,
side: u32,
tint: Option<Color>,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
struct FilterKey {
tint: Option<Color>,
numbers: [u32; 4],
}
impl FilterKey {
fn new(filters: &ImageFilters, scale: f32) -> FilterKey {
FilterKey {
tint: filters.tint,
numbers: [
filters.brightness,
filters.contrast,
filters.saturation,
filters.blur * scale,
]
.map(f32::to_bits),
}
}
}
#[derive(Clone)]
struct Shared(Rc<[u8]>);
impl PartialEq for Shared {
fn eq(&self, other: &Shared) -> bool {
Rc::ptr_eq(&self.0, &other.0)
}
}
impl Eq for Shared {}
impl Hash for Shared {
fn hash<H: Hasher>(&self, state: &mut H) {
(Rc::as_ptr(&self.0) as *const u8 as usize, self.0.len()).hash(state);
}
}
impl std::fmt::Debug for Shared {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} bytes", self.0.len())
}
}
struct Entry {
frames: Option<Frames>,
used: u64,
}
impl Key {
fn same_file(&self, other: &Key) -> bool {
match (self, other) {
(
Key::Image { path, modified, .. },
Key::Image {
path: other_path,
modified: other_modified,
..
},
) => (path, modified) == (other_path, other_modified),
_ => false,
}
}
}
impl Icons {
pub(crate) fn new() -> Icons {
Icons::with_themes(Themes::from_env())
}
pub(crate) fn with_themes(themes: Themes) -> Icons {
Icons {
theme: DEFAULT_THEME.to_owned(),
themes,
cache: HashMap::new(),
frame: 0,
failed: HashSet::new(),
failed_masks: HashSet::new(),
rasterized: 0,
captures: HashMap::new(),
playbacks: HashMap::new(),
transitions: HashMap::new(),
modified: HashMap::new(),
shows: HashMap::new(),
window: 0,
}
}
pub(crate) fn start_frame(&mut self, window: u64) {
self.window = window;
}
pub(crate) fn set_theme(&mut self, theme: String) {
self.theme = theme;
}
#[cfg(test)]
pub(crate) fn cached(&self) -> usize {
self.cache.len()
}
#[cfg(test)]
pub(crate) fn frames_cached(&self) -> usize {
self
.cache
.values()
.filter_map(|entry| entry.frames.as_ref())
.map(|frames| frames.frames.len())
.sum()
}
#[cfg(test)]
pub(crate) fn rasterized(&self) -> usize {
self.rasterized
}
#[cfg(test)]
pub(crate) fn captures(&self) -> usize {
self.captures.len()
}
pub(crate) fn get(
&mut self,
source: &IconSource,
(side, size, scale): (u32, u32, u32),
tint: Option<Color>,
) -> Option<&Pixmap> {
let key = match source {
IconSource::Name(name) => Key::Name {
theme: self.theme.clone(),
name: name.clone(),
size,
scale,
side,
tint,
},
IconSource::File(path) => Key::File {
path: path.clone(),
side,
tint,
},
IconSource::Pixels(pixels) => Key::Pixels {
data: Shared(Rc::clone(&pixels.data)),
width: pixels.width,
height: pixels.height,
side,
tint,
},
};
if !self.cache.contains_key(&key) {
let rasterized = match source {
IconSource::Name(name) => match self.themes.find(&self.theme, name, size, scale) {
Some(path) => rasterize_file(&path, side),
None => Err(format!("no icon named {name:?} in theme {:?}", self.theme)),
},
IconSource::File(path) => rasterize_file(path, side),
IconSource::Pixels(pixels) => from_argb(pixels).and_then(|image| fit(&image, side)),
};
let tinted = rasterized.map(|mut pixmap| {
if let Some(tint) = tint {
crate::filters::tint(pixmap.data_mut(), tint);
}
pixmap
});
self.store(key.clone(), tinted.map(Frames::still), || describe(source));
}
self.used(&key)
}
pub(crate) fn image(
&mut self,
image: &ImageView<'_>,
size: (u32, u32),
scale: f32,
) -> Option<Drawn<'_>> {
let key = self.load(image.path, image, size, scale, true);
let entry = self.cache.get_mut(&key)?;
entry.used = self.frame;
let count = entry
.frames
.as_ref()
.map_or(0, |frames| frames.frames.len());
let index = if count > 1 {
self.play(image, &key, count)
} else {
self.playbacks.remove(&image.id);
0
};
if image.transition == ImageTransition::None {
if let Some(left) = self
.transitions
.remove(&image.id)
.and_then(|shown| shown.left)
{
self.forget(left, image.id, Some(&key));
}
self.show(image.id, &key);
return frame(&self.cache, &key, index).map(Drawn::One);
}
let from = self.leaving(image, &key, size, scale);
let shown = self.transitions.entry(image.id).or_default();
shown.used = self.frame;
shown.window = self.window;
if count == 0 {
let good = shown.good.clone()?;
let key = self.load(&good, image, size, scale, true);
self.show(image.id, &key);
return frame(&self.cache, &key, 0).map(Drawn::One);
}
shown.good = Some(image.path.to_owned());
self.show(image.id, &key);
let to = frame(&self.cache, &key, index)?;
Some(match (from, image.leaving) {
(Some(from), Some((_, t))) => Drawn::Two {
from: from.and_then(|from| frame(&self.cache, &from, 0)),
to,
t,
},
_ => Drawn::One(to),
})
}
pub(crate) fn mask(&mut self, path: &Path, size: (u32, u32), scale: f32) -> Option<&Pixmap> {
let image = ImageView {
id: u64::MAX,
path,
rect: crabshell_ui::Rect::default(),
fit: Fit::Fill,
filters: ImageFilters::default(),
playing: false,
transition: ImageTransition::None,
leaving: None,
};
let log = !self.failed_masks.contains(path);
let key = self.load(path, &image, size, scale, log);
if self
.cache
.get(&key)
.is_none_or(|entry| entry.frames.is_none())
{
if self.failed_masks.len() > CAPACITY {
self.failed_masks.clear();
}
self.failed_masks.insert(path.to_owned());
return None;
}
frame(&self.cache, &key, 0)
}
#[cfg(test)]
pub(crate) fn picture(
&mut self,
image: &ImageView<'_>,
size: (u32, u32),
scale: f32,
) -> Option<&Pixmap> {
match self.image(image, size, scale)? {
Drawn::One(pixmap) | Drawn::Two { to: pixmap, .. } => Some(pixmap),
}
}
#[cfg(test)]
pub(crate) fn cached_bytes(&self) -> usize {
self
.cache
.values()
.filter_map(|entry| entry.frames.as_ref())
.flat_map(|frames| &frames.frames)
.map(|frame| frame.pixmap.data().len())
.sum()
}
fn leaving(
&mut self,
image: &ImageView<'_>,
key: &Key,
size: (u32, u32),
scale: f32,
) -> Option<Option<Key>> {
let shown = self.transitions.entry(image.id).or_default();
let Some((named, _)) = image.leaving else {
shown.leaving = None;
if let Some(left) = shown.left.take() {
self.forget(left, image.id, Some(key));
}
return None;
};
let drawn = match &shown.leaving {
Some((before, drawn)) if before == named => drawn.clone(),
_ => {
let good = shown.good.clone();
let loads = {
let left = self.load(named, image, size, scale, true);
self
.cache
.get(&left)
.is_some_and(|entry| entry.frames.is_some())
};
let drawn = if loads { Some(named.to_owned()) } else { good };
let shown = self.transitions.entry(image.id).or_default();
shown.leaving = Some((named.to_owned(), drawn.clone()));
drawn
}
};
let nothing = drawn.is_none();
let left = match drawn {
Some(drawn) => Some(self.load(&drawn, image, size, scale, true)).filter(|left| left != key),
None => None,
};
let shown = self.transitions.entry(image.id).or_default();
if shown.left != left {
let replaced = std::mem::replace(&mut shown.left, left.clone());
if let Some(replaced) = replaced {
self.forget(replaced, image.id, Some(key));
}
}
match left {
Some(left) => Some(Some(left)),
None => nothing.then_some(None),
}
}
fn forget(&mut self, key: Key, id: u64, current: Option<&Key>) {
let recent = self.frame.saturating_sub(RECENT - 1);
let kept = current == Some(&key)
|| self
.shows
.iter()
.any(|(&other, (shown, when))| other != id && *shown == key && *when >= recent)
|| self
.transitions
.iter()
.any(|(&other, shown)| other != id && shown.left.as_ref() == Some(&key));
if !kept {
self.cache.remove(&key);
}
}
fn show(&mut self, id: u64, key: &Key) {
match self.shows.get_mut(&id) {
Some((shown, when)) if shown == key => *when = self.frame,
_ => {
self.shows.insert(id, (key.clone(), self.frame));
}
}
}
fn key(
&mut self,
path: &Path,
image: &ImageView<'_>,
(width, height): (u32, u32),
scale: f32,
) -> Key {
let modified = match self.modified.get(path) {
Some(modified) => *modified,
None => {
let modified = std::fs::metadata(path)
.and_then(|metadata| metadata.modified())
.ok();
self.modified.insert(path.to_owned(), modified);
modified
}
};
Key::Image {
path: path.to_owned(),
modified,
width,
height,
fit: image.fit,
filters: FilterKey::new(&image.filters, scale),
}
}
fn load(
&mut self,
path: &Path,
image: &ImageView<'_>,
size: (u32, u32),
scale: f32,
log: bool,
) -> Key {
let key = self.key(path, image, size, scale);
if !self.cache.contains_key(&key) {
if !log {
self.failed.insert(key.clone());
}
let room = MAX_CACHED_BYTES.saturating_sub(self.bytes_in_use());
let rasterized = crate::animation::frames(path, size, image.fit, &image.filters, scale, room);
self.store(key.clone(), rasterized, || {
format!("image {}", path.display())
});
}
if let Some(entry) = self.cache.get_mut(&key) {
entry.used = self.frame;
}
key
}
fn play(&mut self, image: &ImageView<'_>, key: &Key, count: usize) -> usize {
let playback = self
.playbacks
.entry(image.id)
.or_insert_with(|| Playback::new(key.clone()));
if !playback.key.same_file(key) {
*playback = Playback::new(key.clone());
}
playback.key = key.clone();
playback.frame = playback.frame.min(count - 1);
playback.drawn = true;
playback.playing = image.playing;
playback.used = self.frame;
if !image.playing {
playback.due = None;
}
playback.frame
}
pub(crate) fn next_frame(&mut self, now: Instant) -> Option<Instant> {
let Icons {
playbacks, cache, ..
} = self;
for playback in playbacks.values_mut() {
if !std::mem::take(&mut playback.drawn)
|| !playback.playing
|| playback.finished
|| playback.due.is_some()
{
continue;
}
let delay = cache
.get(&playback.key)
.and_then(|entry| entry.frames.as_ref())
.and_then(|frames| frames.frames.get(playback.frame))
.map(|frame| frame.delay);
if let Some(delay) = delay {
playback.due = Some(match playback.since {
Some(since) if since + delay > now => since + delay,
_ => now + delay,
});
}
}
playbacks.values().filter_map(|playback| playback.due).min()
}
pub(crate) fn advance(&mut self, now: Instant) -> Vec<u64> {
let mut moved = Vec::new();
for (&id, playback) in &mut self.playbacks {
let Some(due) = playback.due.filter(|due| *due <= now) else {
continue;
};
playback.due = None;
playback.since = Some(due);
let Some(frames) = self
.cache
.get(&playback.key)
.and_then(|entry| entry.frames.as_ref())
else {
continue;
};
if playback.frame + 1 < frames.frames.len() {
playback.frame += 1;
} else if frames
.repeats
.is_none_or(|repeats| playback.played < repeats)
{
playback.played += 1;
playback.frame = 0;
} else {
playback.finished = true;
continue;
}
moved.push(id);
}
moved
}
pub(crate) fn capture(
&mut self,
view: u64,
frame: &CaptureFrame,
width: u32,
height: u32,
fit: Fit,
) -> Option<&Pixmap> {
let shared = Shared(Rc::clone(&frame.rgba));
let kept = self.captures.get(&view).is_some_and(|kept| {
kept.frame == shared && (kept.width, kept.height, kept.fit) == (width, height, fit)
});
if !kept {
let scaled = PixmapRef::from_bytes(&frame.rgba, frame.width, frame.height)
.ok_or_else(|| {
format!(
"{} bytes for {}x{}",
frame.rgba.len(),
frame.width,
frame.height
)
})
.and_then(|source| crate::images::scale(source, width, height, fit));
if let Err(error) = &scaled {
tracing::warn!(%error, "couldn't draw a captured frame");
}
self.captures.insert(
view,
Capture {
frame: shared,
width,
height,
fit,
pixmap: scaled.ok(),
used: self.frame,
},
);
}
let kept = self.captures.get_mut(&view)?;
kept.used = self.frame;
kept.pixmap.as_ref()
}
fn store(&mut self, key: Key, rasterized: Result<Frames, String>, what: impl FnOnce() -> String) {
self.rasterized += 1;
let frames = match rasterized {
Ok(frames) => Some(frames),
Err(error) => {
if self.failed.len() > CAPACITY {
self.failed.clear();
}
if self.failed.insert(key.clone()) {
tracing::warn!(source = %what(), %error, "couldn't draw an icon or image");
}
None
}
};
let used = self.frame;
self.cache.insert(key, Entry { frames, used });
}
fn used(&mut self, key: &Key) -> Option<&Pixmap> {
let entry = self.cache.get_mut(key)?;
entry.used = self.frame;
Some(&entry.frames.as_ref()?.frames.first()?.pixmap)
}
fn bytes_in_use(&self) -> usize {
let recent = self.frame.saturating_sub(RECENT - 1);
let waiting: HashSet<&Key> = self
.playbacks
.values()
.filter(|playback| playback.due.is_some())
.map(|playback| &playback.key)
.collect();
self
.cache
.iter()
.filter(|(key, entry)| entry.used >= recent || waiting.contains(key))
.filter_map(|(_, entry)| entry.frames.as_ref())
.flat_map(|frames| &frames.frames)
.map(|frame| frame.pixmap.data().len())
.sum()
}
pub(crate) fn forget_captures(&mut self, gone: &[u64]) {
self.captures.retain(|view, _| !gone.contains(view));
}
fn let_go_of_left_pictures(&mut self) {
let mut gone = Vec::new();
for id in crabshell_ui::dropped_images() {
self.shows.remove(&id);
self.playbacks.remove(&id);
if let Some(left) = self.transitions.remove(&id).and_then(|shown| shown.left) {
gone.push((id, left));
}
}
let (window, frame) = (self.window, self.frame);
for (&id, shown) in &mut self.transitions {
if shown.window == window && shown.used < frame {
shown.leaving = None;
if let Some(left) = shown.left.take() {
gone.push((id, left));
}
}
}
for (id, left) in gone {
let current = self.shows.get(&id).map(|(key, _)| key.clone());
self.forget(left, id, current.as_ref());
}
}
pub(crate) fn end_frame(&mut self) {
self.modified.clear();
self.let_go_of_left_pictures();
let recent = self.frame.saturating_sub(RECENT - 1);
self.captures.retain(|_, kept| kept.used >= recent);
for playback in self.playbacks.values() {
if playback.due.is_some()
&& let Some(entry) = self.cache.get_mut(&playback.key)
{
entry.used = self.frame;
}
}
if self.playbacks.len() > CAPACITY {
self
.playbacks
.retain(|_, playback| playback.due.is_some() || playback.used >= recent);
}
if self.transitions.len() > CAPACITY {
self.transitions.retain(|_, shown| shown.used >= recent);
}
if self.shows.len() > CAPACITY {
self.shows.retain(|_, (_, when)| *when >= recent);
}
let bytes: usize = self
.cache
.values()
.filter_map(|entry| entry.frames.as_ref())
.flat_map(|frames| &frames.frames)
.map(|frame| frame.pixmap.data().len())
.sum();
if self.cache.len() > CAPACITY || bytes > MAX_CACHED_BYTES {
let oldest = self.frame.saturating_sub(RECENT - 1);
self.cache.retain(|_, entry| entry.used >= oldest);
}
self.frame += 1;
}
}
fn frame<'a>(cache: &'a HashMap<Key, Entry>, key: &Key, index: usize) -> Option<&'a Pixmap> {
Some(&cache.get(key)?.frames.as_ref()?.frames.get(index)?.pixmap)
}
fn describe(source: &IconSource) -> String {
match source {
IconSource::Name(name) => format!("name {name:?}"),
IconSource::File(path) => format!("file {}", path.display()),
IconSource::Pixels(pixels) => format!("{}x{} pixels", pixels.width, pixels.height),
}
}
fn rasterize_file(path: &Path, side: u32) -> Result<Pixmap, String> {
let extension = path
.extension()
.and_then(|extension| extension.to_str())
.map(str::to_ascii_lowercase);
match extension.as_deref() {
Some("svg") => rasterize_svg(&read_capped(path, MAX_SVG_BYTES)?, side),
Some("png") => {
let max_pixels = u64::from(MAX_PNG_SIDE) * u64::from(MAX_PNG_SIDE);
fit(
&decode_png(&read_capped(path, MAX_PNG_BYTES)?, MAX_PNG_SIDE, max_pixels)?,
side,
)
}
_ => Err(String::from("only .svg and .png files can be icons")),
}
}
pub(crate) fn read_capped(path: &Path, cap: u64) -> Result<Vec<u8>, String> {
let metadata = std::fs::metadata(path).map_err(|error| error.to_string())?;
if !metadata.is_file() {
return Err(String::from("not a regular file"));
}
if metadata.len() > cap {
return Err(format!(
"the file is {} bytes, more than {cap}",
metadata.len()
));
}
let file = std::fs::File::open(path).map_err(|error| error.to_string())?;
read_limited(file, cap)
}
pub(crate) fn read_limited(reader: impl Read, cap: u64) -> Result<Vec<u8>, String> {
let mut data = Vec::new();
reader
.take(cap + 1)
.read_to_end(&mut data)
.map_err(|error| error.to_string())?;
if data.len() as u64 > cap {
return Err(format!("more than {cap} bytes"));
}
Ok(data)
}
fn rasterize_svg(data: &[u8], side: u32) -> Result<Pixmap, String> {
let tree = svg_tree(data)?;
let size = tree.size();
let mut pixmap = empty(side)?;
let scale = side as f32 / size.width().max(size.height());
let transform = Transform::from_scale(scale, scale).post_translate(
(side as f32 - size.width() * scale) / 2.0,
(side as f32 - size.height() * scale) / 2.0,
);
resvg::render(&tree, transform, &mut pixmap.as_mut());
Ok(pixmap)
}
pub(crate) fn svg_tree(data: &[u8]) -> Result<usvg::Tree, String> {
let options = usvg::Options {
image_href_resolver: usvg::ImageHrefResolver {
resolve_data: usvg::ImageHrefResolver::default_data_resolver(),
resolve_string: Box::new(|_, _| None),
},
..usvg::Options::default()
};
let tree = usvg::Tree::from_data(data, &options).map_err(|error| error.to_string())?;
if !tree.filters().is_empty() {
return Err(String::from("the SVG uses a filter, which icons may not"));
}
Ok(tree)
}
pub(crate) fn decode_png(data: &[u8], max_side: u32, max_pixels: u64) -> Result<Pixmap, String> {
let limits = png::Limits {
bytes: max_pixels.saturating_mul(8).min(usize::MAX as u64) as usize,
};
let mut decoder = png::Decoder::new_with_limits(Cursor::new(data), limits);
decoder.set_transformations(png::Transformations::normalize_to_color8());
let mut reader = decoder.read_info().map_err(|error| error.to_string())?;
let (width, height) = (reader.info().width, reader.info().height);
if width > max_side || height > max_side || u64::from(width) * u64::from(height) > max_pixels {
return Err(format!(
"a {width}x{height} PNG is larger than {max_side} pixels a side or {max_pixels} pixels"
));
}
let size = reader
.output_buffer_size()
.ok_or("the PNG is too large to decode")?;
let mut buffer = vec![0; size];
let info = reader
.next_frame(&mut buffer)
.map_err(|error| error.to_string())?;
let rows = buffer
.chunks_exact(info.line_size)
.take(info.height as usize);
let mut rgba = Vec::with_capacity(info.width as usize * info.height as usize * 4);
for row in rows {
match info.color_type {
png::ColorType::Rgba => rgba.extend_from_slice(&row[..info.width as usize * 4]),
png::ColorType::Rgb => {
for pixel in row.as_chunks::<3>().0.iter().take(info.width as usize) {
rgba.extend_from_slice(&[pixel[0], pixel[1], pixel[2], 255]);
}
}
png::ColorType::GrayscaleAlpha => {
for pixel in row.as_chunks::<2>().0.iter().take(info.width as usize) {
rgba.extend_from_slice(&[pixel[0], pixel[0], pixel[0], pixel[1]]);
}
}
png::ColorType::Grayscale => {
for &gray in row.iter().take(info.width as usize) {
rgba.extend_from_slice(&[gray, gray, gray, 255]);
}
}
png::ColorType::Indexed => return Err(String::from("unexpanded palette PNG")),
}
}
premultiplied(rgba, info.width, info.height)
}
fn from_argb(pixels: &IconPixels) -> Result<Pixmap, String> {
if pixels.width > MAX_PIXELS_SIDE || pixels.height > MAX_PIXELS_SIDE {
return Err(format!(
"{}x{} pixels are larger than {MAX_PIXELS_SIDE} a side",
pixels.width, pixels.height
));
}
let expected = u64::from(pixels.width) * u64::from(pixels.height) * 4;
if pixels.data.len() as u64 != expected {
return Err(format!(
"{}x{} pixels need {expected} bytes, got {}",
pixels.width,
pixels.height,
pixels.data.len()
));
}
let rgba = pixels
.data
.as_chunks::<4>()
.0
.iter()
.flat_map(|argb| [argb[1], argb[2], argb[3], argb[0]])
.collect();
premultiplied(rgba, pixels.width, pixels.height)
}
pub(crate) fn premultiplied(mut rgba: Vec<u8>, width: u32, height: u32) -> Result<Pixmap, String> {
for pixel in rgba.as_chunks_mut::<4>().0 {
let alpha = u16::from(pixel[3]);
for channel in &mut pixel[..3] {
*channel = ((u16::from(*channel) * alpha + 127) / 255) as u8;
}
}
let size = tiny_skia::IntSize::from_wh(width, height).ok_or("an icon with no pixels")?;
Pixmap::from_vec(rgba, size).ok_or_else(|| String::from("icon pixels of the wrong length"))
}
fn fit(image: &Pixmap, side: u32) -> Result<Pixmap, String> {
let mut halved;
let mut image = image;
while image.width().max(image.height()) > side * 2 {
halved = halve(image.as_ref())?;
image = &halved;
}
let mut pixmap = empty(side)?;
let scale = side as f32 / image.width().max(image.height()) as f32;
let transform = Transform::from_scale(scale, scale).post_translate(
(side as f32 - image.width() as f32 * scale) / 2.0,
(side as f32 - image.height() as f32 * scale) / 2.0,
);
let paint = PixmapPaint {
quality: FilterQuality::Bicubic,
..PixmapPaint::default()
};
pixmap.draw_pixmap(0, 0, image.as_ref(), &paint, transform, None);
Ok(pixmap)
}
pub(crate) fn halve(image: PixmapRef<'_>) -> Result<Pixmap, String> {
let (width, height) = (image.width(), image.height());
let (half_width, half_height) = (width.div_ceil(2), height.div_ceil(2));
let mut half = Pixmap::new(half_width, half_height).ok_or("an icon with no pixels")?;
let source = image.data();
let target = half.data_mut();
for y in 0..half_height {
for x in 0..half_width {
let mut sum = [0_u32; 4];
for (dx, dy) in [(0, 0), (1, 0), (0, 1), (1, 1)] {
let sx = (x * 2 + dx).min(width - 1);
let sy = (y * 2 + dy).min(height - 1);
let index = (sy * width + sx) as usize * 4;
for (total, &value) in sum.iter_mut().zip(&source[index..index + 4]) {
*total += u32::from(value);
}
}
let index = (y * half_width + x) as usize * 4;
for (value, total) in target[index..index + 4].iter_mut().zip(sum) {
*value = ((total + 2) / 4) as u8;
}
}
}
Ok(half)
}
fn empty(side: u32) -> Result<Pixmap, String> {
Pixmap::new(side, side).ok_or_else(|| String::from("an icon with no pixels"))
}