-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnpd2png.go
81 lines (68 loc) · 1.29 KB
/
npd2png.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
package main
import (
"image"
"image/color"
"image/png"
"io"
"log"
"os"
"path/filepath"
)
const (
width = 320
height = 190
)
func replaceExt(path string, ext string) string {
return path[0:len(path)-len(filepath.Ext(path))] + ext
}
func convert(inputFile string, outputFile string) error {
fileIn, err := os.Open(inputFile)
if err != nil {
return err
}
defer fileIn.Close()
fileIn.Seek(14, 0)
img := image.NewRGBA(image.Rect(0, 0, width, height))
buffer := make([]byte, width*3)
for row := 0; row < height; row++ {
n, err := io.ReadFull(fileIn, buffer)
if err != nil {
return err
}
if n < len(buffer) {
break
}
col := 0
for i := 0; i < len(buffer); i += 3 {
c := color.RGBA{buffer[i], buffer[i+1], buffer[i+2], 255}
img.Set(col, row, c)
col++
}
}
fileOut, err := os.Create(outputFile)
if err != nil {
return err
}
defer fileOut.Close()
err = png.Encode(fileOut, img)
if err != nil {
return err
}
return nil
}
func main() {
files, err := filepath.Glob("*.NPD")
if err != nil {
log.Fatal(err)
}
for _, file := range files {
outFile := replaceExt(file, ".png")
log.Printf("Convert %s to %s", file, outFile)
err := convert(file, outFile)
if err != nil {
log.Print("Error: ", err.Error())
} else {
os.Remove(file)
}
}
}