1
1
use crate :: { DivoomAPIError , DivoomAPIResult } ;
2
2
use std:: fs:: File ;
3
- use std:: io:: BufReader ;
3
+ use std:: io:: { BufReader , Read } ;
4
4
use tiny_skia:: Pixmap ;
5
5
6
6
/// Load resources into a series of `tiny_skia::Pixmap`, so we can use them to build the animations.
7
7
pub struct DivoomAnimationResourceLoader { }
8
8
9
9
impl DivoomAnimationResourceLoader {
10
- /// Load from local png file
11
- pub fn png ( file_path : & str ) -> DivoomAPIResult < Pixmap > {
10
+ /// Load png resource from local file
11
+ pub fn png_file ( file_path : & str ) -> DivoomAPIResult < Pixmap > {
12
12
let frame = Pixmap :: load_png ( file_path) ?;
13
13
Ok ( frame)
14
14
}
15
15
16
- /// Load from jpeg file
16
+ /// Load png resource from a memory buffer
17
+ pub fn png_buf ( buf : & [ u8 ] ) -> DivoomAPIResult < Pixmap > {
18
+ let frame = Pixmap :: decode_png ( buf) ?;
19
+ Ok ( frame)
20
+ }
21
+
22
+ /// Load png resource from Read trait
23
+ pub fn png < R : Read > ( reader : R ) -> DivoomAPIResult < Pixmap > {
24
+ let mut buffer = Vec :: new ( ) ;
25
+ let mut buf_reader = BufReader :: new ( reader) ;
26
+ buf_reader. read_to_end ( & mut buffer) ?;
27
+ DivoomAnimationResourceLoader :: png_buf ( & buffer)
28
+ }
29
+
30
+ /// Load jpeg resource from local file
17
31
#[ cfg( feature = "resource-loader-jpeg" ) ]
18
- pub fn jpeg ( file_path : & str ) -> DivoomAPIResult < Pixmap > {
32
+ pub fn jpeg_file ( file_path : & str ) -> DivoomAPIResult < Pixmap > {
19
33
let file = File :: open ( file_path) ?;
20
- let mut decoder = jpeg_decoder:: Decoder :: new ( BufReader :: new ( file) ) ;
34
+ DivoomAnimationResourceLoader :: jpeg ( file)
35
+ }
36
+
37
+ /// Load jpeg resource from Read trait
38
+ #[ cfg( feature = "resource-loader-jpeg" ) ]
39
+ pub fn jpeg < R : Read > ( reader : R ) -> DivoomAPIResult < Pixmap > {
40
+ let mut decoder = jpeg_decoder:: Decoder :: new ( BufReader :: new ( reader) ) ;
21
41
let pixels = decoder. decode ( ) ?;
22
42
23
43
let metadata = decoder. info ( ) . unwrap ( ) ;
@@ -80,16 +100,22 @@ impl DivoomAnimationResourceLoader {
80
100
Ok ( frame)
81
101
}
82
102
83
- /// Load from local gif file
103
+ /// Load gif resource from local file
84
104
#[ cfg( feature = "resource-loader-gif" ) ]
85
- pub fn gif ( file_path : & str ) -> DivoomAPIResult < Vec < Pixmap > > {
86
- let mut frames = vec ! [ ] ;
105
+ pub fn gif_file ( file_path : & str ) -> DivoomAPIResult < Vec < Pixmap > > {
87
106
let input = File :: open ( file_path) ?;
107
+ DivoomAnimationResourceLoader :: gif ( input)
108
+ }
109
+
110
+ /// Load gif resource from Read trait
111
+ #[ cfg( feature = "resource-loader-gif" ) ]
112
+ pub fn gif < R : Read > ( reader : R ) -> DivoomAPIResult < Vec < Pixmap > > {
113
+ let mut frames = vec ! [ ] ;
88
114
89
115
let mut options = gif:: DecodeOptions :: new ( ) ;
90
116
options. set_color_output ( gif:: ColorOutput :: RGBA ) ;
91
117
92
- let mut decoder = options. read_info ( input ) ?;
118
+ let mut decoder = options. read_info ( reader ) ?;
93
119
while let Some ( frame) = decoder. read_next_frame ( ) ? {
94
120
let mut frame_pixmap = Pixmap :: new ( frame. width as u32 , frame. height as u32 ) . unwrap ( ) ;
95
121
assert_eq ! ( frame_pixmap. data( ) . len( ) , frame. buffer. len( ) ) ;
@@ -128,7 +154,7 @@ mod tests {
128
154
#[ test]
129
155
fn divoom_resource_loader_can_load_png_file ( ) {
130
156
let frame =
131
- DivoomAnimationResourceLoader :: png ( "test_data/animation_builder_tests/logo.png" )
157
+ DivoomAnimationResourceLoader :: png_file ( "test_data/animation_builder_tests/logo.png" )
132
158
. unwrap ( ) ;
133
159
134
160
let non_zero_bits_count = frame. data ( ) . as_ref ( ) . iter ( ) . filter ( |x| * * x != 0u8 ) . count ( ) ;
@@ -137,7 +163,7 @@ mod tests {
137
163
138
164
#[ test]
139
165
fn divoom_resource_loader_can_load_jpeg_grayscale_file ( ) {
140
- let frame = DivoomAnimationResourceLoader :: jpeg (
166
+ let frame = DivoomAnimationResourceLoader :: jpeg_file (
141
167
"test_data/animation_builder_tests/logo_grayscale.jpg" ,
142
168
)
143
169
. unwrap ( ) ;
@@ -148,19 +174,21 @@ mod tests {
148
174
149
175
#[ test]
150
176
fn divoom_resource_loader_can_load_jpeg_rgb_file ( ) {
151
- let frame =
152
- DivoomAnimationResourceLoader :: jpeg ( "test_data/animation_builder_tests/logo_rgb.jpg" )
153
- . unwrap ( ) ;
177
+ let frame = DivoomAnimationResourceLoader :: jpeg_file (
178
+ "test_data/animation_builder_tests/logo_rgb.jpg" ,
179
+ )
180
+ . unwrap ( ) ;
154
181
155
182
let non_zero_bits_count = frame. data ( ) . as_ref ( ) . iter ( ) . filter ( |x| * * x != 0u8 ) . count ( ) ;
156
183
assert_ne ! ( non_zero_bits_count, 0 ) ;
157
184
}
158
185
159
186
#[ test]
160
187
fn divoom_resource_loader_can_load_jpeg_cmyk_file ( ) {
161
- let frame =
162
- DivoomAnimationResourceLoader :: jpeg ( "test_data/animation_builder_tests/logo_cmyk.jpg" )
163
- . unwrap ( ) ;
188
+ let frame = DivoomAnimationResourceLoader :: jpeg_file (
189
+ "test_data/animation_builder_tests/logo_cmyk.jpg" ,
190
+ )
191
+ . unwrap ( ) ;
164
192
165
193
let non_zero_bits_count = frame. data ( ) . as_ref ( ) . iter ( ) . filter ( |x| * * x != 0u8 ) . count ( ) ;
166
194
assert_ne ! ( non_zero_bits_count, 0 ) ;
@@ -169,7 +197,7 @@ mod tests {
169
197
#[ test]
170
198
fn divoom_resource_loader_can_load_gif_file ( ) {
171
199
let frames =
172
- DivoomAnimationResourceLoader :: gif ( "test_data/animation_builder_tests/logo.gif" )
200
+ DivoomAnimationResourceLoader :: gif_file ( "test_data/animation_builder_tests/logo.gif" )
173
201
. unwrap ( ) ;
174
202
assert_eq ! ( frames. len( ) , 1 ) ;
175
203
0 commit comments