-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
82 lines (71 loc) · 1.31 KB
/
main.go
File metadata and controls
82 lines (71 loc) · 1.31 KB
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
// Command w2t changes white pixels in images to be transparent
package main
import (
"flag"
"image"
"image/color"
_ "image/jpeg"
"image/png"
_ "image/png"
"log"
"os"
"sync"
)
var (
fInPlace bool
)
func init() {
flag.BoolVar(&fInPlace, "replace", false, "replace the images")
}
func main() {
flag.Parse()
queue := make(chan struct{}, 10)
wg := &sync.WaitGroup{}
for _, p := range flag.Args() {
wg.Add(1)
go do(queue, wg, p)
}
wg.Wait()
}
func do(queue chan struct{}, wg *sync.WaitGroup, p string) {
queue <- struct{}{}
defer wg.Done()
defer func() {
<-queue
}()
f, err := os.Open(p)
check(err)
defer f.Close()
img, _, err := image.Decode(f)
check(err)
f.Close()
bnd := img.Bounds()
// for now always save as colored and transparent png
out := image.NewNRGBA(bnd)
for x := 0; x < bnd.Max.X; x++ {
for y := 0; y < bnd.Max.Y; y++ {
col := img.At(x, y)
r, g, b, a := col.RGBA()
if true || a == 0xffff && r == g && r == b {
rr := uint16(r)
out.Set(x, y, color.NRGBA{0, 0, 0, 255 - uint8(rr>>8)})
} else {
out.Set(x, y, col)
}
}
}
if fInPlace {
f, err = os.Create(p)
check(err)
check(png.Encode(f, out))
} else {
f, err = os.Create(p + ".mod")
check(err)
check(png.Encode(f, out))
}
}
func check(err error) {
if err != nil {
log.Fatalln(err)
}
}