draw2d icon indicating copy to clipboard operation
draw2d copied to clipboard

Error drawing line to a subImage

Open ricochet2200 opened this issue 10 years ago • 0 comments

I am able to use draw2d with an RGBA image. However, if I take a SubImage of that RGBA it does not appear to draw properly with draw2d. I have included the minimum code below to reproduce my problem:

package main

import ( "fmt" "github.com/llgcode/draw2d/draw2dimg" "image" "image/color" "image/draw" )

func main() {

    img := image.NewRGBA(image.Rect(0, 0, 10, 10))
    draw.Draw(img, img.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src)
    fmt.Println("img.Bounds()", img.Bounds())
    gc := draw2dimg.NewGraphicContext(img)
    gc.SetStrokeColor(color.Black)

    // Draw a horizontal line with 1 pixel margin on each side
    gc.MoveTo(1, 1)
    gc.LineTo(9, 1)
    gc.Stroke()
    draw2dimg.SaveToPngFile("image.png", img)

    // SubImage chops off 1 pixel from each side of the image
    rect := image.Rect(1, 1, 9, 9)
    sub := img.SubImage(rect).(draw.Image)
    fmt.Println("sub.Bounds()", sub.Bounds())
    gc2 := draw2dimg.NewGraphicContext(sub)
    gc2.SetStrokeColor(color.Black)

    // The should create an equal sized bar below the first one. But doesn't :(
    gc2.MoveTo(1, 5)
    gc2.LineTo(9, 5)
    gc2.Stroke()

    // image/draw.Set can change these values, but draw2d cannot
    // Note that the coordinates are different from Set than draw2d
    sub.Set(8, 1, color.RGBA{0, 0, 0, 255})
    sub.Set(9, 1, color.RGBA{0, 0, 0, 255}) // Does not draw, out of range for sub

    // Note saving img not sub.  It made it easier for me to visualize, but other may want sub
    draw2dimg.SaveToPngFile("sub.png", img)

}

The second line does not draw as wide as I think it should. I'm pretty sure this is a bug.

Below is sub.png (not a =). You may have to save it and zoom in a bunch to see the problem. sub

Thanks!

ricochet2200 avatar Sep 11 '15 20:09 ricochet2200