@@ -111,7 +111,13 @@ impl<'a> Buffer<'a> {
111
111
}
112
112
113
113
/// The backend that drawing a bitmap
114
- pub struct BitMapBackend < ' a , P : ' static + Pixel < Subpixel =u8 > > {
114
+ pub struct BitMapBackend <
115
+ ' a ,
116
+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "image" ) ) ]
117
+ P : ' static + Pixel < Subpixel =u8 > ,
118
+ #[ cfg( target_arch = "wasm32" ) ]
119
+ P : ' static
120
+ > {
115
121
_pix : PhantomData < P > ,
116
122
/// The path to the image
117
123
#[ allow( dead_code) ]
@@ -124,7 +130,13 @@ pub struct BitMapBackend<'a, P: 'static + Pixel<Subpixel=u8>> {
124
130
saved : bool ,
125
131
}
126
132
127
- impl < ' a , P : ' static + Pixel < Subpixel =u8 > > BitMapBackend < ' a , P > {
133
+ impl <
134
+ ' a ,
135
+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "image" ) ) ]
136
+ P : ' static + Pixel < Subpixel =u8 > ,
137
+ #[ cfg( target_arch = "wasm32" ) ]
138
+ P : ' static
139
+ > BitMapBackend < ' a , P > {
128
140
/// Create a new bitmap backend
129
141
#[ cfg( all( not( target_arch = "wasm32" ) , feature = "image" ) ) ]
130
142
pub fn new < T : AsRef < Path > + ?Sized > ( path : & ' a T , ( w, h) : ( u32 , u32 ) ) -> Self {
@@ -165,6 +177,16 @@ impl<'a, P: 'static + Pixel<Subpixel=u8>> BitMapBackend<'a, P> {
165
177
} )
166
178
}
167
179
180
+ #[ cfg( target_arch = "wasm32" ) ]
181
+ fn get_channel ( ) -> u32 {
182
+ 3
183
+ }
184
+
185
+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "image" ) ) ]
186
+ fn get_channel ( ) -> u32 {
187
+ P :: CHANNEL_COUNT
188
+ }
189
+
168
190
/// Create a new bitmap backend which only lives in-memory
169
191
///
170
192
/// When this is used, the bitmap backend will write to a user provided [u8] array (or Vec<u8>).
@@ -173,7 +195,8 @@ impl<'a, P: 'static + Pixel<Subpixel=u8>> BitMapBackend<'a, P> {
173
195
/// - `buf`: The buffer to operate
174
196
/// - `dimension`: The size of the image in pixels
175
197
pub fn with_buffer ( buf : & ' a mut [ u8 ] , ( w, h) : ( u32 , u32 ) ) -> Self {
176
- if ( w * h * P :: CHANNEL_COUNT as u32 ) as usize > buf. len ( ) {
198
+ let c = BitMapBackend :: < P > :: get_channel ( ) ;
199
+ if ( w * h * c as u32 ) as usize > buf. len ( ) {
177
200
// TODO: This doesn't deserve a panic.
178
201
panic ! (
179
202
"Wrong image size: H = {}, W = {}, BufSize = {}" ,
@@ -362,7 +385,13 @@ impl<'a, P: 'static + Pixel<Subpixel=u8>> BitMapBackend<'a, P> {
362
385
}
363
386
}
364
387
365
- impl < ' a , P : ' static + Pixel < Subpixel =u8 > > DrawingBackend for BitMapBackend < ' a , P > {
388
+ impl <
389
+ ' a ,
390
+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "image" ) ) ]
391
+ P : ' static + Pixel < Subpixel =u8 > ,
392
+ #[ cfg( target_arch = "wasm32" ) ]
393
+ P : ' static
394
+ > DrawingBackend for BitMapBackend < ' a , P > {
366
395
type ErrorType = BitMapBackendError ;
367
396
368
397
fn get_size ( & self ) -> ( u32 , u32 ) {
@@ -556,7 +585,12 @@ impl<'a, P: 'static + Pixel<Subpixel=u8>> DrawingBackend for BitMapBackend<'a, P
556
585
}
557
586
}
558
587
559
- impl < P : ' static + Pixel < Subpixel =u8 > > Drop for BitMapBackend < ' _ , P > {
588
+ impl <
589
+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "image" ) ) ]
590
+ P : ' static + Pixel < Subpixel =u8 > ,
591
+ #[ cfg( target_arch = "wasm32" ) ]
592
+ P : ' static
593
+ > Drop for BitMapBackend < ' _ , P > {
560
594
fn drop ( & mut self ) {
561
595
if !self . saved {
562
596
self . present ( ) . expect ( "Unable to save the bitmap" ) ;
0 commit comments