-
Notifications
You must be signed in to change notification settings - Fork 1
/
image.go
60 lines (47 loc) · 1.22 KB
/
image.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
package ppic
import (
"image"
"sync"
)
// rect draws a rectangle on the provided surface.
func rect(img image.Paletted, x, y, size int, c uint8) {
x *= size
y *= size
for cY := y; cY < y+size; cY++ {
for cX := x; cX < x+size; cX++ {
img.Pix[(cY-img.Rect.Min.Y)*img.Stride+(cX-img.Rect.Min.X)] = c
}
}
}
// GenerateImage returns an image for the specified grid.
func GenerateImage(grid [8][8]bool, size int, p Palette) (image.Image, error) {
if size%8 != 0 {
return nil, ErrInvalidSize
}
// The size of each pixel in the image.
pSize := size / 8
// Create the image and image data.
img := image.NewPaletted(image.Rect(0, 0, size, size), p.Palette())
// Create a wait group so we can wait for all of our goroutines to finish.
wg := sync.WaitGroup{}
// There are going to be 64 goroutines (it's an 8x8 image).
wg.Add(64)
// Draw the image data onto the image.
for y, row := range grid {
for x, val := range row {
var c uint8
// We're drawing the foreground if the value is set.
if val {
c = 1
}
// Draw the pixel.
go func(x, y int, c uint8) {
rect(*img, x, y, pSize, c)
wg.Done()
}(x, y, c)
}
}
// Wait for all of the goroutines to finish.
wg.Wait()
return img, nil
}