Example of generating an image
This commit is contained in:
parent
9ccf27add8
commit
02944ee3dc
|
@ -0,0 +1,28 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"learning/pic"
|
||||||
|
"flag"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Pic(dx, dy int) [][]uint8 {
|
||||||
|
result := make([][]uint8, dy)
|
||||||
|
for i := 0; i < dx; i++ {
|
||||||
|
result[i] = make([]uint8, dx)
|
||||||
|
}
|
||||||
|
var ii, jj uint8
|
||||||
|
for i := 0; i < dy; i++ {
|
||||||
|
for j := 0; j < dy; j++ {
|
||||||
|
ii = uint8(i)
|
||||||
|
jj = uint8(j)
|
||||||
|
result[i][j] = ii ^ jj
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
image := pic.CalculateImage(Pic)
|
||||||
|
pic.SaveImage(flag.Arg(0), image)
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
// 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 = 256
|
||||||
|
)
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue