unicornpaint

A web-based painting app for raspberry PI and pimoroni Unicorn Hat HD
Log | Files | Refs | README

MakeGif.go (2235B)


      1 // +build ignore
      2 
      3 package main
      4 
      5 import (
      6 	"encoding/json"
      7 	"image"
      8 	"image/color"
      9 	"image/color/palette"
     10 	"image/draw"
     11 	"image/gif"
     12 	"io/ioutil"
     13 	"log"
     14 	"os"
     15 	"time"
     16 
     17 	"github.com/MFAshby/unicornpaint/unicorn"
     18 )
     19 
     20 var (
     21 	un unicorn.Unicorn
     22 )
     23 
     24 func imageFromPixels(pixels [][][]uint8) image.Image {
     25 	width := len(pixels)
     26 	height := len(pixels[0])
     27 	im1 := image.NewRGBA(image.Rect(0, 0, width, height))
     28 	for x := 0; x < width; x++ {
     29 		for y := 0; y < height; y++ {
     30 			r, g, b := unicorn.Rgb(pixels[x][y])
     31 			col := color.RGBA{
     32 				R: r,
     33 				G: g,
     34 				B: b,
     35 				A: 100,
     36 			}
     37 			im1.Set(x, y, col)
     38 		}
     39 	}
     40 	return im1
     41 }
     42 
     43 func toPaletted(im image.Image) *image.Paletted {
     44 	b := im.Bounds()
     45 	pm := image.NewPaletted(b, palette.Plan9[:256])
     46 	draw.FloydSteinberg.Draw(pm, b, im, image.ZP)
     47 	return pm
     48 }
     49 
     50 var (
     51 	stop bool
     52 )
     53 
     54 func renderImage(un unicorn.Unicorn, im image.Image) {
     55 	b := im.Bounds()
     56 	width := b.Dx()
     57 	height := b.Dy()
     58 	for x := 0; x < width; x++ {
     59 		for y := 0; y < height; y++ {
     60 			r, g, b, _ := im.At(x, y).RGBA()
     61 			un.SetPixel(uint8(x), uint8(y), uint8(r), uint8(g), uint8(b))
     62 		}
     63 	}
     64 	un.Show()
     65 }
     66 
     67 func renderGif(un unicorn.Unicorn, gf *gif.GIF) {
     68 	for !stop {
     69 		for i := 0; i < len(gf.Image); i++ {
     70 			im := gf.Image[i]
     71 			delay := gf.Delay[i] //100ths of a second
     72 			renderImage(un, im)
     73 			time.Sleep(time.Duration(delay * 10000000)) // nanoseconds 10^-9 sec
     74 		}
     75 	}
     76 }
     77 
     78 func main() {
     79 	b1, _ := ioutil.ReadFile("saves/rain1")
     80 	b2, _ := ioutil.ReadFile("saves/rain2")
     81 
     82 	px1 := [][][]uint8{}
     83 	json.Unmarshal(b1, &px1)
     84 	px2 := [][][]uint8{}
     85 	json.Unmarshal(b2, &px2)
     86 
     87 	im1 := toPaletted(imageFromPixels(px1))
     88 	im2 := toPaletted(imageFromPixels(px2))
     89 
     90 	gf := &gif.GIF{
     91 		Image: []*image.Paletted{im1, im2},
     92 		Delay: []int{50, 50}, // 100ths of a second
     93 	}
     94 	// im := image.NewPaletted(
     95 	// 	image.Rect(0, 0, 16, 16),
     96 	// 	palette.WebSafe)
     97 
     98 	// gf := &gif.GIF{
     99 	// 	Image: []*image.Paletted{im},
    100 	// 	Delay: []int{100},
    101 	// }
    102 
    103 	f1, err := os.Create("saves/blank.gif")
    104 	if err != nil {
    105 		log.Fatalf("Error opening GIF file to write %v", err)
    106 	}
    107 	defer f1.Close()
    108 	err = gif.EncodeAll(f1, gf)
    109 	if err != nil {
    110 		log.Printf("Error writing GIF %v", err)
    111 	}
    112 
    113 	// un, _ = unicorn.NewUnicorn()
    114 
    115 	// go renderGif(un, gf)
    116 
    117 	// un.MainLoop()
    118 }