-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
98 lines (91 loc) · 2.21 KB
/
main.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"flag"
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
"path/filepath"
"strings"
"wechat-dat2picture/asset"
"wechat-dat2picture/imgtype"
)
func init() {
logrus.SetLevel(logrus.DebugLevel)
}
func main() {
var datPath string
var picPath string
flag.StringVar(&datPath, "datPath", "./dat", "待转换的dat文件目录")
flag.StringVar(&picPath, "picPath", "./pic", "转换后的存放目录")
flag.Parse()
logrus.Info("datPath = ", datPath)
logrus.Info("picPath = ", picPath)
err := os.MkdirAll(picPath, 0775)
if err != nil {
logrus.Error("mkdir for picPath error: ", err)
return
}
dat2Picture(datPath, picPath)
}
func dat2Picture(datPath string, picPath string) {
bytesSampleDat, err := asset.Asset("sample/color.dat")
if err != nil {
logrus.Error("readFile for sampleDat error: ", err)
return
}
bytesSampleJpg, err := asset.Asset("sample/color.jpg")
if err != nil {
logrus.Error("readFile for sampleJpg error: ", err)
return
}
var rule [256]byte
var mapped [256]bool
var mappedCount int32
for i, v := range bytesSampleDat {
if mapped[int(v)] == false {
rule[int(v)] = bytesSampleJpg[i]
mapped[int(v)] = true
mappedCount++
}
if mappedCount == 256 {
break
}
}
files, err := ioutil.ReadDir(datPath)
if err != nil {
logrus.Error("ReadDir for datPath error: ", err)
return
}
for _, f := range files {
if f.IsDir() {
logrus.Info("ignore directory: ", f.Name())
continue
}
if filepath.Ext(strings.TrimSpace(f.Name())) != ".dat" {
logrus.Info("ignore not .dat file: ", f.Name())
continue
}
datFilename := filepath.Join(datPath, f.Name())
bytesDat, err := ioutil.ReadFile(datFilename)
if err != nil {
logrus.Warnf("readFile: %s error: %v", datFilename, err)
continue
}
datHeader := [3]byte{bytesDat[0], bytesDat[1], bytesDat[2]}
imgType, err := imgtype.Get(datHeader)
if err != nil {
logrus.Error("getImgType error: ", err)
continue
}
var target []byte
for _, bDat := range bytesDat {
target = append(target, rule[bDat])
}
picFilename := filepath.Join(picPath, f.Name()+"."+imgType)
err = ioutil.WriteFile(picFilename, target, 0644)
if err != nil {
logrus.Error("WriteFile error: ", err)
continue
}
}
}