// Image places the image in s image at (x,y) with dimensions (w,h) func Image(x, y float64, w, h int, s string) { f, ferr := os.Open(s) if ferr != nil { fakeimage(x, y, w, h, s) return } defer f.Close() img, _, err := image.Decode(f) if err != nil { fakeimage(x, y, w, h, s) return } bounds := img.Bounds() minx := bounds.Min.X maxx := bounds.Max.X miny := bounds.Min.Y maxy := bounds.Max.Y data := make([]C.VGubyte, w*h*4) n := 0 // println("minx", minx, "maxx", maxx, "miny", miny, "maxy", maxy) for y := miny; y < maxy; y++ { for x := minx; x < maxx; x++ { r, g, b, a := img.At(x, (maxy-1)-y).RGBA() // OpenVG has origin at lower left, y increasing up data[n] = C.VGubyte(r) n++ data[n] = C.VGubyte(g) n++ data[n] = C.VGubyte(b) n++ data[n] = C.VGubyte(a) n++ } } C.makeimage(C.VGfloat(x), C.VGfloat(y), C.int(w), C.int(h), &data[0]) }
// poly converts coordinate slices func poly(x, y []float64) (*C.VGfloat, *C.VGfloat, C.VGint) { size := len(x) if size != len(y) { return nil, nil, 0 } px := make([]C.VGfloat, size) py := make([]C.VGfloat, size) for i := 0; i < size; i++ { px[i] = C.VGfloat(x[i]) py[i] = C.VGfloat(y[i]) } return &px[0], &py[0], C.VGint(size) }
// makestops prepares the color/stop vector func makeramp(r []Offcolor) (*C.VGfloat, C.int) { lr := len(r) nr := lr * 5 cs := make([]C.VGfloat, nr) j := 0 for i := 0; i < lr; i++ { cs[j] = C.VGfloat(r[i].Offset) j++ cs[j] = C.VGfloat(VGfloat(r[i].Red) / 255.0) j++ cs[j] = C.VGfloat(VGfloat(r[i].Green) / 255.0) j++ cs[j] = C.VGfloat(VGfloat(r[i].Blue) / 255.0) j++ cs[j] = C.VGfloat(r[i].Alpha) j++ } return &cs[0], C.int(lr) }
// Image places the named image at (x,y) with dimensions (w,h) func Image(x, y float64, w, h int, s string) { var img image.Image var derr error f, err := os.Open(s) if err != nil { fakeimage(x, y, w, h, s) return } img, _, derr = image.Decode(f) defer f.Close() if derr != nil { fakeimage(x, y, w, h, s) return } bounds := img.Bounds() minx := bounds.Min.X maxx := bounds.Max.X miny := bounds.Min.Y maxy := bounds.Max.Y data := make([]C.VGubyte, w*h*4) n := 0 var r, g, b, a uint32 for yp := miny; yp < maxy; yp++ { for xp := minx; xp < maxx; xp++ { r, g, b, a = img.At(xp, (maxy-1)-yp).RGBA() // OpenVG has origin at lower left, y increasing up data[n] = C.VGubyte(r) n++ data[n] = C.VGubyte(g) n++ data[n] = C.VGubyte(b) n++ data[n] = C.VGubyte(a) n++ } } C.makeimage(C.VGfloat(x), C.VGfloat(y), C.int(w), C.int(h), &data[0]) }
// Img places an image object at (x,y) func Img(x, y VGfloat, im image.Image) { bounds := im.Bounds() minx := bounds.Min.X maxx := bounds.Max.X miny := bounds.Min.Y maxy := bounds.Max.Y data := make([]C.VGubyte, bounds.Dx()*bounds.Dy()*4) n := 0 var r, g, b, a uint32 for yp := miny; yp < maxy; yp++ { for xp := minx; xp < maxx; xp++ { r, g, b, a = im.At(xp, (maxy-1)-yp).RGBA() // OpenVG has origin at lower left, y increasing up data[n] = C.VGubyte(r >> 8) n++ data[n] = C.VGubyte(g >> 8) n++ data[n] = C.VGubyte(b >> 8) n++ data[n] = C.VGubyte(a >> 8) n++ } } C.makeimage(C.VGfloat(x), C.VGfloat(y), C.int(bounds.Dx()), C.int(bounds.Dy()), &data[0]) }
// Ellipse draws an ellipse at (x,y) with dimensions (w,h) func Ellipse(x, y, w, h float64, style ...string) { C.Ellipse(C.VGfloat(x), C.VGfloat(y), C.VGfloat(w), C.VGfloat(h)) }
// Rect draws a rounded rectangle at (x,y) with dimensions (w,h). // the corner radii are at (rw, rh) func Roundrect(x, y, w, h, rw, rh float64, style ...string) { C.Roundrect(C.VGfloat(x), C.VGfloat(y), C.VGfloat(w), C.VGfloat(h), C.VGfloat(rw), C.VGfloat(rh)) }
// Arc draws an arc at (x,y) with dimensions (w,h). // the arc starts at the angle sa, extended to aext func Arc(x, y, w, h, sa, aext VGfloat) { C.Arc(C.VGfloat(x), C.VGfloat(y), C.VGfloat(w), C.VGfloat(h), C.VGfloat(sa), C.VGfloat(aext)) }
// Shear warps the coordinate system by (x,y) func Shear(x, y float64) { C.Shear(C.VGfloat(x), C.VGfloat(y)) }
// Translate translates the coordinate system to (x,y) func Translate(x, y float64) { C.Translate(C.VGfloat(x), C.VGfloat(y)) }
// Cbezier draws a cubic bezier curve with extrema (sx, sy) and (ex, ey). // Control points at (cx, cy) and (px, py) func Cbezier(sx, sy, cx, cy, px, py, ex, ey float64, style ...string) { C.Cbezier(C.VGfloat(sx), C.VGfloat(sy), C.VGfloat(cx), C.VGfloat(cy), C.VGfloat(px), C.VGfloat(py), C.VGfloat(ex), C.VGfloat(ey)) }
// BackgroundRGB clears the screen with the specified background color using a RGBA quad func BackgroundRGB(r, g, b uint8, alpha float64) { C.BackgroundRGB(C.uint(r), C.uint(g), C.uint(b), C.VGfloat(alpha)) }
// FillRGB sets the fill color, using RGB triples func FillRGB(r, g, b uint8, alpha float64) { C.Fill(C.uint(r), C.uint(g), C.uint(b), C.VGfloat(alpha)) }
// Shear warps the coordinate system by (x,y) func Shear(x, y VGfloat) { C.Shear(C.VGfloat(x), C.VGfloat(y)) }
// Scale scales the coordinate system by (x,y) func Scale(x, y VGfloat) { C.Scale(C.VGfloat(x), C.VGfloat(y)) }
// Rotate rotates the coordinate system around the specifed angle func Rotate(r VGfloat) { C.Rotate(C.VGfloat(r)) }
// Translate translates the coordinate system to (x,y) func Translate(x, y VGfloat) { C.Translate(C.VGfloat(x), C.VGfloat(y)) }
// TextEnd draws text end-aligned at (x,y) func TextEnd(x, y VGfloat, s string, font string, size int) { t := C.CString(s) C.TextEnd(C.VGfloat(x), C.VGfloat(y), t, selectfont(font), C.int(size)) C.free(unsafe.Pointer(t)) }
// Circle draws a circle centered at (x,y), with radius r func Circle(x, y, r float64, style ...string) { C.Circle(C.VGfloat(x), C.VGfloat(y), C.VGfloat(r)) }
// StrokeRGB sets the stroke color, using RGB triples func StrokeRGB(r, g, b uint8, alpha float64) { C.Stroke(C.uint(r), C.uint(g), C.uint(b), C.VGfloat(alpha)) }
// Qbezier draws a quadratic bezier curve with extrema (sx, sy) and (ex, ey) // Control points are at (cx, cy) func Qbezier(sx, sy, cx, cy, ex, ey float64, style ...string) { C.Qbezier(C.VGfloat(sx), C.VGfloat(sy), C.VGfloat(cx), C.VGfloat(cy), C.VGfloat(ex), C.VGfloat(ey)) }
// StrokeWidth sets the stroke width func StrokeWidth(w float64) { C.StrokeWidth(C.VGfloat(w)) }
// Arc draws an arc at (x,y) with dimensions (w,h). // the arc starts at the angle sa, extended to aext func Arc(x, y, w, h, sa, aext float64, style ...string) { C.Arc(C.VGfloat(x), C.VGfloat(y), C.VGfloat(w), C.VGfloat(h), C.VGfloat(sa), C.VGfloat(aext)) }
// Qbezier draws a quadratic bezier curve with extrema (sx, sy) and (ex, ey) // Control points are at (cx, cy) func Qbezier(sx, sy, cx, cy, ex, ey VGfloat) { C.Qbezier(C.VGfloat(sx), C.VGfloat(sy), C.VGfloat(cx), C.VGfloat(cy), C.VGfloat(ex), C.VGfloat(ey)) }
// TextEnd draws text end-aligned at (x,y) func TextEnd(x, y float64, s string, font string, size int, style ...string) { t := C.CString(s) defer C.free(unsafe.Pointer(t)) C.TextEnd(C.VGfloat(x), C.VGfloat(y), t, selectfont(font), C.int(size)) }
// Line draws a line between two points func Line(x1, y1, x2, y2 float64, style ...string) { C.Line(C.VGfloat(x1), C.VGfloat(y1), C.VGfloat(x2), C.VGfloat(y2)) }
// Rotate rotates the coordinate system around the specifed angle func Rotate(r float64) { C.Rotate(C.VGfloat(r)) }
// Rect draws a rectangle at (x,y) with dimensions (w,h) func Rect(x, y, w, h float64, style ...string) { C.Rect(C.VGfloat(x), C.VGfloat(y), C.VGfloat(w), C.VGfloat(h)) }
// Scale scales the coordinate system by (x,y) func Scale(x, y float64) { C.Scale(C.VGfloat(x), C.VGfloat(y)) }
// Cbezier draws a cubic bezier curve with extrema (sx, sy) and (ex, ey). // Control points at (cx, cy) and (px, py) func Cbezier(sx, sy, cx, cy, px, py, ex, ey VGfloat) { C.Cbezier(C.VGfloat(sx), C.VGfloat(sy), C.VGfloat(cx), C.VGfloat(cy), C.VGfloat(px), C.VGfloat(py), C.VGfloat(ex), C.VGfloat(ey)) }