adk_vision/sent.rs
1//! [`SentImage`] and the [`VisionInput`] seal: what a backend is handed.
2
3use std::sync::Arc;
4
5use crate::digest::Digest;
6use crate::egress::ReleasedImage;
7use crate::error::Result;
8use crate::geometry::TransformChain;
9use crate::media::DecodedImage;
10
11/// The image an on-device backend sees: pixels, the source image's digest, and the
12/// transform chain from the source to these pixels.
13///
14/// Every trait method takes a `SentImage` (on-device backends) or a [`ReleasedImage`]
15/// (remote backends), never an `ImageRef` or raw pixels. Only `VisionRuntime` constructs a
16/// `SentImage`, and only the egress gate a `ReleasedImage`. A remote backend implements its
17/// trait for `ReleasedImage`, so handing it a `SentImage` does not compile (V-I1):
18///
19/// ```compile_fail,E0308
20/// use adk_vision::{DetectQuery, Detector, ReleasedImage, SentImage, VisionCx};
21///
22/// async fn leak(remote: &dyn Detector<ReleasedImage>, cx: &VisionCx, image: &SentImage) {
23/// // A remote backend takes only an image the egress gate released.
24/// let _ = remote.detect(cx, image, &DetectQuery::Concepts(vec!["face".into()])).await;
25/// }
26/// ```
27///
28/// Neither can a `SentImage` be made outside the runtime:
29///
30/// ```compile_fail,E0451
31/// use std::sync::Arc;
32/// use adk_vision::{DecodedImage, Digest, SentImage, TransformChain};
33///
34/// let pixels = Arc::new(DecodedImage::filled(1, 1, [0, 0, 0, 255]));
35/// let forged = SentImage { source: Digest::of(b"x"), pixels, chain: TransformChain::new(1, 1) };
36/// ```
37#[derive(Clone, Debug)]
38pub struct SentImage {
39 source: Digest,
40 pixels: Arc<DecodedImage>,
41 chain: TransformChain,
42}
43
44impl SentImage {
45 pub(crate) fn new(source: Digest, pixels: Arc<DecodedImage>, chain: TransformChain) -> Self {
46 debug_assert_eq!(chain.output_size(), pixels.size());
47 Self { source, pixels, chain }
48 }
49
50 /// The digest of the source image these pixels came from.
51 pub fn source(&self) -> Digest {
52 self.source
53 }
54
55 /// The pixels.
56 pub fn pixels(&self) -> &DecodedImage {
57 &self.pixels
58 }
59
60 /// `(width, height)` of the pixels.
61 pub fn size(&self) -> (u32, u32) {
62 self.pixels.size()
63 }
64
65 /// The transforms from the source image to these pixels.
66 pub fn chain(&self) -> &TransformChain {
67 &self.chain
68 }
69
70 /// The pixels encoded as PNG.
71 ///
72 /// # Errors
73 ///
74 /// As [`DecodedImage::encode_png`].
75 pub fn encode_png(&self) -> Result<Vec<u8>> {
76 self.pixels.encode_png()
77 }
78}
79
80mod sealed {
81 pub trait Sealed {}
82 impl Sealed for super::SentImage {}
83 impl Sealed for crate::egress::ReleasedImage {}
84}
85
86/// The two kinds of image a backend can be handed: [`SentImage`] for on-device backends
87/// and [`ReleasedImage`] for remote ones. Sealed: no other type implements it.
88pub trait VisionInput: sealed::Sealed + Send + Sync + 'static {
89 /// `true` for [`ReleasedImage`].
90 const REMOTE: bool;
91
92 /// The clip a video backend of the same kind is handed:
93 /// [`SentClip`](crate::SentClip) on the device, [`ReleasedClip`](crate::ReleasedClip)
94 /// off it.
95 type Clip: Send + Sync + std::fmt::Debug + 'static;
96
97 /// The pixels and transform chain.
98 fn sent(&self) -> &SentImage;
99}
100
101impl VisionInput for SentImage {
102 const REMOTE: bool = false;
103
104 type Clip = crate::video::SentClip;
105
106 fn sent(&self) -> &SentImage {
107 self
108 }
109}
110
111impl VisionInput for ReleasedImage {
112 const REMOTE: bool = true;
113
114 type Clip = crate::egress::ReleasedClip;
115
116 fn sent(&self) -> &SentImage {
117 self.image()
118 }
119}