git clone https://garage44.eu/jeroen/pinnacle.gitcartopolis-agent · 2026-11-30T15:34:59Z
feat(media): pictures from files, decoded by Wil and fitted to a box The first step to opening photos in Files: `pinnacle_media::picture` tells a picture by its first bytes (PNG, JPEG, GIF, WebP), decodes it through Wil's wil_core within 50 megapixels, and fits it into the box it is shown in, centred, never grown, its transparency over the background, as the window's 0x00RRGGBB pixels. It lives in media because its tests run in the gate there; ui and programs have none.
Cargo.lock | 8 +++++
media/Cargo.toml | 6 +++-
media/src/lib.rs | 6 +++-
media/src/picture.rs | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++
media/src/tests.rs | 51 +++++++++++++++++++++++++++
5 files changed, 166 insertions(+), 2 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
index ff5f857..13a02d2 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -148,6 +148,9 @@ version = "0.1.0"
[[package]]
name = "pinnacle-media"
version = "0.1.0"
+dependencies = [
+ "wil_core",
+]
[[package]]
name = "pinnacle-modem"
@@ -312,6 +315,11 @@ dependencies = [
"pinnacle-records",
]
+[[package]]
+name = "wil_core"
+version = "0.1.0"
+source = "git+https://code.garage44.eu/jeroen/wil.git?rev=e3cc7d2#e3cc7d22cc3c9398e043b16476c6923a6525cca3"
+
[[package]]
name = "xtask"
version = "0.1.0"
diff --git a/media/Cargo.toml b/media/Cargo.toml
index 15446bb..8b66bc7 100644
--- a/media/Cargo.toml
+++ b/media/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "pinnacle-media"
-description = "Pinnacle's own codecs for calls: a block video codec and IMA ADPCM audio"
+description = "Pinnacle's own codecs for calls (a block video codec, IMA ADPCM audio), and pictures from files shown through Wil"
version.workspace = true
edition.workspace = true
license.workspace = true
@@ -8,3 +8,7 @@ publish.workspace = true
[lib]
doctest = false
+
+[dependencies]
+# Wil, the codecs (ours too, no outside code): the pictures `picture` shows.
+wil_core = { git = "https://code.garage44.eu/jeroen/wil.git", rev = "e3cc7d2" }
diff --git a/media/src/lib.rs b/media/src/lib.rs
index f598ff3..97dc70d 100644
--- a/media/src/lib.rs
+++ b/media/src/lib.rs
@@ -1,4 +1,4 @@
-//! Pinnacle's own codecs for calls.
+//! Pinnacle's own codecs for calls, and pictures shown from files.
//!
//! **Video** (`video`): frames in YCbCr 4:2:0, cut into 16×16 macroblocks.
//! A macroblock the same as last frame's (within a threshold) costs one
@@ -10,12 +10,16 @@
//!
//! **Audio** (`adpcm`): IMA ADPCM, 4 bits a sample, 20 ms frames that each
//! carry their own decoder state, so a lost frame costs only itself.
+//!
+//! **Pictures** (`picture`): a file told to be a picture by its first
+//! bytes, decoded by Wil, and fitted into the box it is shown in.
#![no_std]
extern crate alloc;
pub mod adpcm;
+pub mod picture;
pub mod video;
#[cfg(test)]
diff --git a/media/src/picture.rs b/media/src/picture.rs
new file mode 100644
index 0000000..bee9060
--- /dev/null
+++ b/media/src/picture.rs
@@ -0,0 +1,97 @@
+//! Pictures shown on a screen: whether a file is one (by its first bytes,
+//! never its name), decoded by Wil (`jeroen/wil`: PNG, JPEG, GIF, WebP),
+//! and fitted into the box it is shown in as the window's pixels.
+
+use alloc::vec::Vec;
+
+pub use wil_core::{Error, Format};
+use wil_core::{Image, Limits};
+
+/// The largest picture file opened, in bytes: a phone camera's photo is a
+/// fraction of it.
+pub const FILE_MAX: u64 = 64 << 20;
+
+/// The most pixels a picture may have: 50 megapixels, 200 MB decoded. A
+/// file that claims more is refused before anything is allocated for it.
+const PIXELS_MAX: usize = 50_000_000;
+
+/// The format of a picture whose file starts with `head`, if it is one
+/// Pinnacle shows.
+pub fn sniff(head: &[u8]) -> Option<Format> {
+ Format::sniff(head)
+}
+
+/// A decoded picture, and the last fitting of it drawn.
+pub struct Picture {
+ image: Image,
+ fitted: Option<Fitted>,
+}
+
+/// A picture fitted into a box: where in the box (centred), its size, and
+/// its pixels as 0x00RRGGBB, transparency blended over the background.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub struct Fitted {
+ /// The box it was fitted into, and the background blended over.
+ room: (u32, u32, u32),
+ pub x: u32,
+ pub y: u32,
+ pub width: u32,
+ pub height: u32,
+ pub pixels: Vec<u32>,
+}
+
+impl Picture {
+ /// The picture in `data`, a whole file.
+ pub fn decode(data: &[u8]) -> Result<Picture, Error> {
+ let limits = Limits {
+ max_pixels: PIXELS_MAX,
+ ..Limits::default()
+ };
+ Ok(Picture {
+ image: wil_core::image::decode_with(data, &limits)?,
+ fitted: None,
+ })
+ }
+
+ pub fn width(&self) -> u32 {
+ self.image.width
+ }
+
+ pub fn height(&self) -> u32 {
+ self.image.height
+ }
+
+ /// The picture as large as fits in `width` × `height` with its shape
+ /// kept, never larger than it is, centred; kept until the box or the
+ /// background changes.
+ pub fn fit(&mut self, width: u32, height: u32, background: u32) -> &Fitted {
+ let room = (width, height, background);
+ if self.fitted.as_ref().is_none_or(|f| f.room != room) {
+ let image = self.image.fit(width.max(1), height.max(1));
+ self.fitted = Some(Fitted {
+ room,
+ x: width.saturating_sub(image.width) / 2,
+ y: height.saturating_sub(image.height) / 2,
+ width: image.width,
+ height: image.height,
+ pixels: over(&image, background),
+ });
+ }
+ self.fitted.as_ref().expect("fitted above")
+ }
+}
+
+/// `image`'s pixels as 0x00RRGGBB, each blended by its alpha over
+/// `background`.
+fn over(image: &Image, background: u32) -> Vec<u32> {
+ let mix = |c: u8, b: u32, a: u32| (u32::from(c) * a + b * (255 - a) + 127) / 255;
+ let (br, bg, bb) = (background >> 16 & 0xff, background >> 8 & 0xff, background & 0xff);
+ image
+ .pixels
+ .chunks_exact(4)
+ .map(|p| {
+ let a = u32::from(p[3]);
+ mix(p[0], br, a) << 16 | mix(p[1], bg, a) << 8 | mix(p[2], bb, a)
+ })
+ .collect()
+}
diff --git a/media/src/tests.rs b/media/src/tests.rs
index aa91d61..ad9c3ff 100644
--- a/media/src/tests.rs
+++ b/media/src/tests.rs
@@ -111,3 +111,54 @@ fn adpcm_keeps_a_tone() {
let snr = 10.0 * (signal / noise).log10();
assert!(snr > 20.0, "SNR {snr} dB");
}
+
+/// A PNG `w` × `h`: red on the left half, see-through blue on the right.
+fn png(w: u32, h: u32) -> Vec<u8> {
+ let mut image = wil_core::Image::new(w, h);
+ for (i, p) in image.pixels.chunks_exact_mut(4).enumerate() {
+ let left = (i as u32 % w) < w / 2;
+ p.copy_from_slice(if left { &[255, 0, 0, 255] } else { &[0, 0, 255, 0] });
+ }
+ wil_core::png::encode(&image, wil_core::deflate::Level::DEFAULT)
+}
+
+#[test]
+fn a_picture_is_told_by_its_bytes_not_its_name() {
+ assert_eq!(picture::sniff(&png(4, 4)), Some(picture::Format::Png));
+ assert_eq!(picture::sniff(b"Shopping list\nbread"), None);
+ // A file of zeros and odd bytes, which nothing opens.
+ assert_eq!(picture::sniff(&[0, 1, 2, 3, 0xfe, 0, 7, 8]), None);
+ assert_eq!(picture::sniff(&[]), None);
+}
+
+#[test]
+fn a_picture_is_fitted_into_its_box_keeping_its_shape() {
+ let mut p = picture::Picture::decode(&png(400, 200)).unwrap();
+ assert_eq!((p.width(), p.height()), (400, 200));
+ // A tall box: as wide as the box, centred from top to bottom.
+ let f = p.fit(200, 300, 0x00_ff_ff_ff);
+ assert_eq!((f.width, f.height, f.x, f.y), (200, 100, 0, 100));
+ assert_eq!(f.pixels.len(), 200 * 100);
+ // Red where it is opaque; the see-through half is the background.
+ assert_eq!(f.pixels[0], 0x00_ff_00_00);
+ assert_eq!(f.pixels[199], 0x00_ff_ff_ff);
+ // A wide box: as high as the box, centred from side to side.
+ let f = p.fit(1000, 50, 0);
+ assert_eq!((f.width, f.height, f.x, f.y), (100, 50, 450, 0));
+ assert_eq!(f.pixels[99], 0);
+ // A box larger than the picture: never grown, only centred.
+ let f = p.fit(800, 600, 0);
+ assert_eq!((f.width, f.height, f.x, f.y), (400, 200, 200, 200));
+}
+
+#[test]
+fn a_broken_or_huge_picture_is_refused_not_a_panic() {
+ let mut data = png(64, 64);
+ data.truncate(data.len() / 2);
+ assert!(picture::Picture::decode(&data).is_err());
+ // Says it is 60000 × 60000 (3.6 gigapixels) in its header.
+ let mut data = png(1, 1);
+ data[16..20].copy_from_slice(&60_000u32.to_be_bytes());
+ data[20..24].copy_from_slice(&60_000u32.to_be_bytes());
+ assert!(picture::Picture::decode(&data).is_err());
+}