-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebarfer.go
160 lines (139 loc) · 3.14 KB
/
debarfer.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"bufio"
"fmt"
"image"
"image/color"
"image/gif"
"os"
)
func pixel_is_transparent(img *image.Paletted, x int, y int) bool {
_, _, _, a := img.At(x, y).RGBA()
return a == 0
}
func pixel_is_light(img *image.Paletted, x int, y int) bool {
r, g, b, _ := img.At(x, y).RGBA()
sum := r + g + b
return (sum > 0x18000) && (sum < 0x2e000)
}
func should_turn_pixel_transparent(img *image.Paletted, x int, y int) bool {
xmin := img.Rect.Min.X
ymin := img.Rect.Min.Y
xmax := img.Rect.Max.X - 1
ymax := img.Rect.Max.Y - 1
i_am_light := pixel_is_light(img, x, y)
// A light pixel between a transparent pixel and a dark pixel -> transparent
if (y > ymin) && (y < ymax) && i_am_light {
if pixel_is_transparent(img, x, y-1) && !pixel_is_light(img, x, y+1) {
return true
}
if pixel_is_transparent(img, x, y+1) && !pixel_is_light(img, x, y-1) {
return true
}
}
if (x > xmin) && (x < xmax) && i_am_light {
if pixel_is_transparent(img, x-1, y) && !pixel_is_light(img, x+1, y) {
return true
}
if pixel_is_transparent(img, x+1, y) && !pixel_is_light(img, x-1, y) {
return true
}
}
// A light pixel between a dark pixel and the edge -> transparent
if i_am_light {
if y == ymin && !pixel_is_light(img, x, y+1) {
return true
}
if y == ymax && !pixel_is_light(img, x, y-1) {
return true
}
if x == xmin && !pixel_is_light(img, x+1, y) {
return true
}
if x == xmax && !pixel_is_light(img, x-1, y) {
return true
}
}
return false
}
func debarf_frame(frame *image.Paletted) error {
xmin := frame.Rect.Min.X
ymin := frame.Rect.Min.Y
xmax := frame.Rect.Max.X
ymax := frame.Rect.Max.Y
fmt.Printf("(%d, %d), (%d, %d)\n", xmin, xmax, ymin, ymax)
for y := ymin; y < ymax; y++ {
for x := xmin; x < xmax; x++ {
c := frame.At(x, y)
r, g, b, a := c.RGBA()
if pixel_is_transparent(frame, x, y) {
fmt.Printf("...")
} else if should_turn_pixel_transparent(frame, x, y) {
fmt.Printf("xxx")
frame.Set(x, y, color.RGBA{0, 0, 0, 0})
} else {
r /= 0x1000
g /= 0x1000
b /= 0x1000
if a != 0xffff {
fmt.Printf("barf")
}
fmt.Printf("%x%x%x", r, g, b)
}
}
fmt.Printf("\n")
}
return nil
}
func debarf_image(img *gif.GIF) error {
fmt.Printf("\n")
for i := 0; i < len(img.Image); i++ {
err := debarf_frame(img.Image[i])
if err != nil {
return err
}
}
return nil
}
func process_file(filename string) error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
reader := bufio.NewReader(f)
g, err := gif.DecodeAll(reader)
if err != nil {
return err
}
err = debarf_image(g)
of, err := os.Create("out.gif")
if err != nil {
return err
}
defer of.Close()
writer := bufio.NewWriter(of)
err = gif.EncodeAll(writer, g)
if err != nil {
return err
}
return err
}
func usage() {
fmt.Printf("Usage: debarfer <inputfile0> [<inputfile1> ...]\n")
}
func main() {
if len(os.Args) < 2 {
usage()
return
}
for i := 1; i < len(os.Args); i++ {
fmt.Printf("Processing file %s...", os.Args[i])
err := process_file(os.Args[i])
if err == nil {
fmt.Printf("Success!\n")
} else {
fmt.Printf("%s\n", err.Error())
}
}
}