@@ -5,7 +5,8 @@ use rasterize::*;
55use  std:: { 
66    env, 
77    fs:: File , 
8-     io:: { BufWriter ,  Read } , 
8+     io:: { BufWriter ,  Read ,  Write } , 
9+     str:: FromStr , 
910    sync:: Arc , 
1011} ; 
1112use  tracing_subscriber:: { EnvFilter ,  fmt:: format:: FmtSpan } ; 
@@ -18,10 +19,42 @@ enum RasterizerType {
1819    SignedDifference , 
1920} 
2021
22+ #[ derive( Debug ,  Clone ,  Copy ) ]  
23+ enum  OutputFormat  { 
24+     Bmp , 
25+     Rgba , 
26+     Png , 
27+ } 
28+ 
29+ impl  OutputFormat  { 
30+     fn  write ( self ,  image :  & Layer < LinColor > ,  out :  impl  Write )  -> Result < ( ) ,  Error >  { 
31+         match  self  { 
32+             OutputFormat :: Bmp  => image. write_bmp ( out) ?, 
33+             OutputFormat :: Png  => image. write_png ( out) ?, 
34+             OutputFormat :: Rgba  => image. write_rgba ( out) ?, 
35+         } 
36+         Ok ( ( ) ) 
37+     } 
38+ } 
39+ 
40+ impl  FromStr  for  OutputFormat  { 
41+     type  Err  = Error ; 
42+ 
43+     fn  from_str ( s :  & str )  -> Result < Self ,  Self :: Err >  { 
44+         match  s { 
45+             "bmp"  => Ok ( OutputFormat :: Bmp ) , 
46+             "png"  => Ok ( OutputFormat :: Png ) , 
47+             "rgba"  => Ok ( OutputFormat :: Rgba ) , 
48+             _ => Err ( format ! ( "Invalid output format: {s}" ) . into ( ) ) , 
49+         } 
50+     } 
51+ } 
52+ 
2153#[ derive( Debug ) ]  
2254struct  Args  { 
2355    input_file :  String , 
2456    output_file :  String , 
57+     output_format :  OutputFormat , 
2558    outline :  bool , 
2659    width :  Option < usize > , 
2760    stroke :  Option < Scalar > , 
@@ -46,6 +79,7 @@ impl Args {
4679        let  mut  result = Args  { 
4780            input_file :  String :: new ( ) , 
4881            output_file :  String :: new ( ) , 
82+             output_format :  OutputFormat :: Bmp , 
4983            outline :  false , 
5084            width :  None , 
5185            stroke :  None , 
@@ -83,6 +117,10 @@ impl Args {
83117                "-o"  => { 
84118                    result. outline  = true ; 
85119                } 
120+                 "-of"  => { 
121+                     let  format = args. next ( ) . ok_or ( "-of requries argument" ) ?. parse ( ) ?; 
122+                     result. output_format  = format; 
123+                 } 
86124                "-a"  => { 
87125                    result. rasterizer  = RasterizerType :: ActiveEdge ; 
88126                } 
@@ -123,7 +161,7 @@ impl Args {
123161            ) ; 
124162            eprintln ! ( "\n USAGE:" ) ; 
125163            eprintln ! ( 
126-                 "    {} [-w <width>] [-b <bbox>] [-s <stroke>] [-f <flatness>] [-o] [-a] [-fg <color>] [-bg <color>] <file.path> <out.bmp >" , 
164+                 "    {} [-w <width>] [-b <bbox>] [-s <stroke>] [-f <flatness>] [-o] [-of <format>] [- a] [-fg <color>] [-bg <color>] <file.path> <output_file >" , 
127165                cmd
128166            ) ; 
129167            eprintln ! ( "\n ARGS:" ) ; 
@@ -132,6 +170,7 @@ impl Args {
132170            eprintln ! ( "    -t <transform>     apply transform" ) ; 
133171            eprintln ! ( "    -s <stroke_width>  stroke path before rendering" ) ; 
134172            eprintln ! ( "    -o                 show outline with control points instead of filling" ) ; 
173+             eprintln ! ( "    -of <format>       output file format (bmp, png, rgba)" ) ; 
135174            eprintln ! ( 
136175                "    -a                 use active-edge instead of signed-difference rasterizer" 
137176            ) ; 
@@ -315,9 +354,9 @@ fn main() -> Result<(), Error> {
315354        let  _ = save. enter ( ) ; 
316355        if  args. output_file  != "-"  { 
317356            let  mut  image_file = BufWriter :: new ( File :: create ( args. output_file ) ?) ; 
318-             image . write_bmp ( & mut  image_file) ?; 
357+             args . output_format . write ( & image ,   & mut  image_file) ?; 
319358        }  else  { 
320-             image . write_bmp ( std:: io:: stdout ( ) ) ?; 
359+             args . output_format . write ( & image ,   std:: io:: stdout ( ) ) ?; 
321360        } 
322361    } 
323362
0 commit comments