skelterjohn / go.wde

Windows, drawing and events for Go
217 stars 46 forks source link

CopyRGBA: support images that don't start at (0,0) #45

Closed sqweek closed 9 years ago

sqweek commented 9 years ago

Arguably this is a bug with go's image package and its failure to allow efficient blitting implementations to/from non-builtin image types (BGRA in our case). Anyway, the current implementation of CopyRGBA is not useful when dealing with images whose top-left bounding point is not image.ZP.

The following code demonstrates the issue:

package main

import (
    "image"
    "image/color"
    "image/draw"
    "github.com/skelterjohn/go.wde"
    _ "github.com/skelterjohn/go.wde/init"
)

func main() {
    go func() {
        w, _ := wde.NewWindow(300, 300)
        w.Show()
        screen := w.Screen()
        draw.Draw(screen, screen.Bounds(), &image.Uniform{color.Black}, image.ZP, draw.Src)
        centre := image.NewRGBA(screen.Bounds().Inset(50))
        src := &image.Uniform{color.RGBA{0xff, 0x00, 0x00, 0xff}}
        draw.Draw(centre, centre.Bounds(), src, image.ZP, draw.Src)
        screen.CopyRGBA(centre, centre.Bounds())
        w.FlushImage()
    }()
    wde.Run()
}

ie. the screen's image has bounds (0,0)-(300,300). centre has bounds (50,50)-(250,250). But the part of the window that actually gets drawn red is (100,100)-(250,250) - there's an extra translation going on.

This PR causes the area (50,50)-(250,250) to be drawn red (tested on linux only). It shouldn't change the results for images that are anchored on image.ZP.