Skip to content

Commit 0e44f71

Browse files
committed
Loading pixel and permutations
1 parent 0b310f3 commit 0e44f71

File tree

8 files changed

+891
-0
lines changed

8 files changed

+891
-0
lines changed

10-load-pixel/main.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"image"
6+
"image/jpeg"
7+
"log"
8+
"os"
9+
"path/filepath"
10+
"time"
11+
)
12+
13+
var counter int
14+
15+
type pixel struct {
16+
r, g, b, a uint32
17+
}
18+
19+
func main() {
20+
21+
start := time.Now()
22+
images := getImages("../images/")
23+
24+
// range over the [] holding the []pixel - eg, give me each img
25+
// range over the []pixel hold the pixels - eg, give me each pixel
26+
for i, img := range images {
27+
for j, pixel := range img {
28+
fmt.Println("Image", i, "\t pixel", j, "\t r g b a:", pixel)
29+
if j == 10 {
30+
break
31+
}
32+
}
33+
}
34+
fmt.Println("PIXELS EXAMINED:", counter)
35+
fmt.Printf("%.2fs elapsed\n", time.Since(start).Seconds())
36+
}
37+
38+
func getImages(dir string) [][]pixel {
39+
40+
var images [][]pixel
41+
42+
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
43+
if info.IsDir() {
44+
return nil
45+
}
46+
47+
img := loadImage(path)
48+
pixels := getPixels(img)
49+
images = append(images, pixels)
50+
return nil
51+
})
52+
53+
return images
54+
}
55+
56+
func loadImage(filename string) image.Image {
57+
f, err := os.Open(filename)
58+
if err != nil {
59+
log.Fatal(err)
60+
}
61+
defer f.Close()
62+
63+
img, err := jpeg.Decode(f)
64+
if err != nil {
65+
log.Fatal(err)
66+
}
67+
68+
return img
69+
}
70+
71+
func getPixels(img image.Image) []pixel {
72+
73+
bounds := img.Bounds()
74+
fmt.Println(bounds.Dx(), " x ", bounds.Dy()) // debugging
75+
pixels := make([]pixel, bounds.Dx()*bounds.Dy())
76+
77+
for i := 0; i < bounds.Dx()*bounds.Dy(); i++ {
78+
x := i % bounds.Dx()
79+
y := i / bounds.Dx()
80+
r, g, b, a := img.At(x, y).RGBA()
81+
pixels[i].r = r
82+
pixels[i].g = g
83+
pixels[i].b = b
84+
pixels[i].a = a
85+
counter++
86+
}
87+
88+
return pixels
89+
}

11-load-pixel-gorutine/main.go

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

12-image-struct/main.go

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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

Comments
 (0)