Skip to content

Instantly share code, notes, and snippets.

@nna774
Last active August 29, 2015 14:07
Show Gist options
  • Save nna774/cd44f4ec4fbe8e3cc3d8 to your computer and use it in GitHub Desktop.
Save nna774/cd44f4ec4fbe8e3cc3d8 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"encoding/base64"
"fmt"
"image"
"image/png"
)
func Pic(dx, dy int) [][]uint8 {
img := make([][]uint8, dy);
for i := range img{
img[i] = make([]uint8, dx);
for j := range img[i] {
img[i][j] = uint8(i^j); // 式
}
}
return img;
}
func main() {
Show(Pic)
}
func Show(f func(int, int) [][]uint8) {
const (
dx = 512
dy = 512
)
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] = 0
m.Pix[i+3] = v
}
}
ShowImage(m)
}
func ShowImage(m image.Image) {
var buf bytes.Buffer
err := png.Encode(&buf, m)
if err != nil {
panic(err)
}
enc := base64.StdEncoding.EncodeToString(buf.Bytes())
fmt.Println("data:image/png;base64," + enc)
}
// go tutrial https://code.google.com/p/go-tour/ のコードの改変したものを含む
// 元のソースコードはApache License である
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment