1- use std:: { convert:: TryFrom , fs:: File , io:: BufReader , path:: Path } ;
1+ use std:: {
2+ convert:: TryFrom ,
3+ fs:: File ,
4+ io:: BufReader ,
5+ path:: Path ,
6+ sync:: atomic:: { AtomicUsize , Ordering } ,
7+ } ;
28
39use embedded_graphics:: {
410 pixelcolor:: { raw:: ToBytes , BinaryColor , Gray8 , Rgb888 } ,
@@ -7,14 +13,23 @@ use embedded_graphics::{
713
814use crate :: { output_image:: OutputImage , output_settings:: OutputSettings } ;
915
16+ static NEXT_ID : AtomicUsize = AtomicUsize :: new ( 0 ) ;
17+
1018/// Simulator display.
11- #[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
19+ #[ derive( Debug , Clone , Eq , PartialOrd , Ord , Hash ) ]
1220pub struct SimulatorDisplay < C > {
1321 size : Size ,
1422 pub ( crate ) pixels : Box < [ C ] > ,
23+ pub ( crate ) id : usize ,
1524}
1625
1726impl < C : PixelColor > SimulatorDisplay < C > {
27+ fn new_common ( size : Size , pixels : Box < [ C ] > ) -> Self {
28+ let id = NEXT_ID . fetch_add ( 1 , Ordering :: SeqCst ) ;
29+
30+ Self { size, pixels, id }
31+ }
32+
1833 /// Creates a new display filled with a color.
1934 ///
2035 /// This constructor can be used if `C` doesn't implement `From<BinaryColor>` or another
@@ -23,7 +38,7 @@ impl<C: PixelColor> SimulatorDisplay<C> {
2338 let pixel_count = size. width as usize * size. height as usize ;
2439 let pixels = vec ! [ default_color; pixel_count] . into_boxed_slice ( ) ;
2540
26- SimulatorDisplay { size, pixels }
41+ SimulatorDisplay :: new_common ( size, pixels)
2742 }
2843
2944 /// Returns the color of the pixel at a point.
@@ -75,10 +90,7 @@ impl<C: PixelColor> SimulatorDisplay<C> {
7590 . into_boxed_slice ( ) ;
7691
7792 if pixels. iter ( ) . any ( |p| * p == BinaryColor :: On ) {
78- Some ( SimulatorDisplay {
79- pixels,
80- size : self . size ,
81- } )
93+ Some ( SimulatorDisplay :: new_common ( self . size , pixels) )
8294 } else {
8395 None
8496 }
@@ -122,8 +134,9 @@ where
122134 /// // example: output_image.save_png("out.png")?;
123135 /// ```
124136 pub fn to_rgb_output_image ( & self , output_settings : & OutputSettings ) -> OutputImage < Rgb888 > {
125- let mut output = OutputImage :: new ( self , output_settings) ;
126- output. update ( self ) ;
137+ let size = output_settings. framebuffer_size ( self ) ;
138+ let mut output = OutputImage :: new ( size) ;
139+ output. update ( self , Point :: zero ( ) , output_settings) ;
127140
128141 output
129142 }
@@ -152,8 +165,9 @@ where
152165 & self ,
153166 output_settings : & OutputSettings ,
154167 ) -> OutputImage < Gray8 > {
155- let mut output = OutputImage :: new ( self , output_settings) ;
156- output. update ( self ) ;
168+ let size = output_settings. framebuffer_size ( self ) ;
169+ let mut output = OutputImage :: new ( size) ;
170+ output. update ( self , Point :: zero ( ) , output_settings) ;
157171
158172 output
159173 }
@@ -226,10 +240,10 @@ where
226240 . map ( |p| Rgb888 :: new ( p[ 0 ] , p[ 1 ] , p[ 2 ] ) . into ( ) )
227241 . collect ( ) ;
228242
229- Ok ( Self {
230- size : Size :: new ( image. width ( ) , image. height ( ) ) ,
243+ Ok ( Self :: new_common (
244+ Size :: new ( image. width ( ) , image. height ( ) ) ,
231245 pixels,
232- } )
246+ ) )
233247 }
234248}
235249
@@ -257,6 +271,12 @@ impl<C> OriginDimensions for SimulatorDisplay<C> {
257271 }
258272}
259273
274+ impl < C : PartialEq > PartialEq for SimulatorDisplay < C > {
275+ fn eq ( & self , other : & Self ) -> bool {
276+ self . size == other. size && self . pixels == other. pixels
277+ }
278+ }
279+
260280#[ cfg( test) ]
261281mod tests {
262282 use super :: * ;
@@ -321,6 +341,7 @@ mod tests {
321341 . map ( |c| BinaryColor :: from ( * c != 0 ) )
322342 . collect :: < Vec < _ > > ( )
323343 . into_boxed_slice ( ) ,
344+ id : 0 ,
324345 } ;
325346
326347 let expected = [
@@ -345,6 +366,7 @@ mod tests {
345366 . map ( |c| Gray2 :: new ( * c) )
346367 . collect :: < Vec < _ > > ( )
347368 . into_boxed_slice ( ) ,
369+ id : 0 ,
348370 } ;
349371
350372 let expected = [
@@ -370,6 +392,7 @@ mod tests {
370392 . map ( |c| Gray4 :: new ( * c) )
371393 . collect :: < Vec < _ > > ( )
372394 . into_boxed_slice ( ) ,
395+ id : 0 ,
373396 } ;
374397
375398 let expected = [
@@ -398,6 +421,7 @@ mod tests {
398421 . map ( Gray8 :: new)
399422 . collect :: < Vec < _ > > ( )
400423 . into_boxed_slice ( ) ,
424+ id : 0 ,
401425 } ;
402426
403427 assert_eq ! ( & display. to_be_bytes( ) , & expected) ;
@@ -412,6 +436,7 @@ mod tests {
412436 let display = SimulatorDisplay {
413437 size : Size :: new ( 2 , 1 ) ,
414438 pixels : expected. clone ( ) . into_boxed_slice ( ) ,
439+ id : 0 ,
415440 } ;
416441
417442 assert_eq ! ( & display. to_be_bytes( ) , & [ 0x80 , 0x00 , 0x00 , 0x01 ] ) ;
@@ -425,6 +450,7 @@ mod tests {
425450 let display = SimulatorDisplay {
426451 size : Size :: new ( 2 , 1 ) ,
427452 pixels : expected. clone ( ) . into_boxed_slice ( ) ,
453+ id : 0 ,
428454 } ;
429455
430456 assert_eq ! (
0 commit comments