1
+ package main
2
+
3
+ // alias the "image" package
4
+ // b/c we are naming our struct "image"
5
+ import (
6
+ "fmt"
7
+ stdimage "image"
8
+ "image/jpeg"
9
+ "log"
10
+ "os"
11
+ "path/filepath"
12
+ "strings"
13
+ "sync"
14
+ "time"
15
+ )
16
+
17
+ type pixel struct {
18
+ r , g , b , a uint32
19
+ }
20
+
21
+ type image struct {
22
+ name string
23
+ pixels []pixel
24
+ width int
25
+ height int
26
+ }
27
+
28
+ func main () {
29
+ start := time .Now ()
30
+
31
+ images , err := getImages ()
32
+ if err != nil {
33
+ log .Println ("Error getting images" , err )
34
+ }
35
+
36
+ // range over the [] holding the []image - eg, give me each img
37
+ // range over the []pixel holding the pixels - eg, give me each pixel
38
+ for i , img := range images {
39
+ for j , pixel := range img .pixels {
40
+ fmt .Println ("Image" , i , "\t pixel" , j , "\t r g b a:" , pixel )
41
+ if j == 10 {
42
+ break
43
+ }
44
+ }
45
+ }
46
+
47
+ fmt .Printf ("%.2fs elapsed\n " , time .Since (start ).Seconds ())
48
+ }
49
+
50
+ func getImages () ([]image , error ) {
51
+
52
+ paths , err := getPaths ()
53
+ if err != nil {
54
+ log .Println ("Error getting paths" , err )
55
+ }
56
+
57
+ var mu sync.Mutex
58
+ var wg sync.WaitGroup
59
+ wg .Add (len (paths ))
60
+
61
+ var images []image
62
+ for _ , path := range paths {
63
+ go func (path string ) {
64
+ image := getPixels (path )
65
+
66
+ mu .Lock ()
67
+ {
68
+ images = append (images , image )
69
+ }
70
+ mu .Unlock ()
71
+
72
+ wg .Done ()
73
+ }(path )
74
+ }
75
+
76
+ wg .Wait ()
77
+
78
+ return images , nil
79
+ }
80
+
81
+ func getPaths () ([]string , error ) {
82
+ const dir = "../images/"
83
+ var paths []string
84
+
85
+ wf := func (path string , info os.FileInfo , err error ) error {
86
+ if info .IsDir () {
87
+ return nil
88
+ }
89
+ paths = append (paths , path )
90
+ return nil
91
+ }
92
+
93
+ if err := filepath .Walk (dir , wf ); err != nil {
94
+ return nil , err
95
+ }
96
+
97
+ return paths , nil
98
+ }
99
+
100
+ func getPixels (path string ) image {
101
+ img := loadImage (path )
102
+ bounds := img .Bounds ()
103
+ fmt .Println (bounds .Dx (), " x " , bounds .Dy ()) // debugging
104
+ pixels := make ([]pixel , bounds .Dx ()* bounds .Dy ())
105
+
106
+ for i := 0 ; i < bounds .Dx ()* bounds .Dy (); i ++ {
107
+ x := i % bounds .Dx ()
108
+ y := i / bounds .Dx ()
109
+ r , g , b , a := img .At (x , y ).RGBA ()
110
+ pixels [i ].r = r
111
+ pixels [i ].g = g
112
+ pixels [i ].b = b
113
+ pixels [i ].a = a
114
+ }
115
+
116
+ xs := strings .Split (path , "/" )
117
+ name := xs [(len (xs ) - 1 ):][0 ]
118
+ image := image {
119
+ name : name ,
120
+ pixels : pixels ,
121
+ width : bounds .Dx (),
122
+ height : bounds .Dy (),
123
+ }
124
+ return image
125
+ }
126
+
127
+ func loadImage (filename string ) stdimage.Image {
128
+ f , err := os .Open (filename )
129
+ if err != nil {
130
+ log .Fatal (err )
131
+ }
132
+ defer f .Close ()
133
+
134
+ img , err := jpeg .Decode (f )
135
+ if err != nil {
136
+ log .Fatal (err )
137
+ }
138
+
139
+ return img
140
+ }
0 commit comments