Skip to content

Instantly share code, notes, and snippets.

@jtrim
Created January 3, 2018 07:01
Show Gist options
  • Save jtrim/067166a56e7c43bfcf28fb8954c406d5 to your computer and use it in GitHub Desktop.
Save jtrim/067166a56e7c43bfcf28fb8954c406d5 to your computer and use it in GitHub Desktop.
package main
import (
"image"
"image/png"
"os"
)
func Show(f func(int, int) [][]uint8) {
const (
dx = 1000
dy = 1000
)
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
}
}
ShowImageFile(m)
}
func ShowImageFile(m image.Image) {
var outImg, _ = os.Create("/tmp/img.png")
defer outImg.Close()
err := png.Encode(outImg, m)
if err != nil {
panic(err)
}
}
func Fibb(fibbs []uint64, numiters int) []uint64 {
if numiters == 0 {
return fibbs
}
currentfibs := fibbs[len(fibbs)-2:]
a, b := currentfibs[0], currentfibs[1]
return Fibb(append(fibbs, b, a+b), numiters-1)
}
func Pic(dx, dy int) [][]uint8 {
var resultPic [][]uint8
var fibbs = Fibb([]uint64{1, 2}, (dx*dy)-2)
for i := 0; i < dy; i++ {
resultPic = append(resultPic, []uint8{})
for j := 0; j < dx; j++ {
resultPic[i] = append(resultPic[i], uint8(fibbs[i*j]%255))
}
}
return resultPic
}
func main() {
Show(Pic)
}
@jtrim
Copy link
Author

jtrim commented Jan 3, 2018

img

@jtrim
Copy link
Author

jtrim commented Jan 3, 2018

Several code snippets borrowed from the golang.org/x/tour/pic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment