1
0
gotour-notes/genimage/pic/pic.go

44 lines
787 B
Go
Raw Normal View History

2012-08-01 08:13:46 -07:00
// 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
2012-08-02 14:04:07 -07:00
dy = 1024
2012-08-01 08:13:46 -07:00
)
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)
}
}