This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
73 lines (67 loc) · 1.6 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
package main
import (
"flag"
"fmt"
"image"
"image/draw"
"log"
"os"
)
func main() {
var method string
var outputFormat string
var outputName string
var outputQuality int
var threshold int
flag.StringVar(&method, "m", "o4", "Dithering method (o4, o9, t, r)")
flag.StringVar(&outputFormat, "f", "jpg", "Output file format (jpg, png)")
flag.StringVar(&outputName, "o", "output", "Output file name")
flag.IntVar(&outputQuality, "q", 100, "Output image quality (1-100)")
flag.IntVar(&threshold, "t", 0, "Threshold value for threshold dithering")
flag.Parse()
file, _ := os.Open(flag.Arg(0))
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
log.Fatal(err)
}
bounds := img.Bounds()
grayImg := image.NewGray(bounds)
draw.Draw(grayImg, bounds, img, bounds.Min, draw.Src)
w, h := bounds.Max.X, bounds.Max.Y
d := &Dither{
sourceImage: grayImg,
width: w,
height: h,
newImage: image.NewRGBA(bounds),
outputFormat: outputFormat,
outputName: outputName,
outputQuality: outputQuality,
threshold: threshold,
}
switch method {
case "o4":
d.OrderedDither4()
case "o9":
d.OrderedDither9()
case "t":
d.ThresholdDither()
case "r":
d.RandomDither()
case "a":
d.outputName = fmt.Sprintf("%s_o4", outputName)
d.OrderedDither4()
d.SaveFile()
d.outputName = fmt.Sprintf("%s_o9", outputName)
d.OrderedDither9()
d.SaveFile()
d.outputName = fmt.Sprintf("%s_t", outputName)
d.ThresholdDither()
d.SaveFile()
d.outputName = fmt.Sprintf("%s_r", outputName)
d.RandomDither()
d.SaveFile()
default:
log.Fatal("Unknown mode flag")
}
}