1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//! A pixel-readback capability for [`embedded-graphics`] draw targets.
//!
//! Compositing needs to know what is already on the target before it writes.
//! Antialiased edges, translucent fills, blend modes and every other
//! read-modify-write effect blend a foreground colour *into* a backdrop, and
//! the backdrop is whatever the target currently holds. [`ReadbackTarget`] is
//! the capability that says a target can answer that question, so rendering
//! code can require it generically:
//!
//! ```
//! use embedded_graphics_core::{draw_target::DrawTarget, primitives::Rectangle};
//! use embedded_graphics_readback::ReadbackTarget;
//!
//! # fn blend_over<C>(fg: C, _bg: C, _coverage: u8) -> C { fg }
//! /// Blend `fg` over one scanline run at the given per-pixel coverage.
//! fn composite_run<D: ReadbackTarget>(
//! target: &mut D,
//! run: &Rectangle,
//! fg: D::Color,
//! coverage: &[u8],
//! scratch: &mut [D::Color],
//! ) {
//! target.read_area(run, scratch);
//! for (px, &cov) in scratch.iter_mut().zip(coverage) {
//! *px = blend_over(fg, *px, cov);
//! }
//! let _ = target.fill_contiguous(run, scratch.iter().copied());
//! }
//! ```
//!
//! `ReadbackTarget: DrawTarget`, so a single bound supplies both halves of that
//! loop and a single `Self::Color` names the pixel type throughout. The
//! immutable read finishes before the mutable write begins, so one `&mut D`
//! carries the whole operation — no staging buffer for the shape's bounding
//! box, just a scratch run.
//!
//! It is a thin, dependency-light contract over [`embedded-graphics-core`]:
//! [`read_pixel`](ReadbackTarget::read_pixel) is the one required method, and
//! [`read_area`](ReadbackTarget::read_area) copies a region out row-major in
//! one call — defaulting to a `read_pixel` loop, overridable with a block copy.
//!
//! # Layers
//!
//! Because the capability belongs to the *target*, a wrapper carries it forward
//! by delegating both methods. [`adapters`] ships the three that matter —
//! [`Shifted`](adapters::Shifted), [`Windowed`](adapters::Windowed) and
//! [`Masked`](adapters::Masked) — so a pipeline keeps readback end to end:
//!
//! ```
//! # use embedded_graphics_core::{
//! # Pixel, draw_target::DrawTarget, geometry::{OriginDimensions, Point, Size},
//! # pixelcolor::{Rgb565, RgbColor}, primitives::Rectangle,
//! # };
//! # use embedded_graphics_readback::ReadbackTarget;
//! use embedded_graphics_readback::ReadbackTargetExt;
//!
//! # struct Fb([Rgb565; 64]);
//! # impl Fb {
//! # fn index(p: Point) -> Option<usize> {
//! # ((0..8).contains(&p.x) && (0..8).contains(&p.y)).then(|| (p.y * 8 + p.x) as usize)
//! # }
//! # }
//! # impl OriginDimensions for Fb {
//! # fn size(&self) -> Size { Size::new(8, 8) }
//! # }
//! # impl DrawTarget for Fb {
//! # type Color = Rgb565;
//! # type Error = core::convert::Infallible;
//! # fn draw_iter<I>(&mut self, _: I) -> Result<(), Self::Error>
//! # where I: IntoIterator<Item = Pixel<Self::Color>> { Ok(()) }
//! # }
//! # impl ReadbackTarget for Fb {
//! # fn read_pixel(&self, p: Point) -> Option<Rgb565> {
//! # Fb::index(p).map(|i| self.0[i])
//! # }
//! # }
//! # let mut fb = Fb([Rgb565::BLACK; 64]);
//! let mut layer = fb.shifted(Point::new(2, 2));
//! let mut view = layer.masked(&Rectangle::new(Point::zero(), Size::new(4, 4)));
//!
//! composite_run(&mut view, /* ... */);
//! # fn composite_run<D: ReadbackTarget>(_: &mut D) {}
//! ```
//!
//! `embedded-graphics`' own [`DrawTargetExt`] adapters hold their parent in a
//! private field with no accessor, so readback cannot be delegated through
//! them — these are read-capable equivalents with the same write semantics.
//!
//! # Implementing it
//!
//! [`read_pixel`](ReadbackTarget::read_pixel) shares its signature with
//! [`GetPixel::pixel`](embedded_graphics_core::image::GetPixel::pixel), so a
//! target that already implements that trait delegates in one line. A reader
//! that hands back a bare colour needs a bounds guard instead, since
//! `read_pixel` answers `None` outside the bounding box:
//!
//! ```
//! use embedded_graphics_core::{
//! draw_target::DrawTarget,
//! geometry::{Dimensions, Point},
//! image::GetPixel,
//! pixelcolor::Rgb565,
//! };
//! use embedded_graphics_readback::ReadbackTarget;
//!
//! # use embedded_graphics_core::{geometry::Size, primitives::Rectangle, Pixel};
//! # struct Canvas;
//! # impl Canvas {
//! # fn get_pixel(&self, _p: Point) -> Rgb565 { Rgb565::new(0, 0, 0) }
//! # }
//! # struct Fb;
//! # struct Sim(Canvas);
//! # impl Dimensions for Fb {
//! # fn bounding_box(&self) -> Rectangle { Rectangle::new(Point::zero(), Size::new(64, 64)) }
//! # }
//! # impl DrawTarget for Fb {
//! # type Color = Rgb565;
//! # type Error = core::convert::Infallible;
//! # fn draw_iter<I>(&mut self, _: I) -> Result<(), Self::Error>
//! # where I: IntoIterator<Item = Pixel<Self::Color>> { Ok(()) }
//! # }
//! # impl GetPixel for Fb {
//! # type Color = Rgb565;
//! # fn pixel(&self, _p: Point) -> Option<Rgb565> { None }
//! # }
//! # impl Dimensions for Sim {
//! # fn bounding_box(&self) -> Rectangle { Rectangle::new(Point::zero(), Size::new(64, 64)) }
//! # }
//! # impl DrawTarget for Sim {
//! # type Color = Rgb565;
//! # type Error = core::convert::Infallible;
//! # fn draw_iter<I>(&mut self, _: I) -> Result<(), Self::Error>
//! # where I: IntoIterator<Item = Pixel<Self::Color>> { Ok(()) }
//! # }
//! // Already a `GetPixel`: delegate.
//! impl ReadbackTarget for Fb {
//! fn read_pixel(&self, point: Point) -> Option<Self::Color> {
//! GetPixel::pixel(self, point)
//! }
//! }
//!
//! // Hands back a bare colour: guard on the bounding box.
//! impl ReadbackTarget for Sim {
//! fn read_pixel(&self, point: Point) -> Option<Self::Color> {
//! self.bounding_box()
//! .contains(point)
//! .then(|| self.0.get_pixel(point))
//! }
//! }
//! ```
//!
//! # `embedded-graphics` framebuffers
//!
//! [`embedded_graphics::framebuffer::Framebuffer`] is supported out of the box
//! under the `framebuffer` feature:
//!
//! ```toml
//! embedded-graphics-readback = { version = "0.1", features = ["framebuffer"] }
//! ```
//!
//! The impl covers every colour type and data order `embedded-graphics` makes
//! drawable, and overrides [`read_area`](ReadbackTarget::read_area) to build
//! the backing image once per region. Draw into a `Framebuffer`, hand it to any
//! `ReadbackTarget` renderer, and flush it to the panel — which also makes it
//! the shortest route to readback for a streaming driver.
//!
//! # Who implements it
//!
//! Framebuffers and canvases — anything holding pixels in RAM, where a read is
//! a slice index — are the natural implementors, along with buffered display
//! drivers that expose their buffer and simulators wrapped in a newtype.
//! Streaming drivers hold no pixels, so they leave the trait unimplemented and
//! callers keep the write-only path.
//!
//! [`embedded_graphics::framebuffer::Framebuffer`]:
//! https://docs.rs/embedded-graphics/latest/embedded_graphics/framebuffer/struct.Framebuffer.html
//! [`DrawTargetExt`]:
//! https://docs.rs/embedded-graphics/latest/embedded_graphics/draw_target/trait.DrawTargetExt.html
//!
//! [`embedded-graphics`]: https://docs.rs/embedded-graphics
//! [`embedded-graphics-core`]: https://docs.rs/embedded-graphics-core
//! [`DrawTarget`]: embedded_graphics_core::draw_target::DrawTarget
pub use ReadbackTargetExt;
use ;
/// A [`DrawTarget`] that can read back the pixels it has drawn.
///
/// Implement this for targets that retain their pixels in memory (framebuffers,
/// canvases, simulators). Streaming targets that forward straight to a bus
/// cannot read back and should not implement it.
///
/// # Cost contract
///
/// Destination-aware renderers call [`read_area`](Self::read_area) **once per
/// run on the hot path** — they assume a read is roughly the cost of a write.
/// That holds for RAM-backed framebuffers, where a read is slice arithmetic;
/// those are the intended targets. A panel whose only read primitive is a slow
/// per-pixel bus command should not implement this trait merely because it can:
/// the default [`read_area`](Self::read_area) turns every antialiased run into
/// `width` bus round-trips. Implement it where reads are cheap, and override
/// [`read_area`](Self::read_area) with a block copy whenever the target allows
/// it.
/// The per-pixel loop behind [`ReadbackTarget::read_area`]'s default; the
/// [`adapters`] fall back to it where a region only partly overlaps a layer.
pub