66 "image"
77 _ "image/gif"
88 _ "image/jpeg"
9- _ "image/png"
9+ "image/png"
1010 "log"
1111 "os"
1212 "strings"
@@ -59,20 +59,24 @@ func handleZebraCommands(cmd, ip, port string) bool {
5959 return false
6060}
6161
62- func parseFlags () (string , string , string , string , string , string , float64 ) {
63- var filename , zebraCmd , graphicType , imageEdit , ip , port string
62+ func parseFlags () (string , string , string , string , string , string , string , float64 , bool , bool ) {
63+ var filename , zebraCmd , graphicType , imageEdit , ip , port , output string
6464 var resizeFactor float64
65+ var lines , decode bool
6566
6667 flag .StringVar (& filename , "file" , "" , "filename to convert to zpl" )
6768 flag .StringVar (& zebraCmd , "cmd" , "" , "send special command to printer [cancel,calib,feed,info,config,diag]" )
6869 flag .StringVar (& graphicType , "type" , "CompressedASCII" , "type of graphic field encoding [ASCII,Binary,CompressedASCII,Z64]" )
6970 flag .StringVar (& imageEdit , "edit" , "" , "manipulate the image [invert,monochrome]" )
7071 flag .StringVar (& ip , "ip" , "" , "send zpl to printer" )
7172 flag .StringVar (& port , "port" , "9100" , "network port of printer" )
73+ flag .StringVar (& output , "out" , "" , "output filename for decoded PNG" )
7274 flag .Float64Var (& resizeFactor , "resize" , 1.0 , "zoom/resize the image" )
75+ flag .BoolVar (& lines , "lines" , false , "output black pixel runs as ZPL line commands instead of a graphic field" )
76+ flag .BoolVar (& decode , "decode" , false , "convert a ZPL file containing a ^GF field to PNG" )
7377
7478 flag .Parse ()
75- return filename , zebraCmd , graphicType , imageEdit , ip , port , resizeFactor
79+ return filename , zebraCmd , graphicType , imageEdit , ip , port , output , resizeFactor , lines , decode
7680}
7781
7882func openImageFile (filename string ) (image.Image , image.Config , error ) {
@@ -135,8 +139,28 @@ func getGraphicType(typeFlag string) zplgfa.GraphicType {
135139 }
136140}
137141
142+ func decodeZPLFile (filename , output string ) error {
143+ data , err := os .ReadFile (filename )
144+ if err != nil {
145+ return fmt .Errorf ("could not read the file \" %s\" : %s" , filename , err )
146+ }
147+ img , err := zplgfa .ConvertZPLToImage (string (data ))
148+ if err != nil {
149+ return err
150+ }
151+ if output == "" {
152+ return png .Encode (os .Stdout , img )
153+ }
154+ file , err := os .Create (output )
155+ if err != nil {
156+ return fmt .Errorf ("could not create the file \" %s\" : %s" , output , err )
157+ }
158+ defer file .Close ()
159+ return png .Encode (file , img )
160+ }
161+
138162func main () {
139- filename , zebraCmd , graphicTypeFlag , imageEdit , ip , port , resizeFactor := parseFlags ()
163+ filename , zebraCmd , graphicTypeFlag , imageEdit , ip , port , output , resizeFactor , lines , decode := parseFlags ()
140164
141165 if handleZebraCommands (zebraCmd , ip , port ) && filename == "" {
142166 return
@@ -147,6 +171,13 @@ func main() {
147171 return
148172 }
149173
174+ if decode {
175+ if err := decodeZPLFile (filename , output ); err != nil {
176+ log .Printf ("Warning: %s\n " , err )
177+ }
178+ return
179+ }
180+
150181 img , config , err := openImageFile (filename )
151182 if err != nil {
152183 log .Printf ("Warning: %s\n " , err )
@@ -157,6 +188,9 @@ func main() {
157188
158189 flat := zplgfa .FlattenImage (img )
159190 gfimg := zplgfa .ConvertToZPL (flat , getGraphicType (graphicTypeFlag ))
191+ if lines {
192+ gfimg = zplgfa .ConvertToZPLLines (flat )
193+ }
160194
161195 if ip != "" {
162196 sendDataToZebra (ip , port , gfimg )
0 commit comments