Skip to content

Commit 8b5b823

Browse files
committed
fix target_arch="wasm32"
1 parent 673a23d commit 8b5b823

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

src/drawing/backend_impl/bitmap.rs

+39-5
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,13 @@ impl<'a> Buffer<'a> {
111111
}
112112

113113
/// 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+
> {
115121
_pix: PhantomData<P>,
116122
/// The path to the image
117123
#[allow(dead_code)]
@@ -124,7 +130,13 @@ pub struct BitMapBackend<'a, P: 'static + Pixel<Subpixel=u8>> {
124130
saved: bool,
125131
}
126132

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> {
128140
/// Create a new bitmap backend
129141
#[cfg(all(not(target_arch = "wasm32"), feature = "image"))]
130142
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> {
165177
})
166178
}
167179

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+
168190
/// Create a new bitmap backend which only lives in-memory
169191
///
170192
/// 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> {
173195
/// - `buf`: The buffer to operate
174196
/// - `dimension`: The size of the image in pixels
175197
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() {
177200
// TODO: This doesn't deserve a panic.
178201
panic!(
179202
"Wrong image size: H = {}, W = {}, BufSize = {}",
@@ -362,7 +385,13 @@ impl<'a, P: 'static + Pixel<Subpixel=u8>> BitMapBackend<'a, P> {
362385
}
363386
}
364387

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> {
366395
type ErrorType = BitMapBackendError;
367396

368397
fn get_size(&self) -> (u32, u32) {
@@ -556,7 +585,12 @@ impl<'a, P: 'static + Pixel<Subpixel=u8>> DrawingBackend for BitMapBackend<'a, P
556585
}
557586
}
558587

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> {
560594
fn drop(&mut self) {
561595
if !self.saved {
562596
self.present().expect("Unable to save the bitmap");

src/element/image.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ impl<'a, Coord> BitMapElement<'a, Coord> {
4848

4949
/// Make the bitmap element as a bitmap backend, so that we can use
5050
/// plotters drawing functionality on the bitmap element
51-
pub fn as_bitmap_backend<P: image::Pixel<Subpixel=u8>>(&mut self) -> BitMapBackend<P> {
51+
pub fn as_bitmap_backend<
52+
#[cfg(all(not(target_arch = "wasm32"), feature = "image"))]
53+
P: 'static + Pixel<Subpixel=u8>,
54+
#[cfg(target_arch = "wasm32")]
55+
P: 'static
56+
>(&mut self) -> BitMapBackend<P> {
5257
BitMapBackend::<P>::with_buffer(self.image.to_mut(), self.size)
5358
}
5459
}

0 commit comments

Comments
 (0)