mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-26 18:52:44 +01:00
graphics: Revive image_test.go
This commit is contained in:
parent
fbd07b1865
commit
90aeee5144
231
image_test.go
231
image_test.go
@ -15,13 +15,29 @@
|
||||
package ebiten_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
_ "image/png"
|
||||
"math"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
. "github.com/hajimehoshi/ebiten"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
code := 0
|
||||
// Run an Ebiten process so that (*Image).At is available.
|
||||
f := func(screen *Image) error {
|
||||
code = m.Run()
|
||||
return errors.New("regular termination")
|
||||
}
|
||||
Run(f, 320, 240, 1, "Test")
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
var ebitenImageBin = ""
|
||||
|
||||
func openImage(path string) (image.Image, error) {
|
||||
@ -50,6 +66,99 @@ func openEbitenImage(path string) (*Image, image.Image, error) {
|
||||
return eimg, img, nil
|
||||
}
|
||||
|
||||
func diff(x, y uint8) uint8 {
|
||||
if x <= y {
|
||||
return y - x
|
||||
}
|
||||
return x - y
|
||||
}
|
||||
|
||||
func TestImagePixels(t *testing.T) {
|
||||
img0, img, err := openEbitenImage("testdata/ebiten.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
if got := img0.Bounds().Size(); got != img.Bounds().Size() {
|
||||
t.Errorf("img size: got %d; want %d", got, img.Bounds().Size())
|
||||
}
|
||||
|
||||
for j := 0; j < img0.Bounds().Size().Y; j++ {
|
||||
for i := 0; i < img0.Bounds().Size().X; i++ {
|
||||
got := img0.At(i, j)
|
||||
want := color.RGBAModel.Convert(img.At(i, j))
|
||||
if got != want {
|
||||
t.Errorf("img0 At(%d, %d): got %#v; want %#v", i, j, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageComposition(t *testing.T) {
|
||||
img2Color := color.NRGBA{0x24, 0x3f, 0x6a, 0x88}
|
||||
img3Color := color.NRGBA{0x85, 0xa3, 0x08, 0xd3}
|
||||
|
||||
// TODO: Rename this to img0
|
||||
img1, _, err := openEbitenImage("testdata/ebiten.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
w, h := img1.Bounds().Size().X, img1.Bounds().Size().Y
|
||||
|
||||
img2, err := NewImage(w, h, FilterNearest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
img3, err := NewImage(w, h, FilterNearest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
img2.Fill(img2Color)
|
||||
img3.Fill(img3Color)
|
||||
img_12_3, err := NewImage(w, h, FilterNearest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
img2.DrawImage(img1, nil)
|
||||
img3.DrawImage(img2, nil)
|
||||
img_12_3.DrawImage(img3, nil)
|
||||
|
||||
img2.Fill(img2Color)
|
||||
img3.Fill(img3Color)
|
||||
img_1_23, err := NewImage(w, h, FilterNearest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
img3.DrawImage(img2, nil)
|
||||
img3.DrawImage(img1, nil)
|
||||
img_1_23.DrawImage(img3, nil)
|
||||
|
||||
for j := 0; j < h; j++ {
|
||||
for i := 0; i < w; i++ {
|
||||
c1 := img_12_3.At(i, j).(color.RGBA)
|
||||
c2 := img_1_23.At(i, j).(color.RGBA)
|
||||
if 1 < diff(c1.R, c2.R) || 1 < diff(c1.G, c2.G) || 1 < diff(c1.B, c2.B) || 1 < diff(c1.A, c2.A) {
|
||||
t.Errorf("img_12_3.At(%d, %d) = %#v; img_1_23.At(%[1]d, %[2]d) = %#[4]v", i, j, c1, c2)
|
||||
}
|
||||
if c1.A == 0 {
|
||||
t.Fatalf("img_12_3.At(%d, %d).A = 0; nothing is rendered?", i, j)
|
||||
}
|
||||
if c2.A == 0 {
|
||||
t.Fatalf("img_1_23.At(%d, %d).A = 0; nothing is rendered?", i, j)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageSelf(t *testing.T) {
|
||||
img, _, err := openEbitenImage("testdata/ebiten.png")
|
||||
if err != nil {
|
||||
@ -61,6 +170,78 @@ func TestImageSelf(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageDotByDotInversion(t *testing.T) {
|
||||
img0, _, err := openEbitenImage("testdata/ebiten.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
w, h := img0.Size()
|
||||
img1, err := NewImage(w, h, FilterNearest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
op := &DrawImageOptions{}
|
||||
op.GeoM.Rotate(math.Pi)
|
||||
op.GeoM.Translate(float64(w), float64(h))
|
||||
img1.DrawImage(img0, op)
|
||||
|
||||
for j := 0; j < h; j++ {
|
||||
for i := 0; i < w; i++ {
|
||||
c0 := img0.At(i, j).(color.RGBA)
|
||||
c1 := img1.At(w-i-1, h-j-1).(color.RGBA)
|
||||
if c0 != c1 {
|
||||
t.Errorf("img0.At(%[1]d, %[2]d) should equal to img1.At(%[3]d, %[4]d) but not: %[5]v vs %[6]v", i, j, w-i-1, h-j-1, c0, c1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplacePixels(t *testing.T) {
|
||||
origImg, err := openImage("testdata/ebiten.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
// Convert to RGBA
|
||||
img := image.NewRGBA(origImg.Bounds())
|
||||
draw.Draw(img, img.Bounds(), origImg, image.ZP, draw.Src)
|
||||
|
||||
size := img.Bounds().Size()
|
||||
img0, err := NewImage(size.X, size.Y, FilterNearest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
img0.ReplacePixels(img.Pix)
|
||||
for j := 0; j < img0.Bounds().Size().Y; j++ {
|
||||
for i := 0; i < img0.Bounds().Size().X; i++ {
|
||||
got := img0.At(i, j)
|
||||
want := img.At(i, j)
|
||||
if got != want {
|
||||
t.Errorf("img0 At(%d, %d): got %#v; want %#v", i, j, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
p := make([]uint8, 4*size.X*size.Y)
|
||||
for i, _ := range p {
|
||||
p[i] = 0x80
|
||||
}
|
||||
img0.ReplacePixels(p)
|
||||
for j := 0; j < img0.Bounds().Size().Y; j++ {
|
||||
for i := 0; i < img0.Bounds().Size().X; i++ {
|
||||
got := img0.At(i, j)
|
||||
want := color.RGBA{p[4*i], p[4*i+1], p[4*i+2], p[4*i+3]}
|
||||
if got != want {
|
||||
t.Errorf("img0 At(%d, %d): got %#v; want %#v", i, j, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageDispose(t *testing.T) {
|
||||
img, err := NewImage(16, 16, FilterNearest)
|
||||
if err != nil {
|
||||
@ -71,3 +252,53 @@ func TestImageDispose(t *testing.T) {
|
||||
t.Errorf("img.Dipose() returns error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func TestImageCompositeModeLighter(t *testing.T) {
|
||||
img0, _, err := openEbitenImage("testdata/ebiten.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
w, h := img0.Size()
|
||||
img1, err := NewImage(w, h, FilterNearest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
img1.Fill(color.RGBA{0x01, 0x02, 0x03, 0x04})
|
||||
op := &DrawImageOptions{}
|
||||
op.CompositeMode = CompositeModeLighter
|
||||
img1.DrawImage(img0, op)
|
||||
for j := 0; j < img1.Bounds().Size().Y; j++ {
|
||||
for i := 0; i < img1.Bounds().Size().X; i++ {
|
||||
got := img1.At(i, j).(color.RGBA)
|
||||
want := img0.At(i, j).(color.RGBA)
|
||||
want.R = uint8(min(0xff, int(want.R)+1))
|
||||
want.G = uint8(min(0xff, int(want.G)+2))
|
||||
want.B = uint8(min(0xff, int(want.B)+3))
|
||||
want.A = uint8(min(0xff, int(want.A)+4))
|
||||
if got != want {
|
||||
t.Errorf("img1 At(%d, %d): got %#v; want %#v", i, j, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewImageFromEbitenImage(t *testing.T) {
|
||||
img, _, err := openEbitenImage("testdata/ebiten.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
if _, err := NewImageFromImage(img, FilterNearest); err != nil {
|
||||
t.Errorf("NewImageFromImage returns error: %v", err)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user