-
Notifications
You must be signed in to change notification settings - Fork 1
/
uppg2.go
51 lines (46 loc) · 797 Bytes
/
uppg2.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
package main
import (
"bytes"
"encoding/base64"
"fmt"
"image"
"image/png"
)
func Pic(dx, dy int) [][]uint8 {
pic := make([][]uint8, dx)
for i := range pic {
pic[i] = make([]uint8, dy)
for j := range pic[i] {
pic[i][j] = uint8(i * j)
}
}
return pic
}
func main() {
Show(Pic)
}
func Show(f func(int, int) [][]uint8) {
const (
dx = 256
dy = 256
)
data := f(dx, dy)
m := image.NewNRGBA(image.Rect(0, 0, dx, dy))
for y := 0; y < dy; y++ {
for x := 0; x < dx; x++ {
v := data[y][x]
i := y*m.Stride + x*4
m.Pix[i] = v
m.Pix[i+1] = v
m.Pix[i+2] = 255
m.Pix[i+3] = 255
}
}
ShowImage(m)
}
func ShowImage(m image.Image) {
var buf bytes.Buffer
png.Encode(&buf, m)
enc := base64.StdEncoding.EncodeToString(buf.Bytes())
fmt.Println("IMAGE:" + enc)
}