-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
55 lines (42 loc) · 867 Bytes
/
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
package image
import (
"fmt"
"image"
"image/color"
"image/png"
"math"
"os"
"github.com/fedemengo/d2bist/internal/types"
)
const (
maxW = 5_000
maxH = 5_000
)
func WriteToPNG(bits []types.Bit, filename string) error {
n := int(math.Min(math.Sqrt(float64(len(bits))), maxW))
currW := n + 1
currH := n + 1
upLeft := image.Point{0, 0}
lowRight := image.Point{currW, currH}
img := image.NewRGBA(image.Rectangle{upLeft, lowRight})
black := color.RGBA{0, 0, 0, 0xff}
white := color.RGBA{255, 255, 255, 0xff}
for x := 0; x < currW; x++ {
for y := 0; y < currH; y++ {
idx := y*currW + x
if idx >= len(bits) {
break
}
if bits[idx] == 0 {
img.Set(x, y, white)
} else {
img.Set(x, y, black)
}
}
}
f, err := os.Create(fmt.Sprintf("%s.png", filename))
if err != nil {
return err
}
return png.Encode(f, img)
}