2016-05-11 19:00:22 +02:00
|
|
|
// Copyright 2016 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package ebiten_test
|
|
|
|
|
|
|
|
import (
|
2018-03-14 19:48:32 +01:00
|
|
|
"bytes"
|
2016-05-16 16:35:58 +02:00
|
|
|
"errors"
|
2016-05-11 19:00:22 +02:00
|
|
|
"image"
|
2016-05-16 16:35:58 +02:00
|
|
|
"image/color"
|
|
|
|
"image/draw"
|
2016-05-11 19:00:22 +02:00
|
|
|
_ "image/png"
|
2016-05-16 16:35:58 +02:00
|
|
|
"math"
|
|
|
|
"os"
|
2016-05-11 19:00:22 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "github.com/hajimehoshi/ebiten"
|
2017-12-11 15:07:01 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
2018-03-14 19:48:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/examples/resources/images"
|
2018-11-17 12:20:46 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
2018-04-05 20:54:10 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/testflock"
|
2016-05-11 19:00:22 +02:00
|
|
|
)
|
|
|
|
|
2016-05-16 16:35:58 +02:00
|
|
|
func TestMain(m *testing.M) {
|
2018-04-05 20:54:10 +02:00
|
|
|
testflock.Lock()
|
|
|
|
defer testflock.Unlock()
|
|
|
|
|
2016-05-16 16:35:58 +02:00
|
|
|
code := 0
|
|
|
|
// Run an Ebiten process so that (*Image).At is available.
|
2016-08-01 19:47:45 +02:00
|
|
|
regularTermination := errors.New("regular termination")
|
2016-05-16 16:35:58 +02:00
|
|
|
f := func(screen *Image) error {
|
|
|
|
code = m.Run()
|
2016-08-01 19:47:45 +02:00
|
|
|
return regularTermination
|
|
|
|
}
|
|
|
|
if err := Run(f, 320, 240, 1, "Test"); err != nil && err != regularTermination {
|
|
|
|
panic(err)
|
2016-05-16 16:35:58 +02:00
|
|
|
}
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
2018-03-14 19:48:32 +01:00
|
|
|
func openEbitenImage() (*Image, image.Image, error) {
|
|
|
|
img, _, err := image.Decode(bytes.NewReader(images.Ebiten_png))
|
2016-05-11 19:00:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
eimg, err := NewImageFromImage(img, FilterNearest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return eimg, img, nil
|
|
|
|
}
|
|
|
|
|
2018-02-03 09:00:57 +01:00
|
|
|
func abs(x int) int {
|
|
|
|
if x < 0 {
|
|
|
|
return -x
|
2016-05-16 16:35:58 +02:00
|
|
|
}
|
2018-02-03 09:00:57 +01:00
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
// sameColors compares c1 and c2 and returns a boolean value indicating
|
|
|
|
// if the two colors are (almost) same.
|
|
|
|
//
|
|
|
|
// Pixels read from GPU might include errors (#492), and
|
|
|
|
// sameColors considers such errors as delta.
|
|
|
|
func sameColors(c1, c2 color.RGBA, delta int) bool {
|
|
|
|
return abs(int(c1.R)-int(c2.R)) <= delta &&
|
|
|
|
abs(int(c1.G)-int(c2.G)) <= delta &&
|
|
|
|
abs(int(c1.B)-int(c2.B)) <= delta &&
|
|
|
|
abs(int(c1.A)-int(c2.A)) <= delta
|
2016-05-16 16:35:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestImagePixels(t *testing.T) {
|
2018-03-14 19:48:32 +01:00
|
|
|
img0, img, err := openEbitenImage()
|
2016-05-16 16:35:58 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if got := img0.Bounds().Size(); got != img.Bounds().Size() {
|
2016-12-26 19:18:56 +01:00
|
|
|
t.Fatalf("img size: got %d; want %d", got, img.Bounds().Size())
|
2016-05-16 16:35:58 +02:00
|
|
|
}
|
|
|
|
|
2017-02-04 20:24:07 +01:00
|
|
|
w, h := img0.Bounds().Size().X, img0.Bounds().Size().Y
|
|
|
|
// Check out of range part
|
2018-11-17 12:20:46 +01:00
|
|
|
w2, h2 := graphics.NextPowerOf2Int(w), graphics.NextPowerOf2Int(h)
|
2017-02-04 20:24:07 +01:00
|
|
|
for j := -100; j < h2+100; j++ {
|
|
|
|
for i := -100; i < w2+100; i++ {
|
2016-05-16 16:35:58 +02:00
|
|
|
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
|
2018-03-14 19:48:32 +01:00
|
|
|
img1, _, err := openEbitenImage()
|
2016-05-16 16:35:58 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-08-03 04:05:10 +02:00
|
|
|
if err := img2.Fill(img2Color); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := img3.Fill(img3Color); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2016-05-16 16:35:58 +02:00
|
|
|
img_12_3, err := NewImage(w, h, FilterNearest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2016-08-03 04:05:10 +02:00
|
|
|
if err := img2.DrawImage(img1, nil); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := img3.DrawImage(img2, nil); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := img_12_3.DrawImage(img3, nil); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2016-05-16 16:35:58 +02:00
|
|
|
|
2016-08-03 04:05:10 +02:00
|
|
|
if err := img2.Fill(img2Color); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := img3.Fill(img3Color); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2016-05-16 16:35:58 +02:00
|
|
|
img_1_23, err := NewImage(w, h, FilterNearest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2016-08-03 04:05:10 +02:00
|
|
|
if err := img3.DrawImage(img2, nil); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := img3.DrawImage(img1, nil); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := img_1_23.DrawImage(img3, nil); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2016-05-16 16:35:58 +02:00
|
|
|
|
|
|
|
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)
|
2018-02-03 09:00:57 +01:00
|
|
|
if !sameColors(c1, c2, 1) {
|
2016-05-16 16:35:58 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 19:00:22 +02:00
|
|
|
func TestImageSelf(t *testing.T) {
|
2018-03-10 16:40:37 +01:00
|
|
|
// Note that mutex usages: without defer, unlocking is not called when panicing.
|
2017-03-03 18:20:13 +01:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r == nil {
|
|
|
|
t.Errorf("DrawImage must panic but not")
|
|
|
|
}
|
|
|
|
}()
|
2018-03-14 19:48:32 +01:00
|
|
|
img, _, err := openEbitenImage()
|
2016-05-11 19:00:22 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2017-03-03 18:20:13 +01:00
|
|
|
img.DrawImage(img, nil)
|
2016-05-11 19:00:22 +02:00
|
|
|
}
|
|
|
|
|
2017-02-04 22:15:51 +01:00
|
|
|
func TestImageScale(t *testing.T) {
|
2017-02-05 08:05:19 +01:00
|
|
|
for _, scale := range []int{2, 3, 4} {
|
2018-03-14 19:48:32 +01:00
|
|
|
img0, _, err := openEbitenImage()
|
2017-02-05 08:05:19 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w, h := img0.Size()
|
|
|
|
img1, err := NewImage(w*scale, h*scale, FilterNearest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
op := &DrawImageOptions{}
|
|
|
|
op.GeoM.Scale(float64(scale), float64(scale))
|
2018-03-10 16:28:30 +01:00
|
|
|
|
2017-02-05 08:05:19 +01:00
|
|
|
if err := img1.DrawImage(img0, op); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2017-02-04 22:15:51 +01:00
|
|
|
|
2017-05-27 10:25:47 +02:00
|
|
|
for j := 0; j < h*scale; j++ {
|
|
|
|
for i := 0; i < w*scale; i++ {
|
2017-02-05 08:05:19 +01:00
|
|
|
c0 := img0.At(i/scale, j/scale).(color.RGBA)
|
|
|
|
c1 := img1.At(i, j).(color.RGBA)
|
|
|
|
if c0 != c1 {
|
2017-05-27 10:25:47 +02:00
|
|
|
t.Fatalf("img0.At(%[1]d, %[2]d) should equal to img1.At(%[3]d, %[4]d) (with scale %[5]d) but not: %[6]v vs %[7]v", i/2, j/2, i, j, scale, c0, c1)
|
2017-02-05 08:05:19 +01:00
|
|
|
}
|
2017-02-04 22:15:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-04 13:41:00 +01:00
|
|
|
func TestImage90DegreeRotate(t *testing.T) {
|
2018-03-14 19:48:32 +01:00
|
|
|
img0, _, err := openEbitenImage()
|
2017-02-04 13:41:00 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w, h := img0.Size()
|
|
|
|
img1, err := NewImage(h, w, FilterNearest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
op := &DrawImageOptions{}
|
|
|
|
op.GeoM.Rotate(math.Pi / 2)
|
|
|
|
op.GeoM.Translate(float64(h), 0)
|
|
|
|
if err := img1.DrawImage(img0, op); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
|
|
|
c0 := img0.At(i, j).(color.RGBA)
|
|
|
|
c1 := img1.At(h-j-1, i).(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, h-j-1, i, c0, c1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 16:35:58 +02:00
|
|
|
func TestImageDotByDotInversion(t *testing.T) {
|
2018-03-14 19:48:32 +01:00
|
|
|
img0, _, err := openEbitenImage()
|
2016-05-16 16:35:58 +02:00
|
|
|
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))
|
2016-08-03 04:05:10 +02:00
|
|
|
if err := img1.DrawImage(img0, op); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2016-05-16 16:35:58 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-10 16:28:30 +01:00
|
|
|
func TestImageReplacePixels(t *testing.T) {
|
2018-03-03 12:42:16 +01:00
|
|
|
// Create a dummy image so that the shared texture is used and origImg's position is shfited.
|
2018-03-03 12:35:44 +01:00
|
|
|
dummyImg, _ := NewImageFromImage(image.NewRGBA(image.Rect(0, 0, 16, 16)), FilterDefault)
|
|
|
|
defer dummyImg.Dispose()
|
|
|
|
|
2018-03-14 19:48:32 +01:00
|
|
|
_, origImg, err := openEbitenImage()
|
2016-05-16 16:35:58 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2018-03-14 19:48:32 +01:00
|
|
|
// Convert to *image.RGBA just in case.
|
2016-05-16 16:35:58 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-08-03 04:05:10 +02:00
|
|
|
if err := img0.ReplacePixels(img.Pix); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2016-05-16 16:35:58 +02:00
|
|
|
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)
|
2016-05-18 05:09:48 +02:00
|
|
|
for i := range p {
|
2016-05-16 16:35:58 +02:00
|
|
|
p[i] = 0x80
|
|
|
|
}
|
2016-08-03 04:05:10 +02:00
|
|
|
if err := img0.ReplacePixels(p); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2016-08-22 17:13:44 +02:00
|
|
|
// Even if p is changed after calling ReplacePixel, img0 uses the original values.
|
|
|
|
for i := range p {
|
|
|
|
p[i] = 0
|
|
|
|
}
|
2016-05-16 16:35:58 +02:00
|
|
|
for j := 0; j < img0.Bounds().Size().Y; j++ {
|
|
|
|
for i := 0; i < img0.Bounds().Size().X; i++ {
|
|
|
|
got := img0.At(i, j)
|
2016-08-22 17:13:44 +02:00
|
|
|
want := color.RGBA{0x80, 0x80, 0x80, 0x80}
|
2016-05-16 16:35:58 +02:00
|
|
|
if got != want {
|
|
|
|
t.Errorf("img0 At(%d, %d): got %#v; want %#v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:24:04 +01:00
|
|
|
func TestImageReplacePixelsNil(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r == nil {
|
|
|
|
t.Errorf("ReplacePixels(nil) must panic")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
img, err := NewImage(16, 16, FilterNearest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
img.Fill(color.White)
|
|
|
|
img.ReplacePixels(nil)
|
|
|
|
}
|
|
|
|
|
2016-05-11 19:00:22 +02:00
|
|
|
func TestImageDispose(t *testing.T) {
|
|
|
|
img, err := NewImage(16, 16, FilterNearest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2018-03-03 10:51:52 +01:00
|
|
|
img.Fill(color.White)
|
2016-05-11 19:00:22 +02:00
|
|
|
if err := img.Dispose(); err != nil {
|
|
|
|
t.Errorf("img.Dipose() returns error: %v", err)
|
|
|
|
}
|
2018-03-03 10:51:52 +01:00
|
|
|
|
|
|
|
// The color is transparent (color.RGBA{}).
|
|
|
|
// Note that the value's type must be color.RGBA.
|
|
|
|
got := img.At(0, 0)
|
|
|
|
want := color.RGBA{}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("img.At(0, 0) got: %v, want: %v", got, want)
|
|
|
|
}
|
2016-05-11 19:00:22 +02:00
|
|
|
}
|
2016-05-16 16:35:58 +02:00
|
|
|
|
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImageCompositeModeLighter(t *testing.T) {
|
2018-03-14 19:48:32 +01:00
|
|
|
img0, _, err := openEbitenImage()
|
2016-05-16 16:35:58 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w, h := img0.Size()
|
|
|
|
img1, err := NewImage(w, h, FilterNearest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2016-08-03 04:05:10 +02:00
|
|
|
if err := img1.Fill(color.RGBA{0x01, 0x02, 0x03, 0x04}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2016-05-16 16:35:58 +02:00
|
|
|
op := &DrawImageOptions{}
|
|
|
|
op.CompositeMode = CompositeModeLighter
|
2016-08-03 04:05:10 +02:00
|
|
|
if err := img1.DrawImage(img0, op); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2018-03-20 19:42:58 +01:00
|
|
|
for j := 0; j < img1.Bounds().Size().Y; j++ {
|
2016-05-16 16:35:58 +02:00
|
|
|
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) {
|
2018-03-14 19:48:32 +01:00
|
|
|
img, _, err := openEbitenImage()
|
2016-05-16 16:35:58 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err := NewImageFromImage(img, FilterNearest); err != nil {
|
|
|
|
t.Errorf("NewImageFromImage returns error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2016-07-02 16:24:43 +02:00
|
|
|
|
|
|
|
func TestNewImageFromSubImage(t *testing.T) {
|
2018-03-14 19:48:32 +01:00
|
|
|
_, img, err := openEbitenImage()
|
2016-07-02 16:24:43 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-30 16:31:59 +02:00
|
|
|
|
|
|
|
type mutableRGBA struct {
|
|
|
|
r, g, b, a uint8
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mutableRGBA) RGBA() (r, g, b, a uint32) {
|
|
|
|
return uint32(c.r) * 0x101, uint32(c.g) * 0x101, uint32(c.b) * 0x101, uint32(c.a) * 0x101
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImageFill(t *testing.T) {
|
|
|
|
w, h := 10, 10
|
|
|
|
img, err := NewImage(w, h, FilterNearest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
clr := &mutableRGBA{0x80, 0x80, 0x80, 0x80}
|
|
|
|
if err := img.Fill(clr); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
clr.r = 0
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
|
|
|
got := img.At(i, j)
|
|
|
|
want := color.RGBA{0x80, 0x80, 0x80, 0x80}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("img At(%d, %d): got %#v; want %#v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-04 19:06:18 +01:00
|
|
|
|
2018-11-28 23:00:22 +01:00
|
|
|
// Issue #740
|
|
|
|
func TestImageClear(t *testing.T) {
|
|
|
|
const w, h = 128, 256
|
|
|
|
img, err := NewImage(w, h, FilterNearest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
img.Fill(color.White)
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
|
|
|
got := img.At(i, j)
|
|
|
|
want := color.RGBA{0xff, 0xff, 0xff, 0xff}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("img At(%d, %d): got %#v; want %#v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
img.Clear()
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
|
|
|
got := img.At(i, j)
|
|
|
|
want := color.RGBA{}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("img At(%d, %d): got %#v; want %#v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 17:48:49 +02:00
|
|
|
// Issue #317, #558, #724
|
2017-02-10 19:32:38 +01:00
|
|
|
func TestImageEdge(t *testing.T) {
|
|
|
|
const (
|
2018-10-25 17:48:49 +02:00
|
|
|
img0Width = 16
|
|
|
|
img0Height = 16
|
2018-11-08 17:31:26 +01:00
|
|
|
img0InnerWidth = 10
|
|
|
|
img0InnerHeight = 10
|
2018-10-25 17:48:49 +02:00
|
|
|
img0OffsetWidth = (img0Width - img0InnerWidth) / 2
|
|
|
|
img0OffsetHeight = (img0Height - img0InnerHeight) / 2
|
|
|
|
|
2017-02-10 19:32:38 +01:00
|
|
|
img1Width = 32
|
|
|
|
img1Height = 32
|
|
|
|
)
|
2018-02-02 18:33:39 +01:00
|
|
|
img0, _ := NewImage(img0Width, img0Height, FilterNearest)
|
2017-02-10 19:32:38 +01:00
|
|
|
pixels := make([]uint8, 4*img0Width*img0Height)
|
|
|
|
for j := 0; j < img0Height; j++ {
|
|
|
|
for i := 0; i < img0Width; i++ {
|
|
|
|
idx := 4 * (i + j*img0Width)
|
|
|
|
switch {
|
2018-10-25 17:48:49 +02:00
|
|
|
case img0OffsetWidth <= i && i < img0Width-img0OffsetWidth &&
|
2018-11-08 17:31:26 +01:00
|
|
|
img0OffsetHeight <= j && j < img0Height-img0OffsetHeight:
|
2017-02-10 19:32:38 +01:00
|
|
|
pixels[idx] = 0xff
|
|
|
|
pixels[idx+1] = 0
|
|
|
|
pixels[idx+2] = 0
|
|
|
|
pixels[idx+3] = 0xff
|
|
|
|
default:
|
|
|
|
pixels[idx] = 0
|
|
|
|
pixels[idx+1] = 0xff
|
|
|
|
pixels[idx+2] = 0
|
|
|
|
pixels[idx+3] = 0xff
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-02 18:33:39 +01:00
|
|
|
img0.ReplacePixels(pixels)
|
2018-03-10 19:02:03 +01:00
|
|
|
img1, _ := NewImage(img1Width, img1Height, FilterDefault)
|
2017-02-10 19:32:38 +01:00
|
|
|
red := color.RGBA{0xff, 0, 0, 0xff}
|
|
|
|
transparent := color.RGBA{0, 0, 0, 0}
|
2017-12-02 12:51:18 +01:00
|
|
|
|
2018-03-20 16:38:17 +01:00
|
|
|
angles := []float64{}
|
2018-05-13 16:00:06 +02:00
|
|
|
for a := 0; a < 1440; a++ {
|
|
|
|
angles = append(angles, float64(a)/1440*2*math.Pi)
|
2018-03-20 16:38:17 +01:00
|
|
|
}
|
2018-10-25 17:48:49 +02:00
|
|
|
for a := 0; a < 4096; a += 3 {
|
|
|
|
// a++ should be fine, but it takes long to test.
|
2018-05-13 16:00:06 +02:00
|
|
|
angles = append(angles, float64(a)/4096*2*math.Pi)
|
2018-03-20 16:38:17 +01:00
|
|
|
}
|
|
|
|
|
2018-11-08 17:31:26 +01:00
|
|
|
img0Sub := img0.SubImage(image.Rect(img0OffsetWidth, img0OffsetHeight, img0Width-img0OffsetWidth, img0Height-img0OffsetHeight)).(*Image)
|
2018-10-23 16:27:27 +02:00
|
|
|
|
2018-10-25 17:48:49 +02:00
|
|
|
for _, s := range []float64{1, 0.5, 0.25} {
|
|
|
|
for _, f := range []Filter{FilterNearest, FilterLinear} {
|
|
|
|
for _, a := range angles {
|
|
|
|
img1.Clear()
|
|
|
|
op := &DrawImageOptions{}
|
2018-11-08 17:31:26 +01:00
|
|
|
w, h := img0Sub.Size()
|
2018-10-25 17:48:49 +02:00
|
|
|
op.GeoM.Translate(-float64(w)/2, -float64(h)/2)
|
|
|
|
op.GeoM.Scale(s, s)
|
|
|
|
op.GeoM.Rotate(a)
|
|
|
|
op.GeoM.Translate(img1Width/2, img1Height/2)
|
|
|
|
op.Filter = f
|
2018-10-23 16:27:27 +02:00
|
|
|
img1.DrawImage(img0Sub, op)
|
2018-11-08 16:45:36 +01:00
|
|
|
allTransparent := true
|
2018-10-25 17:48:49 +02:00
|
|
|
for j := 0; j < img1Height; j++ {
|
|
|
|
for i := 0; i < img1Width; i++ {
|
|
|
|
c := img1.At(i, j)
|
|
|
|
if c == transparent {
|
2018-03-10 19:02:03 +01:00
|
|
|
continue
|
|
|
|
}
|
2018-11-08 16:45:36 +01:00
|
|
|
allTransparent = false
|
2018-10-25 17:48:49 +02:00
|
|
|
switch f {
|
|
|
|
case FilterNearest:
|
|
|
|
if c == red {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case FilterLinear:
|
2018-11-08 16:45:36 +01:00
|
|
|
if _, g, b, _ := c.RGBA(); g == 0 && b == 0 {
|
2018-10-25 17:48:49 +02:00
|
|
|
continue
|
|
|
|
}
|
2018-03-10 19:02:03 +01:00
|
|
|
}
|
2018-11-08 17:31:26 +01:00
|
|
|
t.Fatalf("img1.At(%d, %d) (filter: %d, scale: %f, angle: %f) want: red or transparent, got: %v", i, j, f, s, a, c)
|
2018-03-10 19:02:03 +01:00
|
|
|
}
|
2017-02-10 19:32:38 +01:00
|
|
|
}
|
2018-11-08 17:31:26 +01:00
|
|
|
if allTransparent {
|
2018-11-08 16:45:36 +01:00
|
|
|
t.Fatalf("img1 (filter: %d, scale: %f, angle: %f) is transparent but should not", f, s, a)
|
|
|
|
}
|
2017-02-10 19:32:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 15:28:14 +02:00
|
|
|
|
2017-08-23 17:11:08 +02:00
|
|
|
// Issue #419
|
|
|
|
func TestImageTooManyFill(t *testing.T) {
|
2017-12-02 14:25:02 +01:00
|
|
|
const width = 1024
|
2017-08-23 17:11:08 +02:00
|
|
|
|
2018-05-28 19:20:33 +02:00
|
|
|
indexToColor := func(index int) uint8 {
|
|
|
|
return uint8((17*index + 0x40) % 256)
|
|
|
|
}
|
|
|
|
|
2017-08-23 17:11:08 +02:00
|
|
|
src, _ := NewImage(1, 1, FilterNearest)
|
|
|
|
dst, _ := NewImage(width, 1, FilterNearest)
|
|
|
|
for i := 0; i < width; i++ {
|
2017-12-06 12:53:02 +01:00
|
|
|
c := indexToColor(i)
|
|
|
|
src.Fill(color.RGBA{c, c, c, 0xff})
|
2017-08-23 17:11:08 +02:00
|
|
|
op := &DrawImageOptions{}
|
|
|
|
op.GeoM.Translate(float64(i), 0)
|
|
|
|
dst.DrawImage(src, op)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < width; i++ {
|
2017-12-06 12:53:02 +01:00
|
|
|
c := indexToColor(i)
|
2018-08-01 18:10:06 +02:00
|
|
|
got := dst.At(i, 0).(color.RGBA)
|
2017-12-06 12:53:02 +01:00
|
|
|
want := color.RGBA{c, c, c, 0xff}
|
2018-02-03 09:26:44 +01:00
|
|
|
if !sameColors(got, want, 1) {
|
|
|
|
t.Errorf("dst.At(%d, %d): got %#v, want: %#v", i, 0, got, want)
|
2017-08-23 17:11:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:28:14 +02:00
|
|
|
func BenchmarkDrawImage(b *testing.B) {
|
|
|
|
img0, _ := NewImage(16, 16, FilterNearest)
|
|
|
|
img1, _ := NewImage(16, 16, FilterNearest)
|
|
|
|
op := &DrawImageOptions{}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
img0.DrawImage(img1, op)
|
|
|
|
}
|
|
|
|
}
|
2017-12-11 15:07:01 +01:00
|
|
|
|
2018-12-03 01:53:12 +01:00
|
|
|
func TestImageLinearGradiation(t *testing.T) {
|
|
|
|
img0, _ := NewImage(2, 2, FilterNearest)
|
|
|
|
img0.ReplacePixels([]byte{
|
|
|
|
0xff, 0x00, 0x00, 0xff,
|
|
|
|
0x00, 0xff, 0x00, 0xff,
|
|
|
|
0x00, 0x00, 0xff, 0xff,
|
|
|
|
0xff, 0xff, 0xff, 0xff,
|
|
|
|
})
|
|
|
|
|
|
|
|
const w, h = 32, 32
|
|
|
|
img1, _ := NewImage(w, h, FilterNearest)
|
|
|
|
op := &DrawImageOptions{}
|
|
|
|
op.GeoM.Scale(w, h)
|
|
|
|
op.GeoM.Translate(-w/4, -h/4)
|
|
|
|
op.Filter = FilterLinear
|
|
|
|
img1.DrawImage(img0, op)
|
|
|
|
|
|
|
|
for j := 1; j < h-1; j++ {
|
|
|
|
for i := 1; i < w-1; i++ {
|
|
|
|
c := img1.At(i, j).(color.RGBA)
|
|
|
|
if c.R == 0 || c.R == 0xff {
|
|
|
|
t.Errorf("img1.At(%d, %d).R must be in between 0x01 and 0xfe but %#v", i, j, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImageLinearEdges(t *testing.T) {
|
2018-04-15 18:50:53 +02:00
|
|
|
src, _ := NewImage(32, 32, FilterDefault)
|
|
|
|
dst, _ := NewImage(64, 64, FilterDefault)
|
2017-12-11 15:07:01 +01:00
|
|
|
src.Fill(color.RGBA{0, 0xff, 0, 0xff})
|
|
|
|
ebitenutil.DrawRect(src, 8, 8, 16, 16, color.RGBA{0xff, 0, 0, 0xff})
|
|
|
|
|
|
|
|
op := &DrawImageOptions{}
|
|
|
|
op.GeoM.Translate(8, 8)
|
|
|
|
op.GeoM.Scale(2, 2)
|
2018-04-15 18:50:53 +02:00
|
|
|
op.Filter = FilterLinear
|
2018-10-23 16:27:27 +02:00
|
|
|
dst.DrawImage(src.SubImage(image.Rect(8, 8, 24, 24)).(*Image), op)
|
2017-12-11 15:07:01 +01:00
|
|
|
|
|
|
|
for j := 0; j < 64; j++ {
|
|
|
|
for i := 0; i < 64; i++ {
|
2018-08-01 18:10:06 +02:00
|
|
|
c := dst.At(i, j).(color.RGBA)
|
2017-12-11 15:07:01 +01:00
|
|
|
got := c.G
|
|
|
|
want := uint8(0)
|
2018-02-03 09:00:57 +01:00
|
|
|
if abs(int(c.G)-int(want)) > 1 {
|
2017-12-11 15:07:01 +01:00
|
|
|
t.Errorf("dst At(%d, %d).G: got %#v, want: %#v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-13 16:25:35 +01:00
|
|
|
|
|
|
|
func TestImageOutside(t *testing.T) {
|
2017-12-13 16:49:53 +01:00
|
|
|
src, _ := NewImage(5, 10, FilterNearest) // internal texture size is 8x16.
|
2017-12-13 16:25:35 +01:00
|
|
|
dst, _ := NewImage(4, 4, FilterNearest)
|
|
|
|
src.Fill(color.RGBA{0xff, 0, 0, 0xff})
|
|
|
|
|
|
|
|
cases := []struct {
|
2017-12-14 03:52:11 +01:00
|
|
|
X, Y, Width, Height int
|
2017-12-13 16:25:35 +01:00
|
|
|
}{
|
2017-12-14 03:52:11 +01:00
|
|
|
{-4, -4, 4, 4},
|
|
|
|
{5, 0, 4, 4},
|
|
|
|
{0, 10, 4, 4},
|
|
|
|
{5, 10, 4, 4},
|
|
|
|
{8, 0, 4, 4},
|
|
|
|
{0, 16, 4, 4},
|
|
|
|
{8, 16, 4, 4},
|
|
|
|
{8, -4, 4, 4},
|
|
|
|
{-4, 16, 4, 4},
|
|
|
|
{5, 10, 0, 0},
|
|
|
|
{5, 10, -2, -2}, // non-well-formed rectangle
|
2017-12-13 16:25:35 +01:00
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
dst.Clear()
|
|
|
|
|
|
|
|
op := &DrawImageOptions{}
|
|
|
|
op.GeoM.Translate(0, 0)
|
2018-10-23 16:27:27 +02:00
|
|
|
dst.DrawImage(src.SubImage(image.Rectangle{
|
2017-12-14 03:52:11 +01:00
|
|
|
Min: image.Pt(c.X, c.Y),
|
|
|
|
Max: image.Pt(c.X+c.Width, c.Y+c.Height),
|
2018-10-23 16:27:27 +02:00
|
|
|
}).(*Image), op)
|
2017-12-13 16:25:35 +01:00
|
|
|
|
|
|
|
for j := 0; j < 4; j++ {
|
|
|
|
for i := 0; i < 4; i++ {
|
2018-08-01 18:10:06 +02:00
|
|
|
got := dst.At(i, j).(color.RGBA)
|
2017-12-13 16:25:35 +01:00
|
|
|
want := color.RGBA{0, 0, 0, 0}
|
|
|
|
if got != want {
|
2017-12-14 03:52:11 +01:00
|
|
|
t.Errorf("src(x: %d, y: %d, w: %d, h: %d), dst At(%d, %d): got %#v, want: %#v", c.X, c.Y, c.Width, c.Height, i, j, got, want)
|
2017-12-13 16:25:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-15 17:19:59 +01:00
|
|
|
|
|
|
|
func TestImageOutsideUpperLeft(t *testing.T) {
|
|
|
|
src, _ := NewImage(4, 4, FilterNearest)
|
|
|
|
dst1, _ := NewImage(16, 16, FilterNearest)
|
|
|
|
dst2, _ := NewImage(16, 16, FilterNearest)
|
|
|
|
src.Fill(color.RGBA{0xff, 0, 0, 0xff})
|
|
|
|
|
|
|
|
op := &DrawImageOptions{}
|
|
|
|
op.GeoM.Rotate(math.Pi / 4)
|
2018-10-23 16:27:27 +02:00
|
|
|
dst1.DrawImage(src.SubImage(image.Rect(-4, -4, 8, 8)).(*Image), op)
|
2017-12-15 17:19:59 +01:00
|
|
|
|
|
|
|
op = &DrawImageOptions{}
|
2018-10-23 16:27:27 +02:00
|
|
|
// The outside part of the source rect is just ignored.
|
|
|
|
// This behavior was changed as of 1.9.0-alpha.
|
|
|
|
// op.GeoM.Translate(4, 4)
|
2017-12-15 17:19:59 +01:00
|
|
|
op.GeoM.Rotate(math.Pi / 4)
|
|
|
|
dst2.DrawImage(src, op)
|
|
|
|
|
|
|
|
for j := 0; j < 16; j++ {
|
|
|
|
for i := 0; i < 16; i++ {
|
2018-08-01 18:10:06 +02:00
|
|
|
got := dst1.At(i, j).(color.RGBA)
|
|
|
|
want := dst2.At(i, j).(color.RGBA)
|
2017-12-15 17:19:59 +01:00
|
|
|
if got != want {
|
|
|
|
t.Errorf("got: dst1.At(%d, %d): %#v, want: dst2.At(%d, %d): %#v", i, j, got, i, j, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-02 19:52:47 +01:00
|
|
|
|
2018-03-01 18:56:53 +01:00
|
|
|
func TestImageSize(t *testing.T) {
|
|
|
|
const (
|
|
|
|
w = 17
|
|
|
|
h = 31
|
|
|
|
)
|
|
|
|
img, _ := NewImage(w, h, FilterDefault)
|
|
|
|
gotW, gotH := img.Size()
|
|
|
|
if gotW != w {
|
|
|
|
t.Errorf("got: %d, want: %d", gotW, w)
|
|
|
|
}
|
|
|
|
if gotH != h {
|
|
|
|
t.Errorf("got: %d, want: %d", gotH, h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-03 09:20:07 +01:00
|
|
|
func TestImageSize1(t *testing.T) {
|
|
|
|
src, _ := NewImage(1, 1, FilterNearest)
|
|
|
|
dst, _ := NewImage(1, 1, FilterNearest)
|
|
|
|
src.Fill(color.White)
|
|
|
|
dst.DrawImage(src, nil)
|
2018-08-01 18:10:06 +02:00
|
|
|
got := src.At(0, 0).(color.RGBA)
|
|
|
|
want := color.RGBA{0xff, 0xff, 0xff, 0xff}
|
2018-02-03 09:20:07 +01:00
|
|
|
if !sameColors(got, want, 1) {
|
|
|
|
t.Errorf("got: %#v, want: %#v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestImageSize4096(t *testing.T) {
|
2018-02-02 19:52:47 +01:00
|
|
|
src, _ := NewImage(4096, 4096, FilterNearest)
|
|
|
|
dst, _ := NewImage(4096, 4096, FilterNearest)
|
|
|
|
pix := make([]byte, 4096*4096*4)
|
2018-02-03 07:39:17 +01:00
|
|
|
for i := 0; i < 4096; i++ {
|
|
|
|
j := 4095
|
|
|
|
idx := 4 * (i + j*4096)
|
|
|
|
pix[idx] = uint8(i + j)
|
|
|
|
pix[idx+1] = uint8((i + j) >> 8)
|
|
|
|
pix[idx+2] = uint8((i + j) >> 16)
|
|
|
|
pix[idx+3] = 0xff
|
|
|
|
}
|
|
|
|
for j := 0; j < 4096; j++ {
|
|
|
|
i := 4095
|
|
|
|
idx := 4 * (i + j*4096)
|
|
|
|
pix[idx] = uint8(i + j)
|
|
|
|
pix[idx+1] = uint8((i + j) >> 8)
|
|
|
|
pix[idx+2] = uint8((i + j) >> 16)
|
|
|
|
pix[idx+3] = 0xff
|
2018-02-02 19:52:47 +01:00
|
|
|
}
|
|
|
|
src.ReplacePixels(pix)
|
|
|
|
dst.DrawImage(src, nil)
|
2018-02-03 07:39:17 +01:00
|
|
|
for i := 4095; i < 4096; i++ {
|
|
|
|
j := 4095
|
2018-08-01 18:10:06 +02:00
|
|
|
got := dst.At(i, j).(color.RGBA)
|
2018-02-03 07:39:17 +01:00
|
|
|
want := color.RGBA{uint8(i + j), uint8((i + j) >> 8), uint8((i + j) >> 16), 0xff}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("At(%d, %d): got: %#v, want: %#v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
2018-02-02 19:52:47 +01:00
|
|
|
for j := 4095; j < 4096; j++ {
|
2018-02-03 07:39:17 +01:00
|
|
|
i := 4095
|
2018-08-01 18:10:06 +02:00
|
|
|
got := dst.At(i, j).(color.RGBA)
|
2018-02-03 07:39:17 +01:00
|
|
|
want := color.RGBA{uint8(i + j), uint8((i + j) >> 8), uint8((i + j) >> 16), 0xff}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("At(%d, %d): got: %#v, want: %#v", i, j, got, want)
|
2018-02-02 19:52:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-25 13:54:35 +01:00
|
|
|
|
|
|
|
func TestImageCopy(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r == nil {
|
2018-11-28 22:24:04 +01:00
|
|
|
t.Errorf("copying image and using it must panic")
|
2018-02-25 13:54:35 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
img0, _ := NewImage(256, 256, FilterDefault)
|
|
|
|
img1 := *img0
|
|
|
|
img1.Fill(color.Transparent)
|
|
|
|
}
|
2018-05-13 16:47:59 +02:00
|
|
|
|
|
|
|
func TestImageStretch(t *testing.T) {
|
|
|
|
img0, _ := NewImage(16, 17, FilterDefault)
|
|
|
|
|
|
|
|
pix := make([]byte, 4*16*17)
|
|
|
|
for i := 0; i < 16*16; i++ {
|
|
|
|
pix[4*i] = 0xff
|
|
|
|
pix[4*i+3] = 0xff
|
|
|
|
}
|
|
|
|
for i := 0; i < 16; i++ {
|
|
|
|
pix[4*(16*16+i)+1] = 0xff
|
|
|
|
pix[4*(16*16+i)+3] = 0xff
|
|
|
|
}
|
|
|
|
img0.ReplacePixels(pix)
|
|
|
|
|
|
|
|
// TODO: 4096 doesn't pass on MacBook Pro (#611).
|
2018-09-27 18:13:11 +02:00
|
|
|
const h = 4000
|
2018-05-13 16:47:59 +02:00
|
|
|
img1, _ := NewImage(16, h, FilterDefault)
|
|
|
|
for i := 1; i < h; i++ {
|
|
|
|
img1.Clear()
|
|
|
|
op := &DrawImageOptions{}
|
|
|
|
op.GeoM.Scale(1, float64(i)/16)
|
2018-10-23 16:27:27 +02:00
|
|
|
img1.DrawImage(img0.SubImage(image.Rect(0, 0, 16, 16)).(*Image), op)
|
2018-05-13 16:47:59 +02:00
|
|
|
for j := -1; j <= 1; j++ {
|
|
|
|
got := img1.At(0, i+j).(color.RGBA)
|
|
|
|
want := color.RGBA{}
|
|
|
|
if j < 0 {
|
|
|
|
want = color.RGBA{0xff, 0, 0, 0xff}
|
|
|
|
}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("At(%d, %d) (i=%d): got: %#v, want: %#v", 0, i+j, i, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-28 19:20:33 +02:00
|
|
|
|
2018-11-08 17:58:41 +01:00
|
|
|
func TestImageSprites(t *testing.T) {
|
2018-05-28 19:20:33 +02:00
|
|
|
const (
|
|
|
|
width = 512
|
|
|
|
height = 512
|
|
|
|
)
|
|
|
|
|
|
|
|
src, _ := NewImage(4, 4, FilterNearest)
|
|
|
|
src.Fill(color.RGBA{0xff, 0xff, 0xff, 0xff})
|
|
|
|
dst, _ := NewImage(width, height, FilterNearest)
|
|
|
|
for j := 0; j < height/4; j++ {
|
|
|
|
for i := 0; i < width/4; i++ {
|
|
|
|
op := &DrawImageOptions{}
|
|
|
|
op.GeoM.Translate(float64(i*4), float64(j*4))
|
|
|
|
dst.DrawImage(src, op)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for j := 0; j < height/4; j++ {
|
|
|
|
for i := 0; i < width/4; i++ {
|
2018-08-01 18:10:06 +02:00
|
|
|
got := dst.At(i*4, j*4).(color.RGBA)
|
2018-05-28 19:20:33 +02:00
|
|
|
want := color.RGBA{0xff, 0xff, 0xff, 0xff}
|
|
|
|
if !sameColors(got, want, 1) {
|
|
|
|
t.Errorf("dst.At(%d, %d): got %#v, want: %#v", i*4, j*4, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-01 17:42:43 +02:00
|
|
|
|
2018-11-08 17:58:41 +01:00
|
|
|
func TestImageMipmap(t *testing.T) {
|
2018-08-01 17:42:43 +02:00
|
|
|
src, _, err := openEbitenImage()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w, h := src.Size()
|
|
|
|
|
2018-08-02 17:02:49 +02:00
|
|
|
l1, _ := NewImage(w/2, h/2, FilterDefault)
|
2018-08-01 17:42:43 +02:00
|
|
|
op := &DrawImageOptions{}
|
|
|
|
op.GeoM.Scale(1/2.0, 1/2.0)
|
|
|
|
op.Filter = FilterLinear
|
|
|
|
l1.DrawImage(src, op)
|
|
|
|
|
|
|
|
l1w, l1h := l1.Size()
|
2018-08-02 17:02:49 +02:00
|
|
|
l2, _ := NewImage(l1w/2, l1h/2, FilterDefault)
|
2018-08-01 17:42:43 +02:00
|
|
|
op = &DrawImageOptions{}
|
|
|
|
op.GeoM.Scale(1/2.0, 1/2.0)
|
|
|
|
op.Filter = FilterLinear
|
|
|
|
l2.DrawImage(l1, op)
|
|
|
|
|
|
|
|
gotDst, _ := NewImage(w, h, FilterDefault)
|
|
|
|
op = &DrawImageOptions{}
|
|
|
|
op.GeoM.Scale(1/5.0, 1/5.0)
|
|
|
|
op.Filter = FilterLinear
|
|
|
|
gotDst.DrawImage(src, op)
|
|
|
|
|
|
|
|
wantDst, _ := NewImage(w, h, FilterDefault)
|
|
|
|
op = &DrawImageOptions{}
|
|
|
|
op.GeoM.Scale(4.0/5.0, 4.0/5.0)
|
|
|
|
op.Filter = FilterLinear
|
|
|
|
wantDst.DrawImage(l2, op)
|
|
|
|
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < h; i++ {
|
|
|
|
got := gotDst.At(i, j).(color.RGBA)
|
|
|
|
want := wantDst.At(i, j).(color.RGBA)
|
|
|
|
if !sameColors(got, want, 1) {
|
|
|
|
t.Errorf("At(%d, %d): got: %#v, want: %#v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-10 19:44:51 +02:00
|
|
|
|
|
|
|
// Issue #710
|
2018-11-08 17:58:41 +01:00
|
|
|
func TestImageMipmapColor(t *testing.T) {
|
2018-10-10 19:44:51 +02:00
|
|
|
img0, _ := NewImage(256, 256, FilterDefault)
|
|
|
|
img1, _ := NewImage(128, 128, FilterDefault)
|
|
|
|
img1.Fill(color.White)
|
|
|
|
|
|
|
|
for i := 0; i < 8; i++ {
|
|
|
|
img0.Clear()
|
|
|
|
|
|
|
|
s := 1 - float64(i)/8
|
|
|
|
|
|
|
|
op := &DrawImageOptions{}
|
|
|
|
op.Filter = FilterLinear
|
|
|
|
op.GeoM.Scale(s, s)
|
|
|
|
op.ColorM.Scale(1, 1, 0, 1)
|
|
|
|
img0.DrawImage(img1, op)
|
|
|
|
|
|
|
|
op.GeoM.Translate(128, 0)
|
|
|
|
op.ColorM.Reset()
|
|
|
|
op.ColorM.Scale(0, 1, 1, 1)
|
|
|
|
img0.DrawImage(img1, op)
|
|
|
|
|
|
|
|
want := color.RGBA{0, 0xff, 0xff, 0xff}
|
|
|
|
got := img0.At(128, 0)
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("want: %#v, got: %#v", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-25 18:43:00 +02:00
|
|
|
|
|
|
|
// Issue #725
|
|
|
|
func TestImageMiamapAndDrawTriangle(t *testing.T) {
|
|
|
|
img0, _ := NewImage(32, 32, FilterDefault)
|
|
|
|
img1, _ := NewImage(128, 128, FilterDefault)
|
|
|
|
img2, _ := NewImage(128, 128, FilterDefault)
|
|
|
|
|
|
|
|
// Fill img1 red and create img1's mipmap
|
|
|
|
img1.Fill(color.RGBA{0xff, 0, 0, 0xff})
|
|
|
|
op := &DrawImageOptions{}
|
|
|
|
op.GeoM.Scale(0.25, 0.25)
|
|
|
|
op.Filter = FilterLinear
|
|
|
|
img0.DrawImage(img1, op)
|
|
|
|
|
|
|
|
// Call DrawTriangle on img1 and fill it with green
|
|
|
|
img2.Fill(color.RGBA{0, 0xff, 0, 0xff})
|
|
|
|
vs := []Vertex{
|
|
|
|
{
|
|
|
|
DstX: 0,
|
|
|
|
DstY: 0,
|
|
|
|
SrcX: 0,
|
|
|
|
SrcY: 0,
|
|
|
|
ColorR: 1,
|
|
|
|
ColorG: 1,
|
|
|
|
ColorB: 1,
|
|
|
|
ColorA: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DstX: 128,
|
|
|
|
DstY: 0,
|
|
|
|
SrcX: 128,
|
|
|
|
SrcY: 0,
|
|
|
|
ColorR: 1,
|
|
|
|
ColorG: 1,
|
|
|
|
ColorB: 1,
|
|
|
|
ColorA: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DstX: 0,
|
|
|
|
DstY: 128,
|
|
|
|
SrcX: 0,
|
|
|
|
SrcY: 128,
|
|
|
|
ColorR: 1,
|
|
|
|
ColorG: 1,
|
|
|
|
ColorB: 1,
|
|
|
|
ColorA: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DstX: 128,
|
|
|
|
DstY: 128,
|
|
|
|
SrcX: 128,
|
|
|
|
SrcY: 128,
|
|
|
|
ColorR: 1,
|
|
|
|
ColorG: 1,
|
|
|
|
ColorB: 1,
|
|
|
|
ColorA: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
img1.DrawTriangles(vs, []uint16{0, 1, 2, 1, 2, 3}, img2, nil)
|
|
|
|
|
|
|
|
// Draw img1 (green) again. Confirm mipmap is correctly updated.
|
|
|
|
img0.Clear()
|
|
|
|
op = &DrawImageOptions{}
|
|
|
|
op.GeoM.Scale(0.25, 0.25)
|
|
|
|
op.Filter = FilterLinear
|
|
|
|
img0.DrawImage(img1, op)
|
|
|
|
|
|
|
|
w, h := img0.Size()
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
|
|
|
c := img0.At(i, j).(color.RGBA)
|
|
|
|
if c.R != 0 {
|
|
|
|
t.Errorf("img0.At(%d, %d): red want %d got %d", i, j, 0, c.R)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-23 16:27:27 +02:00
|
|
|
|
|
|
|
func TestImageSubImageAt(t *testing.T) {
|
|
|
|
img, _ := NewImage(16, 16, FilterDefault)
|
|
|
|
img.Fill(color.RGBA{0xff, 0, 0, 0xff})
|
|
|
|
|
|
|
|
got := img.SubImage(image.Rect(1, 1, 16, 16)).At(0, 0).(color.RGBA)
|
|
|
|
want := color.RGBA{}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
got = img.SubImage(image.Rect(1, 1, 16, 16)).At(1, 1).(color.RGBA)
|
|
|
|
want = color.RGBA{0xff, 0, 0, 0xff}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 20:22:19 +01:00
|
|
|
|
|
|
|
func TestImageSubImageSize(t *testing.T) {
|
|
|
|
img, _ := NewImage(16, 16, FilterDefault)
|
|
|
|
img.Fill(color.RGBA{0xff, 0, 0, 0xff})
|
|
|
|
|
|
|
|
got, _ := img.SubImage(image.Rect(1, 1, 16, 16)).(*Image).Size()
|
|
|
|
want := 15
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2018-12-01 20:44:06 +01:00
|
|
|
|
|
|
|
func TestImageDrawImmediately(t *testing.T) {
|
|
|
|
const w, h = 16, 16
|
|
|
|
img0, _ := NewImage(w, h, FilterDefault)
|
|
|
|
img1, _ := NewImage(w, h, FilterDefault)
|
|
|
|
// Do not manipulate img0 here.
|
|
|
|
|
|
|
|
img0.Fill(color.RGBA{0xff, 0, 0, 0xff})
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
|
|
|
got := img0.At(i, j).(color.RGBA)
|
|
|
|
want := color.RGBA{0xff, 0, 0, 0xff}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("img0.At(%d, %d): got %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
img0.DrawImage(img1, nil)
|
|
|
|
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
|
|
|
got := img0.At(i, j).(color.RGBA)
|
|
|
|
want := color.RGBA{0xff, 0, 0, 0xff}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("img0.At(%d, %d): got %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|