/
main.go
133 lines (115 loc) · 2.9 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
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
package main
import (
"bufio"
"fmt"
"image"
"image/color"
"image/draw"
_ "image/jpeg"
"image/png"
"log"
"math/rand"
"net/http"
"os"
"strconv"
)
func main() {
if len(os.Args) != 5 {
fmt.Println(len(os.Args))
fmt.Println("Generates a png. \n Need to call with: path of image\n path of new image\n probability of block \n size of square")
}
old := os.Args[1]
new := os.Args[2]
p, err := strconv.Atoi(os.Args[3])
if err != nil {
log.Fatal(err)
}
size, err := strconv.Atoi(os.Args[4])
if err != nil {
log.Fatal(err)
}
f, _ := os.Create(new)
w := bufio.NewWriter(f)
defer f.Close()
defer w.Flush()
sourceImage, err := getImageFromPath(old)
if err != nil {
log.Fatal(err)
}
resultImage, err := transformImage(sourceImage, p, size)
if err != nil {
log.Fatal(err)
}
png.Encode(w, resultImage)
}
func transformImage(img image.Image, p int, size int) (image.Image, error) {
output := image.NewRGBA(img.Bounds())
rectangles := splitSquare(img.Bounds(), size)
draw.Draw(output, output.Bounds(), img, image.ZP, draw.Src)
for _, r := range rectangles {
if rand.Intn(p+1) == 0 {
c, err := getAverageColorForRegion(img, r)
if err != nil {
return nil, err
}
draw.Draw(output, r, &image.Uniform{c}, image.ZP, draw.Src)
}
}
return output, nil
}
func getImageFromPath(path string) (image.Image, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
img, _, err := image.Decode(f)
return img, err
}
func getImageFromURL(url string) (image.Image, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
img, _, err := image.Decode(resp.Body)
return img, err
}
func getAverageColorForRegion(img image.Image, rc image.Rectangle) (color.Color, error) {
if !rc.In(img.Bounds()) {
return nil, fmt.Errorf("r is not withing img bounds")
}
var red, green, blue, alpha uint
pixels := uint(rc.Dx() * rc.Dy())
for i := rc.Min.X; i < rc.Max.X; i++ {
for j := rc.Min.Y; j < rc.Max.Y; j++ {
r, g, b, a := img.At(i, j).RGBA()
red += uint(r)
green += uint(g)
blue += uint(b)
alpha += uint(a)
}
}
return color.RGBA64{uint16(red / pixels), uint16(green / pixels), uint16(blue / pixels), uint16(alpha / pixels)}, nil
}
func colorDistance(co1, co2 color.Color) int {
//This returns the square of the color distance.
m := color.YCbCrModel
c2 := m.Convert(co2).(color.YCbCr)
c1 := m.Convert(co1).(color.YCbCr)
return int((c2.Y-c1.Y)*(c2.Y-c1.Y) + (c2.Cb-c1.Cb)*(c2.Cb-c1.Cb) + (c2.Cr-c1.Cr)*(c2.Cr-c1.Cr))
}
func splitSquare(r image.Rectangle, size int) []image.Rectangle {
var nx, ny int
nx = ((r.Max.X - r.Min.X) / size)
ny = ((r.Max.Y - r.Min.Y) / size)
//do all that 2d array crap
rectangles := make([]image.Rectangle, 0, nx*ny)
for i := 0; i < nx; i++ {
for j := 0; j < ny; j++ {
x := r.Min.X + i*size
y := r.Min.Y + j*size
rect := image.Rect(x, y, x+size, y+size)
rectangles = append(rectangles, rect)
}
}
return rectangles
}