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
|
/*
Copyright (C) 2019 Leo Tenenbaum
This file is part of AutoDistortion.
AutoDistortion is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
AutoDistortion is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with AutoDistortion. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"os"
"fmt"
"bufio"
"strings"
"flag"
"time"
"errors"
"image"
"image/png" // Image file types
"image/jpeg"
"math/rand"
"github.com/pommicket/autodistortion/autodistortion"
)
func distort(filenameIn string, filenameOut string, functionLen int, nThreads int) error {
reader, err := os.Open(filenameIn)
if err != nil { return err }
var outputFormat string
if strings.HasSuffix(filenameOut, ".jpg") || strings.HasSuffix(filenameOut, ".jpeg") {
outputFormat = "jpeg"
} else if strings.HasSuffix(filenameOut, ".png") {
outputFormat = "png"
} else {
return errors.New(fmt.Sprintf("Did not recognize format of: %v", filenameOut))
}
// Create it early so that if there's an error, we know sooner rather than
// later
outFile, err := os.Create(filenameOut)
if err != nil { return err }
defer outFile.Close()
image, _, err := image.Decode(reader)
reader.Close()
if err != nil { return err }
outImage := autodistortion.Distort(image, functionLen, nThreads)
if err != nil { return err }
switch outputFormat {
case "png":
err = png.Encode(outFile, outImage)
case "jpeg":
err = jpeg.Encode(outFile, outImage, nil)
}
return err
}
func main() {
seed := flag.Int64("seed", time.Now().UTC().UnixNano(), "The seed to use for the random number generator")
var filenameIn, filenameOut string
flag.StringVar(&filenameIn, "in", "user input", "Which file should be distorted")
flag.StringVar(&filenameOut, "out", "[in]_distorted.[extension]", "Which file to output to")
fLength := flag.Int("function-len", 40, "The length of the distortion functions")
threads := flag.Int("threads", 64, "Number of threads to use")
quiet := flag.Bool("quiet", false, "Output nothing")
flag.Parse()
rand.Seed(*seed)
if !*quiet {
fmt.Println("Using seed:", *seed)
}
reader := bufio.NewReader(os.Stdin)
if filenameIn == "user input" {
if !*quiet {
fmt.Print("What file would you like to distort? ")
}
text, err := reader.ReadString('\n')
if err != nil {
fmt.Println(err)
return
}
filenameIn = strings.TrimSpace(text) // Remove newline
}
if filenameOut == "[in]_distorted.[extension]" {
// Set filenameOut to foo.png -> foo_distorted.png
dotPos := strings.IndexByte(filenameIn, '.')
if dotPos == -1 {
fmt.Println("Error: That file doesn't have a file extension!")
return
}
filenameOut = filenameIn[:dotPos] + "_distorted" + filenameIn[dotPos:]
}
if !*quiet { fmt.Println("Loading...") }
err := distort(filenameIn, filenameOut, *fLength, *threads)
if err != nil {
if !*quiet { fmt.Println("Error:", err) }
return
}
if !*quiet { fmt.Println("Done! Output:", filenameOut) }
}
|