1
1
use crate :: drawing:: backend:: { BackendCoord , BackendStyle , DrawingBackend , DrawingErrorKind } ;
2
2
use crate :: style:: { Color , RGBAColor } ;
3
+ use std:: marker:: PhantomData ;
3
4
4
- use image:: { ImageBuffer , ImageError , Rgb } ;
5
+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "image" ) ) ]
6
+ mod image_encoding_support {
7
+ pub ( super ) use image:: { ImageBuffer , ImageError , Rgb } ;
8
+ pub ( super ) use std:: path:: Path ;
9
+ pub ( super ) type BorrowedImage < ' a > = ImageBuffer < Rgb < u8 > , & ' a mut [ u8 ] > ;
10
+ }
11
+
12
+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "image" ) ) ]
13
+ use image_encoding_support:: * ;
14
+
15
+ #[ derive( Debug ) ]
16
+ pub enum BitMapBackendError {
17
+ InvalidBuffer ,
18
+ IOError ( std:: io:: Error ) ,
19
+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "image" ) ) ]
20
+ ImageError ( ImageError ) ,
21
+ }
5
22
6
- use std:: path:: Path ;
23
+ impl std:: fmt:: Display for BitMapBackendError {
24
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
25
+ write ! ( f, "{:?}" , self )
26
+ }
27
+ }
7
28
8
- pub type BorrowedImage < ' a > = ImageBuffer < Rgb < u8 > , & ' a mut [ u8 ] > ;
29
+ impl std :: error :: Error for BitMapBackendError { }
9
30
10
31
fn blend ( prev : & mut u8 , new : u8 , a : f64 ) {
11
32
* prev = ( ( f64:: from ( * prev) ) * ( 1.0 - a) + a * f64:: from ( new) ) . min ( 255.0 ) as u8 ;
12
33
}
13
34
14
- #[ cfg( feature = "gif" ) ]
35
+ #[ cfg( all ( feature = "gif" , not ( target_arch = "wasm32" ) , feature = "image" ) ) ]
15
36
mod gif_support {
16
37
use super :: * ;
17
38
use gif:: { Encoder as GifEncoder , Frame as GifFrame , Repeat , SetParameter } ;
@@ -29,15 +50,18 @@ mod gif_support {
29
50
path : T ,
30
51
dim : ( u32 , u32 ) ,
31
52
delay : u32 ,
32
- ) -> Result < Self , ImageError > {
53
+ ) -> Result < Self , BitMapBackendError > {
33
54
let mut encoder = GifEncoder :: new (
34
- File :: create ( path. as_ref ( ) ) . map_err ( ImageError :: IoError ) ?,
55
+ File :: create ( path. as_ref ( ) ) . map_err ( BitMapBackendError :: IOError ) ?,
35
56
dim. 0 as u16 ,
36
57
dim. 1 as u16 ,
37
58
& [ ] ,
38
- ) ?;
59
+ )
60
+ . map_err ( BitMapBackendError :: IOError ) ?;
39
61
40
- encoder. set ( Repeat :: Infinite ) ?;
62
+ encoder
63
+ . set ( Repeat :: Infinite )
64
+ . map_err ( BitMapBackendError :: IOError ) ?;
41
65
42
66
Ok ( Self {
43
67
encoder,
@@ -47,34 +71,39 @@ mod gif_support {
47
71
} )
48
72
}
49
73
50
- pub ( super ) fn flush_frame ( & mut self , buffer : & [ u8 ] ) -> Result < ( ) , ImageError > {
74
+ pub ( super ) fn flush_frame ( & mut self , buffer : & [ u8 ] ) -> Result < ( ) , BitMapBackendError > {
51
75
let mut frame =
52
76
GifFrame :: from_rgb_speed ( self . width as u16 , self . height as u16 , buffer, 10 ) ;
53
77
54
78
frame. delay = self . delay as u16 ;
55
79
56
- self . encoder . write_frame ( & frame) ?;
80
+ self . encoder
81
+ . write_frame ( & frame)
82
+ . map_err ( BitMapBackendError :: IOError ) ?;
57
83
58
84
Ok ( ( ) )
59
85
}
60
86
}
61
87
}
62
88
63
89
enum Target < ' a > {
90
+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "image" ) ) ]
64
91
File ( & ' a Path ) ,
65
- Buffer ,
66
- #[ cfg( feature = "gif" ) ]
92
+ Buffer ( PhantomData < & ' a u32 > ) ,
93
+ #[ cfg( all ( feature = "gif" , not ( target_arch = "wasm32" ) , feature = "image" ) ) ]
67
94
Gif ( Box < gif_support:: GifFile > ) ,
68
95
}
69
96
70
97
enum Buffer < ' a > {
98
+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "image" ) ) ]
71
99
Owned ( Vec < u8 > ) ,
72
100
Borrowed ( & ' a mut [ u8 ] ) ,
73
101
}
74
102
75
103
impl < ' a > Buffer < ' a > {
76
104
fn borrow_buffer ( & mut self ) -> & mut [ u8 ] {
77
105
match self {
106
+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "image" ) ) ]
78
107
Buffer :: Owned ( buf) => & mut buf[ ..] ,
79
108
Buffer :: Borrowed ( buf) => * buf,
80
109
}
@@ -84,20 +113,19 @@ impl<'a> Buffer<'a> {
84
113
/// The backend that drawing a bitmap
85
114
pub struct BitMapBackend < ' a > {
86
115
/// The path to the image
116
+ #[ allow( dead_code) ]
87
117
target : Target < ' a > ,
88
-
89
118
/// The size of the image
90
119
size : ( u32 , u32 ) ,
91
-
92
120
/// The data buffer of the image
93
121
buffer : Buffer < ' a > ,
94
-
95
122
/// Flag indicates if the bitmap has been saved
96
123
saved : bool ,
97
124
}
98
125
99
126
impl < ' a > BitMapBackend < ' a > {
100
127
/// Create a new bitmap backend
128
+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "image" ) ) ]
101
129
pub fn new < T : AsRef < Path > + ?Sized > ( path : & ' a T , ( w, h) : ( u32 , u32 ) ) -> Self {
102
130
Self {
103
131
target : Target :: File ( path. as_ref ( ) ) ,
@@ -116,12 +144,12 @@ impl<'a> BitMapBackend<'a> {
116
144
/// - `path`: The path to the GIF file to create
117
145
/// - `dimension`: The size of the GIF image
118
146
/// - `speed`: The amount of time for each frame to display
119
- #[ cfg( feature = "gif" ) ]
147
+ #[ cfg( all ( feature = "gif" , not ( target_arch = "wasm32" ) , feature = "image" ) ) ]
120
148
pub fn gif < T : AsRef < Path > > (
121
149
path : T ,
122
150
( w, h) : ( u32 , u32 ) ,
123
151
frame_delay : u32 ,
124
- ) -> Result < Self , ImageError > {
152
+ ) -> Result < Self , BitMapBackendError > {
125
153
Ok ( Self {
126
154
target : Target :: Gif ( Box :: new ( gif_support:: GifFile :: new (
127
155
path,
@@ -153,7 +181,7 @@ impl<'a> BitMapBackend<'a> {
153
181
}
154
182
155
183
Self {
156
- target : Target :: Buffer ,
184
+ target : Target :: Buffer ( PhantomData ) ,
157
185
size : ( w, h) ,
158
186
buffer : Buffer :: Borrowed ( buf) ,
159
187
saved : false ,
@@ -331,33 +359,41 @@ impl<'a> BitMapBackend<'a> {
331
359
}
332
360
333
361
impl < ' a > DrawingBackend for BitMapBackend < ' a > {
334
- type ErrorType = ImageError ;
362
+ type ErrorType = BitMapBackendError ;
335
363
336
364
fn get_size ( & self ) -> ( u32 , u32 ) {
337
365
self . size
338
366
}
339
367
340
- fn ensure_prepared ( & mut self ) -> Result < ( ) , DrawingErrorKind < ImageError > > {
368
+ fn ensure_prepared ( & mut self ) -> Result < ( ) , DrawingErrorKind < BitMapBackendError > > {
341
369
self . saved = false ;
342
370
Ok ( ( ) )
343
371
}
344
372
345
- fn present ( & mut self ) -> Result < ( ) , DrawingErrorKind < ImageError > > {
373
+ #[ cfg( any( target_arch = "wasm32" , not( feature = "image" ) ) ) ]
374
+ fn present ( & mut self ) -> Result < ( ) , DrawingErrorKind < BitMapBackendError > > {
375
+ Ok ( ( ) )
376
+ }
377
+
378
+ #[ cfg( all( not( target_arch = "wasm32" ) , feature = "image" ) ) ]
379
+ fn present ( & mut self ) -> Result < ( ) , DrawingErrorKind < BitMapBackendError > > {
346
380
let ( w, h) = self . get_size ( ) ;
347
381
match & mut self . target {
348
382
Target :: File ( path) => {
349
383
if let Some ( img) = BorrowedImage :: from_raw ( w, h, self . buffer . borrow_buffer ( ) ) {
350
- img. save ( & path)
351
- . map_err ( |x| DrawingErrorKind :: DrawingError ( ImageError :: IoError ( x) ) ) ?;
384
+ img. save ( & path) . map_err ( |x| {
385
+ DrawingErrorKind :: DrawingError ( BitMapBackendError :: IOError ( x) )
386
+ } ) ?;
352
387
self . saved = true ;
353
388
Ok ( ( ) )
354
389
} else {
355
- Err ( DrawingErrorKind :: DrawingError ( ImageError :: DimensionError ) )
390
+ Err ( DrawingErrorKind :: DrawingError (
391
+ BitMapBackendError :: InvalidBuffer ,
392
+ ) )
356
393
}
357
394
}
358
- Target :: Buffer => Ok ( ( ) ) ,
395
+ Target :: Buffer ( _ ) => Ok ( ( ) ) ,
359
396
360
- #[ cfg( feature = "gif" ) ]
361
397
Target :: Gif ( target) => {
362
398
target
363
399
. flush_frame ( self . buffer . borrow_buffer ( ) )
@@ -372,7 +408,7 @@ impl<'a> DrawingBackend for BitMapBackend<'a> {
372
408
& mut self ,
373
409
point : BackendCoord ,
374
410
color : & RGBAColor ,
375
- ) -> Result < ( ) , DrawingErrorKind < ImageError > > {
411
+ ) -> Result < ( ) , DrawingErrorKind < BitMapBackendError > > {
376
412
if point. 0 < 0 || point. 1 < 0 {
377
413
return Ok ( ( ) ) ;
378
414
}
@@ -474,10 +510,10 @@ impl<'a> DrawingBackend for BitMapBackend<'a> {
474
510
fn blit_bitmap < ' b > (
475
511
& mut self ,
476
512
pos : BackendCoord ,
477
- src : & ' b image:: ImageBuffer < image:: Rgb < u8 > , & ' b [ u8 ] > ,
513
+ ( sw, sh) : ( u32 , u32 ) ,
514
+ src : & ' b [ u8 ] ,
478
515
) -> Result < ( ) , DrawingErrorKind < Self :: ErrorType > > {
479
516
let ( dw, dh) = self . get_size ( ) ;
480
- let ( sw, sh) = src. dimensions ( ) ;
481
517
482
518
let ( x0, y0) = pos;
483
519
let ( x1, y1) = ( x0 + sw as i32 , y0 + sh as i32 ) ;
@@ -498,7 +534,7 @@ impl<'a> DrawingBackend for BitMapBackend<'a> {
498
534
let mut dst = & mut self . get_raw_pixel_buffer ( ) [ dst_start..] ;
499
535
500
536
let src_start = 3 * ( ( sh as i32 + y0 - y1) * sw as i32 + ( sw as i32 + x0 - x1) ) as usize ;
501
- let mut src = & ( * * src) [ src_start..] ;
537
+ let mut src = & src[ src_start..] ;
502
538
503
539
if src_gap == 0 && dst_gap == 0 {
504
540
chunk_size *= num_chunks;
0 commit comments