1
0
gotour-notes/genimage/pic/pic.go
2012-08-02 15:23:46 -06:00

44 lines
787 B
Go

// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pic
import (
"bytes"
"image"
"image/png"
"log"
"os"
)
func CalculateImage(f func(int, int) [][]uint8) image.Image {
const (
dx = 256
dy = 1024
)
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
}
}
return m
}
func SaveImage(filename string, m image.Image) {
var buf bytes.Buffer
png.Encode(&buf, m)
if outfile, err := os.Create(filename); err != nil {
log.Fatal(err)
} else {
buf.WriteTo(outfile)
}
}