-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
107 lines (94 loc) · 1.89 KB
/
reader.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
package main
import (
"image"
"image/color"
"image/jpeg"
"io/ioutil"
"os"
"path"
)
type rImage struct {
src image.Image
fileName string
}
func readImages(dirName string) []rImage {
files, err := ioutil.ReadDir(dirName)
if err != nil {
panic(err)
}
images := make([]rImage, 0, 10)
for _, elem := range files {
img, err := os.Open(path.Join(dirName, elem.Name()))
if err != nil {
panic(err)
}
src, err := jpeg.Decode(img)
if err != nil {
panic(err)
}
images = append(images, rImage{src: src, fileName: elem.Name()})
_ = img.Close()
}
return images
}
func readImage(imagePath string) *rImage {
img, err := os.Open(imagePath)
if err != nil {
panic(err)
}
defer img.Close()
src, err := jpeg.Decode(img)
if err != nil {
panic(err)
}
return &rImage{src: src, fileName: path.Base(imagePath)}
}
func (r *rImage) new2dArray() [][]byte {
height := r.src.Bounds().Dy()
width := r.src.Bounds().Dx()
res := make2dArray(width, height)
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
pixel := r.src.At(x, y)
originalColor := color.RGBAModel.Convert(pixel).(color.RGBA)
r := float64(originalColor.R) * 0.92126
g := float64(originalColor.G) * 0.97152
b := float64(originalColor.B) * 0.90722
if (r+g+b)/3 > 127 {
res[x][y] = 0
} else {
res[x][y] = 1
}
}
}
return res
}
func make2dArray(xm, ym int) [][]byte {
res := make([][]byte, xm)
for i := 0; i < xm; i++ {
res[i] = make([]byte, ym)
}
return res
}
func getFileNames(dirName string) []string {
res := make([]string, 0)
fs, err := ioutil.ReadDir(dirName)
if err != nil {
panic(err)
}
for _, file := range fs {
res = append(res, file.Name())
}
return res
}
func saveImage(path string, image *image.RGBA) {
file, err := os.Create(path)
if err != nil {
panic(err)
}
defer file.Close()
err = jpeg.Encode(file, image, nil)
if err != nil {
panic(err)
}
}