aboutsummaryrefslogtreecommitdiff
path: root/MakeGif.go
blob: 8b651ea883bdad81da90ed0bfb58be3686fa3193 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// +build ignore

package main

import (
	"encoding/json"
	"image"
	"image/color"
	"image/color/palette"
	"image/draw"
	"image/gif"
	"io/ioutil"
	"log"
	"os"
	"time"

	"github.com/MFAshby/unicornpaint/unicorn"
)

var (
	un unicorn.Unicorn
)

func imageFromPixels(pixels [][][]uint8) image.Image {
	width := len(pixels)
	height := len(pixels[0])
	im1 := image.NewRGBA(image.Rect(0, 0, width, height))
	for x := 0; x < width; x++ {
		for y := 0; y < height; y++ {
			r, g, b := unicorn.Rgb(pixels[x][y])
			col := color.RGBA{
				R: r,
				G: g,
				B: b,
				A: 100,
			}
			im1.Set(x, y, col)
		}
	}
	return im1
}

func toPaletted(im image.Image) *image.Paletted {
	b := im.Bounds()
	pm := image.NewPaletted(b, palette.Plan9[:256])
	draw.FloydSteinberg.Draw(pm, b, im, image.ZP)
	return pm
}

var (
	stop bool
)

func renderImage(un unicorn.Unicorn, im image.Image) {
	b := im.Bounds()
	width := b.Dx()
	height := b.Dy()
	for x := 0; x < width; x++ {
		for y := 0; y < height; y++ {
			r, g, b, _ := im.At(x, y).RGBA()
			un.SetPixel(uint8(x), uint8(y), uint8(r), uint8(g), uint8(b))
		}
	}
	un.Show()
}

func renderGif(un unicorn.Unicorn, gf *gif.GIF) {
	for !stop {
		for i := 0; i < len(gf.Image); i++ {
			im := gf.Image[i]
			delay := gf.Delay[i] //100ths of a second
			renderImage(un, im)
			time.Sleep(time.Duration(delay * 10000000)) // nanoseconds 10^-9 sec
		}
	}
}

func main() {
	b1, _ := ioutil.ReadFile("saves/rain1")
	b2, _ := ioutil.ReadFile("saves/rain2")

	px1 := [][][]uint8{}
	json.Unmarshal(b1, &px1)
	px2 := [][][]uint8{}
	json.Unmarshal(b2, &px2)

	im1 := toPaletted(imageFromPixels(px1))
	im2 := toPaletted(imageFromPixels(px2))

	gf := &gif.GIF{
		Image: []*image.Paletted{im1, im2},
		Delay: []int{50, 50}, // 100ths of a second
	}

	f1, err := os.Create("saves/rain.gif")
	if err != nil {
		log.Fatalf("Error opening GIF file to write %v", err)
	}
	defer f1.Close()
	err = gif.EncodeAll(f1, gf)
	if err != nil {
		log.Printf("Error writing GIF %v", err)
	}

	// un, _ = unicorn.NewUnicorn()

	// go renderGif(un, gf)

	// un.MainLoop()
}