Created
November 12, 2009 12:49
-
-
Save gyuque/232865 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ("./canvas"; | |
"os"; | |
"math"; | |
"syscall"; | |
"image/png"; | |
"fmt"; | |
) | |
func main() { | |
img := canvas.EmptyCanvasImage(320, 240, canvas.Color32(0xffffffff)); | |
drawTestPattern(img); | |
outfile,e := outputFile("./out.png"); | |
if e == nil { | |
png.Encode(outfile, img); | |
} | |
fmt.Printf("done\n"); | |
outfile.Close(); | |
} | |
func drawTestPattern(img *canvas.CanvasImage) { | |
kr := uint32(255); | |
kb := uint32(0); | |
cx := img.Width()/2; | |
cy := img.Height()/2; | |
var ox, oy, x, y int; | |
for a := 0;a <= 360;a++ { | |
ox = x; | |
oy = y; | |
r := float64(a) * 0.0174533; | |
rf := math.Sin(r*6.0); | |
x = cx + int(math.Sin(r) * 90.0*rf); | |
y = cy + int(math.Cos(r) * 90.0*rf); | |
if a==0 {continue;} | |
img.DrawLine(ox, oy, x, y, canvas.Color32(0xff008800 | (kr<<16) | (kb))); | |
if 0 != (a&1) { | |
kr--; | |
kb++; | |
} | |
} | |
} | |
type OutFile struct { | |
fd int; | |
} | |
func outputFile(name string) (file *OutFile, err os.Error) | |
{ | |
fd, e := syscall.Open(name, syscall.O_CREAT|syscall.O_WRONLY, 0766); | |
if e==0 { | |
f := new(OutFile); | |
f.fd = fd; | |
return f, nil; | |
} | |
return nil, os.Errno(e); | |
} | |
func (f *OutFile) Write(b []byte) (ret int, err os.Error) { | |
if f == nil { | |
return -1, os.EINVAL; | |
} | |
r, e := syscall.Write(f.fd, b); | |
if e != 0 { | |
err = os.Errno(e); | |
} | |
return int(r), err | |
} | |
func (f *OutFile) Close() os.Error { | |
if f == nil { | |
return os.EINVAL | |
} | |
e := syscall.Close(f.fd); | |
f.fd = -1; | |
if e != 0 { | |
return os.Errno(e); | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment