use crate::source::TileKey;
use slint::{Rgba8Pixel, SharedPixelBuffer};
pub(crate) fn format_url(template: &str, key: TileKey) -> String {
template
.replace("{z}", &key.z.to_string())
.replace("{x}", &key.x.to_string())
.replace("{y}", &key.y.to_string())
}
pub(crate) fn decode_png_to_buffer(bytes: &[u8]) -> Option<SharedPixelBuffer<Rgba8Pixel>> {
let img = image::load_from_memory(bytes).ok()?;
let rgba = img.to_rgba8();
let (w, h) = rgba.dimensions();
let mut buf = SharedPixelBuffer::<Rgba8Pixel>::new(w, h);
let dst = buf.make_mut_slice();
for (out, chunk) in dst.iter_mut().zip(rgba.chunks_exact(4)) {
*out = Rgba8Pixel {
r: chunk[0],
g: chunk[1],
b: chunk[2],
a: chunk[3],
};
}
Some(buf)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn format_url_substitutes() {
assert_eq!(
format_url("https://x/{z}/{x}/{y}.png", TileKey { x: 5, y: 10, z: 2 }),
"https://x/2/5/10.png"
);
}
}