func faceDetect(i *image.Image, o *image.Image) { cvImage := opencv.FromImage(*i) _, err := os.Stat(faceDetectionHaarCascade) if err != nil { fmt.Println(err) os.Exit(1) } cascade := opencv.LoadHaarClassifierCascade(faceDetectionHaarCascade) faces := cascade.DetectObjects(cvImage) gc := draw2d.NewGraphicContext((*o).(*image.RGBA)) if debug == true { fmt.Println("Faces detected:", len(faces)) } for _, face := range faces { if debug == true { fmt.Printf("Face: x: %d y: %d w: %d h: %d\n", face.X(), face.Y(), face.Width(), face.Height()) } draw2d.Ellipse( gc, float64(face.X()+(face.Width()/2)), float64(face.Y()+(face.Height()/2)), float64(face.Width()/2), float64(face.Height())/2) gc.SetFillColor(color.RGBA{255, 0, 0, 255}) gc.Fill() } }
func newContext(w, h int) (image.Image, draw2d.GraphicContext) { m := image.NewRGBA(image.Rect(0, 0, w, h)) ctxt := draw2d.NewGraphicContext(m) ctxt.SetFillColor(image.White) ctxt.SetStrokeColor(image.Black) return m, ctxt }
func (c *Canvas) textImage(font vg.Font, str string) *image.RGBA { w := font.Width(str).Dots(c) h := font.Extents().Height.Dots(c) img := image.NewRGBA(image.Rect(0, 0, int(w+0.5), int(h+0.5))) gc := draw2d.NewGraphicContext(img) gc.SetDPI(int(c.DPI())) gc.SetFillColor(c.color[len(c.color)-1]) gc.SetStrokeColor(c.color[len(c.color)-1]) data, ok := fontMap[font.Name()] if !ok { panic(fmt.Sprintf("Font name %s is unknown", font.Name())) } if !registeredFont[font.Name()] { draw2d.RegisterFont(data, font.Font()) registeredFont[font.Name()] = true } gc.SetFontData(data) gc.SetFontSize(font.Size.Points()) gc.MoveTo(0, h+font.Extents().Descent.Dots(c)) gc.FillString(str) return img }
func (k *Karta) drawImage() { img := image.NewRGBA(image.Rect(0, 0, k.Width, k.Height)) l := draw2d.NewGraphicContext(img) l.SetLineWidth(1.2) // Iterate over cells for i, cell := range k.Diagram.Cells { l.SetFillColor(k.Cells[i].FillColor) l.SetStrokeColor(k.Cells[i].StrokeColor) for _, hedge := range cell.Halfedges { a := hedge.GetStartpoint() b := hedge.GetEndpoint() l.MoveTo(a.X, a.Y) l.LineTo(b.X, b.Y) } l.FillStroke() } l.Close() k.Image = img }
func RenderString(text string, fd draw2d.FontData, size float64, color color.Color) (buffer image.Image) { const stretchFactor = 1.2 height := GetFontHeight(fd, size) * stretchFactor widthMax := float64(len(text)) * size buf := image.NewRGBA(image.Rectangle{ Min: image.Point{0, 0}, Max: image.Point{int(widthMax + 1), int(height + 1)}, }) gc := draw2d.NewGraphicContext(buf) gc.Translate(0, height/stretchFactor) gc.SetFontData(fd) gc.SetFontSize(size) gc.SetStrokeColor(color) width := gc.FillString(text) buffer = buf.SubImage(image.Rectangle{ Min: image.Point{0, 0}, Max: image.Point{int(width + 1), int(height + 1)}, }) return }
func line(i *image.RGBA, x, y, x0, y0 int) { gc := draw2d.NewGraphicContext(i) gc.SetStrokeColor(color.RGBA{0xff, 0xff, 0xff, 0xff}) gc.MoveTo(float64(x), float64(y)) gc.LineTo(float64(x0), float64(y0)) gc.Stroke() }
func drawImage(bmp image.Image, state *DrawingState) image.Image { result := image.NewRGBA(bmp.Bounds()) draw.Draw(result, result.Bounds(), image.Black, image.ZP, draw.Src) g := draw2d.NewGraphicContext(result) g.SetStrokeColor(color.White) g.SetLineWidth(1) const maxIterations = 1000000 var newState *DrawingState for i := 0; i < maxIterations; i++ { newState = Next(bmp, state) if newState == nil { break } g.MoveTo(float64(state.P.X), float64(state.P.Y)) if newState.Clr != 0 { g.LineTo(float64(newState.P.X), float64(newState.P.Y)) g.Stroke() } state = newState } return result }
func (f *Foundation) Draw(buffer draw.Image, invalidRects RectSet) { gc := draw2d.NewGraphicContext(buffer) f.DoPaint(gc) for child, bounds := range f.getChildBoundsMap() { r := RectangleForRect(bounds) // only redraw those that have been invalidated or are // otherwise unable to draw themselves if child.buffer == nil || invalidRects.Intersects(bounds) { subInv := invalidRects.Intersection(bounds).Translate(bounds.Min.Times(-1)) or := image.Rectangle{ Max: image.Point{int(child.Size.X), int(child.Size.Y)}, } if child.buffer == nil || child.buffer.Bounds() != or { child.buffer = image.NewRGBA(or) } else { for _, r := range subInv { ir := RectangleForRect(r) ZeroRGBA(child.buffer.(*image.RGBA).SubImage(ir).(*image.RGBA)) } } subInv = subInv.Translate(bounds.Min.Times(-1)) child.Drawer.Draw(child.buffer, subInv) } draw.Draw(buffer, r, child.buffer, image.Point{0, 0}, f.DrawOp) } }
func (wf *WindowFoundation) handleWindowDrawing() { // TODO: collect a dirty region (possibly disjoint), and draw in one go? wf.ParentDrawBuffer = make(chan image.Image) for { select { case dirtyBounds := <-wf.Redraw: gc := draw2d.NewGraphicContext(wf.W.Screen()) gc.Clear() gc.BeginPath() // TODO: pass dirtyBounds too, to avoid redrawing out of reach components _ = dirtyBounds wf.doPaint(gc) dr := DrawRequest{ Dirty: dirtyBounds, } wf.Draw <- dr wf.W.FlushImage() case buffer := <-wf.ParentDrawBuffer: draw.Draw(wf.W.Screen(), buffer.Bounds(), buffer, image.Point{0, 0}, draw.Src) // TODO: don't do this every time - give a window for all expected buffers to // come in before flushing prematurely wf.W.FlushImage() } } }
func (p *GesturePane) Render() (*image.RGBA, error) { img := image.NewRGBA(image.Rect(0, 0, 16, 16)) if p.last != nil { x := math.Floor(float64(p.last.Position.X)/float64(0xffff)*float64(16)) + 0.5 y := math.Floor(float64(p.last.Position.Y)/float64(0xffff)*float64(16)) + 0.5 z := math.Floor(float64(p.last.Position.Z)/float64(0xffff)*float64(16)) + 0.5 r, _ := colorful.Hex("#FF000") g, _ := colorful.Hex("#00FF00") b, _ := colorful.Hex("#0000FF") gc := draw2d.NewGraphicContext(img) gc.SetStrokeColor(r) gc.MoveTo(0, x) gc.LineTo(16, x) gc.Stroke() gc.SetStrokeColor(g) gc.MoveTo(y, 0) gc.LineTo(y, 16) gc.Stroke() gc.SetStrokeColor(b) gc.MoveTo(16-z, 0) gc.LineTo(16-z, 16) gc.Stroke() } return img, nil }
func (e *Entry) render() { const stretchFactor = 1.2 text := string(e.text) height := uik.GetFontHeight(e.fd, e.fontSize) * stretchFactor widthMax := float64(len(text)) * e.fontSize buf := image.NewRGBA(image.Rectangle{ Min: image.Point{0, 0}, Max: image.Point{int(widthMax + 1), int(height + 1)}, }) gc := draw2d.NewGraphicContext(buf) gc.Translate(0, height/stretchFactor) gc.SetFontData(e.fd) gc.SetFontSize(e.fontSize) gc.SetStrokeColor(color.Black) var left float64 e.runeOffsets = []float64{0} for _, r := range e.text { rt := string(r) width := gc.FillString(rt) gc.Translate(width, 0) left += width e.runeOffsets = append(e.runeOffsets, left) } e.textBuffer = buf.SubImage(image.Rectangle{ Min: image.Point{0, 0}, Max: image.Point{int(left + 1), int(height + 1)}, }).(*image.RGBA) }
// Gets the singleton ImageGraphicContext. func GetTheImageGraphicContext() *draw2d.ImageGraphicContext { if graphicContextInstance == nil { graphicContextInstance = draw2d.NewGraphicContext(GetTheImage().canvas) } return graphicContextInstance }
func Draw(r *td6.Ride) image.Image { width := PIECE_WIDTH * (len(r.TrackData.Elements) + 2) rect := image.Rect(0, 0, width, IMG_HEIGHT) i := image.NewRGBA(rect) c := color.RGBA{0xff, 0xff, 0xff, 0xff} draw.Draw(i, i.Bounds(), &image.Uniform{c}, image.ZP, draw.Src) gc := draw2d.NewGraphicContext(i) x := float64(PIECE_WIDTH) y := float64(IMG_HEIGHT) - 20.0 for j := 0; j < len(r.TrackData.Elements); j++ { gc.MoveTo(x, y) elem := r.TrackData.Elements[j] seg := elem.Segment if elem.ChainLift { gc.SetStrokeColor(BLUE) } else { gc.SetStrokeColor(RED) } y -= 8.0 * float64(seg.ElevationDelta) gc.LineTo(float64(x+PIECE_WIDTH), y) gc.Stroke() x += PIECE_WIDTH } return i }
func initGc(w, h int) (image.Image, draw2d.GraphicContext) { d := image.NewRGBA(image.Rect(0, 0, w, h)) gc := draw2d.NewGraphicContext(d) gc.SetStrokeColor(image.Black) gc.SetFillColor(image.White) return d, gc }
func main() { i := image.NewRGBA(image.Rect(0, 0, 200, 200)) gc := draw2d.NewGraphicContext(i) gc.MoveTo(10.0, 10.0) gc.LineTo(100.0, 10.0) gc.Stroke() saveToPngFile("TestPath.png", i) }
func (f *PngDebugDraw) BeginFrame(info figo.FrameData) { f.frame = info f.cImg = image.NewRGBA(WorldRect(info.HalfWidth, info.HalfHeight, f.DecToPix)) f.ctx = draw2d.NewGraphicContext(f.cImg) f.offset = glm.Vector2{info.HalfWidth, info.HalfHeight} f.ctx.SetFillColor(color.White) f.ctx.Clear() }
func Vis(feat *rimg64.Multi, weights WeightSet, cell int) image.Image { if feat.Channels == 31 { return Vis(compress(feat, weights), weights, cell) } width, height := feat.Width*cell, feat.Height*cell img := image.NewRGBA(image.Rect(0, 0, width, height)) // Fill background. bg := color.Gray{0} if weights == Signed { bg = color.Gray{128} } draw.Draw(img, img.Bounds(), image.NewUniform(bg), image.ZP, draw.Src) // Rescale intensities to [0, 1]. var dst vec.Mutable = vec.Slice(feat.Elems) var src vec.Const = dst if weights == Signed { max, _ := vec.Max(vec.Abs(src)) rescale := func(x float64) float64 { return (1 + x/max) / 2 } vec.Copy(dst, vec.Map(src, rescale)) } else { switch weights { case Neg: src = vec.Scale(-1, src) case Abs: src = vec.Abs(src) default: } max, _ := vec.Max(src) if max <= 0 { vec.Copy(dst, vec.Zeros(src.Len())) } else { rescale := func(x float64) float64 { return math.Max(0, x/max) } vec.Copy(dst, vec.Map(src, rescale)) } } gc := draw2d.NewGraphicContext(img) gc.SetLineWidth(1) // Draw cells. for x := 0; x < feat.Width; x++ { for y := 0; y < feat.Height; y++ { drawCell(feat, x, y, gc, cell) } } return img }
func BenchmarkLabelDraw(b *testing.B) { b.StopTimer() label := NewLabel(geom.Coord{100, 100}, LabelConfig{"hello world", 12, color.Black}) ctx := draw2d.NewGraphicContext(image.NewRGBA(image.Rect(0, 0, 100, 100))) b.StartTimer() for i := 0; i < b.N; i++ { label.DoPaint(ctx) } }
func BenchmarkCheckboxDraw(b *testing.B) { b.StopTimer() checkbox := NewCheckbox(geom.Coord{100, 100}) ctx := draw2d.NewGraphicContext(image.NewRGBA(image.Rect(0, 0, 100, 100))) b.StartTimer() for i := 0; i < b.N; i++ { checkbox.DoPaint(ctx) } }
func BenchmarkButtonDraw(b *testing.B) { b.StopTimer() button := NewButton("test") ctx := draw2d.NewGraphicContext(image.NewRGBA(image.Rect(0, 0, 100, 100))) b.StartTimer() for i := 0; i < b.N; i++ { button.DoPaint(ctx) } }
func BenchmarkEntryDraw(b *testing.B) { b.StopTimer() entry := NewEntry(geom.Coord{100, 100}, "hello world") ctx := draw2d.NewGraphicContext(image.NewRGBA(image.Rect(0, 0, 100, 100))) b.StartTimer() for i := 0; i < b.N; i++ { entry.DoPaint(ctx) } }
func BenchmarkRadioDraw(b *testing.B) { b.StopTimer() radio := NewRadio([]string{"foo", "bar", "baz"}) ctx := draw2d.NewGraphicContext(image.NewRGBA(image.Rect(0, 0, 100, 100))) b.StartTimer() for i := 0; i < b.N; i++ { radio.DoPaint(ctx) } }
// Paint is called when the Area needs to be redrawn. // The part of the Area that needs to be redrawn is stored in cliprect. // Before Paint() is called, this region is cleared with a system-defined background color. // You MUST handle this event, and you MUST return a valid image, otherwise deadlocks and panicking will occur. // The image returned must have the same size as rect (but does not have to have the same origin points). func (area *canvasArea) Paint(cliprect image.Rectangle) *image.RGBA { //area.img.SetRGBA(0, 0, color.RGBA{0, 0, 0, 255}) dc := draw2d.NewGraphicContext(area.img) dc.Save() dc.SetFillColor(color.Black) draw2d.Ellipse(dc, 16, 16, 8, 8) dc.FillStroke() dc.Restore() return area.img.SubImage(cliprect).(*image.RGBA) }
func initGc(w, h int) (image.Image, draw2d.GraphicContext) { i := image.NewRGBA(image.Rect(0, 0, w, h)) gc := draw2d.NewGraphicContext(i) gc.SetStrokeColor(image.Black) gc.SetFillColor(image.White) // fill the background //gc.Clear() return i, gc }
func (b *Block) PrepareBuffer() (gc draw2d.GraphicContext) { min := image.Point{0, 0} max := image.Point{int(b.Size.X), int(b.Size.Y)} if b.Buffer == nil || b.Buffer.Bounds().Min != min || b.Buffer.Bounds().Max != max { b.Buffer = image.NewRGBA(image.Rectangle{ Min: min, Max: max, }) } gc = draw2d.NewGraphicContext(b.Buffer) return }
// AddTo returns a new ImageGraphics which will write to (width x height) sized // area starting at (x,y) on the provided image img. The rest of the parameters // are the same as in New(). func AddTo(img *image.RGBA, x, y, width, height int, bgcol color.RGBA, font *truetype.Font, fontsize int) *ImageGraphics { gc := draw2d.NewGraphicContext(img) gc.SetStrokeColor(image.Black) gc.SetFillColor(bgcol) gc.Translate(float64(x)+0.5, float64(y)+0.5) gc.ClearRect(x, y, x+width, y+height) if font == nil { font = defaultFont } return &ImageGraphics{Image: img, x0: x, y0: y, w: width, h: height, bg: bgcol, gc: gc, font: font, fs: fontsize} }
// Create a new canvas func CreateNewCanvas(w, h int) Canvas { i := image.NewRGBA(image.Rect(0, 0, w, h)) gc := draw2d.NewGraphicContext(i) gc.SetStrokeColor(image.Black) gc.SetFillColor(image.White) // fill the background gc.Clear() gc.SetFillColor(image.Black) return Canvas{gc, i, w, h} }
func drawPolyLine(img *image.RGBA, color color.Color, coords [][]float64) { path := draw2d.NewPathStorage() for i, coord := range coords { if i == 0 { path.MoveTo(coord[0], coord[1]) } else { path.LineTo(coord[0], coord[1]) } } gc := draw2d.NewGraphicContext(img) gc.SetStrokeColor(color) gc.Stroke(path) }
func main() { var file *os.File var outFile *os.File var img image.Image var err error if file, err = os.Open("pkg.png"); err != nil { println("Error", err) return } defer file.Close() if img, err = png.Decode(file); err != nil { println("Error", err) return } // 描画の前にフォントのディレクトリを設定しておく。 // フォント名はルールに従って draw2d.SetFontFolder("font/") // draw2d.SetFontFolder("/System/Library/Fonts/") i := image.NewRGBA(img.Bounds()) gc := draw2d.NewGraphicContext(i) // 画像のコピーをしようと思ったら、DrawImage()を呼び出して、元の画像を描画させる? gc.DrawImage(img) // 任意の線を描画 gc.MoveTo(60.0, 10.0) gc.LineTo(60.0, 20.0) gc.MoveTo(65.0, 10.0) gc.LineTo(65.0, 20.0) gc.MoveTo(58.0, 13.0) gc.LineTo(68.0, 13.0) gc.MoveTo(58.0, 16.0) gc.LineTo(68.0, 16.0) gc.Stroke() gc.StrokeStringAt("golang", 70, 10) // gc.FillStringAt("golang", 70, 10) if outFile, err = os.Create("out_write_pkg.png"); err != nil { println("Error", err) return } defer outFile.Close() if err = png.Encode(outFile, i); err != nil { println("Error", err) return } }
// Make a new TurtleGraphics System. // // A ruleset needs to be passed in, translating the // LSystem state into drawing function calls. func NewTurtleGraphics(width, height int, rules *TurtleGraphicsRules) *TurtleGraphics { tg := &TurtleGraphics{ rules: rules, InitPosX: float64(width / 2), InitPosY: float64(height / 2), InitAngle: 0.0, } tg.Image = image.NewRGBA(image.Rect(0, 0, width, height)) tg.Gc = draw2d.NewGraphicContext(tg.Image) tg.Gc.Clear() return tg }