-
Notifications
You must be signed in to change notification settings - Fork 6
/
extract.go
52 lines (48 loc) · 955 Bytes
/
extract.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"errors"
"fmt"
"image/png"
"os"
"path/filepath"
"github.com/fiam/max7456tool/mcm"
"github.com/urfave/cli/v2"
)
func extractAction(ctx *cli.Context) error {
if ctx.NArg() != 2 {
return errors.New("extract requires 2 arguments, see help extract")
}
f, err := os.Open(ctx.Args().Get(0))
if err != nil {
return err
}
defer f.Close()
dec, err := mcm.NewDecoder(f)
if err != nil {
return err
}
dir := ctx.Args().Get(1)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
blanks := ctx.Bool("blanks")
for ii := 0; ii < dec.NChars(); ii++ {
ch := dec.CharAt(ii)
if !blanks && ch.IsBlank() {
continue
}
im := ch.Image(nil)
output := filepath.Join(dir, fmt.Sprintf("%03d", ii)+".png")
f, err := openOutputFile(output)
if err != nil {
return err
}
if err := png.Encode(f, im); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
}
return nil
}