-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
ditherer.go
211 lines (180 loc) · 4.46 KB
/
ditherer.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package dither
import (
"flag"
"fmt"
"image"
"image/color"
_ "image/jpeg"
"image/png"
"log"
"os"
"time"
)
type file struct {
name string
}
// Command line flags
var (
outputDir string
colorType string
treshold bool
multiplier float64
cmd flag.FlagSet
)
// Open reads the source image
func (file *file) Open() (image.Image, error) {
f, err := os.Open(file.name)
if err != nil {
return nil, err
}
defer f.Close()
img, _, err := image.Decode(f)
return img, err
}
// Grayscale converts an image to grayscale mode
func (file *file) Grayscale(input image.Image) (*image.Gray, error) {
bounds := input.Bounds()
gray := image.NewGray(bounds)
for x := bounds.Min.X; x < bounds.Dx(); x++ {
for y := bounds.Min.Y; y < bounds.Dy(); y++ {
pixel := input.At(x, y)
gray.Set(x, y, pixel)
}
}
return gray, nil
}
// tresholdDithering creates a tresholded image
func (file *file) tresholdDithering(input *image.Gray) (*image.Gray, error) {
var (
bounds = input.Bounds()
dithered = image.NewGray(bounds)
dx = bounds.Dx()
dy = bounds.Dy()
)
for x := 0; x < dx; x++ {
for y := 0; y < dy; y++ {
pixel := input.GrayAt(x, y)
threshold := func(pixel color.Gray) color.Gray {
if pixel.Y > 123 {
return color.Gray{Y: 255}
}
return color.Gray{Y: 0}
}
dithered.Set(x, y, threshold(pixel))
}
}
output, err := os.Create(outputDir + "/treshold.png")
if err != nil {
return nil, err
}
defer output.Close()
err = png.Encode(output, dithered)
if err != nil {
log.Fatal(err)
}
return dithered, nil
}
// Process parses the command line inputs and calls the defined dithering method
func Process(ditherers []Dither) {
cmd = *flag.NewFlagSet("commands", flag.ExitOnError)
cmd.StringVar(&outputDir, "o", "", "Output folder")
cmd.StringVar(&colorType, "e", "all", "Generates & exports the color and greyscale mode halftone images. Options: 'all', 'color', 'mono'")
cmd.BoolVar(&treshold, "t", true, "Option to export the tresholded image")
cmd.Float64Var(&multiplier, "em", 1.18, "Error multiplier")
log.SetPrefix("dithergo: ")
log.SetFlags(0)
cmd.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s <image>\n", os.Args[0])
cmd.PrintDefaults()
}
if len(os.Args) == 1 {
fmt.Fprintf(os.Stderr, "Usage: %s <image>\n", os.Args[0])
os.Exit(0)
}
if os.Args[1] == "--help" || os.Args[1] == "-h" {
fmt.Fprintf(os.Stderr, "Usage: %s <image>\n", os.Args[0])
cmd.PrintDefaults()
os.Exit(0)
}
// Parse flags before to use them
cmd.Parse(os.Args[2:])
if len(cmd.Args()) > 0 {
cmd.Usage()
log.Printf("missing input file.")
os.Exit(0)
}
if len(outputDir) == 0 {
cmd.Usage()
log.Printf("missing output directory.")
os.Exit(0)
}
input := &file{name: string(os.Args[1])}
img, err := input.Open()
if err != nil {
log.Fatalf("Input error: %v", err)
}
// Channel to signal the completion event
done := make(chan struct{})
fmt.Print("Rendering image...")
now := time.Now()
progress(done)
// Run the ditherer method
func(input *file, done chan struct{}) {
if cmd.Parsed() {
if _, err := os.Stat(outputDir); os.IsNotExist(err) {
os.Mkdir(outputDir, os.ModePerm)
}
_ = os.Mkdir(outputDir+"/color", os.ModePerm)
_ = os.Mkdir(outputDir+"/mono", os.ModePerm)
if treshold {
gray, _ := input.Grayscale(img)
input.tresholdDithering(gray)
}
for _, ditherer := range ditherers {
dc := ditherer.Color(img, float32(multiplier))
dg := ditherer.Monochrome(img, float32(multiplier))
cex := outputDir + "/color/"
gex := outputDir + "/mono/"
switch colorType {
case "all":
generateOutput(ditherer, dc, cex)
generateOutput(ditherer, dg, gex)
case "color":
generateOutput(ditherer, dc, cex)
case "mono":
generateOutput(ditherer, dg, gex)
}
}
done <- struct{}{}
}
}(input, done)
since := time.Since(now)
fmt.Println("\nDone✓")
fmt.Printf("Rendered in: %.2fs\n", since.Seconds())
}
// generateOutput render the generated image
func generateOutput(dither Dither, img image.Image, exportDir string) {
output, err := os.Create(exportDir + dither.Type + ".png")
if err != nil {
log.Fatal(err)
}
defer output.Close()
err = png.Encode(output, img)
if err != nil {
log.Fatal(err)
}
}
// progress visualize the rendering progress
func progress(done chan struct{}) {
ticker := time.NewTicker(time.Millisecond * 200)
go func() {
for {
select {
case <-ticker.C:
fmt.Print(".")
case <-done:
ticker.Stop()
}
}
}()
}