@@ -5,8 +5,7 @@ use rasterize::*;
55use  std:: { 
66    env, 
77    fs:: File , 
8-     io:: { BufReader ,  BufWriter ,  Read ,  Write } , 
9-     str:: FromStr , 
8+     io:: { BufReader ,  BufWriter ,  Read } , 
109    sync:: Arc , 
1110} ; 
1211use  tracing_subscriber:: { EnvFilter ,  fmt:: format:: FmtSpan } ; 
@@ -19,47 +18,11 @@ enum RasterizerType {
1918    SignedDifference , 
2019} 
2120
22- #[ derive( Debug ,  Clone ,  Copy ) ]  
23- enum  OutputFormat  { 
24-     Bmp , 
25-     Rgba , 
26-     #[ cfg( feature = "png" ) ]  
27-     Png , 
28- } 
29- 
30- impl  OutputFormat  { 
31-     fn  write ( self ,  image :  & Layer < LinColor > ,  out :  impl  Write )  -> Result < ( ) ,  Error >  { 
32-         match  self  { 
33-             OutputFormat :: Bmp  => image. write_bmp ( out) ?, 
34-             #[ cfg( feature = "png" ) ]  
35-             OutputFormat :: Png  => image. write_png ( out) ?, 
36-             OutputFormat :: Rgba  => image. write_rgba ( out) ?, 
37-         } 
38-         Ok ( ( ) ) 
39-     } 
40- } 
41- 
42- impl  FromStr  for  OutputFormat  { 
43-     type  Err  = Error ; 
44- 
45-     fn  from_str ( s :  & str )  -> Result < Self ,  Self :: Err >  { 
46-         match  s { 
47-             "bmp"  => Ok ( OutputFormat :: Bmp ) , 
48-             "rgba"  => Ok ( OutputFormat :: Rgba ) , 
49-             #[ cfg( feature = "png" ) ]  
50-             "png"  => Ok ( OutputFormat :: Png ) , 
51-             #[ cfg( not( feature = "png" ) ) ]  
52-             "png"  => Err ( "png feature is disabled" . into ( ) ) , 
53-             _ => Err ( format ! ( "Invalid output format: {s}" ) . into ( ) ) , 
54-         } 
55-     } 
56- } 
57- 
5821#[ derive( Debug ) ]  
5922struct  Args  { 
6023    input_file :  String , 
6124    output_file :  String , 
62-     output_format :  OutputFormat , 
25+     output_format :  ImageWriteFormat , 
6326    outline :  bool , 
6427    size :  Size , 
6528    stroke :  Option < Scalar > , 
@@ -84,7 +47,7 @@ impl Args {
8447        let  mut  result = Args  { 
8548            input_file :  String :: new ( ) , 
8649            output_file :  String :: new ( ) , 
87-             output_format :  OutputFormat :: Bmp , 
50+             output_format :  ImageWriteFormat :: Bmp , 
8851            outline :  false , 
8952            size :  Size  { 
9053                height :  0 , 
@@ -109,8 +72,8 @@ impl Args {
10972                "-w"  => { 
11073                    result. size . width  = args. next ( ) . ok_or ( "-w requires argument" ) ?. parse ( ) ?; 
11174                } 
112-                 "-b "  => { 
113-                     let  view_box = args. next ( ) . ok_or ( "-b  requires argument" ) ?; 
75+                 "-v "  => { 
76+                     let  view_box = args. next ( ) . ok_or ( "-v  requires argument" ) ?; 
11477                    result. view_box . replace ( view_box. parse ( ) ?) ; 
11578                } 
11679                "-t"  => { 
@@ -121,21 +84,8 @@ impl Args {
12184                    let  stroke = args. next ( ) . ok_or ( "-s requres argument" ) ?; 
12285                    result. stroke . replace ( stroke. parse ( ) ?) ; 
12386                } 
124-                 "-o"  => { 
125-                     result. outline  = true ; 
126-                 } 
127-                 "-of"  => { 
128-                     result. output_format  = args. next ( ) . ok_or ( "-of requries argument" ) ?. parse ( ) ?; 
129-                 } 
130-                 "-a"  => { 
131-                     result. rasterizer  = RasterizerType :: ActiveEdge ; 
132-                 } 
13387                "-f"  => { 
134-                     let  flatness:  Scalar  = args. next ( ) . ok_or ( "-f requres argument" ) ?. parse ( ) ?; 
135-                     if  flatness < EPSILON  { 
136-                         return  Err ( "flatness is too small" . into ( ) ) ; 
137-                     } 
138-                     result. flatness  = flatness; 
88+                     result. output_format  = args. next ( ) . ok_or ( "-of requries argument" ) ?. parse ( ) ?; 
13989                } 
14090                "-fg"  => { 
14191                    let  fg = args
@@ -151,6 +101,19 @@ impl Args {
151101                        . parse ( ) ?; 
152102                    result. bg . replace ( bg) ; 
153103                } 
104+                 "-ro"  => { 
105+                     result. outline  = true ; 
106+                 } 
107+                 "-ra"  => { 
108+                     result. rasterizer  = RasterizerType :: ActiveEdge ; 
109+                 } 
110+                 "-rf"  => { 
111+                     let  flatness:  Scalar  = args. next ( ) . ok_or ( "-rf requres argument" ) ?. parse ( ) ?; 
112+                     if  flatness < EPSILON  { 
113+                         return  Err ( "flatness is too small" . into ( ) ) ; 
114+                     } 
115+                     result. flatness  = flatness; 
116+                 } 
154117                _ => { 
155118                    positional += 1 ; 
156119                    match  positional { 
@@ -163,35 +126,38 @@ impl Args {
163126        } 
164127        if  positional < 2  { 
165128            eprintln ! ( 
166-                 "Very simple tool that accepts SVG path as an input and produces rasterized image" 
129+                 "Very simple tool that accepts SVG path as an input and produces rasterized image" , 
167130            ) ; 
168131            eprintln ! ( "\n USAGE:" ) ; 
169132            eprintln ! ( 
170-                 "    rasterize [-h <height>] [-w <width>] [-b <bbox >] [-t <transform>] [-s <stroke>]" , 
133+                 "    rasterize [-h <height>] [-w <width>] [-v <view_box >] [-t <transform>] [-s <stroke>]" , 
171134            ) ; 
172135            eprintln ! ( 
173-                 "              [-f <flatness >] [-o ] [-of  <format>] [-a] [-fg <color >] [-bg <color> ]" , 
136+                 "              [-fg <color >] [-bg <color> ] [-f  <format>] [-rf <flatness >] [-ro] [-ra ]" , 
174137            ) ; 
175138            eprintln ! ( "              <input_file> <output_file>" ) ; 
176139            eprintln ! ( "\n ARGS:" ) ; 
177140            eprintln ! ( "    -h <height>        height in pixels of the output image" ) ; 
178141            eprintln ! ( "    -w <width>         width in pixels of the output image" ) ; 
179-             eprintln ! ( "    -b  <view_box>      view box" ) ; 
142+             eprintln ! ( "    -v  <view_box>      view box" ) ; 
180143            eprintln ! ( "    -t <transform>     apply transform" ) ; 
181144            eprintln ! ( "    -s <stroke_width>  stroke path before rendering" ) ; 
182-             eprintln ! ( "    -o                 show outline with control points instead of filling" ) ; 
183-             eprintln ! ( "    -of <format>       output file format (bmp, png, rgba)" ) ; 
184-             eprintln ! ( 
185-                 "    -a                 use active-edge instead of signed-difference rasterizer" 
186-             ) ; 
145+             eprintln ! ( "    -f <format>        output file format (bmp, png, rgba)" ) ; 
187146            eprintln ! ( "    -fg <color>        foreground color" ) ; 
188147            eprintln ! ( "    -bg <color>        background color" ) ; 
189148            eprintln ! ( 
190-                 "    -f  <flatness>       flatness used by rasterizer (defualt: {})" , 
149+                 "    -rf  <flatness>     flatness used by rasterizer (defualt: {})" , 
191150                DEFAULT_FLATNESS 
192151            ) ; 
152+             eprintln ! ( 
153+                 "    -ro                rasterize outline with control points instead of filling" 
154+             ) ; 
155+             eprintln ! ( 
156+                 "    -ra                rasterize active-edge instead of signed-difference rasterizer" 
157+             ) ; 
193158            eprintln ! ( "    <input_file>       file containing SVG path ('-' means stdin)" ) ; 
194159            eprintln ! ( "    <output_file>      image rendered in the BMP format ('-' means stdout)" ) ; 
160+             eprintln ! ( "\n VERSION: {}" ,  env!( "CARGO_PKG_VERSION" ) ) ; 
195161            std:: process:: exit ( 1 ) ; 
196162        } 
197163        Ok ( result) 
@@ -350,9 +316,9 @@ fn main() -> Result<(), Error> {
350316        let  _ = save. enter ( ) ; 
351317        if  args. output_file  != "-"  { 
352318            let  mut  image_file = BufWriter :: new ( File :: create ( args. output_file ) ?) ; 
353-             args . output_format . write ( & image ,  & mut  image_file) ?; 
319+             image . write ( args . output_format ,  & mut  image_file) ?; 
354320        }  else  { 
355-             args . output_format . write ( & image ,  std:: io:: stdout ( ) ) ?; 
321+             image . write ( args . output_format ,  std:: io:: stdout ( ) ) ?; 
356322        } 
357323    } 
358324
0 commit comments