-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort-pixels.go
144 lines (116 loc) · 3.75 KB
/
sort-pixels.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
package main
import (
"fmt"
"github.com/longears/sortpixels/myimage"
"github.com/longears/sortpixels/utils"
"os"
"runtime"
"runtime/debug"
"strings"
)
// How many times to repeat the vertical & horizontal sort step
const N_SORTS = 6
// How many threads to run in parallel
var THREADPOOL_SIZE int
func init() {
THREADPOOL_SIZE = runtime.NumCPU()
runtime.GOMAXPROCS(runtime.NumCPU())
}
//================================================================================
// IMAGE MODIFICATION ALGORITHMS
// Read the image from the path inFn,
// sort the pixels,
// and save the result to the path outFn.
// Return an error if the input file is not decodable as an image.
func sortPixelsValue(inFn, outFn string) {
myImage := myimage.MakeMyImageFromPath(inFn)
fmt.Println(" scrambling")
myImage.SortColumns("random", THREADPOOL_SIZE)
myImage.SortRows("random", THREADPOOL_SIZE)
fmt.Println(" sorting using value")
for ii := 0; ii < N_SORTS; ii++ {
myImage.SortColumns("v", THREADPOOL_SIZE)
myImage.SortRows("h2", THREADPOOL_SIZE)
}
myImage.SortColumns("v", THREADPOOL_SIZE)
myImage.SaveAs(outFn)
}
func sortPixelsSaturationValue(inFn, outFn string) {
myImage := myimage.MakeMyImageFromPath(inFn)
fmt.Println(" scrambling")
myImage.SortColumns("random", THREADPOOL_SIZE)
myImage.SortRows("random", THREADPOOL_SIZE)
fmt.Println(" sorting using saturation and value")
for ii := 0; ii < N_SORTS; ii++ {
myImage.SortColumns("sv", THREADPOOL_SIZE)
myImage.SortRows("h2", THREADPOOL_SIZE)
}
myImage.SortColumns("sv", THREADPOOL_SIZE)
myImage.SaveAs(outFn)
}
func congregatePixels(inFn, outFn string) {
myImage := myimage.MakeMyImageFromPath(inFn)
fmt.Println(" resizing")
myImage = myImage.ThumbnailByPixels(512)
fmt.Println(" scrambling")
myImage.SortColumns("random", THREADPOOL_SIZE)
myImage.SortRows("random", THREADPOOL_SIZE)
fmt.Println(" congregating (large scale)")
myImage.Congregate(0, 55) // maxMoveDist, percent of image visited per iteration
fmt.Println(" congregating (small scale)")
myImage.Congregate(8, 75) // maxMoveDist, percent of image visited per iteration
myImage.SaveAs(outFn)
}
func transformImage(inFn, tag string, transformFunction func(string, string)) {
// build outFn from inFn
outFn := inFn
if strings.Contains(outFn, ".") {
dotii := strings.LastIndex(outFn, ".")
outFn = outFn[:dotii] + "." + tag + ".png"
} else {
outFn += "." + tag
}
if strings.Contains(outFn, "/") {
outFn = outFn[strings.LastIndex(outFn, "/")+1:]
}
outFn = "output/" + outFn
// read, sort, and save (unless file has already been sorted)
fmt.Println(inFn)
if utils.PathExists(outFn) {
fmt.Println(" SKIPPING: already exists")
} else {
transformFunction(inFn, outFn)
}
// attempt to give memory back to the OS
debug.FreeOSMemory()
fmt.Println()
}
//================================================================================
// MAIN
func main() {
fmt.Println("------------------------------------------------------------\\")
defer fmt.Println("------------------------------------------------------------/")
// handle command line
if len(os.Args) < 2 {
fmt.Println()
fmt.Println(" usage: sort input.png [input2.jpg input3.png ...]")
fmt.Println()
fmt.Println(" Sort the pixels in the image(s) and save to the ./output/ folder.")
fmt.Println()
return
}
// make output directory if needed
if !utils.PathExists("output") {
err := os.Mkdir("output", 0755)
if err != nil {
panic(fmt.Sprintf("%v", err))
}
}
// open, sort, and save input images
for inputII := 1; inputII < len(os.Args); inputII++ {
inFn := os.Args[inputII]
// transformImage(inFn, "sorted_v", sortPixelsValue)
// transformImage(inFn, "sorted_sv", sortPixelsSaturationValue)
transformImage(inFn, "congregated", congregatePixels)
}
}