@@ -9,8 +9,13 @@ pub mod interpreter;
99pub mod stdin;
1010
1111use std:: error:: Error ;
12+ use std:: fs;
13+ use std:: io:: { BufReader , Cursor } ;
1214
15+ use base64:: Engine ;
16+ use base64:: prelude:: { BASE64_STANDARD , BASE64_STANDARD_NO_PAD } ;
1317use codel:: Codel ;
18+ use itertools:: Itertools ;
1419use rustc_hash:: FxHashSet ;
1520
1621use crate :: args:: Args ;
@@ -35,7 +40,21 @@ pub fn run(args: &Args) -> Result<(), Box<dyn Error>> {
3540 } else {
3641 None
3742 } ;
38- let img = Image :: new ( & args. image_file , args. codel_size , default_color) ?;
43+
44+ let image_content: Vec < u8 > = if args. base64 {
45+ let base64_encoded_image = args. image_file . split_whitespace ( ) . join ( "" ) ; //`split_whitespace()` removes the wrapping newlines of GNU `base64` command.
46+ if base64_encoded_image. len ( ) . is_multiple_of ( 4 ) {
47+ BASE64_STANDARD . decode ( & base64_encoded_image) ?
48+ } else {
49+ BASE64_STANDARD_NO_PAD . decode ( & base64_encoded_image) ?
50+ }
51+ } else {
52+ fs:: read ( & args. image_file ) ?
53+ } ;
54+
55+ let reader = BufReader :: new ( Cursor :: new ( image_content) ) ;
56+
57+ let img = Image :: new ( reader, args. codel_size , default_color) ?;
3958 debug_print ( args. verbose , & format ! ( "{}" , img) ) ;
4059
4160 if img. get_codel_at ( ( 0 , 0 ) ) . is_black ( ) {
@@ -167,6 +186,7 @@ mod tests {
167186 fall_back_to_white : true ,
168187 fall_back_to_black : false ,
169188 max_iter : None ,
189+ base64 : false ,
170190 verbose : false ,
171191 } ;
172192 assert ! ( run( & args) . is_ok( ) ) ;
0 commit comments