mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
graphics: Add TestNewImageFromSubImage (not bug fix)
This commit is contained in:
parent
9de581f832
commit
fb8b6e93e5
2
image.go
2
image.go
@ -405,7 +405,7 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
|
||||
rgbaImg, ok := source.(*image.RGBA)
|
||||
if !ok || source.Bounds().Min != image.ZP {
|
||||
origImg := source
|
||||
newImg := image.NewRGBA(origImg.Bounds())
|
||||
newImg := image.NewRGBA(image.Rect(0, 0, w, h))
|
||||
draw.Draw(newImg, newImg.Bounds(), origImg, origImg.Bounds().Min, draw.Src)
|
||||
rgbaImg = newImg
|
||||
}
|
||||
|
@ -302,3 +302,35 @@ func TestNewImageFromEbitenImage(t *testing.T) {
|
||||
t.Errorf("NewImageFromImage returns error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewImageFromSubImage(t *testing.T) {
|
||||
img, err := openImage("testdata/ebiten.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
w, h := img.Bounds().Dx(), img.Bounds().Dy()
|
||||
subImg := img.(*image.NRGBA).SubImage(image.Rect(1, 1, w-1, h-1))
|
||||
eimg, err := NewImageFromImage(subImg, FilterNearest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
sw, sh := subImg.Bounds().Dx(), subImg.Bounds().Dy()
|
||||
w2, h2 := eimg.Size()
|
||||
if w2 != sw {
|
||||
t.Errorf("eimg Width: got %#v; want %#v", w2, sw)
|
||||
}
|
||||
if h2 != sh {
|
||||
t.Errorf("eimg Width: got %#v; want %#v", h2, sh)
|
||||
}
|
||||
for j := 0; j < h2; j++ {
|
||||
for i := 0; i < w2; i++ {
|
||||
got := eimg.At(i, j)
|
||||
want := color.RGBAModel.Convert(img.At(i+1, j+1))
|
||||
if got != want {
|
||||
t.Errorf("img0 At(%d, %d): got %#v; want %#v", i, j, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user